blob: b4ab11be31cfe3997381c881dc576809f5731b22 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#include "vim.h"
11
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010012static void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize, long Prenum);
13static void win_init(win_T *newp, win_T *oldp, int flags);
14static void win_init_some(win_T *newp, win_T *oldp);
15static void frame_comp_pos(frame_T *topfrp, int *row, int *col);
16static void frame_setheight(frame_T *curfrp, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010017static void frame_setwidth(frame_T *curfrp, int width);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010018static void win_exchange(long);
19static void win_rotate(int, int);
20static void win_totop(int size, int flags);
21static void win_equal_rec(win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010022static win_T *win_free_mem(win_T *win, int *dirp, tabpage_T *tp);
23static frame_T *win_altframe(win_T *win, tabpage_T *tp);
24static tabpage_T *alt_tabpage(void);
25static win_T *frame2win(frame_T *frp);
26static int frame_has_win(frame_T *frp, win_T *wp);
27static void frame_new_height(frame_T *topfrp, int height, int topfirst, int wfh);
28static int frame_fixed_height(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010029static int frame_fixed_width(frame_T *frp);
30static void frame_add_statusline(frame_T *frp);
31static void frame_new_width(frame_T *topfrp, int width, int leftfirst, int wfw);
32static void frame_add_vsep(frame_T *frp);
33static int frame_minwidth(frame_T *topfrp, win_T *next_curwin);
34static void frame_fix_width(win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010035static int win_alloc_firstwin(win_T *oldwin);
36static void new_frame(win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010037static tabpage_T *alloc_tabpage(void);
38static int leave_tabpage(buf_T *new_curbuf, int trigger_leave_autocmds);
39static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds);
40static void frame_fix_height(win_T *wp);
41static int frame_minheight(frame_T *topfrp, win_T *next_curwin);
Bram Moolenaarc917da42016-07-19 22:31:36 +020042static void win_enter_ext(win_T *wp, int undo_sync, int no_curwin, int trigger_new_autocmds, int trigger_enter_autocmds, int trigger_leave_autocmds);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010043static void win_free(win_T *wp, tabpage_T *tp);
44static void frame_append(frame_T *after, frame_T *frp);
45static void frame_insert(frame_T *before, frame_T *frp);
46static void frame_remove(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010047static void win_goto_ver(int up, long count);
48static void win_goto_hor(int left, long count);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010049static void frame_add_height(frame_T *frp, int n);
50static void last_status_rec(frame_T *fr, int statusline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void make_snapshot_rec(frame_T *fr, frame_T **frp);
53static void clear_snapshot(tabpage_T *tp, int idx);
54static void clear_snapshot_rec(frame_T *fr);
55static int check_snapshot_rec(frame_T *sn, frame_T *fr);
56static win_T *restore_snapshot_rec(frame_T *sn, frame_T *fr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000057
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static int frame_check_height(frame_T *topfrp, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010059static int frame_check_width(frame_T *topfrp, int width);
Bram Moolenaarb893ac22013-06-26 14:04:47 +020060
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010061static win_T *win_alloc(win_T *after, int hidden);
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000063#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000064
Bram Moolenaar4033c552017-09-16 20:54:51 +020065#define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000066
67static char *m_onlyone = N_("Already only one window");
68
Bram Moolenaar071d4272004-06-13 20:20:40 +000069/*
Bram Moolenaar72e83c12019-02-22 16:09:52 +010070 * All CTRL-W window commands are handled here, called from normal_cmd().
Bram Moolenaar071d4272004-06-13 20:20:40 +000071 */
72 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +010073do_window(
74 int nchar,
75 long Prenum,
76 int xchar) /* extra char from ":wincmd gx" or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000077{
78 long Prenum1;
79 win_T *wp;
80#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
81 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000082 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000083#endif
84#ifdef FEAT_FIND_ID
85 int type = FIND_DEFINE;
86 int len;
87#endif
88 char_u cbuf[40];
89
Bram Moolenaar815b76b2019-06-01 14:15:52 +020090 if (NOT_IN_POPUP_WINDOW)
91 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
93#ifdef FEAT_CMDWIN
Bram Moolenaar6f470022018-04-10 18:47:20 +020094# define CHECK_CMDWIN \
95 do { \
96 if (cmdwin_type != 0) \
97 { \
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010098 emsg(_(e_cmdwin)); \
Bram Moolenaar6f470022018-04-10 18:47:20 +020099 return; \
100 } \
101 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#else
Bram Moolenaar6f470022018-04-10 18:47:20 +0200103# define CHECK_CMDWIN do { /**/ } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200106 Prenum1 = Prenum == 0 ? 1 : Prenum;
107
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108 switch (nchar)
109 {
110/* split current window in two parts, horizontally */
111 case 'S':
112 case Ctrl_S:
113 case 's':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200114 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000116#ifdef FEAT_QUICKFIX
117 /* When splitting the quickfix window open a new buffer in it,
118 * don't replicate the quickfix buffer. */
119 if (bt_quickfix(curbuf))
120 goto newwindow;
121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#ifdef FEAT_GUI
123 need_mouse_correct = TRUE;
124#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200125 (void)win_split((int)Prenum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126 break;
127
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128/* split current window in two parts, vertically */
129 case Ctrl_V:
130 case 'v':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200131 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100133#ifdef FEAT_QUICKFIX
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000134 /* When splitting the quickfix window open a new buffer in it,
135 * don't replicate the quickfix buffer. */
136 if (bt_quickfix(curbuf))
137 goto newwindow;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100138#endif
139#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100141#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200142 (void)win_split((int)Prenum, WSP_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145/* split current window and edit alternate file */
146 case Ctrl_HAT:
147 case '^':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200148 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100150
151 if (buflist_findnr(Prenum == 0
152 ? curwin->w_alt_fnum : Prenum) == NULL)
153 {
154 if (Prenum == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100155 emsg(_(e_noalt));
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100156 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100157 semsg(_("E92: Buffer %ld not found"), Prenum);
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100158 break;
159 }
160
161 if (!curbuf_locked() && win_split(0, 0) == OK)
162 (void)buflist_getfile(
163 Prenum == 0 ? curwin->w_alt_fnum : Prenum,
164 (linenr_T)0, GETF_ALT, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 break;
166
167/* open new window */
168 case Ctrl_N:
169 case 'n':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200170 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000172#ifdef FEAT_QUICKFIX
173newwindow:
174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000176 /* window height */
177 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 else
179 cbuf[0] = NUL;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100180#if defined(FEAT_QUICKFIX)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000181 if (nchar == 'v' || nchar == Ctrl_V)
182 STRCAT(cbuf, "v");
183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 STRCAT(cbuf, "new");
185 do_cmdline_cmd(cbuf);
186 break;
187
188/* quit current window */
189 case Ctrl_Q:
190 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100192 cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100193 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 break;
195
196/* close current window */
197 case Ctrl_C:
198 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100200 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100201 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 break;
203
Bram Moolenaar4033c552017-09-16 20:54:51 +0200204#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205/* close preview window */
206 case Ctrl_Z:
207 case 'z':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200208 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 do_cmdline_cmd((char_u *)"pclose");
211 break;
212
213/* cursor to preview window */
214 case 'P':
Bram Moolenaar29323592016-07-24 22:04:11 +0200215 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 if (wp->w_p_pvw)
217 break;
218 if (wp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100219 emsg(_("E441: There is no preview window"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 else
221 win_goto(wp);
222 break;
223#endif
224
225/* close all but current window */
226 case Ctrl_O:
227 case 'o':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200228 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100230 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100231 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 break;
233
234/* cursor to next window with wrap around */
235 case Ctrl_W:
236 case 'w':
237/* cursor to previous window with wrap around */
238 case 'W':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200239 CHECK_CMDWIN;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +0100240 if (ONE_WINDOW && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 beep_flush();
242 else
243 {
244 if (Prenum) /* go to specified window */
245 {
246 for (wp = firstwin; --Prenum > 0; )
247 {
248 if (wp->w_next == NULL)
249 break;
250 else
251 wp = wp->w_next;
252 }
253 }
254 else
255 {
256 if (nchar == 'W') /* go to previous window */
257 {
258 wp = curwin->w_prev;
259 if (wp == NULL)
260 wp = lastwin; /* wrap around */
261 }
262 else /* go to next window */
263 {
264 wp = curwin->w_next;
265 if (wp == NULL)
266 wp = firstwin; /* wrap around */
267 }
268 }
269 win_goto(wp);
270 }
271 break;
272
273/* cursor to window below */
274 case 'j':
275 case K_DOWN:
276 case Ctrl_J:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200277 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 win_goto_ver(FALSE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 break;
280
281/* cursor to window above */
282 case 'k':
283 case K_UP:
284 case Ctrl_K:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200285 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 win_goto_ver(TRUE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 break;
288
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289/* cursor to left window */
290 case 'h':
291 case K_LEFT:
292 case Ctrl_H:
293 case K_BS:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200294 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 win_goto_hor(TRUE, Prenum1);
296 break;
297
298/* cursor to right window */
299 case 'l':
300 case K_RIGHT:
301 case Ctrl_L:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200302 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 win_goto_hor(FALSE, Prenum1);
304 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000306/* move window to new tab page */
307 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000308 if (one_window())
Bram Moolenaar32526b32019-01-19 17:43:09 +0100309 msg(_(m_onlyone));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000310 else
311 {
312 tabpage_T *oldtab = curtab;
313 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000314
315 /* First create a new tab with the window, then go back to
316 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000317 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000318 if (win_new_tabpage((int)Prenum) == OK
319 && valid_tabpage(oldtab))
320 {
321 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200322 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000323 if (curwin == wp)
324 win_close(curwin, FALSE);
325 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200326 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000327 }
328 }
329 break;
330
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331/* cursor to top-left window */
332 case 't':
333 case Ctrl_T:
334 win_goto(firstwin);
335 break;
336
337/* cursor to bottom-right window */
338 case 'b':
339 case Ctrl_B:
340 win_goto(lastwin);
341 break;
342
343/* cursor to last accessed (previous) window */
344 case 'p':
345 case Ctrl_P:
Bram Moolenaar3dda7db2016-04-03 21:22:58 +0200346 if (!win_valid(prevwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 beep_flush();
348 else
349 win_goto(prevwin);
350 break;
351
352/* exchange current and next window */
353 case 'x':
354 case Ctrl_X:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200355 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 win_exchange(Prenum);
357 break;
358
359/* rotate windows downwards */
360 case Ctrl_R:
361 case 'r':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200362 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 win_rotate(FALSE, (int)Prenum1); /* downwards */
365 break;
366
367/* rotate windows upwards */
368 case 'R':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200369 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 win_rotate(TRUE, (int)Prenum1); /* upwards */
372 break;
373
374/* move window to the very top/bottom/left/right */
375 case 'K':
376 case 'J':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 case 'H':
378 case 'L':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200379 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 win_totop((int)Prenum,
381 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
382 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
383 break;
384
385/* make all windows the same height */
386 case '=':
387#ifdef FEAT_GUI
388 need_mouse_correct = TRUE;
389#endif
390 win_equal(NULL, FALSE, 'b');
391 break;
392
393/* increase current window height */
394 case '+':
395#ifdef FEAT_GUI
396 need_mouse_correct = TRUE;
397#endif
398 win_setheight(curwin->w_height + (int)Prenum1);
399 break;
400
401/* decrease current window height */
402 case '-':
403#ifdef FEAT_GUI
404 need_mouse_correct = TRUE;
405#endif
406 win_setheight(curwin->w_height - (int)Prenum1);
407 break;
408
409/* set current window height */
410 case Ctrl__:
411 case '_':
412#ifdef FEAT_GUI
413 need_mouse_correct = TRUE;
414#endif
415 win_setheight(Prenum ? (int)Prenum : 9999);
416 break;
417
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418/* increase current window width */
419 case '>':
420#ifdef FEAT_GUI
421 need_mouse_correct = TRUE;
422#endif
423 win_setwidth(curwin->w_width + (int)Prenum1);
424 break;
425
426/* decrease current window width */
427 case '<':
428#ifdef FEAT_GUI
429 need_mouse_correct = TRUE;
430#endif
431 win_setwidth(curwin->w_width - (int)Prenum1);
432 break;
433
434/* set current window width */
435 case '|':
436#ifdef FEAT_GUI
437 need_mouse_correct = TRUE;
438#endif
439 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
440 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441
442/* jump to tag and split window if tag exists (in preview window) */
443#if defined(FEAT_QUICKFIX)
444 case '}':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200445 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 if (Prenum)
447 g_do_tagpreview = Prenum;
448 else
449 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200451 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 case ']':
453 case Ctrl_RSB:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200454 CHECK_CMDWIN;
Bram Moolenaard355c502014-09-23 13:48:43 +0200455 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 if (Prenum)
457 postponed_split = Prenum;
458 else
459 postponed_split = -1;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200460#ifdef FEAT_QUICKFIX
Bram Moolenaar56095e12014-10-09 10:44:37 +0200461 if (nchar != '}')
462 g_do_tagpreview = 0;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464
Bram Moolenaard355c502014-09-23 13:48:43 +0200465 /* Execute the command right here, required when "wincmd ]"
466 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 do_nv_ident(Ctrl_RSB, NUL);
468 break;
469
470#ifdef FEAT_SEARCHPATH
471/* edit file name under cursor in a new window */
472 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000473 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000475wingotofile:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200476 CHECK_CMDWIN;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000477
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000478 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 if (ptr != NULL)
480 {
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200481 tabpage_T *oldtab = curtab;
482 win_T *oldwin = curwin;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000483# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000485# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 setpcmark();
487 if (win_split(0, 0) == OK)
488 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200489 RESET_BINDING(curwin);
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200490 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
491 ECMD_HIDE, NULL) == FAIL)
492 {
493 /* Failed to open the file, close the window
494 * opened for it. */
495 win_close(curwin, FALSE);
496 goto_tabpage_win(oldtab, oldwin);
497 }
498 else if (nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000499 {
500 curwin->w_cursor.lnum = lnum;
501 check_cursor_lnum();
502 beginline(BL_SOL | BL_FIX);
503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 }
505 vim_free(ptr);
506 }
507 break;
508#endif
509
510#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000511/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 * new window -- webb
513 */
514 case 'i': /* Go to any match */
515 case Ctrl_I:
516 type = FIND_ANY;
517 /* FALLTHROUGH */
518 case 'd': /* Go to definition, using 'define' */
519 case Ctrl_D:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200520 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
522 break;
523 find_pattern_in_path(ptr, 0, len, TRUE,
524 Prenum == 0 ? TRUE : FALSE, type,
525 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
526 curwin->w_set_curswant = TRUE;
527 break;
528#endif
529
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200530/* Quickfix window only: view the result under the cursor in a new split. */
531#if defined(FEAT_QUICKFIX)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000532 case K_KENTER:
533 case CAR:
Bram Moolenaar05159a02005-02-26 23:04:13 +0000534 if (bt_quickfix(curbuf))
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200535 qf_view_result(TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000536 break;
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200537#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000538
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539/* CTRL-W g extended commands */
540 case 'g':
541 case Ctrl_G:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200542 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543#ifdef USE_ON_FLY_SCROLL
544 dont_scroll = TRUE; /* disallow scrolling here */
545#endif
546 ++no_mapping;
547 ++allow_keys; /* no mapping for xchar, but allow key codes */
548 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000549 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 --no_mapping;
552 --allow_keys;
553#ifdef FEAT_CMDL_INFO
554 (void)add_to_showcmd(xchar);
555#endif
556 switch (xchar)
557 {
558#if defined(FEAT_QUICKFIX)
559 case '}':
560 xchar = Ctrl_RSB;
561 if (Prenum)
562 g_do_tagpreview = Prenum;
563 else
564 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200566 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 case ']':
568 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200569 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 if (Prenum)
571 postponed_split = Prenum;
572 else
573 postponed_split = -1;
574
575 /* Execute the command right here, required when
576 * "wincmd g}" was used in a function. */
577 do_nv_ident('g', xchar);
578 break;
579
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000580#ifdef FEAT_SEARCHPATH
581 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000582 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100583 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000584 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000585 goto wingotofile;
586#endif
Bram Moolenaar72e83c12019-02-22 16:09:52 +0100587 case 't': // CTRL-W gt: go to next tab page
588 goto_tabpage((int)Prenum);
589 break;
590
Bram Moolenaar882d02e2019-02-22 17:56:43 +0100591 case 'T': // CTRL-W gT: go to previous tab page
592 goto_tabpage(-(int)Prenum1);
593 break;
594
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 default:
596 beep_flush();
597 break;
598 }
599 break;
600
601 default: beep_flush();
602 break;
603 }
604}
605
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100606/*
Bram Moolenaarb7316892019-05-01 18:08:42 +0200607 * Figure out the address type for ":wincmd".
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100608 */
609 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100610get_wincmd_addr_type(char_u *arg, exarg_T *eap)
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100611{
612 switch (*arg)
613 {
614 case 'S':
615 case Ctrl_S:
616 case 's':
617 case Ctrl_N:
618 case 'n':
619 case 'j':
620 case Ctrl_J:
621 case 'k':
622 case Ctrl_K:
623 case 'T':
624 case Ctrl_R:
625 case 'r':
626 case 'R':
627 case 'K':
628 case 'J':
629 case '+':
630 case '-':
631 case Ctrl__:
632 case '_':
633 case '|':
634 case ']':
635 case Ctrl_RSB:
636 case 'g':
637 case Ctrl_G:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100638 case Ctrl_V:
639 case 'v':
640 case 'h':
641 case Ctrl_H:
642 case 'l':
643 case Ctrl_L:
644 case 'H':
645 case 'L':
646 case '>':
647 case '<':
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100648#if defined(FEAT_QUICKFIX)
649 case '}':
650#endif
651#ifdef FEAT_SEARCHPATH
652 case 'f':
653 case 'F':
654 case Ctrl_F:
655#endif
656#ifdef FEAT_FIND_ID
657 case 'i':
658 case Ctrl_I:
659 case 'd':
660 case Ctrl_D:
661#endif
Bram Moolenaarb7316892019-05-01 18:08:42 +0200662 // window size or any count
663 eap->addr_type = ADDR_OTHER;
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100664 break;
665
666 case Ctrl_HAT:
667 case '^':
Bram Moolenaarb7316892019-05-01 18:08:42 +0200668 // buffer number
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100669 eap->addr_type = ADDR_BUFFERS;
670 break;
671
672 case Ctrl_Q:
673 case 'q':
674 case Ctrl_C:
675 case 'c':
676 case Ctrl_O:
677 case 'o':
678 case Ctrl_W:
679 case 'w':
680 case 'W':
681 case 'x':
682 case Ctrl_X:
Bram Moolenaarb7316892019-05-01 18:08:42 +0200683 // window number
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100684 eap->addr_type = ADDR_WINDOWS;
685 break;
686
687#if defined(FEAT_QUICKFIX)
688 case Ctrl_Z:
689 case 'z':
690 case 'P':
691#endif
692 case 't':
693 case Ctrl_T:
694 case 'b':
695 case Ctrl_B:
696 case 'p':
697 case Ctrl_P:
698 case '=':
699 case CAR:
Bram Moolenaarb7316892019-05-01 18:08:42 +0200700 // no count
701 eap->addr_type = ADDR_NONE;
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100702 break;
703 }
704}
705
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100706 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100707cmd_with_count(
708 char *cmd,
709 char_u *bufp,
710 size_t bufsize,
711 long Prenum)
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100712{
713 size_t len = STRLEN(cmd);
714
715 STRCPY(bufp, cmd);
716 if (Prenum > 0)
717 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
718}
719
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720/*
721 * split the current window, implements CTRL-W s and :split
722 *
723 * "size" is the height or width for the new window, 0 to use half of current
724 * height or width.
725 *
726 * "flags":
727 * WSP_ROOM: require enough room for new window
728 * WSP_VERT: vertical split.
729 * WSP_TOP: open window at the top-left of the shell (help window).
730 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
731 * WSP_HELP: creating the help window, keep layout snapshot
732 *
733 * return FAIL for failure, OK otherwise
734 */
735 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100736win_split(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737{
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200738 if (NOT_IN_POPUP_WINDOW)
739 return FAIL;
740
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000741 /* When the ":tab" modifier was used open a new tab page instead. */
742 if (may_open_tabpage() == OK)
743 return OK;
744
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 /* Add flags from ":vertical", ":topleft" and ":botright". */
746 flags |= cmdmod.split;
747 if ((flags & WSP_TOP) && (flags & WSP_BOT))
748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100749 emsg(_("E442: Can't split topleft and botright at the same time"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 return FAIL;
751 }
752
753 /* When creating the help window make a snapshot of the window layout.
754 * Otherwise clear the snapshot, it's now invalid. */
755 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000756 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000758 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759
760 return win_split_ins(size, flags, NULL, 0);
761}
762
763/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100764 * When "new_wp" is NULL: split the current window in two.
765 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 * top/left/right/bottom.
767 * return FAIL for failure, OK otherwise
768 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000769 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100770win_split_ins(
771 int size,
772 int flags,
773 win_T *new_wp,
774 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100776 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 win_T *oldwin;
778 int new_size = size;
779 int i;
780 int need_status = 0;
781 int do_equal = FALSE;
782 int needed;
783 int available;
784 int oldwin_height = 0;
785 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200786 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200788 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200789 int wmh1;
Bram Moolenaar98da6ec2018-04-13 22:15:46 +0200790 int did_set_fraction = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
792 if (flags & WSP_TOP)
793 oldwin = firstwin;
794 else if (flags & WSP_BOT)
795 oldwin = lastwin;
796 else
797 oldwin = curwin;
798
799 /* add a status line when p_ls == 1 and splitting the first window */
Bram Moolenaar459ca562016-11-10 18:16:33 +0100800 if (ONE_WINDOW && p_ls == 1 && oldwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 {
Bram Moolenaar415a6932017-12-05 20:31:07 +0100802 if (VISIBLE_HEIGHT(oldwin) <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100804 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 return FAIL;
806 }
807 need_status = STATUS_HEIGHT;
808 }
809
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000810#ifdef FEAT_GUI
811 /* May be needed for the scrollbars that are going to change. */
812 if (gui.in_use)
813 out_flush();
814#endif
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (flags & WSP_VERT)
817 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200818 int wmw1;
819 int minwidth;
820
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
823 /*
824 * Check if we are able to split the current window and compute its
825 * width.
826 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200827 /* Current window requires at least 1 space. */
828 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
829 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200831 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200832 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200834 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200836 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200838 else if (p_ea)
839 {
840 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
841 prevfrp = oldwin->w_frame;
842 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
843 frp = frp->fr_parent)
844 {
845 if (frp->fr_layout == FR_ROW)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +0100846 FOR_ALL_FRAMES(frp2, frp->fr_child)
Bram Moolenaar54368f22014-07-23 15:21:20 +0200847 if (frp2 != prevfrp)
848 minwidth += frame_minwidth(frp2, NOWIN);
849 prevfrp = frp;
850 }
851 available = topframe->fr_width;
852 needed += minwidth;
853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200855 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200856 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
857 available = oldwin->w_frame->fr_width;
858 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200859 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100860 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100862 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 return FAIL;
864 }
865 if (new_size == 0)
866 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200867 if (new_size > available - minwidth - 1)
868 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200869 if (new_size < wmw1)
870 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
872 /* if it doesn't fit in the current window, need win_equal() */
873 if (oldwin->w_width - new_size - 1 < p_wmw)
874 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000875
876 /* We don't like to take lines for the new window from a
877 * 'winfixwidth' window. Take them from a window to the left or right
Bram Moolenaar38e34832017-03-19 20:22:36 +0100878 * instead, if possible. Add one for the separator. */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000879 if (oldwin->w_p_wfw)
Bram Moolenaar38e34832017-03-19 20:22:36 +0100880 win_setwidth_win(oldwin->w_width + new_size + 1, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000881
882 /* Only make all windows the same width if one of them (except oldwin)
883 * is wider than one of the split windows. */
884 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
885 && oldwin->w_frame->fr_parent != NULL)
886 {
887 frp = oldwin->w_frame->fr_parent->fr_child;
888 while (frp != NULL)
889 {
890 if (frp->fr_win != oldwin && frp->fr_win != NULL
891 && (frp->fr_win->w_width > new_size
892 || frp->fr_win->w_width > oldwin->w_width
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100893 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000894 {
895 do_equal = TRUE;
896 break;
897 }
898 frp = frp->fr_next;
899 }
900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 }
902 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 {
904 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905
906 /*
907 * Check if we are able to split the current window and compute its
908 * height.
909 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200910 /* Current window requires at least 1 space. */
Bram Moolenaar415a6932017-12-05 20:31:07 +0100911 wmh1 = (p_wmh == 0 ? 1 : p_wmh) + WINBAR_HEIGHT(curwin);
Bram Moolenaar1f538352014-07-16 18:19:27 +0200912 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200914 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200915 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200917 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200919 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200921 else if (p_ea)
922 {
923 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
924 prevfrp = oldwin->w_frame;
925 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
926 frp = frp->fr_parent)
927 {
928 if (frp->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +0100929 FOR_ALL_FRAMES(frp2, frp->fr_child)
Bram Moolenaar54368f22014-07-23 15:21:20 +0200930 if (frp2 != prevfrp)
931 minheight += frame_minheight(frp2, NOWIN);
932 prevfrp = frp;
933 }
934 available = topframe->fr_height;
935 needed += minheight;
936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 else
938 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200939 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
940 available = oldwin->w_frame->fr_height;
941 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100943 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100945 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 return FAIL;
947 }
948 oldwin_height = oldwin->w_height;
949 if (need_status)
950 {
951 oldwin->w_status_height = STATUS_HEIGHT;
952 oldwin_height -= STATUS_HEIGHT;
953 }
954 if (new_size == 0)
955 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200956 if (new_size > available - minheight - STATUS_HEIGHT)
957 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200958 if (new_size < wmh1)
959 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961 /* if it doesn't fit in the current window, need win_equal() */
962 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
963 do_equal = TRUE;
964
965 /* We don't like to take lines for the new window from a
966 * 'winfixheight' window. Take them from a window above or below
967 * instead, if possible. */
968 if (oldwin->w_p_wfh)
969 {
Bram Moolenaar98da6ec2018-04-13 22:15:46 +0200970 /* Set w_fraction now so that the cursor keeps the same relative
971 * vertical position using the old height. */
972 set_fraction(oldwin);
973 did_set_fraction = TRUE;
974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
976 oldwin);
977 oldwin_height = oldwin->w_height;
978 if (need_status)
979 oldwin_height -= STATUS_HEIGHT;
980 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000981
982 /* Only make all windows the same height if one of them (except oldwin)
983 * is higher than one of the split windows. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100984 if (!do_equal && p_ea && size == 0 && *p_ead != 'h'
Bram Moolenaar67f71312007-08-12 14:55:56 +0000985 && oldwin->w_frame->fr_parent != NULL)
986 {
987 frp = oldwin->w_frame->fr_parent->fr_child;
988 while (frp != NULL)
989 {
990 if (frp->fr_win != oldwin && frp->fr_win != NULL
991 && (frp->fr_win->w_height > new_size
992 || frp->fr_win->w_height > oldwin_height - new_size
993 - STATUS_HEIGHT))
994 {
995 do_equal = TRUE;
996 break;
997 }
998 frp = frp->fr_next;
999 }
1000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 }
1002
1003 /*
1004 * allocate new window structure and link it in the window list
1005 */
1006 if ((flags & WSP_TOP) == 0
1007 && ((flags & WSP_BOT)
1008 || (flags & WSP_BELOW)
1009 || (!(flags & WSP_ABOVE)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001010 && ( (flags & WSP_VERT) ? p_spr : p_sb))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 {
1012 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001013 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001014 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 else
1016 win_append(oldwin, wp);
1017 }
1018 else
1019 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001020 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001021 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 else
1023 win_append(oldwin->w_prev, wp);
1024 }
1025
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001026 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 {
1028 if (wp == NULL)
1029 return FAIL;
1030
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001031 new_frame(wp);
1032 if (wp->w_frame == NULL)
1033 {
1034 win_free(wp, NULL);
1035 return FAIL;
1036 }
1037
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001038 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001039 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
1041
1042 /*
1043 * Reorganise the tree of frames to insert the new window.
1044 */
1045 if (flags & (WSP_TOP | WSP_BOT))
1046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1048 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 {
1050 curfrp = topframe->fr_child;
1051 if (flags & WSP_BOT)
1052 while (curfrp->fr_next != NULL)
1053 curfrp = curfrp->fr_next;
1054 }
1055 else
1056 curfrp = topframe;
1057 before = (flags & WSP_TOP);
1058 }
1059 else
1060 {
1061 curfrp = oldwin->w_frame;
1062 if (flags & WSP_BELOW)
1063 before = FALSE;
1064 else if (flags & WSP_ABOVE)
1065 before = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001066 else if (flags & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 before = !p_spr;
1068 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 before = !p_sb;
1070 }
1071 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1072 {
1073 /* Need to create a new frame in the tree to make a branch. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001074 frp = ALLOC_CLEAR_ONE(frame_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 *frp = *curfrp;
1076 curfrp->fr_layout = layout;
1077 frp->fr_parent = curfrp;
1078 frp->fr_next = NULL;
1079 frp->fr_prev = NULL;
1080 curfrp->fr_child = frp;
1081 curfrp->fr_win = NULL;
1082 curfrp = frp;
1083 if (frp->fr_win != NULL)
1084 oldwin->w_frame = frp;
1085 else
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01001086 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 frp->fr_parent = curfrp;
1088 }
1089
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001090 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001091 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001093 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 frp->fr_parent = curfrp->fr_parent;
1095
1096 /* Insert the new frame at the right place in the frame list. */
1097 if (before)
1098 frame_insert(curfrp, frp);
1099 else
1100 frame_append(curfrp, frp);
1101
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001102 /* Set w_fraction now so that the cursor keeps the same relative
1103 * vertical position. */
Bram Moolenaar98da6ec2018-04-13 22:15:46 +02001104 if (!did_set_fraction)
1105 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001106 wp->w_fraction = oldwin->w_fraction;
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 if (flags & WSP_VERT)
1109 {
1110 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001111
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 if (need_status)
1113 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001114 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 oldwin->w_status_height = need_status;
1116 }
1117 if (flags & (WSP_TOP | WSP_BOT))
1118 {
1119 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001120 wp->w_winrow = tabline_height();
Bram Moolenaard326ad62017-09-18 20:31:41 +02001121 win_new_height(wp, curfrp->fr_height - (p_ls > 0)
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001122 - WINBAR_HEIGHT(wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 wp->w_status_height = (p_ls > 0);
1124 }
1125 else
1126 {
1127 /* height and row of new window is same as current window */
1128 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01001129 win_new_height(wp, VISIBLE_HEIGHT(oldwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 wp->w_status_height = oldwin->w_status_height;
1131 }
1132 frp->fr_height = curfrp->fr_height;
1133
1134 /* "new_size" of the current window goes to the new window, use
1135 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001136 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 if (before)
1138 wp->w_vsep_width = 1;
1139 else
1140 {
1141 wp->w_vsep_width = oldwin->w_vsep_width;
1142 oldwin->w_vsep_width = 1;
1143 }
1144 if (flags & (WSP_TOP | WSP_BOT))
1145 {
1146 if (flags & WSP_BOT)
1147 frame_add_vsep(curfrp);
1148 /* Set width of neighbor frame */
1149 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001150 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1151 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 }
1153 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001154 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (before) /* new window left of current one */
1156 {
1157 wp->w_wincol = oldwin->w_wincol;
1158 oldwin->w_wincol += new_size + 1;
1159 }
1160 else /* new window right of current one */
1161 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1162 frame_fix_width(oldwin);
1163 frame_fix_width(wp);
1164 }
1165 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {
1167 /* width and column of new window is same as current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 if (flags & (WSP_TOP | WSP_BOT))
1169 {
1170 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001171 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 wp->w_vsep_width = 0;
1173 }
1174 else
1175 {
1176 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001177 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 wp->w_vsep_width = oldwin->w_vsep_width;
1179 }
1180 frp->fr_width = curfrp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181
1182 /* "new_size" of the current window goes to the new window, use
1183 * one row for the status line */
1184 win_new_height(wp, new_size);
1185 if (flags & (WSP_TOP | WSP_BOT))
Bram Moolenaar991dea32016-05-24 11:31:32 +02001186 {
Bram Moolenaard326ad62017-09-18 20:31:41 +02001187 int new_fr_height = curfrp->fr_height - new_size
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001188 + WINBAR_HEIGHT(wp) ;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001189
1190 if (!((flags & WSP_BOT) && p_ls == 0))
1191 new_fr_height -= STATUS_HEIGHT;
1192 frame_new_height(curfrp, new_fr_height, flags & WSP_TOP, FALSE);
1193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 else
1195 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1196 if (before) /* new window above current one */
1197 {
1198 wp->w_winrow = oldwin->w_winrow;
1199 wp->w_status_height = STATUS_HEIGHT;
1200 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1201 }
1202 else /* new window below current one */
1203 {
Bram Moolenaar415a6932017-12-05 20:31:07 +01001204 wp->w_winrow = oldwin->w_winrow + VISIBLE_HEIGHT(oldwin)
1205 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 wp->w_status_height = oldwin->w_status_height;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001207 if (!(flags & WSP_BOT))
1208 oldwin->w_status_height = STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 if (flags & WSP_BOT)
1211 frame_add_statusline(curfrp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 frame_fix_height(wp);
1213 frame_fix_height(oldwin);
1214 }
1215
1216 if (flags & (WSP_TOP | WSP_BOT))
1217 (void)win_comp_pos();
1218
1219 /*
1220 * Both windows need redrawing
1221 */
1222 redraw_win_later(wp, NOT_VALID);
1223 wp->w_redr_status = TRUE;
1224 redraw_win_later(oldwin, NOT_VALID);
1225 oldwin->w_redr_status = TRUE;
1226
1227 if (need_status)
1228 {
1229 msg_row = Rows - 1;
1230 msg_col = sc_col;
1231 msg_clr_eos_force(); /* Old command/ruler may still be there */
1232 comp_col();
1233 msg_row = Rows - 1;
1234 msg_col = 0; /* put position back at start of line */
1235 }
1236
1237 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001238 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 */
1240 if (do_equal || dir != 0)
1241 win_equal(wp, TRUE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001243 : dir == 'h' ? 'b' : 'v');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1246 * size was given. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (flags & WSP_VERT)
1248 {
1249 i = p_wiw;
1250 if (size != 0)
1251 p_wiw = size;
1252
1253# ifdef FEAT_GUI
1254 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1255 if (gui.in_use)
1256 gui_init_which_components(NULL);
1257# endif
1258 }
1259 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {
1261 i = p_wh;
1262 if (size != 0)
1263 p_wh = size;
1264 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001265
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001266#ifdef FEAT_JUMPLIST
1267 /* Keep same changelist position in new window. */
1268 wp->w_changelistidx = oldwin->w_changelistidx;
1269#endif
1270
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001271 /*
1272 * make the new window the current window
1273 */
Bram Moolenaarc917da42016-07-19 22:31:36 +02001274 win_enter_ext(wp, FALSE, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 if (flags & WSP_VERT)
1276 p_wiw = i;
1277 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 p_wh = i;
1279
1280 return OK;
1281}
1282
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001283
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001284/*
1285 * Initialize window "newp" from window "oldp".
1286 * Used when splitting a window and when creating a new tab page.
1287 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001288 * WSP_NEWLOC may be specified in flags to prevent the location list from
1289 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001290 */
1291 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001292win_init(win_T *newp, win_T *oldp, int flags UNUSED)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001293{
1294 int i;
1295
1296 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001297#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001298 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001299#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001300 oldp->w_buffer->b_nwindows++;
1301 newp->w_cursor = oldp->w_cursor;
1302 newp->w_valid = 0;
1303 newp->w_curswant = oldp->w_curswant;
1304 newp->w_set_curswant = oldp->w_set_curswant;
1305 newp->w_topline = oldp->w_topline;
1306#ifdef FEAT_DIFF
1307 newp->w_topfill = oldp->w_topfill;
1308#endif
1309 newp->w_leftcol = oldp->w_leftcol;
1310 newp->w_pcmark = oldp->w_pcmark;
1311 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1312 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001313 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001314 newp->w_fraction = oldp->w_fraction;
1315 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1316#ifdef FEAT_JUMPLIST
1317 copy_jumplist(oldp, newp);
1318#endif
1319#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001320 if (flags & WSP_NEWLOC)
1321 {
1322 /* Don't copy the location list. */
1323 newp->w_llist = NULL;
1324 newp->w_llist_ref = NULL;
1325 }
1326 else
Bram Moolenaar09037502018-09-25 22:08:14 +02001327 copy_loclist_stack(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001328#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001329 newp->w_localdir = (oldp->w_localdir == NULL)
1330 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001331
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001332 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001333 for (i = 0; i < oldp->w_tagstacklen; i++)
1334 {
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02001335 taggy_T *tag = &newp->w_tagstack[i];
1336 *tag = oldp->w_tagstack[i];
1337 if (tag->tagname != NULL)
1338 tag->tagname = vim_strsave(tag->tagname);
1339 if (tag->user_data != NULL)
1340 tag->user_data = vim_strsave(tag->user_data);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001341 }
1342 newp->w_tagstackidx = oldp->w_tagstackidx;
1343 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001344#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001345 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001346#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001347
1348 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001349
Bram Moolenaara971b822011-09-14 14:43:25 +02001350#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001351 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001352#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001353}
1354
1355/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001356 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001357 * Only the essential things are copied.
1358 */
1359 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001360win_init_some(win_T *newp, win_T *oldp)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001361{
1362 /* Use the same argument list. */
1363 newp->w_alist = oldp->w_alist;
1364 ++newp->w_alist->al_refcount;
1365 newp->w_arg_idx = oldp->w_arg_idx;
1366
1367 /* copy options from existing window */
1368 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001369}
1370
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001371 static int
Bram Moolenaar682725c2019-05-25 20:10:37 +02001372win_valid_popup(win_T *win UNUSED)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001373{
1374#ifdef FEAT_TEXT_PROP
1375 win_T *wp;
1376
1377 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1378 if (wp == win)
1379 return TRUE;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001380 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001381 if (wp == win)
1382 return TRUE;
1383#endif
1384 return FALSE;
1385}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001388 * Check if "win" is a pointer to an existing window in the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 */
1390 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001391win_valid(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
1393 win_T *wp;
1394
1395 if (win == NULL)
1396 return FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02001397 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 if (wp == win)
1399 return TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001400 return win_valid_popup(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401}
1402
1403/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001404 * Check if "win" is a pointer to an existing window in any tab page.
1405 */
1406 int
1407win_valid_any_tab(win_T *win)
1408{
1409 win_T *wp;
1410 tabpage_T *tp;
1411
1412 if (win == NULL)
1413 return FALSE;
1414 FOR_ALL_TABPAGES(tp)
1415 {
1416 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
1417 {
1418 if (wp == win)
1419 return TRUE;
1420 }
1421 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001422 return win_valid_popup(win);
Bram Moolenaare59215c2016-08-14 19:08:45 +02001423}
1424
1425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 * Return the number of windows.
1427 */
1428 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001429win_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430{
1431 win_T *wp;
1432 int count = 0;
1433
Bram Moolenaar29323592016-07-24 22:04:11 +02001434 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 ++count;
1436 return count;
1437}
1438
1439/*
1440 * Make "count" windows on the screen.
1441 * Return actual number of windows on the screen.
1442 * Must be called when there is just one window, filling the whole screen
1443 * (excluding the command line).
1444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001446make_windows(
1447 int count,
1448 int vertical UNUSED) /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
1450 int maxcount;
1451 int todo;
1452
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 if (vertical)
1454 {
1455 /* Each windows needs at least 'winminwidth' lines and a separator
1456 * column. */
1457 maxcount = (curwin->w_width + curwin->w_vsep_width
1458 - (p_wiw - p_wmw)) / (p_wmw + 1);
1459 }
1460 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 {
1462 /* Each window needs at least 'winminheight' lines and a status line. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001463 maxcount = (VISIBLE_HEIGHT(curwin) + curwin->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1465 }
1466
1467 if (maxcount < 2)
1468 maxcount = 2;
1469 if (count > maxcount)
1470 count = maxcount;
1471
1472 /*
1473 * add status line now, otherwise first window will be too big
1474 */
1475 if (count > 1)
1476 last_status(TRUE);
1477
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 /*
1479 * Don't execute autocommands while creating the windows. Must do that
1480 * when putting the buffers in the windows.
1481 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001482 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
1484 /* todo is number of windows left to create */
1485 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 if (vertical)
1487 {
1488 if (win_split(curwin->w_width - (curwin->w_width - todo)
1489 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1490 break;
1491 }
1492 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 {
1494 if (win_split(curwin->w_height - (curwin->w_height - todo
1495 * STATUS_HEIGHT) / (todo + 1)
1496 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1497 break;
1498 }
1499
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001500 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501
1502 /* return actual number of windows */
1503 return (count - todo);
1504}
1505
1506/*
1507 * Exchange current and next window
1508 */
1509 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001510win_exchange(long Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
1512 frame_T *frp;
1513 frame_T *frp2;
1514 win_T *wp;
1515 win_T *wp2;
1516 int temp;
1517
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001518 if (NOT_IN_POPUP_WINDOW)
1519 return;
1520 if (ONE_WINDOW) // just one window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 {
1522 beep_flush();
1523 return;
1524 }
1525
1526#ifdef FEAT_GUI
1527 need_mouse_correct = TRUE;
1528#endif
1529
1530 /*
1531 * find window to exchange with
1532 */
1533 if (Prenum)
1534 {
1535 frp = curwin->w_frame->fr_parent->fr_child;
1536 while (frp != NULL && --Prenum > 0)
1537 frp = frp->fr_next;
1538 }
1539 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1540 frp = curwin->w_frame->fr_next;
1541 else /* Swap last window in row/col with previous */
1542 frp = curwin->w_frame->fr_prev;
1543
1544 /* We can only exchange a window with another window, not with a frame
1545 * containing windows. */
1546 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1547 return;
1548 wp = frp->fr_win;
1549
1550/*
1551 * 1. remove curwin from the list. Remember after which window it was in wp2
1552 * 2. insert curwin before wp in the list
1553 * if wp != wp2
1554 * 3. remove wp from the list
1555 * 4. insert wp after wp2
1556 * 5. exchange the status line height and vsep width.
1557 */
1558 wp2 = curwin->w_prev;
1559 frp2 = curwin->w_frame->fr_prev;
1560 if (wp->w_prev != curwin)
1561 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001562 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 frame_remove(curwin->w_frame);
1564 win_append(wp->w_prev, curwin);
1565 frame_insert(frp, curwin->w_frame);
1566 }
1567 if (wp != wp2)
1568 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001569 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 frame_remove(wp->w_frame);
1571 win_append(wp2, wp);
1572 if (frp2 == NULL)
1573 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1574 else
1575 frame_append(frp2, wp->w_frame);
1576 }
1577 temp = curwin->w_status_height;
1578 curwin->w_status_height = wp->w_status_height;
1579 wp->w_status_height = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 temp = curwin->w_vsep_width;
1581 curwin->w_vsep_width = wp->w_vsep_width;
1582 wp->w_vsep_width = temp;
1583
1584 /* If the windows are not in the same frame, exchange the sizes to avoid
1585 * messing up the window layout. Otherwise fix the frame sizes. */
1586 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1587 {
1588 temp = curwin->w_height;
1589 curwin->w_height = wp->w_height;
1590 wp->w_height = temp;
1591 temp = curwin->w_width;
1592 curwin->w_width = wp->w_width;
1593 wp->w_width = temp;
1594 }
1595 else
1596 {
1597 frame_fix_height(curwin);
1598 frame_fix_height(wp);
1599 frame_fix_width(curwin);
1600 frame_fix_width(wp);
1601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602
1603 (void)win_comp_pos(); /* recompute window positions */
1604
1605 win_enter(wp, TRUE);
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01001606 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607}
1608
1609/*
1610 * rotate windows: if upwards TRUE the second window becomes the first one
1611 * if upwards FALSE the first window becomes the second one
1612 */
1613 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001614win_rotate(int upwards, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615{
1616 win_T *wp1;
1617 win_T *wp2;
1618 frame_T *frp;
1619 int n;
1620
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001621 if (ONE_WINDOW) /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
1623 beep_flush();
1624 return;
1625 }
1626
1627#ifdef FEAT_GUI
1628 need_mouse_correct = TRUE;
1629#endif
1630
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 /* Check if all frames in this row/col have one window. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01001632 FOR_ALL_FRAMES(frp, curwin->w_frame->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 if (frp->fr_win == NULL)
1634 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001635 emsg(_("E443: Cannot rotate when another window is split"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 return;
1637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
1639 while (count--)
1640 {
1641 if (upwards) /* first window becomes last window */
1642 {
1643 /* remove first window/frame from the list */
1644 frp = curwin->w_frame->fr_parent->fr_child;
1645 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001646 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 frame_remove(frp);
1648
1649 /* find last frame and append removed window/frame after it */
1650 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1651 ;
1652 win_append(frp->fr_win, wp1);
1653 frame_append(frp, wp1->w_frame);
1654
1655 wp2 = frp->fr_win; /* previously last window */
1656 }
1657 else /* last window becomes first window */
1658 {
1659 /* find last window/frame in the list and remove it */
1660 for (frp = curwin->w_frame; frp->fr_next != NULL;
1661 frp = frp->fr_next)
1662 ;
1663 wp1 = frp->fr_win;
1664 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001665 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 frame_remove(frp);
1667
1668 /* append the removed window/frame before the first in the list */
1669 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1670 frame_insert(frp->fr_parent->fr_child, frp);
1671 }
1672
1673 /* exchange status height and vsep width of old and new last window */
1674 n = wp2->w_status_height;
1675 wp2->w_status_height = wp1->w_status_height;
1676 wp1->w_status_height = n;
1677 frame_fix_height(wp1);
1678 frame_fix_height(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 n = wp2->w_vsep_width;
1680 wp2->w_vsep_width = wp1->w_vsep_width;
1681 wp1->w_vsep_width = n;
1682 frame_fix_width(wp1);
1683 frame_fix_width(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001685 /* recompute w_winrow and w_wincol for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 (void)win_comp_pos();
1687 }
1688
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01001689 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690}
1691
1692/*
1693 * Move the current window to the very top/bottom/left/right of the screen.
1694 */
1695 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001696win_totop(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697{
1698 int dir;
1699 int height = curwin->w_height;
1700
Bram Moolenaar459ca562016-11-10 18:16:33 +01001701 if (ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 {
1703 beep_flush();
1704 return;
1705 }
1706
1707 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001708 (void)winframe_remove(curwin, &dir, NULL);
1709 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 last_status(FALSE); /* may need to remove last status line */
1711 (void)win_comp_pos(); /* recompute window positions */
1712
1713 /* Split a window on the desired side and put the window there. */
1714 (void)win_split_ins(size, flags, curwin, dir);
1715 if (!(flags & WSP_VERT))
1716 {
1717 win_setheight(height);
1718 if (p_ea)
1719 win_equal(curwin, TRUE, 'v');
1720 }
1721
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001722#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1724 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001725 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727}
1728
1729/*
1730 * Move window "win1" to below/right of "win2" and make "win1" the current
1731 * window. Only works within the same frame!
1732 */
1733 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001734win_move_after(win_T *win1, win_T *win2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735{
1736 int height;
1737
1738 /* check if the arguments are reasonable */
1739 if (win1 == win2)
1740 return;
1741
1742 /* check if there is something to do */
1743 if (win2->w_next != win1)
1744 {
1745 /* may need move the status line/vertical separator of the last window
1746 * */
1747 if (win1 == lastwin)
1748 {
1749 height = win1->w_prev->w_status_height;
1750 win1->w_prev->w_status_height = win1->w_status_height;
1751 win1->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001752 if (win1->w_prev->w_vsep_width == 1)
1753 {
1754 /* Remove the vertical separator from the last-but-one window,
1755 * add it to the last window. Adjust the frame widths. */
1756 win1->w_prev->w_vsep_width = 0;
1757 win1->w_prev->w_frame->fr_width -= 1;
1758 win1->w_vsep_width = 1;
1759 win1->w_frame->fr_width += 1;
1760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
1762 else if (win2 == lastwin)
1763 {
1764 height = win1->w_status_height;
1765 win1->w_status_height = win2->w_status_height;
1766 win2->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001767 if (win1->w_vsep_width == 1)
1768 {
1769 /* Remove the vertical separator from win1, add it to the last
1770 * window, win2. Adjust the frame widths. */
1771 win2->w_vsep_width = 1;
1772 win2->w_frame->fr_width += 1;
1773 win1->w_vsep_width = 0;
1774 win1->w_frame->fr_width -= 1;
1775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001777 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 frame_remove(win1->w_frame);
1779 win_append(win2, win1);
1780 frame_append(win2->w_frame, win1->w_frame);
1781
1782 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1783 redraw_later(NOT_VALID);
1784 }
1785 win_enter(win1, FALSE);
1786}
1787
1788/*
1789 * Make all windows the same height.
1790 * 'next_curwin' will soon be the current window, make sure it has enough
1791 * rows.
1792 */
1793 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001794win_equal(
1795 win_T *next_curwin, /* pointer to current window to be or NULL */
1796 int current, /* do only frame with current window */
1797 int dir) /* 'v' for vertically, 'h' for horizontally,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 'b' for both, 0 for using p_ead */
1799{
1800 if (dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 dir = *p_ead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001803 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001804 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805}
1806
1807/*
1808 * Set a frame to a new position and height, spreading the available room
1809 * equally over contained frames.
1810 * The window "next_curwin" (if not NULL) should at least get the size from
1811 * 'winheight' and 'winwidth' if possible.
1812 */
1813 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001814win_equal_rec(
1815 win_T *next_curwin, /* pointer to current window to be or NULL */
1816 int current, /* do only frame with current window */
1817 frame_T *topfr, /* frame to set size off */
1818 int dir, /* 'v', 'h' or 'b', see win_equal() */
1819 int col, /* horizontal position for frame */
1820 int row, /* vertical position for frame */
1821 int width, /* new width of frame */
1822 int height) /* new height of frame */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 int n, m;
1825 int extra_sep = 0;
1826 int wincount, totwincount = 0;
1827 frame_T *fr;
1828 int next_curwin_size = 0;
1829 int room = 0;
1830 int new_size;
1831 int has_next_curwin = 0;
1832 int hnc;
1833
1834 if (topfr->fr_layout == FR_LEAF)
1835 {
1836 /* Set the width/height of this frame.
1837 * Redraw when size or position changes */
1838 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 )
1841 {
1842 topfr->fr_win->w_winrow = row;
1843 frame_new_height(topfr, height, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001845 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01001846 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 }
1848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 else if (topfr->fr_layout == FR_ROW)
1850 {
1851 topfr->fr_width = width;
1852 topfr->fr_height = height;
1853
1854 if (dir != 'v') /* equalize frame widths */
1855 {
1856 /* Compute the maximum number of windows horizontally in this
1857 * frame. */
1858 n = frame_minwidth(topfr, NOWIN);
1859 /* add one for the rightmost window, it doesn't have a separator */
1860 if (col + width == Columns)
1861 extra_sep = 1;
1862 else
1863 extra_sep = 0;
1864 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001865 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001867 /*
1868 * Compute width for "next_curwin" window and room available for
1869 * other windows.
1870 * "m" is the minimal width when counting p_wiw for "next_curwin".
1871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 m = frame_minwidth(topfr, next_curwin);
1873 room = width - m;
1874 if (room < 0)
1875 {
1876 next_curwin_size = p_wiw + room;
1877 room = 0;
1878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 else
1880 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001881 next_curwin_size = -1;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01001882 FOR_ALL_FRAMES(fr, topfr->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001883 {
1884 /* If 'winfixwidth' set keep the window width if
1885 * possible.
1886 * Watch out for this window being the next_curwin. */
1887 if (frame_fixed_width(fr))
1888 {
1889 n = frame_minwidth(fr, NOWIN);
1890 new_size = fr->fr_width;
1891 if (frame_has_win(fr, next_curwin))
1892 {
1893 room += p_wiw - p_wmw;
1894 next_curwin_size = 0;
1895 if (new_size < p_wiw)
1896 new_size = p_wiw;
1897 }
1898 else
1899 /* These windows don't use up room. */
1900 totwincount -= (n + (fr->fr_next == NULL
1901 ? extra_sep : 0)) / (p_wmw + 1);
1902 room -= new_size - n;
1903 if (room < 0)
1904 {
1905 new_size += room;
1906 room = 0;
1907 }
1908 fr->fr_newwidth = new_size;
1909 }
1910 }
1911 if (next_curwin_size == -1)
1912 {
1913 if (!has_next_curwin)
1914 next_curwin_size = 0;
1915 else if (totwincount > 1
1916 && (room + (totwincount - 2))
1917 / (totwincount - 1) > p_wiw)
1918 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001919 /* Can make all windows wider than 'winwidth', spread
1920 * the room equally. */
1921 next_curwin_size = (room + p_wiw
1922 + (totwincount - 1) * p_wmw
1923 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001924 room -= next_curwin_size - p_wiw;
1925 }
1926 else
1927 next_curwin_size = p_wiw;
1928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001930
1931 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 --totwincount; /* don't count curwin */
1933 }
1934
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01001935 FOR_ALL_FRAMES(fr, topfr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 wincount = 1;
1938 if (fr->fr_next == NULL)
1939 /* last frame gets all that remains (avoid roundoff error) */
1940 new_size = width;
1941 else if (dir == 'v')
1942 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001943 else if (frame_fixed_width(fr))
1944 {
1945 new_size = fr->fr_newwidth;
1946 wincount = 0; /* doesn't count as a sizeable window */
1947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 else
1949 {
1950 /* Compute the maximum number of windows horiz. in "fr". */
1951 n = frame_minwidth(fr, NOWIN);
1952 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1953 / (p_wmw + 1);
1954 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001955 if (has_next_curwin)
1956 hnc = frame_has_win(fr, next_curwin);
1957 else
1958 hnc = FALSE;
1959 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001961 if (totwincount == 0)
1962 new_size = room;
1963 else
1964 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001966 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 {
1968 next_curwin_size -= p_wiw - (m - n);
1969 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001970 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001972 else
1973 room -= new_size;
1974 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
1976
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001977 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 * window, unless equalizing all frames. */
1979 if (!current || dir != 'v' || topfr->fr_parent != NULL
1980 || (new_size != fr->fr_width)
1981 || frame_has_win(fr, next_curwin))
1982 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001983 new_size, height);
1984 col += new_size;
1985 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 totwincount -= wincount;
1987 }
1988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 else /* topfr->fr_layout == FR_COL */
1990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 topfr->fr_width = width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 topfr->fr_height = height;
1993
1994 if (dir != 'h') /* equalize frame heights */
1995 {
1996 /* Compute maximum number of windows vertically in this frame. */
1997 n = frame_minheight(topfr, NOWIN);
1998 /* add one for the bottom window if it doesn't have a statusline */
1999 if (row + height == cmdline_row && p_ls == 0)
2000 extra_sep = 1;
2001 else
2002 extra_sep = 0;
2003 totwincount = (n + extra_sep) / (p_wmh + 1);
2004 has_next_curwin = frame_has_win(topfr, next_curwin);
2005
2006 /*
2007 * Compute height for "next_curwin" window and room available for
2008 * other windows.
2009 * "m" is the minimal height when counting p_wh for "next_curwin".
2010 */
2011 m = frame_minheight(topfr, next_curwin);
2012 room = height - m;
2013 if (room < 0)
2014 {
2015 /* The room is less then 'winheight', use all space for the
2016 * current window. */
2017 next_curwin_size = p_wh + room;
2018 room = 0;
2019 }
2020 else
2021 {
2022 next_curwin_size = -1;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002023 FOR_ALL_FRAMES(fr, topfr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 {
2025 /* If 'winfixheight' set keep the window height if
2026 * possible.
2027 * Watch out for this window being the next_curwin. */
2028 if (frame_fixed_height(fr))
2029 {
2030 n = frame_minheight(fr, NOWIN);
2031 new_size = fr->fr_height;
2032 if (frame_has_win(fr, next_curwin))
2033 {
2034 room += p_wh - p_wmh;
2035 next_curwin_size = 0;
2036 if (new_size < p_wh)
2037 new_size = p_wh;
2038 }
2039 else
2040 /* These windows don't use up room. */
2041 totwincount -= (n + (fr->fr_next == NULL
2042 ? extra_sep : 0)) / (p_wmh + 1);
2043 room -= new_size - n;
2044 if (room < 0)
2045 {
2046 new_size += room;
2047 room = 0;
2048 }
2049 fr->fr_newheight = new_size;
2050 }
2051 }
2052 if (next_curwin_size == -1)
2053 {
2054 if (!has_next_curwin)
2055 next_curwin_size = 0;
2056 else if (totwincount > 1
2057 && (room + (totwincount - 2))
2058 / (totwincount - 1) > p_wh)
2059 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002060 /* can make all windows higher than 'winheight',
2061 * spread the room equally. */
2062 next_curwin_size = (room + p_wh
2063 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 + (totwincount - 1)) / totwincount;
2065 room -= next_curwin_size - p_wh;
2066 }
2067 else
2068 next_curwin_size = p_wh;
2069 }
2070 }
2071
2072 if (has_next_curwin)
2073 --totwincount; /* don't count curwin */
2074 }
2075
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002076 FOR_ALL_FRAMES(fr, topfr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 wincount = 1;
2079 if (fr->fr_next == NULL)
2080 /* last frame gets all that remains (avoid roundoff error) */
2081 new_size = height;
2082 else if (dir == 'h')
2083 new_size = fr->fr_height;
2084 else if (frame_fixed_height(fr))
2085 {
2086 new_size = fr->fr_newheight;
2087 wincount = 0; /* doesn't count as a sizeable window */
2088 }
2089 else
2090 {
2091 /* Compute the maximum number of windows vert. in "fr". */
2092 n = frame_minheight(fr, NOWIN);
2093 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2094 / (p_wmh + 1);
2095 m = frame_minheight(fr, next_curwin);
2096 if (has_next_curwin)
2097 hnc = frame_has_win(fr, next_curwin);
2098 else
2099 hnc = FALSE;
2100 if (hnc) /* don't count next_curwin */
2101 --wincount;
2102 if (totwincount == 0)
2103 new_size = room;
2104 else
2105 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2106 / totwincount;
2107 if (hnc) /* add next_curwin size */
2108 {
2109 next_curwin_size -= p_wh - (m - n);
2110 new_size += next_curwin_size;
2111 room -= new_size - next_curwin_size;
2112 }
2113 else
2114 room -= new_size;
2115 new_size += n;
2116 }
2117 /* Skip frame that is full width when splitting or closing a
2118 * window, unless equalizing all frames. */
2119 if (!current || dir != 'h' || topfr->fr_parent != NULL
2120 || (new_size != fr->fr_height)
2121 || frame_has_win(fr, next_curwin))
2122 win_equal_rec(next_curwin, current, fr, dir, col, row,
2123 width, new_size);
2124 row += new_size;
2125 height -= new_size;
2126 totwincount -= wincount;
2127 }
2128 }
2129}
2130
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002131#ifdef FEAT_JOB_CHANNEL
2132 static void
2133leaving_window(win_T *win)
2134{
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002135 // Only matters for a prompt window.
2136 if (!bt_prompt(win->w_buffer))
2137 return;
2138
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002139 // When leaving a prompt window stop Insert mode and perhaps restart
2140 // it when entering that window again.
2141 win->w_buffer->b_prompt_insert = restart_edit;
Bram Moolenaar942b4542018-06-17 16:23:34 +02002142 if (restart_edit != 0 && mode_displayed)
2143 clear_cmdline = TRUE; /* unshow mode later */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002144 restart_edit = NUL;
2145
2146 // When leaving the window (or closing the window) was done from a
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002147 // callback we need to break out of the Insert mode loop and restart Insert
2148 // mode when entering the window again.
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002149 if (State & INSERT)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002150 {
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002151 stop_insert_mode = TRUE;
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002152 if (win->w_buffer->b_prompt_insert == NUL)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002153 win->w_buffer->b_prompt_insert = 'A';
2154 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002155}
2156
2157 static void
2158entering_window(win_T *win)
2159{
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002160 // Only matters for a prompt window.
2161 if (!bt_prompt(win->w_buffer))
2162 return;
2163
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002164 // When switching to a prompt buffer that was in Insert mode, don't stop
2165 // Insert mode, it may have been set in leaving_window().
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002166 if (win->w_buffer->b_prompt_insert != NUL)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002167 stop_insert_mode = FALSE;
2168
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002169 // When entering the prompt window restart Insert mode if we were in Insert
2170 // mode when we left it.
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002171 restart_edit = win->w_buffer->b_prompt_insert;
2172}
2173#endif
2174
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002176 * Close all windows for buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 */
2178 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002179close_windows(
2180 buf_T *buf,
2181 int keep_curwin) /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002183 win_T *wp;
2184 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002185 int h = tabline_height();
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002186 int count = tabpage_index(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
2188 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002189
Bram Moolenaar459ca562016-11-10 18:16:33 +01002190 for (wp = firstwin; wp != NULL && !ONE_WINDOW; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002192 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002193 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 {
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002195 if (win_close(wp, FALSE) == FAIL)
2196 /* If closing the window fails give up, to avoid looping
2197 * forever. */
2198 break;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002199
2200 /* Start all over, autocommands may change the window layout. */
2201 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 }
2203 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002204 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002206
2207 /* Also check windows in other tab pages. */
2208 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2209 {
2210 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002211 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002212 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002213 if (wp->w_buffer == buf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002214 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaarf740b292006-02-16 22:11:02 +00002215 {
2216 win_close_othertab(wp, FALSE, tp);
2217
2218 /* Start all over, the tab page may be closed and
2219 * autocommands may change the window layout. */
2220 nexttp = first_tabpage;
2221 break;
2222 }
2223 }
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002226
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002227 if (count != tabpage_index(NULL))
2228 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002229
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002230 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002231 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002232 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233}
2234
2235/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002236 * Return TRUE if the current window is the only window that exists (ignoring
2237 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002238 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002239 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002240 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002241last_window(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002242{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002243 return (one_window() && first_tabpage->tp_next == NULL);
2244}
2245
2246/*
2247 * Return TRUE if there is only one window other than "aucmd_win" in the
2248 * current tab page.
2249 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002250 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002251one_window(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002252{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002253 win_T *wp;
2254 int seen_one = FALSE;
2255
2256 FOR_ALL_WINDOWS(wp)
2257 {
2258 if (wp != aucmd_win)
2259 {
2260 if (seen_one)
2261 return FALSE;
2262 seen_one = TRUE;
2263 }
2264 }
2265 return TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002266}
2267
2268/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002269 * Close the possibly last window in a tab page.
2270 * Returns TRUE when the window was closed already.
2271 */
2272 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002273close_last_window_tabpage(
2274 win_T *win,
2275 int free_buf,
2276 tabpage_T *prev_curtab)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002277{
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002278 if (ONE_WINDOW)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002279 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002280 buf_T *old_curbuf = curbuf;
2281
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002282 /*
2283 * Closing the last window in a tab page. First go to another tab
2284 * page and then close the window and the tab page. This avoids that
2285 * curwin and curtab are invalid while we are freeing memory, they may
2286 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002287 * Don't trigger autocommands yet, they may use wrong values, so do
2288 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002289 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002290 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002291 redraw_tabline = TRUE;
2292
2293 /* Safety check: Autocommands may have closed the window when jumping
2294 * to the other tab page. */
2295 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2296 {
2297 int h = tabline_height();
2298
2299 win_close_othertab(win, free_buf, prev_curtab);
2300 if (h != tabline_height())
2301 shell_new_rows();
2302 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002303#ifdef FEAT_JOB_CHANNEL
2304 entering_window(curwin);
2305#endif
Bram Moolenaara8596c42012-06-13 14:28:20 +02002306 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2307 * that now. */
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002308 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002309 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002310 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2311 if (old_curbuf != curbuf)
2312 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002313 return TRUE;
2314 }
2315 return FALSE;
2316}
2317
2318/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002319 * Close the buffer of "win" and unload it if "free_buf" is TRUE.
2320 * "abort_if_last" is passed to close_buffer(): abort closing if all other
2321 * windows are closed.
2322 */
2323 static void
2324win_close_buffer(win_T *win, int free_buf, int abort_if_last)
2325{
2326#ifdef FEAT_SYN_HL
2327 // Free independent synblock before the buffer is freed.
2328 if (win->w_buffer != NULL)
2329 reset_synblock(win);
2330#endif
2331
2332#ifdef FEAT_QUICKFIX
2333 // When the quickfix/location list window is closed, unlist the buffer.
2334 if (win->w_buffer != NULL && bt_quickfix(win->w_buffer))
2335 win->w_buffer->b_p_bl = FALSE;
2336#endif
2337
2338 // Close the link to the buffer.
2339 if (win->w_buffer != NULL)
2340 {
2341 bufref_T bufref;
2342
2343 set_bufref(&bufref, curbuf);
2344 win->w_closing = TRUE;
2345 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0,
2346 abort_if_last);
2347 if (win_valid_any_tab(win))
2348 win->w_closing = FALSE;
2349 // Make sure curbuf is valid. It can become invalid if 'bufhidden' is
2350 // "wipe".
2351 if (!bufref_valid(&bufref))
2352 curbuf = firstbuf;
2353 }
2354}
2355
2356/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002357 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 * If "free_buf" is TRUE related buffer may be unloaded.
2359 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002360 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002361 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002363 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002364win_close(win_T *win, int free_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365{
2366 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 int dir;
2370 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002371 tabpage_T *prev_curtab = curtab;
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002372 frame_T *win_frame = win->w_frame->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002374 if (NOT_IN_POPUP_WINDOW)
2375 return FAIL;
2376
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002377 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002379 emsg(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002380 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 }
2382
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002383 if (win->w_closing || (win->w_buffer != NULL
2384 && win->w_buffer->b_locked > 0))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002385 return FAIL; /* window is already being closed */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002386 if (win_unlisted(win))
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002387 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002388 emsg(_("E813: Cannot close autocmd or popup window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002389 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002390 }
2391 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2392 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002393 emsg(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002394 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002395 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002396
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002397 /* When closing the last window in a tab page first go to another tab page
2398 * and then close the window and the tab page to avoid that curwin and
2399 * curtab are invalid while we are freeing memory. */
2400 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002401 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002402
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 /* When closing the help window, try restoring a snapshot after closing
2404 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002405 if (bt_help(win->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 help_window = TRUE;
2407 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002408 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 if (win == curwin)
2411 {
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002412#ifdef FEAT_JOB_CHANNEL
2413 leaving_window(curwin);
2414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 /*
2416 * Guess which window is going to be the new current window.
2417 * This may change because of the autocommands (sigh).
2418 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002419 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
2421 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002422 * Be careful: If autocommands delete the window or cause this window
2423 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 */
2425 if (wp->w_buffer != curbuf)
2426 {
2427 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002428 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002430 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002431 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002432 win->w_closing = FALSE;
2433 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002434 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002436 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002438 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002439 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002440 win->w_closing = FALSE;
2441 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002442 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002443#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 /* autocmds may abort script processing */
2445 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002446 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002450#ifdef FEAT_GUI
Bram Moolenaar647e24b2019-03-17 16:39:46 +01002451 // Avoid trouble with scrollbars that are going to be deleted in
2452 // win_free().
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002453 if (gui.in_use)
2454 out_flush();
2455#endif
2456
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002457 win_close_buffer(win, free_buf, TRUE);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002458
Bram Moolenaar802418d2013-01-17 14:00:11 +01002459 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2460 && (last_window() || curtab != prev_curtab
2461 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002462 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002463 /* Autocommands have closed all windows, quit now. Restore
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002464 * curwin->w_buffer, otherwise writing viminfo may fail. */
2465 if (curwin->w_buffer == NULL)
2466 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002467 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002468 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002469
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002470 /* Autocommands may have moved to another tab page. */
2471 if (curtab != prev_curtab && win_valid_any_tab(win)
2472 && win->w_buffer == NULL)
2473 {
2474 /* Need to close the window anyway, since the buffer is NULL. */
2475 win_close_othertab(win, FALSE, prev_curtab);
2476 return FAIL;
2477 }
2478
2479 /* Autocommands may have closed the window already or closed the only
2480 * other window. */
2481 if (!win_valid(win) || last_window()
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002482 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002483 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
Bram Moolenaara971b822011-09-14 14:43:25 +02002485 /* Free the memory used for the window and get the window that received
2486 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002487 wp = win_free_mem(win, &dir, NULL);
2488
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 /* Make sure curwin isn't invalid. It can cause severe trouble when
2490 * printing an error message. For win_equal() curbuf needs to be valid
2491 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002492 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
2494 curwin = wp;
2495#ifdef FEAT_QUICKFIX
2496 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2497 {
2498 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002499 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 * finding another window to go to.
2501 */
2502 for (;;)
2503 {
2504 if (wp->w_next == NULL)
2505 wp = firstwin;
2506 else
2507 wp = wp->w_next;
2508 if (wp == curwin)
2509 break;
2510 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2511 {
2512 curwin = wp;
2513 break;
2514 }
2515 }
2516 }
2517#endif
2518 curbuf = curwin->w_buffer;
2519 close_curwin = TRUE;
Bram Moolenaarf79225e2017-03-18 23:11:04 +01002520
2521 /* The cursor position may be invalid if the buffer changed after last
2522 * using the window. */
2523 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002525 if (p_ea && (*p_ead == 'b' || *p_ead == dir))
Bram Moolenaar8eeeba82017-06-25 22:45:39 +02002526 /* If the frame of the closed window contains the new current window,
2527 * only resize that frame. Otherwise resize all windows. */
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002528 win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 else
2530 win_comp_pos();
2531 if (close_curwin)
2532 {
Bram Moolenaarc917da42016-07-19 22:31:36 +02002533 win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 if (other_buffer)
2535 /* careful: after this wp and win may be invalid! */
2536 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 }
2538
2539 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002540 * If last window has a status line now and we don't want one,
2541 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 */
2543 last_status(FALSE);
2544
2545 /* After closing the help window, try restoring the window layout from
2546 * before it was opened. */
2547 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002548 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002550#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2552 if (gui.in_use && !win_hasvertsplit())
2553 gui_init_which_components(NULL);
2554#endif
2555
2556 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002557 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558}
2559
2560/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002561 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002562 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002563 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002564 * Caller must check if buffer is hidden and whether the tabline needs to be
2565 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002566 */
2567 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002568win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002569{
2570 win_T *wp;
2571 int dir;
2572 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002573 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002574
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002575 /* Get here with win->w_buffer == NULL when win_close() detects the tab
2576 * page changed. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002577 if (win->w_closing || (win->w_buffer != NULL
2578 && win->w_buffer->b_locked > 0))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002579 return; /* window is already being closed */
Bram Moolenaar362ce482012-06-06 19:02:45 +02002580
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002581 if (win->w_buffer != NULL)
2582 /* Close the link to the buffer. */
2583 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002584
2585 /* Careful: Autocommands may have closed the tab page or made it the
2586 * current tab page. */
2587 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2588 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002589 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002590 return;
2591
2592 /* Autocommands may have closed the window already. */
2593 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2594 ;
2595 if (wp == NULL)
2596 return;
2597
Bram Moolenaarf740b292006-02-16 22:11:02 +00002598 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002599 if (tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002600 {
2601 if (tp == first_tabpage)
2602 first_tabpage = tp->tp_next;
2603 else
2604 {
2605 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2606 ptp = ptp->tp_next)
2607 ;
2608 if (ptp == NULL)
2609 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002610 internal_error("win_close_othertab()");
Bram Moolenaarf740b292006-02-16 22:11:02 +00002611 return;
2612 }
2613 ptp->tp_next = tp->tp_next;
2614 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002615 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002616 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002617
2618 /* Free the memory used for the window. */
2619 win_free_mem(win, &dir, tp);
2620
2621 if (free_tp)
2622 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002623}
2624
2625/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002626 * Free the memory used for a window.
2627 * Returns a pointer to the window that got the freed up space.
2628 */
2629 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002630win_free_mem(
2631 win_T *win,
2632 int *dirp, /* set to 'v' or 'h' for direction if 'ea' */
2633 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002634{
2635 frame_T *frp;
2636 win_T *wp;
2637
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002638 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002639 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002640 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002641 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002642 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002643
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002644 /* When deleting the current window of another tab page select a new
2645 * current window. */
2646 if (tp != NULL && win == tp->tp_curwin)
2647 tp->tp_curwin = wp;
2648
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002649 return wp;
2650}
2651
2652#if defined(EXITFREE) || defined(PROTO)
2653 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002654win_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002655{
2656 int dummy;
2657
Bram Moolenaarf740b292006-02-16 22:11:02 +00002658 while (first_tabpage->tp_next != NULL)
2659 tabpage_close(TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002660
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002661 if (aucmd_win != NULL)
2662 {
2663 (void)win_free_mem(aucmd_win, &dummy, NULL);
2664 aucmd_win = NULL;
2665 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002666# ifdef FEAT_TEXT_PROP
2667 close_all_popups();
2668# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002669
2670 while (firstwin != NULL)
2671 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002672
2673 /* No window should be used after this. Set curwin to NULL to crash
2674 * instead of using freed memory. */
2675 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002676}
2677#endif
2678
2679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 * Remove a window and its frame from the tree of frames.
2681 * Returns a pointer to the window that got the freed up space.
2682 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002683 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002684winframe_remove(
2685 win_T *win,
2686 int *dirp UNUSED, /* set to 'v' or 'h' for direction if 'ea' */
2687 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
2689 frame_T *frp, *frp2, *frp3;
2690 frame_T *frp_close = win->w_frame;
2691 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692
2693 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002694 * If there is only one window there is nothing to remove.
2695 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002696 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002697 return NULL;
2698
2699 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 * Remove the window from its frame.
2701 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002702 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 wp = frame2win(frp2);
2704
2705 /* Remove this frame from the list of frames. */
2706 frame_remove(frp_close);
2707
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (frp_close->fr_parent->fr_layout == FR_COL)
2709 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002710 /* When 'winfixheight' is set, try to find another frame in the column
2711 * (as close to the closed frame as possible) to distribute the height
2712 * to. */
2713 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2714 {
2715 frp = frp_close->fr_prev;
2716 frp3 = frp_close->fr_next;
2717 while (frp != NULL || frp3 != NULL)
2718 {
2719 if (frp != NULL)
2720 {
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002721 if (!frame_fixed_height(frp))
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002722 {
2723 frp2 = frp;
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002724 wp = frame2win(frp2);
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002725 break;
2726 }
2727 frp = frp->fr_prev;
2728 }
2729 if (frp3 != NULL)
2730 {
2731 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2732 {
2733 frp2 = frp3;
2734 wp = frp3->fr_win;
2735 break;
2736 }
2737 frp3 = frp3->fr_next;
2738 }
2739 }
2740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2742 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 *dirp = 'v';
2744 }
2745 else
2746 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002747 /* When 'winfixwidth' is set, try to find another frame in the column
2748 * (as close to the closed frame as possible) to distribute the width
2749 * to. */
2750 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2751 {
2752 frp = frp_close->fr_prev;
2753 frp3 = frp_close->fr_next;
2754 while (frp != NULL || frp3 != NULL)
2755 {
2756 if (frp != NULL)
2757 {
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002758 if (!frame_fixed_width(frp))
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002759 {
2760 frp2 = frp;
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002761 wp = frame2win(frp2);
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002762 break;
2763 }
2764 frp = frp->fr_prev;
2765 }
2766 if (frp3 != NULL)
2767 {
2768 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2769 {
2770 frp2 = frp3;
2771 wp = frp3->fr_win;
2772 break;
2773 }
2774 frp3 = frp3->fr_next;
2775 }
2776 }
2777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002779 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 *dirp = 'h';
2781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
2783 /* If rows/columns go to a window below/right its positions need to be
2784 * updated. Can only be done after the sizes have been updated. */
2785 if (frp2 == frp_close->fr_next)
2786 {
2787 int row = win->w_winrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02002788 int col = win->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789
2790 frame_comp_pos(frp2, &row, &col);
2791 }
2792
2793 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2794 {
2795 /* There is no other frame in this list, move its info to the parent
2796 * and remove it. */
2797 frp2->fr_parent->fr_layout = frp2->fr_layout;
2798 frp2->fr_parent->fr_child = frp2->fr_child;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002799 FOR_ALL_FRAMES(frp, frp2->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 frp->fr_parent = frp2->fr_parent;
2801 frp2->fr_parent->fr_win = frp2->fr_win;
2802 if (frp2->fr_win != NULL)
2803 frp2->fr_win->w_frame = frp2->fr_parent;
2804 frp = frp2->fr_parent;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002805 if (topframe->fr_child == frp2)
2806 topframe->fr_child = frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 vim_free(frp2);
2808
2809 frp2 = frp->fr_parent;
2810 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2811 {
2812 /* The frame above the parent has the same layout, have to merge
2813 * the frames into this list. */
2814 if (frp2->fr_child == frp)
2815 frp2->fr_child = frp->fr_child;
2816 frp->fr_child->fr_prev = frp->fr_prev;
2817 if (frp->fr_prev != NULL)
2818 frp->fr_prev->fr_next = frp->fr_child;
2819 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2820 {
2821 frp3->fr_parent = frp2;
2822 if (frp3->fr_next == NULL)
2823 {
2824 frp3->fr_next = frp->fr_next;
2825 if (frp->fr_next != NULL)
2826 frp->fr_next->fr_prev = frp3;
2827 break;
2828 }
2829 }
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002830 if (topframe->fr_child == frp)
2831 topframe->fr_child = frp2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 vim_free(frp);
2833 }
2834 }
2835
2836 return wp;
2837}
2838
2839/*
Bram Moolenaarc136af22018-05-04 20:15:38 +02002840 * Return a pointer to the frame that will receive the empty screen space that
2841 * is left over after "win" is closed.
2842 *
2843 * If 'splitbelow' or 'splitright' is set, the space goes above or to the left
2844 * by default. Otherwise, the free space goes below or to the right. The
2845 * result is that opening a window and then immediately closing it will
2846 * preserve the initial window layout. The 'wfh' and 'wfw' settings are
2847 * respected when possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 */
2849 static frame_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002850win_altframe(
2851 win_T *win,
2852 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853{
2854 frame_T *frp;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002855 frame_T *other_fr, *target_fr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002857 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002858 return alt_tabpage()->tp_curwin->w_frame;
2859
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 frp = win->w_frame;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002861
2862 if (frp->fr_prev == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 return frp->fr_next;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002864 if (frp->fr_next == NULL)
2865 return frp->fr_prev;
2866
2867 target_fr = frp->fr_next;
2868 other_fr = frp->fr_prev;
2869 if (p_spr || p_sb)
2870 {
2871 target_fr = frp->fr_prev;
2872 other_fr = frp->fr_next;
2873 }
2874
2875 /* If 'wfh' or 'wfw' is set for the target and not for the alternate
2876 * window, reverse the selection. */
2877 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2878 {
2879 if (frame_fixed_width(target_fr) && !frame_fixed_width(other_fr))
2880 target_fr = other_fr;
2881 }
2882 else
2883 {
2884 if (frame_fixed_height(target_fr) && !frame_fixed_height(other_fr))
2885 target_fr = other_fr;
2886 }
2887
2888 return target_fr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889}
2890
2891/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002892 * Return the tabpage that will be used if the current one is closed.
2893 */
2894 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002895alt_tabpage(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002896{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002897 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002898
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002899 /* Use the next tab page if possible. */
2900 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002901 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002902
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002903 /* Find the last but one tab page. */
2904 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2905 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002906 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002907}
2908
2909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 * Find the left-upper window in frame "frp".
2911 */
2912 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002913frame2win(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914{
2915 while (frp->fr_win == NULL)
2916 frp = frp->fr_child;
2917 return frp->fr_win;
2918}
2919
2920/*
2921 * Return TRUE if frame "frp" contains window "wp".
2922 */
2923 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002924frame_has_win(frame_T *frp, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925{
2926 frame_T *p;
2927
2928 if (frp->fr_layout == FR_LEAF)
2929 return frp->fr_win == wp;
2930
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002931 FOR_ALL_FRAMES(p, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 if (frame_has_win(p, wp))
2933 return TRUE;
2934 return FALSE;
2935}
2936
2937/*
2938 * Set a new height for a frame. Recursively sets the height for contained
2939 * frames and windows. Caller must take care of positions.
2940 */
2941 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002942frame_new_height(
2943 frame_T *topfrp,
2944 int height,
2945 int topfirst, /* resize topmost contained frame first */
2946 int wfh) /* obey 'winfixheight' when there is a choice;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 may cause the height not to be set */
2948{
2949 frame_T *frp;
2950 int extra_lines;
2951 int h;
2952
2953 if (topfrp->fr_win != NULL)
2954 {
2955 /* Simple case: just one window. */
2956 win_new_height(topfrp->fr_win,
Bram Moolenaard326ad62017-09-18 20:31:41 +02002957 height - topfrp->fr_win->w_status_height
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01002958 - WINBAR_HEIGHT(topfrp->fr_win));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 else if (topfrp->fr_layout == FR_ROW)
2961 {
2962 do
2963 {
2964 /* All frames in this row get the same new height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002965 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 {
2967 frame_new_height(frp, height, topfirst, wfh);
2968 if (frp->fr_height > height)
2969 {
2970 /* Could not fit the windows, make the whole row higher. */
2971 height = frp->fr_height;
2972 break;
2973 }
2974 }
2975 }
2976 while (frp != NULL);
2977 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002978 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 {
2980 /* Complicated case: Resize a column of frames. Resize the bottom
2981 * frame first, frames above that when needed. */
2982
2983 frp = topfrp->fr_child;
2984 if (wfh)
2985 /* Advance past frames with one window with 'wfh' set. */
2986 while (frame_fixed_height(frp))
2987 {
2988 frp = frp->fr_next;
2989 if (frp == NULL)
2990 return; /* no frame without 'wfh', give up */
2991 }
2992 if (!topfirst)
2993 {
2994 /* Find the bottom frame of this column */
2995 while (frp->fr_next != NULL)
2996 frp = frp->fr_next;
2997 if (wfh)
2998 /* Advance back for frames with one window with 'wfh' set. */
2999 while (frame_fixed_height(frp))
3000 frp = frp->fr_prev;
3001 }
3002
3003 extra_lines = height - topfrp->fr_height;
3004 if (extra_lines < 0)
3005 {
3006 /* reduce height of contained frames, bottom or top frame first */
3007 while (frp != NULL)
3008 {
3009 h = frame_minheight(frp, NULL);
3010 if (frp->fr_height + extra_lines < h)
3011 {
3012 extra_lines += frp->fr_height - h;
3013 frame_new_height(frp, h, topfirst, wfh);
3014 }
3015 else
3016 {
3017 frame_new_height(frp, frp->fr_height + extra_lines,
3018 topfirst, wfh);
3019 break;
3020 }
3021 if (topfirst)
3022 {
3023 do
3024 frp = frp->fr_next;
3025 while (wfh && frp != NULL && frame_fixed_height(frp));
3026 }
3027 else
3028 {
3029 do
3030 frp = frp->fr_prev;
3031 while (wfh && frp != NULL && frame_fixed_height(frp));
3032 }
3033 /* Increase "height" if we could not reduce enough frames. */
3034 if (frp == NULL)
3035 height -= extra_lines;
3036 }
3037 }
3038 else if (extra_lines > 0)
3039 {
3040 /* increase height of bottom or top frame */
3041 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
3042 }
3043 }
3044 topfrp->fr_height = height;
3045}
3046
3047/*
3048 * Return TRUE if height of frame "frp" should not be changed because of
3049 * the 'winfixheight' option.
3050 */
3051 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003052frame_fixed_height(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 /* frame with one window: fixed height if 'winfixheight' set. */
3055 if (frp->fr_win != NULL)
3056 return frp->fr_win->w_p_wfh;
3057
3058 if (frp->fr_layout == FR_ROW)
3059 {
3060 /* The frame is fixed height if one of the frames in the row is fixed
3061 * height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003062 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 if (frame_fixed_height(frp))
3064 return TRUE;
3065 return FALSE;
3066 }
3067
3068 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
3069 * frames in the row are fixed height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003070 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 if (!frame_fixed_height(frp))
3072 return FALSE;
3073 return TRUE;
3074}
3075
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003077 * Return TRUE if width of frame "frp" should not be changed because of
3078 * the 'winfixwidth' option.
3079 */
3080 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003081frame_fixed_width(frame_T *frp)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003082{
3083 /* frame with one window: fixed width if 'winfixwidth' set. */
3084 if (frp->fr_win != NULL)
3085 return frp->fr_win->w_p_wfw;
3086
3087 if (frp->fr_layout == FR_COL)
3088 {
3089 /* The frame is fixed width if one of the frames in the row is fixed
3090 * width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003091 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003092 if (frame_fixed_width(frp))
3093 return TRUE;
3094 return FALSE;
3095 }
3096
3097 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
3098 * frames in the row are fixed width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003099 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003100 if (!frame_fixed_width(frp))
3101 return FALSE;
3102 return TRUE;
3103}
3104
3105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 * Add a status line to windows at the bottom of "frp".
3107 * Note: Does not check if there is room!
3108 */
3109 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003110frame_add_statusline(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 win_T *wp;
3113
3114 if (frp->fr_layout == FR_LEAF)
3115 {
3116 wp = frp->fr_win;
3117 if (wp->w_status_height == 0)
3118 {
3119 if (wp->w_height > 0) /* don't make it negative */
3120 --wp->w_height;
3121 wp->w_status_height = STATUS_HEIGHT;
3122 }
3123 }
3124 else if (frp->fr_layout == FR_ROW)
3125 {
3126 /* Handle all the frames in the row. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003127 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 frame_add_statusline(frp);
3129 }
3130 else /* frp->fr_layout == FR_COL */
3131 {
3132 /* Only need to handle the last frame in the column. */
3133 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3134 ;
3135 frame_add_statusline(frp);
3136 }
3137}
3138
3139/*
3140 * Set width of a frame. Handles recursively going through contained frames.
3141 * May remove separator line for windows at the right side (for win_close()).
3142 */
3143 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003144frame_new_width(
3145 frame_T *topfrp,
3146 int width,
3147 int leftfirst, /* resize leftmost contained frame first */
3148 int wfw) /* obey 'winfixwidth' when there is a choice;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003149 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150{
3151 frame_T *frp;
3152 int extra_cols;
3153 int w;
3154 win_T *wp;
3155
3156 if (topfrp->fr_layout == FR_LEAF)
3157 {
3158 /* Simple case: just one window. */
3159 wp = topfrp->fr_win;
3160 /* Find out if there are any windows right of this one. */
3161 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3162 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3163 break;
3164 if (frp->fr_parent == NULL)
3165 wp->w_vsep_width = 0;
3166 win_new_width(wp, width - wp->w_vsep_width);
3167 }
3168 else if (topfrp->fr_layout == FR_COL)
3169 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003170 do
3171 {
3172 /* All frames in this column get the same new width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003173 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003174 {
3175 frame_new_width(frp, width, leftfirst, wfw);
3176 if (frp->fr_width > width)
3177 {
3178 /* Could not fit the windows, make whole column wider. */
3179 width = frp->fr_width;
3180 break;
3181 }
3182 }
3183 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 }
3185 else /* fr_layout == FR_ROW */
3186 {
3187 /* Complicated case: Resize a row of frames. Resize the rightmost
3188 * frame first, frames left of it when needed. */
3189
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003191 if (wfw)
3192 /* Advance past frames with one window with 'wfw' set. */
3193 while (frame_fixed_width(frp))
3194 {
3195 frp = frp->fr_next;
3196 if (frp == NULL)
3197 return; /* no frame without 'wfw', give up */
3198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003200 {
3201 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 while (frp->fr_next != NULL)
3203 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003204 if (wfw)
3205 /* Advance back for frames with one window with 'wfw' set. */
3206 while (frame_fixed_width(frp))
3207 frp = frp->fr_prev;
3208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209
3210 extra_cols = width - topfrp->fr_width;
3211 if (extra_cols < 0)
3212 {
3213 /* reduce frame width, rightmost frame first */
3214 while (frp != NULL)
3215 {
3216 w = frame_minwidth(frp, NULL);
3217 if (frp->fr_width + extra_cols < w)
3218 {
3219 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003220 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 }
3222 else
3223 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003224 frame_new_width(frp, frp->fr_width + extra_cols,
3225 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 break;
3227 }
3228 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003229 {
3230 do
3231 frp = frp->fr_next;
3232 while (wfw && frp != NULL && frame_fixed_width(frp));
3233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003235 {
3236 do
3237 frp = frp->fr_prev;
3238 while (wfw && frp != NULL && frame_fixed_width(frp));
3239 }
3240 /* Increase "width" if we could not reduce enough frames. */
3241 if (frp == NULL)
3242 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 }
3244 }
3245 else if (extra_cols > 0)
3246 {
3247 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003248 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 }
3250 }
3251 topfrp->fr_width = width;
3252}
3253
3254/*
3255 * Add the vertical separator to windows at the right side of "frp".
3256 * Note: Does not check if there is room!
3257 */
3258 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003259frame_add_vsep(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260{
3261 win_T *wp;
3262
3263 if (frp->fr_layout == FR_LEAF)
3264 {
3265 wp = frp->fr_win;
3266 if (wp->w_vsep_width == 0)
3267 {
3268 if (wp->w_width > 0) /* don't make it negative */
3269 --wp->w_width;
3270 wp->w_vsep_width = 1;
3271 }
3272 }
3273 else if (frp->fr_layout == FR_COL)
3274 {
3275 /* Handle all the frames in the column. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003276 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 frame_add_vsep(frp);
3278 }
3279 else /* frp->fr_layout == FR_ROW */
3280 {
3281 /* Only need to handle the last frame in the row. */
3282 frp = frp->fr_child;
3283 while (frp->fr_next != NULL)
3284 frp = frp->fr_next;
3285 frame_add_vsep(frp);
3286 }
3287}
3288
3289/*
3290 * Set frame width from the window it contains.
3291 */
3292 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003293frame_fix_width(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
3295 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3296}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297
3298/*
3299 * Set frame height from the window it contains.
3300 */
3301 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003302frame_fix_height(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303{
Bram Moolenaar415a6932017-12-05 20:31:07 +01003304 wp->w_frame->fr_height = VISIBLE_HEIGHT(wp) + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305}
3306
3307/*
3308 * Compute the minimal height for frame "topfrp".
3309 * Uses the 'winminheight' option.
3310 * When "next_curwin" isn't NULL, use p_wh for this window.
3311 * When "next_curwin" is NOWIN, don't use at least one line for the current
3312 * window.
3313 */
3314 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003315frame_minheight(frame_T *topfrp, win_T *next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 frame_T *frp;
3318 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
3321 if (topfrp->fr_win != NULL)
3322 {
3323 if (topfrp->fr_win == next_curwin)
3324 m = p_wh + topfrp->fr_win->w_status_height;
3325 else
3326 {
3327 /* window: minimal height of the window plus status line */
3328 m = p_wmh + topfrp->fr_win->w_status_height;
Bram Moolenaar415a6932017-12-05 20:31:07 +01003329 if (topfrp->fr_win == curwin && next_curwin == NULL)
3330 {
3331 /* Current window is minimal one line high and WinBar is
3332 * visible. */
3333 if (p_wmh == 0)
3334 ++m;
3335 m += WINBAR_HEIGHT(curwin);
3336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
3338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 else if (topfrp->fr_layout == FR_ROW)
3340 {
3341 /* get the minimal height from each frame in this row */
3342 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003343 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 {
3345 n = frame_minheight(frp, next_curwin);
3346 if (n > m)
3347 m = n;
3348 }
3349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 else
3351 {
3352 /* Add up the minimal heights for all frames in this column. */
3353 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003354 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 m += frame_minheight(frp, next_curwin);
3356 }
3357
3358 return m;
3359}
3360
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361/*
3362 * Compute the minimal width for frame "topfrp".
3363 * When "next_curwin" isn't NULL, use p_wiw for this window.
3364 * When "next_curwin" is NOWIN, don't use at least one column for the current
3365 * window.
3366 */
3367 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003368frame_minwidth(
3369 frame_T *topfrp,
3370 win_T *next_curwin) /* use p_wh and p_wiw for next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371{
3372 frame_T *frp;
3373 int m, n;
3374
3375 if (topfrp->fr_win != NULL)
3376 {
3377 if (topfrp->fr_win == next_curwin)
3378 m = p_wiw + topfrp->fr_win->w_vsep_width;
3379 else
3380 {
3381 /* window: minimal width of the window plus separator column */
3382 m = p_wmw + topfrp->fr_win->w_vsep_width;
3383 /* Current window is minimal one column wide */
3384 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3385 ++m;
3386 }
3387 }
3388 else if (topfrp->fr_layout == FR_COL)
3389 {
3390 /* get the minimal width from each frame in this column */
3391 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003392 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 {
3394 n = frame_minwidth(frp, next_curwin);
3395 if (n > m)
3396 m = n;
3397 }
3398 }
3399 else
3400 {
3401 /* Add up the minimal widths for all frames in this row. */
3402 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003403 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 m += frame_minwidth(frp, next_curwin);
3405 }
3406
3407 return m;
3408}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
3410
3411/*
3412 * Try to close all windows except current one.
3413 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3414 * used and the buffer was modified.
3415 *
3416 * Used by ":bdel" and ":only".
3417 */
3418 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003419close_others(
3420 int message,
3421 int forceit) /* always hide all other windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
3423 win_T *wp;
3424 win_T *nextwp;
3425 int r;
3426
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003427 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003429 if (message && !autocmd_busy)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003430 msg(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 return;
3432 }
3433
3434 /* Be very careful here: autocommands may change the window layout. */
3435 for (wp = firstwin; win_valid(wp); wp = nextwp)
3436 {
3437 nextwp = wp->w_next;
3438 if (wp != curwin) /* don't close current window */
3439 {
3440
3441 /* Check if it's allowed to abandon this window */
3442 r = can_abandon(wp->w_buffer, forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 if (!win_valid(wp)) /* autocommands messed wp up */
3444 {
3445 nextwp = firstwin;
3446 continue;
3447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 if (!r)
3449 {
3450#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3451 if (message && (p_confirm || cmdmod.confirm) && p_write)
3452 {
3453 dialog_changed(wp->w_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 if (!win_valid(wp)) /* autocommands messed wp up */
3455 {
3456 nextwp = firstwin;
3457 continue;
3458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 }
3460 if (bufIsChanged(wp->w_buffer))
3461#endif
3462 continue;
3463 }
Bram Moolenaareb44a682017-08-03 22:44:55 +02003464 win_close(wp, !buf_hide(wp->w_buffer)
3465 && !bufIsChanged(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 }
3467 }
3468
Bram Moolenaar459ca562016-11-10 18:16:33 +01003469 if (message && !ONE_WINDOW)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003470 emsg(_("E445: Other window contains changes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471}
3472
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003474 * Init the current window "curwin".
3475 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 */
3477 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003478curwin_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003480 win_init_empty(curwin);
3481}
3482
3483 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003484win_init_empty(win_T *wp)
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003485{
3486 redraw_win_later(wp, NOT_VALID);
3487 wp->w_lines_valid = 0;
3488 wp->w_cursor.lnum = 1;
3489 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003490 wp->w_cursor.coladd = 0;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003491 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3492 wp->w_pcmark.col = 0;
3493 wp->w_prev_pcmark.lnum = 0;
3494 wp->w_prev_pcmark.col = 0;
3495 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003497 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003499 wp->w_botline = 2;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003500#if defined(FEAT_SYN_HL) || defined(FEAT_SPELL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003501 wp->w_s = &wp->w_buffer->b_s;
3502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503}
3504
3505/*
3506 * Allocate the first window and put an empty buffer in it.
3507 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003508 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003510 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003511win_alloc_first(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003513 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003514 return FAIL;
3515
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003516 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003517 if (first_tabpage == NULL)
3518 return FAIL;
3519 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003520 curtab = first_tabpage;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003521
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003522 return OK;
3523}
3524
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003525/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003526 * Allocate and init a window that is not a regular window.
3527 * This can only be done after the first window is fully initialized, thus it
3528 * can't be in win_alloc_first().
3529 */
3530 win_T *
3531win_alloc_popup_win(void)
3532{
3533 win_T *wp;
3534
3535 wp = win_alloc(NULL, TRUE);
3536 if (wp != NULL)
3537 {
3538 // We need to initialize options with something, using the current
3539 // window makes most sense.
3540 win_init_some(wp, curwin);
3541
3542 RESET_BINDING(wp);
3543 new_frame(wp);
3544 }
3545 return wp;
3546}
3547
3548/*
3549 * Initialize window "wp" to display buffer "buf".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003550 */
3551 void
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003552win_init_popup_win(win_T *wp, buf_T *buf)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003553{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003554 wp->w_buffer = buf;
3555 ++buf->b_nwindows;
3556 win_init_empty(wp); // set cursor and topline to safe values
3557
3558 // Make sure w_localdir and globaldir are NULL to avoid a chdir() in
3559 // win_enter_ext().
3560 VIM_CLEAR(wp->w_localdir);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003561}
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003562
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003563/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003564 * Allocate the first window or the first window in a new tab page.
3565 * When "oldwin" is NULL create an empty buffer for it.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003566 * When "oldwin" is not NULL copy info from it to the new window.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003567 * Return FAIL when something goes wrong (out of memory).
3568 */
3569 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003570win_alloc_firstwin(win_T *oldwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003571{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003572 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003573 if (oldwin == NULL)
3574 {
3575 /* Very first window, need to create an empty buffer for it and
3576 * initialize from scratch. */
3577 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3578 if (curwin == NULL || curbuf == NULL)
3579 return FAIL;
3580 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003581#ifdef FEAT_SYN_HL
3582 curwin->w_s = &(curbuf->b_s);
3583#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003584 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003585 curwin->w_alist = &global_alist;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003586 curwin_init(); /* init current window */
3587 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003588 else
3589 {
3590 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003591 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003592
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003593 /* We don't want cursor- and scroll-binding in the first window. */
3594 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003597 new_frame(curwin);
3598 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003599 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003600 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 topframe->fr_height = Rows - p_ch;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003603
3604 return OK;
3605}
3606
3607/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003608 * Create a frame for window "wp".
3609 */
3610 static void
3611new_frame(win_T *wp)
3612{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003613 frame_T *frp = ALLOC_CLEAR_ONE(frame_T);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003614
3615 wp->w_frame = frp;
3616 if (frp != NULL)
3617 {
3618 frp->fr_layout = FR_LEAF;
3619 frp->fr_win = wp;
3620 }
3621}
3622
3623/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003624 * Initialize the window and frame size to the maximum.
3625 */
3626 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003627win_init_size(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003628{
3629 firstwin->w_height = ROWS_AVAIL;
3630 topframe->fr_height = ROWS_AVAIL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003631 firstwin->w_width = Columns;
3632 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633}
3634
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003635/*
3636 * Allocate a new tabpage_T and init the values.
3637 * Returns NULL when out of memory.
3638 */
3639 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003640alloc_tabpage(void)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003641{
3642 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003643# ifdef FEAT_GUI
3644 int i;
3645# endif
3646
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003647
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003648 tp = ALLOC_CLEAR_ONE(tabpage_T);
Bram Moolenaar429fa852013-04-15 12:27:36 +02003649 if (tp == NULL)
3650 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003651
Bram Moolenaar429fa852013-04-15 12:27:36 +02003652# ifdef FEAT_EVAL
3653 /* init t: variables */
3654 tp->tp_vars = dict_alloc();
3655 if (tp->tp_vars == NULL)
3656 {
3657 vim_free(tp);
3658 return NULL;
3659 }
3660 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3661# endif
3662
3663# ifdef FEAT_GUI
3664 for (i = 0; i < 3; i++)
3665 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003666# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003667# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003668 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003669# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003670 tp->tp_ch_used = p_ch;
3671
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003672 return tp;
3673}
3674
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003675 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003676free_tabpage(tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003677{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003678 int idx;
3679
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003680# ifdef FEAT_DIFF
3681 diff_clear(tp);
3682# endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003683# ifdef FEAT_TEXT_PROP
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02003684 while (tp->tp_first_popupwin != NULL)
3685 popup_close_tabpage(tp, tp->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003686#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003687 for (idx = 0; idx < SNAP_COUNT; ++idx)
3688 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003689#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003690 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3691 hash_init(&tp->tp_vars->dv_hashtab);
3692 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003693#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003694
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003695 vim_free(tp->tp_localdir);
3696
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003697#ifdef FEAT_PYTHON
3698 python_tabpage_free(tp);
3699#endif
3700
3701#ifdef FEAT_PYTHON3
3702 python3_tabpage_free(tp);
3703#endif
3704
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003705 vim_free(tp);
3706}
3707
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003708/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003709 * Create a new Tab page with one window.
3710 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003711 * When "after" is 0 put it just after the current Tab page.
3712 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003713 * Return FAIL or OK.
3714 */
3715 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003716win_new_tabpage(int after)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003717{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003718 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003719 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003720 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003721
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003722 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003723 if (newtp == NULL)
3724 return FAIL;
3725
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003726 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003727 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003728 {
3729 vim_free(newtp);
3730 return FAIL;
3731 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003732 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003733
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003734 newtp->tp_localdir = (tp->tp_localdir == NULL)
3735 ? NULL : vim_strsave(tp->tp_localdir);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003736 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003737 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003738 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003739 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003740 if (after == 1)
3741 {
3742 /* New tab page becomes the first one. */
3743 newtp->tp_next = first_tabpage;
3744 first_tabpage = newtp;
3745 }
3746 else
3747 {
3748 if (after > 0)
3749 {
3750 /* Put new tab page before tab page "after". */
3751 n = 2;
3752 for (tp = first_tabpage; tp->tp_next != NULL
3753 && n < after; tp = tp->tp_next)
3754 ++n;
3755 }
3756 newtp->tp_next = tp->tp_next;
3757 tp->tp_next = newtp;
3758 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003759 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003760 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003761 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003762
3763 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003764 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003765
3766#if defined(FEAT_GUI)
3767 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3768 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003769 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003770#endif
Bram Moolenaar6d41c782018-06-06 09:11:12 +02003771#ifdef FEAT_JOB_CHANNEL
3772 entering_window(curwin);
3773#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003774
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01003775 redraw_all_later(NOT_VALID);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003776 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003777 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003778 apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003779 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003780 return OK;
3781 }
3782
3783 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003784 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003785 return FAIL;
3786}
3787
3788/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003789 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3790 * like with ":split".
3791 * Returns OK if a new tab page was created, FAIL otherwise.
3792 */
3793 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003794may_open_tabpage(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003795{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003796 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003797
Bram Moolenaard326ce82007-03-11 14:48:29 +00003798 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003799 {
3800 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003801 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003802 return win_new_tabpage(n);
3803 }
3804 return FAIL;
3805}
3806
3807/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003808 * Create up to "maxcount" tabpages with empty windows.
3809 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003810 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003811 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003812make_tabpages(int maxcount)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003813{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003814 int count = maxcount;
3815 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003816
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003817 /* Limit to 'tabpagemax' tabs. */
3818 if (count > p_tpm)
3819 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003820
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003821 /*
3822 * Don't execute autocommands while creating the tab pages. Must do that
3823 * when putting the buffers in the windows.
3824 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003825 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003826
3827 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003828 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003829 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003830
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003831 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003832
3833 /* return actual number of tab pages */
3834 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003835}
3836
3837/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003838 * Return TRUE when "tpc" points to a valid tab page.
3839 */
3840 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003841valid_tabpage(tabpage_T *tpc)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003842{
3843 tabpage_T *tp;
3844
Bram Moolenaar29323592016-07-24 22:04:11 +02003845 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003846 if (tp == tpc)
3847 return TRUE;
3848 return FALSE;
3849}
3850
3851/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003852 * Return TRUE when "tpc" points to a valid tab page and at least one window is
3853 * valid.
3854 */
3855 int
3856valid_tabpage_win(tabpage_T *tpc)
3857{
3858 tabpage_T *tp;
3859 win_T *wp;
3860
3861 FOR_ALL_TABPAGES(tp)
3862 {
3863 if (tp == tpc)
3864 {
3865 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
3866 {
3867 if (win_valid_any_tab(wp))
3868 return TRUE;
3869 }
3870 return FALSE;
3871 }
3872 }
3873 /* shouldn't happen */
3874 return FALSE;
3875}
3876
3877/*
3878 * Close tabpage "tab", assuming it has no windows in it.
3879 * There must be another tabpage or this will crash.
3880 */
3881 void
3882close_tabpage(tabpage_T *tab)
3883{
3884 tabpage_T *ptp;
3885
3886 if (tab == first_tabpage)
3887 {
3888 first_tabpage = tab->tp_next;
3889 ptp = first_tabpage;
3890 }
3891 else
3892 {
3893 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tab;
3894 ptp = ptp->tp_next)
3895 ;
Bram Moolenaara37ffaa2017-03-21 21:58:00 +01003896 assert(ptp != NULL);
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003897 ptp->tp_next = tab->tp_next;
3898 }
3899
3900 goto_tabpage_tp(ptp, FALSE, FALSE);
3901 free_tabpage(tab);
3902}
3903
3904/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003905 * Find tab page "n" (first one is 1). Returns NULL when not found.
3906 */
3907 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003908find_tabpage(int n)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003909{
3910 tabpage_T *tp;
3911 int i = 1;
3912
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003913 if (n == 0)
3914 return curtab;
3915
Bram Moolenaarf740b292006-02-16 22:11:02 +00003916 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3917 ++i;
3918 return tp;
3919}
3920
3921/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003922 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003923 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003924 */
3925 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003926tabpage_index(tabpage_T *ftp)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003927{
3928 int i = 1;
3929 tabpage_T *tp;
3930
3931 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3932 ++i;
3933 return i;
3934}
3935
3936/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003937 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003938 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003939 * FAIL.
3940 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003941 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003942 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003943leave_tabpage(
3944 buf_T *new_curbuf UNUSED, /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003945 NULL if unknown */
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003946 int trigger_leave_autocmds UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003947{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003948 tabpage_T *tp = curtab;
3949
Bram Moolenaar6d41c782018-06-06 09:11:12 +02003950#ifdef FEAT_JOB_CHANNEL
3951 leaving_window(curwin);
3952#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003953 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003954 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003955 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003956 if (new_curbuf != curbuf)
3957 {
3958 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3959 if (curtab != tp)
3960 return FAIL;
3961 }
3962 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3963 if (curtab != tp)
3964 return FAIL;
3965 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003966 if (curtab != tp)
3967 return FAIL;
3968 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003969#if defined(FEAT_GUI)
3970 /* Remove the scrollbars. They may be added back later. */
3971 if (gui.in_use)
3972 gui_remove_scrollbars();
3973#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003974 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003975 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003976 tp->tp_firstwin = firstwin;
3977 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003978 tp->tp_old_Rows = Rows;
3979 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003980 firstwin = NULL;
3981 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003982 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003983}
3984
3985/*
3986 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003987 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003988 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3989 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003990 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003991 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003992enter_tabpage(
3993 tabpage_T *tp,
3994 buf_T *old_curbuf UNUSED,
Bram Moolenaarf1d25012016-03-03 12:22:53 +01003995 int trigger_enter_autocmds,
3996 int trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003997{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003998 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003999 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004000
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004001 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004002 firstwin = tp->tp_firstwin;
4003 lastwin = tp->tp_lastwin;
4004 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00004005
4006 /* We would like doing the TabEnter event first, but we don't have a
4007 * valid current window yet, which may break some commands.
4008 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaarc917da42016-07-19 22:31:36 +02004009 win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE,
Bram Moolenaard6949742013-06-16 14:18:28 +02004010 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00004011 prevwin = next_prevwin;
4012
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004013 last_status(FALSE); /* status line may appear or disappear */
4014 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004015#ifdef FEAT_DIFF
4016 diff_need_scrollbind = TRUE;
4017#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004018
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004019 /* The tabpage line may have appeared or disappeared, may need to resize
4020 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004021 * frame sizes too. Use the stored value of p_ch, so that it can be
4022 * different for each tab page. */
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +02004023 if (p_ch != curtab->tp_ch_used)
4024 clear_cmdline = TRUE;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004025 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004026 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
4027#ifdef FEAT_GUI_TABLINE
4028 && !gui_use_tabline()
4029#endif
4030 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004031 shell_new_rows();
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004032 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004033 shell_new_columns(); /* update window widths */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004034
4035#if defined(FEAT_GUI)
4036 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
4037 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004038 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004039#endif
4040
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01004041 /* Apply autocommands after updating the display, when 'rows' and
4042 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004043 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02004044 {
4045 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
4046 if (old_curbuf != curbuf)
4047 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4048 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01004049
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01004050 redraw_all_later(NOT_VALID);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004051}
4052
4053/*
4054 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004055 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004056 */
4057 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004058goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004059{
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004060 tabpage_T *tp = NULL; // shut up compiler
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004061 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004062 int i;
4063
Bram Moolenaard68071d2006-05-02 22:08:30 +00004064 if (text_locked())
4065 {
4066 /* Not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02004067 text_locked_msg();
Bram Moolenaard68071d2006-05-02 22:08:30 +00004068 return;
4069 }
4070
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004071 /* If there is only one it can't work. */
4072 if (first_tabpage->tp_next == NULL)
4073 {
4074 if (n > 1)
4075 beep_flush();
4076 return;
4077 }
4078
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004079 if (n == 0)
4080 {
4081 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004082 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004083 tp = first_tabpage;
4084 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004085 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004086 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004087 else if (n < 0)
4088 {
4089 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
4090 * this N times. */
4091 ttp = curtab;
4092 for (i = n; i < 0; ++i)
4093 {
4094 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
4095 tp = tp->tp_next)
4096 ;
4097 ttp = tp;
4098 }
4099 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004100 else if (n == 9999)
4101 {
4102 /* Go to last tab page. */
4103 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
4104 ;
4105 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004106 else
4107 {
4108 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004109 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004110 if (tp == NULL)
4111 {
4112 beep_flush();
4113 return;
4114 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004115 }
4116
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004117 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004118
4119#ifdef FEAT_GUI_TABLINE
4120 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004121 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004122#endif
4123}
4124
4125/*
4126 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004127 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4128 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004129 * Note: doesn't update the GUI tab.
4130 */
4131 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004132goto_tabpage_tp(
4133 tabpage_T *tp,
4134 int trigger_enter_autocmds,
4135 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004136{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02004137 /* Don't repeat a message in another tab page. */
4138 set_keep_msg(NULL, 0);
4139
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004140 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4141 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004142 {
4143 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004144 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4145 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004146 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004147 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4148 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004149 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004150}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
4152/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004153 * Enter window "wp" in tab page "tp".
4154 * Also updates the GUI tab.
4155 */
4156 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004157goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004158{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004159 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004160 if (curtab == tp && win_valid(wp))
4161 {
4162 win_enter(wp, TRUE);
4163# ifdef FEAT_GUI_TABLINE
4164 if (gui_use_tabline())
4165 gui_mch_set_curtab(tabpage_index(curtab));
4166# endif
4167 }
4168}
4169
4170/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004171 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004172 */
4173 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004174tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004175{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004176 int n = 1;
4177 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004178
4179 if (first_tabpage->tp_next == NULL)
4180 return;
4181
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004182 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4183 ++n;
4184
4185 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4186 && tp->tp_next == curtab))
4187 return;
4188
4189 tp_dst = tp;
4190
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004191 /* Remove the current tab page from the list of tab pages. */
4192 if (curtab == first_tabpage)
4193 first_tabpage = curtab->tp_next;
4194 else
4195 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004196 FOR_ALL_TABPAGES(tp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004197 if (tp->tp_next == curtab)
4198 break;
4199 if (tp == NULL) /* "cannot happen" */
4200 return;
4201 tp->tp_next = curtab->tp_next;
4202 }
4203
4204 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004205 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004206 {
4207 curtab->tp_next = first_tabpage;
4208 first_tabpage = curtab;
4209 }
4210 else
4211 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004212 curtab->tp_next = tp_dst->tp_next;
4213 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004214 }
4215
4216 /* Need to redraw the tabline. Tab page contents doesn't change. */
4217 redraw_tabline = TRUE;
4218}
4219
4220
4221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 * Go to another window.
4223 * When jumping to another buffer, stop Visual mode. Do this before
4224 * changing windows so we can yank the selection into the '*' register.
4225 * When jumping to another window on the same buffer, adjust its cursor
4226 * position to keep the same Visual area.
4227 */
4228 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004229win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004231#ifdef FEAT_CONCEAL
4232 win_T *owp = curwin;
4233#endif
4234
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004235 if (NOT_IN_POPUP_WINDOW)
4236 return;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004237 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 {
4239 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004240 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 return;
4242 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004243 if (curbuf_locked())
4244 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004245
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 if (wp->w_buffer != curbuf)
4247 reset_VIsual_and_resel();
4248 else if (VIsual_active)
4249 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251#ifdef FEAT_GUI
4252 need_mouse_correct = TRUE;
4253#endif
4254 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004255
4256#ifdef FEAT_CONCEAL
Bram Moolenaar535d5b62019-01-11 20:45:36 +01004257 // Conceal cursor line in previous window, unconceal in current window.
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004258 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01004259 redrawWinline(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004260 if (curwin->w_p_cole > 0 && !msg_scrolled)
4261 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263}
4264
4265#if defined(FEAT_PERL) || defined(PROTO)
4266/*
4267 * Find window number "winnr" (counting top to bottom).
4268 */
4269 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004270win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271{
4272 win_T *wp;
4273
Bram Moolenaar29323592016-07-24 22:04:11 +02004274 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 if (--winnr == 0)
4276 break;
4277 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278}
4279#endif
4280
Bram Moolenaar4033c552017-09-16 20:54:51 +02004281#if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004282/*
4283 * Find the tabpage for window "win".
4284 */
4285 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004286win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004287{
4288 win_T *wp;
4289 tabpage_T *tp;
4290
Bram Moolenaar29323592016-07-24 22:04:11 +02004291 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004292 if (wp == win)
4293 return tp;
4294 return NULL;
4295}
4296#endif
4297
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298/*
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004299 * Get the above or below neighbor window of the specified window.
4300 * up - TRUE for the above neighbor
4301 * count - nth neighbor window
4302 * Returns the specified window if the neighbor is not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 */
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004304 win_T *
4305win_vert_neighbor(tabpage_T *tp, win_T *wp, int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306{
4307 frame_T *fr;
4308 frame_T *nfr;
4309 frame_T *foundfr;
4310
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004311 foundfr = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 while (count--)
4313 {
4314 /*
4315 * First go upwards in the tree of frames until we find a upwards or
4316 * downwards neighbor.
4317 */
4318 fr = foundfr;
4319 for (;;)
4320 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004321 if (fr == tp->tp_topframe)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 goto end;
4323 if (up)
4324 nfr = fr->fr_prev;
4325 else
4326 nfr = fr->fr_next;
4327 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4328 break;
4329 fr = fr->fr_parent;
4330 }
4331
4332 /*
4333 * Now go downwards to find the bottom or top frame in it.
4334 */
4335 for (;;)
4336 {
4337 if (nfr->fr_layout == FR_LEAF)
4338 {
4339 foundfr = nfr;
4340 break;
4341 }
4342 fr = nfr->fr_child;
4343 if (nfr->fr_layout == FR_ROW)
4344 {
4345 /* Find the frame at the cursor row. */
4346 while (fr->fr_next != NULL
4347 && frame2win(fr)->w_wincol + fr->fr_width
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004348 <= wp->w_wincol + wp->w_wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 fr = fr->fr_next;
4350 }
4351 if (nfr->fr_layout == FR_COL && up)
4352 while (fr->fr_next != NULL)
4353 fr = fr->fr_next;
4354 nfr = fr;
4355 }
4356 }
4357end:
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004358 return foundfr != NULL ? foundfr->fr_win : NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359}
4360
4361/*
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004362 * Move to window above or below "count" times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 */
4364 static void
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004365win_goto_ver(
4366 int up, // TRUE to go to win above
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004367 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368{
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004369 win_T *win;
4370
4371 win = win_vert_neighbor(curtab, curwin, up, count);
4372 if (win != NULL)
4373 win_goto(win);
4374}
4375
4376/*
4377 * Get the left or right neighbor window of the specified window.
4378 * left - TRUE for the left neighbor
4379 * count - nth neighbor window
4380 * Returns the specified window if the neighbor is not found.
4381 */
4382 win_T *
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02004383win_horz_neighbor(tabpage_T *tp, win_T *wp, int left, long count)
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004384{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 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 left or
4394 * right 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 (left)
4402 nfr = fr->fr_prev;
4403 else
4404 nfr = fr->fr_next;
4405 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4406 break;
4407 fr = fr->fr_parent;
4408 }
4409
4410 /*
4411 * Now go downwards to find the leftmost or rightmost 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_COL)
4422 {
4423 /* Find the frame at the cursor row. */
4424 while (fr->fr_next != NULL
4425 && frame2win(fr)->w_winrow + fr->fr_height
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004426 <= wp->w_winrow + wp->w_wrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 fr = fr->fr_next;
4428 }
4429 if (nfr->fr_layout == FR_ROW && left)
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;
4437}
4438
4439/*
4440 * Move to left or right window.
4441 */
4442 static void
4443win_goto_hor(
4444 int left, // TRUE to go to left win
4445 long count)
4446{
4447 win_T *win;
4448
4449 win = win_horz_neighbor(curtab, curwin, left, count);
4450 if (win != NULL)
4451 win_goto(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454/*
4455 * Make window "wp" the current window.
4456 */
4457 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004458win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459{
Bram Moolenaarc917da42016-07-19 22:31:36 +02004460 win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461}
4462
4463/*
4464 * Make window wp the current window.
4465 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4466 * been closed and isn't valid.
4467 */
4468 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004469win_enter_ext(
4470 win_T *wp,
4471 int undo_sync,
4472 int curwin_invalid,
Bram Moolenaar6f470022018-04-10 18:47:20 +02004473 int trigger_new_autocmds,
4474 int trigger_enter_autocmds,
4475 int trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
4479 if (wp == curwin && !curwin_invalid) /* nothing to do */
4480 return;
4481
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004482#ifdef FEAT_JOB_CHANNEL
4483 if (!curwin_invalid)
4484 leaving_window(curwin);
4485#endif
4486
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004487 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
4489 /*
4490 * Be careful: If autocommands delete the window, return now.
4491 */
4492 if (wp->w_buffer != curbuf)
4493 {
4494 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4495 other_buffer = TRUE;
4496 if (!win_valid(wp))
4497 return;
4498 }
4499 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4500 if (!win_valid(wp))
4501 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004502#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 /* autocmds may abort script processing */
4504 if (aborting())
4505 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508
4509 /* sync undo before leaving the current buffer */
4510 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004511 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004512
4513 /* Might need to scroll the old window before switching, e.g., when the
4514 * cursor was moved. */
4515 update_topline();
4516
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 /* may have to copy the buffer options when 'cpo' contains 'S' */
4518 if (wp->w_buffer != curbuf)
4519 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4520 if (!curwin_invalid)
4521 {
4522 prevwin = curwin; /* remember for CTRL-W p */
4523 curwin->w_redr_status = TRUE;
4524 }
4525 curwin = wp;
4526 curbuf = wp->w_buffer;
4527 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 if (!virtual_active())
4529 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 changed_line_abv_curs(); /* assume cursor position needs updating */
4531
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004532 if (curwin->w_localdir != NULL || curtab->tp_localdir != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004534 char_u *dirname;
4535
4536 // Window or tab has a local directory: Save current directory as
4537 // global directory (unless that was done already) and change to the
4538 // local directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 if (globaldir == NULL)
4540 {
4541 char_u cwd[MAXPATHL];
4542
4543 if (mch_dirname(cwd, MAXPATHL) == OK)
4544 globaldir = vim_strsave(cwd);
4545 }
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004546 if (curwin->w_localdir != NULL)
4547 dirname = curwin->w_localdir;
4548 else
4549 dirname = curtab->tp_localdir;
4550
4551 if (mch_chdir((char *)dirname) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004552 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 }
4554 else if (globaldir != NULL)
4555 {
4556 /* Window doesn't have a local directory and we are not in the global
4557 * directory: Change to the global directory. */
Bram Moolenaar42335f52018-09-13 15:33:43 +02004558 vim_ignored = mch_chdir((char *)globaldir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004559 VIM_CLEAR(globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 shorten_fnames(TRUE);
4561 }
4562
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004563#ifdef FEAT_JOB_CHANNEL
4564 entering_window(curwin);
4565#endif
Bram Moolenaarc917da42016-07-19 22:31:36 +02004566 if (trigger_new_autocmds)
4567 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004568 if (trigger_enter_autocmds)
4569 {
4570 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4571 if (other_buffer)
4572 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
4575#ifdef FEAT_TITLE
4576 maketitle();
4577#endif
4578 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004579 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 if (restart_edit)
4581 redraw_later(VALID); /* causes status line redraw */
4582
4583 /* set window height to desired minimal value */
4584 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4585 win_setheight((int)p_wh);
4586 else if (curwin->w_height == 0)
4587 win_setheight(1);
4588
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004590 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 win_setwidth((int)p_wiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
4593#ifdef FEAT_MOUSE
4594 setmouse(); /* in case jumped to/from help buffer */
4595#endif
4596
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004597 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02004598 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599}
4600
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004603 * Jump to the first open window that contains buffer "buf", if one exists.
4604 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 */
4606 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004607buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004609 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004611 if (curwin->w_buffer == buf)
4612 wp = curwin;
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004613 else
Bram Moolenaar29323592016-07-24 22:04:11 +02004614 FOR_ALL_WINDOWS(wp)
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004615 if (wp->w_buffer == buf)
4616 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 if (wp != NULL)
4618 win_enter(wp, FALSE);
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004619 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004621
4622/*
4623 * Jump to the first open window in any tab page that contains buffer "buf",
4624 * if one exists.
4625 * Returns a pointer to the window found, otherwise NULL.
4626 */
4627 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004628buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004629{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004630 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004631 tabpage_T *tp;
4632
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004633 if (wp != NULL)
4634 return wp;
4635
Bram Moolenaar29323592016-07-24 22:04:11 +02004636 FOR_ALL_TABPAGES(tp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004637 if (tp != curtab)
4638 {
4639 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4640 if (wp->w_buffer == buf)
4641 break;
4642 if (wp != NULL)
4643 {
4644 goto_tabpage_win(tp, wp);
4645 if (curwin != wp)
4646 wp = NULL; /* something went wrong */
4647 break;
4648 }
4649 }
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004650 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004651}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652
Bram Moolenaar888ccac2016-06-04 18:49:36 +02004653static int last_win_id = LOWEST_WIN_ID - 1;
Bram Moolenaar86edef62016-03-13 18:07:30 +01004654
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004656 * Allocate a window structure and link it in the window list when "hidden" is
4657 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004660win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004662 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663
4664 /*
4665 * allocate window structure and linesizes arrays
4666 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004667 new_wp = ALLOC_CLEAR_ONE(win_T);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004668 if (new_wp == NULL)
4669 return NULL;
4670
4671 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004673 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004674 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 }
4676
Bram Moolenaar86edef62016-03-13 18:07:30 +01004677 new_wp->w_id = ++last_win_id;
4678
Bram Moolenaar429fa852013-04-15 12:27:36 +02004679#ifdef FEAT_EVAL
4680 /* init w: variables */
4681 new_wp->w_vars = dict_alloc();
4682 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004684 win_free_lsize(new_wp);
4685 vim_free(new_wp);
4686 return NULL;
4687 }
4688 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004689#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004690
Bram Moolenaar429fa852013-04-15 12:27:36 +02004691 /* Don't execute autocommands while the window is not properly
4692 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4693 * event. */
4694 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004695
Bram Moolenaar429fa852013-04-15 12:27:36 +02004696 /*
4697 * link the window in the window list
4698 */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004699 if (!hidden)
4700 win_append(after, new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004701 new_wp->w_wincol = 0;
4702 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703
Bram Moolenaar429fa852013-04-15 12:27:36 +02004704 /* position the display and the cursor at the top of the file. */
4705 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004707 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004709 new_wp->w_botline = 2;
4710 new_wp->w_cursor.lnum = 1;
Bram Moolenaar429fa852013-04-15 12:27:36 +02004711 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712
Bram Moolenaar375e3392019-01-31 18:26:10 +01004713 // use global option value for global-local options
4714 new_wp->w_p_so = -1;
4715 new_wp->w_p_siso = -1;
4716
Bram Moolenaar429fa852013-04-15 12:27:36 +02004717 /* We won't calculate w_fraction until resizing the window */
4718 new_wp->w_fraction = 0;
4719 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720
4721#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004722 if (gui.in_use)
4723 {
4724 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4725 SBAR_LEFT, new_wp);
4726 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4727 SBAR_RIGHT, new_wp);
4728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729#endif
4730#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004731 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004733 unblock_autocmds();
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004734#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004735 new_wp->w_match_head = NULL;
4736 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004737#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004738 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739}
4740
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004742 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 */
4744 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004745win_free(
4746 win_T *wp,
4747 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748{
4749 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004750 buf_T *buf;
4751 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004753#ifdef FEAT_FOLDING
4754 clearFolding(wp);
4755#endif
4756
4757 /* reduce the reference count to the argument list. */
4758 alist_unlink(wp->w_alist);
4759
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004760 /* Don't execute autocommands while the window is halfway being deleted.
4761 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004762 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004763
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004764#ifdef FEAT_LUA
4765 lua_window_free(wp);
4766#endif
4767
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004768#ifdef FEAT_MZSCHEME
4769 mzscheme_window_free(wp);
4770#endif
4771
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772#ifdef FEAT_PERL
4773 perl_win_free(wp);
4774#endif
4775
4776#ifdef FEAT_PYTHON
4777 python_window_free(wp);
4778#endif
4779
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004780#ifdef FEAT_PYTHON3
4781 python3_window_free(wp);
4782#endif
4783
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784#ifdef FEAT_TCL
4785 tcl_window_free(wp);
4786#endif
4787
4788#ifdef FEAT_RUBY
4789 ruby_window_free(wp);
4790#endif
4791
4792 clear_winopt(&wp->w_onebuf_opt);
4793 clear_winopt(&wp->w_allbuf_opt);
4794
4795#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004796 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4797 hash_init(&wp->w_vars->dv_hashtab);
4798 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799#endif
4800
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004801 {
4802 tabpage_T *ttp;
4803
4804 if (prevwin == wp)
4805 prevwin = NULL;
Bram Moolenaar29323592016-07-24 22:04:11 +02004806 FOR_ALL_TABPAGES(ttp)
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004807 if (ttp->tp_prevwin == wp)
4808 ttp->tp_prevwin = NULL;
4809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 win_free_lsize(wp);
4811
4812 for (i = 0; i < wp->w_tagstacklen; ++i)
4813 vim_free(wp->w_tagstack[i].tagname);
4814
4815 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004816
Bram Moolenaarff18df02013-07-24 17:51:57 +02004817 /* Remove the window from the b_wininfo lists, it may happen that the
4818 * freed memory is re-used for another window. */
Bram Moolenaar29323592016-07-24 22:04:11 +02004819 FOR_ALL_BUFFERS(buf)
Bram Moolenaarff18df02013-07-24 17:51:57 +02004820 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4821 if (wip->wi_win == wp)
4822 wip->wi_win = NULL;
4823
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004825 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004827
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828#ifdef FEAT_JUMPLIST
4829 free_jumplist(wp);
4830#endif
4831
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004832#ifdef FEAT_QUICKFIX
4833 qf_free_all(wp);
4834#endif
4835
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836#ifdef FEAT_GUI
4837 if (gui.in_use)
4838 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4840 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4841 }
4842#endif /* FEAT_GUI */
4843
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004844#ifdef FEAT_MENU
4845 remove_winbar(wp);
4846#endif
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02004847#ifdef FEAT_TEXT_PROP
4848 free_callback(&wp->w_filter_cb);
4849#endif
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004850
Bram Moolenaar860cae12010-06-05 23:22:07 +02004851#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004852 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004853#endif
4854
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004855 if (win_valid_any_tab(wp))
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004856 win_remove(wp, tp);
Bram Moolenaar3be85852014-06-12 14:01:31 +02004857 if (autocmd_busy)
4858 {
4859 wp->w_next = au_pending_free_win;
4860 au_pending_free_win = wp;
4861 }
4862 else
4863 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004864
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004865 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866}
4867
4868/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004869 * Return TRUE if "wp" is not in the list of windows: the autocmd window or a
4870 * popup window.
4871 */
4872 int
4873win_unlisted(win_T *wp)
4874{
4875 return wp == aucmd_win || bt_popup(wp->w_buffer);
4876}
4877
Bram Moolenaar35d5af62019-05-26 20:44:10 +02004878#if defined(FEAT_TEXT_PROP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004879/*
4880 * Free a popup window. This does not take the window out of the window list
4881 * and assumes there is only one toplevel frame, no split.
4882 */
4883 void
4884win_free_popup(win_T *win)
4885{
4886 win_close_buffer(win, TRUE, FALSE);
Bram Moolenaar35d5af62019-05-26 20:44:10 +02004887# if defined(FEAT_TIMERS)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02004888 if (win->w_popup_timer != NULL)
4889 stop_timer(win->w_popup_timer);
Bram Moolenaar35d5af62019-05-26 20:44:10 +02004890# endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004891 vim_free(win->w_frame);
4892 win_free(win, NULL);
4893}
Bram Moolenaar35d5af62019-05-26 20:44:10 +02004894#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004895
4896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 * Append window "wp" in the window list after window "after".
4898 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004899 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004900win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901{
4902 win_T *before;
4903
4904 if (after == NULL) /* after NULL is in front of the first */
4905 before = firstwin;
4906 else
4907 before = after->w_next;
4908
4909 wp->w_next = before;
4910 wp->w_prev = after;
4911 if (after == NULL)
4912 firstwin = wp;
4913 else
4914 after->w_next = wp;
4915 if (before == NULL)
4916 lastwin = wp;
4917 else
4918 before->w_prev = wp;
4919}
4920
4921/*
4922 * Remove a window from the window list.
4923 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004924 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004925win_remove(
4926 win_T *wp,
4927 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928{
4929 if (wp->w_prev != NULL)
4930 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004931 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004932 firstwin = curtab->tp_firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004933 else
4934 tp->tp_firstwin = wp->w_next;
Bram Moolenaar816968d2017-09-29 21:29:18 +02004935
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 if (wp->w_next != NULL)
4937 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004938 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004939 lastwin = curtab->tp_lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004940 else
4941 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942}
4943
4944/*
4945 * Append frame "frp" in a frame list after frame "after".
4946 */
4947 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004948frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949{
4950 frp->fr_next = after->fr_next;
4951 after->fr_next = frp;
4952 if (frp->fr_next != NULL)
4953 frp->fr_next->fr_prev = frp;
4954 frp->fr_prev = after;
4955}
4956
4957/*
4958 * Insert frame "frp" in a frame list before frame "before".
4959 */
4960 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004961frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962{
4963 frp->fr_next = before;
4964 frp->fr_prev = before->fr_prev;
4965 before->fr_prev = frp;
4966 if (frp->fr_prev != NULL)
4967 frp->fr_prev->fr_next = frp;
4968 else
4969 frp->fr_parent->fr_child = frp;
4970}
4971
4972/*
4973 * Remove a frame from a frame list.
4974 */
4975 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004976frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977{
4978 if (frp->fr_prev != NULL)
4979 frp->fr_prev->fr_next = frp->fr_next;
4980 else
Bram Moolenaar6f361c92018-01-31 19:06:50 +01004981 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 frp->fr_parent->fr_child = frp->fr_next;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01004983 /* special case: topframe->fr_child == frp */
4984 if (topframe->fr_child == frp)
4985 topframe->fr_child = frp->fr_next;
4986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 if (frp->fr_next != NULL)
4988 frp->fr_next->fr_prev = frp->fr_prev;
4989}
4990
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991/*
4992 * Allocate w_lines[] for window "wp".
4993 * Return FAIL for failure, OK for success.
4994 */
4995 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004996win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997{
4998 wp->w_lines_valid = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004999 wp->w_lines = ALLOC_CLEAR_MULT(wline_T, Rows );
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 if (wp->w_lines == NULL)
5001 return FAIL;
5002 return OK;
5003}
5004
5005/*
5006 * free lsize arrays for a window
5007 */
5008 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005009win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02005011 /* TODO: why would wp be NULL here? */
5012 if (wp != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005013 VIM_CLEAR(wp->w_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014}
5015
5016/*
5017 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005018 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 */
5020 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005021shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005023 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024
5025 if (firstwin == NULL) /* not initialized yet */
5026 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 if (h < frame_minheight(topframe, NULL))
5028 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005029
5030 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 * that doesn't result in the right height, forget about that option. */
5032 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02005033 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 frame_new_height(topframe, h, FALSE, FALSE);
5035
5036 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 compute_cmdrow();
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005038 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005039
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040#if 0
5041 /* Disabled: don't want making the screen smaller make a window larger. */
5042 if (p_ea)
5043 win_equal(curwin, FALSE, 'v');
5044#endif
5045}
5046
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047/*
5048 * Called from win_new_shellsize() after Columns changed.
5049 */
5050 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005051shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052{
5053 if (firstwin == NULL) /* not initialized yet */
5054 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005055
5056 /* First try setting the widths of windows with 'winfixwidth'. If that
5057 * doesn't result in the right width, forget about that option. */
5058 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02005059 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005060 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
5061
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
5063#if 0
5064 /* Disabled: don't want making the screen smaller make a window larger. */
5065 if (p_ea)
5066 win_equal(curwin, FALSE, 'h');
5067#endif
5068}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
5070#if defined(FEAT_CMDWIN) || defined(PROTO)
5071/*
5072 * Save the size of all windows in "gap".
5073 */
5074 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005075win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076
5077{
5078 win_T *wp;
5079
5080 ga_init2(gap, (int)sizeof(int), 1);
5081 if (ga_grow(gap, win_count() * 2) == OK)
Bram Moolenaar29323592016-07-24 22:04:11 +02005082 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
5084 ((int *)gap->ga_data)[gap->ga_len++] =
5085 wp->w_width + wp->w_vsep_width;
5086 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
5087 }
5088}
5089
5090/*
5091 * Restore window sizes, but only if the number of windows is still the same.
5092 * Does not free the growarray.
5093 */
5094 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005095win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096{
5097 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02005098 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099
5100 if (win_count() * 2 == gap->ga_len)
5101 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005102 /* The order matters, because frames contain other frames, but it's
5103 * difficult to get right. The easy way out is to do it twice. */
5104 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005106 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02005107 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb643e772014-07-16 15:18:26 +02005108 {
5109 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
5110 win_setheight_win(((int *)gap->ga_data)[i++], wp);
5111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 }
5113 /* recompute the window positions */
5114 (void)win_comp_pos();
5115 }
5116}
5117#endif /* FEAT_CMDWIN */
5118
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119/*
5120 * Update the position for all windows, using the width and height of the
5121 * frames.
5122 * Returns the row just after the last window.
5123 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005124 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005125win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005127 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 int col = 0;
5129
5130 frame_comp_pos(topframe, &row, &col);
5131 return row;
5132}
5133
5134/*
5135 * Update the position of the windows in frame "topfrp", using the width and
5136 * height of the frames.
5137 * "*row" and "*col" are the top-left position of the frame. They are updated
5138 * to the bottom-right position plus one.
5139 */
5140 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005141frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142{
5143 win_T *wp;
5144 frame_T *frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 int startcol;
5146 int startrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005147 int h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
5149 wp = topfrp->fr_win;
5150 if (wp != NULL)
5151 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005152 if (wp->w_winrow != *row || wp->w_wincol != *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 {
5154 /* position changed, redraw */
5155 wp->w_winrow = *row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 wp->w_wincol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 redraw_win_later(wp, NOT_VALID);
5158 wp->w_redr_status = TRUE;
5159 }
Bram Moolenaar415a6932017-12-05 20:31:07 +01005160 /* WinBar will not show if the window height is zero */
5161 h = VISIBLE_HEIGHT(wp) + wp->w_status_height;
5162 *row += h > topfrp->fr_height ? topfrp->fr_height : h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 *col += wp->w_width + wp->w_vsep_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165 else
5166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 startrow = *row;
5168 startcol = *col;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005169 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 if (topfrp->fr_layout == FR_ROW)
5172 *row = startrow; /* all frames are at the same row */
5173 else
5174 *col = startcol; /* all frames are at the same col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 frame_comp_pos(frp, row, col);
5176 }
5177 }
5178}
5179
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180/*
5181 * Set current window height and take care of repositioning other windows to
5182 * fit around it.
5183 */
5184 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005185win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186{
5187 win_setheight_win(height, curwin);
5188}
5189
5190/*
5191 * Set the window height of window "win" and take care of repositioning other
5192 * windows to fit around it.
5193 */
5194 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005195win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196{
5197 int row;
5198
5199 if (win == curwin)
5200 {
5201 /* Always keep current window at least one line high, even when
5202 * 'winminheight' is zero. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 if (height < p_wmh)
5204 height = p_wmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 if (height == 0)
5206 height = 1;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005207 height += WINBAR_HEIGHT(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 }
5209
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 frame_setheight(win->w_frame, height + win->w_status_height);
5211
5212 /* recompute the window positions */
5213 row = win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214
5215 /*
5216 * If there is extra space created between the last window and the command
5217 * line, clear it.
5218 */
5219 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5220 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5221 cmdline_row = row;
5222 msg_row = row;
5223 msg_col = 0;
5224
5225 redraw_all_later(NOT_VALID);
5226}
5227
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228/*
5229 * Set the height of a frame to "height" and take care that all frames and
5230 * windows inside it are resized. Also resize frames on the left and right if
5231 * the are in the same FR_ROW frame.
5232 *
5233 * Strategy:
5234 * If the frame is part of a FR_COL frame, try fitting the frame in that
5235 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5236 * go to containing frames to resize them and make room.
5237 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5238 * Check for the minimal height of the FR_ROW frame.
5239 * At the top level we can also use change the command line height.
5240 */
5241 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005242frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243{
5244 int room; /* total number of lines available */
5245 int take; /* number of lines taken from other windows */
5246 int room_cmdline; /* lines available from cmdline */
5247 int run;
5248 frame_T *frp;
5249 int h;
5250 int room_reserved;
5251
5252 /* If the height already is the desired value, nothing to do. */
5253 if (curfrp->fr_height == height)
5254 return;
5255
5256 if (curfrp->fr_parent == NULL)
5257 {
5258 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005259 if (height > ROWS_AVAIL)
5260 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 if (height > 0)
5262 frame_new_height(curfrp, height, FALSE, FALSE);
5263 }
5264 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5265 {
5266 /* Row of frames: Also need to resize frames left and right of this
5267 * one. First check for the minimal height of these. */
5268 h = frame_minheight(curfrp->fr_parent, NULL);
5269 if (height < h)
5270 height = h;
5271 frame_setheight(curfrp->fr_parent, height);
5272 }
5273 else
5274 {
5275 /*
5276 * Column of frames: try to change only frames in this column.
5277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 /*
5279 * Do this twice:
5280 * 1: compute room available, if it's not enough try resizing the
5281 * containing frame.
5282 * 2: compute the room available and adjust the height to it.
5283 * Try not to reduce the height of a window with 'winfixheight' set.
5284 */
5285 for (run = 1; run <= 2; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {
5287 room = 0;
5288 room_reserved = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005289 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
5291 if (frp != curfrp
5292 && frp->fr_win != NULL
5293 && frp->fr_win->w_p_wfh)
5294 room_reserved += frp->fr_height;
5295 room += frp->fr_height;
5296 if (frp != curfrp)
5297 room -= frame_minheight(frp, NULL);
5298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 if (curfrp->fr_width != Columns)
5300 room_cmdline = 0;
5301 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 {
5303 room_cmdline = Rows - p_ch - (lastwin->w_winrow
Bram Moolenaar415a6932017-12-05 20:31:07 +01005304 + VISIBLE_HEIGHT(lastwin)
5305 + lastwin->w_status_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 if (room_cmdline < 0)
5307 room_cmdline = 0;
5308 }
5309
5310 if (height <= room + room_cmdline)
5311 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 if (run == 2 || curfrp->fr_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 {
5314 if (height > room + room_cmdline)
5315 height = room + room_cmdline;
5316 break;
5317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 frame_setheight(curfrp->fr_parent, height
5319 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
5321
5322 /*
5323 * Compute the number of lines we will take from others frames (can be
5324 * negative!).
5325 */
5326 take = height - curfrp->fr_height;
5327
5328 /* If there is not enough room, also reduce the height of a window
5329 * with 'winfixheight' set. */
5330 if (height > room + room_cmdline - room_reserved)
5331 room_reserved = room + room_cmdline - height;
5332 /* If there is only a 'winfixheight' window and making the
5333 * window smaller, need to make the other window taller. */
5334 if (take < 0 && room - curfrp->fr_height < room_reserved)
5335 room_reserved = 0;
5336
5337 if (take > 0 && room_cmdline > 0)
5338 {
5339 /* use lines from cmdline first */
5340 if (take < room_cmdline)
5341 room_cmdline = take;
5342 take -= room_cmdline;
5343 topframe->fr_height += room_cmdline;
5344 }
5345
5346 /*
5347 * set the current frame to the new height
5348 */
5349 frame_new_height(curfrp, height, FALSE, FALSE);
5350
5351 /*
5352 * First take lines from the frames after the current frame. If
5353 * that is not enough, takes lines from frames above the current
5354 * frame.
5355 */
5356 for (run = 0; run < 2; ++run)
5357 {
5358 if (run == 0)
5359 frp = curfrp->fr_next; /* 1st run: start with next window */
5360 else
5361 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5362 while (frp != NULL && take != 0)
5363 {
5364 h = frame_minheight(frp, NULL);
5365 if (room_reserved > 0
5366 && frp->fr_win != NULL
5367 && frp->fr_win->w_p_wfh)
5368 {
5369 if (room_reserved >= frp->fr_height)
5370 room_reserved -= frp->fr_height;
5371 else
5372 {
5373 if (frp->fr_height - room_reserved > take)
5374 room_reserved = frp->fr_height - take;
5375 take -= frp->fr_height - room_reserved;
5376 frame_new_height(frp, room_reserved, FALSE, FALSE);
5377 room_reserved = 0;
5378 }
5379 }
5380 else
5381 {
5382 if (frp->fr_height - take < h)
5383 {
5384 take -= frp->fr_height - h;
5385 frame_new_height(frp, h, FALSE, FALSE);
5386 }
5387 else
5388 {
5389 frame_new_height(frp, frp->fr_height - take,
5390 FALSE, FALSE);
5391 take = 0;
5392 }
5393 }
5394 if (run == 0)
5395 frp = frp->fr_next;
5396 else
5397 frp = frp->fr_prev;
5398 }
5399 }
5400 }
5401}
5402
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403/*
5404 * Set current window width and take care of repositioning other windows to
5405 * fit around it.
5406 */
5407 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005408win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409{
5410 win_setwidth_win(width, curwin);
5411}
5412
5413 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005414win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415{
5416 /* Always keep current window at least one column wide, even when
5417 * 'winminwidth' is zero. */
5418 if (wp == curwin)
5419 {
5420 if (width < p_wmw)
5421 width = p_wmw;
5422 if (width == 0)
5423 width = 1;
5424 }
5425
5426 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5427
5428 /* recompute the window positions */
5429 (void)win_comp_pos();
5430
5431 redraw_all_later(NOT_VALID);
5432}
5433
5434/*
5435 * Set the width of a frame to "width" and take care that all frames and
5436 * windows inside it are resized. Also resize frames above and below if the
5437 * are in the same FR_ROW frame.
5438 *
5439 * Strategy is similar to frame_setheight().
5440 */
5441 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005442frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443{
5444 int room; /* total number of lines available */
5445 int take; /* number of lines taken from other windows */
5446 int run;
5447 frame_T *frp;
5448 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005449 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450
5451 /* If the width already is the desired value, nothing to do. */
5452 if (curfrp->fr_width == width)
5453 return;
5454
5455 if (curfrp->fr_parent == NULL)
5456 /* topframe: can't change width */
5457 return;
5458
5459 if (curfrp->fr_parent->fr_layout == FR_COL)
5460 {
5461 /* Column of frames: Also need to resize frames above and below of
5462 * this one. First check for the minimal width of these. */
5463 w = frame_minwidth(curfrp->fr_parent, NULL);
5464 if (width < w)
5465 width = w;
5466 frame_setwidth(curfrp->fr_parent, width);
5467 }
5468 else
5469 {
5470 /*
5471 * Row of frames: try to change only frames in this row.
5472 *
5473 * Do this twice:
5474 * 1: compute room available, if it's not enough try resizing the
5475 * containing frame.
5476 * 2: compute the room available and adjust the width to it.
5477 */
5478 for (run = 1; run <= 2; ++run)
5479 {
5480 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005481 room_reserved = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005482 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005484 if (frp != curfrp
5485 && frp->fr_win != NULL
5486 && frp->fr_win->w_p_wfw)
5487 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 room += frp->fr_width;
5489 if (frp != curfrp)
5490 room -= frame_minwidth(frp, NULL);
5491 }
5492
5493 if (width <= room)
5494 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005495 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 {
5497 if (width > room)
5498 width = room;
5499 break;
5500 }
5501 frame_setwidth(curfrp->fr_parent, width
5502 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5503 }
5504
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 /*
5506 * Compute the number of lines we will take from others frames (can be
5507 * negative!).
5508 */
5509 take = width - curfrp->fr_width;
5510
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005511 /* If there is not enough room, also reduce the width of a window
5512 * with 'winfixwidth' set. */
5513 if (width > room - room_reserved)
5514 room_reserved = room - width;
5515 /* If there is only a 'winfixwidth' window and making the
5516 * window smaller, need to make the other window narrower. */
5517 if (take < 0 && room - curfrp->fr_width < room_reserved)
5518 room_reserved = 0;
5519
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 /*
5521 * set the current frame to the new width
5522 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005523 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524
5525 /*
5526 * First take lines from the frames right of the current frame. If
5527 * that is not enough, takes lines from frames left of the current
5528 * frame.
5529 */
5530 for (run = 0; run < 2; ++run)
5531 {
5532 if (run == 0)
5533 frp = curfrp->fr_next; /* 1st run: start with next window */
5534 else
5535 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5536 while (frp != NULL && take != 0)
5537 {
5538 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005539 if (room_reserved > 0
5540 && frp->fr_win != NULL
5541 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005543 if (room_reserved >= frp->fr_width)
5544 room_reserved -= frp->fr_width;
5545 else
5546 {
5547 if (frp->fr_width - room_reserved > take)
5548 room_reserved = frp->fr_width - take;
5549 take -= frp->fr_width - room_reserved;
5550 frame_new_width(frp, room_reserved, FALSE, FALSE);
5551 room_reserved = 0;
5552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 }
5554 else
5555 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005556 if (frp->fr_width - take < w)
5557 {
5558 take -= frp->fr_width - w;
5559 frame_new_width(frp, w, FALSE, FALSE);
5560 }
5561 else
5562 {
5563 frame_new_width(frp, frp->fr_width - take,
5564 FALSE, FALSE);
5565 take = 0;
5566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 }
5568 if (run == 0)
5569 frp = frp->fr_next;
5570 else
5571 frp = frp->fr_prev;
5572 }
5573 }
5574 }
5575}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576
5577/*
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005578 * Check 'winminheight' for a valid value and reduce it if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 */
5580 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005581win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
5583 int room;
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005584 int needed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005587 // loop until there is a 'winminheight' that is possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 while (p_wmh > 0)
5589 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005590 room = Rows - p_ch;
5591 needed = frame_minheight(topframe, NULL);
5592 if (room >= needed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 break;
5594 --p_wmh;
5595 if (first)
5596 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005597 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 first = FALSE;
5599 }
5600 }
5601}
5602
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005603/*
5604 * Check 'winminwidth' for a valid value and reduce it if needed.
5605 */
5606 void
5607win_setminwidth(void)
5608{
5609 int room;
5610 int needed;
5611 int first = TRUE;
5612
5613 // loop until there is a 'winminheight' that is possible
5614 while (p_wmw > 0)
5615 {
5616 room = Columns;
5617 needed = frame_minwidth(topframe, NULL);
5618 if (room >= needed)
5619 break;
5620 --p_wmw;
5621 if (first)
5622 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005623 emsg(_(e_noroom));
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005624 first = FALSE;
5625 }
5626 }
5627}
5628
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005629#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630
5631/*
5632 * Status line of dragwin is dragged "offset" lines down (negative is up).
5633 */
5634 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005635win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
5637 frame_T *curfr;
5638 frame_T *fr;
5639 int room;
5640 int row;
5641 int up; /* if TRUE, drag status line up, otherwise down */
5642 int n;
5643
5644 fr = dragwin->w_frame;
5645 curfr = fr;
5646 if (fr != topframe) /* more than one window */
5647 {
5648 fr = fr->fr_parent;
5649 /* When the parent frame is not a column of frames, its parent should
5650 * be. */
5651 if (fr->fr_layout != FR_COL)
5652 {
5653 curfr = fr;
5654 if (fr != topframe) /* only a row of windows, may drag statusline */
5655 fr = fr->fr_parent;
5656 }
5657 }
5658
5659 /* If this is the last frame in a column, may want to resize the parent
5660 * frame instead (go two up to skip a row of frames). */
5661 while (curfr != topframe && curfr->fr_next == NULL)
5662 {
5663 if (fr != topframe)
5664 fr = fr->fr_parent;
5665 curfr = fr;
5666 if (fr != topframe)
5667 fr = fr->fr_parent;
5668 }
5669
5670 if (offset < 0) /* drag up */
5671 {
5672 up = TRUE;
5673 offset = -offset;
5674 /* sum up the room of the current frame and above it */
5675 if (fr == curfr)
5676 {
5677 /* only one window */
5678 room = fr->fr_height - frame_minheight(fr, NULL);
5679 }
5680 else
5681 {
5682 room = 0;
5683 for (fr = fr->fr_child; ; fr = fr->fr_next)
5684 {
5685 room += fr->fr_height - frame_minheight(fr, NULL);
5686 if (fr == curfr)
5687 break;
5688 }
5689 }
5690 fr = curfr->fr_next; /* put fr at frame that grows */
5691 }
5692 else /* drag down */
5693 {
5694 up = FALSE;
5695 /*
5696 * Only dragging the last status line can reduce p_ch.
5697 */
5698 room = Rows - cmdline_row;
5699 if (curfr->fr_next == NULL)
5700 room -= 1;
5701 else
5702 room -= p_ch;
5703 if (room < 0)
5704 room = 0;
5705 /* sum up the room of frames below of the current one */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005706 FOR_ALL_FRAMES(fr, curfr->fr_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 room += fr->fr_height - frame_minheight(fr, NULL);
5708 fr = curfr; /* put fr at window that grows */
5709 }
5710
5711 if (room < offset) /* Not enough room */
5712 offset = room; /* Move as far as we can */
5713 if (offset <= 0)
5714 return;
5715
5716 /*
5717 * Grow frame fr by "offset" lines.
5718 * Doesn't happen when dragging the last status line up.
5719 */
5720 if (fr != NULL)
5721 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5722
5723 if (up)
5724 fr = curfr; /* current frame gets smaller */
5725 else
5726 fr = curfr->fr_next; /* next frame gets smaller */
5727
5728 /*
5729 * Now make the other frames smaller.
5730 */
5731 while (fr != NULL && offset > 0)
5732 {
5733 n = frame_minheight(fr, NULL);
5734 if (fr->fr_height - offset <= n)
5735 {
5736 offset -= fr->fr_height - n;
5737 frame_new_height(fr, n, !up, FALSE);
5738 }
5739 else
5740 {
5741 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5742 break;
5743 }
5744 if (up)
5745 fr = fr->fr_prev;
5746 else
5747 fr = fr->fr_next;
5748 }
5749 row = win_comp_pos();
5750 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5751 cmdline_row = row;
5752 p_ch = Rows - cmdline_row;
5753 if (p_ch < 1)
5754 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005755 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005756 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 showmode();
5758}
5759
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760/*
5761 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5762 */
5763 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005764win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765{
5766 frame_T *curfr;
5767 frame_T *fr;
5768 int room;
5769 int left; /* if TRUE, drag separator line left, otherwise right */
5770 int n;
5771
5772 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005773 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 return;
5775 curfr = fr;
5776 fr = fr->fr_parent;
5777 /* When the parent frame is not a row of frames, its parent should be. */
5778 if (fr->fr_layout != FR_ROW)
5779 {
5780 if (fr == topframe) /* only a column of windows (cannot happen?) */
5781 return;
5782 curfr = fr;
5783 fr = fr->fr_parent;
5784 }
5785
5786 /* If this is the last frame in a row, may want to resize a parent
5787 * frame instead. */
5788 while (curfr->fr_next == NULL)
5789 {
5790 if (fr == topframe)
5791 break;
5792 curfr = fr;
5793 fr = fr->fr_parent;
5794 if (fr != topframe)
5795 {
5796 curfr = fr;
5797 fr = fr->fr_parent;
5798 }
5799 }
5800
5801 if (offset < 0) /* drag left */
5802 {
5803 left = TRUE;
5804 offset = -offset;
5805 /* sum up the room of the current frame and left of it */
5806 room = 0;
5807 for (fr = fr->fr_child; ; fr = fr->fr_next)
5808 {
5809 room += fr->fr_width - frame_minwidth(fr, NULL);
5810 if (fr == curfr)
5811 break;
5812 }
5813 fr = curfr->fr_next; /* put fr at frame that grows */
5814 }
5815 else /* drag right */
5816 {
5817 left = FALSE;
5818 /* sum up the room of frames right of the current one */
5819 room = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005820 FOR_ALL_FRAMES(fr, curfr->fr_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 room += fr->fr_width - frame_minwidth(fr, NULL);
5822 fr = curfr; /* put fr at window that grows */
5823 }
5824
5825 if (room < offset) /* Not enough room */
5826 offset = room; /* Move as far as we can */
5827 if (offset <= 0) /* No room at all, quit. */
5828 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005829 if (fr == NULL)
5830 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
5832 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005833 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834
5835 /* shrink other frames: current and at the left or at the right */
5836 if (left)
5837 fr = curfr; /* current frame gets smaller */
5838 else
5839 fr = curfr->fr_next; /* next frame gets smaller */
5840
5841 while (fr != NULL && offset > 0)
5842 {
5843 n = frame_minwidth(fr, NULL);
5844 if (fr->fr_width - offset <= n)
5845 {
5846 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005847 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 }
5849 else
5850 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005851 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 break;
5853 }
5854 if (left)
5855 fr = fr->fr_prev;
5856 else
5857 fr = fr->fr_next;
5858 }
5859 (void)win_comp_pos();
5860 redraw_all_later(NOT_VALID);
5861}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862#endif /* FEAT_MOUSE */
5863
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005864#define FRACTION_MULT 16384L
5865
5866/*
5867 * Set wp->w_fraction for the current w_wrow and w_height.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005868 * Has no effect when the window is less than two lines.
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005869 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005870 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005871set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005872{
Bram Moolenaar3679c172017-11-22 22:22:11 +01005873 if (wp->w_height > 1)
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005874 // When cursor is in the first line the percentage is computed as if
5875 // it's halfway that line. Thus with two lines it is 25%, with three
5876 // lines 17%, etc. Similarly for the last line: 75%, 83%, etc.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005877 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005878 + FRACTION_MULT / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005879}
5880
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881/*
5882 * Set the height of a window.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005883 * "height" excludes any window toolbar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 * This takes care of the things inside the window, not what happens to the
5885 * window position, the frame or to other windows.
5886 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005887 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005888win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889{
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005890 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891
5892 /* Don't want a negative height. Happens when splitting a tiny window.
5893 * Will equalize heights soon to fix it. */
5894 if (height < 0)
5895 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005896 if (wp->w_height == height)
5897 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005899 if (wp->w_height > 0)
5900 {
5901 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005902 /* w_wrow needs to be valid. When setting 'laststatus' this may
5903 * call win_new_height() recursively. */
5904 validate_cursor();
5905 if (wp->w_height != prev_height)
5906 return; /* Recursive call already changed the size, bail out here
5907 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005908 if (wp->w_wrow != wp->w_prev_fraction_row)
5909 set_fraction(wp);
5910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911
5912 wp->w_height = height;
5913 wp->w_skipcol = 0;
5914
Bram Moolenaar955f1982017-02-05 15:10:51 +01005915 /* There is no point in adjusting the scroll position when exiting. Some
5916 * values might be invalid. */
5917 if (!exiting)
5918 scroll_to_fraction(wp, prev_height);
Bram Moolenaar46328f92016-08-28 15:39:57 +02005919}
5920
5921 void
5922scroll_to_fraction(win_T *wp, int prev_height)
5923{
5924 linenr_T lnum;
5925 int sline, line_size;
5926 int height = wp->w_height;
5927
Bram Moolenaara9b25352019-05-12 14:25:30 +02005928 // Don't change w_topline in any of these cases:
5929 // - window height is 0
5930 // - 'scrollbind' is set and this isn't the current window
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +02005931 // - window height is sufficient to display the whole buffer and first line
5932 // is visible.
Bram Moolenaara9b25352019-05-12 14:25:30 +02005933 if (height > 0
5934 && (!wp->w_p_scb || wp == curwin)
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +02005935 && (height < wp->w_buffer->b_ml.ml_line_count || wp->w_topline > 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005937 /*
5938 * Find a value for w_topline that shows the cursor at the same
5939 * relative position in the window as before (more or less).
5940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 lnum = wp->w_cursor.lnum;
5942 if (lnum < 1) /* can happen when starting up */
5943 lnum = 1;
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005944 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L)
5945 / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5947 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005948
5949 if (sline >= 0)
5950 {
5951 /* Make sure the whole cursor line is visible, if possible. */
5952 int rows = plines_win(wp, lnum, FALSE);
5953
5954 if (sline > wp->w_height - rows)
5955 {
5956 sline = wp->w_height - rows;
5957 wp->w_wrow -= rows - line_size;
5958 }
5959 }
5960
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 if (sline < 0)
5962 {
5963 /*
5964 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005965 * Make cursor line the first line in the window. If not enough
5966 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 */
5968 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005969 if (wp->w_wrow >= wp->w_height
Bram Moolenaar02631462017-09-22 15:20:32 +02005970 && (wp->w_width - win_col_off(wp)) > 0)
Bram Moolenaar26470632006-10-24 19:12:40 +00005971 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005972 wp->w_skipcol += wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00005973 --wp->w_wrow;
5974 while (wp->w_wrow >= wp->w_height)
5975 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005976 wp->w_skipcol += wp->w_width - win_col_off(wp)
Bram Moolenaar26470632006-10-24 19:12:40 +00005977 + win_col_off2(wp);
5978 --wp->w_wrow;
5979 }
5980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005982 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005984 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 {
5986#ifdef FEAT_FOLDING
5987 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5988 if (lnum == 1)
5989 {
5990 /* first line in buffer is folded */
5991 line_size = 1;
5992 --sline;
5993 break;
5994 }
5995#endif
5996 --lnum;
5997#ifdef FEAT_DIFF
5998 if (lnum == wp->w_topline)
5999 line_size = plines_win_nofill(wp, lnum, TRUE)
6000 + wp->w_topfill;
6001 else
6002#endif
6003 line_size = plines_win(wp, lnum, TRUE);
6004 sline -= line_size;
6005 }
Bram Moolenaar34114692005-01-02 11:28:13 +00006006
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 if (sline < 0)
6008 {
6009 /*
6010 * Line we want at top would go off top of screen. Use next
6011 * line instead.
6012 */
6013#ifdef FEAT_FOLDING
6014 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
6015#endif
6016 lnum++;
6017 wp->w_wrow -= line_size + sline;
6018 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02006019 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 {
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006021 // First line of file reached, use that as topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 lnum = 1;
6023 wp->w_wrow -= sline;
6024 }
6025 }
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006026 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 }
6028
6029 if (wp == curwin)
6030 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006031 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 update_topline();
6033 curs_columns(FALSE); /* validate w_wrow */
6034 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02006035 if (prev_height > 0)
6036 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037
6038 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006039 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 invalidate_botline_win(wp);
6042}
6043
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044/*
6045 * Set the width of a window.
6046 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02006047 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006048win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049{
6050 wp->w_width = width;
6051 wp->w_lines_valid = 0;
6052 changed_line_abv_curs_win(wp);
6053 invalidate_botline_win(wp);
6054 if (wp == curwin)
6055 {
6056 update_topline();
6057 curs_columns(TRUE); /* validate w_wrow */
6058 }
6059 redraw_win_later(wp, NOT_VALID);
6060 wp->w_redr_status = TRUE;
6061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062
6063 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006064win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065{
6066 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
6067 if (wp->w_p_scr == 0)
6068 wp->w_p_scr = 1;
6069}
6070
6071/*
6072 * command_height: called whenever p_ch has been changed
6073 */
6074 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006075command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 int h;
6078 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006079 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006081 /* Use the value of p_ch that we remembered. This is needed for when the
6082 * GUI starts up, we can't be sure in what order things happen. And when
6083 * p_ch was changed in another tab page. */
6084 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006085
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 /* Find bottom frame with width of screen. */
6087 frp = lastwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 while (frp->fr_width != Columns && frp->fr_parent != NULL)
6089 frp = frp->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090
6091 /* Avoid changing the height of a window with 'winfixheight' set. */
6092 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
6093 && frp->fr_win->w_p_wfh)
6094 frp = frp->fr_prev;
6095
6096 if (starting != NO_SCREEN)
6097 {
6098 cmdline_row = Rows - p_ch;
6099
6100 if (p_ch > old_p_ch) /* p_ch got bigger */
6101 {
6102 while (p_ch > old_p_ch)
6103 {
6104 if (frp == NULL)
6105 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006106 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00006108 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 cmdline_row = Rows - p_ch;
6110 break;
6111 }
6112 h = frp->fr_height - frame_minheight(frp, NULL);
6113 if (h > p_ch - old_p_ch)
6114 h = p_ch - old_p_ch;
6115 old_p_ch += h;
6116 frame_add_height(frp, -h);
6117 frp = frp->fr_prev;
6118 }
6119
6120 /* Recompute window positions. */
6121 (void)win_comp_pos();
6122
6123 /* clear the lines added to cmdline */
6124 if (full_screen)
6125 screen_fill((int)(cmdline_row), (int)Rows, 0,
6126 (int)Columns, ' ', ' ', 0);
6127 msg_row = cmdline_row;
6128 redraw_cmdline = TRUE;
6129 return;
6130 }
6131
6132 if (msg_row < cmdline_row)
6133 msg_row = cmdline_row;
6134 redraw_cmdline = TRUE;
6135 }
6136 frame_add_height(frp, (int)(old_p_ch - p_ch));
6137
6138 /* Recompute window positions. */
6139 if (frp != lastwin->w_frame)
6140 (void)win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141}
6142
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143/*
6144 * Resize frame "frp" to be "n" lines higher (negative for less high).
6145 * Also resize the frames it is contained in.
6146 */
6147 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006148frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149{
6150 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
6151 for (;;)
6152 {
6153 frp = frp->fr_parent;
6154 if (frp == NULL)
6155 break;
6156 frp->fr_height += n;
6157 }
6158}
6159
6160/*
6161 * Add or remove a status line for the bottom window(s), according to the
6162 * value of 'laststatus'.
6163 */
6164 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006165last_status(
6166 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167{
6168 /* Don't make a difference between horizontal or vertical split. */
6169 last_status_rec(topframe, (p_ls == 2
Bram Moolenaar459ca562016-11-10 18:16:33 +01006170 || (p_ls == 1 && (morewin || !ONE_WINDOW))));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171}
6172
6173 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006174last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175{
6176 frame_T *fp;
6177 win_T *wp;
6178
6179 if (fr->fr_layout == FR_LEAF)
6180 {
6181 wp = fr->fr_win;
6182 if (wp->w_status_height != 0 && !statusline)
6183 {
6184 /* remove status line */
6185 win_new_height(wp, wp->w_height + 1);
6186 wp->w_status_height = 0;
6187 comp_col();
6188 }
6189 else if (wp->w_status_height == 0 && statusline)
6190 {
6191 /* Find a frame to take a line from. */
6192 fp = fr;
6193 while (fp->fr_height <= frame_minheight(fp, NULL))
6194 {
6195 if (fp == topframe)
6196 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006197 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 return;
6199 }
6200 /* In a column of frames: go to frame above. If already at
6201 * the top or in a row of frames: go to parent. */
6202 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6203 fp = fp->fr_prev;
6204 else
6205 fp = fp->fr_parent;
6206 }
6207 wp->w_status_height = 1;
6208 if (fp != fr)
6209 {
6210 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6211 frame_fix_height(wp);
6212 (void)win_comp_pos();
6213 }
6214 else
6215 win_new_height(wp, wp->w_height - 1);
6216 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006217 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 }
6219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 else if (fr->fr_layout == FR_ROW)
6221 {
6222 /* vertically split windows, set status line for each one */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006223 FOR_ALL_FRAMES(fp, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 last_status_rec(fp, statusline);
6225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 else
6227 {
6228 /* horizontally split window, set status line for last one */
6229 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6230 ;
6231 last_status_rec(fp, statusline);
6232 }
6233}
6234
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006235/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006236 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006237 */
6238 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006239tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006240{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006241#ifdef FEAT_GUI_TABLINE
6242 /* When the GUI has the tabline then this always returns zero. */
6243 if (gui_use_tabline())
6244 return 0;
6245#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006246 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006247 {
6248 case 0: return 0;
6249 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6250 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006251 return 1;
6252}
6253
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254/*
6255 * Return the minimal number of rows that is needed on the screen to display
6256 * the current number of windows.
6257 */
6258 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006259min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260{
6261 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006262 tabpage_T *tp;
6263 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264
6265 if (firstwin == NULL) /* not initialized yet */
6266 return MIN_LINES;
6267
Bram Moolenaarf740b292006-02-16 22:11:02 +00006268 total = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02006269 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006270 {
6271 n = frame_minheight(tp->tp_topframe, NULL);
6272 if (total < n)
6273 total = n;
6274 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006275 total += tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006276 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 return total;
6278}
6279
6280/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006281 * Return TRUE if there is only one window (in the current tab page), not
6282 * counting a help or preview window, unless it is the current window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006283 * Does not count unlisted windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 */
6285 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006286only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 int count = 0;
6289 win_T *wp;
6290
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006291 /* If there is another tab page there always is another window. */
6292 if (first_tabpage->tp_next != NULL)
6293 return FALSE;
6294
Bram Moolenaar29323592016-07-24 22:04:11 +02006295 FOR_ALL_WINDOWS(wp)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006296 if (wp->w_buffer != NULL
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006297 && (!((bt_help(wp->w_buffer) && !bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298# ifdef FEAT_QUICKFIX
6299 || wp->w_p_pvw
6300# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006301 ) || wp == curwin) && wp != aucmd_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 ++count;
6303 return (count <= 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304}
6305
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306/*
6307 * Correct the cursor line number in other windows. Used after changing the
6308 * current buffer, and before applying autocommands.
6309 * When "do_curwin" is TRUE, also check current window.
6310 */
6311 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006312check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313{
6314 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006315 tabpage_T *tp;
6316
6317 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 {
Bram Moolenaara68e5952019-04-25 22:22:01 +02006320 // save the original cursor position and topline
6321 wp->w_save_cursor.w_cursor_save = wp->w_cursor;
6322 wp->w_save_cursor.w_topline_save = wp->w_topline;
6323
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6325 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6326 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6327 wp->w_topline = curbuf->b_ml.ml_line_count;
Bram Moolenaara68e5952019-04-25 22:22:01 +02006328
6329 // save the corrected cursor position and topline
6330 wp->w_save_cursor.w_cursor_corr = wp->w_cursor;
6331 wp->w_save_cursor.w_topline_corr = wp->w_topline;
6332 }
6333}
6334
6335/*
6336 * Reset cursor and topline to its stored values from check_lnums().
6337 * check_lnums() must have been called first!
6338 */
6339 void
6340reset_lnums()
6341{
6342 win_T *wp;
6343 tabpage_T *tp;
6344
6345 FOR_ALL_TAB_WINDOWS(tp, wp)
6346 if (wp->w_buffer == curbuf)
6347 {
6348 // Restore the value if the autocommand didn't change it.
6349 if (EQUAL_POS(wp->w_save_cursor.w_cursor_corr, wp->w_cursor))
6350 wp->w_cursor = wp->w_save_cursor.w_cursor_save;
6351 if (wp->w_save_cursor.w_topline_corr == wp->w_topline)
6352 wp->w_topline = wp->w_save_cursor.w_topline_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 }
6354}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355
6356/*
6357 * A snapshot of the window sizes, to restore them after closing the help
6358 * window.
6359 * Only these fields are used:
6360 * fr_layout
6361 * fr_width
6362 * fr_height
6363 * fr_next
6364 * fr_child
6365 * fr_win (only valid for the old curwin, NULL otherwise)
6366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367
6368/*
6369 * Create a snapshot of the current frame sizes.
6370 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006371 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006372make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006374 clear_snapshot(curtab, idx);
6375 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376}
6377
6378 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006379make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006381 *frp = ALLOC_CLEAR_ONE(frame_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 if (*frp == NULL)
6383 return;
6384 (*frp)->fr_layout = fr->fr_layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 (*frp)->fr_width = fr->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 (*frp)->fr_height = fr->fr_height;
6387 if (fr->fr_next != NULL)
6388 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6389 if (fr->fr_child != NULL)
6390 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6391 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6392 (*frp)->fr_win = curwin;
6393}
6394
6395/*
6396 * Remove any existing snapshot.
6397 */
6398 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006399clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006401 clear_snapshot_rec(tp->tp_snapshot[idx]);
6402 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403}
6404
6405 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006406clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407{
6408 if (fr != NULL)
6409 {
6410 clear_snapshot_rec(fr->fr_next);
6411 clear_snapshot_rec(fr->fr_child);
6412 vim_free(fr);
6413 }
6414}
6415
6416/*
6417 * Restore a previously created snapshot, if there is any.
6418 * This is only done if the screen size didn't change and the window layout is
6419 * still the same.
6420 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006421 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006422restore_snapshot(
6423 int idx,
6424 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425{
6426 win_T *wp;
6427
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006428 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006429 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006430 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6431 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006433 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 win_comp_pos();
6435 if (wp != NULL && close_curwin)
6436 win_goto(wp);
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01006437 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006439 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440}
6441
6442/*
6443 * Check if frames "sn" and "fr" have the same layout, same following frames
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006444 * and same children. And the window pointer is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 */
6446 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006447check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
6449 if (sn->fr_layout != fr->fr_layout
6450 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6451 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6452 || (sn->fr_next != NULL
6453 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6454 || (sn->fr_child != NULL
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006455 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01006456 || (sn->fr_win != NULL && !win_valid(sn->fr_win)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 return FAIL;
6458 return OK;
6459}
6460
6461/*
6462 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6463 * following frames and children.
6464 * Returns a pointer to the old current window, or NULL.
6465 */
6466 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006467restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
6469 win_T *wp = NULL;
6470 win_T *wp2;
6471
6472 fr->fr_height = sn->fr_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 fr->fr_width = sn->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 if (fr->fr_layout == FR_LEAF)
6475 {
6476 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006477 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 wp = sn->fr_win;
6479 }
6480 if (sn->fr_next != NULL)
6481 {
6482 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6483 if (wp2 != NULL)
6484 wp = wp2;
6485 }
6486 if (sn->fr_child != NULL)
6487 {
6488 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6489 if (wp2 != NULL)
6490 wp = wp2;
6491 }
6492 return wp;
6493}
6494
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006495#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6496 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006497/*
6498 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006499 * restore_win() MUST be called to undo, also when FAIL is returned.
6500 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006501 * When "no_display" is TRUE the display won't be affected, no redraw is
6502 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006503 * Returns FAIL if switching to "win" failed.
6504 */
6505 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006506switch_win(
Bram Moolenaar816968d2017-09-29 21:29:18 +02006507 win_T **save_curwin,
6508 tabpage_T **save_curtab,
6509 win_T *win,
6510 tabpage_T *tp,
6511 int no_display)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006512{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006513 block_autocmds();
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006514 return switch_win_noblock(save_curwin, save_curtab, win, tp, no_display);
6515}
6516
6517/*
6518 * As switch_win() but without blocking autocommands.
6519 */
6520 int
6521switch_win_noblock(
6522 win_T **save_curwin,
6523 tabpage_T **save_curtab,
6524 win_T *win,
6525 tabpage_T *tp,
6526 int no_display)
6527{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006528 *save_curwin = curwin;
6529 if (tp != NULL)
6530 {
6531 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006532 if (no_display)
6533 {
6534 curtab->tp_firstwin = firstwin;
6535 curtab->tp_lastwin = lastwin;
6536 curtab = tp;
6537 firstwin = curtab->tp_firstwin;
6538 lastwin = curtab->tp_lastwin;
6539 }
6540 else
6541 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006542 }
6543 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006544 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006545 curwin = win;
6546 curbuf = curwin->w_buffer;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006547 return OK;
6548}
6549
6550/*
6551 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006552 * When "no_display" is TRUE the display won't be affected, no redraw is
6553 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006554 */
6555 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006556restore_win(
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006557 win_T *save_curwin,
6558 tabpage_T *save_curtab,
6559 int no_display)
6560{
6561 restore_win_noblock(save_curwin, save_curtab, no_display);
6562 unblock_autocmds();
6563}
6564
6565/*
6566 * As restore_win() but without unblocking autocommands.
6567 */
6568 void
6569restore_win_noblock(
6570 win_T *save_curwin,
6571 tabpage_T *save_curtab,
6572 int no_display)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006573{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006574 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006575 {
6576 if (no_display)
6577 {
6578 curtab->tp_firstwin = firstwin;
6579 curtab->tp_lastwin = lastwin;
6580 curtab = save_curtab;
6581 firstwin = curtab->tp_firstwin;
6582 lastwin = curtab->tp_lastwin;
6583 }
6584 else
6585 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6586 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006587 if (win_valid(save_curwin))
6588 {
6589 curwin = save_curwin;
6590 curbuf = curwin->w_buffer;
6591 }
Bram Moolenaareea16992019-05-31 17:34:48 +02006592#ifdef FEAT_TEXT_PROP
6593 else if (bt_popup(curwin->w_buffer))
6594 // original window was closed and now we're in a popup window: Go
6595 // to the first valid window.
6596 win_goto(firstwin);
6597#endif
Bram Moolenaar105bc352013-05-17 16:03:57 +02006598}
6599
6600/*
6601 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6602 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6603 */
6604 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006605switch_buffer(bufref_T *save_curbuf, buf_T *buf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006606{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006607 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006608 set_bufref(save_curbuf, curbuf);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006609 --curbuf->b_nwindows;
6610 curbuf = buf;
6611 curwin->w_buffer = buf;
6612 ++curbuf->b_nwindows;
6613}
6614
6615/*
6616 * Restore the current buffer after using switch_buffer().
6617 */
6618 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006619restore_buffer(bufref_T *save_curbuf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006620{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006621 unblock_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006622 /* Check for valid buffer, just in case. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006623 if (bufref_valid(save_curbuf))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006624 {
6625 --curbuf->b_nwindows;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006626 curwin->w_buffer = save_curbuf->br_buf;
6627 curbuf = save_curbuf->br_buf;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006628 ++curbuf->b_nwindows;
6629 }
6630}
6631#endif
6632
Bram Moolenaar4033c552017-09-16 20:54:51 +02006633#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634/*
6635 * Return TRUE if there is any vertically split window.
6636 */
6637 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006638win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639{
6640 frame_T *fr;
6641
6642 if (topframe->fr_layout == FR_ROW)
6643 return TRUE;
6644
6645 if (topframe->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006646 FOR_ALL_FRAMES(fr, topframe->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 if (fr->fr_layout == FR_ROW)
6648 return TRUE;
6649
6650 return FALSE;
6651}
6652#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006653
6654#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6655/*
6656 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006657 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006658 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6659 * If no particular ID is desired, -1 must be specified for 'id'.
6660 * Return ID of added match, -1 on failure.
6661 */
6662 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006663match_add(
6664 win_T *wp,
6665 char_u *grp,
6666 char_u *pat,
6667 int prio,
6668 int id,
6669 list_T *pos_list,
6670 char_u *conceal_char UNUSED) /* pointer to conceal replacement char */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006671{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006672 matchitem_T *cur;
6673 matchitem_T *prev;
6674 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006675 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006676 regprog_T *regprog = NULL;
6677 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006678
Bram Moolenaarb3414592014-06-17 17:48:32 +02006679 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006680 return -1;
6681 if (id < -1 || id == 0)
6682 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006683 semsg(_("E799: Invalid ID: %d (must be greater than or equal to 1)"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006684 return -1;
6685 }
6686 if (id != -1)
6687 {
6688 cur = wp->w_match_head;
6689 while (cur != NULL)
6690 {
6691 if (cur->id == id)
6692 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006693 semsg(_("E801: ID already taken: %d"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006694 return -1;
6695 }
6696 cur = cur->next;
6697 }
6698 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006699 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006700 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006701 semsg(_(e_nogroup), grp);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006702 return -1;
6703 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006704 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006705 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006706 semsg(_(e_invarg2), pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006707 return -1;
6708 }
6709
6710 /* Find available match ID. */
6711 while (id == -1)
6712 {
6713 cur = wp->w_match_head;
6714 while (cur != NULL && cur->id != wp->w_next_match_id)
6715 cur = cur->next;
6716 if (cur == NULL)
6717 id = wp->w_next_match_id;
6718 wp->w_next_match_id++;
6719 }
6720
6721 /* Build new match. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006722 m = ALLOC_CLEAR_ONE(matchitem_T);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006723 m->id = id;
6724 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006725 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006726 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006727 m->match.regprog = regprog;
6728 m->match.rmm_ic = FALSE;
6729 m->match.rmm_maxcol = 0;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01006730# if defined(FEAT_CONCEAL)
Bram Moolenaar6561d522015-07-21 15:48:27 +02006731 m->conceal_char = 0;
6732 if (conceal_char != NULL)
6733 m->conceal_char = (*mb_ptr2char)(conceal_char);
Bram Moolenaar42356152016-03-31 22:27:40 +02006734# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006735
Bram Moolenaarb3414592014-06-17 17:48:32 +02006736 /* Set up position matches */
6737 if (pos_list != NULL)
6738 {
6739 linenr_T toplnum = 0;
6740 linenr_T botlnum = 0;
6741 listitem_T *li;
6742 int i;
6743
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006744 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006745 i++, li = li->li_next)
6746 {
6747 linenr_T lnum = 0;
6748 colnr_T col = 0;
6749 int len = 1;
6750 list_T *subl;
6751 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006752 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006753
Bram Moolenaarb3414592014-06-17 17:48:32 +02006754 if (li->li_tv.v_type == VAR_LIST)
6755 {
6756 subl = li->li_tv.vval.v_list;
6757 if (subl == NULL)
6758 goto fail;
6759 subli = subl->lv_first;
6760 if (subli == NULL)
6761 goto fail;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006762 lnum = tv_get_number_chk(&subli->li_tv, &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006763 if (error == TRUE)
6764 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006765 if (lnum == 0)
6766 {
6767 --i;
6768 continue;
6769 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006770 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006771 subli = subli->li_next;
6772 if (subli != NULL)
6773 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006774 col = tv_get_number_chk(&subli->li_tv, &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006775 if (error == TRUE)
6776 goto fail;
6777 subli = subli->li_next;
6778 if (subli != NULL)
6779 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006780 len = tv_get_number_chk(&subli->li_tv, &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006781 if (error == TRUE)
6782 goto fail;
6783 }
6784 }
6785 m->pos.pos[i].col = col;
6786 m->pos.pos[i].len = len;
6787 }
6788 else if (li->li_tv.v_type == VAR_NUMBER)
6789 {
6790 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006791 {
6792 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006793 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006794 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006795 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6796 m->pos.pos[i].col = 0;
6797 m->pos.pos[i].len = 0;
6798 }
6799 else
6800 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006801 emsg(_("List or number required"));
Bram Moolenaarb3414592014-06-17 17:48:32 +02006802 goto fail;
6803 }
6804 if (toplnum == 0 || lnum < toplnum)
6805 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006806 if (botlnum == 0 || lnum >= botlnum)
6807 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006808 }
6809
6810 /* Calculate top and bottom lines for redrawing area */
6811 if (toplnum != 0)
6812 {
6813 if (wp->w_buffer->b_mod_set)
6814 {
6815 if (wp->w_buffer->b_mod_top > toplnum)
6816 wp->w_buffer->b_mod_top = toplnum;
6817 if (wp->w_buffer->b_mod_bot < botlnum)
6818 wp->w_buffer->b_mod_bot = botlnum;
6819 }
6820 else
6821 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006822 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006823 wp->w_buffer->b_mod_top = toplnum;
6824 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006825 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006826 }
6827 m->pos.toplnum = toplnum;
6828 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006829 rtype = VALID;
6830 }
6831 }
6832
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006833 /* Insert new match. The match list is in ascending order with regard to
6834 * the match priorities. */
6835 cur = wp->w_match_head;
6836 prev = cur;
6837 while (cur != NULL && prio >= cur->priority)
6838 {
6839 prev = cur;
6840 cur = cur->next;
6841 }
6842 if (cur == prev)
6843 wp->w_match_head = m;
6844 else
6845 prev->next = m;
6846 m->next = cur;
6847
Bram Moolenaarb3414592014-06-17 17:48:32 +02006848 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006849 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006850
6851fail:
6852 vim_free(m);
6853 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006854}
6855
6856/*
6857 * Delete match with ID 'id' in the match list of window 'wp'.
6858 * Print error messages if 'perr' is TRUE.
6859 */
6860 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006861match_delete(win_T *wp, int id, int perr)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006862{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006863 matchitem_T *cur = wp->w_match_head;
6864 matchitem_T *prev = cur;
6865 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006866
6867 if (id < 1)
6868 {
6869 if (perr == TRUE)
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006870 semsg(_("E802: Invalid ID: %d (must be greater than or equal to 1)"),
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006871 id);
6872 return -1;
6873 }
6874 while (cur != NULL && cur->id != id)
6875 {
6876 prev = cur;
6877 cur = cur->next;
6878 }
6879 if (cur == NULL)
6880 {
6881 if (perr == TRUE)
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006882 semsg(_("E803: ID not found: %d"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006883 return -1;
6884 }
6885 if (cur == prev)
6886 wp->w_match_head = cur->next;
6887 else
6888 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006889 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006890 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006891 if (cur->pos.toplnum != 0)
6892 {
6893 if (wp->w_buffer->b_mod_set)
6894 {
6895 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
6896 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6897 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
6898 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
6899 }
6900 else
6901 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006902 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006903 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6904 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006905 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006906 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006907 rtype = VALID;
6908 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006909 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006910 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006911 return 0;
6912}
6913
6914/*
6915 * Delete all matches in the match list of window 'wp'.
6916 */
6917 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006918clear_matches(win_T *wp)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006919{
6920 matchitem_T *m;
6921
6922 while (wp->w_match_head != NULL)
6923 {
6924 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006925 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006926 vim_free(wp->w_match_head->pattern);
6927 vim_free(wp->w_match_head);
6928 wp->w_match_head = m;
6929 }
6930 redraw_later(SOME_VALID);
6931}
6932
6933/*
6934 * Get match from ID 'id' in window 'wp'.
6935 * Return NULL if match not found.
6936 */
6937 matchitem_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006938get_match(win_T *wp, int id)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006939{
6940 matchitem_T *cur = wp->w_match_head;
6941
6942 while (cur != NULL && cur->id != id)
6943 cur = cur->next;
6944 return cur;
6945}
6946#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02006947
6948#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6949 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006950get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02006951{
6952 int i = 1;
6953 win_T *w;
6954
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006955 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02006956 ++i;
6957
6958 if (w == NULL)
6959 return 0;
6960 else
6961 return i;
6962}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006963
6964 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006965get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006966{
6967 int i = 1;
6968 tabpage_T *t;
6969
6970 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
6971 ++i;
6972
6973 if (t == NULL)
6974 return 0;
6975 else
6976 return i;
6977}
Bram Moolenaar6d216452013-05-12 19:00:41 +02006978#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006979
6980/*
6981 * Return TRUE if "topfrp" and its children are at the right height.
6982 */
6983 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006984frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006985{
6986 frame_T *frp;
6987
6988 if (topfrp->fr_height != height)
6989 return FALSE;
6990
6991 if (topfrp->fr_layout == FR_ROW)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006992 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006993 if (frp->fr_height != height)
6994 return FALSE;
6995
6996 return TRUE;
6997}
6998
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006999/*
7000 * Return TRUE if "topfrp" and its children are at the right width.
7001 */
7002 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007003frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007004{
7005 frame_T *frp;
7006
7007 if (topfrp->fr_width != width)
7008 return FALSE;
7009
7010 if (topfrp->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01007011 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007012 if (frp->fr_width != width)
7013 return FALSE;
7014
7015 return TRUE;
7016}
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007017
Bram Moolenaar86edef62016-03-13 18:07:30 +01007018#if defined(FEAT_EVAL) || defined(PROTO)
7019 int
7020win_getid(typval_T *argvars)
7021{
7022 int winnr;
7023 win_T *wp;
7024
7025 if (argvars[0].v_type == VAR_UNKNOWN)
7026 return curwin->w_id;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007027 winnr = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01007028 if (winnr > 0)
7029 {
7030 if (argvars[1].v_type == VAR_UNKNOWN)
7031 wp = firstwin;
7032 else
7033 {
7034 tabpage_T *tp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007035 int tabnr = tv_get_number(&argvars[1]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01007036
Bram Moolenaar29323592016-07-24 22:04:11 +02007037 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007038 if (--tabnr == 0)
7039 break;
7040 if (tp == NULL)
7041 return -1;
Bram Moolenaar8e639052016-11-13 14:31:40 +01007042 if (tp == curtab)
7043 wp = firstwin;
7044 else
7045 wp = tp->tp_firstwin;
Bram Moolenaar86edef62016-03-13 18:07:30 +01007046 }
7047 for ( ; wp != NULL; wp = wp->w_next)
7048 if (--winnr == 0)
7049 return wp->w_id;
7050 }
7051 return 0;
7052}
7053
7054 int
7055win_gotoid(typval_T *argvars)
7056{
7057 win_T *wp;
7058 tabpage_T *tp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007059 int id = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01007060
Bram Moolenaar29323592016-07-24 22:04:11 +02007061 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007062 if (wp->w_id == id)
7063 {
7064 goto_tabpage_win(tp, wp);
7065 return 1;
7066 }
7067 return 0;
7068}
7069
7070 void
7071win_id2tabwin(typval_T *argvars, list_T *list)
7072{
7073 win_T *wp;
7074 tabpage_T *tp;
7075 int winnr = 1;
7076 int tabnr = 1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007077 int id = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01007078
Bram Moolenaar29323592016-07-24 22:04:11 +02007079 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007080 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007081 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007082 {
7083 if (wp->w_id == id)
7084 {
7085 list_append_number(list, tabnr);
7086 list_append_number(list, winnr);
7087 return;
7088 }
7089 ++winnr;
7090 }
7091 ++tabnr;
7092 winnr = 1;
7093 }
7094 list_append_number(list, 0);
7095 list_append_number(list, 0);
7096}
7097
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007098 win_T *
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01007099win_id2wp(int id)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007100{
7101 win_T *wp;
7102 tabpage_T *tp;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007103
7104 FOR_ALL_TAB_WINDOWS(tp, wp)
7105 if (wp->w_id == id)
7106 return wp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02007107#ifdef FEAT_TEXT_PROP
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007108 // popup windows are in separate lists
Bram Moolenaar4d784b22019-05-25 19:51:39 +02007109 FOR_ALL_TABPAGES(tp)
7110 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
7111 if (wp->w_id == id)
7112 return wp;
7113 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
7114 if (wp->w_id == id)
7115 return wp;
7116#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007117
7118 return NULL;
7119}
7120
Bram Moolenaar86edef62016-03-13 18:07:30 +01007121 int
7122win_id2win(typval_T *argvars)
7123{
7124 win_T *wp;
7125 int nr = 1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007126 int id = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01007127
Bram Moolenaar29323592016-07-24 22:04:11 +02007128 FOR_ALL_WINDOWS(wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007129 {
7130 if (wp->w_id == id)
7131 return nr;
7132 ++nr;
7133 }
7134 return 0;
7135}
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007136
7137 void
7138win_findbuf(typval_T *argvars, list_T *list)
7139{
7140 win_T *wp;
7141 tabpage_T *tp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007142 int bufnr = tv_get_number(&argvars[0]);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007143
Bram Moolenaar29323592016-07-24 22:04:11 +02007144 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007145 if (wp->w_buffer->b_fnum == bufnr)
7146 list_append_number(list, wp->w_id);
7147}
7148
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02007149/*
7150 * Get the layout of the given tab page for winlayout().
7151 */
7152 void
7153get_framelayout(frame_T *fr, list_T *l, int outer)
7154{
7155 frame_T *child;
7156 list_T *fr_list;
7157 list_T *win_list;
7158
7159 if (fr == NULL)
7160 return;
7161
7162 if (outer)
7163 // outermost call from f_winlayout()
7164 fr_list = l;
7165 else
7166 {
7167 fr_list = list_alloc();
7168 if (fr_list == NULL)
7169 return;
7170 list_append_list(l, fr_list);
7171 }
7172
7173 if (fr->fr_layout == FR_LEAF)
7174 {
7175 if (fr->fr_win != NULL)
7176 {
7177 list_append_string(fr_list, (char_u *)"leaf", -1);
7178 list_append_number(fr_list, fr->fr_win->w_id);
7179 }
7180 }
7181 else
7182 {
7183 list_append_string(fr_list,
7184 fr->fr_layout == FR_ROW ? (char_u *)"row" : (char_u *)"col", -1);
7185
7186 win_list = list_alloc();
7187 if (win_list == NULL)
7188 return;
7189 list_append_list(fr_list, win_list);
7190 child = fr->fr_child;
7191 while (child != NULL)
7192 {
7193 get_framelayout(child, win_list, FALSE);
7194 child = child->fr_next;
7195 }
7196 }
7197}
Bram Moolenaar86edef62016-03-13 18:07:30 +01007198#endif