blob: 4abceb4d9a89218348cc943971aacb6eeb02cc2b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#include "vim.h"
11
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010012static void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize, long Prenum);
13static void win_init(win_T *newp, win_T *oldp, int flags);
14static void win_init_some(win_T *newp, win_T *oldp);
15static void frame_comp_pos(frame_T *topfrp, int *row, int *col);
16static void frame_setheight(frame_T *curfrp, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010017static void frame_setwidth(frame_T *curfrp, int width);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010018static void win_exchange(long);
19static void win_rotate(int, int);
20static void win_totop(int size, int flags);
21static void win_equal_rec(win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010022static win_T *win_free_mem(win_T *win, int *dirp, tabpage_T *tp);
23static frame_T *win_altframe(win_T *win, tabpage_T *tp);
24static tabpage_T *alt_tabpage(void);
25static win_T *frame2win(frame_T *frp);
26static int frame_has_win(frame_T *frp, win_T *wp);
27static void frame_new_height(frame_T *topfrp, int height, int topfirst, int wfh);
28static int frame_fixed_height(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010029static int frame_fixed_width(frame_T *frp);
30static void frame_add_statusline(frame_T *frp);
31static void frame_new_width(frame_T *topfrp, int width, int leftfirst, int wfw);
32static void frame_add_vsep(frame_T *frp);
33static int frame_minwidth(frame_T *topfrp, win_T *next_curwin);
34static void frame_fix_width(win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010035static int win_alloc_firstwin(win_T *oldwin);
36static void new_frame(win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010037static tabpage_T *alloc_tabpage(void);
38static int leave_tabpage(buf_T *new_curbuf, int trigger_leave_autocmds);
39static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds);
40static void frame_fix_height(win_T *wp);
41static int frame_minheight(frame_T *topfrp, win_T *next_curwin);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020042static int may_open_tabpage(void);
Bram Moolenaarc917da42016-07-19 22:31:36 +020043static void win_enter_ext(win_T *wp, int undo_sync, int no_curwin, int trigger_new_autocmds, int trigger_enter_autocmds, int trigger_leave_autocmds);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010044static void win_free(win_T *wp, tabpage_T *tp);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020045static int win_unlisted(win_T *wp);
46static void win_append(win_T *after, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010047static void frame_append(frame_T *after, frame_T *frp);
48static void frame_insert(frame_T *before, frame_T *frp);
49static void frame_remove(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010050static void win_goto_ver(int up, long count);
51static void win_goto_hor(int left, long count);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void frame_add_height(frame_T *frp, int n);
53static void last_status_rec(frame_T *fr, int statusline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010055static void make_snapshot_rec(frame_T *fr, frame_T **frp);
56static void clear_snapshot(tabpage_T *tp, int idx);
57static void clear_snapshot_rec(frame_T *fr);
58static int check_snapshot_rec(frame_T *sn, frame_T *fr);
59static win_T *restore_snapshot_rec(frame_T *sn, frame_T *fr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010061static int frame_check_height(frame_T *topfrp, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010062static int frame_check_width(frame_T *topfrp, int width);
Bram Moolenaarb893ac22013-06-26 14:04:47 +020063
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static win_T *win_alloc(win_T *after, int hidden);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000066#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
Bram Moolenaar4033c552017-09-16 20:54:51 +020068#define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000069
70static char *m_onlyone = N_("Already only one window");
71
Bram Moolenaar1417c762019-07-27 17:31:36 +020072// When non-zero splitting a window is forbidden. Used to avoid that nasty
73// autocommands mess up the window structure.
74static int split_disallowed = 0;
75
76// #define WIN_DEBUG
77#ifdef WIN_DEBUG
78/*
79 * Call this method to log the current window layout.
80 */
81 static void
82log_frame_layout(frame_T *frame)
83{
84 ch_log(NULL, "layout %s, wi: %d, he: %d, wwi: %d, whe: %d, id: %d",
85 frame->fr_layout == FR_LEAF ? "LEAF"
86 : frame->fr_layout == FR_ROW ? "ROW" : "COL",
87 frame->fr_width,
88 frame->fr_height,
89 frame->fr_win == NULL ? -1 : frame->fr_win->w_width,
90 frame->fr_win == NULL ? -1 : frame->fr_win->w_height,
91 frame->fr_win == NULL ? -1 : frame->fr_win->w_id);
92 if (frame->fr_child != NULL)
93 {
94 ch_log(NULL, "children");
95 log_frame_layout(frame->fr_child);
96 if (frame->fr_next != NULL)
97 ch_log(NULL, "END of children");
98 }
99 if (frame->fr_next != NULL)
100 log_frame_layout(frame->fr_next);
101}
102#endif
103
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104/*
Bram Moolenaar72e83c12019-02-22 16:09:52 +0100105 * All CTRL-W window commands are handled here, called from normal_cmd().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 */
107 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100108do_window(
109 int nchar,
110 long Prenum,
111 int xchar) /* extra char from ":wincmd gx" or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112{
113 long Prenum1;
114 win_T *wp;
115#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
116 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000117 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#endif
119#ifdef FEAT_FIND_ID
120 int type = FIND_DEFINE;
121 int len;
122#endif
123 char_u cbuf[40];
124
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +0200125 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200126 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127
128#ifdef FEAT_CMDWIN
Bram Moolenaar6f470022018-04-10 18:47:20 +0200129# define CHECK_CMDWIN \
130 do { \
131 if (cmdwin_type != 0) \
132 { \
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100133 emsg(_(e_cmdwin)); \
Bram Moolenaar6f470022018-04-10 18:47:20 +0200134 return; \
135 } \
136 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#else
Bram Moolenaar6f470022018-04-10 18:47:20 +0200138# define CHECK_CMDWIN do { /**/ } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
140
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200141 Prenum1 = Prenum == 0 ? 1 : Prenum;
142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 switch (nchar)
144 {
145/* split current window in two parts, horizontally */
146 case 'S':
147 case Ctrl_S:
148 case 's':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200149 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000151#ifdef FEAT_QUICKFIX
152 /* When splitting the quickfix window open a new buffer in it,
153 * don't replicate the quickfix buffer. */
154 if (bt_quickfix(curbuf))
155 goto newwindow;
156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#ifdef FEAT_GUI
158 need_mouse_correct = TRUE;
159#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200160 (void)win_split((int)Prenum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 break;
162
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163/* split current window in two parts, vertically */
164 case Ctrl_V:
165 case 'v':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200166 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100168#ifdef FEAT_QUICKFIX
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000169 /* When splitting the quickfix window open a new buffer in it,
170 * don't replicate the quickfix buffer. */
171 if (bt_quickfix(curbuf))
172 goto newwindow;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100173#endif
174#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100176#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200177 (void)win_split((int)Prenum, WSP_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179
180/* split current window and edit alternate file */
181 case Ctrl_HAT:
182 case '^':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200183 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100185
186 if (buflist_findnr(Prenum == 0
187 ? curwin->w_alt_fnum : Prenum) == NULL)
188 {
189 if (Prenum == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100190 emsg(_(e_noalt));
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100191 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100192 semsg(_("E92: Buffer %ld not found"), Prenum);
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100193 break;
194 }
195
196 if (!curbuf_locked() && win_split(0, 0) == OK)
197 (void)buflist_getfile(
198 Prenum == 0 ? curwin->w_alt_fnum : Prenum,
199 (linenr_T)0, GETF_ALT, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 break;
201
202/* open new window */
203 case Ctrl_N:
204 case 'n':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200205 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000207#ifdef FEAT_QUICKFIX
208newwindow:
209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000211 /* window height */
212 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 else
214 cbuf[0] = NUL;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100215#if defined(FEAT_QUICKFIX)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000216 if (nchar == 'v' || nchar == Ctrl_V)
217 STRCAT(cbuf, "v");
218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 STRCAT(cbuf, "new");
220 do_cmdline_cmd(cbuf);
221 break;
222
223/* quit current window */
224 case Ctrl_Q:
225 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100227 cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100228 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 break;
230
231/* close current window */
232 case Ctrl_C:
233 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100235 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100236 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 break;
238
Bram Moolenaar4033c552017-09-16 20:54:51 +0200239#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240/* close preview window */
241 case Ctrl_Z:
242 case 'z':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200243 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 do_cmdline_cmd((char_u *)"pclose");
246 break;
247
248/* cursor to preview window */
249 case 'P':
Bram Moolenaar29323592016-07-24 22:04:11 +0200250 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 if (wp->w_p_pvw)
252 break;
253 if (wp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100254 emsg(_("E441: There is no preview window"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 else
256 win_goto(wp);
257 break;
258#endif
259
260/* close all but current window */
261 case Ctrl_O:
262 case 'o':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200263 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100265 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100266 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 break;
268
269/* cursor to next window with wrap around */
270 case Ctrl_W:
271 case 'w':
272/* cursor to previous window with wrap around */
273 case 'W':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200274 CHECK_CMDWIN;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +0100275 if (ONE_WINDOW && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 beep_flush();
277 else
278 {
279 if (Prenum) /* go to specified window */
280 {
281 for (wp = firstwin; --Prenum > 0; )
282 {
283 if (wp->w_next == NULL)
284 break;
285 else
286 wp = wp->w_next;
287 }
288 }
289 else
290 {
291 if (nchar == 'W') /* go to previous window */
292 {
293 wp = curwin->w_prev;
294 if (wp == NULL)
295 wp = lastwin; /* wrap around */
296 }
297 else /* go to next window */
298 {
299 wp = curwin->w_next;
300 if (wp == NULL)
301 wp = firstwin; /* wrap around */
302 }
303 }
304 win_goto(wp);
305 }
306 break;
307
308/* cursor to window below */
309 case 'j':
310 case K_DOWN:
311 case Ctrl_J:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200312 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 win_goto_ver(FALSE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 break;
315
316/* cursor to window above */
317 case 'k':
318 case K_UP:
319 case Ctrl_K:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200320 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 win_goto_ver(TRUE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 break;
323
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324/* cursor to left window */
325 case 'h':
326 case K_LEFT:
327 case Ctrl_H:
328 case K_BS:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200329 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 win_goto_hor(TRUE, Prenum1);
331 break;
332
333/* cursor to right window */
334 case 'l':
335 case K_RIGHT:
336 case Ctrl_L:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200337 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 win_goto_hor(FALSE, Prenum1);
339 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000341/* move window to new tab page */
342 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000343 if (one_window())
Bram Moolenaar32526b32019-01-19 17:43:09 +0100344 msg(_(m_onlyone));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000345 else
346 {
347 tabpage_T *oldtab = curtab;
348 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000349
350 /* First create a new tab with the window, then go back to
351 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000352 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000353 if (win_new_tabpage((int)Prenum) == OK
354 && valid_tabpage(oldtab))
355 {
356 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200357 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000358 if (curwin == wp)
359 win_close(curwin, FALSE);
360 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200361 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000362 }
363 }
364 break;
365
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366/* cursor to top-left window */
367 case 't':
368 case Ctrl_T:
369 win_goto(firstwin);
370 break;
371
372/* cursor to bottom-right window */
373 case 'b':
374 case Ctrl_B:
375 win_goto(lastwin);
376 break;
377
378/* cursor to last accessed (previous) window */
379 case 'p':
380 case Ctrl_P:
Bram Moolenaar3dda7db2016-04-03 21:22:58 +0200381 if (!win_valid(prevwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 beep_flush();
383 else
384 win_goto(prevwin);
385 break;
386
387/* exchange current and next window */
388 case 'x':
389 case Ctrl_X:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200390 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 win_exchange(Prenum);
392 break;
393
394/* rotate windows downwards */
395 case Ctrl_R:
396 case 'r':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200397 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 win_rotate(FALSE, (int)Prenum1); /* downwards */
400 break;
401
402/* rotate windows upwards */
403 case 'R':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200404 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 win_rotate(TRUE, (int)Prenum1); /* upwards */
407 break;
408
409/* move window to the very top/bottom/left/right */
410 case 'K':
411 case 'J':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 case 'H':
413 case 'L':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200414 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 win_totop((int)Prenum,
416 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
417 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
418 break;
419
420/* make all windows the same height */
421 case '=':
422#ifdef FEAT_GUI
423 need_mouse_correct = TRUE;
424#endif
425 win_equal(NULL, FALSE, 'b');
426 break;
427
428/* increase current window height */
429 case '+':
430#ifdef FEAT_GUI
431 need_mouse_correct = TRUE;
432#endif
433 win_setheight(curwin->w_height + (int)Prenum1);
434 break;
435
436/* decrease current window height */
437 case '-':
438#ifdef FEAT_GUI
439 need_mouse_correct = TRUE;
440#endif
441 win_setheight(curwin->w_height - (int)Prenum1);
442 break;
443
444/* set current window height */
445 case Ctrl__:
446 case '_':
447#ifdef FEAT_GUI
448 need_mouse_correct = TRUE;
449#endif
450 win_setheight(Prenum ? (int)Prenum : 9999);
451 break;
452
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453/* increase current window width */
454 case '>':
455#ifdef FEAT_GUI
456 need_mouse_correct = TRUE;
457#endif
458 win_setwidth(curwin->w_width + (int)Prenum1);
459 break;
460
461/* decrease current window width */
462 case '<':
463#ifdef FEAT_GUI
464 need_mouse_correct = TRUE;
465#endif
466 win_setwidth(curwin->w_width - (int)Prenum1);
467 break;
468
469/* set current window width */
470 case '|':
471#ifdef FEAT_GUI
472 need_mouse_correct = TRUE;
473#endif
474 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
475 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476
477/* jump to tag and split window if tag exists (in preview window) */
478#if defined(FEAT_QUICKFIX)
479 case '}':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200480 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 if (Prenum)
482 g_do_tagpreview = Prenum;
483 else
484 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200486 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 case ']':
488 case Ctrl_RSB:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200489 CHECK_CMDWIN;
Bram Moolenaard355c502014-09-23 13:48:43 +0200490 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 if (Prenum)
492 postponed_split = Prenum;
493 else
494 postponed_split = -1;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200495#ifdef FEAT_QUICKFIX
Bram Moolenaar56095e12014-10-09 10:44:37 +0200496 if (nchar != '}')
497 g_do_tagpreview = 0;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499
Bram Moolenaard355c502014-09-23 13:48:43 +0200500 /* Execute the command right here, required when "wincmd ]"
501 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 do_nv_ident(Ctrl_RSB, NUL);
503 break;
504
505#ifdef FEAT_SEARCHPATH
506/* edit file name under cursor in a new window */
507 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000508 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000510wingotofile:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200511 CHECK_CMDWIN;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000512
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000513 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (ptr != NULL)
515 {
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200516 tabpage_T *oldtab = curtab;
517 win_T *oldwin = curwin;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000518# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000520# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 setpcmark();
522 if (win_split(0, 0) == OK)
523 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200524 RESET_BINDING(curwin);
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200525 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
526 ECMD_HIDE, NULL) == FAIL)
527 {
528 /* Failed to open the file, close the window
529 * opened for it. */
530 win_close(curwin, FALSE);
531 goto_tabpage_win(oldtab, oldwin);
532 }
533 else if (nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000534 {
535 curwin->w_cursor.lnum = lnum;
536 check_cursor_lnum();
537 beginline(BL_SOL | BL_FIX);
538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 }
540 vim_free(ptr);
541 }
542 break;
543#endif
544
545#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000546/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 * new window -- webb
548 */
549 case 'i': /* Go to any match */
550 case Ctrl_I:
551 type = FIND_ANY;
552 /* FALLTHROUGH */
553 case 'd': /* Go to definition, using 'define' */
554 case Ctrl_D:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200555 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
557 break;
558 find_pattern_in_path(ptr, 0, len, TRUE,
559 Prenum == 0 ? TRUE : FALSE, type,
560 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
561 curwin->w_set_curswant = TRUE;
562 break;
563#endif
564
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200565/* Quickfix window only: view the result under the cursor in a new split. */
566#if defined(FEAT_QUICKFIX)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000567 case K_KENTER:
568 case CAR:
Bram Moolenaar05159a02005-02-26 23:04:13 +0000569 if (bt_quickfix(curbuf))
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200570 qf_view_result(TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000571 break;
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200572#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000573
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574/* CTRL-W g extended commands */
575 case 'g':
576 case Ctrl_G:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200577 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578#ifdef USE_ON_FLY_SCROLL
579 dont_scroll = TRUE; /* disallow scrolling here */
580#endif
581 ++no_mapping;
582 ++allow_keys; /* no mapping for xchar, but allow key codes */
583 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000584 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 --no_mapping;
587 --allow_keys;
588#ifdef FEAT_CMDL_INFO
589 (void)add_to_showcmd(xchar);
590#endif
591 switch (xchar)
592 {
593#if defined(FEAT_QUICKFIX)
594 case '}':
595 xchar = Ctrl_RSB;
596 if (Prenum)
597 g_do_tagpreview = Prenum;
598 else
599 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200601 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 case ']':
603 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200604 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 if (Prenum)
606 postponed_split = Prenum;
607 else
608 postponed_split = -1;
609
610 /* Execute the command right here, required when
611 * "wincmd g}" was used in a function. */
612 do_nv_ident('g', xchar);
613 break;
614
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000615#ifdef FEAT_SEARCHPATH
616 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000617 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100618 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000619 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000620 goto wingotofile;
621#endif
Bram Moolenaar72e83c12019-02-22 16:09:52 +0100622 case 't': // CTRL-W gt: go to next tab page
623 goto_tabpage((int)Prenum);
624 break;
625
Bram Moolenaar882d02e2019-02-22 17:56:43 +0100626 case 'T': // CTRL-W gT: go to previous tab page
627 goto_tabpage(-(int)Prenum1);
628 break;
629
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 default:
631 beep_flush();
632 break;
633 }
634 break;
635
636 default: beep_flush();
637 break;
638 }
639}
640
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100641/*
Bram Moolenaarb7316892019-05-01 18:08:42 +0200642 * Figure out the address type for ":wincmd".
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100643 */
644 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100645get_wincmd_addr_type(char_u *arg, exarg_T *eap)
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100646{
647 switch (*arg)
648 {
649 case 'S':
650 case Ctrl_S:
651 case 's':
652 case Ctrl_N:
653 case 'n':
654 case 'j':
655 case Ctrl_J:
656 case 'k':
657 case Ctrl_K:
658 case 'T':
659 case Ctrl_R:
660 case 'r':
661 case 'R':
662 case 'K':
663 case 'J':
664 case '+':
665 case '-':
666 case Ctrl__:
667 case '_':
668 case '|':
669 case ']':
670 case Ctrl_RSB:
671 case 'g':
672 case Ctrl_G:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100673 case Ctrl_V:
674 case 'v':
675 case 'h':
676 case Ctrl_H:
677 case 'l':
678 case Ctrl_L:
679 case 'H':
680 case 'L':
681 case '>':
682 case '<':
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100683#if defined(FEAT_QUICKFIX)
684 case '}':
685#endif
686#ifdef FEAT_SEARCHPATH
687 case 'f':
688 case 'F':
689 case Ctrl_F:
690#endif
691#ifdef FEAT_FIND_ID
692 case 'i':
693 case Ctrl_I:
694 case 'd':
695 case Ctrl_D:
696#endif
Bram Moolenaarb7316892019-05-01 18:08:42 +0200697 // window size or any count
698 eap->addr_type = ADDR_OTHER;
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100699 break;
700
701 case Ctrl_HAT:
702 case '^':
Bram Moolenaarb7316892019-05-01 18:08:42 +0200703 // buffer number
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100704 eap->addr_type = ADDR_BUFFERS;
705 break;
706
707 case Ctrl_Q:
708 case 'q':
709 case Ctrl_C:
710 case 'c':
711 case Ctrl_O:
712 case 'o':
713 case Ctrl_W:
714 case 'w':
715 case 'W':
716 case 'x':
717 case Ctrl_X:
Bram Moolenaarb7316892019-05-01 18:08:42 +0200718 // window number
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100719 eap->addr_type = ADDR_WINDOWS;
720 break;
721
722#if defined(FEAT_QUICKFIX)
723 case Ctrl_Z:
724 case 'z':
725 case 'P':
726#endif
727 case 't':
728 case Ctrl_T:
729 case 'b':
730 case Ctrl_B:
731 case 'p':
732 case Ctrl_P:
733 case '=':
734 case CAR:
Bram Moolenaarb7316892019-05-01 18:08:42 +0200735 // no count
736 eap->addr_type = ADDR_NONE;
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100737 break;
738 }
739}
740
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100741 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100742cmd_with_count(
743 char *cmd,
744 char_u *bufp,
745 size_t bufsize,
746 long Prenum)
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100747{
748 size_t len = STRLEN(cmd);
749
750 STRCPY(bufp, cmd);
751 if (Prenum > 0)
752 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
753}
754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755/*
Bram Moolenaar1417c762019-07-27 17:31:36 +0200756 * If "split_disallowed" is set given an error and return FAIL.
757 * Otherwise return OK.
758 */
759 static int
760check_split_disallowed()
761{
762 if (split_disallowed > 0)
763 {
764 emsg(_("E242: Can't split a window while closing another"));
765 return FAIL;
766 }
767 return OK;
768}
769
770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 * split the current window, implements CTRL-W s and :split
772 *
773 * "size" is the height or width for the new window, 0 to use half of current
774 * height or width.
775 *
776 * "flags":
777 * WSP_ROOM: require enough room for new window
778 * WSP_VERT: vertical split.
779 * WSP_TOP: open window at the top-left of the shell (help window).
780 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
781 * WSP_HELP: creating the help window, keep layout snapshot
782 *
783 * return FAIL for failure, OK otherwise
784 */
785 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100786win_split(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +0200788 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200789 return FAIL;
790
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000791 /* When the ":tab" modifier was used open a new tab page instead. */
792 if (may_open_tabpage() == OK)
793 return OK;
794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 /* Add flags from ":vertical", ":topleft" and ":botright". */
796 flags |= cmdmod.split;
797 if ((flags & WSP_TOP) && (flags & WSP_BOT))
798 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100799 emsg(_("E442: Can't split topleft and botright at the same time"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 return FAIL;
801 }
Bram Moolenaar1417c762019-07-27 17:31:36 +0200802 if (check_split_disallowed() == FAIL)
803 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
805 /* When creating the help window make a snapshot of the window layout.
806 * Otherwise clear the snapshot, it's now invalid. */
807 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000808 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000810 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812 return win_split_ins(size, flags, NULL, 0);
813}
814
815/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100816 * When "new_wp" is NULL: split the current window in two.
817 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 * top/left/right/bottom.
819 * return FAIL for failure, OK otherwise
820 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000821 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100822win_split_ins(
823 int size,
824 int flags,
825 win_T *new_wp,
826 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100828 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 win_T *oldwin;
830 int new_size = size;
831 int i;
832 int need_status = 0;
833 int do_equal = FALSE;
834 int needed;
835 int available;
836 int oldwin_height = 0;
837 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200838 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200840 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200841 int wmh1;
Bram Moolenaar98da6ec2018-04-13 22:15:46 +0200842 int did_set_fraction = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
844 if (flags & WSP_TOP)
845 oldwin = firstwin;
846 else if (flags & WSP_BOT)
847 oldwin = lastwin;
848 else
849 oldwin = curwin;
850
851 /* add a status line when p_ls == 1 and splitting the first window */
Bram Moolenaar459ca562016-11-10 18:16:33 +0100852 if (ONE_WINDOW && p_ls == 1 && oldwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {
Bram Moolenaar415a6932017-12-05 20:31:07 +0100854 if (VISIBLE_HEIGHT(oldwin) <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100856 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 return FAIL;
858 }
859 need_status = STATUS_HEIGHT;
860 }
861
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000862#ifdef FEAT_GUI
863 /* May be needed for the scrollbars that are going to change. */
864 if (gui.in_use)
865 out_flush();
866#endif
867
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 if (flags & WSP_VERT)
869 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200870 int wmw1;
871 int minwidth;
872
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
875 /*
876 * Check if we are able to split the current window and compute its
877 * width.
878 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200879 /* Current window requires at least 1 space. */
880 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
881 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200883 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200884 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200886 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200888 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200890 else if (p_ea)
891 {
892 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
893 prevfrp = oldwin->w_frame;
894 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
895 frp = frp->fr_parent)
896 {
897 if (frp->fr_layout == FR_ROW)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +0100898 FOR_ALL_FRAMES(frp2, frp->fr_child)
Bram Moolenaar54368f22014-07-23 15:21:20 +0200899 if (frp2 != prevfrp)
900 minwidth += frame_minwidth(frp2, NOWIN);
901 prevfrp = frp;
902 }
903 available = topframe->fr_width;
904 needed += minwidth;
905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200907 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200908 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
909 available = oldwin->w_frame->fr_width;
910 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200911 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100912 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100914 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 return FAIL;
916 }
917 if (new_size == 0)
918 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200919 if (new_size > available - minwidth - 1)
920 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200921 if (new_size < wmw1)
922 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923
924 /* if it doesn't fit in the current window, need win_equal() */
925 if (oldwin->w_width - new_size - 1 < p_wmw)
926 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000927
928 /* We don't like to take lines for the new window from a
929 * 'winfixwidth' window. Take them from a window to the left or right
Bram Moolenaar38e34832017-03-19 20:22:36 +0100930 * instead, if possible. Add one for the separator. */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000931 if (oldwin->w_p_wfw)
Bram Moolenaar38e34832017-03-19 20:22:36 +0100932 win_setwidth_win(oldwin->w_width + new_size + 1, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000933
934 /* Only make all windows the same width if one of them (except oldwin)
935 * is wider than one of the split windows. */
936 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
Bram Moolenaar1417c762019-07-27 17:31:36 +0200937 && oldwin->w_frame->fr_parent != NULL)
Bram Moolenaar67f71312007-08-12 14:55:56 +0000938 {
939 frp = oldwin->w_frame->fr_parent->fr_child;
940 while (frp != NULL)
941 {
942 if (frp->fr_win != oldwin && frp->fr_win != NULL
943 && (frp->fr_win->w_width > new_size
944 || frp->fr_win->w_width > oldwin->w_width
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100945 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000946 {
947 do_equal = TRUE;
948 break;
949 }
950 frp = frp->fr_next;
951 }
952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
954 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 {
956 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957
958 /*
959 * Check if we are able to split the current window and compute its
960 * height.
961 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200962 /* Current window requires at least 1 space. */
Bram Moolenaar415a6932017-12-05 20:31:07 +0100963 wmh1 = (p_wmh == 0 ? 1 : p_wmh) + WINBAR_HEIGHT(curwin);
Bram Moolenaar1f538352014-07-16 18:19:27 +0200964 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200966 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200967 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200969 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200971 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200973 else if (p_ea)
974 {
975 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
976 prevfrp = oldwin->w_frame;
977 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
978 frp = frp->fr_parent)
979 {
980 if (frp->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +0100981 FOR_ALL_FRAMES(frp2, frp->fr_child)
Bram Moolenaar54368f22014-07-23 15:21:20 +0200982 if (frp2 != prevfrp)
983 minheight += frame_minheight(frp2, NOWIN);
984 prevfrp = frp;
985 }
986 available = topframe->fr_height;
987 needed += minheight;
988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 else
990 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200991 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
992 available = oldwin->w_frame->fr_height;
993 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100995 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100997 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 return FAIL;
999 }
1000 oldwin_height = oldwin->w_height;
1001 if (need_status)
1002 {
1003 oldwin->w_status_height = STATUS_HEIGHT;
1004 oldwin_height -= STATUS_HEIGHT;
1005 }
1006 if (new_size == 0)
1007 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +02001008 if (new_size > available - minheight - STATUS_HEIGHT)
1009 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +02001010 if (new_size < wmh1)
1011 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
1013 /* if it doesn't fit in the current window, need win_equal() */
1014 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
1015 do_equal = TRUE;
1016
1017 /* We don't like to take lines for the new window from a
1018 * 'winfixheight' window. Take them from a window above or below
1019 * instead, if possible. */
1020 if (oldwin->w_p_wfh)
1021 {
Bram Moolenaar98da6ec2018-04-13 22:15:46 +02001022 /* Set w_fraction now so that the cursor keeps the same relative
1023 * vertical position using the old height. */
1024 set_fraction(oldwin);
1025 did_set_fraction = TRUE;
1026
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
1028 oldwin);
1029 oldwin_height = oldwin->w_height;
1030 if (need_status)
1031 oldwin_height -= STATUS_HEIGHT;
1032 }
Bram Moolenaar67f71312007-08-12 14:55:56 +00001033
1034 /* Only make all windows the same height if one of them (except oldwin)
1035 * is higher than one of the split windows. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001036 if (!do_equal && p_ea && size == 0 && *p_ead != 'h'
Bram Moolenaar67f71312007-08-12 14:55:56 +00001037 && oldwin->w_frame->fr_parent != NULL)
1038 {
1039 frp = oldwin->w_frame->fr_parent->fr_child;
1040 while (frp != NULL)
1041 {
1042 if (frp->fr_win != oldwin && frp->fr_win != NULL
1043 && (frp->fr_win->w_height > new_size
1044 || frp->fr_win->w_height > oldwin_height - new_size
1045 - STATUS_HEIGHT))
1046 {
1047 do_equal = TRUE;
1048 break;
1049 }
1050 frp = frp->fr_next;
1051 }
1052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 }
1054
1055 /*
1056 * allocate new window structure and link it in the window list
1057 */
1058 if ((flags & WSP_TOP) == 0
1059 && ((flags & WSP_BOT)
1060 || (flags & WSP_BELOW)
1061 || (!(flags & WSP_ABOVE)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001062 && ( (flags & WSP_VERT) ? p_spr : p_sb))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 {
1064 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001065 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001066 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 else
1068 win_append(oldwin, wp);
1069 }
1070 else
1071 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001072 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001073 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 else
1075 win_append(oldwin->w_prev, wp);
1076 }
1077
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001078 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 {
1080 if (wp == NULL)
1081 return FAIL;
1082
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001083 new_frame(wp);
1084 if (wp->w_frame == NULL)
1085 {
1086 win_free(wp, NULL);
1087 return FAIL;
1088 }
1089
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001090 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001091 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 }
1093
1094 /*
1095 * Reorganise the tree of frames to insert the new window.
1096 */
1097 if (flags & (WSP_TOP | WSP_BOT))
1098 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1100 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 {
1102 curfrp = topframe->fr_child;
1103 if (flags & WSP_BOT)
1104 while (curfrp->fr_next != NULL)
1105 curfrp = curfrp->fr_next;
1106 }
1107 else
1108 curfrp = topframe;
1109 before = (flags & WSP_TOP);
1110 }
1111 else
1112 {
1113 curfrp = oldwin->w_frame;
1114 if (flags & WSP_BELOW)
1115 before = FALSE;
1116 else if (flags & WSP_ABOVE)
1117 before = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001118 else if (flags & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 before = !p_spr;
1120 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 before = !p_sb;
1122 }
1123 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1124 {
1125 /* Need to create a new frame in the tree to make a branch. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001126 frp = ALLOC_CLEAR_ONE(frame_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 *frp = *curfrp;
1128 curfrp->fr_layout = layout;
1129 frp->fr_parent = curfrp;
1130 frp->fr_next = NULL;
1131 frp->fr_prev = NULL;
1132 curfrp->fr_child = frp;
1133 curfrp->fr_win = NULL;
1134 curfrp = frp;
1135 if (frp->fr_win != NULL)
1136 oldwin->w_frame = frp;
1137 else
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01001138 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 frp->fr_parent = curfrp;
1140 }
1141
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001142 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001143 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001145 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 frp->fr_parent = curfrp->fr_parent;
1147
1148 /* Insert the new frame at the right place in the frame list. */
1149 if (before)
1150 frame_insert(curfrp, frp);
1151 else
1152 frame_append(curfrp, frp);
1153
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001154 /* Set w_fraction now so that the cursor keeps the same relative
1155 * vertical position. */
Bram Moolenaar98da6ec2018-04-13 22:15:46 +02001156 if (!did_set_fraction)
1157 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001158 wp->w_fraction = oldwin->w_fraction;
1159
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (flags & WSP_VERT)
1161 {
1162 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001163
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 if (need_status)
1165 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001166 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 oldwin->w_status_height = need_status;
1168 }
1169 if (flags & (WSP_TOP | WSP_BOT))
1170 {
1171 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001172 wp->w_winrow = tabline_height();
Bram Moolenaard326ad62017-09-18 20:31:41 +02001173 win_new_height(wp, curfrp->fr_height - (p_ls > 0)
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001174 - WINBAR_HEIGHT(wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 wp->w_status_height = (p_ls > 0);
1176 }
1177 else
1178 {
1179 /* height and row of new window is same as current window */
1180 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01001181 win_new_height(wp, VISIBLE_HEIGHT(oldwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 wp->w_status_height = oldwin->w_status_height;
1183 }
1184 frp->fr_height = curfrp->fr_height;
1185
1186 /* "new_size" of the current window goes to the new window, use
1187 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001188 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (before)
1190 wp->w_vsep_width = 1;
1191 else
1192 {
1193 wp->w_vsep_width = oldwin->w_vsep_width;
1194 oldwin->w_vsep_width = 1;
1195 }
1196 if (flags & (WSP_TOP | WSP_BOT))
1197 {
1198 if (flags & WSP_BOT)
1199 frame_add_vsep(curfrp);
1200 /* Set width of neighbor frame */
1201 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001202 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1203 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001206 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 if (before) /* new window left of current one */
1208 {
1209 wp->w_wincol = oldwin->w_wincol;
1210 oldwin->w_wincol += new_size + 1;
1211 }
1212 else /* new window right of current one */
1213 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1214 frame_fix_width(oldwin);
1215 frame_fix_width(wp);
1216 }
1217 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
1219 /* width and column of new window is same as current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 if (flags & (WSP_TOP | WSP_BOT))
1221 {
1222 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001223 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 wp->w_vsep_width = 0;
1225 }
1226 else
1227 {
1228 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001229 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 wp->w_vsep_width = oldwin->w_vsep_width;
1231 }
1232 frp->fr_width = curfrp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
1234 /* "new_size" of the current window goes to the new window, use
1235 * one row for the status line */
1236 win_new_height(wp, new_size);
1237 if (flags & (WSP_TOP | WSP_BOT))
Bram Moolenaar991dea32016-05-24 11:31:32 +02001238 {
Bram Moolenaard326ad62017-09-18 20:31:41 +02001239 int new_fr_height = curfrp->fr_height - new_size
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001240 + WINBAR_HEIGHT(wp) ;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001241
1242 if (!((flags & WSP_BOT) && p_ls == 0))
1243 new_fr_height -= STATUS_HEIGHT;
1244 frame_new_height(curfrp, new_fr_height, flags & WSP_TOP, FALSE);
1245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 else
1247 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1248 if (before) /* new window above current one */
1249 {
1250 wp->w_winrow = oldwin->w_winrow;
1251 wp->w_status_height = STATUS_HEIGHT;
1252 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1253 }
1254 else /* new window below current one */
1255 {
Bram Moolenaar415a6932017-12-05 20:31:07 +01001256 wp->w_winrow = oldwin->w_winrow + VISIBLE_HEIGHT(oldwin)
1257 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 wp->w_status_height = oldwin->w_status_height;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001259 if (!(flags & WSP_BOT))
1260 oldwin->w_status_height = STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 if (flags & WSP_BOT)
1263 frame_add_statusline(curfrp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 frame_fix_height(wp);
1265 frame_fix_height(oldwin);
1266 }
1267
1268 if (flags & (WSP_TOP | WSP_BOT))
1269 (void)win_comp_pos();
1270
1271 /*
1272 * Both windows need redrawing
1273 */
1274 redraw_win_later(wp, NOT_VALID);
1275 wp->w_redr_status = TRUE;
1276 redraw_win_later(oldwin, NOT_VALID);
1277 oldwin->w_redr_status = TRUE;
1278
1279 if (need_status)
1280 {
1281 msg_row = Rows - 1;
1282 msg_col = sc_col;
1283 msg_clr_eos_force(); /* Old command/ruler may still be there */
1284 comp_col();
1285 msg_row = Rows - 1;
1286 msg_col = 0; /* put position back at start of line */
1287 }
1288
1289 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001290 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 */
1292 if (do_equal || dir != 0)
1293 win_equal(wp, TRUE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001295 : dir == 'h' ? 'b' : 'v');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296
1297 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1298 * size was given. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if (flags & WSP_VERT)
1300 {
1301 i = p_wiw;
1302 if (size != 0)
1303 p_wiw = size;
1304
1305# ifdef FEAT_GUI
1306 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1307 if (gui.in_use)
1308 gui_init_which_components(NULL);
1309# endif
1310 }
1311 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {
1313 i = p_wh;
1314 if (size != 0)
1315 p_wh = size;
1316 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001317
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001318#ifdef FEAT_JUMPLIST
1319 /* Keep same changelist position in new window. */
1320 wp->w_changelistidx = oldwin->w_changelistidx;
1321#endif
1322
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001323 /*
1324 * make the new window the current window
1325 */
Bram Moolenaarc917da42016-07-19 22:31:36 +02001326 win_enter_ext(wp, FALSE, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 if (flags & WSP_VERT)
1328 p_wiw = i;
1329 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 p_wh = i;
1331
1332 return OK;
1333}
1334
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001335
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001336/*
1337 * Initialize window "newp" from window "oldp".
1338 * Used when splitting a window and when creating a new tab page.
1339 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001340 * WSP_NEWLOC may be specified in flags to prevent the location list from
1341 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001342 */
1343 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001344win_init(win_T *newp, win_T *oldp, int flags UNUSED)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001345{
1346 int i;
1347
1348 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001349#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001350 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001351#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001352 oldp->w_buffer->b_nwindows++;
1353 newp->w_cursor = oldp->w_cursor;
1354 newp->w_valid = 0;
1355 newp->w_curswant = oldp->w_curswant;
1356 newp->w_set_curswant = oldp->w_set_curswant;
1357 newp->w_topline = oldp->w_topline;
1358#ifdef FEAT_DIFF
1359 newp->w_topfill = oldp->w_topfill;
1360#endif
1361 newp->w_leftcol = oldp->w_leftcol;
1362 newp->w_pcmark = oldp->w_pcmark;
1363 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1364 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001365 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001366 newp->w_fraction = oldp->w_fraction;
1367 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1368#ifdef FEAT_JUMPLIST
1369 copy_jumplist(oldp, newp);
1370#endif
1371#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001372 if (flags & WSP_NEWLOC)
1373 {
1374 /* Don't copy the location list. */
1375 newp->w_llist = NULL;
1376 newp->w_llist_ref = NULL;
1377 }
1378 else
Bram Moolenaar09037502018-09-25 22:08:14 +02001379 copy_loclist_stack(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001380#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001381 newp->w_localdir = (oldp->w_localdir == NULL)
1382 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001383
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001384 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001385 for (i = 0; i < oldp->w_tagstacklen; i++)
1386 {
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02001387 taggy_T *tag = &newp->w_tagstack[i];
1388 *tag = oldp->w_tagstack[i];
1389 if (tag->tagname != NULL)
1390 tag->tagname = vim_strsave(tag->tagname);
1391 if (tag->user_data != NULL)
1392 tag->user_data = vim_strsave(tag->user_data);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001393 }
1394 newp->w_tagstackidx = oldp->w_tagstackidx;
1395 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001396#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001397 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001398#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001399
1400 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001401
Bram Moolenaara971b822011-09-14 14:43:25 +02001402#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001403 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001404#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001405}
1406
1407/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001408 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001409 * Only the essential things are copied.
1410 */
1411 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001412win_init_some(win_T *newp, win_T *oldp)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001413{
1414 /* Use the same argument list. */
1415 newp->w_alist = oldp->w_alist;
1416 ++newp->w_alist->al_refcount;
1417 newp->w_arg_idx = oldp->w_arg_idx;
1418
1419 /* copy options from existing window */
1420 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001421}
1422
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001423/*
1424 * Return TRUE if "win" is a global popup or a popup in the current tab page.
1425 */
Bram Moolenaarb53fb312019-06-13 23:59:52 +02001426 int
Bram Moolenaar682725c2019-05-25 20:10:37 +02001427win_valid_popup(win_T *win UNUSED)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001428{
1429#ifdef FEAT_TEXT_PROP
1430 win_T *wp;
1431
1432 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1433 if (wp == win)
1434 return TRUE;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001435 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001436 if (wp == win)
1437 return TRUE;
1438#endif
1439 return FALSE;
1440}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001443 * Check if "win" is a pointer to an existing window in the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 */
1445 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001446win_valid(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447{
1448 win_T *wp;
1449
1450 if (win == NULL)
1451 return FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02001452 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 if (wp == win)
1454 return TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001455 return win_valid_popup(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456}
1457
1458/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001459 * Check if "win" is a pointer to an existing window in any tab page.
1460 */
1461 int
1462win_valid_any_tab(win_T *win)
1463{
1464 win_T *wp;
1465 tabpage_T *tp;
1466
1467 if (win == NULL)
1468 return FALSE;
1469 FOR_ALL_TABPAGES(tp)
1470 {
1471 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
1472 {
1473 if (wp == win)
1474 return TRUE;
1475 }
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001476#ifdef FEAT_TEXT_PROP
1477 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1478 if (wp == win)
1479 return TRUE;
1480#endif
Bram Moolenaare59215c2016-08-14 19:08:45 +02001481 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001482 return win_valid_popup(win);
Bram Moolenaare59215c2016-08-14 19:08:45 +02001483}
1484
1485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 * Return the number of windows.
1487 */
1488 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001489win_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491 win_T *wp;
1492 int count = 0;
1493
Bram Moolenaar29323592016-07-24 22:04:11 +02001494 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 ++count;
1496 return count;
1497}
1498
1499/*
1500 * Make "count" windows on the screen.
1501 * Return actual number of windows on the screen.
1502 * Must be called when there is just one window, filling the whole screen
1503 * (excluding the command line).
1504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001506make_windows(
1507 int count,
1508 int vertical UNUSED) /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
1510 int maxcount;
1511 int todo;
1512
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 if (vertical)
1514 {
1515 /* Each windows needs at least 'winminwidth' lines and a separator
1516 * column. */
1517 maxcount = (curwin->w_width + curwin->w_vsep_width
1518 - (p_wiw - p_wmw)) / (p_wmw + 1);
1519 }
1520 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 {
1522 /* Each window needs at least 'winminheight' lines and a status line. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001523 maxcount = (VISIBLE_HEIGHT(curwin) + curwin->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1525 }
1526
1527 if (maxcount < 2)
1528 maxcount = 2;
1529 if (count > maxcount)
1530 count = maxcount;
1531
1532 /*
1533 * add status line now, otherwise first window will be too big
1534 */
1535 if (count > 1)
1536 last_status(TRUE);
1537
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 /*
1539 * Don't execute autocommands while creating the windows. Must do that
1540 * when putting the buffers in the windows.
1541 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001542 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
1544 /* todo is number of windows left to create */
1545 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if (vertical)
1547 {
1548 if (win_split(curwin->w_width - (curwin->w_width - todo)
1549 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1550 break;
1551 }
1552 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 {
1554 if (win_split(curwin->w_height - (curwin->w_height - todo
1555 * STATUS_HEIGHT) / (todo + 1)
1556 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1557 break;
1558 }
1559
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001560 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561
1562 /* return actual number of windows */
1563 return (count - todo);
1564}
1565
1566/*
1567 * Exchange current and next window
1568 */
1569 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001570win_exchange(long Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
1572 frame_T *frp;
1573 frame_T *frp2;
1574 win_T *wp;
1575 win_T *wp2;
1576 int temp;
1577
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02001578 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001579 return;
1580 if (ONE_WINDOW) // just one window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 {
1582 beep_flush();
1583 return;
1584 }
1585
1586#ifdef FEAT_GUI
1587 need_mouse_correct = TRUE;
1588#endif
1589
1590 /*
1591 * find window to exchange with
1592 */
1593 if (Prenum)
1594 {
1595 frp = curwin->w_frame->fr_parent->fr_child;
1596 while (frp != NULL && --Prenum > 0)
1597 frp = frp->fr_next;
1598 }
1599 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1600 frp = curwin->w_frame->fr_next;
1601 else /* Swap last window in row/col with previous */
1602 frp = curwin->w_frame->fr_prev;
1603
1604 /* We can only exchange a window with another window, not with a frame
1605 * containing windows. */
1606 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1607 return;
1608 wp = frp->fr_win;
1609
1610/*
1611 * 1. remove curwin from the list. Remember after which window it was in wp2
1612 * 2. insert curwin before wp in the list
1613 * if wp != wp2
1614 * 3. remove wp from the list
1615 * 4. insert wp after wp2
1616 * 5. exchange the status line height and vsep width.
1617 */
1618 wp2 = curwin->w_prev;
1619 frp2 = curwin->w_frame->fr_prev;
1620 if (wp->w_prev != curwin)
1621 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001622 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 frame_remove(curwin->w_frame);
1624 win_append(wp->w_prev, curwin);
1625 frame_insert(frp, curwin->w_frame);
1626 }
1627 if (wp != wp2)
1628 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001629 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 frame_remove(wp->w_frame);
1631 win_append(wp2, wp);
1632 if (frp2 == NULL)
1633 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1634 else
1635 frame_append(frp2, wp->w_frame);
1636 }
1637 temp = curwin->w_status_height;
1638 curwin->w_status_height = wp->w_status_height;
1639 wp->w_status_height = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 temp = curwin->w_vsep_width;
1641 curwin->w_vsep_width = wp->w_vsep_width;
1642 wp->w_vsep_width = temp;
1643
1644 /* If the windows are not in the same frame, exchange the sizes to avoid
1645 * messing up the window layout. Otherwise fix the frame sizes. */
1646 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1647 {
1648 temp = curwin->w_height;
1649 curwin->w_height = wp->w_height;
1650 wp->w_height = temp;
1651 temp = curwin->w_width;
1652 curwin->w_width = wp->w_width;
1653 wp->w_width = temp;
1654 }
1655 else
1656 {
1657 frame_fix_height(curwin);
1658 frame_fix_height(wp);
1659 frame_fix_width(curwin);
1660 frame_fix_width(wp);
1661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
1663 (void)win_comp_pos(); /* recompute window positions */
1664
1665 win_enter(wp, TRUE);
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01001666 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667}
1668
1669/*
1670 * rotate windows: if upwards TRUE the second window becomes the first one
1671 * if upwards FALSE the first window becomes the second one
1672 */
1673 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001674win_rotate(int upwards, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675{
1676 win_T *wp1;
1677 win_T *wp2;
1678 frame_T *frp;
1679 int n;
1680
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001681 if (ONE_WINDOW) /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 {
1683 beep_flush();
1684 return;
1685 }
1686
1687#ifdef FEAT_GUI
1688 need_mouse_correct = TRUE;
1689#endif
1690
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 /* Check if all frames in this row/col have one window. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01001692 FOR_ALL_FRAMES(frp, curwin->w_frame->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 if (frp->fr_win == NULL)
1694 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001695 emsg(_("E443: Cannot rotate when another window is split"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 return;
1697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
1699 while (count--)
1700 {
1701 if (upwards) /* first window becomes last window */
1702 {
1703 /* remove first window/frame from the list */
1704 frp = curwin->w_frame->fr_parent->fr_child;
1705 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001706 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 frame_remove(frp);
1708
1709 /* find last frame and append removed window/frame after it */
1710 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1711 ;
1712 win_append(frp->fr_win, wp1);
1713 frame_append(frp, wp1->w_frame);
1714
1715 wp2 = frp->fr_win; /* previously last window */
1716 }
1717 else /* last window becomes first window */
1718 {
1719 /* find last window/frame in the list and remove it */
1720 for (frp = curwin->w_frame; frp->fr_next != NULL;
1721 frp = frp->fr_next)
1722 ;
1723 wp1 = frp->fr_win;
1724 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001725 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 frame_remove(frp);
1727
1728 /* append the removed window/frame before the first in the list */
1729 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1730 frame_insert(frp->fr_parent->fr_child, frp);
1731 }
1732
1733 /* exchange status height and vsep width of old and new last window */
1734 n = wp2->w_status_height;
1735 wp2->w_status_height = wp1->w_status_height;
1736 wp1->w_status_height = n;
1737 frame_fix_height(wp1);
1738 frame_fix_height(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 n = wp2->w_vsep_width;
1740 wp2->w_vsep_width = wp1->w_vsep_width;
1741 wp1->w_vsep_width = n;
1742 frame_fix_width(wp1);
1743 frame_fix_width(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001745 /* recompute w_winrow and w_wincol for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 (void)win_comp_pos();
1747 }
1748
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01001749 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750}
1751
1752/*
1753 * Move the current window to the very top/bottom/left/right of the screen.
1754 */
1755 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001756win_totop(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
1758 int dir;
1759 int height = curwin->w_height;
1760
Bram Moolenaar459ca562016-11-10 18:16:33 +01001761 if (ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 {
1763 beep_flush();
1764 return;
1765 }
Bram Moolenaar1417c762019-07-27 17:31:36 +02001766 if (check_split_disallowed() == FAIL)
1767 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
1769 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001770 (void)winframe_remove(curwin, &dir, NULL);
1771 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 last_status(FALSE); /* may need to remove last status line */
1773 (void)win_comp_pos(); /* recompute window positions */
1774
1775 /* Split a window on the desired side and put the window there. */
1776 (void)win_split_ins(size, flags, curwin, dir);
1777 if (!(flags & WSP_VERT))
1778 {
1779 win_setheight(height);
1780 if (p_ea)
1781 win_equal(curwin, TRUE, 'v');
1782 }
1783
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001784#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1786 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001787 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789}
1790
1791/*
1792 * Move window "win1" to below/right of "win2" and make "win1" the current
1793 * window. Only works within the same frame!
1794 */
1795 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001796win_move_after(win_T *win1, win_T *win2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797{
1798 int height;
1799
1800 /* check if the arguments are reasonable */
1801 if (win1 == win2)
1802 return;
1803
1804 /* check if there is something to do */
1805 if (win2->w_next != win1)
1806 {
Bram Moolenaar1417c762019-07-27 17:31:36 +02001807 if (win1->w_frame->fr_parent != win2->w_frame->fr_parent)
1808 {
1809 iemsg("INTERNAL: trying to move a window into another frame");
1810 return;
1811 }
1812
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 /* may need move the status line/vertical separator of the last window
1814 * */
1815 if (win1 == lastwin)
1816 {
1817 height = win1->w_prev->w_status_height;
1818 win1->w_prev->w_status_height = win1->w_status_height;
1819 win1->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001820 if (win1->w_prev->w_vsep_width == 1)
1821 {
1822 /* Remove the vertical separator from the last-but-one window,
1823 * add it to the last window. Adjust the frame widths. */
1824 win1->w_prev->w_vsep_width = 0;
1825 win1->w_prev->w_frame->fr_width -= 1;
1826 win1->w_vsep_width = 1;
1827 win1->w_frame->fr_width += 1;
1828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 }
1830 else if (win2 == lastwin)
1831 {
1832 height = win1->w_status_height;
1833 win1->w_status_height = win2->w_status_height;
1834 win2->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001835 if (win1->w_vsep_width == 1)
1836 {
1837 /* Remove the vertical separator from win1, add it to the last
1838 * window, win2. Adjust the frame widths. */
1839 win2->w_vsep_width = 1;
1840 win2->w_frame->fr_width += 1;
1841 win1->w_vsep_width = 0;
1842 win1->w_frame->fr_width -= 1;
1843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001845 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 frame_remove(win1->w_frame);
1847 win_append(win2, win1);
1848 frame_append(win2->w_frame, win1->w_frame);
1849
1850 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1851 redraw_later(NOT_VALID);
1852 }
1853 win_enter(win1, FALSE);
1854}
1855
1856/*
1857 * Make all windows the same height.
1858 * 'next_curwin' will soon be the current window, make sure it has enough
1859 * rows.
1860 */
1861 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001862win_equal(
1863 win_T *next_curwin, /* pointer to current window to be or NULL */
1864 int current, /* do only frame with current window */
1865 int dir) /* 'v' for vertically, 'h' for horizontally,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 'b' for both, 0 for using p_ead */
1867{
1868 if (dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 dir = *p_ead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001871 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001872 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873}
1874
1875/*
1876 * Set a frame to a new position and height, spreading the available room
1877 * equally over contained frames.
1878 * The window "next_curwin" (if not NULL) should at least get the size from
1879 * 'winheight' and 'winwidth' if possible.
1880 */
1881 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001882win_equal_rec(
1883 win_T *next_curwin, /* pointer to current window to be or NULL */
1884 int current, /* do only frame with current window */
1885 frame_T *topfr, /* frame to set size off */
1886 int dir, /* 'v', 'h' or 'b', see win_equal() */
1887 int col, /* horizontal position for frame */
1888 int row, /* vertical position for frame */
1889 int width, /* new width of frame */
1890 int height) /* new height of frame */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891{
1892 int n, m;
1893 int extra_sep = 0;
1894 int wincount, totwincount = 0;
1895 frame_T *fr;
1896 int next_curwin_size = 0;
1897 int room = 0;
1898 int new_size;
1899 int has_next_curwin = 0;
1900 int hnc;
1901
1902 if (topfr->fr_layout == FR_LEAF)
1903 {
1904 /* Set the width/height of this frame.
1905 * Redraw when size or position changes */
1906 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 )
1909 {
1910 topfr->fr_win->w_winrow = row;
1911 frame_new_height(topfr, height, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001913 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01001914 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 else if (topfr->fr_layout == FR_ROW)
1918 {
1919 topfr->fr_width = width;
1920 topfr->fr_height = height;
1921
1922 if (dir != 'v') /* equalize frame widths */
1923 {
1924 /* Compute the maximum number of windows horizontally in this
1925 * frame. */
1926 n = frame_minwidth(topfr, NOWIN);
1927 /* add one for the rightmost window, it doesn't have a separator */
1928 if (col + width == Columns)
1929 extra_sep = 1;
1930 else
1931 extra_sep = 0;
1932 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001933 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001935 /*
1936 * Compute width for "next_curwin" window and room available for
1937 * other windows.
1938 * "m" is the minimal width when counting p_wiw for "next_curwin".
1939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 m = frame_minwidth(topfr, next_curwin);
1941 room = width - m;
1942 if (room < 0)
1943 {
1944 next_curwin_size = p_wiw + room;
1945 room = 0;
1946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 else
1948 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001949 next_curwin_size = -1;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01001950 FOR_ALL_FRAMES(fr, topfr->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001951 {
1952 /* If 'winfixwidth' set keep the window width if
1953 * possible.
1954 * Watch out for this window being the next_curwin. */
1955 if (frame_fixed_width(fr))
1956 {
1957 n = frame_minwidth(fr, NOWIN);
1958 new_size = fr->fr_width;
1959 if (frame_has_win(fr, next_curwin))
1960 {
1961 room += p_wiw - p_wmw;
1962 next_curwin_size = 0;
1963 if (new_size < p_wiw)
1964 new_size = p_wiw;
1965 }
1966 else
1967 /* These windows don't use up room. */
1968 totwincount -= (n + (fr->fr_next == NULL
1969 ? extra_sep : 0)) / (p_wmw + 1);
1970 room -= new_size - n;
1971 if (room < 0)
1972 {
1973 new_size += room;
1974 room = 0;
1975 }
1976 fr->fr_newwidth = new_size;
1977 }
1978 }
1979 if (next_curwin_size == -1)
1980 {
1981 if (!has_next_curwin)
1982 next_curwin_size = 0;
1983 else if (totwincount > 1
1984 && (room + (totwincount - 2))
1985 / (totwincount - 1) > p_wiw)
1986 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001987 /* Can make all windows wider than 'winwidth', spread
1988 * the room equally. */
1989 next_curwin_size = (room + p_wiw
1990 + (totwincount - 1) * p_wmw
1991 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001992 room -= next_curwin_size - p_wiw;
1993 }
1994 else
1995 next_curwin_size = p_wiw;
1996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001998
1999 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 --totwincount; /* don't count curwin */
2001 }
2002
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002003 FOR_ALL_FRAMES(fr, topfr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 wincount = 1;
2006 if (fr->fr_next == NULL)
2007 /* last frame gets all that remains (avoid roundoff error) */
2008 new_size = width;
2009 else if (dir == 'v')
2010 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002011 else if (frame_fixed_width(fr))
2012 {
2013 new_size = fr->fr_newwidth;
2014 wincount = 0; /* doesn't count as a sizeable window */
2015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 else
2017 {
2018 /* Compute the maximum number of windows horiz. in "fr". */
2019 n = frame_minwidth(fr, NOWIN);
2020 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2021 / (p_wmw + 1);
2022 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002023 if (has_next_curwin)
2024 hnc = frame_has_win(fr, next_curwin);
2025 else
2026 hnc = FALSE;
2027 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002029 if (totwincount == 0)
2030 new_size = room;
2031 else
2032 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002034 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
2036 next_curwin_size -= p_wiw - (m - n);
2037 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002038 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002040 else
2041 room -= new_size;
2042 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
2044
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002045 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 * window, unless equalizing all frames. */
2047 if (!current || dir != 'v' || topfr->fr_parent != NULL
2048 || (new_size != fr->fr_width)
2049 || frame_has_win(fr, next_curwin))
2050 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002051 new_size, height);
2052 col += new_size;
2053 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 totwincount -= wincount;
2055 }
2056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 else /* topfr->fr_layout == FR_COL */
2058 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 topfr->fr_width = width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 topfr->fr_height = height;
2061
2062 if (dir != 'h') /* equalize frame heights */
2063 {
2064 /* Compute maximum number of windows vertically in this frame. */
2065 n = frame_minheight(topfr, NOWIN);
2066 /* add one for the bottom window if it doesn't have a statusline */
2067 if (row + height == cmdline_row && p_ls == 0)
2068 extra_sep = 1;
2069 else
2070 extra_sep = 0;
2071 totwincount = (n + extra_sep) / (p_wmh + 1);
2072 has_next_curwin = frame_has_win(topfr, next_curwin);
2073
2074 /*
2075 * Compute height for "next_curwin" window and room available for
2076 * other windows.
2077 * "m" is the minimal height when counting p_wh for "next_curwin".
2078 */
2079 m = frame_minheight(topfr, next_curwin);
2080 room = height - m;
2081 if (room < 0)
2082 {
2083 /* The room is less then 'winheight', use all space for the
2084 * current window. */
2085 next_curwin_size = p_wh + room;
2086 room = 0;
2087 }
2088 else
2089 {
2090 next_curwin_size = -1;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002091 FOR_ALL_FRAMES(fr, topfr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
2093 /* If 'winfixheight' set keep the window height if
2094 * possible.
2095 * Watch out for this window being the next_curwin. */
2096 if (frame_fixed_height(fr))
2097 {
2098 n = frame_minheight(fr, NOWIN);
2099 new_size = fr->fr_height;
2100 if (frame_has_win(fr, next_curwin))
2101 {
2102 room += p_wh - p_wmh;
2103 next_curwin_size = 0;
2104 if (new_size < p_wh)
2105 new_size = p_wh;
2106 }
2107 else
2108 /* These windows don't use up room. */
2109 totwincount -= (n + (fr->fr_next == NULL
2110 ? extra_sep : 0)) / (p_wmh + 1);
2111 room -= new_size - n;
2112 if (room < 0)
2113 {
2114 new_size += room;
2115 room = 0;
2116 }
2117 fr->fr_newheight = new_size;
2118 }
2119 }
2120 if (next_curwin_size == -1)
2121 {
2122 if (!has_next_curwin)
2123 next_curwin_size = 0;
2124 else if (totwincount > 1
2125 && (room + (totwincount - 2))
2126 / (totwincount - 1) > p_wh)
2127 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002128 /* can make all windows higher than 'winheight',
2129 * spread the room equally. */
2130 next_curwin_size = (room + p_wh
2131 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 + (totwincount - 1)) / totwincount;
2133 room -= next_curwin_size - p_wh;
2134 }
2135 else
2136 next_curwin_size = p_wh;
2137 }
2138 }
2139
2140 if (has_next_curwin)
2141 --totwincount; /* don't count curwin */
2142 }
2143
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002144 FOR_ALL_FRAMES(fr, topfr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 wincount = 1;
2147 if (fr->fr_next == NULL)
2148 /* last frame gets all that remains (avoid roundoff error) */
2149 new_size = height;
2150 else if (dir == 'h')
2151 new_size = fr->fr_height;
2152 else if (frame_fixed_height(fr))
2153 {
2154 new_size = fr->fr_newheight;
2155 wincount = 0; /* doesn't count as a sizeable window */
2156 }
2157 else
2158 {
2159 /* Compute the maximum number of windows vert. in "fr". */
2160 n = frame_minheight(fr, NOWIN);
2161 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2162 / (p_wmh + 1);
2163 m = frame_minheight(fr, next_curwin);
2164 if (has_next_curwin)
2165 hnc = frame_has_win(fr, next_curwin);
2166 else
2167 hnc = FALSE;
2168 if (hnc) /* don't count next_curwin */
2169 --wincount;
2170 if (totwincount == 0)
2171 new_size = room;
2172 else
2173 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2174 / totwincount;
2175 if (hnc) /* add next_curwin size */
2176 {
2177 next_curwin_size -= p_wh - (m - n);
2178 new_size += next_curwin_size;
2179 room -= new_size - next_curwin_size;
2180 }
2181 else
2182 room -= new_size;
2183 new_size += n;
2184 }
2185 /* Skip frame that is full width when splitting or closing a
2186 * window, unless equalizing all frames. */
2187 if (!current || dir != 'h' || topfr->fr_parent != NULL
2188 || (new_size != fr->fr_height)
2189 || frame_has_win(fr, next_curwin))
2190 win_equal_rec(next_curwin, current, fr, dir, col, row,
2191 width, new_size);
2192 row += new_size;
2193 height -= new_size;
2194 totwincount -= wincount;
2195 }
2196 }
2197}
2198
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002199#ifdef FEAT_JOB_CHANNEL
2200 static void
2201leaving_window(win_T *win)
2202{
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002203 // Only matters for a prompt window.
2204 if (!bt_prompt(win->w_buffer))
2205 return;
2206
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002207 // When leaving a prompt window stop Insert mode and perhaps restart
2208 // it when entering that window again.
2209 win->w_buffer->b_prompt_insert = restart_edit;
Bram Moolenaar942b4542018-06-17 16:23:34 +02002210 if (restart_edit != 0 && mode_displayed)
2211 clear_cmdline = TRUE; /* unshow mode later */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002212 restart_edit = NUL;
2213
2214 // When leaving the window (or closing the window) was done from a
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002215 // callback we need to break out of the Insert mode loop and restart Insert
2216 // mode when entering the window again.
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002217 if (State & INSERT)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002218 {
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002219 stop_insert_mode = TRUE;
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002220 if (win->w_buffer->b_prompt_insert == NUL)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002221 win->w_buffer->b_prompt_insert = 'A';
2222 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002223}
2224
2225 static void
2226entering_window(win_T *win)
2227{
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002228 // Only matters for a prompt window.
2229 if (!bt_prompt(win->w_buffer))
2230 return;
2231
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002232 // When switching to a prompt buffer that was in Insert mode, don't stop
2233 // Insert mode, it may have been set in leaving_window().
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002234 if (win->w_buffer->b_prompt_insert != NUL)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002235 stop_insert_mode = FALSE;
2236
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002237 // When entering the prompt window restart Insert mode if we were in Insert
2238 // mode when we left it.
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002239 restart_edit = win->w_buffer->b_prompt_insert;
2240}
2241#endif
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002244 * Close all windows for buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 */
2246 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002247close_windows(
2248 buf_T *buf,
2249 int keep_curwin) /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002251 win_T *wp;
2252 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002253 int h = tabline_height();
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002254 int count = tabpage_index(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255
2256 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002257
Bram Moolenaar459ca562016-11-10 18:16:33 +01002258 for (wp = firstwin; wp != NULL && !ONE_WINDOW; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002260 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002261 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002263 if (win_close(wp, FALSE) == FAIL)
2264 /* If closing the window fails give up, to avoid looping
2265 * forever. */
2266 break;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002267
2268 /* Start all over, autocommands may change the window layout. */
2269 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002272 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002274
2275 /* Also check windows in other tab pages. */
2276 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2277 {
2278 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002279 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002280 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002281 if (wp->w_buffer == buf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002282 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaarf740b292006-02-16 22:11:02 +00002283 {
2284 win_close_othertab(wp, FALSE, tp);
2285
2286 /* Start all over, the tab page may be closed and
2287 * autocommands may change the window layout. */
2288 nexttp = first_tabpage;
2289 break;
2290 }
2291 }
2292
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002294
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002295 if (count != tabpage_index(NULL))
2296 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002297
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002298 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002299 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002300 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301}
2302
2303/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002304 * Return TRUE if the current window is the only window that exists (ignoring
2305 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002306 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002307 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002308 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002309last_window(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002310{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002311 return (one_window() && first_tabpage->tp_next == NULL);
2312}
2313
2314/*
2315 * Return TRUE if there is only one window other than "aucmd_win" in the
2316 * current tab page.
2317 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002318 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002319one_window(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002320{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002321 win_T *wp;
2322 int seen_one = FALSE;
2323
2324 FOR_ALL_WINDOWS(wp)
2325 {
2326 if (wp != aucmd_win)
2327 {
2328 if (seen_one)
2329 return FALSE;
2330 seen_one = TRUE;
2331 }
2332 }
2333 return TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002334}
2335
2336/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002337 * Close the possibly last window in a tab page.
2338 * Returns TRUE when the window was closed already.
2339 */
2340 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002341close_last_window_tabpage(
2342 win_T *win,
2343 int free_buf,
2344 tabpage_T *prev_curtab)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002345{
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002346 if (ONE_WINDOW)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002347 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002348 buf_T *old_curbuf = curbuf;
2349
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002350 /*
2351 * Closing the last window in a tab page. First go to another tab
2352 * page and then close the window and the tab page. This avoids that
2353 * curwin and curtab are invalid while we are freeing memory, they may
2354 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002355 * Don't trigger autocommands yet, they may use wrong values, so do
2356 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002357 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002358 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002359 redraw_tabline = TRUE;
2360
2361 /* Safety check: Autocommands may have closed the window when jumping
2362 * to the other tab page. */
2363 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2364 {
2365 int h = tabline_height();
2366
2367 win_close_othertab(win, free_buf, prev_curtab);
2368 if (h != tabline_height())
2369 shell_new_rows();
2370 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002371#ifdef FEAT_JOB_CHANNEL
2372 entering_window(curwin);
2373#endif
Bram Moolenaara8596c42012-06-13 14:28:20 +02002374 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2375 * that now. */
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002376 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002377 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002378 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2379 if (old_curbuf != curbuf)
2380 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002381 return TRUE;
2382 }
2383 return FALSE;
2384}
2385
2386/*
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +02002387 * Close the buffer of "win" and unload it if "action" is DOBUF_UNLOAD.
2388 * "action" can also be zero (do nothing) or DOBUF_WIPE.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002389 * "abort_if_last" is passed to close_buffer(): abort closing if all other
2390 * windows are closed.
2391 */
2392 static void
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +02002393win_close_buffer(win_T *win, int action, int abort_if_last)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002394{
2395#ifdef FEAT_SYN_HL
2396 // Free independent synblock before the buffer is freed.
2397 if (win->w_buffer != NULL)
2398 reset_synblock(win);
2399#endif
2400
2401#ifdef FEAT_QUICKFIX
2402 // When the quickfix/location list window is closed, unlist the buffer.
2403 if (win->w_buffer != NULL && bt_quickfix(win->w_buffer))
2404 win->w_buffer->b_p_bl = FALSE;
2405#endif
2406
2407 // Close the link to the buffer.
2408 if (win->w_buffer != NULL)
2409 {
2410 bufref_T bufref;
2411
2412 set_bufref(&bufref, curbuf);
2413 win->w_closing = TRUE;
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +02002414 close_buffer(win, win->w_buffer, action, abort_if_last);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002415 if (win_valid_any_tab(win))
2416 win->w_closing = FALSE;
2417 // Make sure curbuf is valid. It can become invalid if 'bufhidden' is
2418 // "wipe".
2419 if (!bufref_valid(&bufref))
2420 curbuf = firstbuf;
2421 }
2422}
2423
2424/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002425 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 * If "free_buf" is TRUE related buffer may be unloaded.
2427 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002428 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002429 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002431 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002432win_close(win_T *win, int free_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433{
2434 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 int dir;
2438 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002439 tabpage_T *prev_curtab = curtab;
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002440 frame_T *win_frame = win->w_frame->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002442 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002443 return FAIL;
2444
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002445 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002447 emsg(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002448 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
2450
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002451 if (win->w_closing || (win->w_buffer != NULL
2452 && win->w_buffer->b_locked > 0))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002453 return FAIL; /* window is already being closed */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002454 if (win_unlisted(win))
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002455 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002456 emsg(_("E813: Cannot close autocmd or popup window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002457 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002458 }
2459 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2460 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002461 emsg(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002462 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002463 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002464
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002465 /* When closing the last window in a tab page first go to another tab page
2466 * and then close the window and the tab page to avoid that curwin and
2467 * curtab are invalid while we are freeing memory. */
2468 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002469 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002470
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 /* When closing the help window, try restoring a snapshot after closing
2472 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002473 if (bt_help(win->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 help_window = TRUE;
2475 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002476 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 if (win == curwin)
2479 {
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002480#ifdef FEAT_JOB_CHANNEL
2481 leaving_window(curwin);
2482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 /*
2484 * Guess which window is going to be the new current window.
2485 * This may change because of the autocommands (sigh).
2486 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002487 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
2489 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002490 * Be careful: If autocommands delete the window or cause this window
2491 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 */
2493 if (wp->w_buffer != curbuf)
2494 {
2495 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002496 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002498 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002499 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002500 win->w_closing = FALSE;
2501 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002502 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002504 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002506 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002507 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002508 win->w_closing = FALSE;
2509 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002510 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002511#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 /* autocmds may abort script processing */
2513 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002514 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002518#ifdef FEAT_GUI
Bram Moolenaar647e24b2019-03-17 16:39:46 +01002519 // Avoid trouble with scrollbars that are going to be deleted in
2520 // win_free().
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002521 if (gui.in_use)
2522 out_flush();
2523#endif
2524
Bram Moolenaar12034e22019-08-25 22:25:02 +02002525#ifdef FEAT_TEXT_PROP
2526 if (popup_win_closed(win) && !win_valid(win))
2527 return FAIL;
2528#endif
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +02002529 win_close_buffer(win, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002530
Bram Moolenaar802418d2013-01-17 14:00:11 +01002531 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2532 && (last_window() || curtab != prev_curtab
2533 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002534 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002535 /* Autocommands have closed all windows, quit now. Restore
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002536 * curwin->w_buffer, otherwise writing viminfo may fail. */
2537 if (curwin->w_buffer == NULL)
2538 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002539 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002540 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002541
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002542 /* Autocommands may have moved to another tab page. */
2543 if (curtab != prev_curtab && win_valid_any_tab(win)
2544 && win->w_buffer == NULL)
2545 {
2546 /* Need to close the window anyway, since the buffer is NULL. */
2547 win_close_othertab(win, FALSE, prev_curtab);
2548 return FAIL;
2549 }
2550
2551 /* Autocommands may have closed the window already or closed the only
2552 * other window. */
2553 if (!win_valid(win) || last_window()
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002554 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002555 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
Bram Moolenaar1417c762019-07-27 17:31:36 +02002557 // Now we are really going to close the window. Disallow any autocommand
2558 // to split a window to avoid trouble.
2559 ++split_disallowed;
2560
Bram Moolenaara971b822011-09-14 14:43:25 +02002561 /* Free the memory used for the window and get the window that received
2562 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002563 wp = win_free_mem(win, &dir, NULL);
2564
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 /* Make sure curwin isn't invalid. It can cause severe trouble when
2566 * printing an error message. For win_equal() curbuf needs to be valid
2567 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002568 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
2570 curwin = wp;
2571#ifdef FEAT_QUICKFIX
2572 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2573 {
2574 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002575 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 * finding another window to go to.
2577 */
2578 for (;;)
2579 {
2580 if (wp->w_next == NULL)
2581 wp = firstwin;
2582 else
2583 wp = wp->w_next;
2584 if (wp == curwin)
2585 break;
2586 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2587 {
2588 curwin = wp;
2589 break;
2590 }
2591 }
2592 }
2593#endif
2594 curbuf = curwin->w_buffer;
2595 close_curwin = TRUE;
Bram Moolenaarf79225e2017-03-18 23:11:04 +01002596
2597 /* The cursor position may be invalid if the buffer changed after last
2598 * using the window. */
2599 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002601 if (p_ea && (*p_ead == 'b' || *p_ead == dir))
Bram Moolenaar8eeeba82017-06-25 22:45:39 +02002602 /* If the frame of the closed window contains the new current window,
2603 * only resize that frame. Otherwise resize all windows. */
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002604 win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 else
2606 win_comp_pos();
2607 if (close_curwin)
2608 {
Bram Moolenaarc917da42016-07-19 22:31:36 +02002609 win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 if (other_buffer)
2611 /* careful: after this wp and win may be invalid! */
2612 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 }
2614
Bram Moolenaar1417c762019-07-27 17:31:36 +02002615 --split_disallowed;
2616
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002618 * If last window has a status line now and we don't want one,
2619 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 */
2621 last_status(FALSE);
2622
2623 /* After closing the help window, try restoring the window layout from
2624 * before it was opened. */
2625 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002626 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002628#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2630 if (gui.in_use && !win_hasvertsplit())
2631 gui_init_which_components(NULL);
2632#endif
2633
2634 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002635 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636}
2637
2638/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002639 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002640 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002641 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002642 * Caller must check if buffer is hidden and whether the tabline needs to be
2643 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002644 */
2645 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002646win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002647{
2648 win_T *wp;
2649 int dir;
2650 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002651 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002652
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002653 /* Get here with win->w_buffer == NULL when win_close() detects the tab
2654 * page changed. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002655 if (win->w_closing || (win->w_buffer != NULL
2656 && win->w_buffer->b_locked > 0))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002657 return; /* window is already being closed */
Bram Moolenaar362ce482012-06-06 19:02:45 +02002658
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002659 if (win->w_buffer != NULL)
2660 /* Close the link to the buffer. */
2661 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002662
2663 /* Careful: Autocommands may have closed the tab page or made it the
2664 * current tab page. */
2665 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2666 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002667 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002668 return;
2669
2670 /* Autocommands may have closed the window already. */
2671 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2672 ;
2673 if (wp == NULL)
2674 return;
2675
Bram Moolenaarf740b292006-02-16 22:11:02 +00002676 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002677 if (tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002678 {
2679 if (tp == first_tabpage)
2680 first_tabpage = tp->tp_next;
2681 else
2682 {
2683 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2684 ptp = ptp->tp_next)
2685 ;
2686 if (ptp == NULL)
2687 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002688 internal_error("win_close_othertab()");
Bram Moolenaarf740b292006-02-16 22:11:02 +00002689 return;
2690 }
2691 ptp->tp_next = tp->tp_next;
2692 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002693 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002694 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002695
2696 /* Free the memory used for the window. */
2697 win_free_mem(win, &dir, tp);
2698
2699 if (free_tp)
2700 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002701}
2702
2703/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002704 * Free the memory used for a window.
2705 * Returns a pointer to the window that got the freed up space.
2706 */
2707 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002708win_free_mem(
2709 win_T *win,
2710 int *dirp, /* set to 'v' or 'h' for direction if 'ea' */
2711 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002712{
2713 frame_T *frp;
2714 win_T *wp;
2715
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002716 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002717 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002718 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002719 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002720 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002721
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002722 /* When deleting the current window of another tab page select a new
2723 * current window. */
2724 if (tp != NULL && win == tp->tp_curwin)
2725 tp->tp_curwin = wp;
2726
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002727 return wp;
2728}
2729
2730#if defined(EXITFREE) || defined(PROTO)
2731 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002732win_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002733{
2734 int dummy;
2735
Bram Moolenaarf740b292006-02-16 22:11:02 +00002736 while (first_tabpage->tp_next != NULL)
2737 tabpage_close(TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002738
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002739 if (aucmd_win != NULL)
2740 {
2741 (void)win_free_mem(aucmd_win, &dummy, NULL);
2742 aucmd_win = NULL;
2743 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002744# ifdef FEAT_TEXT_PROP
2745 close_all_popups();
2746# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002747
2748 while (firstwin != NULL)
2749 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002750
2751 /* No window should be used after this. Set curwin to NULL to crash
2752 * instead of using freed memory. */
2753 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002754}
2755#endif
2756
2757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 * Remove a window and its frame from the tree of frames.
2759 * Returns a pointer to the window that got the freed up space.
2760 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002761 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002762winframe_remove(
2763 win_T *win,
2764 int *dirp UNUSED, /* set to 'v' or 'h' for direction if 'ea' */
2765 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766{
2767 frame_T *frp, *frp2, *frp3;
2768 frame_T *frp_close = win->w_frame;
2769 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770
2771 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002772 * If there is only one window there is nothing to remove.
2773 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002774 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002775 return NULL;
2776
2777 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 * Remove the window from its frame.
2779 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002780 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 wp = frame2win(frp2);
2782
2783 /* Remove this frame from the list of frames. */
2784 frame_remove(frp_close);
2785
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 if (frp_close->fr_parent->fr_layout == FR_COL)
2787 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002788 /* When 'winfixheight' is set, try to find another frame in the column
2789 * (as close to the closed frame as possible) to distribute the height
2790 * to. */
2791 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2792 {
2793 frp = frp_close->fr_prev;
2794 frp3 = frp_close->fr_next;
2795 while (frp != NULL || frp3 != NULL)
2796 {
2797 if (frp != NULL)
2798 {
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002799 if (!frame_fixed_height(frp))
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002800 {
2801 frp2 = frp;
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002802 wp = frame2win(frp2);
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002803 break;
2804 }
2805 frp = frp->fr_prev;
2806 }
2807 if (frp3 != NULL)
2808 {
2809 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2810 {
2811 frp2 = frp3;
2812 wp = frp3->fr_win;
2813 break;
2814 }
2815 frp3 = frp3->fr_next;
2816 }
2817 }
2818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2820 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 *dirp = 'v';
2822 }
2823 else
2824 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002825 /* When 'winfixwidth' is set, try to find another frame in the column
2826 * (as close to the closed frame as possible) to distribute the width
2827 * to. */
2828 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2829 {
2830 frp = frp_close->fr_prev;
2831 frp3 = frp_close->fr_next;
2832 while (frp != NULL || frp3 != NULL)
2833 {
2834 if (frp != NULL)
2835 {
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002836 if (!frame_fixed_width(frp))
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002837 {
2838 frp2 = frp;
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002839 wp = frame2win(frp2);
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002840 break;
2841 }
2842 frp = frp->fr_prev;
2843 }
2844 if (frp3 != NULL)
2845 {
2846 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2847 {
2848 frp2 = frp3;
2849 wp = frp3->fr_win;
2850 break;
2851 }
2852 frp3 = frp3->fr_next;
2853 }
2854 }
2855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002857 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 *dirp = 'h';
2859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860
2861 /* If rows/columns go to a window below/right its positions need to be
2862 * updated. Can only be done after the sizes have been updated. */
2863 if (frp2 == frp_close->fr_next)
2864 {
2865 int row = win->w_winrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02002866 int col = win->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
2868 frame_comp_pos(frp2, &row, &col);
2869 }
2870
2871 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2872 {
2873 /* There is no other frame in this list, move its info to the parent
2874 * and remove it. */
2875 frp2->fr_parent->fr_layout = frp2->fr_layout;
2876 frp2->fr_parent->fr_child = frp2->fr_child;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002877 FOR_ALL_FRAMES(frp, frp2->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 frp->fr_parent = frp2->fr_parent;
2879 frp2->fr_parent->fr_win = frp2->fr_win;
2880 if (frp2->fr_win != NULL)
2881 frp2->fr_win->w_frame = frp2->fr_parent;
2882 frp = frp2->fr_parent;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002883 if (topframe->fr_child == frp2)
2884 topframe->fr_child = frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 vim_free(frp2);
2886
2887 frp2 = frp->fr_parent;
2888 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2889 {
2890 /* The frame above the parent has the same layout, have to merge
2891 * the frames into this list. */
2892 if (frp2->fr_child == frp)
2893 frp2->fr_child = frp->fr_child;
2894 frp->fr_child->fr_prev = frp->fr_prev;
2895 if (frp->fr_prev != NULL)
2896 frp->fr_prev->fr_next = frp->fr_child;
2897 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2898 {
2899 frp3->fr_parent = frp2;
2900 if (frp3->fr_next == NULL)
2901 {
2902 frp3->fr_next = frp->fr_next;
2903 if (frp->fr_next != NULL)
2904 frp->fr_next->fr_prev = frp3;
2905 break;
2906 }
2907 }
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002908 if (topframe->fr_child == frp)
2909 topframe->fr_child = frp2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 vim_free(frp);
2911 }
2912 }
2913
2914 return wp;
2915}
2916
2917/*
Bram Moolenaarc136af22018-05-04 20:15:38 +02002918 * Return a pointer to the frame that will receive the empty screen space that
2919 * is left over after "win" is closed.
2920 *
2921 * If 'splitbelow' or 'splitright' is set, the space goes above or to the left
2922 * by default. Otherwise, the free space goes below or to the right. The
2923 * result is that opening a window and then immediately closing it will
2924 * preserve the initial window layout. The 'wfh' and 'wfw' settings are
2925 * respected when possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 */
2927 static frame_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002928win_altframe(
2929 win_T *win,
2930 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
2932 frame_T *frp;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002933 frame_T *other_fr, *target_fr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002935 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002936 return alt_tabpage()->tp_curwin->w_frame;
2937
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 frp = win->w_frame;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002939
2940 if (frp->fr_prev == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 return frp->fr_next;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002942 if (frp->fr_next == NULL)
2943 return frp->fr_prev;
2944
2945 target_fr = frp->fr_next;
2946 other_fr = frp->fr_prev;
2947 if (p_spr || p_sb)
2948 {
2949 target_fr = frp->fr_prev;
2950 other_fr = frp->fr_next;
2951 }
2952
2953 /* If 'wfh' or 'wfw' is set for the target and not for the alternate
2954 * window, reverse the selection. */
2955 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2956 {
2957 if (frame_fixed_width(target_fr) && !frame_fixed_width(other_fr))
2958 target_fr = other_fr;
2959 }
2960 else
2961 {
2962 if (frame_fixed_height(target_fr) && !frame_fixed_height(other_fr))
2963 target_fr = other_fr;
2964 }
2965
2966 return target_fr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967}
2968
2969/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002970 * Return the tabpage that will be used if the current one is closed.
2971 */
2972 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002973alt_tabpage(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002974{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002975 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002976
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002977 /* Use the next tab page if possible. */
2978 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002979 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002980
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002981 /* Find the last but one tab page. */
2982 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2983 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002984 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002985}
2986
2987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 * Find the left-upper window in frame "frp".
2989 */
2990 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002991frame2win(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
2993 while (frp->fr_win == NULL)
2994 frp = frp->fr_child;
2995 return frp->fr_win;
2996}
2997
2998/*
2999 * Return TRUE if frame "frp" contains window "wp".
3000 */
3001 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003002frame_has_win(frame_T *frp, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003{
3004 frame_T *p;
3005
3006 if (frp->fr_layout == FR_LEAF)
3007 return frp->fr_win == wp;
3008
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003009 FOR_ALL_FRAMES(p, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 if (frame_has_win(p, wp))
3011 return TRUE;
3012 return FALSE;
3013}
3014
3015/*
3016 * Set a new height for a frame. Recursively sets the height for contained
3017 * frames and windows. Caller must take care of positions.
3018 */
3019 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003020frame_new_height(
3021 frame_T *topfrp,
3022 int height,
3023 int topfirst, /* resize topmost contained frame first */
3024 int wfh) /* obey 'winfixheight' when there is a choice;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 may cause the height not to be set */
3026{
3027 frame_T *frp;
3028 int extra_lines;
3029 int h;
3030
3031 if (topfrp->fr_win != NULL)
3032 {
3033 /* Simple case: just one window. */
3034 win_new_height(topfrp->fr_win,
Bram Moolenaard326ad62017-09-18 20:31:41 +02003035 height - topfrp->fr_win->w_status_height
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01003036 - WINBAR_HEIGHT(topfrp->fr_win));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 else if (topfrp->fr_layout == FR_ROW)
3039 {
3040 do
3041 {
3042 /* All frames in this row get the same new height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003043 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 {
3045 frame_new_height(frp, height, topfirst, wfh);
3046 if (frp->fr_height > height)
3047 {
3048 /* Could not fit the windows, make the whole row higher. */
3049 height = frp->fr_height;
3050 break;
3051 }
3052 }
3053 }
3054 while (frp != NULL);
3055 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003056 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 {
3058 /* Complicated case: Resize a column of frames. Resize the bottom
3059 * frame first, frames above that when needed. */
3060
3061 frp = topfrp->fr_child;
3062 if (wfh)
3063 /* Advance past frames with one window with 'wfh' set. */
3064 while (frame_fixed_height(frp))
3065 {
3066 frp = frp->fr_next;
3067 if (frp == NULL)
3068 return; /* no frame without 'wfh', give up */
3069 }
3070 if (!topfirst)
3071 {
3072 /* Find the bottom frame of this column */
3073 while (frp->fr_next != NULL)
3074 frp = frp->fr_next;
3075 if (wfh)
3076 /* Advance back for frames with one window with 'wfh' set. */
3077 while (frame_fixed_height(frp))
3078 frp = frp->fr_prev;
3079 }
3080
3081 extra_lines = height - topfrp->fr_height;
3082 if (extra_lines < 0)
3083 {
3084 /* reduce height of contained frames, bottom or top frame first */
3085 while (frp != NULL)
3086 {
3087 h = frame_minheight(frp, NULL);
3088 if (frp->fr_height + extra_lines < h)
3089 {
3090 extra_lines += frp->fr_height - h;
3091 frame_new_height(frp, h, topfirst, wfh);
3092 }
3093 else
3094 {
3095 frame_new_height(frp, frp->fr_height + extra_lines,
3096 topfirst, wfh);
3097 break;
3098 }
3099 if (topfirst)
3100 {
3101 do
3102 frp = frp->fr_next;
3103 while (wfh && frp != NULL && frame_fixed_height(frp));
3104 }
3105 else
3106 {
3107 do
3108 frp = frp->fr_prev;
3109 while (wfh && frp != NULL && frame_fixed_height(frp));
3110 }
3111 /* Increase "height" if we could not reduce enough frames. */
3112 if (frp == NULL)
3113 height -= extra_lines;
3114 }
3115 }
3116 else if (extra_lines > 0)
3117 {
3118 /* increase height of bottom or top frame */
3119 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
3120 }
3121 }
3122 topfrp->fr_height = height;
3123}
3124
3125/*
3126 * Return TRUE if height of frame "frp" should not be changed because of
3127 * the 'winfixheight' option.
3128 */
3129 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003130frame_fixed_height(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
3132 /* frame with one window: fixed height if 'winfixheight' set. */
3133 if (frp->fr_win != NULL)
3134 return frp->fr_win->w_p_wfh;
3135
3136 if (frp->fr_layout == FR_ROW)
3137 {
3138 /* The frame is fixed height if one of the frames in the row is fixed
3139 * height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003140 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 if (frame_fixed_height(frp))
3142 return TRUE;
3143 return FALSE;
3144 }
3145
3146 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
3147 * frames in the row are fixed height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003148 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 if (!frame_fixed_height(frp))
3150 return FALSE;
3151 return TRUE;
3152}
3153
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003155 * Return TRUE if width of frame "frp" should not be changed because of
3156 * the 'winfixwidth' option.
3157 */
3158 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003159frame_fixed_width(frame_T *frp)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003160{
3161 /* frame with one window: fixed width if 'winfixwidth' set. */
3162 if (frp->fr_win != NULL)
3163 return frp->fr_win->w_p_wfw;
3164
3165 if (frp->fr_layout == FR_COL)
3166 {
3167 /* The frame is fixed width if one of the frames in the row is fixed
3168 * width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003169 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003170 if (frame_fixed_width(frp))
3171 return TRUE;
3172 return FALSE;
3173 }
3174
3175 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
3176 * frames in the row are fixed width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003177 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003178 if (!frame_fixed_width(frp))
3179 return FALSE;
3180 return TRUE;
3181}
3182
3183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 * Add a status line to windows at the bottom of "frp".
3185 * Note: Does not check if there is room!
3186 */
3187 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003188frame_add_statusline(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189{
3190 win_T *wp;
3191
3192 if (frp->fr_layout == FR_LEAF)
3193 {
3194 wp = frp->fr_win;
3195 if (wp->w_status_height == 0)
3196 {
3197 if (wp->w_height > 0) /* don't make it negative */
3198 --wp->w_height;
3199 wp->w_status_height = STATUS_HEIGHT;
3200 }
3201 }
3202 else if (frp->fr_layout == FR_ROW)
3203 {
3204 /* Handle all the frames in the row. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003205 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 frame_add_statusline(frp);
3207 }
3208 else /* frp->fr_layout == FR_COL */
3209 {
3210 /* Only need to handle the last frame in the column. */
3211 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3212 ;
3213 frame_add_statusline(frp);
3214 }
3215}
3216
3217/*
3218 * Set width of a frame. Handles recursively going through contained frames.
3219 * May remove separator line for windows at the right side (for win_close()).
3220 */
3221 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003222frame_new_width(
3223 frame_T *topfrp,
3224 int width,
3225 int leftfirst, /* resize leftmost contained frame first */
3226 int wfw) /* obey 'winfixwidth' when there is a choice;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003227 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228{
3229 frame_T *frp;
3230 int extra_cols;
3231 int w;
3232 win_T *wp;
3233
3234 if (topfrp->fr_layout == FR_LEAF)
3235 {
3236 /* Simple case: just one window. */
3237 wp = topfrp->fr_win;
3238 /* Find out if there are any windows right of this one. */
3239 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3240 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3241 break;
3242 if (frp->fr_parent == NULL)
3243 wp->w_vsep_width = 0;
3244 win_new_width(wp, width - wp->w_vsep_width);
3245 }
3246 else if (topfrp->fr_layout == FR_COL)
3247 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003248 do
3249 {
3250 /* All frames in this column get the same new width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003251 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003252 {
3253 frame_new_width(frp, width, leftfirst, wfw);
3254 if (frp->fr_width > width)
3255 {
3256 /* Could not fit the windows, make whole column wider. */
3257 width = frp->fr_width;
3258 break;
3259 }
3260 }
3261 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 }
3263 else /* fr_layout == FR_ROW */
3264 {
3265 /* Complicated case: Resize a row of frames. Resize the rightmost
3266 * frame first, frames left of it when needed. */
3267
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003269 if (wfw)
3270 /* Advance past frames with one window with 'wfw' set. */
3271 while (frame_fixed_width(frp))
3272 {
3273 frp = frp->fr_next;
3274 if (frp == NULL)
3275 return; /* no frame without 'wfw', give up */
3276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003278 {
3279 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 while (frp->fr_next != NULL)
3281 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003282 if (wfw)
3283 /* Advance back for frames with one window with 'wfw' set. */
3284 while (frame_fixed_width(frp))
3285 frp = frp->fr_prev;
3286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
3288 extra_cols = width - topfrp->fr_width;
3289 if (extra_cols < 0)
3290 {
3291 /* reduce frame width, rightmost frame first */
3292 while (frp != NULL)
3293 {
3294 w = frame_minwidth(frp, NULL);
3295 if (frp->fr_width + extra_cols < w)
3296 {
3297 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003298 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 }
3300 else
3301 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003302 frame_new_width(frp, frp->fr_width + extra_cols,
3303 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 break;
3305 }
3306 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003307 {
3308 do
3309 frp = frp->fr_next;
3310 while (wfw && frp != NULL && frame_fixed_width(frp));
3311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003313 {
3314 do
3315 frp = frp->fr_prev;
3316 while (wfw && frp != NULL && frame_fixed_width(frp));
3317 }
3318 /* Increase "width" if we could not reduce enough frames. */
3319 if (frp == NULL)
3320 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 }
3322 }
3323 else if (extra_cols > 0)
3324 {
3325 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003326 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 }
3328 }
3329 topfrp->fr_width = width;
3330}
3331
3332/*
3333 * Add the vertical separator to windows at the right side of "frp".
3334 * Note: Does not check if there is room!
3335 */
3336 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003337frame_add_vsep(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
3339 win_T *wp;
3340
3341 if (frp->fr_layout == FR_LEAF)
3342 {
3343 wp = frp->fr_win;
3344 if (wp->w_vsep_width == 0)
3345 {
3346 if (wp->w_width > 0) /* don't make it negative */
3347 --wp->w_width;
3348 wp->w_vsep_width = 1;
3349 }
3350 }
3351 else if (frp->fr_layout == FR_COL)
3352 {
3353 /* Handle all the frames in the column. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003354 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 frame_add_vsep(frp);
3356 }
3357 else /* frp->fr_layout == FR_ROW */
3358 {
3359 /* Only need to handle the last frame in the row. */
3360 frp = frp->fr_child;
3361 while (frp->fr_next != NULL)
3362 frp = frp->fr_next;
3363 frame_add_vsep(frp);
3364 }
3365}
3366
3367/*
3368 * Set frame width from the window it contains.
3369 */
3370 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003371frame_fix_width(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
3373 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3374}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375
3376/*
3377 * Set frame height from the window it contains.
3378 */
3379 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003380frame_fix_height(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
Bram Moolenaar415a6932017-12-05 20:31:07 +01003382 wp->w_frame->fr_height = VISIBLE_HEIGHT(wp) + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383}
3384
3385/*
3386 * Compute the minimal height for frame "topfrp".
3387 * Uses the 'winminheight' option.
3388 * When "next_curwin" isn't NULL, use p_wh for this window.
3389 * When "next_curwin" is NOWIN, don't use at least one line for the current
3390 * window.
3391 */
3392 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003393frame_minheight(frame_T *topfrp, win_T *next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
3395 frame_T *frp;
3396 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398
3399 if (topfrp->fr_win != NULL)
3400 {
3401 if (topfrp->fr_win == next_curwin)
3402 m = p_wh + topfrp->fr_win->w_status_height;
3403 else
3404 {
3405 /* window: minimal height of the window plus status line */
3406 m = p_wmh + topfrp->fr_win->w_status_height;
Bram Moolenaar415a6932017-12-05 20:31:07 +01003407 if (topfrp->fr_win == curwin && next_curwin == NULL)
3408 {
3409 /* Current window is minimal one line high and WinBar is
3410 * visible. */
3411 if (p_wmh == 0)
3412 ++m;
3413 m += WINBAR_HEIGHT(curwin);
3414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 }
3416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 else if (topfrp->fr_layout == FR_ROW)
3418 {
3419 /* get the minimal height from each frame in this row */
3420 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003421 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 {
3423 n = frame_minheight(frp, next_curwin);
3424 if (n > m)
3425 m = n;
3426 }
3427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 else
3429 {
3430 /* Add up the minimal heights for all frames in this column. */
3431 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003432 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 m += frame_minheight(frp, next_curwin);
3434 }
3435
3436 return m;
3437}
3438
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439/*
3440 * Compute the minimal width for frame "topfrp".
3441 * When "next_curwin" isn't NULL, use p_wiw for this window.
3442 * When "next_curwin" is NOWIN, don't use at least one column for the current
3443 * window.
3444 */
3445 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003446frame_minwidth(
3447 frame_T *topfrp,
3448 win_T *next_curwin) /* use p_wh and p_wiw for next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449{
3450 frame_T *frp;
3451 int m, n;
3452
3453 if (topfrp->fr_win != NULL)
3454 {
3455 if (topfrp->fr_win == next_curwin)
3456 m = p_wiw + topfrp->fr_win->w_vsep_width;
3457 else
3458 {
3459 /* window: minimal width of the window plus separator column */
3460 m = p_wmw + topfrp->fr_win->w_vsep_width;
3461 /* Current window is minimal one column wide */
3462 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3463 ++m;
3464 }
3465 }
3466 else if (topfrp->fr_layout == FR_COL)
3467 {
3468 /* get the minimal width from each frame in this column */
3469 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003470 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 {
3472 n = frame_minwidth(frp, next_curwin);
3473 if (n > m)
3474 m = n;
3475 }
3476 }
3477 else
3478 {
3479 /* Add up the minimal widths for all frames in this row. */
3480 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003481 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 m += frame_minwidth(frp, next_curwin);
3483 }
3484
3485 return m;
3486}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
3488
3489/*
3490 * Try to close all windows except current one.
3491 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3492 * used and the buffer was modified.
3493 *
3494 * Used by ":bdel" and ":only".
3495 */
3496 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003497close_others(
3498 int message,
3499 int forceit) /* always hide all other windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
3501 win_T *wp;
3502 win_T *nextwp;
3503 int r;
3504
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003505 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003507 if (message && !autocmd_busy)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003508 msg(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 return;
3510 }
3511
3512 /* Be very careful here: autocommands may change the window layout. */
3513 for (wp = firstwin; win_valid(wp); wp = nextwp)
3514 {
3515 nextwp = wp->w_next;
3516 if (wp != curwin) /* don't close current window */
3517 {
3518
3519 /* Check if it's allowed to abandon this window */
3520 r = can_abandon(wp->w_buffer, forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (!win_valid(wp)) /* autocommands messed wp up */
3522 {
3523 nextwp = firstwin;
3524 continue;
3525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 if (!r)
3527 {
3528#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3529 if (message && (p_confirm || cmdmod.confirm) && p_write)
3530 {
3531 dialog_changed(wp->w_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 if (!win_valid(wp)) /* autocommands messed wp up */
3533 {
3534 nextwp = firstwin;
3535 continue;
3536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
3538 if (bufIsChanged(wp->w_buffer))
3539#endif
3540 continue;
3541 }
Bram Moolenaareb44a682017-08-03 22:44:55 +02003542 win_close(wp, !buf_hide(wp->w_buffer)
3543 && !bufIsChanged(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
3545 }
3546
Bram Moolenaar459ca562016-11-10 18:16:33 +01003547 if (message && !ONE_WINDOW)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003548 emsg(_("E445: Other window contains changes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549}
3550
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003551 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003552win_init_empty(win_T *wp)
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003553{
3554 redraw_win_later(wp, NOT_VALID);
3555 wp->w_lines_valid = 0;
3556 wp->w_cursor.lnum = 1;
3557 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003558 wp->w_cursor.coladd = 0;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003559 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3560 wp->w_pcmark.col = 0;
3561 wp->w_prev_pcmark.lnum = 0;
3562 wp->w_prev_pcmark.col = 0;
3563 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003565 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003567 wp->w_botline = 2;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003568#if defined(FEAT_SYN_HL) || defined(FEAT_SPELL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003569 wp->w_s = &wp->w_buffer->b_s;
3570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571}
3572
3573/*
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003574 * Init the current window "curwin".
3575 * Called when a new file is being edited.
3576 */
3577 void
3578curwin_init(void)
3579{
3580 win_init_empty(curwin);
3581}
3582
3583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 * Allocate the first window and put an empty buffer in it.
3585 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003586 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003588 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003589win_alloc_first(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003591 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003592 return FAIL;
3593
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003594 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003595 if (first_tabpage == NULL)
3596 return FAIL;
3597 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003598 curtab = first_tabpage;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003599
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003600 return OK;
3601}
3602
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003603/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003604 * Allocate and init a window that is not a regular window.
3605 * This can only be done after the first window is fully initialized, thus it
3606 * can't be in win_alloc_first().
3607 */
3608 win_T *
3609win_alloc_popup_win(void)
3610{
3611 win_T *wp;
3612
3613 wp = win_alloc(NULL, TRUE);
3614 if (wp != NULL)
3615 {
3616 // We need to initialize options with something, using the current
3617 // window makes most sense.
3618 win_init_some(wp, curwin);
3619
3620 RESET_BINDING(wp);
3621 new_frame(wp);
3622 }
3623 return wp;
3624}
3625
3626/*
3627 * Initialize window "wp" to display buffer "buf".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003628 */
3629 void
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003630win_init_popup_win(win_T *wp, buf_T *buf)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003631{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003632 wp->w_buffer = buf;
3633 ++buf->b_nwindows;
3634 win_init_empty(wp); // set cursor and topline to safe values
3635
3636 // Make sure w_localdir and globaldir are NULL to avoid a chdir() in
3637 // win_enter_ext().
3638 VIM_CLEAR(wp->w_localdir);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003639}
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003640
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003641/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003642 * Allocate the first window or the first window in a new tab page.
3643 * When "oldwin" is NULL create an empty buffer for it.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003644 * When "oldwin" is not NULL copy info from it to the new window.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003645 * Return FAIL when something goes wrong (out of memory).
3646 */
3647 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003648win_alloc_firstwin(win_T *oldwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003649{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003650 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003651 if (oldwin == NULL)
3652 {
3653 /* Very first window, need to create an empty buffer for it and
3654 * initialize from scratch. */
3655 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3656 if (curwin == NULL || curbuf == NULL)
3657 return FAIL;
3658 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003659#ifdef FEAT_SYN_HL
3660 curwin->w_s = &(curbuf->b_s);
3661#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003662 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003663 curwin->w_alist = &global_alist;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003664 curwin_init(); /* init current window */
3665 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003666 else
3667 {
3668 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003669 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003670
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003671 /* We don't want cursor- and scroll-binding in the first window. */
3672 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003675 new_frame(curwin);
3676 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003677 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003678 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 topframe->fr_height = Rows - p_ch;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003681
3682 return OK;
3683}
3684
3685/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003686 * Create a frame for window "wp".
3687 */
3688 static void
3689new_frame(win_T *wp)
3690{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003691 frame_T *frp = ALLOC_CLEAR_ONE(frame_T);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003692
3693 wp->w_frame = frp;
3694 if (frp != NULL)
3695 {
3696 frp->fr_layout = FR_LEAF;
3697 frp->fr_win = wp;
3698 }
3699}
3700
3701/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003702 * Initialize the window and frame size to the maximum.
3703 */
3704 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003705win_init_size(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003706{
3707 firstwin->w_height = ROWS_AVAIL;
3708 topframe->fr_height = ROWS_AVAIL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003709 firstwin->w_width = Columns;
3710 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711}
3712
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003713/*
3714 * Allocate a new tabpage_T and init the values.
3715 * Returns NULL when out of memory.
3716 */
3717 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003718alloc_tabpage(void)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003719{
3720 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003721# ifdef FEAT_GUI
3722 int i;
3723# endif
3724
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003725
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003726 tp = ALLOC_CLEAR_ONE(tabpage_T);
Bram Moolenaar429fa852013-04-15 12:27:36 +02003727 if (tp == NULL)
3728 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003729
Bram Moolenaar429fa852013-04-15 12:27:36 +02003730# ifdef FEAT_EVAL
3731 /* init t: variables */
3732 tp->tp_vars = dict_alloc();
3733 if (tp->tp_vars == NULL)
3734 {
3735 vim_free(tp);
3736 return NULL;
3737 }
3738 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3739# endif
3740
3741# ifdef FEAT_GUI
3742 for (i = 0; i < 3; i++)
3743 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003744# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003745# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003746 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003747# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003748 tp->tp_ch_used = p_ch;
3749
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003750 return tp;
3751}
3752
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003753 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003754free_tabpage(tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003755{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003756 int idx;
3757
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003758# ifdef FEAT_DIFF
3759 diff_clear(tp);
3760# endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003761# ifdef FEAT_TEXT_PROP
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02003762 while (tp->tp_first_popupwin != NULL)
3763 popup_close_tabpage(tp, tp->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003764#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003765 for (idx = 0; idx < SNAP_COUNT; ++idx)
3766 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003767#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003768 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3769 hash_init(&tp->tp_vars->dv_hashtab);
3770 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003771#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003772
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003773 vim_free(tp->tp_localdir);
3774
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003775#ifdef FEAT_PYTHON
3776 python_tabpage_free(tp);
3777#endif
3778
3779#ifdef FEAT_PYTHON3
3780 python3_tabpage_free(tp);
3781#endif
3782
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003783 vim_free(tp);
3784}
3785
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003786/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003787 * Create a new Tab page with one window.
3788 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003789 * When "after" is 0 put it just after the current Tab page.
3790 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003791 * Return FAIL or OK.
3792 */
3793 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003794win_new_tabpage(int after)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003795{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003796 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003797 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003798 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003799
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003800 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003801 if (newtp == NULL)
3802 return FAIL;
3803
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003804 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003805 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003806 {
3807 vim_free(newtp);
3808 return FAIL;
3809 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003810 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003811
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003812 newtp->tp_localdir = (tp->tp_localdir == NULL)
3813 ? NULL : vim_strsave(tp->tp_localdir);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003814 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003815 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003816 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003817 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003818 if (after == 1)
3819 {
3820 /* New tab page becomes the first one. */
3821 newtp->tp_next = first_tabpage;
3822 first_tabpage = newtp;
3823 }
3824 else
3825 {
3826 if (after > 0)
3827 {
3828 /* Put new tab page before tab page "after". */
3829 n = 2;
3830 for (tp = first_tabpage; tp->tp_next != NULL
3831 && n < after; tp = tp->tp_next)
3832 ++n;
3833 }
3834 newtp->tp_next = tp->tp_next;
3835 tp->tp_next = newtp;
3836 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003837 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003838 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003839 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003840
3841 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003842 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003843
3844#if defined(FEAT_GUI)
3845 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3846 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003847 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003848#endif
Bram Moolenaar6d41c782018-06-06 09:11:12 +02003849#ifdef FEAT_JOB_CHANNEL
3850 entering_window(curwin);
3851#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003852
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01003853 redraw_all_later(NOT_VALID);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003854 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003855 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003856 apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003857 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003858 return OK;
3859 }
3860
3861 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003862 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003863 return FAIL;
3864}
3865
3866/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003867 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3868 * like with ":split".
3869 * Returns OK if a new tab page was created, FAIL otherwise.
3870 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003871 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003872may_open_tabpage(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003873{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003874 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003875
Bram Moolenaard326ce82007-03-11 14:48:29 +00003876 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003877 {
3878 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003879 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003880 return win_new_tabpage(n);
3881 }
3882 return FAIL;
3883}
3884
3885/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003886 * Create up to "maxcount" tabpages with empty windows.
3887 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003888 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003889 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003890make_tabpages(int maxcount)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003891{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003892 int count = maxcount;
3893 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003894
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003895 /* Limit to 'tabpagemax' tabs. */
3896 if (count > p_tpm)
3897 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003898
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003899 /*
3900 * Don't execute autocommands while creating the tab pages. Must do that
3901 * when putting the buffers in the windows.
3902 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003903 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003904
3905 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003906 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003907 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003908
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003909 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003910
3911 /* return actual number of tab pages */
3912 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003913}
3914
3915/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003916 * Return TRUE when "tpc" points to a valid tab page.
3917 */
3918 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003919valid_tabpage(tabpage_T *tpc)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003920{
3921 tabpage_T *tp;
3922
Bram Moolenaar29323592016-07-24 22:04:11 +02003923 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003924 if (tp == tpc)
3925 return TRUE;
3926 return FALSE;
3927}
3928
3929/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003930 * Return TRUE when "tpc" points to a valid tab page and at least one window is
3931 * valid.
3932 */
3933 int
3934valid_tabpage_win(tabpage_T *tpc)
3935{
3936 tabpage_T *tp;
3937 win_T *wp;
3938
3939 FOR_ALL_TABPAGES(tp)
3940 {
3941 if (tp == tpc)
3942 {
3943 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
3944 {
3945 if (win_valid_any_tab(wp))
3946 return TRUE;
3947 }
3948 return FALSE;
3949 }
3950 }
3951 /* shouldn't happen */
3952 return FALSE;
3953}
3954
3955/*
3956 * Close tabpage "tab", assuming it has no windows in it.
3957 * There must be another tabpage or this will crash.
3958 */
3959 void
3960close_tabpage(tabpage_T *tab)
3961{
3962 tabpage_T *ptp;
3963
3964 if (tab == first_tabpage)
3965 {
3966 first_tabpage = tab->tp_next;
3967 ptp = first_tabpage;
3968 }
3969 else
3970 {
3971 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tab;
3972 ptp = ptp->tp_next)
3973 ;
Bram Moolenaara37ffaa2017-03-21 21:58:00 +01003974 assert(ptp != NULL);
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003975 ptp->tp_next = tab->tp_next;
3976 }
3977
3978 goto_tabpage_tp(ptp, FALSE, FALSE);
3979 free_tabpage(tab);
3980}
3981
3982/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003983 * Find tab page "n" (first one is 1). Returns NULL when not found.
3984 */
3985 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003986find_tabpage(int n)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003987{
3988 tabpage_T *tp;
3989 int i = 1;
3990
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003991 if (n == 0)
3992 return curtab;
3993
Bram Moolenaarf740b292006-02-16 22:11:02 +00003994 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3995 ++i;
3996 return tp;
3997}
3998
3999/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004000 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004001 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004002 */
4003 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004004tabpage_index(tabpage_T *ftp)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004005{
4006 int i = 1;
4007 tabpage_T *tp;
4008
4009 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
4010 ++i;
4011 return i;
4012}
4013
4014/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004015 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004016 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004017 * FAIL.
4018 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004019 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004020 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004021leave_tabpage(
4022 buf_T *new_curbuf UNUSED, /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004023 NULL if unknown */
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004024 int trigger_leave_autocmds UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004025{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004026 tabpage_T *tp = curtab;
4027
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004028#ifdef FEAT_JOB_CHANNEL
4029 leaving_window(curwin);
4030#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004031 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004032 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004033 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004034 if (new_curbuf != curbuf)
4035 {
4036 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4037 if (curtab != tp)
4038 return FAIL;
4039 }
4040 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4041 if (curtab != tp)
4042 return FAIL;
4043 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004044 if (curtab != tp)
4045 return FAIL;
4046 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004047#if defined(FEAT_GUI)
4048 /* Remove the scrollbars. They may be added back later. */
4049 if (gui.in_use)
4050 gui_remove_scrollbars();
4051#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004052 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004053 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004054 tp->tp_firstwin = firstwin;
4055 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004056 tp->tp_old_Rows = Rows;
4057 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004058 firstwin = NULL;
4059 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004060 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004061}
4062
4063/*
4064 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004065 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004066 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4067 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004068 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004069 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004070enter_tabpage(
4071 tabpage_T *tp,
4072 buf_T *old_curbuf UNUSED,
Bram Moolenaarf1d25012016-03-03 12:22:53 +01004073 int trigger_enter_autocmds,
4074 int trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004075{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004076 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00004077 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004078
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004079 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004080 firstwin = tp->tp_firstwin;
4081 lastwin = tp->tp_lastwin;
4082 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00004083
4084 /* We would like doing the TabEnter event first, but we don't have a
4085 * valid current window yet, which may break some commands.
4086 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaarc917da42016-07-19 22:31:36 +02004087 win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE,
Bram Moolenaard6949742013-06-16 14:18:28 +02004088 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00004089 prevwin = next_prevwin;
4090
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004091 last_status(FALSE); /* status line may appear or disappear */
4092 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004093#ifdef FEAT_DIFF
4094 diff_need_scrollbind = TRUE;
4095#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004096
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004097 /* The tabpage line may have appeared or disappeared, may need to resize
4098 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004099 * frame sizes too. Use the stored value of p_ch, so that it can be
4100 * different for each tab page. */
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +02004101 if (p_ch != curtab->tp_ch_used)
4102 clear_cmdline = TRUE;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004103 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004104 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
4105#ifdef FEAT_GUI_TABLINE
4106 && !gui_use_tabline()
4107#endif
4108 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004109 shell_new_rows();
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004110 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004111 shell_new_columns(); /* update window widths */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004112
4113#if defined(FEAT_GUI)
4114 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
4115 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004116 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004117#endif
4118
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01004119 /* Apply autocommands after updating the display, when 'rows' and
4120 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004121 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02004122 {
4123 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
4124 if (old_curbuf != curbuf)
4125 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4126 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01004127
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01004128 redraw_all_later(NOT_VALID);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004129}
4130
4131/*
4132 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004133 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004134 */
4135 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004136goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004137{
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004138 tabpage_T *tp = NULL; // shut up compiler
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004139 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004140 int i;
4141
Bram Moolenaard68071d2006-05-02 22:08:30 +00004142 if (text_locked())
4143 {
4144 /* Not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02004145 text_locked_msg();
Bram Moolenaard68071d2006-05-02 22:08:30 +00004146 return;
4147 }
4148
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004149 /* If there is only one it can't work. */
4150 if (first_tabpage->tp_next == NULL)
4151 {
4152 if (n > 1)
4153 beep_flush();
4154 return;
4155 }
4156
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004157 if (n == 0)
4158 {
4159 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004160 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004161 tp = first_tabpage;
4162 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004163 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004164 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004165 else if (n < 0)
4166 {
4167 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
4168 * this N times. */
4169 ttp = curtab;
4170 for (i = n; i < 0; ++i)
4171 {
4172 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
4173 tp = tp->tp_next)
4174 ;
4175 ttp = tp;
4176 }
4177 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004178 else if (n == 9999)
4179 {
4180 /* Go to last tab page. */
4181 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
4182 ;
4183 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004184 else
4185 {
4186 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004187 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004188 if (tp == NULL)
4189 {
4190 beep_flush();
4191 return;
4192 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004193 }
4194
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004195 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004196
4197#ifdef FEAT_GUI_TABLINE
4198 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004199 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004200#endif
4201}
4202
4203/*
4204 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004205 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4206 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004207 * Note: doesn't update the GUI tab.
4208 */
4209 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004210goto_tabpage_tp(
4211 tabpage_T *tp,
4212 int trigger_enter_autocmds,
4213 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004214{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02004215 /* Don't repeat a message in another tab page. */
4216 set_keep_msg(NULL, 0);
4217
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004218 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4219 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004220 {
4221 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004222 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4223 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004224 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004225 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4226 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004227 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004228}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
4230/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004231 * Enter window "wp" in tab page "tp".
4232 * Also updates the GUI tab.
4233 */
4234 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004235goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004236{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004237 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004238 if (curtab == tp && win_valid(wp))
4239 {
4240 win_enter(wp, TRUE);
4241# ifdef FEAT_GUI_TABLINE
4242 if (gui_use_tabline())
4243 gui_mch_set_curtab(tabpage_index(curtab));
4244# endif
4245 }
4246}
4247
4248/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004249 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004250 */
4251 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004252tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004253{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004254 int n = 1;
4255 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004256
4257 if (first_tabpage->tp_next == NULL)
4258 return;
4259
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004260 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4261 ++n;
4262
4263 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4264 && tp->tp_next == curtab))
4265 return;
4266
4267 tp_dst = tp;
4268
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004269 /* Remove the current tab page from the list of tab pages. */
4270 if (curtab == first_tabpage)
4271 first_tabpage = curtab->tp_next;
4272 else
4273 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004274 FOR_ALL_TABPAGES(tp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004275 if (tp->tp_next == curtab)
4276 break;
4277 if (tp == NULL) /* "cannot happen" */
4278 return;
4279 tp->tp_next = curtab->tp_next;
4280 }
4281
4282 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004283 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004284 {
4285 curtab->tp_next = first_tabpage;
4286 first_tabpage = curtab;
4287 }
4288 else
4289 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004290 curtab->tp_next = tp_dst->tp_next;
4291 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004292 }
4293
4294 /* Need to redraw the tabline. Tab page contents doesn't change. */
4295 redraw_tabline = TRUE;
4296}
4297
4298
4299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 * Go to another window.
4301 * When jumping to another buffer, stop Visual mode. Do this before
4302 * changing windows so we can yank the selection into the '*' register.
4303 * When jumping to another window on the same buffer, adjust its cursor
4304 * position to keep the same Visual area.
4305 */
4306 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004307win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004309#ifdef FEAT_CONCEAL
4310 win_T *owp = curwin;
4311#endif
4312
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02004313 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004314 return;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004315 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 {
4317 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004318 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 return;
4320 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004321 if (curbuf_locked())
4322 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004323
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 if (wp->w_buffer != curbuf)
4325 reset_VIsual_and_resel();
4326 else if (VIsual_active)
4327 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
4329#ifdef FEAT_GUI
4330 need_mouse_correct = TRUE;
4331#endif
4332 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004333
4334#ifdef FEAT_CONCEAL
Bram Moolenaar535d5b62019-01-11 20:45:36 +01004335 // Conceal cursor line in previous window, unconceal in current window.
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004336 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01004337 redrawWinline(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004338 if (curwin->w_p_cole > 0 && !msg_scrolled)
4339 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341}
4342
4343#if defined(FEAT_PERL) || defined(PROTO)
4344/*
4345 * Find window number "winnr" (counting top to bottom).
4346 */
4347 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004348win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349{
4350 win_T *wp;
4351
Bram Moolenaar29323592016-07-24 22:04:11 +02004352 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 if (--winnr == 0)
4354 break;
4355 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356}
4357#endif
4358
Bram Moolenaar4033c552017-09-16 20:54:51 +02004359#if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004360/*
4361 * Find the tabpage for window "win".
4362 */
4363 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004364win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004365{
4366 win_T *wp;
4367 tabpage_T *tp;
4368
Bram Moolenaar29323592016-07-24 22:04:11 +02004369 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004370 if (wp == win)
4371 return tp;
4372 return NULL;
4373}
4374#endif
4375
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376/*
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004377 * Get the above or below neighbor window of the specified window.
4378 * up - TRUE for the above neighbor
4379 * count - nth neighbor window
4380 * Returns the specified window if the neighbor is not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 */
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004382 win_T *
4383win_vert_neighbor(tabpage_T *tp, win_T *wp, int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
4385 frame_T *fr;
4386 frame_T *nfr;
4387 frame_T *foundfr;
4388
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004389 foundfr = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 while (count--)
4391 {
4392 /*
4393 * First go upwards in the tree of frames until we find a upwards or
4394 * downwards neighbor.
4395 */
4396 fr = foundfr;
4397 for (;;)
4398 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004399 if (fr == tp->tp_topframe)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 goto end;
4401 if (up)
4402 nfr = fr->fr_prev;
4403 else
4404 nfr = fr->fr_next;
4405 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4406 break;
4407 fr = fr->fr_parent;
4408 }
4409
4410 /*
4411 * Now go downwards to find the bottom or top frame in it.
4412 */
4413 for (;;)
4414 {
4415 if (nfr->fr_layout == FR_LEAF)
4416 {
4417 foundfr = nfr;
4418 break;
4419 }
4420 fr = nfr->fr_child;
4421 if (nfr->fr_layout == FR_ROW)
4422 {
4423 /* Find the frame at the cursor row. */
4424 while (fr->fr_next != NULL
4425 && frame2win(fr)->w_wincol + fr->fr_width
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004426 <= wp->w_wincol + wp->w_wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 fr = fr->fr_next;
4428 }
4429 if (nfr->fr_layout == FR_COL && up)
4430 while (fr->fr_next != NULL)
4431 fr = fr->fr_next;
4432 nfr = fr;
4433 }
4434 }
4435end:
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004436 return foundfr != NULL ? foundfr->fr_win : NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437}
4438
4439/*
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004440 * Move to window above or below "count" times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 */
4442 static void
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004443win_goto_ver(
4444 int up, // TRUE to go to win above
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004445 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446{
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004447 win_T *win;
4448
4449 win = win_vert_neighbor(curtab, curwin, up, count);
4450 if (win != NULL)
4451 win_goto(win);
4452}
4453
4454/*
4455 * Get the left or right neighbor window of the specified window.
4456 * left - TRUE for the left neighbor
4457 * count - nth neighbor window
4458 * Returns the specified window if the neighbor is not found.
4459 */
4460 win_T *
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02004461win_horz_neighbor(tabpage_T *tp, win_T *wp, int left, long count)
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004462{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 frame_T *fr;
4464 frame_T *nfr;
4465 frame_T *foundfr;
4466
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004467 foundfr = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 while (count--)
4469 {
4470 /*
4471 * First go upwards in the tree of frames until we find a left or
4472 * right neighbor.
4473 */
4474 fr = foundfr;
4475 for (;;)
4476 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004477 if (fr == tp->tp_topframe)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 goto end;
4479 if (left)
4480 nfr = fr->fr_prev;
4481 else
4482 nfr = fr->fr_next;
4483 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4484 break;
4485 fr = fr->fr_parent;
4486 }
4487
4488 /*
4489 * Now go downwards to find the leftmost or rightmost frame in it.
4490 */
4491 for (;;)
4492 {
4493 if (nfr->fr_layout == FR_LEAF)
4494 {
4495 foundfr = nfr;
4496 break;
4497 }
4498 fr = nfr->fr_child;
4499 if (nfr->fr_layout == FR_COL)
4500 {
4501 /* Find the frame at the cursor row. */
4502 while (fr->fr_next != NULL
4503 && frame2win(fr)->w_winrow + fr->fr_height
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004504 <= wp->w_winrow + wp->w_wrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 fr = fr->fr_next;
4506 }
4507 if (nfr->fr_layout == FR_ROW && left)
4508 while (fr->fr_next != NULL)
4509 fr = fr->fr_next;
4510 nfr = fr;
4511 }
4512 }
4513end:
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004514 return foundfr != NULL ? foundfr->fr_win : NULL;
4515}
4516
4517/*
4518 * Move to left or right window.
4519 */
4520 static void
4521win_goto_hor(
4522 int left, // TRUE to go to left win
4523 long count)
4524{
4525 win_T *win;
4526
4527 win = win_horz_neighbor(curtab, curwin, left, count);
4528 if (win != NULL)
4529 win_goto(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
4532/*
4533 * Make window "wp" the current window.
4534 */
4535 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004536win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537{
Bram Moolenaarc917da42016-07-19 22:31:36 +02004538 win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539}
4540
4541/*
4542 * Make window wp the current window.
4543 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4544 * been closed and isn't valid.
4545 */
4546 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004547win_enter_ext(
4548 win_T *wp,
4549 int undo_sync,
4550 int curwin_invalid,
Bram Moolenaar6f470022018-04-10 18:47:20 +02004551 int trigger_new_autocmds,
4552 int trigger_enter_autocmds,
4553 int trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
4557 if (wp == curwin && !curwin_invalid) /* nothing to do */
4558 return;
4559
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004560#ifdef FEAT_JOB_CHANNEL
4561 if (!curwin_invalid)
4562 leaving_window(curwin);
4563#endif
4564
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004565 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 {
4567 /*
4568 * Be careful: If autocommands delete the window, return now.
4569 */
4570 if (wp->w_buffer != curbuf)
4571 {
4572 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4573 other_buffer = TRUE;
4574 if (!win_valid(wp))
4575 return;
4576 }
4577 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4578 if (!win_valid(wp))
4579 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004580#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 /* autocmds may abort script processing */
4582 if (aborting())
4583 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586
4587 /* sync undo before leaving the current buffer */
4588 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004589 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004590
4591 /* Might need to scroll the old window before switching, e.g., when the
4592 * cursor was moved. */
4593 update_topline();
4594
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 /* may have to copy the buffer options when 'cpo' contains 'S' */
4596 if (wp->w_buffer != curbuf)
4597 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4598 if (!curwin_invalid)
4599 {
4600 prevwin = curwin; /* remember for CTRL-W p */
4601 curwin->w_redr_status = TRUE;
4602 }
4603 curwin = wp;
4604 curbuf = wp->w_buffer;
4605 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 if (!virtual_active())
4607 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 changed_line_abv_curs(); /* assume cursor position needs updating */
4609
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004610 if (curwin->w_localdir != NULL || curtab->tp_localdir != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004612 char_u *dirname;
4613
4614 // Window or tab has a local directory: Save current directory as
4615 // global directory (unless that was done already) and change to the
4616 // local directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 if (globaldir == NULL)
4618 {
4619 char_u cwd[MAXPATHL];
4620
4621 if (mch_dirname(cwd, MAXPATHL) == OK)
4622 globaldir = vim_strsave(cwd);
4623 }
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004624 if (curwin->w_localdir != NULL)
4625 dirname = curwin->w_localdir;
4626 else
4627 dirname = curtab->tp_localdir;
4628
4629 if (mch_chdir((char *)dirname) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004630 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 }
4632 else if (globaldir != NULL)
4633 {
4634 /* Window doesn't have a local directory and we are not in the global
4635 * directory: Change to the global directory. */
Bram Moolenaar42335f52018-09-13 15:33:43 +02004636 vim_ignored = mch_chdir((char *)globaldir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004637 VIM_CLEAR(globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 shorten_fnames(TRUE);
4639 }
4640
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004641#ifdef FEAT_JOB_CHANNEL
4642 entering_window(curwin);
4643#endif
Bram Moolenaarc917da42016-07-19 22:31:36 +02004644 if (trigger_new_autocmds)
4645 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004646 if (trigger_enter_autocmds)
4647 {
4648 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4649 if (other_buffer)
4650 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652
4653#ifdef FEAT_TITLE
4654 maketitle();
4655#endif
4656 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004657 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 if (restart_edit)
4659 redraw_later(VALID); /* causes status line redraw */
4660
4661 /* set window height to desired minimal value */
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02004662 if (curwin->w_height < p_wh && !curwin->w_p_wfh
4663#ifdef FEAT_TEXT_PROP
4664 && !popup_is_popup(curwin)
4665#endif
4666 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 win_setheight((int)p_wh);
4668 else if (curwin->w_height == 0)
4669 win_setheight(1);
4670
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004672 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 win_setwidth((int)p_wiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674
4675#ifdef FEAT_MOUSE
4676 setmouse(); /* in case jumped to/from help buffer */
4677#endif
4678
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004679 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02004680 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681}
4682
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004685 * Jump to the first open window that contains buffer "buf", if one exists.
4686 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 */
4688 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004689buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004691 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004693 if (curwin->w_buffer == buf)
4694 wp = curwin;
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004695 else
Bram Moolenaar29323592016-07-24 22:04:11 +02004696 FOR_ALL_WINDOWS(wp)
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004697 if (wp->w_buffer == buf)
4698 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 if (wp != NULL)
4700 win_enter(wp, FALSE);
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004701 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004703
4704/*
4705 * Jump to the first open window in any tab page that contains buffer "buf",
4706 * if one exists.
4707 * Returns a pointer to the window found, otherwise NULL.
4708 */
4709 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004710buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004711{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004712 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004713 tabpage_T *tp;
4714
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004715 if (wp != NULL)
4716 return wp;
4717
Bram Moolenaar29323592016-07-24 22:04:11 +02004718 FOR_ALL_TABPAGES(tp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004719 if (tp != curtab)
4720 {
4721 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4722 if (wp->w_buffer == buf)
4723 break;
4724 if (wp != NULL)
4725 {
4726 goto_tabpage_win(tp, wp);
4727 if (curwin != wp)
4728 wp = NULL; /* something went wrong */
4729 break;
4730 }
4731 }
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004732 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004733}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734
Bram Moolenaar888ccac2016-06-04 18:49:36 +02004735static int last_win_id = LOWEST_WIN_ID - 1;
Bram Moolenaar86edef62016-03-13 18:07:30 +01004736
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004738 * Allocate a window structure and link it in the window list when "hidden" is
4739 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004742win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004744 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
4746 /*
4747 * allocate window structure and linesizes arrays
4748 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004749 new_wp = ALLOC_CLEAR_ONE(win_T);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004750 if (new_wp == NULL)
4751 return NULL;
4752
4753 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004755 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004756 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 }
4758
Bram Moolenaar86edef62016-03-13 18:07:30 +01004759 new_wp->w_id = ++last_win_id;
4760
Bram Moolenaar429fa852013-04-15 12:27:36 +02004761#ifdef FEAT_EVAL
4762 /* init w: variables */
4763 new_wp->w_vars = dict_alloc();
4764 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004766 win_free_lsize(new_wp);
4767 vim_free(new_wp);
4768 return NULL;
4769 }
4770 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004771#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004772
Bram Moolenaar429fa852013-04-15 12:27:36 +02004773 /* Don't execute autocommands while the window is not properly
4774 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4775 * event. */
4776 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004777
Bram Moolenaar429fa852013-04-15 12:27:36 +02004778 /*
4779 * link the window in the window list
4780 */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004781 if (!hidden)
4782 win_append(after, new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004783 new_wp->w_wincol = 0;
4784 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
Bram Moolenaar429fa852013-04-15 12:27:36 +02004786 /* position the display and the cursor at the top of the file. */
4787 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004789 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004791 new_wp->w_botline = 2;
4792 new_wp->w_cursor.lnum = 1;
Bram Moolenaar429fa852013-04-15 12:27:36 +02004793 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794
Bram Moolenaar375e3392019-01-31 18:26:10 +01004795 // use global option value for global-local options
4796 new_wp->w_p_so = -1;
4797 new_wp->w_p_siso = -1;
4798
Bram Moolenaar429fa852013-04-15 12:27:36 +02004799 /* We won't calculate w_fraction until resizing the window */
4800 new_wp->w_fraction = 0;
4801 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802
4803#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004804 if (gui.in_use)
4805 {
4806 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4807 SBAR_LEFT, new_wp);
4808 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4809 SBAR_RIGHT, new_wp);
4810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811#endif
4812#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004813 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004815 unblock_autocmds();
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004816#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004817 new_wp->w_match_head = NULL;
4818 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004819#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004820 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821}
4822
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004824 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 */
4826 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004827win_free(
4828 win_T *wp,
4829 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830{
4831 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004832 buf_T *buf;
4833 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004835#ifdef FEAT_FOLDING
4836 clearFolding(wp);
4837#endif
4838
4839 /* reduce the reference count to the argument list. */
4840 alist_unlink(wp->w_alist);
4841
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004842 /* Don't execute autocommands while the window is halfway being deleted.
4843 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004844 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004845
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004846#ifdef FEAT_LUA
4847 lua_window_free(wp);
4848#endif
4849
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004850#ifdef FEAT_MZSCHEME
4851 mzscheme_window_free(wp);
4852#endif
4853
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854#ifdef FEAT_PERL
4855 perl_win_free(wp);
4856#endif
4857
4858#ifdef FEAT_PYTHON
4859 python_window_free(wp);
4860#endif
4861
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004862#ifdef FEAT_PYTHON3
4863 python3_window_free(wp);
4864#endif
4865
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866#ifdef FEAT_TCL
4867 tcl_window_free(wp);
4868#endif
4869
4870#ifdef FEAT_RUBY
4871 ruby_window_free(wp);
4872#endif
4873
4874 clear_winopt(&wp->w_onebuf_opt);
4875 clear_winopt(&wp->w_allbuf_opt);
4876
4877#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004878 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4879 hash_init(&wp->w_vars->dv_hashtab);
4880 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881#endif
4882
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004883 {
4884 tabpage_T *ttp;
4885
4886 if (prevwin == wp)
4887 prevwin = NULL;
Bram Moolenaar29323592016-07-24 22:04:11 +02004888 FOR_ALL_TABPAGES(ttp)
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004889 if (ttp->tp_prevwin == wp)
4890 ttp->tp_prevwin = NULL;
4891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 win_free_lsize(wp);
4893
4894 for (i = 0; i < wp->w_tagstacklen; ++i)
Bram Moolenaar55008aa2019-09-01 20:21:56 +02004895 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 vim_free(wp->w_tagstack[i].tagname);
Bram Moolenaar55008aa2019-09-01 20:21:56 +02004897 vim_free(wp->w_tagstack[i].user_data);
4898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004900
Bram Moolenaarff18df02013-07-24 17:51:57 +02004901 /* Remove the window from the b_wininfo lists, it may happen that the
4902 * freed memory is re-used for another window. */
Bram Moolenaar29323592016-07-24 22:04:11 +02004903 FOR_ALL_BUFFERS(buf)
Bram Moolenaarff18df02013-07-24 17:51:57 +02004904 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4905 if (wip->wi_win == wp)
4906 wip->wi_win = NULL;
4907
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004909 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004911
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912#ifdef FEAT_JUMPLIST
4913 free_jumplist(wp);
4914#endif
4915
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004916#ifdef FEAT_QUICKFIX
4917 qf_free_all(wp);
4918#endif
4919
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920#ifdef FEAT_GUI
4921 if (gui.in_use)
4922 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4924 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4925 }
4926#endif /* FEAT_GUI */
4927
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004928#ifdef FEAT_MENU
4929 remove_winbar(wp);
4930#endif
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02004931#ifdef FEAT_TEXT_PROP
Bram Moolenaar9eaac892019-06-01 22:49:29 +02004932 free_callback(&wp->w_close_cb);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02004933 free_callback(&wp->w_filter_cb);
Bram Moolenaar790498b2019-06-01 22:15:29 +02004934 for (i = 0; i < 4; ++i)
4935 VIM_CLEAR(wp->w_border_highlight[i]);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004936 vim_free(wp->w_scrollbar_highlight);
4937 vim_free(wp->w_thumb_highlight);
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004938 vim_free(wp->w_popup_title);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004939 list_unref(wp->w_popup_mask);
Bram Moolenaar0aca2932019-07-26 22:22:38 +02004940 vim_free(wp->w_popup_mask_cells);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02004941#endif
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004942
Bram Moolenaar860cae12010-06-05 23:22:07 +02004943#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004944 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004945#endif
4946
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004947 if (win_valid_any_tab(wp))
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004948 win_remove(wp, tp);
Bram Moolenaar3be85852014-06-12 14:01:31 +02004949 if (autocmd_busy)
4950 {
4951 wp->w_next = au_pending_free_win;
4952 au_pending_free_win = wp;
4953 }
4954 else
4955 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004956
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004957 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958}
4959
4960/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004961 * Return TRUE if "wp" is not in the list of windows: the autocmd window or a
4962 * popup window.
4963 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004964 static int
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004965win_unlisted(win_T *wp)
4966{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004967 return wp == aucmd_win || WIN_IS_POPUP(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004968}
4969
Bram Moolenaar35d5af62019-05-26 20:44:10 +02004970#if defined(FEAT_TEXT_PROP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004971/*
4972 * Free a popup window. This does not take the window out of the window list
4973 * and assumes there is only one toplevel frame, no split.
4974 */
4975 void
4976win_free_popup(win_T *win)
4977{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004978 if (bt_popup(win->w_buffer))
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004979 win_close_buffer(win, DOBUF_WIPE_REUSE, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004980 else
4981 close_buffer(win, win->w_buffer, 0, FALSE);
Bram Moolenaar35d5af62019-05-26 20:44:10 +02004982# if defined(FEAT_TIMERS)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02004983 if (win->w_popup_timer != NULL)
4984 stop_timer(win->w_popup_timer);
Bram Moolenaar35d5af62019-05-26 20:44:10 +02004985# endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004986 vim_free(win->w_frame);
4987 win_free(win, NULL);
4988}
Bram Moolenaar35d5af62019-05-26 20:44:10 +02004989#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004990
4991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 * Append window "wp" in the window list after window "after".
4993 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004994 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004995win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996{
4997 win_T *before;
4998
4999 if (after == NULL) /* after NULL is in front of the first */
5000 before = firstwin;
5001 else
5002 before = after->w_next;
5003
5004 wp->w_next = before;
5005 wp->w_prev = after;
5006 if (after == NULL)
5007 firstwin = wp;
5008 else
5009 after->w_next = wp;
5010 if (before == NULL)
5011 lastwin = wp;
5012 else
5013 before->w_prev = wp;
5014}
5015
5016/*
5017 * Remove a window from the window list.
5018 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00005019 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005020win_remove(
5021 win_T *wp,
5022 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023{
5024 if (wp->w_prev != NULL)
5025 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005026 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02005027 firstwin = curtab->tp_firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005028 else
5029 tp->tp_firstwin = wp->w_next;
Bram Moolenaar816968d2017-09-29 21:29:18 +02005030
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 if (wp->w_next != NULL)
5032 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005033 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02005034 lastwin = curtab->tp_lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005035 else
5036 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037}
5038
5039/*
5040 * Append frame "frp" in a frame list after frame "after".
5041 */
5042 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005043frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044{
5045 frp->fr_next = after->fr_next;
5046 after->fr_next = frp;
5047 if (frp->fr_next != NULL)
5048 frp->fr_next->fr_prev = frp;
5049 frp->fr_prev = after;
5050}
5051
5052/*
5053 * Insert frame "frp" in a frame list before frame "before".
5054 */
5055 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005056frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057{
5058 frp->fr_next = before;
5059 frp->fr_prev = before->fr_prev;
5060 before->fr_prev = frp;
5061 if (frp->fr_prev != NULL)
5062 frp->fr_prev->fr_next = frp;
5063 else
5064 frp->fr_parent->fr_child = frp;
5065}
5066
5067/*
5068 * Remove a frame from a frame list.
5069 */
5070 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005071frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072{
5073 if (frp->fr_prev != NULL)
5074 frp->fr_prev->fr_next = frp->fr_next;
5075 else
Bram Moolenaar6f361c92018-01-31 19:06:50 +01005076 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 frp->fr_parent->fr_child = frp->fr_next;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01005078 /* special case: topframe->fr_child == frp */
5079 if (topframe->fr_child == frp)
5080 topframe->fr_child = frp->fr_next;
5081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 if (frp->fr_next != NULL)
5083 frp->fr_next->fr_prev = frp->fr_prev;
5084}
5085
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086/*
5087 * Allocate w_lines[] for window "wp".
5088 * Return FAIL for failure, OK for success.
5089 */
5090 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005091win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092{
5093 wp->w_lines_valid = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005094 wp->w_lines = ALLOC_CLEAR_MULT(wline_T, Rows );
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 if (wp->w_lines == NULL)
5096 return FAIL;
5097 return OK;
5098}
5099
5100/*
5101 * free lsize arrays for a window
5102 */
5103 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005104win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02005106 /* TODO: why would wp be NULL here? */
5107 if (wp != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005108 VIM_CLEAR(wp->w_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109}
5110
5111/*
5112 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005113 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 */
5115 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005116shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005118 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119
5120 if (firstwin == NULL) /* not initialized yet */
5121 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (h < frame_minheight(topframe, NULL))
5123 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005124
5125 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 * that doesn't result in the right height, forget about that option. */
5127 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02005128 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 frame_new_height(topframe, h, FALSE, FALSE);
5130
5131 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 compute_cmdrow();
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005133 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005134
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135#if 0
5136 /* Disabled: don't want making the screen smaller make a window larger. */
5137 if (p_ea)
5138 win_equal(curwin, FALSE, 'v');
5139#endif
5140}
5141
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142/*
5143 * Called from win_new_shellsize() after Columns changed.
5144 */
5145 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005146shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147{
5148 if (firstwin == NULL) /* not initialized yet */
5149 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005150
5151 /* First try setting the widths of windows with 'winfixwidth'. If that
5152 * doesn't result in the right width, forget about that option. */
5153 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02005154 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005155 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
5156
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
5158#if 0
5159 /* Disabled: don't want making the screen smaller make a window larger. */
5160 if (p_ea)
5161 win_equal(curwin, FALSE, 'h');
5162#endif
5163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164
5165#if defined(FEAT_CMDWIN) || defined(PROTO)
5166/*
5167 * Save the size of all windows in "gap".
5168 */
5169 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005170win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171
5172{
5173 win_T *wp;
5174
5175 ga_init2(gap, (int)sizeof(int), 1);
5176 if (ga_grow(gap, win_count() * 2) == OK)
Bram Moolenaar29323592016-07-24 22:04:11 +02005177 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 {
5179 ((int *)gap->ga_data)[gap->ga_len++] =
5180 wp->w_width + wp->w_vsep_width;
5181 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
5182 }
5183}
5184
5185/*
5186 * Restore window sizes, but only if the number of windows is still the same.
5187 * Does not free the growarray.
5188 */
5189 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005190win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191{
5192 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02005193 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194
5195 if (win_count() * 2 == gap->ga_len)
5196 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005197 /* The order matters, because frames contain other frames, but it's
5198 * difficult to get right. The easy way out is to do it twice. */
5199 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005201 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02005202 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb643e772014-07-16 15:18:26 +02005203 {
5204 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
5205 win_setheight_win(((int *)gap->ga_data)[i++], wp);
5206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
5208 /* recompute the window positions */
5209 (void)win_comp_pos();
5210 }
5211}
5212#endif /* FEAT_CMDWIN */
5213
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214/*
5215 * Update the position for all windows, using the width and height of the
5216 * frames.
5217 * Returns the row just after the last window.
5218 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005219 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005220win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005222 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 int col = 0;
5224
5225 frame_comp_pos(topframe, &row, &col);
5226 return row;
5227}
5228
5229/*
5230 * Update the position of the windows in frame "topfrp", using the width and
5231 * height of the frames.
5232 * "*row" and "*col" are the top-left position of the frame. They are updated
5233 * to the bottom-right position plus one.
5234 */
5235 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005236frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237{
5238 win_T *wp;
5239 frame_T *frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 int startcol;
5241 int startrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005242 int h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243
5244 wp = topfrp->fr_win;
5245 if (wp != NULL)
5246 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005247 if (wp->w_winrow != *row || wp->w_wincol != *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 {
5249 /* position changed, redraw */
5250 wp->w_winrow = *row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 wp->w_wincol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 redraw_win_later(wp, NOT_VALID);
5253 wp->w_redr_status = TRUE;
5254 }
Bram Moolenaar415a6932017-12-05 20:31:07 +01005255 /* WinBar will not show if the window height is zero */
5256 h = VISIBLE_HEIGHT(wp) + wp->w_status_height;
5257 *row += h > topfrp->fr_height ? topfrp->fr_height : h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 *col += wp->w_width + wp->w_vsep_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 }
5260 else
5261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 startrow = *row;
5263 startcol = *col;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005264 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 if (topfrp->fr_layout == FR_ROW)
5267 *row = startrow; /* all frames are at the same row */
5268 else
5269 *col = startcol; /* all frames are at the same col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 frame_comp_pos(frp, row, col);
5271 }
5272 }
5273}
5274
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275/*
5276 * Set current window height and take care of repositioning other windows to
5277 * fit around it.
5278 */
5279 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005280win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281{
5282 win_setheight_win(height, curwin);
5283}
5284
5285/*
5286 * Set the window height of window "win" and take care of repositioning other
5287 * windows to fit around it.
5288 */
5289 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005290win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291{
5292 int row;
5293
5294 if (win == curwin)
5295 {
5296 /* Always keep current window at least one line high, even when
5297 * 'winminheight' is zero. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 if (height < p_wmh)
5299 height = p_wmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (height == 0)
5301 height = 1;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005302 height += WINBAR_HEIGHT(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 }
5304
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 frame_setheight(win->w_frame, height + win->w_status_height);
5306
5307 /* recompute the window positions */
5308 row = win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309
5310 /*
5311 * If there is extra space created between the last window and the command
5312 * line, clear it.
5313 */
5314 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5315 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5316 cmdline_row = row;
5317 msg_row = row;
5318 msg_col = 0;
5319
5320 redraw_all_later(NOT_VALID);
5321}
5322
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323/*
5324 * Set the height of a frame to "height" and take care that all frames and
5325 * windows inside it are resized. Also resize frames on the left and right if
5326 * the are in the same FR_ROW frame.
5327 *
5328 * Strategy:
5329 * If the frame is part of a FR_COL frame, try fitting the frame in that
5330 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5331 * go to containing frames to resize them and make room.
5332 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5333 * Check for the minimal height of the FR_ROW frame.
5334 * At the top level we can also use change the command line height.
5335 */
5336 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005337frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338{
5339 int room; /* total number of lines available */
5340 int take; /* number of lines taken from other windows */
5341 int room_cmdline; /* lines available from cmdline */
5342 int run;
5343 frame_T *frp;
5344 int h;
5345 int room_reserved;
5346
5347 /* If the height already is the desired value, nothing to do. */
5348 if (curfrp->fr_height == height)
5349 return;
5350
5351 if (curfrp->fr_parent == NULL)
5352 {
5353 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005354 if (height > ROWS_AVAIL)
5355 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 if (height > 0)
5357 frame_new_height(curfrp, height, FALSE, FALSE);
5358 }
5359 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5360 {
5361 /* Row of frames: Also need to resize frames left and right of this
5362 * one. First check for the minimal height of these. */
5363 h = frame_minheight(curfrp->fr_parent, NULL);
5364 if (height < h)
5365 height = h;
5366 frame_setheight(curfrp->fr_parent, height);
5367 }
5368 else
5369 {
5370 /*
5371 * Column of frames: try to change only frames in this column.
5372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 /*
5374 * Do this twice:
5375 * 1: compute room available, if it's not enough try resizing the
5376 * containing frame.
5377 * 2: compute the room available and adjust the height to it.
5378 * Try not to reduce the height of a window with 'winfixheight' set.
5379 */
5380 for (run = 1; run <= 2; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
5382 room = 0;
5383 room_reserved = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005384 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 {
5386 if (frp != curfrp
5387 && frp->fr_win != NULL
5388 && frp->fr_win->w_p_wfh)
5389 room_reserved += frp->fr_height;
5390 room += frp->fr_height;
5391 if (frp != curfrp)
5392 room -= frame_minheight(frp, NULL);
5393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 if (curfrp->fr_width != Columns)
5395 room_cmdline = 0;
5396 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 {
5398 room_cmdline = Rows - p_ch - (lastwin->w_winrow
Bram Moolenaar415a6932017-12-05 20:31:07 +01005399 + VISIBLE_HEIGHT(lastwin)
5400 + lastwin->w_status_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 if (room_cmdline < 0)
5402 room_cmdline = 0;
5403 }
5404
5405 if (height <= room + room_cmdline)
5406 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 if (run == 2 || curfrp->fr_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 {
5409 if (height > room + room_cmdline)
5410 height = room + room_cmdline;
5411 break;
5412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 frame_setheight(curfrp->fr_parent, height
5414 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 }
5416
5417 /*
5418 * Compute the number of lines we will take from others frames (can be
5419 * negative!).
5420 */
5421 take = height - curfrp->fr_height;
5422
5423 /* If there is not enough room, also reduce the height of a window
5424 * with 'winfixheight' set. */
5425 if (height > room + room_cmdline - room_reserved)
5426 room_reserved = room + room_cmdline - height;
5427 /* If there is only a 'winfixheight' window and making the
5428 * window smaller, need to make the other window taller. */
5429 if (take < 0 && room - curfrp->fr_height < room_reserved)
5430 room_reserved = 0;
5431
5432 if (take > 0 && room_cmdline > 0)
5433 {
5434 /* use lines from cmdline first */
5435 if (take < room_cmdline)
5436 room_cmdline = take;
5437 take -= room_cmdline;
5438 topframe->fr_height += room_cmdline;
5439 }
5440
5441 /*
5442 * set the current frame to the new height
5443 */
5444 frame_new_height(curfrp, height, FALSE, FALSE);
5445
5446 /*
5447 * First take lines from the frames after the current frame. If
5448 * that is not enough, takes lines from frames above the current
5449 * frame.
5450 */
5451 for (run = 0; run < 2; ++run)
5452 {
5453 if (run == 0)
5454 frp = curfrp->fr_next; /* 1st run: start with next window */
5455 else
5456 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5457 while (frp != NULL && take != 0)
5458 {
5459 h = frame_minheight(frp, NULL);
5460 if (room_reserved > 0
5461 && frp->fr_win != NULL
5462 && frp->fr_win->w_p_wfh)
5463 {
5464 if (room_reserved >= frp->fr_height)
5465 room_reserved -= frp->fr_height;
5466 else
5467 {
5468 if (frp->fr_height - room_reserved > take)
5469 room_reserved = frp->fr_height - take;
5470 take -= frp->fr_height - room_reserved;
5471 frame_new_height(frp, room_reserved, FALSE, FALSE);
5472 room_reserved = 0;
5473 }
5474 }
5475 else
5476 {
5477 if (frp->fr_height - take < h)
5478 {
5479 take -= frp->fr_height - h;
5480 frame_new_height(frp, h, FALSE, FALSE);
5481 }
5482 else
5483 {
5484 frame_new_height(frp, frp->fr_height - take,
5485 FALSE, FALSE);
5486 take = 0;
5487 }
5488 }
5489 if (run == 0)
5490 frp = frp->fr_next;
5491 else
5492 frp = frp->fr_prev;
5493 }
5494 }
5495 }
5496}
5497
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498/*
5499 * Set current window width and take care of repositioning other windows to
5500 * fit around it.
5501 */
5502 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005503win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
5505 win_setwidth_win(width, curwin);
5506}
5507
5508 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005509win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510{
5511 /* Always keep current window at least one column wide, even when
5512 * 'winminwidth' is zero. */
5513 if (wp == curwin)
5514 {
5515 if (width < p_wmw)
5516 width = p_wmw;
5517 if (width == 0)
5518 width = 1;
5519 }
5520
5521 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5522
5523 /* recompute the window positions */
5524 (void)win_comp_pos();
5525
5526 redraw_all_later(NOT_VALID);
5527}
5528
5529/*
5530 * Set the width of a frame to "width" and take care that all frames and
5531 * windows inside it are resized. Also resize frames above and below if the
5532 * are in the same FR_ROW frame.
5533 *
5534 * Strategy is similar to frame_setheight().
5535 */
5536 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005537frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538{
5539 int room; /* total number of lines available */
5540 int take; /* number of lines taken from other windows */
5541 int run;
5542 frame_T *frp;
5543 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005544 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545
5546 /* If the width already is the desired value, nothing to do. */
5547 if (curfrp->fr_width == width)
5548 return;
5549
5550 if (curfrp->fr_parent == NULL)
5551 /* topframe: can't change width */
5552 return;
5553
5554 if (curfrp->fr_parent->fr_layout == FR_COL)
5555 {
5556 /* Column of frames: Also need to resize frames above and below of
5557 * this one. First check for the minimal width of these. */
5558 w = frame_minwidth(curfrp->fr_parent, NULL);
5559 if (width < w)
5560 width = w;
5561 frame_setwidth(curfrp->fr_parent, width);
5562 }
5563 else
5564 {
5565 /*
5566 * Row of frames: try to change only frames in this row.
5567 *
5568 * Do this twice:
5569 * 1: compute room available, if it's not enough try resizing the
5570 * containing frame.
5571 * 2: compute the room available and adjust the width to it.
5572 */
5573 for (run = 1; run <= 2; ++run)
5574 {
5575 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005576 room_reserved = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005577 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005579 if (frp != curfrp
5580 && frp->fr_win != NULL
5581 && frp->fr_win->w_p_wfw)
5582 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 room += frp->fr_width;
5584 if (frp != curfrp)
5585 room -= frame_minwidth(frp, NULL);
5586 }
5587
5588 if (width <= room)
5589 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005590 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 {
5592 if (width > room)
5593 width = room;
5594 break;
5595 }
5596 frame_setwidth(curfrp->fr_parent, width
5597 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5598 }
5599
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 /*
5601 * Compute the number of lines we will take from others frames (can be
5602 * negative!).
5603 */
5604 take = width - curfrp->fr_width;
5605
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005606 /* If there is not enough room, also reduce the width of a window
5607 * with 'winfixwidth' set. */
5608 if (width > room - room_reserved)
5609 room_reserved = room - width;
5610 /* If there is only a 'winfixwidth' window and making the
5611 * window smaller, need to make the other window narrower. */
5612 if (take < 0 && room - curfrp->fr_width < room_reserved)
5613 room_reserved = 0;
5614
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 /*
5616 * set the current frame to the new width
5617 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005618 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619
5620 /*
5621 * First take lines from the frames right of the current frame. If
5622 * that is not enough, takes lines from frames left of the current
5623 * frame.
5624 */
5625 for (run = 0; run < 2; ++run)
5626 {
5627 if (run == 0)
5628 frp = curfrp->fr_next; /* 1st run: start with next window */
5629 else
5630 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5631 while (frp != NULL && take != 0)
5632 {
5633 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005634 if (room_reserved > 0
5635 && frp->fr_win != NULL
5636 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005638 if (room_reserved >= frp->fr_width)
5639 room_reserved -= frp->fr_width;
5640 else
5641 {
5642 if (frp->fr_width - room_reserved > take)
5643 room_reserved = frp->fr_width - take;
5644 take -= frp->fr_width - room_reserved;
5645 frame_new_width(frp, room_reserved, FALSE, FALSE);
5646 room_reserved = 0;
5647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 }
5649 else
5650 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005651 if (frp->fr_width - take < w)
5652 {
5653 take -= frp->fr_width - w;
5654 frame_new_width(frp, w, FALSE, FALSE);
5655 }
5656 else
5657 {
5658 frame_new_width(frp, frp->fr_width - take,
5659 FALSE, FALSE);
5660 take = 0;
5661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 }
5663 if (run == 0)
5664 frp = frp->fr_next;
5665 else
5666 frp = frp->fr_prev;
5667 }
5668 }
5669 }
5670}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
5672/*
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005673 * Check 'winminheight' for a valid value and reduce it if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 */
5675 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005676win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677{
5678 int room;
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005679 int needed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005682 // loop until there is a 'winminheight' that is possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 while (p_wmh > 0)
5684 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005685 room = Rows - p_ch;
5686 needed = frame_minheight(topframe, NULL);
5687 if (room >= needed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 break;
5689 --p_wmh;
5690 if (first)
5691 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005692 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 first = FALSE;
5694 }
5695 }
5696}
5697
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005698/*
5699 * Check 'winminwidth' for a valid value and reduce it if needed.
5700 */
5701 void
5702win_setminwidth(void)
5703{
5704 int room;
5705 int needed;
5706 int first = TRUE;
5707
5708 // loop until there is a 'winminheight' that is possible
5709 while (p_wmw > 0)
5710 {
5711 room = Columns;
5712 needed = frame_minwidth(topframe, NULL);
5713 if (room >= needed)
5714 break;
5715 --p_wmw;
5716 if (first)
5717 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005718 emsg(_(e_noroom));
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005719 first = FALSE;
5720 }
5721 }
5722}
5723
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005724#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
5726/*
5727 * Status line of dragwin is dragged "offset" lines down (negative is up).
5728 */
5729 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005730win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
5732 frame_T *curfr;
5733 frame_T *fr;
5734 int room;
5735 int row;
5736 int up; /* if TRUE, drag status line up, otherwise down */
5737 int n;
5738
5739 fr = dragwin->w_frame;
5740 curfr = fr;
5741 if (fr != topframe) /* more than one window */
5742 {
5743 fr = fr->fr_parent;
5744 /* When the parent frame is not a column of frames, its parent should
5745 * be. */
5746 if (fr->fr_layout != FR_COL)
5747 {
5748 curfr = fr;
5749 if (fr != topframe) /* only a row of windows, may drag statusline */
5750 fr = fr->fr_parent;
5751 }
5752 }
5753
5754 /* If this is the last frame in a column, may want to resize the parent
5755 * frame instead (go two up to skip a row of frames). */
5756 while (curfr != topframe && curfr->fr_next == NULL)
5757 {
5758 if (fr != topframe)
5759 fr = fr->fr_parent;
5760 curfr = fr;
5761 if (fr != topframe)
5762 fr = fr->fr_parent;
5763 }
5764
5765 if (offset < 0) /* drag up */
5766 {
5767 up = TRUE;
5768 offset = -offset;
5769 /* sum up the room of the current frame and above it */
5770 if (fr == curfr)
5771 {
5772 /* only one window */
5773 room = fr->fr_height - frame_minheight(fr, NULL);
5774 }
5775 else
5776 {
5777 room = 0;
5778 for (fr = fr->fr_child; ; fr = fr->fr_next)
5779 {
5780 room += fr->fr_height - frame_minheight(fr, NULL);
5781 if (fr == curfr)
5782 break;
5783 }
5784 }
5785 fr = curfr->fr_next; /* put fr at frame that grows */
5786 }
5787 else /* drag down */
5788 {
5789 up = FALSE;
5790 /*
5791 * Only dragging the last status line can reduce p_ch.
5792 */
5793 room = Rows - cmdline_row;
5794 if (curfr->fr_next == NULL)
5795 room -= 1;
5796 else
5797 room -= p_ch;
5798 if (room < 0)
5799 room = 0;
5800 /* sum up the room of frames below of the current one */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005801 FOR_ALL_FRAMES(fr, curfr->fr_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 room += fr->fr_height - frame_minheight(fr, NULL);
5803 fr = curfr; /* put fr at window that grows */
5804 }
5805
5806 if (room < offset) /* Not enough room */
5807 offset = room; /* Move as far as we can */
5808 if (offset <= 0)
5809 return;
5810
5811 /*
5812 * Grow frame fr by "offset" lines.
5813 * Doesn't happen when dragging the last status line up.
5814 */
5815 if (fr != NULL)
5816 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5817
5818 if (up)
5819 fr = curfr; /* current frame gets smaller */
5820 else
5821 fr = curfr->fr_next; /* next frame gets smaller */
5822
5823 /*
5824 * Now make the other frames smaller.
5825 */
5826 while (fr != NULL && offset > 0)
5827 {
5828 n = frame_minheight(fr, NULL);
5829 if (fr->fr_height - offset <= n)
5830 {
5831 offset -= fr->fr_height - n;
5832 frame_new_height(fr, n, !up, FALSE);
5833 }
5834 else
5835 {
5836 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5837 break;
5838 }
5839 if (up)
5840 fr = fr->fr_prev;
5841 else
5842 fr = fr->fr_next;
5843 }
5844 row = win_comp_pos();
5845 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5846 cmdline_row = row;
5847 p_ch = Rows - cmdline_row;
5848 if (p_ch < 1)
5849 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005850 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005851 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 showmode();
5853}
5854
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855/*
5856 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5857 */
5858 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005859win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860{
5861 frame_T *curfr;
5862 frame_T *fr;
5863 int room;
5864 int left; /* if TRUE, drag separator line left, otherwise right */
5865 int n;
5866
5867 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005868 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 return;
5870 curfr = fr;
5871 fr = fr->fr_parent;
5872 /* When the parent frame is not a row of frames, its parent should be. */
5873 if (fr->fr_layout != FR_ROW)
5874 {
5875 if (fr == topframe) /* only a column of windows (cannot happen?) */
5876 return;
5877 curfr = fr;
5878 fr = fr->fr_parent;
5879 }
5880
5881 /* If this is the last frame in a row, may want to resize a parent
5882 * frame instead. */
5883 while (curfr->fr_next == NULL)
5884 {
5885 if (fr == topframe)
5886 break;
5887 curfr = fr;
5888 fr = fr->fr_parent;
5889 if (fr != topframe)
5890 {
5891 curfr = fr;
5892 fr = fr->fr_parent;
5893 }
5894 }
5895
5896 if (offset < 0) /* drag left */
5897 {
5898 left = TRUE;
5899 offset = -offset;
5900 /* sum up the room of the current frame and left of it */
5901 room = 0;
5902 for (fr = fr->fr_child; ; fr = fr->fr_next)
5903 {
5904 room += fr->fr_width - frame_minwidth(fr, NULL);
5905 if (fr == curfr)
5906 break;
5907 }
5908 fr = curfr->fr_next; /* put fr at frame that grows */
5909 }
5910 else /* drag right */
5911 {
5912 left = FALSE;
5913 /* sum up the room of frames right of the current one */
5914 room = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005915 FOR_ALL_FRAMES(fr, curfr->fr_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 room += fr->fr_width - frame_minwidth(fr, NULL);
5917 fr = curfr; /* put fr at window that grows */
5918 }
5919
5920 if (room < offset) /* Not enough room */
5921 offset = room; /* Move as far as we can */
5922 if (offset <= 0) /* No room at all, quit. */
5923 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005924 if (fr == NULL)
5925 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926
5927 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005928 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929
5930 /* shrink other frames: current and at the left or at the right */
5931 if (left)
5932 fr = curfr; /* current frame gets smaller */
5933 else
5934 fr = curfr->fr_next; /* next frame gets smaller */
5935
5936 while (fr != NULL && offset > 0)
5937 {
5938 n = frame_minwidth(fr, NULL);
5939 if (fr->fr_width - offset <= n)
5940 {
5941 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005942 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 }
5944 else
5945 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005946 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 break;
5948 }
5949 if (left)
5950 fr = fr->fr_prev;
5951 else
5952 fr = fr->fr_next;
5953 }
5954 (void)win_comp_pos();
5955 redraw_all_later(NOT_VALID);
5956}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957#endif /* FEAT_MOUSE */
5958
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005959#define FRACTION_MULT 16384L
5960
5961/*
5962 * Set wp->w_fraction for the current w_wrow and w_height.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005963 * Has no effect when the window is less than two lines.
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005964 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005965 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005966set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005967{
Bram Moolenaar3679c172017-11-22 22:22:11 +01005968 if (wp->w_height > 1)
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005969 // When cursor is in the first line the percentage is computed as if
5970 // it's halfway that line. Thus with two lines it is 25%, with three
5971 // lines 17%, etc. Similarly for the last line: 75%, 83%, etc.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005972 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005973 + FRACTION_MULT / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005974}
5975
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976/*
5977 * Set the height of a window.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005978 * "height" excludes any window toolbar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 * This takes care of the things inside the window, not what happens to the
5980 * window position, the frame or to other windows.
5981 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005982 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005983win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005985 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
5987 /* Don't want a negative height. Happens when splitting a tiny window.
5988 * Will equalize heights soon to fix it. */
5989 if (height < 0)
5990 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005991 if (wp->w_height == height)
5992 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005994 if (wp->w_height > 0)
5995 {
5996 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005997 /* w_wrow needs to be valid. When setting 'laststatus' this may
5998 * call win_new_height() recursively. */
5999 validate_cursor();
6000 if (wp->w_height != prev_height)
6001 return; /* Recursive call already changed the size, bail out here
6002 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02006003 if (wp->w_wrow != wp->w_prev_fraction_row)
6004 set_fraction(wp);
6005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006
6007 wp->w_height = height;
6008 wp->w_skipcol = 0;
6009
Bram Moolenaar955f1982017-02-05 15:10:51 +01006010 /* There is no point in adjusting the scroll position when exiting. Some
6011 * values might be invalid. */
6012 if (!exiting)
6013 scroll_to_fraction(wp, prev_height);
Bram Moolenaar46328f92016-08-28 15:39:57 +02006014}
6015
6016 void
6017scroll_to_fraction(win_T *wp, int prev_height)
6018{
6019 linenr_T lnum;
6020 int sline, line_size;
6021 int height = wp->w_height;
6022
Bram Moolenaara9b25352019-05-12 14:25:30 +02006023 // Don't change w_topline in any of these cases:
6024 // - window height is 0
6025 // - 'scrollbind' is set and this isn't the current window
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +02006026 // - window height is sufficient to display the whole buffer and first line
6027 // is visible.
Bram Moolenaara9b25352019-05-12 14:25:30 +02006028 if (height > 0
6029 && (!wp->w_p_scb || wp == curwin)
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +02006030 && (height < wp->w_buffer->b_ml.ml_line_count || wp->w_topline > 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 {
Bram Moolenaar34114692005-01-02 11:28:13 +00006032 /*
6033 * Find a value for w_topline that shows the cursor at the same
6034 * relative position in the window as before (more or less).
6035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 lnum = wp->w_cursor.lnum;
6037 if (lnum < 1) /* can happen when starting up */
6038 lnum = 1;
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006039 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L)
6040 / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
6042 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00006043
6044 if (sline >= 0)
6045 {
6046 /* Make sure the whole cursor line is visible, if possible. */
6047 int rows = plines_win(wp, lnum, FALSE);
6048
6049 if (sline > wp->w_height - rows)
6050 {
6051 sline = wp->w_height - rows;
6052 wp->w_wrow -= rows - line_size;
6053 }
6054 }
6055
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 if (sline < 0)
6057 {
6058 /*
6059 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00006060 * Make cursor line the first line in the window. If not enough
6061 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 */
6063 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00006064 if (wp->w_wrow >= wp->w_height
Bram Moolenaar02631462017-09-22 15:20:32 +02006065 && (wp->w_width - win_col_off(wp)) > 0)
Bram Moolenaar26470632006-10-24 19:12:40 +00006066 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006067 wp->w_skipcol += wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00006068 --wp->w_wrow;
6069 while (wp->w_wrow >= wp->w_height)
6070 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006071 wp->w_skipcol += wp->w_width - win_col_off(wp)
Bram Moolenaar26470632006-10-24 19:12:40 +00006072 + win_col_off2(wp);
6073 --wp->w_wrow;
6074 }
6075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02006077 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 {
Bram Moolenaar26470632006-10-24 19:12:40 +00006079 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 {
6081#ifdef FEAT_FOLDING
6082 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
6083 if (lnum == 1)
6084 {
6085 /* first line in buffer is folded */
6086 line_size = 1;
6087 --sline;
6088 break;
6089 }
6090#endif
6091 --lnum;
6092#ifdef FEAT_DIFF
6093 if (lnum == wp->w_topline)
6094 line_size = plines_win_nofill(wp, lnum, TRUE)
6095 + wp->w_topfill;
6096 else
6097#endif
6098 line_size = plines_win(wp, lnum, TRUE);
6099 sline -= line_size;
6100 }
Bram Moolenaar34114692005-01-02 11:28:13 +00006101
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 if (sline < 0)
6103 {
6104 /*
6105 * Line we want at top would go off top of screen. Use next
6106 * line instead.
6107 */
6108#ifdef FEAT_FOLDING
6109 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
6110#endif
6111 lnum++;
6112 wp->w_wrow -= line_size + sline;
6113 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02006114 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 {
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006116 // First line of file reached, use that as topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 lnum = 1;
6118 wp->w_wrow -= sline;
6119 }
6120 }
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006121 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 }
6123
6124 if (wp == curwin)
6125 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006126 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 update_topline();
6128 curs_columns(FALSE); /* validate w_wrow */
6129 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02006130 if (prev_height > 0)
6131 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132
6133 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006134 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 invalidate_botline_win(wp);
6137}
6138
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139/*
6140 * Set the width of a window.
6141 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02006142 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006143win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144{
6145 wp->w_width = width;
6146 wp->w_lines_valid = 0;
6147 changed_line_abv_curs_win(wp);
6148 invalidate_botline_win(wp);
6149 if (wp == curwin)
6150 {
6151 update_topline();
6152 curs_columns(TRUE); /* validate w_wrow */
6153 }
6154 redraw_win_later(wp, NOT_VALID);
6155 wp->w_redr_status = TRUE;
6156}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157
6158 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006159win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160{
6161 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
6162 if (wp->w_p_scr == 0)
6163 wp->w_p_scr = 1;
6164}
6165
6166/*
6167 * command_height: called whenever p_ch has been changed
6168 */
6169 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006170command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 int h;
6173 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006174 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006176 /* Use the value of p_ch that we remembered. This is needed for when the
6177 * GUI starts up, we can't be sure in what order things happen. And when
6178 * p_ch was changed in another tab page. */
6179 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006180
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 /* Find bottom frame with width of screen. */
6182 frp = lastwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 while (frp->fr_width != Columns && frp->fr_parent != NULL)
6184 frp = frp->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
6186 /* Avoid changing the height of a window with 'winfixheight' set. */
6187 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
6188 && frp->fr_win->w_p_wfh)
6189 frp = frp->fr_prev;
6190
6191 if (starting != NO_SCREEN)
6192 {
6193 cmdline_row = Rows - p_ch;
6194
6195 if (p_ch > old_p_ch) /* p_ch got bigger */
6196 {
6197 while (p_ch > old_p_ch)
6198 {
6199 if (frp == NULL)
6200 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006201 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00006203 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 cmdline_row = Rows - p_ch;
6205 break;
6206 }
6207 h = frp->fr_height - frame_minheight(frp, NULL);
6208 if (h > p_ch - old_p_ch)
6209 h = p_ch - old_p_ch;
6210 old_p_ch += h;
6211 frame_add_height(frp, -h);
6212 frp = frp->fr_prev;
6213 }
6214
6215 /* Recompute window positions. */
6216 (void)win_comp_pos();
6217
6218 /* clear the lines added to cmdline */
6219 if (full_screen)
6220 screen_fill((int)(cmdline_row), (int)Rows, 0,
6221 (int)Columns, ' ', ' ', 0);
6222 msg_row = cmdline_row;
6223 redraw_cmdline = TRUE;
6224 return;
6225 }
6226
6227 if (msg_row < cmdline_row)
6228 msg_row = cmdline_row;
6229 redraw_cmdline = TRUE;
6230 }
6231 frame_add_height(frp, (int)(old_p_ch - p_ch));
6232
6233 /* Recompute window positions. */
6234 if (frp != lastwin->w_frame)
6235 (void)win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236}
6237
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238/*
6239 * Resize frame "frp" to be "n" lines higher (negative for less high).
6240 * Also resize the frames it is contained in.
6241 */
6242 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006243frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244{
6245 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
6246 for (;;)
6247 {
6248 frp = frp->fr_parent;
6249 if (frp == NULL)
6250 break;
6251 frp->fr_height += n;
6252 }
6253}
6254
6255/*
6256 * Add or remove a status line for the bottom window(s), according to the
6257 * value of 'laststatus'.
6258 */
6259 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006260last_status(
6261 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 /* Don't make a difference between horizontal or vertical split. */
6264 last_status_rec(topframe, (p_ls == 2
Bram Moolenaar459ca562016-11-10 18:16:33 +01006265 || (p_ls == 1 && (morewin || !ONE_WINDOW))));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266}
6267
6268 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006269last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270{
6271 frame_T *fp;
6272 win_T *wp;
6273
6274 if (fr->fr_layout == FR_LEAF)
6275 {
6276 wp = fr->fr_win;
6277 if (wp->w_status_height != 0 && !statusline)
6278 {
6279 /* remove status line */
6280 win_new_height(wp, wp->w_height + 1);
6281 wp->w_status_height = 0;
6282 comp_col();
6283 }
6284 else if (wp->w_status_height == 0 && statusline)
6285 {
6286 /* Find a frame to take a line from. */
6287 fp = fr;
6288 while (fp->fr_height <= frame_minheight(fp, NULL))
6289 {
6290 if (fp == topframe)
6291 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006292 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 return;
6294 }
6295 /* In a column of frames: go to frame above. If already at
6296 * the top or in a row of frames: go to parent. */
6297 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6298 fp = fp->fr_prev;
6299 else
6300 fp = fp->fr_parent;
6301 }
6302 wp->w_status_height = 1;
6303 if (fp != fr)
6304 {
6305 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6306 frame_fix_height(wp);
6307 (void)win_comp_pos();
6308 }
6309 else
6310 win_new_height(wp, wp->w_height - 1);
6311 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006312 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 }
6314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 else if (fr->fr_layout == FR_ROW)
6316 {
6317 /* vertically split windows, set status line for each one */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006318 FOR_ALL_FRAMES(fp, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 last_status_rec(fp, statusline);
6320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 else
6322 {
6323 /* horizontally split window, set status line for last one */
6324 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6325 ;
6326 last_status_rec(fp, statusline);
6327 }
6328}
6329
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006330/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006331 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006332 */
6333 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006334tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006335{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006336#ifdef FEAT_GUI_TABLINE
6337 /* When the GUI has the tabline then this always returns zero. */
6338 if (gui_use_tabline())
6339 return 0;
6340#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006341 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006342 {
6343 case 0: return 0;
6344 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6345 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006346 return 1;
6347}
6348
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349/*
6350 * Return the minimal number of rows that is needed on the screen to display
6351 * the current number of windows.
6352 */
6353 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006354min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355{
6356 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006357 tabpage_T *tp;
6358 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359
6360 if (firstwin == NULL) /* not initialized yet */
6361 return MIN_LINES;
6362
Bram Moolenaarf740b292006-02-16 22:11:02 +00006363 total = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02006364 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006365 {
6366 n = frame_minheight(tp->tp_topframe, NULL);
6367 if (total < n)
6368 total = n;
6369 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006370 total += tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006371 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 return total;
6373}
6374
6375/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006376 * Return TRUE if there is only one window (in the current tab page), not
6377 * counting a help or preview window, unless it is the current window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006378 * Does not count unlisted windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 */
6380 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006381only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 int count = 0;
6384 win_T *wp;
6385
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006386 /* If there is another tab page there always is another window. */
6387 if (first_tabpage->tp_next != NULL)
6388 return FALSE;
6389
Bram Moolenaar29323592016-07-24 22:04:11 +02006390 FOR_ALL_WINDOWS(wp)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006391 if (wp->w_buffer != NULL
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006392 && (!((bt_help(wp->w_buffer) && !bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393# ifdef FEAT_QUICKFIX
6394 || wp->w_p_pvw
6395# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006396 ) || wp == curwin) && wp != aucmd_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 ++count;
6398 return (count <= 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399}
6400
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401/*
6402 * Correct the cursor line number in other windows. Used after changing the
6403 * current buffer, and before applying autocommands.
6404 * When "do_curwin" is TRUE, also check current window.
6405 */
6406 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006407check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408{
6409 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006410 tabpage_T *tp;
6411
6412 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 {
Bram Moolenaara68e5952019-04-25 22:22:01 +02006415 // save the original cursor position and topline
6416 wp->w_save_cursor.w_cursor_save = wp->w_cursor;
6417 wp->w_save_cursor.w_topline_save = wp->w_topline;
6418
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6420 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6421 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6422 wp->w_topline = curbuf->b_ml.ml_line_count;
Bram Moolenaara68e5952019-04-25 22:22:01 +02006423
6424 // save the corrected cursor position and topline
6425 wp->w_save_cursor.w_cursor_corr = wp->w_cursor;
6426 wp->w_save_cursor.w_topline_corr = wp->w_topline;
6427 }
6428}
6429
6430/*
6431 * Reset cursor and topline to its stored values from check_lnums().
6432 * check_lnums() must have been called first!
6433 */
6434 void
6435reset_lnums()
6436{
6437 win_T *wp;
6438 tabpage_T *tp;
6439
6440 FOR_ALL_TAB_WINDOWS(tp, wp)
6441 if (wp->w_buffer == curbuf)
6442 {
6443 // Restore the value if the autocommand didn't change it.
6444 if (EQUAL_POS(wp->w_save_cursor.w_cursor_corr, wp->w_cursor))
6445 wp->w_cursor = wp->w_save_cursor.w_cursor_save;
6446 if (wp->w_save_cursor.w_topline_corr == wp->w_topline)
6447 wp->w_topline = wp->w_save_cursor.w_topline_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 }
6449}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450
6451/*
6452 * A snapshot of the window sizes, to restore them after closing the help
6453 * window.
6454 * Only these fields are used:
6455 * fr_layout
6456 * fr_width
6457 * fr_height
6458 * fr_next
6459 * fr_child
6460 * fr_win (only valid for the old curwin, NULL otherwise)
6461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462
6463/*
6464 * Create a snapshot of the current frame sizes.
6465 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006466 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006467make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006469 clear_snapshot(curtab, idx);
6470 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471}
6472
6473 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006474make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006476 *frp = ALLOC_CLEAR_ONE(frame_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 if (*frp == NULL)
6478 return;
6479 (*frp)->fr_layout = fr->fr_layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 (*frp)->fr_width = fr->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 (*frp)->fr_height = fr->fr_height;
6482 if (fr->fr_next != NULL)
6483 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6484 if (fr->fr_child != NULL)
6485 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6486 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6487 (*frp)->fr_win = curwin;
6488}
6489
6490/*
6491 * Remove any existing snapshot.
6492 */
6493 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006494clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006496 clear_snapshot_rec(tp->tp_snapshot[idx]);
6497 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498}
6499
6500 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006501clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
6503 if (fr != NULL)
6504 {
6505 clear_snapshot_rec(fr->fr_next);
6506 clear_snapshot_rec(fr->fr_child);
6507 vim_free(fr);
6508 }
6509}
6510
6511/*
6512 * Restore a previously created snapshot, if there is any.
6513 * This is only done if the screen size didn't change and the window layout is
6514 * still the same.
6515 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006516 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006517restore_snapshot(
6518 int idx,
6519 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520{
6521 win_T *wp;
6522
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006523 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006524 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006525 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6526 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006528 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 win_comp_pos();
6530 if (wp != NULL && close_curwin)
6531 win_goto(wp);
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01006532 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006534 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535}
6536
6537/*
6538 * Check if frames "sn" and "fr" have the same layout, same following frames
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006539 * and same children. And the window pointer is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 */
6541 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006542check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543{
6544 if (sn->fr_layout != fr->fr_layout
6545 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6546 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6547 || (sn->fr_next != NULL
6548 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6549 || (sn->fr_child != NULL
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006550 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01006551 || (sn->fr_win != NULL && !win_valid(sn->fr_win)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 return FAIL;
6553 return OK;
6554}
6555
6556/*
6557 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6558 * following frames and children.
6559 * Returns a pointer to the old current window, or NULL.
6560 */
6561 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006562restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563{
6564 win_T *wp = NULL;
6565 win_T *wp2;
6566
6567 fr->fr_height = sn->fr_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 fr->fr_width = sn->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 if (fr->fr_layout == FR_LEAF)
6570 {
6571 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006572 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 wp = sn->fr_win;
6574 }
6575 if (sn->fr_next != NULL)
6576 {
6577 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6578 if (wp2 != NULL)
6579 wp = wp2;
6580 }
6581 if (sn->fr_child != NULL)
6582 {
6583 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6584 if (wp2 != NULL)
6585 wp = wp2;
6586 }
6587 return wp;
6588}
6589
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006590#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6591 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006592/*
6593 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006594 * restore_win() MUST be called to undo, also when FAIL is returned.
6595 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006596 * When "no_display" is TRUE the display won't be affected, no redraw is
6597 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006598 * Returns FAIL if switching to "win" failed.
6599 */
6600 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006601switch_win(
Bram Moolenaar816968d2017-09-29 21:29:18 +02006602 win_T **save_curwin,
6603 tabpage_T **save_curtab,
6604 win_T *win,
6605 tabpage_T *tp,
6606 int no_display)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006607{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006608 block_autocmds();
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006609 return switch_win_noblock(save_curwin, save_curtab, win, tp, no_display);
6610}
6611
6612/*
6613 * As switch_win() but without blocking autocommands.
6614 */
6615 int
6616switch_win_noblock(
6617 win_T **save_curwin,
6618 tabpage_T **save_curtab,
6619 win_T *win,
6620 tabpage_T *tp,
6621 int no_display)
6622{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006623 *save_curwin = curwin;
6624 if (tp != NULL)
6625 {
6626 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006627 if (no_display)
6628 {
6629 curtab->tp_firstwin = firstwin;
6630 curtab->tp_lastwin = lastwin;
6631 curtab = tp;
6632 firstwin = curtab->tp_firstwin;
6633 lastwin = curtab->tp_lastwin;
6634 }
6635 else
6636 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006637 }
6638 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006639 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006640 curwin = win;
6641 curbuf = curwin->w_buffer;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006642 return OK;
6643}
6644
6645/*
6646 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006647 * When "no_display" is TRUE the display won't be affected, no redraw is
6648 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006649 */
6650 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006651restore_win(
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006652 win_T *save_curwin,
6653 tabpage_T *save_curtab,
6654 int no_display)
6655{
6656 restore_win_noblock(save_curwin, save_curtab, no_display);
6657 unblock_autocmds();
6658}
6659
6660/*
6661 * As restore_win() but without unblocking autocommands.
6662 */
6663 void
6664restore_win_noblock(
6665 win_T *save_curwin,
6666 tabpage_T *save_curtab,
6667 int no_display)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006668{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006669 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006670 {
6671 if (no_display)
6672 {
6673 curtab->tp_firstwin = firstwin;
6674 curtab->tp_lastwin = lastwin;
6675 curtab = save_curtab;
6676 firstwin = curtab->tp_firstwin;
6677 lastwin = curtab->tp_lastwin;
6678 }
6679 else
6680 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6681 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006682 if (win_valid(save_curwin))
6683 {
6684 curwin = save_curwin;
6685 curbuf = curwin->w_buffer;
6686 }
Bram Moolenaareea16992019-05-31 17:34:48 +02006687#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02006688 else if (WIN_IS_POPUP(curwin))
Bram Moolenaareea16992019-05-31 17:34:48 +02006689 // original window was closed and now we're in a popup window: Go
6690 // to the first valid window.
6691 win_goto(firstwin);
6692#endif
Bram Moolenaar105bc352013-05-17 16:03:57 +02006693}
6694
6695/*
6696 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6697 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6698 */
6699 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006700switch_buffer(bufref_T *save_curbuf, buf_T *buf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006701{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006702 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006703 set_bufref(save_curbuf, curbuf);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006704 --curbuf->b_nwindows;
6705 curbuf = buf;
6706 curwin->w_buffer = buf;
6707 ++curbuf->b_nwindows;
6708}
6709
6710/*
6711 * Restore the current buffer after using switch_buffer().
6712 */
6713 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006714restore_buffer(bufref_T *save_curbuf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006715{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006716 unblock_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006717 /* Check for valid buffer, just in case. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006718 if (bufref_valid(save_curbuf))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006719 {
6720 --curbuf->b_nwindows;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006721 curwin->w_buffer = save_curbuf->br_buf;
6722 curbuf = save_curbuf->br_buf;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006723 ++curbuf->b_nwindows;
6724 }
6725}
6726#endif
6727
Bram Moolenaar4033c552017-09-16 20:54:51 +02006728#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729/*
6730 * Return TRUE if there is any vertically split window.
6731 */
6732 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006733win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
6735 frame_T *fr;
6736
6737 if (topframe->fr_layout == FR_ROW)
6738 return TRUE;
6739
6740 if (topframe->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006741 FOR_ALL_FRAMES(fr, topframe->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 if (fr->fr_layout == FR_ROW)
6743 return TRUE;
6744
6745 return FALSE;
6746}
6747#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006748
Bram Moolenaar6d216452013-05-12 19:00:41 +02006749#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6750 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006751get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02006752{
6753 int i = 1;
6754 win_T *w;
6755
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006756 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02006757 ++i;
6758
6759 if (w == NULL)
6760 return 0;
6761 else
6762 return i;
6763}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006764
6765 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006766get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006767{
6768 int i = 1;
6769 tabpage_T *t;
6770
6771 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
6772 ++i;
6773
6774 if (t == NULL)
6775 return 0;
6776 else
6777 return i;
6778}
Bram Moolenaar6d216452013-05-12 19:00:41 +02006779#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006780
6781/*
6782 * Return TRUE if "topfrp" and its children are at the right height.
6783 */
6784 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006785frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006786{
6787 frame_T *frp;
6788
6789 if (topfrp->fr_height != height)
6790 return FALSE;
6791
6792 if (topfrp->fr_layout == FR_ROW)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006793 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006794 if (frp->fr_height != height)
6795 return FALSE;
6796
6797 return TRUE;
6798}
6799
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006800/*
6801 * Return TRUE if "topfrp" and its children are at the right width.
6802 */
6803 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006804frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006805{
6806 frame_T *frp;
6807
6808 if (topfrp->fr_width != width)
6809 return FALSE;
6810
6811 if (topfrp->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006812 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006813 if (frp->fr_width != width)
6814 return FALSE;
6815
6816 return TRUE;
6817}
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006818
Bram Moolenaar86edef62016-03-13 18:07:30 +01006819#if defined(FEAT_EVAL) || defined(PROTO)
6820 int
6821win_getid(typval_T *argvars)
6822{
6823 int winnr;
6824 win_T *wp;
6825
6826 if (argvars[0].v_type == VAR_UNKNOWN)
6827 return curwin->w_id;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006828 winnr = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006829 if (winnr > 0)
6830 {
6831 if (argvars[1].v_type == VAR_UNKNOWN)
6832 wp = firstwin;
6833 else
6834 {
6835 tabpage_T *tp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006836 int tabnr = tv_get_number(&argvars[1]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006837
Bram Moolenaar29323592016-07-24 22:04:11 +02006838 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006839 if (--tabnr == 0)
6840 break;
6841 if (tp == NULL)
6842 return -1;
Bram Moolenaar8e639052016-11-13 14:31:40 +01006843 if (tp == curtab)
6844 wp = firstwin;
6845 else
6846 wp = tp->tp_firstwin;
Bram Moolenaar86edef62016-03-13 18:07:30 +01006847 }
6848 for ( ; wp != NULL; wp = wp->w_next)
6849 if (--winnr == 0)
6850 return wp->w_id;
6851 }
6852 return 0;
6853}
6854
6855 int
6856win_gotoid(typval_T *argvars)
6857{
6858 win_T *wp;
6859 tabpage_T *tp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006860 int id = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006861
Bram Moolenaar29323592016-07-24 22:04:11 +02006862 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006863 if (wp->w_id == id)
6864 {
6865 goto_tabpage_win(tp, wp);
6866 return 1;
6867 }
6868 return 0;
6869}
6870
6871 void
6872win_id2tabwin(typval_T *argvars, list_T *list)
6873{
6874 win_T *wp;
6875 tabpage_T *tp;
6876 int winnr = 1;
6877 int tabnr = 1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006878 int id = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006879
Bram Moolenaar29323592016-07-24 22:04:11 +02006880 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006881 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006882 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006883 {
6884 if (wp->w_id == id)
6885 {
6886 list_append_number(list, tabnr);
6887 list_append_number(list, winnr);
6888 return;
6889 }
6890 ++winnr;
6891 }
6892 ++tabnr;
6893 winnr = 1;
6894 }
6895 list_append_number(list, 0);
6896 list_append_number(list, 0);
6897}
6898
Bram Moolenaar820680b2019-08-09 14:56:22 +02006899/*
6900 * Return the window pointer of window "id".
6901 */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006902 win_T *
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006903win_id2wp(int id)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006904{
Bram Moolenaar820680b2019-08-09 14:56:22 +02006905 return win_id2wp_tp(id, NULL);
6906}
6907
6908/*
6909 * Return the window and tab pointer of window "id".
6910 */
6911 win_T *
6912win_id2wp_tp(int id, tabpage_T **tpp)
6913{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006914 win_T *wp;
6915 tabpage_T *tp;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006916
6917 FOR_ALL_TAB_WINDOWS(tp, wp)
6918 if (wp->w_id == id)
Bram Moolenaar820680b2019-08-09 14:56:22 +02006919 {
6920 if (tpp != NULL)
6921 *tpp = tp;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006922 return wp;
Bram Moolenaar820680b2019-08-09 14:56:22 +02006923 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006924#ifdef FEAT_TEXT_PROP
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006925 // popup windows are in separate lists
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006926 FOR_ALL_TABPAGES(tp)
6927 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
6928 if (wp->w_id == id)
Bram Moolenaar820680b2019-08-09 14:56:22 +02006929 {
6930 if (tpp != NULL)
6931 *tpp = tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006932 return wp;
Bram Moolenaar820680b2019-08-09 14:56:22 +02006933 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006934 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
6935 if (wp->w_id == id)
Bram Moolenaar820680b2019-08-09 14:56:22 +02006936 {
6937 if (tpp != NULL)
6938 *tpp = tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006939 return wp;
Bram Moolenaar820680b2019-08-09 14:56:22 +02006940 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006941#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006942
6943 return NULL;
6944}
6945
Bram Moolenaar86edef62016-03-13 18:07:30 +01006946 int
6947win_id2win(typval_T *argvars)
6948{
6949 win_T *wp;
6950 int nr = 1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006951 int id = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006952
Bram Moolenaar29323592016-07-24 22:04:11 +02006953 FOR_ALL_WINDOWS(wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006954 {
6955 if (wp->w_id == id)
6956 return nr;
6957 ++nr;
6958 }
6959 return 0;
6960}
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01006961
6962 void
6963win_findbuf(typval_T *argvars, list_T *list)
6964{
6965 win_T *wp;
6966 tabpage_T *tp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006967 int bufnr = tv_get_number(&argvars[0]);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01006968
Bram Moolenaar29323592016-07-24 22:04:11 +02006969 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01006970 if (wp->w_buffer->b_fnum == bufnr)
6971 list_append_number(list, wp->w_id);
6972}
6973
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02006974/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02006975 * Find window specified by "vp" in tabpage "tp".
6976 */
6977 win_T *
6978find_win_by_nr(
6979 typval_T *vp,
6980 tabpage_T *tp) // NULL for current tab page
6981{
6982 win_T *wp;
6983 int nr = (int)tv_get_number_chk(vp, NULL);
6984
6985 if (nr < 0)
6986 return NULL;
6987 if (nr == 0)
6988 return curwin;
6989
6990 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
6991 {
6992 if (nr >= LOWEST_WIN_ID)
6993 {
6994 if (wp->w_id == nr)
6995 return wp;
6996 }
6997 else if (--nr <= 0)
6998 break;
6999 }
7000 if (nr >= LOWEST_WIN_ID)
7001 {
7002#ifdef FEAT_TEXT_PROP
7003 // check tab-local popup windows
7004 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
7005 if (wp->w_id == nr)
7006 return wp;
7007 // check global popup windows
7008 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
7009 if (wp->w_id == nr)
7010 return wp;
7011#endif
7012 return NULL;
7013 }
7014 return wp;
7015}
7016
7017/*
7018 * Find a window: When using a Window ID in any tab page, when using a number
7019 * in the current tab page.
7020 */
7021 win_T *
7022find_win_by_nr_or_id(typval_T *vp)
7023{
7024 int nr = (int)tv_get_number_chk(vp, NULL);
7025
7026 if (nr >= LOWEST_WIN_ID)
7027 return win_id2wp(tv_get_number(vp));
7028 return find_win_by_nr(vp, NULL);
7029}
7030
7031/*
7032 * Find window specified by "wvp" in tabpage "tvp".
7033 * Returns the tab page in 'ptp'
7034 */
7035 win_T *
7036find_tabwin(
7037 typval_T *wvp, // VAR_UNKNOWN for current window
7038 typval_T *tvp, // VAR_UNKNOWN for current tab page
7039 tabpage_T **ptp)
7040{
7041 win_T *wp = NULL;
7042 tabpage_T *tp = NULL;
7043 long n;
7044
7045 if (wvp->v_type != VAR_UNKNOWN)
7046 {
7047 if (tvp->v_type != VAR_UNKNOWN)
7048 {
7049 n = (long)tv_get_number(tvp);
7050 if (n >= 0)
7051 tp = find_tabpage(n);
7052 }
7053 else
7054 tp = curtab;
7055
7056 if (tp != NULL)
7057 {
7058 wp = find_win_by_nr(wvp, tp);
7059 if (wp == NULL && wvp->v_type == VAR_NUMBER
7060 && wvp->vval.v_number != -1)
7061 // A window with the specified number is not found
7062 tp = NULL;
7063 }
7064 }
7065 else
7066 {
7067 wp = curwin;
7068 tp = curtab;
7069 }
7070
7071 if (ptp != NULL)
7072 *ptp = tp;
7073
7074 return wp;
7075}
7076
7077/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02007078 * Get the layout of the given tab page for winlayout().
7079 */
7080 void
7081get_framelayout(frame_T *fr, list_T *l, int outer)
7082{
7083 frame_T *child;
7084 list_T *fr_list;
7085 list_T *win_list;
7086
7087 if (fr == NULL)
7088 return;
7089
7090 if (outer)
7091 // outermost call from f_winlayout()
7092 fr_list = l;
7093 else
7094 {
7095 fr_list = list_alloc();
7096 if (fr_list == NULL)
7097 return;
7098 list_append_list(l, fr_list);
7099 }
7100
7101 if (fr->fr_layout == FR_LEAF)
7102 {
7103 if (fr->fr_win != NULL)
7104 {
7105 list_append_string(fr_list, (char_u *)"leaf", -1);
7106 list_append_number(fr_list, fr->fr_win->w_id);
7107 }
7108 }
7109 else
7110 {
7111 list_append_string(fr_list,
7112 fr->fr_layout == FR_ROW ? (char_u *)"row" : (char_u *)"col", -1);
7113
7114 win_list = list_alloc();
7115 if (win_list == NULL)
7116 return;
7117 list_append_list(fr_list, win_list);
7118 child = fr->fr_child;
7119 while (child != NULL)
7120 {
7121 get_framelayout(child, win_list, FALSE);
7122 child = child->fr_next;
7123 }
7124 }
7125}
Bram Moolenaar86edef62016-03-13 18:07:30 +01007126#endif