blob: e8e816b838dfd5e927820fa1f1f44f473ab54644 [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 int path_is_url(char_u *p);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010013static void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize, long Prenum);
14static void win_init(win_T *newp, win_T *oldp, int flags);
15static void win_init_some(win_T *newp, win_T *oldp);
16static void frame_comp_pos(frame_T *topfrp, int *row, int *col);
17static void frame_setheight(frame_T *curfrp, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010018static void frame_setwidth(frame_T *curfrp, int width);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010019static void win_exchange(long);
20static void win_rotate(int, int);
21static void win_totop(int size, int flags);
22static 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 +010023static win_T *win_free_mem(win_T *win, int *dirp, tabpage_T *tp);
24static frame_T *win_altframe(win_T *win, tabpage_T *tp);
25static tabpage_T *alt_tabpage(void);
26static win_T *frame2win(frame_T *frp);
27static int frame_has_win(frame_T *frp, win_T *wp);
28static void frame_new_height(frame_T *topfrp, int height, int topfirst, int wfh);
29static int frame_fixed_height(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010030static int frame_fixed_width(frame_T *frp);
31static void frame_add_statusline(frame_T *frp);
32static void frame_new_width(frame_T *topfrp, int width, int leftfirst, int wfw);
33static void frame_add_vsep(frame_T *frp);
34static int frame_minwidth(frame_T *topfrp, win_T *next_curwin);
35static void frame_fix_width(win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010036static int win_alloc_firstwin(win_T *oldwin);
37static void new_frame(win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010038static tabpage_T *alloc_tabpage(void);
39static int leave_tabpage(buf_T *new_curbuf, int trigger_leave_autocmds);
40static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds);
41static void frame_fix_height(win_T *wp);
42static int frame_minheight(frame_T *topfrp, win_T *next_curwin);
Bram Moolenaarc917da42016-07-19 22:31:36 +020043static void win_enter_ext(win_T *wp, int undo_sync, int no_curwin, int trigger_new_autocmds, int trigger_enter_autocmds, int trigger_leave_autocmds);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010044static void win_free(win_T *wp, tabpage_T *tp);
45static void frame_append(frame_T *after, frame_T *frp);
46static void frame_insert(frame_T *before, frame_T *frp);
47static void frame_remove(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010048static void win_goto_ver(int up, long count);
49static void win_goto_hor(int left, long count);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010050static void frame_add_height(frame_T *frp, int n);
51static void last_status_rec(frame_T *fr, int statusline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010053static void make_snapshot_rec(frame_T *fr, frame_T **frp);
54static void clear_snapshot(tabpage_T *tp, int idx);
55static void clear_snapshot_rec(frame_T *fr);
56static int check_snapshot_rec(frame_T *sn, frame_T *fr);
57static win_T *restore_snapshot_rec(frame_T *sn, frame_T *fr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010059static int frame_check_height(frame_T *topfrp, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static int frame_check_width(frame_T *topfrp, int width);
Bram Moolenaarb893ac22013-06-26 14:04:47 +020061
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010062static win_T *win_alloc(win_T *after, int hidden);
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
64#define URL_SLASH 1 /* path_is_url() has found "://" */
65#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
66
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000067#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaar4033c552017-09-16 20:54:51 +020069#define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000070
71static char *m_onlyone = N_("Already only one window");
72
Bram Moolenaar071d4272004-06-13 20:20:40 +000073/*
74 * all CTRL-W window commands are handled here, called from normal_cmd().
75 */
76 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +010077do_window(
78 int nchar,
79 long Prenum,
80 int xchar) /* extra char from ":wincmd gx" or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081{
82 long Prenum1;
83 win_T *wp;
84#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
85 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000086 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#endif
88#ifdef FEAT_FIND_ID
89 int type = FIND_DEFINE;
90 int len;
91#endif
92 char_u cbuf[40];
93
94 if (Prenum == 0)
95 Prenum1 = 1;
96 else
97 Prenum1 = Prenum;
98
99#ifdef FEAT_CMDWIN
Bram Moolenaar6f470022018-04-10 18:47:20 +0200100# define CHECK_CMDWIN \
101 do { \
102 if (cmdwin_type != 0) \
103 { \
104 EMSG(_(e_cmdwin)); \
105 return; \
106 } \
107 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#else
Bram Moolenaar6f470022018-04-10 18:47:20 +0200109# define CHECK_CMDWIN do { /**/ } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110#endif
111
112 switch (nchar)
113 {
114/* split current window in two parts, horizontally */
115 case 'S':
116 case Ctrl_S:
117 case 's':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200118 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000120#ifdef FEAT_QUICKFIX
121 /* When splitting the quickfix window open a new buffer in it,
122 * don't replicate the quickfix buffer. */
123 if (bt_quickfix(curbuf))
124 goto newwindow;
125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#ifdef FEAT_GUI
127 need_mouse_correct = TRUE;
128#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200129 (void)win_split((int)Prenum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 break;
131
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132/* split current window in two parts, vertically */
133 case Ctrl_V:
134 case 'v':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200135 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100137#ifdef FEAT_QUICKFIX
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000138 /* When splitting the quickfix window open a new buffer in it,
139 * don't replicate the quickfix buffer. */
140 if (bt_quickfix(curbuf))
141 goto newwindow;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100142#endif
143#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100145#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200146 (void)win_split((int)Prenum, WSP_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
149/* split current window and edit alternate file */
150 case Ctrl_HAT:
151 case '^':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200152 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100154
155 if (buflist_findnr(Prenum == 0
156 ? curwin->w_alt_fnum : Prenum) == NULL)
157 {
158 if (Prenum == 0)
159 EMSG(_(e_noalt));
160 else
161 EMSGN(_("E92: Buffer %ld not found"), Prenum);
162 break;
163 }
164
165 if (!curbuf_locked() && win_split(0, 0) == OK)
166 (void)buflist_getfile(
167 Prenum == 0 ? curwin->w_alt_fnum : Prenum,
168 (linenr_T)0, GETF_ALT, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 break;
170
171/* open new window */
172 case Ctrl_N:
173 case 'n':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200174 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000176#ifdef FEAT_QUICKFIX
177newwindow:
178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000180 /* window height */
181 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 else
183 cbuf[0] = NUL;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100184#if defined(FEAT_QUICKFIX)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000185 if (nchar == 'v' || nchar == Ctrl_V)
186 STRCAT(cbuf, "v");
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 STRCAT(cbuf, "new");
189 do_cmdline_cmd(cbuf);
190 break;
191
192/* quit current window */
193 case Ctrl_Q:
194 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100196 cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100197 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 break;
199
200/* close current window */
201 case Ctrl_C:
202 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100204 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100205 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 break;
207
Bram Moolenaar4033c552017-09-16 20:54:51 +0200208#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209/* close preview window */
210 case Ctrl_Z:
211 case 'z':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200212 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 do_cmdline_cmd((char_u *)"pclose");
215 break;
216
217/* cursor to preview window */
218 case 'P':
Bram Moolenaar29323592016-07-24 22:04:11 +0200219 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 if (wp->w_p_pvw)
221 break;
222 if (wp == NULL)
223 EMSG(_("E441: There is no preview window"));
224 else
225 win_goto(wp);
226 break;
227#endif
228
229/* close all but current window */
230 case Ctrl_O:
231 case 'o':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200232 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100234 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100235 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 break;
237
238/* cursor to next window with wrap around */
239 case Ctrl_W:
240 case 'w':
241/* cursor to previous window with wrap around */
242 case 'W':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200243 CHECK_CMDWIN;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +0100244 if (ONE_WINDOW && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 beep_flush();
246 else
247 {
248 if (Prenum) /* go to specified window */
249 {
250 for (wp = firstwin; --Prenum > 0; )
251 {
252 if (wp->w_next == NULL)
253 break;
254 else
255 wp = wp->w_next;
256 }
257 }
258 else
259 {
260 if (nchar == 'W') /* go to previous window */
261 {
262 wp = curwin->w_prev;
263 if (wp == NULL)
264 wp = lastwin; /* wrap around */
265 }
266 else /* go to next window */
267 {
268 wp = curwin->w_next;
269 if (wp == NULL)
270 wp = firstwin; /* wrap around */
271 }
272 }
273 win_goto(wp);
274 }
275 break;
276
277/* cursor to window below */
278 case 'j':
279 case K_DOWN:
280 case Ctrl_J:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200281 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 win_goto_ver(FALSE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 break;
284
285/* cursor to window above */
286 case 'k':
287 case K_UP:
288 case Ctrl_K:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200289 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 win_goto_ver(TRUE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 break;
292
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293/* cursor to left window */
294 case 'h':
295 case K_LEFT:
296 case Ctrl_H:
297 case K_BS:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200298 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 win_goto_hor(TRUE, Prenum1);
300 break;
301
302/* cursor to right window */
303 case 'l':
304 case K_RIGHT:
305 case Ctrl_L:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200306 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 win_goto_hor(FALSE, Prenum1);
308 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000310/* move window to new tab page */
311 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000312 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000313 MSG(_(m_onlyone));
314 else
315 {
316 tabpage_T *oldtab = curtab;
317 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000318
319 /* First create a new tab with the window, then go back to
320 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000321 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000322 if (win_new_tabpage((int)Prenum) == OK
323 && valid_tabpage(oldtab))
324 {
325 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200326 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000327 if (curwin == wp)
328 win_close(curwin, FALSE);
329 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200330 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000331 }
332 }
333 break;
334
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335/* cursor to top-left window */
336 case 't':
337 case Ctrl_T:
338 win_goto(firstwin);
339 break;
340
341/* cursor to bottom-right window */
342 case 'b':
343 case Ctrl_B:
344 win_goto(lastwin);
345 break;
346
347/* cursor to last accessed (previous) window */
348 case 'p':
349 case Ctrl_P:
Bram Moolenaar3dda7db2016-04-03 21:22:58 +0200350 if (!win_valid(prevwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 beep_flush();
352 else
353 win_goto(prevwin);
354 break;
355
356/* exchange current and next window */
357 case 'x':
358 case Ctrl_X:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200359 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 win_exchange(Prenum);
361 break;
362
363/* rotate windows downwards */
364 case Ctrl_R:
365 case 'r':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200366 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 win_rotate(FALSE, (int)Prenum1); /* downwards */
369 break;
370
371/* rotate windows upwards */
372 case 'R':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200373 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 win_rotate(TRUE, (int)Prenum1); /* upwards */
376 break;
377
378/* move window to the very top/bottom/left/right */
379 case 'K':
380 case 'J':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 case 'H':
382 case 'L':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200383 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 win_totop((int)Prenum,
385 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
386 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
387 break;
388
389/* make all windows the same height */
390 case '=':
391#ifdef FEAT_GUI
392 need_mouse_correct = TRUE;
393#endif
394 win_equal(NULL, FALSE, 'b');
395 break;
396
397/* increase current window height */
398 case '+':
399#ifdef FEAT_GUI
400 need_mouse_correct = TRUE;
401#endif
402 win_setheight(curwin->w_height + (int)Prenum1);
403 break;
404
405/* decrease current window height */
406 case '-':
407#ifdef FEAT_GUI
408 need_mouse_correct = TRUE;
409#endif
410 win_setheight(curwin->w_height - (int)Prenum1);
411 break;
412
413/* set current window height */
414 case Ctrl__:
415 case '_':
416#ifdef FEAT_GUI
417 need_mouse_correct = TRUE;
418#endif
419 win_setheight(Prenum ? (int)Prenum : 9999);
420 break;
421
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422/* increase current window width */
423 case '>':
424#ifdef FEAT_GUI
425 need_mouse_correct = TRUE;
426#endif
427 win_setwidth(curwin->w_width + (int)Prenum1);
428 break;
429
430/* decrease current window width */
431 case '<':
432#ifdef FEAT_GUI
433 need_mouse_correct = TRUE;
434#endif
435 win_setwidth(curwin->w_width - (int)Prenum1);
436 break;
437
438/* set current window width */
439 case '|':
440#ifdef FEAT_GUI
441 need_mouse_correct = TRUE;
442#endif
443 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
444 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445
446/* jump to tag and split window if tag exists (in preview window) */
447#if defined(FEAT_QUICKFIX)
448 case '}':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200449 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 if (Prenum)
451 g_do_tagpreview = Prenum;
452 else
453 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200455 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 case ']':
457 case Ctrl_RSB:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200458 CHECK_CMDWIN;
Bram Moolenaard355c502014-09-23 13:48:43 +0200459 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 if (Prenum)
461 postponed_split = Prenum;
462 else
463 postponed_split = -1;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200464#ifdef FEAT_QUICKFIX
Bram Moolenaar56095e12014-10-09 10:44:37 +0200465 if (nchar != '}')
466 g_do_tagpreview = 0;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468
Bram Moolenaard355c502014-09-23 13:48:43 +0200469 /* Execute the command right here, required when "wincmd ]"
470 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 do_nv_ident(Ctrl_RSB, NUL);
472 break;
473
474#ifdef FEAT_SEARCHPATH
475/* edit file name under cursor in a new window */
476 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000477 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000479wingotofile:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200480 CHECK_CMDWIN;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000481
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000482 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 if (ptr != NULL)
484 {
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200485 tabpage_T *oldtab = curtab;
486 win_T *oldwin = curwin;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000487# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000489# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 setpcmark();
491 if (win_split(0, 0) == OK)
492 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200493 RESET_BINDING(curwin);
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200494 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
495 ECMD_HIDE, NULL) == FAIL)
496 {
497 /* Failed to open the file, close the window
498 * opened for it. */
499 win_close(curwin, FALSE);
500 goto_tabpage_win(oldtab, oldwin);
501 }
502 else if (nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000503 {
504 curwin->w_cursor.lnum = lnum;
505 check_cursor_lnum();
506 beginline(BL_SOL | BL_FIX);
507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 }
509 vim_free(ptr);
510 }
511 break;
512#endif
513
514#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000515/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 * new window -- webb
517 */
518 case 'i': /* Go to any match */
519 case Ctrl_I:
520 type = FIND_ANY;
521 /* FALLTHROUGH */
522 case 'd': /* Go to definition, using 'define' */
523 case Ctrl_D:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200524 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
526 break;
527 find_pattern_in_path(ptr, 0, len, TRUE,
528 Prenum == 0 ? TRUE : FALSE, type,
529 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
530 curwin->w_set_curswant = TRUE;
531 break;
532#endif
533
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200534/* Quickfix window only: view the result under the cursor in a new split. */
535#if defined(FEAT_QUICKFIX)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000536 case K_KENTER:
537 case CAR:
Bram Moolenaar05159a02005-02-26 23:04:13 +0000538 if (bt_quickfix(curbuf))
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200539 qf_view_result(TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000540 break;
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200541#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000542
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543/* CTRL-W g extended commands */
544 case 'g':
545 case Ctrl_G:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200546 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547#ifdef USE_ON_FLY_SCROLL
548 dont_scroll = TRUE; /* disallow scrolling here */
549#endif
550 ++no_mapping;
551 ++allow_keys; /* no mapping for xchar, but allow key codes */
552 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000553 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 --no_mapping;
556 --allow_keys;
557#ifdef FEAT_CMDL_INFO
558 (void)add_to_showcmd(xchar);
559#endif
560 switch (xchar)
561 {
562#if defined(FEAT_QUICKFIX)
563 case '}':
564 xchar = Ctrl_RSB;
565 if (Prenum)
566 g_do_tagpreview = Prenum;
567 else
568 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200570 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 case ']':
572 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200573 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 if (Prenum)
575 postponed_split = Prenum;
576 else
577 postponed_split = -1;
578
579 /* Execute the command right here, required when
580 * "wincmd g}" was used in a function. */
581 do_nv_ident('g', xchar);
582 break;
583
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000584#ifdef FEAT_SEARCHPATH
585 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000586 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100587 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000588 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000589 goto wingotofile;
590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 default:
592 beep_flush();
593 break;
594 }
595 break;
596
597 default: beep_flush();
598 break;
599 }
600}
601
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100602/*
603 * Figure out the address type for ":wnncmd".
604 */
605 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100606get_wincmd_addr_type(char_u *arg, exarg_T *eap)
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100607{
608 switch (*arg)
609 {
610 case 'S':
611 case Ctrl_S:
612 case 's':
613 case Ctrl_N:
614 case 'n':
615 case 'j':
616 case Ctrl_J:
617 case 'k':
618 case Ctrl_K:
619 case 'T':
620 case Ctrl_R:
621 case 'r':
622 case 'R':
623 case 'K':
624 case 'J':
625 case '+':
626 case '-':
627 case Ctrl__:
628 case '_':
629 case '|':
630 case ']':
631 case Ctrl_RSB:
632 case 'g':
633 case Ctrl_G:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100634 case Ctrl_V:
635 case 'v':
636 case 'h':
637 case Ctrl_H:
638 case 'l':
639 case Ctrl_L:
640 case 'H':
641 case 'L':
642 case '>':
643 case '<':
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100644#if defined(FEAT_QUICKFIX)
645 case '}':
646#endif
647#ifdef FEAT_SEARCHPATH
648 case 'f':
649 case 'F':
650 case Ctrl_F:
651#endif
652#ifdef FEAT_FIND_ID
653 case 'i':
654 case Ctrl_I:
655 case 'd':
656 case Ctrl_D:
657#endif
658 /* window size or any count */
659 eap->addr_type = ADDR_LINES;
660 break;
661
662 case Ctrl_HAT:
663 case '^':
664 /* buffer number */
665 eap->addr_type = ADDR_BUFFERS;
666 break;
667
668 case Ctrl_Q:
669 case 'q':
670 case Ctrl_C:
671 case 'c':
672 case Ctrl_O:
673 case 'o':
674 case Ctrl_W:
675 case 'w':
676 case 'W':
677 case 'x':
678 case Ctrl_X:
679 /* window number */
680 eap->addr_type = ADDR_WINDOWS;
681 break;
682
683#if defined(FEAT_QUICKFIX)
684 case Ctrl_Z:
685 case 'z':
686 case 'P':
687#endif
688 case 't':
689 case Ctrl_T:
690 case 'b':
691 case Ctrl_B:
692 case 'p':
693 case Ctrl_P:
694 case '=':
695 case CAR:
696 /* no count */
697 eap->addr_type = 0;
698 break;
699 }
700}
701
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100702 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100703cmd_with_count(
704 char *cmd,
705 char_u *bufp,
706 size_t bufsize,
707 long Prenum)
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100708{
709 size_t len = STRLEN(cmd);
710
711 STRCPY(bufp, cmd);
712 if (Prenum > 0)
713 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
714}
715
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716/*
717 * split the current window, implements CTRL-W s and :split
718 *
719 * "size" is the height or width for the new window, 0 to use half of current
720 * height or width.
721 *
722 * "flags":
723 * WSP_ROOM: require enough room for new window
724 * WSP_VERT: vertical split.
725 * WSP_TOP: open window at the top-left of the shell (help window).
726 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
727 * WSP_HELP: creating the help window, keep layout snapshot
728 *
729 * return FAIL for failure, OK otherwise
730 */
731 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100732win_split(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000734 /* When the ":tab" modifier was used open a new tab page instead. */
735 if (may_open_tabpage() == OK)
736 return OK;
737
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 /* Add flags from ":vertical", ":topleft" and ":botright". */
739 flags |= cmdmod.split;
740 if ((flags & WSP_TOP) && (flags & WSP_BOT))
741 {
742 EMSG(_("E442: Can't split topleft and botright at the same time"));
743 return FAIL;
744 }
745
746 /* When creating the help window make a snapshot of the window layout.
747 * Otherwise clear the snapshot, it's now invalid. */
748 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000749 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000751 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752
753 return win_split_ins(size, flags, NULL, 0);
754}
755
756/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100757 * When "new_wp" is NULL: split the current window in two.
758 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 * top/left/right/bottom.
760 * return FAIL for failure, OK otherwise
761 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000762 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100763win_split_ins(
764 int size,
765 int flags,
766 win_T *new_wp,
767 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100769 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 win_T *oldwin;
771 int new_size = size;
772 int i;
773 int need_status = 0;
774 int do_equal = FALSE;
775 int needed;
776 int available;
777 int oldwin_height = 0;
778 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200779 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200781 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200782 int wmh1;
Bram Moolenaar98da6ec2018-04-13 22:15:46 +0200783 int did_set_fraction = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784
785 if (flags & WSP_TOP)
786 oldwin = firstwin;
787 else if (flags & WSP_BOT)
788 oldwin = lastwin;
789 else
790 oldwin = curwin;
791
792 /* add a status line when p_ls == 1 and splitting the first window */
Bram Moolenaar459ca562016-11-10 18:16:33 +0100793 if (ONE_WINDOW && p_ls == 1 && oldwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 {
Bram Moolenaar415a6932017-12-05 20:31:07 +0100795 if (VISIBLE_HEIGHT(oldwin) <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
797 EMSG(_(e_noroom));
798 return FAIL;
799 }
800 need_status = STATUS_HEIGHT;
801 }
802
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000803#ifdef FEAT_GUI
804 /* May be needed for the scrollbars that are going to change. */
805 if (gui.in_use)
806 out_flush();
807#endif
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 if (flags & WSP_VERT)
810 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200811 int wmw1;
812 int minwidth;
813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
816 /*
817 * Check if we are able to split the current window and compute its
818 * width.
819 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200820 /* Current window requires at least 1 space. */
821 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
822 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200824 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200825 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200827 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200829 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200831 else if (p_ea)
832 {
833 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
834 prevfrp = oldwin->w_frame;
835 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
836 frp = frp->fr_parent)
837 {
838 if (frp->fr_layout == FR_ROW)
839 for (frp2 = frp->fr_child; frp2 != NULL;
840 frp2 = frp2->fr_next)
841 if (frp2 != prevfrp)
842 minwidth += frame_minwidth(frp2, NOWIN);
843 prevfrp = frp;
844 }
845 available = topframe->fr_width;
846 needed += minwidth;
847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200849 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200850 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
851 available = oldwin->w_frame->fr_width;
852 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200853 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100854 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
856 EMSG(_(e_noroom));
857 return FAIL;
858 }
859 if (new_size == 0)
860 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200861 if (new_size > available - minwidth - 1)
862 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200863 if (new_size < wmw1)
864 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866 /* if it doesn't fit in the current window, need win_equal() */
867 if (oldwin->w_width - new_size - 1 < p_wmw)
868 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000869
870 /* We don't like to take lines for the new window from a
871 * 'winfixwidth' window. Take them from a window to the left or right
Bram Moolenaar38e34832017-03-19 20:22:36 +0100872 * instead, if possible. Add one for the separator. */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000873 if (oldwin->w_p_wfw)
Bram Moolenaar38e34832017-03-19 20:22:36 +0100874 win_setwidth_win(oldwin->w_width + new_size + 1, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000875
876 /* Only make all windows the same width if one of them (except oldwin)
877 * is wider than one of the split windows. */
878 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
879 && oldwin->w_frame->fr_parent != NULL)
880 {
881 frp = oldwin->w_frame->fr_parent->fr_child;
882 while (frp != NULL)
883 {
884 if (frp->fr_win != oldwin && frp->fr_win != NULL
885 && (frp->fr_win->w_width > new_size
886 || frp->fr_win->w_width > oldwin->w_width
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100887 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000888 {
889 do_equal = TRUE;
890 break;
891 }
892 frp = frp->fr_next;
893 }
894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 }
896 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 {
898 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
900 /*
901 * Check if we are able to split the current window and compute its
902 * height.
903 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200904 /* Current window requires at least 1 space. */
Bram Moolenaar415a6932017-12-05 20:31:07 +0100905 wmh1 = (p_wmh == 0 ? 1 : p_wmh) + WINBAR_HEIGHT(curwin);
Bram Moolenaar1f538352014-07-16 18:19:27 +0200906 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200908 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200909 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200911 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200913 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200915 else if (p_ea)
916 {
917 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
918 prevfrp = oldwin->w_frame;
919 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
920 frp = frp->fr_parent)
921 {
922 if (frp->fr_layout == FR_COL)
923 for (frp2 = frp->fr_child; frp2 != NULL;
924 frp2 = frp2->fr_next)
925 if (frp2 != prevfrp)
926 minheight += frame_minheight(frp2, NOWIN);
927 prevfrp = frp;
928 }
929 available = topframe->fr_height;
930 needed += minheight;
931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 else
933 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200934 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
935 available = oldwin->w_frame->fr_height;
936 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100938 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {
940 EMSG(_(e_noroom));
941 return FAIL;
942 }
943 oldwin_height = oldwin->w_height;
944 if (need_status)
945 {
946 oldwin->w_status_height = STATUS_HEIGHT;
947 oldwin_height -= STATUS_HEIGHT;
948 }
949 if (new_size == 0)
950 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200951 if (new_size > available - minheight - STATUS_HEIGHT)
952 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200953 if (new_size < wmh1)
954 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955
956 /* if it doesn't fit in the current window, need win_equal() */
957 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
958 do_equal = TRUE;
959
960 /* We don't like to take lines for the new window from a
961 * 'winfixheight' window. Take them from a window above or below
962 * instead, if possible. */
963 if (oldwin->w_p_wfh)
964 {
Bram Moolenaar98da6ec2018-04-13 22:15:46 +0200965 /* Set w_fraction now so that the cursor keeps the same relative
966 * vertical position using the old height. */
967 set_fraction(oldwin);
968 did_set_fraction = TRUE;
969
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
971 oldwin);
972 oldwin_height = oldwin->w_height;
973 if (need_status)
974 oldwin_height -= STATUS_HEIGHT;
975 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000976
977 /* Only make all windows the same height if one of them (except oldwin)
978 * is higher than one of the split windows. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100979 if (!do_equal && p_ea && size == 0 && *p_ead != 'h'
Bram Moolenaar67f71312007-08-12 14:55:56 +0000980 && oldwin->w_frame->fr_parent != NULL)
981 {
982 frp = oldwin->w_frame->fr_parent->fr_child;
983 while (frp != NULL)
984 {
985 if (frp->fr_win != oldwin && frp->fr_win != NULL
986 && (frp->fr_win->w_height > new_size
987 || frp->fr_win->w_height > oldwin_height - new_size
988 - STATUS_HEIGHT))
989 {
990 do_equal = TRUE;
991 break;
992 }
993 frp = frp->fr_next;
994 }
995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 }
997
998 /*
999 * allocate new window structure and link it in the window list
1000 */
1001 if ((flags & WSP_TOP) == 0
1002 && ((flags & WSP_BOT)
1003 || (flags & WSP_BELOW)
1004 || (!(flags & WSP_ABOVE)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001005 && ( (flags & WSP_VERT) ? p_spr : p_sb))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 {
1007 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001008 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001009 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 else
1011 win_append(oldwin, wp);
1012 }
1013 else
1014 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001015 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001016 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 else
1018 win_append(oldwin->w_prev, wp);
1019 }
1020
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001021 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
1023 if (wp == NULL)
1024 return FAIL;
1025
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001026 new_frame(wp);
1027 if (wp->w_frame == NULL)
1028 {
1029 win_free(wp, NULL);
1030 return FAIL;
1031 }
1032
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001033 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001034 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 }
1036
1037 /*
1038 * Reorganise the tree of frames to insert the new window.
1039 */
1040 if (flags & (WSP_TOP | WSP_BOT))
1041 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1043 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 {
1045 curfrp = topframe->fr_child;
1046 if (flags & WSP_BOT)
1047 while (curfrp->fr_next != NULL)
1048 curfrp = curfrp->fr_next;
1049 }
1050 else
1051 curfrp = topframe;
1052 before = (flags & WSP_TOP);
1053 }
1054 else
1055 {
1056 curfrp = oldwin->w_frame;
1057 if (flags & WSP_BELOW)
1058 before = FALSE;
1059 else if (flags & WSP_ABOVE)
1060 before = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001061 else if (flags & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 before = !p_spr;
1063 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 before = !p_sb;
1065 }
1066 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1067 {
1068 /* Need to create a new frame in the tree to make a branch. */
1069 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
1070 *frp = *curfrp;
1071 curfrp->fr_layout = layout;
1072 frp->fr_parent = curfrp;
1073 frp->fr_next = NULL;
1074 frp->fr_prev = NULL;
1075 curfrp->fr_child = frp;
1076 curfrp->fr_win = NULL;
1077 curfrp = frp;
1078 if (frp->fr_win != NULL)
1079 oldwin->w_frame = frp;
1080 else
1081 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1082 frp->fr_parent = curfrp;
1083 }
1084
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001085 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001086 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001088 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 frp->fr_parent = curfrp->fr_parent;
1090
1091 /* Insert the new frame at the right place in the frame list. */
1092 if (before)
1093 frame_insert(curfrp, frp);
1094 else
1095 frame_append(curfrp, frp);
1096
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001097 /* Set w_fraction now so that the cursor keeps the same relative
1098 * vertical position. */
Bram Moolenaar98da6ec2018-04-13 22:15:46 +02001099 if (!did_set_fraction)
1100 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001101 wp->w_fraction = oldwin->w_fraction;
1102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 if (flags & WSP_VERT)
1104 {
1105 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 if (need_status)
1108 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001109 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 oldwin->w_status_height = need_status;
1111 }
1112 if (flags & (WSP_TOP | WSP_BOT))
1113 {
1114 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001115 wp->w_winrow = tabline_height();
Bram Moolenaard326ad62017-09-18 20:31:41 +02001116 win_new_height(wp, curfrp->fr_height - (p_ls > 0)
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001117 - WINBAR_HEIGHT(wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 wp->w_status_height = (p_ls > 0);
1119 }
1120 else
1121 {
1122 /* height and row of new window is same as current window */
1123 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01001124 win_new_height(wp, VISIBLE_HEIGHT(oldwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 wp->w_status_height = oldwin->w_status_height;
1126 }
1127 frp->fr_height = curfrp->fr_height;
1128
1129 /* "new_size" of the current window goes to the new window, use
1130 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001131 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 if (before)
1133 wp->w_vsep_width = 1;
1134 else
1135 {
1136 wp->w_vsep_width = oldwin->w_vsep_width;
1137 oldwin->w_vsep_width = 1;
1138 }
1139 if (flags & (WSP_TOP | WSP_BOT))
1140 {
1141 if (flags & WSP_BOT)
1142 frame_add_vsep(curfrp);
1143 /* Set width of neighbor frame */
1144 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001145 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1146 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 }
1148 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001149 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 if (before) /* new window left of current one */
1151 {
1152 wp->w_wincol = oldwin->w_wincol;
1153 oldwin->w_wincol += new_size + 1;
1154 }
1155 else /* new window right of current one */
1156 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1157 frame_fix_width(oldwin);
1158 frame_fix_width(wp);
1159 }
1160 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 {
1162 /* width and column of new window is same as current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 if (flags & (WSP_TOP | WSP_BOT))
1164 {
1165 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001166 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 wp->w_vsep_width = 0;
1168 }
1169 else
1170 {
1171 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001172 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 wp->w_vsep_width = oldwin->w_vsep_width;
1174 }
1175 frp->fr_width = curfrp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176
1177 /* "new_size" of the current window goes to the new window, use
1178 * one row for the status line */
1179 win_new_height(wp, new_size);
1180 if (flags & (WSP_TOP | WSP_BOT))
Bram Moolenaar991dea32016-05-24 11:31:32 +02001181 {
Bram Moolenaard326ad62017-09-18 20:31:41 +02001182 int new_fr_height = curfrp->fr_height - new_size
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001183 + WINBAR_HEIGHT(wp) ;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001184
1185 if (!((flags & WSP_BOT) && p_ls == 0))
1186 new_fr_height -= STATUS_HEIGHT;
1187 frame_new_height(curfrp, new_fr_height, flags & WSP_TOP, FALSE);
1188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 else
1190 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1191 if (before) /* new window above current one */
1192 {
1193 wp->w_winrow = oldwin->w_winrow;
1194 wp->w_status_height = STATUS_HEIGHT;
1195 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1196 }
1197 else /* new window below current one */
1198 {
Bram Moolenaar415a6932017-12-05 20:31:07 +01001199 wp->w_winrow = oldwin->w_winrow + VISIBLE_HEIGHT(oldwin)
1200 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 wp->w_status_height = oldwin->w_status_height;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001202 if (!(flags & WSP_BOT))
1203 oldwin->w_status_height = STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 if (flags & WSP_BOT)
1206 frame_add_statusline(curfrp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 frame_fix_height(wp);
1208 frame_fix_height(oldwin);
1209 }
1210
1211 if (flags & (WSP_TOP | WSP_BOT))
1212 (void)win_comp_pos();
1213
1214 /*
1215 * Both windows need redrawing
1216 */
1217 redraw_win_later(wp, NOT_VALID);
1218 wp->w_redr_status = TRUE;
1219 redraw_win_later(oldwin, NOT_VALID);
1220 oldwin->w_redr_status = TRUE;
1221
1222 if (need_status)
1223 {
1224 msg_row = Rows - 1;
1225 msg_col = sc_col;
1226 msg_clr_eos_force(); /* Old command/ruler may still be there */
1227 comp_col();
1228 msg_row = Rows - 1;
1229 msg_col = 0; /* put position back at start of line */
1230 }
1231
1232 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001233 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 */
1235 if (do_equal || dir != 0)
1236 win_equal(wp, TRUE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001238 : dir == 'h' ? 'b' : 'v');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239
1240 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1241 * size was given. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (flags & WSP_VERT)
1243 {
1244 i = p_wiw;
1245 if (size != 0)
1246 p_wiw = size;
1247
1248# ifdef FEAT_GUI
1249 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1250 if (gui.in_use)
1251 gui_init_which_components(NULL);
1252# endif
1253 }
1254 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 {
1256 i = p_wh;
1257 if (size != 0)
1258 p_wh = size;
1259 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001260
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001261#ifdef FEAT_JUMPLIST
1262 /* Keep same changelist position in new window. */
1263 wp->w_changelistidx = oldwin->w_changelistidx;
1264#endif
1265
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001266 /*
1267 * make the new window the current window
1268 */
Bram Moolenaarc917da42016-07-19 22:31:36 +02001269 win_enter_ext(wp, FALSE, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 if (flags & WSP_VERT)
1271 p_wiw = i;
1272 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 p_wh = i;
1274
1275 return OK;
1276}
1277
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001278
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001279/*
1280 * Initialize window "newp" from window "oldp".
1281 * Used when splitting a window and when creating a new tab page.
1282 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001283 * WSP_NEWLOC may be specified in flags to prevent the location list from
1284 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001285 */
1286 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001287win_init(win_T *newp, win_T *oldp, int flags UNUSED)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001288{
1289 int i;
1290
1291 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001292#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001293 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001294#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001295 oldp->w_buffer->b_nwindows++;
1296 newp->w_cursor = oldp->w_cursor;
1297 newp->w_valid = 0;
1298 newp->w_curswant = oldp->w_curswant;
1299 newp->w_set_curswant = oldp->w_set_curswant;
1300 newp->w_topline = oldp->w_topline;
1301#ifdef FEAT_DIFF
1302 newp->w_topfill = oldp->w_topfill;
1303#endif
1304 newp->w_leftcol = oldp->w_leftcol;
1305 newp->w_pcmark = oldp->w_pcmark;
1306 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1307 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001308 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001309 newp->w_fraction = oldp->w_fraction;
1310 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1311#ifdef FEAT_JUMPLIST
1312 copy_jumplist(oldp, newp);
1313#endif
1314#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001315 if (flags & WSP_NEWLOC)
1316 {
1317 /* Don't copy the location list. */
1318 newp->w_llist = NULL;
1319 newp->w_llist_ref = NULL;
1320 }
1321 else
Bram Moolenaar09037502018-09-25 22:08:14 +02001322 copy_loclist_stack(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001323#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001324 newp->w_localdir = (oldp->w_localdir == NULL)
1325 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001326
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001327 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001328 for (i = 0; i < oldp->w_tagstacklen; i++)
1329 {
1330 newp->w_tagstack[i] = oldp->w_tagstack[i];
1331 if (newp->w_tagstack[i].tagname != NULL)
1332 newp->w_tagstack[i].tagname =
1333 vim_strsave(newp->w_tagstack[i].tagname);
1334 }
1335 newp->w_tagstackidx = oldp->w_tagstackidx;
1336 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001337#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001338 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001339#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001340
1341 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001342
Bram Moolenaara971b822011-09-14 14:43:25 +02001343#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001344 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001345#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001346}
1347
1348/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001349 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001350 * Only the essential things are copied.
1351 */
1352 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001353win_init_some(win_T *newp, win_T *oldp)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001354{
1355 /* Use the same argument list. */
1356 newp->w_alist = oldp->w_alist;
1357 ++newp->w_alist->al_refcount;
1358 newp->w_arg_idx = oldp->w_arg_idx;
1359
1360 /* copy options from existing window */
1361 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001362}
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001366 * Check if "win" is a pointer to an existing window in the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 */
1368 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001369win_valid(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
1371 win_T *wp;
1372
1373 if (win == NULL)
1374 return FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02001375 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 if (wp == win)
1377 return TRUE;
1378 return FALSE;
1379}
1380
1381/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001382 * Check if "win" is a pointer to an existing window in any tab page.
1383 */
1384 int
1385win_valid_any_tab(win_T *win)
1386{
1387 win_T *wp;
1388 tabpage_T *tp;
1389
1390 if (win == NULL)
1391 return FALSE;
1392 FOR_ALL_TABPAGES(tp)
1393 {
1394 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
1395 {
1396 if (wp == win)
1397 return TRUE;
1398 }
1399 }
1400 return FALSE;
1401}
1402
1403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 * Return the number of windows.
1405 */
1406 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001407win_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 win_T *wp;
1410 int count = 0;
1411
Bram Moolenaar29323592016-07-24 22:04:11 +02001412 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 ++count;
1414 return count;
1415}
1416
1417/*
1418 * Make "count" windows on the screen.
1419 * Return actual number of windows on the screen.
1420 * Must be called when there is just one window, filling the whole screen
1421 * (excluding the command line).
1422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001424make_windows(
1425 int count,
1426 int vertical UNUSED) /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
1428 int maxcount;
1429 int todo;
1430
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 if (vertical)
1432 {
1433 /* Each windows needs at least 'winminwidth' lines and a separator
1434 * column. */
1435 maxcount = (curwin->w_width + curwin->w_vsep_width
1436 - (p_wiw - p_wmw)) / (p_wmw + 1);
1437 }
1438 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 {
1440 /* Each window needs at least 'winminheight' lines and a status line. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001441 maxcount = (VISIBLE_HEIGHT(curwin) + curwin->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1443 }
1444
1445 if (maxcount < 2)
1446 maxcount = 2;
1447 if (count > maxcount)
1448 count = maxcount;
1449
1450 /*
1451 * add status line now, otherwise first window will be too big
1452 */
1453 if (count > 1)
1454 last_status(TRUE);
1455
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 /*
1457 * Don't execute autocommands while creating the windows. Must do that
1458 * when putting the buffers in the windows.
1459 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001460 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
1462 /* todo is number of windows left to create */
1463 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (vertical)
1465 {
1466 if (win_split(curwin->w_width - (curwin->w_width - todo)
1467 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1468 break;
1469 }
1470 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 {
1472 if (win_split(curwin->w_height - (curwin->w_height - todo
1473 * STATUS_HEIGHT) / (todo + 1)
1474 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1475 break;
1476 }
1477
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001478 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479
1480 /* return actual number of windows */
1481 return (count - todo);
1482}
1483
1484/*
1485 * Exchange current and next window
1486 */
1487 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001488win_exchange(long Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490 frame_T *frp;
1491 frame_T *frp2;
1492 win_T *wp;
1493 win_T *wp2;
1494 int temp;
1495
Bram Moolenaar459ca562016-11-10 18:16:33 +01001496 if (ONE_WINDOW) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 {
1498 beep_flush();
1499 return;
1500 }
1501
1502#ifdef FEAT_GUI
1503 need_mouse_correct = TRUE;
1504#endif
1505
1506 /*
1507 * find window to exchange with
1508 */
1509 if (Prenum)
1510 {
1511 frp = curwin->w_frame->fr_parent->fr_child;
1512 while (frp != NULL && --Prenum > 0)
1513 frp = frp->fr_next;
1514 }
1515 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1516 frp = curwin->w_frame->fr_next;
1517 else /* Swap last window in row/col with previous */
1518 frp = curwin->w_frame->fr_prev;
1519
1520 /* We can only exchange a window with another window, not with a frame
1521 * containing windows. */
1522 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1523 return;
1524 wp = frp->fr_win;
1525
1526/*
1527 * 1. remove curwin from the list. Remember after which window it was in wp2
1528 * 2. insert curwin before wp in the list
1529 * if wp != wp2
1530 * 3. remove wp from the list
1531 * 4. insert wp after wp2
1532 * 5. exchange the status line height and vsep width.
1533 */
1534 wp2 = curwin->w_prev;
1535 frp2 = curwin->w_frame->fr_prev;
1536 if (wp->w_prev != curwin)
1537 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001538 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 frame_remove(curwin->w_frame);
1540 win_append(wp->w_prev, curwin);
1541 frame_insert(frp, curwin->w_frame);
1542 }
1543 if (wp != wp2)
1544 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001545 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 frame_remove(wp->w_frame);
1547 win_append(wp2, wp);
1548 if (frp2 == NULL)
1549 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1550 else
1551 frame_append(frp2, wp->w_frame);
1552 }
1553 temp = curwin->w_status_height;
1554 curwin->w_status_height = wp->w_status_height;
1555 wp->w_status_height = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 temp = curwin->w_vsep_width;
1557 curwin->w_vsep_width = wp->w_vsep_width;
1558 wp->w_vsep_width = temp;
1559
1560 /* If the windows are not in the same frame, exchange the sizes to avoid
1561 * messing up the window layout. Otherwise fix the frame sizes. */
1562 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1563 {
1564 temp = curwin->w_height;
1565 curwin->w_height = wp->w_height;
1566 wp->w_height = temp;
1567 temp = curwin->w_width;
1568 curwin->w_width = wp->w_width;
1569 wp->w_width = temp;
1570 }
1571 else
1572 {
1573 frame_fix_height(curwin);
1574 frame_fix_height(wp);
1575 frame_fix_width(curwin);
1576 frame_fix_width(wp);
1577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
1579 (void)win_comp_pos(); /* recompute window positions */
1580
1581 win_enter(wp, TRUE);
1582 redraw_later(CLEAR);
1583}
1584
1585/*
1586 * rotate windows: if upwards TRUE the second window becomes the first one
1587 * if upwards FALSE the first window becomes the second one
1588 */
1589 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001590win_rotate(int upwards, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591{
1592 win_T *wp1;
1593 win_T *wp2;
1594 frame_T *frp;
1595 int n;
1596
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001597 if (ONE_WINDOW) /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 {
1599 beep_flush();
1600 return;
1601 }
1602
1603#ifdef FEAT_GUI
1604 need_mouse_correct = TRUE;
1605#endif
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 /* Check if all frames in this row/col have one window. */
1608 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1609 frp = frp->fr_next)
1610 if (frp->fr_win == NULL)
1611 {
1612 EMSG(_("E443: Cannot rotate when another window is split"));
1613 return;
1614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
1616 while (count--)
1617 {
1618 if (upwards) /* first window becomes last window */
1619 {
1620 /* remove first window/frame from the list */
1621 frp = curwin->w_frame->fr_parent->fr_child;
1622 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001623 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 frame_remove(frp);
1625
1626 /* find last frame and append removed window/frame after it */
1627 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1628 ;
1629 win_append(frp->fr_win, wp1);
1630 frame_append(frp, wp1->w_frame);
1631
1632 wp2 = frp->fr_win; /* previously last window */
1633 }
1634 else /* last window becomes first window */
1635 {
1636 /* find last window/frame in the list and remove it */
1637 for (frp = curwin->w_frame; frp->fr_next != NULL;
1638 frp = frp->fr_next)
1639 ;
1640 wp1 = frp->fr_win;
1641 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001642 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 frame_remove(frp);
1644
1645 /* append the removed window/frame before the first in the list */
1646 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1647 frame_insert(frp->fr_parent->fr_child, frp);
1648 }
1649
1650 /* exchange status height and vsep width of old and new last window */
1651 n = wp2->w_status_height;
1652 wp2->w_status_height = wp1->w_status_height;
1653 wp1->w_status_height = n;
1654 frame_fix_height(wp1);
1655 frame_fix_height(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 n = wp2->w_vsep_width;
1657 wp2->w_vsep_width = wp1->w_vsep_width;
1658 wp1->w_vsep_width = n;
1659 frame_fix_width(wp1);
1660 frame_fix_width(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001662 /* recompute w_winrow and w_wincol for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 (void)win_comp_pos();
1664 }
1665
1666 redraw_later(CLEAR);
1667}
1668
1669/*
1670 * Move the current window to the very top/bottom/left/right of the screen.
1671 */
1672 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001673win_totop(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674{
1675 int dir;
1676 int height = curwin->w_height;
1677
Bram Moolenaar459ca562016-11-10 18:16:33 +01001678 if (ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 {
1680 beep_flush();
1681 return;
1682 }
1683
1684 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001685 (void)winframe_remove(curwin, &dir, NULL);
1686 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 last_status(FALSE); /* may need to remove last status line */
1688 (void)win_comp_pos(); /* recompute window positions */
1689
1690 /* Split a window on the desired side and put the window there. */
1691 (void)win_split_ins(size, flags, curwin, dir);
1692 if (!(flags & WSP_VERT))
1693 {
1694 win_setheight(height);
1695 if (p_ea)
1696 win_equal(curwin, TRUE, 'v');
1697 }
1698
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001699#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1701 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001702 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704}
1705
1706/*
1707 * Move window "win1" to below/right of "win2" and make "win1" the current
1708 * window. Only works within the same frame!
1709 */
1710 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001711win_move_after(win_T *win1, win_T *win2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712{
1713 int height;
1714
1715 /* check if the arguments are reasonable */
1716 if (win1 == win2)
1717 return;
1718
1719 /* check if there is something to do */
1720 if (win2->w_next != win1)
1721 {
1722 /* may need move the status line/vertical separator of the last window
1723 * */
1724 if (win1 == lastwin)
1725 {
1726 height = win1->w_prev->w_status_height;
1727 win1->w_prev->w_status_height = win1->w_status_height;
1728 win1->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001729 if (win1->w_prev->w_vsep_width == 1)
1730 {
1731 /* Remove the vertical separator from the last-but-one window,
1732 * add it to the last window. Adjust the frame widths. */
1733 win1->w_prev->w_vsep_width = 0;
1734 win1->w_prev->w_frame->fr_width -= 1;
1735 win1->w_vsep_width = 1;
1736 win1->w_frame->fr_width += 1;
1737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 }
1739 else if (win2 == lastwin)
1740 {
1741 height = win1->w_status_height;
1742 win1->w_status_height = win2->w_status_height;
1743 win2->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001744 if (win1->w_vsep_width == 1)
1745 {
1746 /* Remove the vertical separator from win1, add it to the last
1747 * window, win2. Adjust the frame widths. */
1748 win2->w_vsep_width = 1;
1749 win2->w_frame->fr_width += 1;
1750 win1->w_vsep_width = 0;
1751 win1->w_frame->fr_width -= 1;
1752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001754 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 frame_remove(win1->w_frame);
1756 win_append(win2, win1);
1757 frame_append(win2->w_frame, win1->w_frame);
1758
1759 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1760 redraw_later(NOT_VALID);
1761 }
1762 win_enter(win1, FALSE);
1763}
1764
1765/*
1766 * Make all windows the same height.
1767 * 'next_curwin' will soon be the current window, make sure it has enough
1768 * rows.
1769 */
1770 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001771win_equal(
1772 win_T *next_curwin, /* pointer to current window to be or NULL */
1773 int current, /* do only frame with current window */
1774 int dir) /* 'v' for vertically, 'h' for horizontally,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 'b' for both, 0 for using p_ead */
1776{
1777 if (dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 dir = *p_ead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001780 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001781 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782}
1783
1784/*
1785 * Set a frame to a new position and height, spreading the available room
1786 * equally over contained frames.
1787 * The window "next_curwin" (if not NULL) should at least get the size from
1788 * 'winheight' and 'winwidth' if possible.
1789 */
1790 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001791win_equal_rec(
1792 win_T *next_curwin, /* pointer to current window to be or NULL */
1793 int current, /* do only frame with current window */
1794 frame_T *topfr, /* frame to set size off */
1795 int dir, /* 'v', 'h' or 'b', see win_equal() */
1796 int col, /* horizontal position for frame */
1797 int row, /* vertical position for frame */
1798 int width, /* new width of frame */
1799 int height) /* new height of frame */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800{
1801 int n, m;
1802 int extra_sep = 0;
1803 int wincount, totwincount = 0;
1804 frame_T *fr;
1805 int next_curwin_size = 0;
1806 int room = 0;
1807 int new_size;
1808 int has_next_curwin = 0;
1809 int hnc;
1810
1811 if (topfr->fr_layout == FR_LEAF)
1812 {
1813 /* Set the width/height of this frame.
1814 * Redraw when size or position changes */
1815 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 )
1818 {
1819 topfr->fr_win->w_winrow = row;
1820 frame_new_height(topfr, height, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001822 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 redraw_all_later(CLEAR);
1824 }
1825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 else if (topfr->fr_layout == FR_ROW)
1827 {
1828 topfr->fr_width = width;
1829 topfr->fr_height = height;
1830
1831 if (dir != 'v') /* equalize frame widths */
1832 {
1833 /* Compute the maximum number of windows horizontally in this
1834 * frame. */
1835 n = frame_minwidth(topfr, NOWIN);
1836 /* add one for the rightmost window, it doesn't have a separator */
1837 if (col + width == Columns)
1838 extra_sep = 1;
1839 else
1840 extra_sep = 0;
1841 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001842 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001844 /*
1845 * Compute width for "next_curwin" window and room available for
1846 * other windows.
1847 * "m" is the minimal width when counting p_wiw for "next_curwin".
1848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 m = frame_minwidth(topfr, next_curwin);
1850 room = width - m;
1851 if (room < 0)
1852 {
1853 next_curwin_size = p_wiw + room;
1854 room = 0;
1855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 else
1857 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001858 next_curwin_size = -1;
1859 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1860 {
1861 /* If 'winfixwidth' set keep the window width if
1862 * possible.
1863 * Watch out for this window being the next_curwin. */
1864 if (frame_fixed_width(fr))
1865 {
1866 n = frame_minwidth(fr, NOWIN);
1867 new_size = fr->fr_width;
1868 if (frame_has_win(fr, next_curwin))
1869 {
1870 room += p_wiw - p_wmw;
1871 next_curwin_size = 0;
1872 if (new_size < p_wiw)
1873 new_size = p_wiw;
1874 }
1875 else
1876 /* These windows don't use up room. */
1877 totwincount -= (n + (fr->fr_next == NULL
1878 ? extra_sep : 0)) / (p_wmw + 1);
1879 room -= new_size - n;
1880 if (room < 0)
1881 {
1882 new_size += room;
1883 room = 0;
1884 }
1885 fr->fr_newwidth = new_size;
1886 }
1887 }
1888 if (next_curwin_size == -1)
1889 {
1890 if (!has_next_curwin)
1891 next_curwin_size = 0;
1892 else if (totwincount > 1
1893 && (room + (totwincount - 2))
1894 / (totwincount - 1) > p_wiw)
1895 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001896 /* Can make all windows wider than 'winwidth', spread
1897 * the room equally. */
1898 next_curwin_size = (room + p_wiw
1899 + (totwincount - 1) * p_wmw
1900 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001901 room -= next_curwin_size - p_wiw;
1902 }
1903 else
1904 next_curwin_size = p_wiw;
1905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001907
1908 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 --totwincount; /* don't count curwin */
1910 }
1911
1912 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 wincount = 1;
1915 if (fr->fr_next == NULL)
1916 /* last frame gets all that remains (avoid roundoff error) */
1917 new_size = width;
1918 else if (dir == 'v')
1919 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001920 else if (frame_fixed_width(fr))
1921 {
1922 new_size = fr->fr_newwidth;
1923 wincount = 0; /* doesn't count as a sizeable window */
1924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 else
1926 {
1927 /* Compute the maximum number of windows horiz. in "fr". */
1928 n = frame_minwidth(fr, NOWIN);
1929 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1930 / (p_wmw + 1);
1931 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001932 if (has_next_curwin)
1933 hnc = frame_has_win(fr, next_curwin);
1934 else
1935 hnc = FALSE;
1936 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001938 if (totwincount == 0)
1939 new_size = room;
1940 else
1941 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001943 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
1945 next_curwin_size -= p_wiw - (m - n);
1946 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001947 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001949 else
1950 room -= new_size;
1951 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 }
1953
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001954 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 * window, unless equalizing all frames. */
1956 if (!current || dir != 'v' || topfr->fr_parent != NULL
1957 || (new_size != fr->fr_width)
1958 || frame_has_win(fr, next_curwin))
1959 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001960 new_size, height);
1961 col += new_size;
1962 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 totwincount -= wincount;
1964 }
1965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 else /* topfr->fr_layout == FR_COL */
1967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 topfr->fr_width = width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 topfr->fr_height = height;
1970
1971 if (dir != 'h') /* equalize frame heights */
1972 {
1973 /* Compute maximum number of windows vertically in this frame. */
1974 n = frame_minheight(topfr, NOWIN);
1975 /* add one for the bottom window if it doesn't have a statusline */
1976 if (row + height == cmdline_row && p_ls == 0)
1977 extra_sep = 1;
1978 else
1979 extra_sep = 0;
1980 totwincount = (n + extra_sep) / (p_wmh + 1);
1981 has_next_curwin = frame_has_win(topfr, next_curwin);
1982
1983 /*
1984 * Compute height for "next_curwin" window and room available for
1985 * other windows.
1986 * "m" is the minimal height when counting p_wh for "next_curwin".
1987 */
1988 m = frame_minheight(topfr, next_curwin);
1989 room = height - m;
1990 if (room < 0)
1991 {
1992 /* The room is less then 'winheight', use all space for the
1993 * current window. */
1994 next_curwin_size = p_wh + room;
1995 room = 0;
1996 }
1997 else
1998 {
1999 next_curwin_size = -1;
2000 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2001 {
2002 /* If 'winfixheight' set keep the window height if
2003 * possible.
2004 * Watch out for this window being the next_curwin. */
2005 if (frame_fixed_height(fr))
2006 {
2007 n = frame_minheight(fr, NOWIN);
2008 new_size = fr->fr_height;
2009 if (frame_has_win(fr, next_curwin))
2010 {
2011 room += p_wh - p_wmh;
2012 next_curwin_size = 0;
2013 if (new_size < p_wh)
2014 new_size = p_wh;
2015 }
2016 else
2017 /* These windows don't use up room. */
2018 totwincount -= (n + (fr->fr_next == NULL
2019 ? extra_sep : 0)) / (p_wmh + 1);
2020 room -= new_size - n;
2021 if (room < 0)
2022 {
2023 new_size += room;
2024 room = 0;
2025 }
2026 fr->fr_newheight = new_size;
2027 }
2028 }
2029 if (next_curwin_size == -1)
2030 {
2031 if (!has_next_curwin)
2032 next_curwin_size = 0;
2033 else if (totwincount > 1
2034 && (room + (totwincount - 2))
2035 / (totwincount - 1) > p_wh)
2036 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002037 /* can make all windows higher than 'winheight',
2038 * spread the room equally. */
2039 next_curwin_size = (room + p_wh
2040 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 + (totwincount - 1)) / totwincount;
2042 room -= next_curwin_size - p_wh;
2043 }
2044 else
2045 next_curwin_size = p_wh;
2046 }
2047 }
2048
2049 if (has_next_curwin)
2050 --totwincount; /* don't count curwin */
2051 }
2052
2053 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2054 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 wincount = 1;
2056 if (fr->fr_next == NULL)
2057 /* last frame gets all that remains (avoid roundoff error) */
2058 new_size = height;
2059 else if (dir == 'h')
2060 new_size = fr->fr_height;
2061 else if (frame_fixed_height(fr))
2062 {
2063 new_size = fr->fr_newheight;
2064 wincount = 0; /* doesn't count as a sizeable window */
2065 }
2066 else
2067 {
2068 /* Compute the maximum number of windows vert. in "fr". */
2069 n = frame_minheight(fr, NOWIN);
2070 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2071 / (p_wmh + 1);
2072 m = frame_minheight(fr, next_curwin);
2073 if (has_next_curwin)
2074 hnc = frame_has_win(fr, next_curwin);
2075 else
2076 hnc = FALSE;
2077 if (hnc) /* don't count next_curwin */
2078 --wincount;
2079 if (totwincount == 0)
2080 new_size = room;
2081 else
2082 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2083 / totwincount;
2084 if (hnc) /* add next_curwin size */
2085 {
2086 next_curwin_size -= p_wh - (m - n);
2087 new_size += next_curwin_size;
2088 room -= new_size - next_curwin_size;
2089 }
2090 else
2091 room -= new_size;
2092 new_size += n;
2093 }
2094 /* Skip frame that is full width when splitting or closing a
2095 * window, unless equalizing all frames. */
2096 if (!current || dir != 'h' || topfr->fr_parent != NULL
2097 || (new_size != fr->fr_height)
2098 || frame_has_win(fr, next_curwin))
2099 win_equal_rec(next_curwin, current, fr, dir, col, row,
2100 width, new_size);
2101 row += new_size;
2102 height -= new_size;
2103 totwincount -= wincount;
2104 }
2105 }
2106}
2107
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002108#ifdef FEAT_JOB_CHANNEL
2109 static void
2110leaving_window(win_T *win)
2111{
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002112 // Only matters for a prompt window.
2113 if (!bt_prompt(win->w_buffer))
2114 return;
2115
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002116 // When leaving a prompt window stop Insert mode and perhaps restart
2117 // it when entering that window again.
2118 win->w_buffer->b_prompt_insert = restart_edit;
Bram Moolenaar942b4542018-06-17 16:23:34 +02002119 if (restart_edit != 0 && mode_displayed)
2120 clear_cmdline = TRUE; /* unshow mode later */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002121 restart_edit = NUL;
2122
2123 // When leaving the window (or closing the window) was done from a
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002124 // callback we need to break out of the Insert mode loop and restart Insert
2125 // mode when entering the window again.
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002126 if (State & INSERT)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002127 {
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002128 stop_insert_mode = TRUE;
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002129 if (win->w_buffer->b_prompt_insert == NUL)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002130 win->w_buffer->b_prompt_insert = 'A';
2131 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002132}
2133
2134 static void
2135entering_window(win_T *win)
2136{
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002137 // Only matters for a prompt window.
2138 if (!bt_prompt(win->w_buffer))
2139 return;
2140
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002141 // When switching to a prompt buffer that was in Insert mode, don't stop
2142 // Insert mode, it may have been set in leaving_window().
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002143 if (win->w_buffer->b_prompt_insert != NUL)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002144 stop_insert_mode = FALSE;
2145
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002146 // When entering the prompt window restart Insert mode if we were in Insert
2147 // mode when we left it.
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002148 restart_edit = win->w_buffer->b_prompt_insert;
2149}
2150#endif
2151
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002153 * Close all windows for buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 */
2155 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002156close_windows(
2157 buf_T *buf,
2158 int keep_curwin) /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002160 win_T *wp;
2161 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002162 int h = tabline_height();
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002163 int count = tabpage_index(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
2165 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002166
Bram Moolenaar459ca562016-11-10 18:16:33 +01002167 for (wp = firstwin; wp != NULL && !ONE_WINDOW; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002169 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002170 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002172 if (win_close(wp, FALSE) == FAIL)
2173 /* If closing the window fails give up, to avoid looping
2174 * forever. */
2175 break;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002176
2177 /* Start all over, autocommands may change the window layout. */
2178 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 }
2180 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002181 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002183
2184 /* Also check windows in other tab pages. */
2185 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2186 {
2187 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002188 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002189 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002190 if (wp->w_buffer == buf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002191 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaarf740b292006-02-16 22:11:02 +00002192 {
2193 win_close_othertab(wp, FALSE, tp);
2194
2195 /* Start all over, the tab page may be closed and
2196 * autocommands may change the window layout. */
2197 nexttp = first_tabpage;
2198 break;
2199 }
2200 }
2201
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002203
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002204 if (count != tabpage_index(NULL))
2205 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002206
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002207 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002208 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002209 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210}
2211
2212/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002213 * Return TRUE if the current window is the only window that exists (ignoring
2214 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002215 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002216 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002217 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002218last_window(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002219{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002220 return (one_window() && first_tabpage->tp_next == NULL);
2221}
2222
2223/*
2224 * Return TRUE if there is only one window other than "aucmd_win" in the
2225 * current tab page.
2226 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002227 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002228one_window(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002229{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002230 win_T *wp;
2231 int seen_one = FALSE;
2232
2233 FOR_ALL_WINDOWS(wp)
2234 {
2235 if (wp != aucmd_win)
2236 {
2237 if (seen_one)
2238 return FALSE;
2239 seen_one = TRUE;
2240 }
2241 }
2242 return TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002243}
2244
2245/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002246 * Close the possibly last window in a tab page.
2247 * Returns TRUE when the window was closed already.
2248 */
2249 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002250close_last_window_tabpage(
2251 win_T *win,
2252 int free_buf,
2253 tabpage_T *prev_curtab)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002254{
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002255 if (ONE_WINDOW)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002256 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002257 buf_T *old_curbuf = curbuf;
2258
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002259 /*
2260 * Closing the last window in a tab page. First go to another tab
2261 * page and then close the window and the tab page. This avoids that
2262 * curwin and curtab are invalid while we are freeing memory, they may
2263 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002264 * Don't trigger autocommands yet, they may use wrong values, so do
2265 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002266 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002267 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002268 redraw_tabline = TRUE;
2269
2270 /* Safety check: Autocommands may have closed the window when jumping
2271 * to the other tab page. */
2272 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2273 {
2274 int h = tabline_height();
2275
2276 win_close_othertab(win, free_buf, prev_curtab);
2277 if (h != tabline_height())
2278 shell_new_rows();
2279 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002280#ifdef FEAT_JOB_CHANNEL
2281 entering_window(curwin);
2282#endif
Bram Moolenaara8596c42012-06-13 14:28:20 +02002283 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2284 * that now. */
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002285 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002286 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002287 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2288 if (old_curbuf != curbuf)
2289 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002290 return TRUE;
2291 }
2292 return FALSE;
2293}
2294
2295/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002296 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 * If "free_buf" is TRUE related buffer may be unloaded.
2298 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002299 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002300 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002302 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002303win_close(win_T *win, int free_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304{
2305 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 int dir;
2309 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002310 tabpage_T *prev_curtab = curtab;
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002311 frame_T *win_frame = win->w_frame->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002313 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
2315 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002316 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 }
2318
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002319 if (win->w_closing || (win->w_buffer != NULL
2320 && win->w_buffer->b_locked > 0))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002321 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002322 if (win == aucmd_win)
2323 {
2324 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002325 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002326 }
2327 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2328 {
2329 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002330 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002331 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002332
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002333 /* When closing the last window in a tab page first go to another tab page
2334 * and then close the window and the tab page to avoid that curwin and
2335 * curtab are invalid while we are freeing memory. */
2336 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002337 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 /* When closing the help window, try restoring a snapshot after closing
2340 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002341 if (bt_help(win->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 help_window = TRUE;
2343 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002344 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 if (win == curwin)
2347 {
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002348#ifdef FEAT_JOB_CHANNEL
2349 leaving_window(curwin);
2350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 /*
2352 * Guess which window is going to be the new current window.
2353 * This may change because of the autocommands (sigh).
2354 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002355 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356
2357 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002358 * Be careful: If autocommands delete the window or cause this window
2359 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 */
2361 if (wp->w_buffer != curbuf)
2362 {
2363 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002364 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002366 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002367 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002368 win->w_closing = FALSE;
2369 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002370 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002372 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002374 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002375 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002376 win->w_closing = FALSE;
2377 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002378 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002379#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 /* autocmds may abort script processing */
2381 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002382 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002386#ifdef FEAT_GUI
2387 /* Avoid trouble with scrollbars that are going to be deleted in
2388 * win_free(). */
2389 if (gui.in_use)
2390 out_flush();
2391#endif
2392
Bram Moolenaara971b822011-09-14 14:43:25 +02002393#ifdef FEAT_SYN_HL
2394 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002395 if (win->w_buffer != NULL)
2396 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002397#endif
2398
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 /*
2400 * Close the link to the buffer.
2401 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002402 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002403 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002404 bufref_T bufref;
2405
2406 set_bufref(&bufref, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002407 win->w_closing = TRUE;
Bram Moolenaar8f913992012-08-29 15:50:26 +02002408 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002409 if (win_valid_any_tab(win))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002410 win->w_closing = FALSE;
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002411 /* Make sure curbuf is valid. It can become invalid if 'bufhidden' is
2412 * "wipe". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002413 if (!bufref_valid(&bufref))
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002414 curbuf = firstbuf;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002415 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002416
Bram Moolenaar802418d2013-01-17 14:00:11 +01002417 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2418 && (last_window() || curtab != prev_curtab
2419 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002420 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002421 /* Autocommands have closed all windows, quit now. Restore
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002422 * curwin->w_buffer, otherwise writing viminfo may fail. */
2423 if (curwin->w_buffer == NULL)
2424 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002425 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002426 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002427
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002428 /* Autocommands may have moved to another tab page. */
2429 if (curtab != prev_curtab && win_valid_any_tab(win)
2430 && win->w_buffer == NULL)
2431 {
2432 /* Need to close the window anyway, since the buffer is NULL. */
2433 win_close_othertab(win, FALSE, prev_curtab);
2434 return FAIL;
2435 }
2436
2437 /* Autocommands may have closed the window already or closed the only
2438 * other window. */
2439 if (!win_valid(win) || last_window()
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002440 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002441 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442
Bram Moolenaara971b822011-09-14 14:43:25 +02002443 /* Free the memory used for the window and get the window that received
2444 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002445 wp = win_free_mem(win, &dir, NULL);
2446
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 /* Make sure curwin isn't invalid. It can cause severe trouble when
2448 * printing an error message. For win_equal() curbuf needs to be valid
2449 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002450 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {
2452 curwin = wp;
2453#ifdef FEAT_QUICKFIX
2454 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2455 {
2456 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002457 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 * finding another window to go to.
2459 */
2460 for (;;)
2461 {
2462 if (wp->w_next == NULL)
2463 wp = firstwin;
2464 else
2465 wp = wp->w_next;
2466 if (wp == curwin)
2467 break;
2468 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2469 {
2470 curwin = wp;
2471 break;
2472 }
2473 }
2474 }
2475#endif
2476 curbuf = curwin->w_buffer;
2477 close_curwin = TRUE;
Bram Moolenaarf79225e2017-03-18 23:11:04 +01002478
2479 /* The cursor position may be invalid if the buffer changed after last
2480 * using the window. */
2481 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002483 if (p_ea && (*p_ead == 'b' || *p_ead == dir))
Bram Moolenaar8eeeba82017-06-25 22:45:39 +02002484 /* If the frame of the closed window contains the new current window,
2485 * only resize that frame. Otherwise resize all windows. */
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002486 win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 else
2488 win_comp_pos();
2489 if (close_curwin)
2490 {
Bram Moolenaarc917da42016-07-19 22:31:36 +02002491 win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 if (other_buffer)
2493 /* careful: after this wp and win may be invalid! */
2494 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 }
2496
2497 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002498 * If last window has a status line now and we don't want one,
2499 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 */
2501 last_status(FALSE);
2502
2503 /* After closing the help window, try restoring the window layout from
2504 * before it was opened. */
2505 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002506 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002508#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2510 if (gui.in_use && !win_hasvertsplit())
2511 gui_init_which_components(NULL);
2512#endif
2513
2514 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002515 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516}
2517
2518/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002519 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002520 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002521 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002522 * Caller must check if buffer is hidden and whether the tabline needs to be
2523 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002524 */
2525 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002526win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002527{
2528 win_T *wp;
2529 int dir;
2530 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002531 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002532
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002533 /* Get here with win->w_buffer == NULL when win_close() detects the tab
2534 * page changed. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002535 if (win->w_closing || (win->w_buffer != NULL
2536 && win->w_buffer->b_locked > 0))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002537 return; /* window is already being closed */
Bram Moolenaar362ce482012-06-06 19:02:45 +02002538
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002539 if (win->w_buffer != NULL)
2540 /* Close the link to the buffer. */
2541 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002542
2543 /* Careful: Autocommands may have closed the tab page or made it the
2544 * current tab page. */
2545 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2546 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002547 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002548 return;
2549
2550 /* Autocommands may have closed the window already. */
2551 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2552 ;
2553 if (wp == NULL)
2554 return;
2555
Bram Moolenaarf740b292006-02-16 22:11:02 +00002556 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002557 if (tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002558 {
2559 if (tp == first_tabpage)
2560 first_tabpage = tp->tp_next;
2561 else
2562 {
2563 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2564 ptp = ptp->tp_next)
2565 ;
2566 if (ptp == NULL)
2567 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002568 internal_error("win_close_othertab()");
Bram Moolenaarf740b292006-02-16 22:11:02 +00002569 return;
2570 }
2571 ptp->tp_next = tp->tp_next;
2572 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002573 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002574 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002575
2576 /* Free the memory used for the window. */
2577 win_free_mem(win, &dir, tp);
2578
2579 if (free_tp)
2580 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002581}
2582
2583/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002584 * Free the memory used for a window.
2585 * Returns a pointer to the window that got the freed up space.
2586 */
2587 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002588win_free_mem(
2589 win_T *win,
2590 int *dirp, /* set to 'v' or 'h' for direction if 'ea' */
2591 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002592{
2593 frame_T *frp;
2594 win_T *wp;
2595
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002596 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002597 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002598 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002599 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002600 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002601
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002602 /* When deleting the current window of another tab page select a new
2603 * current window. */
2604 if (tp != NULL && win == tp->tp_curwin)
2605 tp->tp_curwin = wp;
2606
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002607 return wp;
2608}
2609
2610#if defined(EXITFREE) || defined(PROTO)
2611 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002612win_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002613{
2614 int dummy;
2615
Bram Moolenaarf740b292006-02-16 22:11:02 +00002616 while (first_tabpage->tp_next != NULL)
2617 tabpage_close(TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002618
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002619 if (aucmd_win != NULL)
2620 {
2621 (void)win_free_mem(aucmd_win, &dummy, NULL);
2622 aucmd_win = NULL;
2623 }
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002624
2625 while (firstwin != NULL)
2626 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002627
2628 /* No window should be used after this. Set curwin to NULL to crash
2629 * instead of using freed memory. */
2630 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002631}
2632#endif
2633
2634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 * Remove a window and its frame from the tree of frames.
2636 * Returns a pointer to the window that got the freed up space.
2637 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002638 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002639winframe_remove(
2640 win_T *win,
2641 int *dirp UNUSED, /* set to 'v' or 'h' for direction if 'ea' */
2642 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
2644 frame_T *frp, *frp2, *frp3;
2645 frame_T *frp_close = win->w_frame;
2646 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
2648 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002649 * If there is only one window there is nothing to remove.
2650 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002651 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002652 return NULL;
2653
2654 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 * Remove the window from its frame.
2656 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002657 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 wp = frame2win(frp2);
2659
2660 /* Remove this frame from the list of frames. */
2661 frame_remove(frp_close);
2662
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (frp_close->fr_parent->fr_layout == FR_COL)
2664 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002665 /* When 'winfixheight' is set, try to find another frame in the column
2666 * (as close to the closed frame as possible) to distribute the height
2667 * to. */
2668 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2669 {
2670 frp = frp_close->fr_prev;
2671 frp3 = frp_close->fr_next;
2672 while (frp != NULL || frp3 != NULL)
2673 {
2674 if (frp != NULL)
2675 {
2676 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2677 {
2678 frp2 = frp;
2679 wp = frp->fr_win;
2680 break;
2681 }
2682 frp = frp->fr_prev;
2683 }
2684 if (frp3 != NULL)
2685 {
2686 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2687 {
2688 frp2 = frp3;
2689 wp = frp3->fr_win;
2690 break;
2691 }
2692 frp3 = frp3->fr_next;
2693 }
2694 }
2695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2697 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 *dirp = 'v';
2699 }
2700 else
2701 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002702 /* When 'winfixwidth' is set, try to find another frame in the column
2703 * (as close to the closed frame as possible) to distribute the width
2704 * to. */
2705 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2706 {
2707 frp = frp_close->fr_prev;
2708 frp3 = frp_close->fr_next;
2709 while (frp != NULL || frp3 != NULL)
2710 {
2711 if (frp != NULL)
2712 {
2713 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2714 {
2715 frp2 = frp;
2716 wp = frp->fr_win;
2717 break;
2718 }
2719 frp = frp->fr_prev;
2720 }
2721 if (frp3 != NULL)
2722 {
2723 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2724 {
2725 frp2 = frp3;
2726 wp = frp3->fr_win;
2727 break;
2728 }
2729 frp3 = frp3->fr_next;
2730 }
2731 }
2732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002734 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 *dirp = 'h';
2736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
2738 /* If rows/columns go to a window below/right its positions need to be
2739 * updated. Can only be done after the sizes have been updated. */
2740 if (frp2 == frp_close->fr_next)
2741 {
2742 int row = win->w_winrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02002743 int col = win->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
2745 frame_comp_pos(frp2, &row, &col);
2746 }
2747
2748 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2749 {
2750 /* There is no other frame in this list, move its info to the parent
2751 * and remove it. */
2752 frp2->fr_parent->fr_layout = frp2->fr_layout;
2753 frp2->fr_parent->fr_child = frp2->fr_child;
2754 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2755 frp->fr_parent = frp2->fr_parent;
2756 frp2->fr_parent->fr_win = frp2->fr_win;
2757 if (frp2->fr_win != NULL)
2758 frp2->fr_win->w_frame = frp2->fr_parent;
2759 frp = frp2->fr_parent;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002760 if (topframe->fr_child == frp2)
2761 topframe->fr_child = frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 vim_free(frp2);
2763
2764 frp2 = frp->fr_parent;
2765 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2766 {
2767 /* The frame above the parent has the same layout, have to merge
2768 * the frames into this list. */
2769 if (frp2->fr_child == frp)
2770 frp2->fr_child = frp->fr_child;
2771 frp->fr_child->fr_prev = frp->fr_prev;
2772 if (frp->fr_prev != NULL)
2773 frp->fr_prev->fr_next = frp->fr_child;
2774 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2775 {
2776 frp3->fr_parent = frp2;
2777 if (frp3->fr_next == NULL)
2778 {
2779 frp3->fr_next = frp->fr_next;
2780 if (frp->fr_next != NULL)
2781 frp->fr_next->fr_prev = frp3;
2782 break;
2783 }
2784 }
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002785 if (topframe->fr_child == frp)
2786 topframe->fr_child = frp2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 vim_free(frp);
2788 }
2789 }
2790
2791 return wp;
2792}
2793
2794/*
Bram Moolenaarc136af22018-05-04 20:15:38 +02002795 * Return a pointer to the frame that will receive the empty screen space that
2796 * is left over after "win" is closed.
2797 *
2798 * If 'splitbelow' or 'splitright' is set, the space goes above or to the left
2799 * by default. Otherwise, the free space goes below or to the right. The
2800 * result is that opening a window and then immediately closing it will
2801 * preserve the initial window layout. The 'wfh' and 'wfw' settings are
2802 * respected when possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 */
2804 static frame_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002805win_altframe(
2806 win_T *win,
2807 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808{
2809 frame_T *frp;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002810 frame_T *other_fr, *target_fr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002812 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002813 return alt_tabpage()->tp_curwin->w_frame;
2814
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 frp = win->w_frame;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002816
2817 if (frp->fr_prev == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 return frp->fr_next;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002819 if (frp->fr_next == NULL)
2820 return frp->fr_prev;
2821
2822 target_fr = frp->fr_next;
2823 other_fr = frp->fr_prev;
2824 if (p_spr || p_sb)
2825 {
2826 target_fr = frp->fr_prev;
2827 other_fr = frp->fr_next;
2828 }
2829
2830 /* If 'wfh' or 'wfw' is set for the target and not for the alternate
2831 * window, reverse the selection. */
2832 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2833 {
2834 if (frame_fixed_width(target_fr) && !frame_fixed_width(other_fr))
2835 target_fr = other_fr;
2836 }
2837 else
2838 {
2839 if (frame_fixed_height(target_fr) && !frame_fixed_height(other_fr))
2840 target_fr = other_fr;
2841 }
2842
2843 return target_fr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844}
2845
2846/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002847 * Return the tabpage that will be used if the current one is closed.
2848 */
2849 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002850alt_tabpage(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002851{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002852 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002853
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002854 /* Use the next tab page if possible. */
2855 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002856 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002857
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002858 /* Find the last but one tab page. */
2859 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2860 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002861 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002862}
2863
2864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 * Find the left-upper window in frame "frp".
2866 */
2867 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002868frame2win(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869{
2870 while (frp->fr_win == NULL)
2871 frp = frp->fr_child;
2872 return frp->fr_win;
2873}
2874
2875/*
2876 * Return TRUE if frame "frp" contains window "wp".
2877 */
2878 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002879frame_has_win(frame_T *frp, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880{
2881 frame_T *p;
2882
2883 if (frp->fr_layout == FR_LEAF)
2884 return frp->fr_win == wp;
2885
2886 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2887 if (frame_has_win(p, wp))
2888 return TRUE;
2889 return FALSE;
2890}
2891
2892/*
2893 * Set a new height for a frame. Recursively sets the height for contained
2894 * frames and windows. Caller must take care of positions.
2895 */
2896 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002897frame_new_height(
2898 frame_T *topfrp,
2899 int height,
2900 int topfirst, /* resize topmost contained frame first */
2901 int wfh) /* obey 'winfixheight' when there is a choice;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 may cause the height not to be set */
2903{
2904 frame_T *frp;
2905 int extra_lines;
2906 int h;
2907
2908 if (topfrp->fr_win != NULL)
2909 {
2910 /* Simple case: just one window. */
2911 win_new_height(topfrp->fr_win,
Bram Moolenaard326ad62017-09-18 20:31:41 +02002912 height - topfrp->fr_win->w_status_height
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01002913 - WINBAR_HEIGHT(topfrp->fr_win));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 else if (topfrp->fr_layout == FR_ROW)
2916 {
2917 do
2918 {
2919 /* All frames in this row get the same new height. */
2920 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2921 {
2922 frame_new_height(frp, height, topfirst, wfh);
2923 if (frp->fr_height > height)
2924 {
2925 /* Could not fit the windows, make the whole row higher. */
2926 height = frp->fr_height;
2927 break;
2928 }
2929 }
2930 }
2931 while (frp != NULL);
2932 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002933 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 {
2935 /* Complicated case: Resize a column of frames. Resize the bottom
2936 * frame first, frames above that when needed. */
2937
2938 frp = topfrp->fr_child;
2939 if (wfh)
2940 /* Advance past frames with one window with 'wfh' set. */
2941 while (frame_fixed_height(frp))
2942 {
2943 frp = frp->fr_next;
2944 if (frp == NULL)
2945 return; /* no frame without 'wfh', give up */
2946 }
2947 if (!topfirst)
2948 {
2949 /* Find the bottom frame of this column */
2950 while (frp->fr_next != NULL)
2951 frp = frp->fr_next;
2952 if (wfh)
2953 /* Advance back for frames with one window with 'wfh' set. */
2954 while (frame_fixed_height(frp))
2955 frp = frp->fr_prev;
2956 }
2957
2958 extra_lines = height - topfrp->fr_height;
2959 if (extra_lines < 0)
2960 {
2961 /* reduce height of contained frames, bottom or top frame first */
2962 while (frp != NULL)
2963 {
2964 h = frame_minheight(frp, NULL);
2965 if (frp->fr_height + extra_lines < h)
2966 {
2967 extra_lines += frp->fr_height - h;
2968 frame_new_height(frp, h, topfirst, wfh);
2969 }
2970 else
2971 {
2972 frame_new_height(frp, frp->fr_height + extra_lines,
2973 topfirst, wfh);
2974 break;
2975 }
2976 if (topfirst)
2977 {
2978 do
2979 frp = frp->fr_next;
2980 while (wfh && frp != NULL && frame_fixed_height(frp));
2981 }
2982 else
2983 {
2984 do
2985 frp = frp->fr_prev;
2986 while (wfh && frp != NULL && frame_fixed_height(frp));
2987 }
2988 /* Increase "height" if we could not reduce enough frames. */
2989 if (frp == NULL)
2990 height -= extra_lines;
2991 }
2992 }
2993 else if (extra_lines > 0)
2994 {
2995 /* increase height of bottom or top frame */
2996 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2997 }
2998 }
2999 topfrp->fr_height = height;
3000}
3001
3002/*
3003 * Return TRUE if height of frame "frp" should not be changed because of
3004 * the 'winfixheight' option.
3005 */
3006 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003007frame_fixed_height(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
3009 /* frame with one window: fixed height if 'winfixheight' set. */
3010 if (frp->fr_win != NULL)
3011 return frp->fr_win->w_p_wfh;
3012
3013 if (frp->fr_layout == FR_ROW)
3014 {
3015 /* The frame is fixed height if one of the frames in the row is fixed
3016 * height. */
3017 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3018 if (frame_fixed_height(frp))
3019 return TRUE;
3020 return FALSE;
3021 }
3022
3023 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
3024 * frames in the row are fixed height. */
3025 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3026 if (!frame_fixed_height(frp))
3027 return FALSE;
3028 return TRUE;
3029}
3030
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003032 * Return TRUE if width of frame "frp" should not be changed because of
3033 * the 'winfixwidth' option.
3034 */
3035 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003036frame_fixed_width(frame_T *frp)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003037{
3038 /* frame with one window: fixed width if 'winfixwidth' set. */
3039 if (frp->fr_win != NULL)
3040 return frp->fr_win->w_p_wfw;
3041
3042 if (frp->fr_layout == FR_COL)
3043 {
3044 /* The frame is fixed width if one of the frames in the row is fixed
3045 * width. */
3046 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3047 if (frame_fixed_width(frp))
3048 return TRUE;
3049 return FALSE;
3050 }
3051
3052 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
3053 * frames in the row are fixed width. */
3054 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3055 if (!frame_fixed_width(frp))
3056 return FALSE;
3057 return TRUE;
3058}
3059
3060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 * Add a status line to windows at the bottom of "frp".
3062 * Note: Does not check if there is room!
3063 */
3064 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003065frame_add_statusline(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
3067 win_T *wp;
3068
3069 if (frp->fr_layout == FR_LEAF)
3070 {
3071 wp = frp->fr_win;
3072 if (wp->w_status_height == 0)
3073 {
3074 if (wp->w_height > 0) /* don't make it negative */
3075 --wp->w_height;
3076 wp->w_status_height = STATUS_HEIGHT;
3077 }
3078 }
3079 else if (frp->fr_layout == FR_ROW)
3080 {
3081 /* Handle all the frames in the row. */
3082 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3083 frame_add_statusline(frp);
3084 }
3085 else /* frp->fr_layout == FR_COL */
3086 {
3087 /* Only need to handle the last frame in the column. */
3088 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3089 ;
3090 frame_add_statusline(frp);
3091 }
3092}
3093
3094/*
3095 * Set width of a frame. Handles recursively going through contained frames.
3096 * May remove separator line for windows at the right side (for win_close()).
3097 */
3098 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003099frame_new_width(
3100 frame_T *topfrp,
3101 int width,
3102 int leftfirst, /* resize leftmost contained frame first */
3103 int wfw) /* obey 'winfixwidth' when there is a choice;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003104 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
3106 frame_T *frp;
3107 int extra_cols;
3108 int w;
3109 win_T *wp;
3110
3111 if (topfrp->fr_layout == FR_LEAF)
3112 {
3113 /* Simple case: just one window. */
3114 wp = topfrp->fr_win;
3115 /* Find out if there are any windows right of this one. */
3116 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3117 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3118 break;
3119 if (frp->fr_parent == NULL)
3120 wp->w_vsep_width = 0;
3121 win_new_width(wp, width - wp->w_vsep_width);
3122 }
3123 else if (topfrp->fr_layout == FR_COL)
3124 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003125 do
3126 {
3127 /* All frames in this column get the same new width. */
3128 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3129 {
3130 frame_new_width(frp, width, leftfirst, wfw);
3131 if (frp->fr_width > width)
3132 {
3133 /* Could not fit the windows, make whole column wider. */
3134 width = frp->fr_width;
3135 break;
3136 }
3137 }
3138 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 }
3140 else /* fr_layout == FR_ROW */
3141 {
3142 /* Complicated case: Resize a row of frames. Resize the rightmost
3143 * frame first, frames left of it when needed. */
3144
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003146 if (wfw)
3147 /* Advance past frames with one window with 'wfw' set. */
3148 while (frame_fixed_width(frp))
3149 {
3150 frp = frp->fr_next;
3151 if (frp == NULL)
3152 return; /* no frame without 'wfw', give up */
3153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003155 {
3156 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 while (frp->fr_next != NULL)
3158 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003159 if (wfw)
3160 /* Advance back for frames with one window with 'wfw' set. */
3161 while (frame_fixed_width(frp))
3162 frp = frp->fr_prev;
3163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
3165 extra_cols = width - topfrp->fr_width;
3166 if (extra_cols < 0)
3167 {
3168 /* reduce frame width, rightmost frame first */
3169 while (frp != NULL)
3170 {
3171 w = frame_minwidth(frp, NULL);
3172 if (frp->fr_width + extra_cols < w)
3173 {
3174 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003175 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 }
3177 else
3178 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003179 frame_new_width(frp, frp->fr_width + extra_cols,
3180 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 break;
3182 }
3183 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003184 {
3185 do
3186 frp = frp->fr_next;
3187 while (wfw && frp != NULL && frame_fixed_width(frp));
3188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003190 {
3191 do
3192 frp = frp->fr_prev;
3193 while (wfw && frp != NULL && frame_fixed_width(frp));
3194 }
3195 /* Increase "width" if we could not reduce enough frames. */
3196 if (frp == NULL)
3197 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
3199 }
3200 else if (extra_cols > 0)
3201 {
3202 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003203 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 }
3205 }
3206 topfrp->fr_width = width;
3207}
3208
3209/*
3210 * Add the vertical separator to windows at the right side of "frp".
3211 * Note: Does not check if there is room!
3212 */
3213 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003214frame_add_vsep(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215{
3216 win_T *wp;
3217
3218 if (frp->fr_layout == FR_LEAF)
3219 {
3220 wp = frp->fr_win;
3221 if (wp->w_vsep_width == 0)
3222 {
3223 if (wp->w_width > 0) /* don't make it negative */
3224 --wp->w_width;
3225 wp->w_vsep_width = 1;
3226 }
3227 }
3228 else if (frp->fr_layout == FR_COL)
3229 {
3230 /* Handle all the frames in the column. */
3231 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3232 frame_add_vsep(frp);
3233 }
3234 else /* frp->fr_layout == FR_ROW */
3235 {
3236 /* Only need to handle the last frame in the row. */
3237 frp = frp->fr_child;
3238 while (frp->fr_next != NULL)
3239 frp = frp->fr_next;
3240 frame_add_vsep(frp);
3241 }
3242}
3243
3244/*
3245 * Set frame width from the window it contains.
3246 */
3247 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003248frame_fix_width(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249{
3250 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3251}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
3253/*
3254 * Set frame height from the window it contains.
3255 */
3256 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003257frame_fix_height(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258{
Bram Moolenaar415a6932017-12-05 20:31:07 +01003259 wp->w_frame->fr_height = VISIBLE_HEIGHT(wp) + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260}
3261
3262/*
3263 * Compute the minimal height for frame "topfrp".
3264 * Uses the 'winminheight' option.
3265 * When "next_curwin" isn't NULL, use p_wh for this window.
3266 * When "next_curwin" is NOWIN, don't use at least one line for the current
3267 * window.
3268 */
3269 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003270frame_minheight(frame_T *topfrp, win_T *next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
3272 frame_T *frp;
3273 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
3276 if (topfrp->fr_win != NULL)
3277 {
3278 if (topfrp->fr_win == next_curwin)
3279 m = p_wh + topfrp->fr_win->w_status_height;
3280 else
3281 {
3282 /* window: minimal height of the window plus status line */
3283 m = p_wmh + topfrp->fr_win->w_status_height;
Bram Moolenaar415a6932017-12-05 20:31:07 +01003284 if (topfrp->fr_win == curwin && next_curwin == NULL)
3285 {
3286 /* Current window is minimal one line high and WinBar is
3287 * visible. */
3288 if (p_wmh == 0)
3289 ++m;
3290 m += WINBAR_HEIGHT(curwin);
3291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 }
3293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 else if (topfrp->fr_layout == FR_ROW)
3295 {
3296 /* get the minimal height from each frame in this row */
3297 m = 0;
3298 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3299 {
3300 n = frame_minheight(frp, next_curwin);
3301 if (n > m)
3302 m = n;
3303 }
3304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 else
3306 {
3307 /* Add up the minimal heights for all frames in this column. */
3308 m = 0;
3309 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3310 m += frame_minheight(frp, next_curwin);
3311 }
3312
3313 return m;
3314}
3315
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316/*
3317 * Compute the minimal width for frame "topfrp".
3318 * When "next_curwin" isn't NULL, use p_wiw for this window.
3319 * When "next_curwin" is NOWIN, don't use at least one column for the current
3320 * window.
3321 */
3322 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003323frame_minwidth(
3324 frame_T *topfrp,
3325 win_T *next_curwin) /* use p_wh and p_wiw for next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326{
3327 frame_T *frp;
3328 int m, n;
3329
3330 if (topfrp->fr_win != NULL)
3331 {
3332 if (topfrp->fr_win == next_curwin)
3333 m = p_wiw + topfrp->fr_win->w_vsep_width;
3334 else
3335 {
3336 /* window: minimal width of the window plus separator column */
3337 m = p_wmw + topfrp->fr_win->w_vsep_width;
3338 /* Current window is minimal one column wide */
3339 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3340 ++m;
3341 }
3342 }
3343 else if (topfrp->fr_layout == FR_COL)
3344 {
3345 /* get the minimal width from each frame in this column */
3346 m = 0;
3347 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3348 {
3349 n = frame_minwidth(frp, next_curwin);
3350 if (n > m)
3351 m = n;
3352 }
3353 }
3354 else
3355 {
3356 /* Add up the minimal widths for all frames in this row. */
3357 m = 0;
3358 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3359 m += frame_minwidth(frp, next_curwin);
3360 }
3361
3362 return m;
3363}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364
3365
3366/*
3367 * Try to close all windows except current one.
3368 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3369 * used and the buffer was modified.
3370 *
3371 * Used by ":bdel" and ":only".
3372 */
3373 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003374close_others(
3375 int message,
3376 int forceit) /* always hide all other windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
3378 win_T *wp;
3379 win_T *nextwp;
3380 int r;
3381
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003382 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003384 if (message && !autocmd_busy)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003385 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 return;
3387 }
3388
3389 /* Be very careful here: autocommands may change the window layout. */
3390 for (wp = firstwin; win_valid(wp); wp = nextwp)
3391 {
3392 nextwp = wp->w_next;
3393 if (wp != curwin) /* don't close current window */
3394 {
3395
3396 /* Check if it's allowed to abandon this window */
3397 r = can_abandon(wp->w_buffer, forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 if (!win_valid(wp)) /* autocommands messed wp up */
3399 {
3400 nextwp = firstwin;
3401 continue;
3402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 if (!r)
3404 {
3405#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3406 if (message && (p_confirm || cmdmod.confirm) && p_write)
3407 {
3408 dialog_changed(wp->w_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (!win_valid(wp)) /* autocommands messed wp up */
3410 {
3411 nextwp = firstwin;
3412 continue;
3413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 }
3415 if (bufIsChanged(wp->w_buffer))
3416#endif
3417 continue;
3418 }
Bram Moolenaareb44a682017-08-03 22:44:55 +02003419 win_close(wp, !buf_hide(wp->w_buffer)
3420 && !bufIsChanged(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 }
3422 }
3423
Bram Moolenaar459ca562016-11-10 18:16:33 +01003424 if (message && !ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 EMSG(_("E445: Other window contains changes"));
3426}
3427
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003429 * Init the current window "curwin".
3430 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 */
3432 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003433curwin_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003435 win_init_empty(curwin);
3436}
3437
3438 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003439win_init_empty(win_T *wp)
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003440{
3441 redraw_win_later(wp, NOT_VALID);
3442 wp->w_lines_valid = 0;
3443 wp->w_cursor.lnum = 1;
3444 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003446 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003448 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3449 wp->w_pcmark.col = 0;
3450 wp->w_prev_pcmark.lnum = 0;
3451 wp->w_prev_pcmark.col = 0;
3452 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003454 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003456 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003458 if (wp->w_p_rl)
3459 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003461 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003463#ifdef FEAT_SYN_HL
3464 wp->w_s = &wp->w_buffer->b_s;
3465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466}
3467
3468/*
3469 * Allocate the first window and put an empty buffer in it.
3470 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003471 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003473 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003474win_alloc_first(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003476 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003477 return FAIL;
3478
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003479 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003480 if (first_tabpage == NULL)
3481 return FAIL;
3482 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003483 curtab = first_tabpage;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003484
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003485 return OK;
3486}
3487
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003488/*
3489 * Init "aucmd_win". This can only be done after the first
3490 * window is fully initialized, thus it can't be in win_alloc_first().
3491 */
3492 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003493win_alloc_aucmd_win(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003494{
3495 aucmd_win = win_alloc(NULL, TRUE);
3496 if (aucmd_win != NULL)
3497 {
3498 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003499 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003500 new_frame(aucmd_win);
3501 }
3502}
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003503
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003504/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003505 * Allocate the first window or the first window in a new tab page.
3506 * When "oldwin" is NULL create an empty buffer for it.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003507 * When "oldwin" is not NULL copy info from it to the new window.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003508 * Return FAIL when something goes wrong (out of memory).
3509 */
3510 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003511win_alloc_firstwin(win_T *oldwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003512{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003513 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003514 if (oldwin == NULL)
3515 {
3516 /* Very first window, need to create an empty buffer for it and
3517 * initialize from scratch. */
3518 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3519 if (curwin == NULL || curbuf == NULL)
3520 return FAIL;
3521 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003522#ifdef FEAT_SYN_HL
3523 curwin->w_s = &(curbuf->b_s);
3524#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003525 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003526 curwin->w_alist = &global_alist;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003527 curwin_init(); /* init current window */
3528 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003529 else
3530 {
3531 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003532 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003533
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003534 /* We don't want cursor- and scroll-binding in the first window. */
3535 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003538 new_frame(curwin);
3539 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003540 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003541 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 topframe->fr_height = Rows - p_ch;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003544
3545 return OK;
3546}
3547
3548/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003549 * Create a frame for window "wp".
3550 */
3551 static void
3552new_frame(win_T *wp)
3553{
3554 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3555
3556 wp->w_frame = frp;
3557 if (frp != NULL)
3558 {
3559 frp->fr_layout = FR_LEAF;
3560 frp->fr_win = wp;
3561 }
3562}
3563
3564/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003565 * Initialize the window and frame size to the maximum.
3566 */
3567 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003568win_init_size(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003569{
3570 firstwin->w_height = ROWS_AVAIL;
3571 topframe->fr_height = ROWS_AVAIL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003572 firstwin->w_width = Columns;
3573 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574}
3575
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003576/*
3577 * Allocate a new tabpage_T and init the values.
3578 * Returns NULL when out of memory.
3579 */
3580 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003581alloc_tabpage(void)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003582{
3583 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003584# ifdef FEAT_GUI
3585 int i;
3586# endif
3587
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003588
3589 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003590 if (tp == NULL)
3591 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003592
Bram Moolenaar429fa852013-04-15 12:27:36 +02003593# ifdef FEAT_EVAL
3594 /* init t: variables */
3595 tp->tp_vars = dict_alloc();
3596 if (tp->tp_vars == NULL)
3597 {
3598 vim_free(tp);
3599 return NULL;
3600 }
3601 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3602# endif
3603
3604# ifdef FEAT_GUI
3605 for (i = 0; i < 3; i++)
3606 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003607# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003608# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003609 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003610# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003611 tp->tp_ch_used = p_ch;
3612
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003613 return tp;
3614}
3615
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003616 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003617free_tabpage(tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003618{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003619 int idx;
3620
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003621# ifdef FEAT_DIFF
3622 diff_clear(tp);
3623# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003624 for (idx = 0; idx < SNAP_COUNT; ++idx)
3625 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003626#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003627 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3628 hash_init(&tp->tp_vars->dv_hashtab);
3629 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003630#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003631
3632#ifdef FEAT_PYTHON
3633 python_tabpage_free(tp);
3634#endif
3635
3636#ifdef FEAT_PYTHON3
3637 python3_tabpage_free(tp);
3638#endif
3639
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003640 vim_free(tp);
3641}
3642
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003643/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003644 * Create a new Tab page with one window.
3645 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003646 * When "after" is 0 put it just after the current Tab page.
3647 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003648 * Return FAIL or OK.
3649 */
3650 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003651win_new_tabpage(int after)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003652{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003653 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003654 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003655 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003656
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003657 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003658 if (newtp == NULL)
3659 return FAIL;
3660
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003661 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003662 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003663 {
3664 vim_free(newtp);
3665 return FAIL;
3666 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003667 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003668
3669 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003670 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003671 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003672 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003673 if (after == 1)
3674 {
3675 /* New tab page becomes the first one. */
3676 newtp->tp_next = first_tabpage;
3677 first_tabpage = newtp;
3678 }
3679 else
3680 {
3681 if (after > 0)
3682 {
3683 /* Put new tab page before tab page "after". */
3684 n = 2;
3685 for (tp = first_tabpage; tp->tp_next != NULL
3686 && n < after; tp = tp->tp_next)
3687 ++n;
3688 }
3689 newtp->tp_next = tp->tp_next;
3690 tp->tp_next = newtp;
3691 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003692 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003693 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003694 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003695
3696 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003697 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003698
3699#if defined(FEAT_GUI)
3700 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3701 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003702 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003703#endif
Bram Moolenaar6d41c782018-06-06 09:11:12 +02003704#ifdef FEAT_JOB_CHANNEL
3705 entering_window(curwin);
3706#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003707
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003708 redraw_all_later(CLEAR);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003709 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003710 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003711 apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003712 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003713 return OK;
3714 }
3715
3716 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003717 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003718 return FAIL;
3719}
3720
3721/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003722 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3723 * like with ":split".
3724 * Returns OK if a new tab page was created, FAIL otherwise.
3725 */
3726 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003727may_open_tabpage(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003728{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003729 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003730
Bram Moolenaard326ce82007-03-11 14:48:29 +00003731 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003732 {
3733 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003734 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003735 return win_new_tabpage(n);
3736 }
3737 return FAIL;
3738}
3739
3740/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003741 * Create up to "maxcount" tabpages with empty windows.
3742 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003743 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003744 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003745make_tabpages(int maxcount)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003746{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003747 int count = maxcount;
3748 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003749
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003750 /* Limit to 'tabpagemax' tabs. */
3751 if (count > p_tpm)
3752 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003753
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003754 /*
3755 * Don't execute autocommands while creating the tab pages. Must do that
3756 * when putting the buffers in the windows.
3757 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003758 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003759
3760 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003761 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003762 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003763
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003764 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003765
3766 /* return actual number of tab pages */
3767 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003768}
3769
3770/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003771 * Return TRUE when "tpc" points to a valid tab page.
3772 */
3773 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003774valid_tabpage(tabpage_T *tpc)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003775{
3776 tabpage_T *tp;
3777
Bram Moolenaar29323592016-07-24 22:04:11 +02003778 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003779 if (tp == tpc)
3780 return TRUE;
3781 return FALSE;
3782}
3783
3784/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003785 * Return TRUE when "tpc" points to a valid tab page and at least one window is
3786 * valid.
3787 */
3788 int
3789valid_tabpage_win(tabpage_T *tpc)
3790{
3791 tabpage_T *tp;
3792 win_T *wp;
3793
3794 FOR_ALL_TABPAGES(tp)
3795 {
3796 if (tp == tpc)
3797 {
3798 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
3799 {
3800 if (win_valid_any_tab(wp))
3801 return TRUE;
3802 }
3803 return FALSE;
3804 }
3805 }
3806 /* shouldn't happen */
3807 return FALSE;
3808}
3809
3810/*
3811 * Close tabpage "tab", assuming it has no windows in it.
3812 * There must be another tabpage or this will crash.
3813 */
3814 void
3815close_tabpage(tabpage_T *tab)
3816{
3817 tabpage_T *ptp;
3818
3819 if (tab == first_tabpage)
3820 {
3821 first_tabpage = tab->tp_next;
3822 ptp = first_tabpage;
3823 }
3824 else
3825 {
3826 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tab;
3827 ptp = ptp->tp_next)
3828 ;
Bram Moolenaara37ffaa2017-03-21 21:58:00 +01003829 assert(ptp != NULL);
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003830 ptp->tp_next = tab->tp_next;
3831 }
3832
3833 goto_tabpage_tp(ptp, FALSE, FALSE);
3834 free_tabpage(tab);
3835}
3836
3837/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003838 * Find tab page "n" (first one is 1). Returns NULL when not found.
3839 */
3840 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003841find_tabpage(int n)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003842{
3843 tabpage_T *tp;
3844 int i = 1;
3845
3846 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3847 ++i;
3848 return tp;
3849}
3850
3851/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003852 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003853 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003854 */
3855 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003856tabpage_index(tabpage_T *ftp)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003857{
3858 int i = 1;
3859 tabpage_T *tp;
3860
3861 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3862 ++i;
3863 return i;
3864}
3865
3866/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003867 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003868 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003869 * FAIL.
3870 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003871 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003872 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003873leave_tabpage(
3874 buf_T *new_curbuf UNUSED, /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003875 NULL if unknown */
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003876 int trigger_leave_autocmds UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003877{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003878 tabpage_T *tp = curtab;
3879
Bram Moolenaar6d41c782018-06-06 09:11:12 +02003880#ifdef FEAT_JOB_CHANNEL
3881 leaving_window(curwin);
3882#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003883 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003884 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003885 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003886 if (new_curbuf != curbuf)
3887 {
3888 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3889 if (curtab != tp)
3890 return FAIL;
3891 }
3892 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3893 if (curtab != tp)
3894 return FAIL;
3895 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003896 if (curtab != tp)
3897 return FAIL;
3898 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003899#if defined(FEAT_GUI)
3900 /* Remove the scrollbars. They may be added back later. */
3901 if (gui.in_use)
3902 gui_remove_scrollbars();
3903#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003904 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003905 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003906 tp->tp_firstwin = firstwin;
3907 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003908 tp->tp_old_Rows = Rows;
3909 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003910 firstwin = NULL;
3911 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003912 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003913}
3914
3915/*
3916 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003917 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003918 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3919 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003920 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003921 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003922enter_tabpage(
3923 tabpage_T *tp,
3924 buf_T *old_curbuf UNUSED,
Bram Moolenaarf1d25012016-03-03 12:22:53 +01003925 int trigger_enter_autocmds,
3926 int trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003927{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003928 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003929 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003930
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003931 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003932 firstwin = tp->tp_firstwin;
3933 lastwin = tp->tp_lastwin;
3934 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003935
3936 /* We would like doing the TabEnter event first, but we don't have a
3937 * valid current window yet, which may break some commands.
3938 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaarc917da42016-07-19 22:31:36 +02003939 win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE,
Bram Moolenaard6949742013-06-16 14:18:28 +02003940 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003941 prevwin = next_prevwin;
3942
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003943 last_status(FALSE); /* status line may appear or disappear */
3944 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003945 must_redraw = CLEAR; /* need to redraw everything */
3946#ifdef FEAT_DIFF
3947 diff_need_scrollbind = TRUE;
3948#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003949
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003950 /* The tabpage line may have appeared or disappeared, may need to resize
3951 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003952 * frame sizes too. Use the stored value of p_ch, so that it can be
3953 * different for each tab page. */
3954 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003955 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3956#ifdef FEAT_GUI_TABLINE
3957 && !gui_use_tabline()
3958#endif
3959 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003960 shell_new_rows();
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003961 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003962 shell_new_columns(); /* update window widths */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003963
3964#if defined(FEAT_GUI)
3965 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3966 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003967 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003968#endif
3969
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003970 /* Apply autocommands after updating the display, when 'rows' and
3971 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003972 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003973 {
3974 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3975 if (old_curbuf != curbuf)
3976 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3977 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003978
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003979 redraw_all_later(CLEAR);
3980}
3981
3982/*
3983 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003984 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003985 */
3986 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003987goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003988{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003989 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003990 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003991 int i;
3992
Bram Moolenaard68071d2006-05-02 22:08:30 +00003993 if (text_locked())
3994 {
3995 /* Not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02003996 text_locked_msg();
Bram Moolenaard68071d2006-05-02 22:08:30 +00003997 return;
3998 }
3999
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004000 /* If there is only one it can't work. */
4001 if (first_tabpage->tp_next == NULL)
4002 {
4003 if (n > 1)
4004 beep_flush();
4005 return;
4006 }
4007
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004008 if (n == 0)
4009 {
4010 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004011 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004012 tp = first_tabpage;
4013 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004014 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004015 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004016 else if (n < 0)
4017 {
4018 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
4019 * this N times. */
4020 ttp = curtab;
4021 for (i = n; i < 0; ++i)
4022 {
4023 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
4024 tp = tp->tp_next)
4025 ;
4026 ttp = tp;
4027 }
4028 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004029 else if (n == 9999)
4030 {
4031 /* Go to last tab page. */
4032 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
4033 ;
4034 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004035 else
4036 {
4037 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004038 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004039 if (tp == NULL)
4040 {
4041 beep_flush();
4042 return;
4043 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004044 }
4045
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004046 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004047
4048#ifdef FEAT_GUI_TABLINE
4049 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004050 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004051#endif
4052}
4053
4054/*
4055 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004056 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4057 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004058 * Note: doesn't update the GUI tab.
4059 */
4060 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004061goto_tabpage_tp(
4062 tabpage_T *tp,
4063 int trigger_enter_autocmds,
4064 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004065{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02004066 /* Don't repeat a message in another tab page. */
4067 set_keep_msg(NULL, 0);
4068
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004069 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4070 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004071 {
4072 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004073 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4074 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004075 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004076 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4077 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004078 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004079}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004082 * Enter window "wp" in tab page "tp".
4083 * Also updates the GUI tab.
4084 */
4085 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004086goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004087{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004088 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004089 if (curtab == tp && win_valid(wp))
4090 {
4091 win_enter(wp, TRUE);
4092# ifdef FEAT_GUI_TABLINE
4093 if (gui_use_tabline())
4094 gui_mch_set_curtab(tabpage_index(curtab));
4095# endif
4096 }
4097}
4098
4099/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004100 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004101 */
4102 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004103tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004104{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004105 int n = 1;
4106 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004107
4108 if (first_tabpage->tp_next == NULL)
4109 return;
4110
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004111 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4112 ++n;
4113
4114 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4115 && tp->tp_next == curtab))
4116 return;
4117
4118 tp_dst = tp;
4119
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004120 /* Remove the current tab page from the list of tab pages. */
4121 if (curtab == first_tabpage)
4122 first_tabpage = curtab->tp_next;
4123 else
4124 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004125 FOR_ALL_TABPAGES(tp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004126 if (tp->tp_next == curtab)
4127 break;
4128 if (tp == NULL) /* "cannot happen" */
4129 return;
4130 tp->tp_next = curtab->tp_next;
4131 }
4132
4133 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004134 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004135 {
4136 curtab->tp_next = first_tabpage;
4137 first_tabpage = curtab;
4138 }
4139 else
4140 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004141 curtab->tp_next = tp_dst->tp_next;
4142 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004143 }
4144
4145 /* Need to redraw the tabline. Tab page contents doesn't change. */
4146 redraw_tabline = TRUE;
4147}
4148
4149
4150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 * Go to another window.
4152 * When jumping to another buffer, stop Visual mode. Do this before
4153 * changing windows so we can yank the selection into the '*' register.
4154 * When jumping to another window on the same buffer, adjust its cursor
4155 * position to keep the same Visual area.
4156 */
4157 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004158win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004160#ifdef FEAT_CONCEAL
4161 win_T *owp = curwin;
4162#endif
4163
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004164 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 {
4166 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004167 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return;
4169 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004170 if (curbuf_locked())
4171 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004172
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 if (wp->w_buffer != curbuf)
4174 reset_VIsual_and_resel();
4175 else if (VIsual_active)
4176 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
4178#ifdef FEAT_GUI
4179 need_mouse_correct = TRUE;
4180#endif
4181 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004182
4183#ifdef FEAT_CONCEAL
4184 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004185 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004186 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004187 if (curwin->w_p_cole > 0 && !msg_scrolled)
4188 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190}
4191
4192#if defined(FEAT_PERL) || defined(PROTO)
4193/*
4194 * Find window number "winnr" (counting top to bottom).
4195 */
4196 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004197win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198{
4199 win_T *wp;
4200
Bram Moolenaar29323592016-07-24 22:04:11 +02004201 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 if (--winnr == 0)
4203 break;
4204 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205}
4206#endif
4207
Bram Moolenaar4033c552017-09-16 20:54:51 +02004208#if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004209/*
4210 * Find the tabpage for window "win".
4211 */
4212 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004213win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004214{
4215 win_T *wp;
4216 tabpage_T *tp;
4217
Bram Moolenaar29323592016-07-24 22:04:11 +02004218 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004219 if (wp == win)
4220 return tp;
4221 return NULL;
4222}
4223#endif
4224
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225/*
4226 * Move to window above or below "count" times.
4227 */
4228 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004229win_goto_ver(
4230 int up, /* TRUE to go to win above */
4231 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232{
4233 frame_T *fr;
4234 frame_T *nfr;
4235 frame_T *foundfr;
4236
4237 foundfr = curwin->w_frame;
4238 while (count--)
4239 {
4240 /*
4241 * First go upwards in the tree of frames until we find a upwards or
4242 * downwards neighbor.
4243 */
4244 fr = foundfr;
4245 for (;;)
4246 {
4247 if (fr == topframe)
4248 goto end;
4249 if (up)
4250 nfr = fr->fr_prev;
4251 else
4252 nfr = fr->fr_next;
4253 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4254 break;
4255 fr = fr->fr_parent;
4256 }
4257
4258 /*
4259 * Now go downwards to find the bottom or top frame in it.
4260 */
4261 for (;;)
4262 {
4263 if (nfr->fr_layout == FR_LEAF)
4264 {
4265 foundfr = nfr;
4266 break;
4267 }
4268 fr = nfr->fr_child;
4269 if (nfr->fr_layout == FR_ROW)
4270 {
4271 /* Find the frame at the cursor row. */
4272 while (fr->fr_next != NULL
4273 && frame2win(fr)->w_wincol + fr->fr_width
4274 <= curwin->w_wincol + curwin->w_wcol)
4275 fr = fr->fr_next;
4276 }
4277 if (nfr->fr_layout == FR_COL && up)
4278 while (fr->fr_next != NULL)
4279 fr = fr->fr_next;
4280 nfr = fr;
4281 }
4282 }
4283end:
4284 if (foundfr != NULL)
4285 win_goto(foundfr->fr_win);
4286}
4287
4288/*
4289 * Move to left or right window.
4290 */
4291 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004292win_goto_hor(
4293 int left, /* TRUE to go to left win */
4294 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295{
4296 frame_T *fr;
4297 frame_T *nfr;
4298 frame_T *foundfr;
4299
4300 foundfr = curwin->w_frame;
4301 while (count--)
4302 {
4303 /*
4304 * First go upwards in the tree of frames until we find a left or
4305 * right neighbor.
4306 */
4307 fr = foundfr;
4308 for (;;)
4309 {
4310 if (fr == topframe)
4311 goto end;
4312 if (left)
4313 nfr = fr->fr_prev;
4314 else
4315 nfr = fr->fr_next;
4316 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4317 break;
4318 fr = fr->fr_parent;
4319 }
4320
4321 /*
4322 * Now go downwards to find the leftmost or rightmost frame in it.
4323 */
4324 for (;;)
4325 {
4326 if (nfr->fr_layout == FR_LEAF)
4327 {
4328 foundfr = nfr;
4329 break;
4330 }
4331 fr = nfr->fr_child;
4332 if (nfr->fr_layout == FR_COL)
4333 {
4334 /* Find the frame at the cursor row. */
4335 while (fr->fr_next != NULL
4336 && frame2win(fr)->w_winrow + fr->fr_height
4337 <= curwin->w_winrow + curwin->w_wrow)
4338 fr = fr->fr_next;
4339 }
4340 if (nfr->fr_layout == FR_ROW && left)
4341 while (fr->fr_next != NULL)
4342 fr = fr->fr_next;
4343 nfr = fr;
4344 }
4345 }
4346end:
4347 if (foundfr != NULL)
4348 win_goto(foundfr->fr_win);
4349}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350
4351/*
4352 * Make window "wp" the current window.
4353 */
4354 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004355win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356{
Bram Moolenaarc917da42016-07-19 22:31:36 +02004357 win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358}
4359
4360/*
4361 * Make window wp the current window.
4362 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4363 * been closed and isn't valid.
4364 */
4365 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004366win_enter_ext(
4367 win_T *wp,
4368 int undo_sync,
4369 int curwin_invalid,
Bram Moolenaar6f470022018-04-10 18:47:20 +02004370 int trigger_new_autocmds,
4371 int trigger_enter_autocmds,
4372 int trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375
4376 if (wp == curwin && !curwin_invalid) /* nothing to do */
4377 return;
4378
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004379#ifdef FEAT_JOB_CHANNEL
4380 if (!curwin_invalid)
4381 leaving_window(curwin);
4382#endif
4383
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004384 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
4386 /*
4387 * Be careful: If autocommands delete the window, return now.
4388 */
4389 if (wp->w_buffer != curbuf)
4390 {
4391 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4392 other_buffer = TRUE;
4393 if (!win_valid(wp))
4394 return;
4395 }
4396 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4397 if (!win_valid(wp))
4398 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004399#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 /* autocmds may abort script processing */
4401 if (aborting())
4402 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
4406 /* sync undo before leaving the current buffer */
4407 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004408 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004409
4410 /* Might need to scroll the old window before switching, e.g., when the
4411 * cursor was moved. */
4412 update_topline();
4413
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 /* may have to copy the buffer options when 'cpo' contains 'S' */
4415 if (wp->w_buffer != curbuf)
4416 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4417 if (!curwin_invalid)
4418 {
4419 prevwin = curwin; /* remember for CTRL-W p */
4420 curwin->w_redr_status = TRUE;
4421 }
4422 curwin = wp;
4423 curbuf = wp->w_buffer;
4424 check_cursor();
4425#ifdef FEAT_VIRTUALEDIT
4426 if (!virtual_active())
4427 curwin->w_cursor.coladd = 0;
4428#endif
4429 changed_line_abv_curs(); /* assume cursor position needs updating */
4430
4431 if (curwin->w_localdir != NULL)
4432 {
4433 /* Window has a local directory: Save current directory as global
4434 * directory (unless that was done already) and change to the local
4435 * directory. */
4436 if (globaldir == NULL)
4437 {
4438 char_u cwd[MAXPATHL];
4439
4440 if (mch_dirname(cwd, MAXPATHL) == OK)
4441 globaldir = vim_strsave(cwd);
4442 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004443 if (mch_chdir((char *)curwin->w_localdir) == 0)
4444 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
4446 else if (globaldir != NULL)
4447 {
4448 /* Window doesn't have a local directory and we are not in the global
4449 * directory: Change to the global directory. */
Bram Moolenaar42335f52018-09-13 15:33:43 +02004450 vim_ignored = mch_chdir((char *)globaldir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004451 VIM_CLEAR(globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 shorten_fnames(TRUE);
4453 }
4454
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004455#ifdef FEAT_JOB_CHANNEL
4456 entering_window(curwin);
4457#endif
Bram Moolenaarc917da42016-07-19 22:31:36 +02004458 if (trigger_new_autocmds)
4459 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004460 if (trigger_enter_autocmds)
4461 {
4462 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4463 if (other_buffer)
4464 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
4467#ifdef FEAT_TITLE
4468 maketitle();
4469#endif
4470 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004471 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 if (restart_edit)
4473 redraw_later(VALID); /* causes status line redraw */
4474
4475 /* set window height to desired minimal value */
4476 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4477 win_setheight((int)p_wh);
4478 else if (curwin->w_height == 0)
4479 win_setheight(1);
4480
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004482 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 win_setwidth((int)p_wiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
4485#ifdef FEAT_MOUSE
4486 setmouse(); /* in case jumped to/from help buffer */
4487#endif
4488
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004489 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02004490 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491}
4492
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004495 * Jump to the first open window that contains buffer "buf", if one exists.
4496 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 */
4498 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004499buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004501 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004503 if (curwin->w_buffer == buf)
4504 wp = curwin;
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004505 else
Bram Moolenaar29323592016-07-24 22:04:11 +02004506 FOR_ALL_WINDOWS(wp)
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004507 if (wp->w_buffer == buf)
4508 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 if (wp != NULL)
4510 win_enter(wp, FALSE);
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004511 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004513
4514/*
4515 * Jump to the first open window in any tab page that contains buffer "buf",
4516 * if one exists.
4517 * Returns a pointer to the window found, otherwise NULL.
4518 */
4519 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004520buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004521{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004522 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004523 tabpage_T *tp;
4524
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004525 if (wp != NULL)
4526 return wp;
4527
Bram Moolenaar29323592016-07-24 22:04:11 +02004528 FOR_ALL_TABPAGES(tp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004529 if (tp != curtab)
4530 {
4531 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4532 if (wp->w_buffer == buf)
4533 break;
4534 if (wp != NULL)
4535 {
4536 goto_tabpage_win(tp, wp);
4537 if (curwin != wp)
4538 wp = NULL; /* something went wrong */
4539 break;
4540 }
4541 }
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004542 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004543}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
Bram Moolenaar888ccac2016-06-04 18:49:36 +02004545static int last_win_id = LOWEST_WIN_ID - 1;
Bram Moolenaar86edef62016-03-13 18:07:30 +01004546
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004548 * Allocate a window structure and link it in the window list when "hidden" is
4549 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004552win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004554 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555
4556 /*
4557 * allocate window structure and linesizes arrays
4558 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004559 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004560 if (new_wp == NULL)
4561 return NULL;
4562
4563 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004565 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004566 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 }
4568
Bram Moolenaar86edef62016-03-13 18:07:30 +01004569 new_wp->w_id = ++last_win_id;
4570
Bram Moolenaar429fa852013-04-15 12:27:36 +02004571#ifdef FEAT_EVAL
4572 /* init w: variables */
4573 new_wp->w_vars = dict_alloc();
4574 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004576 win_free_lsize(new_wp);
4577 vim_free(new_wp);
4578 return NULL;
4579 }
4580 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004581#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004582
Bram Moolenaar429fa852013-04-15 12:27:36 +02004583 /* Don't execute autocommands while the window is not properly
4584 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4585 * event. */
4586 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004587
Bram Moolenaar429fa852013-04-15 12:27:36 +02004588 /*
4589 * link the window in the window list
4590 */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004591 if (!hidden)
4592 win_append(after, new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004593 new_wp->w_wincol = 0;
4594 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595
Bram Moolenaar429fa852013-04-15 12:27:36 +02004596 /* position the display and the cursor at the top of the file. */
4597 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004599 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004601 new_wp->w_botline = 2;
4602 new_wp->w_cursor.lnum = 1;
Bram Moolenaar429fa852013-04-15 12:27:36 +02004603 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
Bram Moolenaar429fa852013-04-15 12:27:36 +02004605 /* We won't calculate w_fraction until resizing the window */
4606 new_wp->w_fraction = 0;
4607 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
4609#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004610 if (gui.in_use)
4611 {
4612 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4613 SBAR_LEFT, new_wp);
4614 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4615 SBAR_RIGHT, new_wp);
4616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617#endif
4618#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004619 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004621 unblock_autocmds();
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004622#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004623 new_wp->w_match_head = NULL;
4624 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004625#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004626 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627}
4628
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004630 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 */
4632 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004633win_free(
4634 win_T *wp,
4635 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636{
4637 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004638 buf_T *buf;
4639 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004641#ifdef FEAT_FOLDING
4642 clearFolding(wp);
4643#endif
4644
4645 /* reduce the reference count to the argument list. */
4646 alist_unlink(wp->w_alist);
4647
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004648 /* Don't execute autocommands while the window is halfway being deleted.
4649 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004650 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004651
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004652#ifdef FEAT_LUA
4653 lua_window_free(wp);
4654#endif
4655
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004656#ifdef FEAT_MZSCHEME
4657 mzscheme_window_free(wp);
4658#endif
4659
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660#ifdef FEAT_PERL
4661 perl_win_free(wp);
4662#endif
4663
4664#ifdef FEAT_PYTHON
4665 python_window_free(wp);
4666#endif
4667
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004668#ifdef FEAT_PYTHON3
4669 python3_window_free(wp);
4670#endif
4671
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672#ifdef FEAT_TCL
4673 tcl_window_free(wp);
4674#endif
4675
4676#ifdef FEAT_RUBY
4677 ruby_window_free(wp);
4678#endif
4679
4680 clear_winopt(&wp->w_onebuf_opt);
4681 clear_winopt(&wp->w_allbuf_opt);
4682
4683#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004684 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4685 hash_init(&wp->w_vars->dv_hashtab);
4686 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687#endif
4688
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004689 {
4690 tabpage_T *ttp;
4691
4692 if (prevwin == wp)
4693 prevwin = NULL;
Bram Moolenaar29323592016-07-24 22:04:11 +02004694 FOR_ALL_TABPAGES(ttp)
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004695 if (ttp->tp_prevwin == wp)
4696 ttp->tp_prevwin = NULL;
4697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 win_free_lsize(wp);
4699
4700 for (i = 0; i < wp->w_tagstacklen; ++i)
4701 vim_free(wp->w_tagstack[i].tagname);
4702
4703 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004704
Bram Moolenaarff18df02013-07-24 17:51:57 +02004705 /* Remove the window from the b_wininfo lists, it may happen that the
4706 * freed memory is re-used for another window. */
Bram Moolenaar29323592016-07-24 22:04:11 +02004707 FOR_ALL_BUFFERS(buf)
Bram Moolenaarff18df02013-07-24 17:51:57 +02004708 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4709 if (wip->wi_win == wp)
4710 wip->wi_win = NULL;
4711
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004713 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004715
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716#ifdef FEAT_JUMPLIST
4717 free_jumplist(wp);
4718#endif
4719
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004720#ifdef FEAT_QUICKFIX
4721 qf_free_all(wp);
4722#endif
4723
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724#ifdef FEAT_GUI
4725 if (gui.in_use)
4726 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4728 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4729 }
4730#endif /* FEAT_GUI */
4731
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004732#ifdef FEAT_MENU
4733 remove_winbar(wp);
4734#endif
4735
Bram Moolenaar860cae12010-06-05 23:22:07 +02004736#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004737 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004738#endif
4739
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004740 if (wp != aucmd_win)
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004741 win_remove(wp, tp);
Bram Moolenaar3be85852014-06-12 14:01:31 +02004742 if (autocmd_busy)
4743 {
4744 wp->w_next = au_pending_free_win;
4745 au_pending_free_win = wp;
4746 }
4747 else
4748 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004749
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004750 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751}
4752
4753/*
4754 * Append window "wp" in the window list after window "after".
4755 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004756 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004757win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758{
4759 win_T *before;
4760
4761 if (after == NULL) /* after NULL is in front of the first */
4762 before = firstwin;
4763 else
4764 before = after->w_next;
4765
4766 wp->w_next = before;
4767 wp->w_prev = after;
4768 if (after == NULL)
4769 firstwin = wp;
4770 else
4771 after->w_next = wp;
4772 if (before == NULL)
4773 lastwin = wp;
4774 else
4775 before->w_prev = wp;
4776}
4777
4778/*
4779 * Remove a window from the window list.
4780 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004781 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004782win_remove(
4783 win_T *wp,
4784 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785{
4786 if (wp->w_prev != NULL)
4787 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004788 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004789 firstwin = curtab->tp_firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004790 else
4791 tp->tp_firstwin = wp->w_next;
Bram Moolenaar816968d2017-09-29 21:29:18 +02004792
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 if (wp->w_next != NULL)
4794 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004795 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004796 lastwin = curtab->tp_lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004797 else
4798 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799}
4800
4801/*
4802 * Append frame "frp" in a frame list after frame "after".
4803 */
4804 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004805frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806{
4807 frp->fr_next = after->fr_next;
4808 after->fr_next = frp;
4809 if (frp->fr_next != NULL)
4810 frp->fr_next->fr_prev = frp;
4811 frp->fr_prev = after;
4812}
4813
4814/*
4815 * Insert frame "frp" in a frame list before frame "before".
4816 */
4817 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004818frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819{
4820 frp->fr_next = before;
4821 frp->fr_prev = before->fr_prev;
4822 before->fr_prev = frp;
4823 if (frp->fr_prev != NULL)
4824 frp->fr_prev->fr_next = frp;
4825 else
4826 frp->fr_parent->fr_child = frp;
4827}
4828
4829/*
4830 * Remove a frame from a frame list.
4831 */
4832 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004833frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834{
4835 if (frp->fr_prev != NULL)
4836 frp->fr_prev->fr_next = frp->fr_next;
4837 else
Bram Moolenaar6f361c92018-01-31 19:06:50 +01004838 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 frp->fr_parent->fr_child = frp->fr_next;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01004840 /* special case: topframe->fr_child == frp */
4841 if (topframe->fr_child == frp)
4842 topframe->fr_child = frp->fr_next;
4843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 if (frp->fr_next != NULL)
4845 frp->fr_next->fr_prev = frp->fr_prev;
4846}
4847
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848/*
4849 * Allocate w_lines[] for window "wp".
4850 * Return FAIL for failure, OK for success.
4851 */
4852 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004853win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854{
4855 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004856 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 if (wp->w_lines == NULL)
4858 return FAIL;
4859 return OK;
4860}
4861
4862/*
4863 * free lsize arrays for a window
4864 */
4865 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004866win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004868 /* TODO: why would wp be NULL here? */
4869 if (wp != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004870 VIM_CLEAR(wp->w_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871}
4872
4873/*
4874 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004875 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 */
4877 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004878shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004880 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881
4882 if (firstwin == NULL) /* not initialized yet */
4883 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 if (h < frame_minheight(topframe, NULL))
4885 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004886
4887 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 * that doesn't result in the right height, forget about that option. */
4889 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004890 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 frame_new_height(topframe, h, FALSE, FALSE);
4892
4893 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 compute_cmdrow();
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004895 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004896
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897#if 0
4898 /* Disabled: don't want making the screen smaller make a window larger. */
4899 if (p_ea)
4900 win_equal(curwin, FALSE, 'v');
4901#endif
4902}
4903
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904/*
4905 * Called from win_new_shellsize() after Columns changed.
4906 */
4907 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004908shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909{
4910 if (firstwin == NULL) /* not initialized yet */
4911 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004912
4913 /* First try setting the widths of windows with 'winfixwidth'. If that
4914 * doesn't result in the right width, forget about that option. */
4915 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004916 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004917 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4918
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4920#if 0
4921 /* Disabled: don't want making the screen smaller make a window larger. */
4922 if (p_ea)
4923 win_equal(curwin, FALSE, 'h');
4924#endif
4925}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
4927#if defined(FEAT_CMDWIN) || defined(PROTO)
4928/*
4929 * Save the size of all windows in "gap".
4930 */
4931 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004932win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933
4934{
4935 win_T *wp;
4936
4937 ga_init2(gap, (int)sizeof(int), 1);
4938 if (ga_grow(gap, win_count() * 2) == OK)
Bram Moolenaar29323592016-07-24 22:04:11 +02004939 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
4941 ((int *)gap->ga_data)[gap->ga_len++] =
4942 wp->w_width + wp->w_vsep_width;
4943 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4944 }
4945}
4946
4947/*
4948 * Restore window sizes, but only if the number of windows is still the same.
4949 * Does not free the growarray.
4950 */
4951 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004952win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953{
4954 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004955 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956
4957 if (win_count() * 2 == gap->ga_len)
4958 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004959 /* The order matters, because frames contain other frames, but it's
4960 * difficult to get right. The easy way out is to do it twice. */
4961 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004963 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004964 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb643e772014-07-16 15:18:26 +02004965 {
4966 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4967 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 }
4970 /* recompute the window positions */
4971 (void)win_comp_pos();
4972 }
4973}
4974#endif /* FEAT_CMDWIN */
4975
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976/*
4977 * Update the position for all windows, using the width and height of the
4978 * frames.
4979 * Returns the row just after the last window.
4980 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004981 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004982win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004984 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 int col = 0;
4986
4987 frame_comp_pos(topframe, &row, &col);
4988 return row;
4989}
4990
4991/*
4992 * Update the position of the windows in frame "topfrp", using the width and
4993 * height of the frames.
4994 * "*row" and "*col" are the top-left position of the frame. They are updated
4995 * to the bottom-right position plus one.
4996 */
4997 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004998frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999{
5000 win_T *wp;
5001 frame_T *frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 int startcol;
5003 int startrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005004 int h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
5006 wp = topfrp->fr_win;
5007 if (wp != NULL)
5008 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005009 if (wp->w_winrow != *row || wp->w_wincol != *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
5011 /* position changed, redraw */
5012 wp->w_winrow = *row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 wp->w_wincol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 redraw_win_later(wp, NOT_VALID);
5015 wp->w_redr_status = TRUE;
5016 }
Bram Moolenaar415a6932017-12-05 20:31:07 +01005017 /* WinBar will not show if the window height is zero */
5018 h = VISIBLE_HEIGHT(wp) + wp->w_status_height;
5019 *row += h > topfrp->fr_height ? topfrp->fr_height : h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 *col += wp->w_width + wp->w_vsep_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022 else
5023 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 startrow = *row;
5025 startcol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
5027 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 if (topfrp->fr_layout == FR_ROW)
5029 *row = startrow; /* all frames are at the same row */
5030 else
5031 *col = startcol; /* all frames are at the same col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 frame_comp_pos(frp, row, col);
5033 }
5034 }
5035}
5036
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037/*
5038 * Set current window height and take care of repositioning other windows to
5039 * fit around it.
5040 */
5041 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005042win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043{
5044 win_setheight_win(height, curwin);
5045}
5046
5047/*
5048 * Set the window height of window "win" and take care of repositioning other
5049 * windows to fit around it.
5050 */
5051 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005052win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053{
5054 int row;
5055
5056 if (win == curwin)
5057 {
5058 /* Always keep current window at least one line high, even when
5059 * 'winminheight' is zero. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 if (height < p_wmh)
5061 height = p_wmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 if (height == 0)
5063 height = 1;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005064 height += WINBAR_HEIGHT(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 }
5066
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 frame_setheight(win->w_frame, height + win->w_status_height);
5068
5069 /* recompute the window positions */
5070 row = win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071
5072 /*
5073 * If there is extra space created between the last window and the command
5074 * line, clear it.
5075 */
5076 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5077 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5078 cmdline_row = row;
5079 msg_row = row;
5080 msg_col = 0;
5081
5082 redraw_all_later(NOT_VALID);
5083}
5084
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085/*
5086 * Set the height of a frame to "height" and take care that all frames and
5087 * windows inside it are resized. Also resize frames on the left and right if
5088 * the are in the same FR_ROW frame.
5089 *
5090 * Strategy:
5091 * If the frame is part of a FR_COL frame, try fitting the frame in that
5092 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5093 * go to containing frames to resize them and make room.
5094 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5095 * Check for the minimal height of the FR_ROW frame.
5096 * At the top level we can also use change the command line height.
5097 */
5098 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005099frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100{
5101 int room; /* total number of lines available */
5102 int take; /* number of lines taken from other windows */
5103 int room_cmdline; /* lines available from cmdline */
5104 int run;
5105 frame_T *frp;
5106 int h;
5107 int room_reserved;
5108
5109 /* If the height already is the desired value, nothing to do. */
5110 if (curfrp->fr_height == height)
5111 return;
5112
5113 if (curfrp->fr_parent == NULL)
5114 {
5115 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005116 if (height > ROWS_AVAIL)
5117 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 if (height > 0)
5119 frame_new_height(curfrp, height, FALSE, FALSE);
5120 }
5121 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5122 {
5123 /* Row of frames: Also need to resize frames left and right of this
5124 * one. First check for the minimal height of these. */
5125 h = frame_minheight(curfrp->fr_parent, NULL);
5126 if (height < h)
5127 height = h;
5128 frame_setheight(curfrp->fr_parent, height);
5129 }
5130 else
5131 {
5132 /*
5133 * Column of frames: try to change only frames in this column.
5134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 /*
5136 * Do this twice:
5137 * 1: compute room available, if it's not enough try resizing the
5138 * containing frame.
5139 * 2: compute the room available and adjust the height to it.
5140 * Try not to reduce the height of a window with 'winfixheight' set.
5141 */
5142 for (run = 1; run <= 2; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 {
5144 room = 0;
5145 room_reserved = 0;
5146 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5147 frp = frp->fr_next)
5148 {
5149 if (frp != curfrp
5150 && frp->fr_win != NULL
5151 && frp->fr_win->w_p_wfh)
5152 room_reserved += frp->fr_height;
5153 room += frp->fr_height;
5154 if (frp != curfrp)
5155 room -= frame_minheight(frp, NULL);
5156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (curfrp->fr_width != Columns)
5158 room_cmdline = 0;
5159 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
5161 room_cmdline = Rows - p_ch - (lastwin->w_winrow
Bram Moolenaar415a6932017-12-05 20:31:07 +01005162 + VISIBLE_HEIGHT(lastwin)
5163 + lastwin->w_status_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 if (room_cmdline < 0)
5165 room_cmdline = 0;
5166 }
5167
5168 if (height <= room + room_cmdline)
5169 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 if (run == 2 || curfrp->fr_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 {
5172 if (height > room + room_cmdline)
5173 height = room + room_cmdline;
5174 break;
5175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 frame_setheight(curfrp->fr_parent, height
5177 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
5179
5180 /*
5181 * Compute the number of lines we will take from others frames (can be
5182 * negative!).
5183 */
5184 take = height - curfrp->fr_height;
5185
5186 /* If there is not enough room, also reduce the height of a window
5187 * with 'winfixheight' set. */
5188 if (height > room + room_cmdline - room_reserved)
5189 room_reserved = room + room_cmdline - height;
5190 /* If there is only a 'winfixheight' window and making the
5191 * window smaller, need to make the other window taller. */
5192 if (take < 0 && room - curfrp->fr_height < room_reserved)
5193 room_reserved = 0;
5194
5195 if (take > 0 && room_cmdline > 0)
5196 {
5197 /* use lines from cmdline first */
5198 if (take < room_cmdline)
5199 room_cmdline = take;
5200 take -= room_cmdline;
5201 topframe->fr_height += room_cmdline;
5202 }
5203
5204 /*
5205 * set the current frame to the new height
5206 */
5207 frame_new_height(curfrp, height, FALSE, FALSE);
5208
5209 /*
5210 * First take lines from the frames after the current frame. If
5211 * that is not enough, takes lines from frames above the current
5212 * frame.
5213 */
5214 for (run = 0; run < 2; ++run)
5215 {
5216 if (run == 0)
5217 frp = curfrp->fr_next; /* 1st run: start with next window */
5218 else
5219 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5220 while (frp != NULL && take != 0)
5221 {
5222 h = frame_minheight(frp, NULL);
5223 if (room_reserved > 0
5224 && frp->fr_win != NULL
5225 && frp->fr_win->w_p_wfh)
5226 {
5227 if (room_reserved >= frp->fr_height)
5228 room_reserved -= frp->fr_height;
5229 else
5230 {
5231 if (frp->fr_height - room_reserved > take)
5232 room_reserved = frp->fr_height - take;
5233 take -= frp->fr_height - room_reserved;
5234 frame_new_height(frp, room_reserved, FALSE, FALSE);
5235 room_reserved = 0;
5236 }
5237 }
5238 else
5239 {
5240 if (frp->fr_height - take < h)
5241 {
5242 take -= frp->fr_height - h;
5243 frame_new_height(frp, h, FALSE, FALSE);
5244 }
5245 else
5246 {
5247 frame_new_height(frp, frp->fr_height - take,
5248 FALSE, FALSE);
5249 take = 0;
5250 }
5251 }
5252 if (run == 0)
5253 frp = frp->fr_next;
5254 else
5255 frp = frp->fr_prev;
5256 }
5257 }
5258 }
5259}
5260
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261/*
5262 * Set current window width and take care of repositioning other windows to
5263 * fit around it.
5264 */
5265 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005266win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267{
5268 win_setwidth_win(width, curwin);
5269}
5270
5271 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005272win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273{
5274 /* Always keep current window at least one column wide, even when
5275 * 'winminwidth' is zero. */
5276 if (wp == curwin)
5277 {
5278 if (width < p_wmw)
5279 width = p_wmw;
5280 if (width == 0)
5281 width = 1;
5282 }
5283
5284 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5285
5286 /* recompute the window positions */
5287 (void)win_comp_pos();
5288
5289 redraw_all_later(NOT_VALID);
5290}
5291
5292/*
5293 * Set the width of a frame to "width" and take care that all frames and
5294 * windows inside it are resized. Also resize frames above and below if the
5295 * are in the same FR_ROW frame.
5296 *
5297 * Strategy is similar to frame_setheight().
5298 */
5299 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005300frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301{
5302 int room; /* total number of lines available */
5303 int take; /* number of lines taken from other windows */
5304 int run;
5305 frame_T *frp;
5306 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005307 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308
5309 /* If the width already is the desired value, nothing to do. */
5310 if (curfrp->fr_width == width)
5311 return;
5312
5313 if (curfrp->fr_parent == NULL)
5314 /* topframe: can't change width */
5315 return;
5316
5317 if (curfrp->fr_parent->fr_layout == FR_COL)
5318 {
5319 /* Column of frames: Also need to resize frames above and below of
5320 * this one. First check for the minimal width of these. */
5321 w = frame_minwidth(curfrp->fr_parent, NULL);
5322 if (width < w)
5323 width = w;
5324 frame_setwidth(curfrp->fr_parent, width);
5325 }
5326 else
5327 {
5328 /*
5329 * Row of frames: try to change only frames in this row.
5330 *
5331 * Do this twice:
5332 * 1: compute room available, if it's not enough try resizing the
5333 * containing frame.
5334 * 2: compute the room available and adjust the width to it.
5335 */
5336 for (run = 1; run <= 2; ++run)
5337 {
5338 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005339 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5341 frp = frp->fr_next)
5342 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005343 if (frp != curfrp
5344 && frp->fr_win != NULL
5345 && frp->fr_win->w_p_wfw)
5346 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 room += frp->fr_width;
5348 if (frp != curfrp)
5349 room -= frame_minwidth(frp, NULL);
5350 }
5351
5352 if (width <= room)
5353 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005354 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 {
5356 if (width > room)
5357 width = room;
5358 break;
5359 }
5360 frame_setwidth(curfrp->fr_parent, width
5361 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5362 }
5363
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 /*
5365 * Compute the number of lines we will take from others frames (can be
5366 * negative!).
5367 */
5368 take = width - curfrp->fr_width;
5369
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005370 /* If there is not enough room, also reduce the width of a window
5371 * with 'winfixwidth' set. */
5372 if (width > room - room_reserved)
5373 room_reserved = room - width;
5374 /* If there is only a 'winfixwidth' window and making the
5375 * window smaller, need to make the other window narrower. */
5376 if (take < 0 && room - curfrp->fr_width < room_reserved)
5377 room_reserved = 0;
5378
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 /*
5380 * set the current frame to the new width
5381 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005382 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383
5384 /*
5385 * First take lines from the frames right of the current frame. If
5386 * that is not enough, takes lines from frames left of the current
5387 * frame.
5388 */
5389 for (run = 0; run < 2; ++run)
5390 {
5391 if (run == 0)
5392 frp = curfrp->fr_next; /* 1st run: start with next window */
5393 else
5394 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5395 while (frp != NULL && take != 0)
5396 {
5397 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005398 if (room_reserved > 0
5399 && frp->fr_win != NULL
5400 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005402 if (room_reserved >= frp->fr_width)
5403 room_reserved -= frp->fr_width;
5404 else
5405 {
5406 if (frp->fr_width - room_reserved > take)
5407 room_reserved = frp->fr_width - take;
5408 take -= frp->fr_width - room_reserved;
5409 frame_new_width(frp, room_reserved, FALSE, FALSE);
5410 room_reserved = 0;
5411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 }
5413 else
5414 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005415 if (frp->fr_width - take < w)
5416 {
5417 take -= frp->fr_width - w;
5418 frame_new_width(frp, w, FALSE, FALSE);
5419 }
5420 else
5421 {
5422 frame_new_width(frp, frp->fr_width - take,
5423 FALSE, FALSE);
5424 take = 0;
5425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 }
5427 if (run == 0)
5428 frp = frp->fr_next;
5429 else
5430 frp = frp->fr_prev;
5431 }
5432 }
5433 }
5434}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435
5436/*
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005437 * Check 'winminheight' for a valid value and reduce it if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 */
5439 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005440win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
5442 int room;
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005443 int needed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005446 // loop until there is a 'winminheight' that is possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 while (p_wmh > 0)
5448 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005449 room = Rows - p_ch;
5450 needed = frame_minheight(topframe, NULL);
5451 if (room >= needed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 break;
5453 --p_wmh;
5454 if (first)
5455 {
5456 EMSG(_(e_noroom));
5457 first = FALSE;
5458 }
5459 }
5460}
5461
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005462/*
5463 * Check 'winminwidth' for a valid value and reduce it if needed.
5464 */
5465 void
5466win_setminwidth(void)
5467{
5468 int room;
5469 int needed;
5470 int first = TRUE;
5471
5472 // loop until there is a 'winminheight' that is possible
5473 while (p_wmw > 0)
5474 {
5475 room = Columns;
5476 needed = frame_minwidth(topframe, NULL);
5477 if (room >= needed)
5478 break;
5479 --p_wmw;
5480 if (first)
5481 {
5482 EMSG(_(e_noroom));
5483 first = FALSE;
5484 }
5485 }
5486}
5487
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005488#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489
5490/*
5491 * Status line of dragwin is dragged "offset" lines down (negative is up).
5492 */
5493 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005494win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495{
5496 frame_T *curfr;
5497 frame_T *fr;
5498 int room;
5499 int row;
5500 int up; /* if TRUE, drag status line up, otherwise down */
5501 int n;
5502
5503 fr = dragwin->w_frame;
5504 curfr = fr;
5505 if (fr != topframe) /* more than one window */
5506 {
5507 fr = fr->fr_parent;
5508 /* When the parent frame is not a column of frames, its parent should
5509 * be. */
5510 if (fr->fr_layout != FR_COL)
5511 {
5512 curfr = fr;
5513 if (fr != topframe) /* only a row of windows, may drag statusline */
5514 fr = fr->fr_parent;
5515 }
5516 }
5517
5518 /* If this is the last frame in a column, may want to resize the parent
5519 * frame instead (go two up to skip a row of frames). */
5520 while (curfr != topframe && curfr->fr_next == NULL)
5521 {
5522 if (fr != topframe)
5523 fr = fr->fr_parent;
5524 curfr = fr;
5525 if (fr != topframe)
5526 fr = fr->fr_parent;
5527 }
5528
5529 if (offset < 0) /* drag up */
5530 {
5531 up = TRUE;
5532 offset = -offset;
5533 /* sum up the room of the current frame and above it */
5534 if (fr == curfr)
5535 {
5536 /* only one window */
5537 room = fr->fr_height - frame_minheight(fr, NULL);
5538 }
5539 else
5540 {
5541 room = 0;
5542 for (fr = fr->fr_child; ; fr = fr->fr_next)
5543 {
5544 room += fr->fr_height - frame_minheight(fr, NULL);
5545 if (fr == curfr)
5546 break;
5547 }
5548 }
5549 fr = curfr->fr_next; /* put fr at frame that grows */
5550 }
5551 else /* drag down */
5552 {
5553 up = FALSE;
5554 /*
5555 * Only dragging the last status line can reduce p_ch.
5556 */
5557 room = Rows - cmdline_row;
5558 if (curfr->fr_next == NULL)
5559 room -= 1;
5560 else
5561 room -= p_ch;
5562 if (room < 0)
5563 room = 0;
5564 /* sum up the room of frames below of the current one */
5565 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5566 room += fr->fr_height - frame_minheight(fr, NULL);
5567 fr = curfr; /* put fr at window that grows */
5568 }
5569
5570 if (room < offset) /* Not enough room */
5571 offset = room; /* Move as far as we can */
5572 if (offset <= 0)
5573 return;
5574
5575 /*
5576 * Grow frame fr by "offset" lines.
5577 * Doesn't happen when dragging the last status line up.
5578 */
5579 if (fr != NULL)
5580 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5581
5582 if (up)
5583 fr = curfr; /* current frame gets smaller */
5584 else
5585 fr = curfr->fr_next; /* next frame gets smaller */
5586
5587 /*
5588 * Now make the other frames smaller.
5589 */
5590 while (fr != NULL && offset > 0)
5591 {
5592 n = frame_minheight(fr, NULL);
5593 if (fr->fr_height - offset <= n)
5594 {
5595 offset -= fr->fr_height - n;
5596 frame_new_height(fr, n, !up, FALSE);
5597 }
5598 else
5599 {
5600 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5601 break;
5602 }
5603 if (up)
5604 fr = fr->fr_prev;
5605 else
5606 fr = fr->fr_next;
5607 }
5608 row = win_comp_pos();
5609 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5610 cmdline_row = row;
5611 p_ch = Rows - cmdline_row;
5612 if (p_ch < 1)
5613 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005614 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005615 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 showmode();
5617}
5618
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619/*
5620 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5621 */
5622 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005623win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
5625 frame_T *curfr;
5626 frame_T *fr;
5627 int room;
5628 int left; /* if TRUE, drag separator line left, otherwise right */
5629 int n;
5630
5631 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005632 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 return;
5634 curfr = fr;
5635 fr = fr->fr_parent;
5636 /* When the parent frame is not a row of frames, its parent should be. */
5637 if (fr->fr_layout != FR_ROW)
5638 {
5639 if (fr == topframe) /* only a column of windows (cannot happen?) */
5640 return;
5641 curfr = fr;
5642 fr = fr->fr_parent;
5643 }
5644
5645 /* If this is the last frame in a row, may want to resize a parent
5646 * frame instead. */
5647 while (curfr->fr_next == NULL)
5648 {
5649 if (fr == topframe)
5650 break;
5651 curfr = fr;
5652 fr = fr->fr_parent;
5653 if (fr != topframe)
5654 {
5655 curfr = fr;
5656 fr = fr->fr_parent;
5657 }
5658 }
5659
5660 if (offset < 0) /* drag left */
5661 {
5662 left = TRUE;
5663 offset = -offset;
5664 /* sum up the room of the current frame and left of it */
5665 room = 0;
5666 for (fr = fr->fr_child; ; fr = fr->fr_next)
5667 {
5668 room += fr->fr_width - frame_minwidth(fr, NULL);
5669 if (fr == curfr)
5670 break;
5671 }
5672 fr = curfr->fr_next; /* put fr at frame that grows */
5673 }
5674 else /* drag right */
5675 {
5676 left = FALSE;
5677 /* sum up the room of frames right of the current one */
5678 room = 0;
5679 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5680 room += fr->fr_width - frame_minwidth(fr, NULL);
5681 fr = curfr; /* put fr at window that grows */
5682 }
5683
5684 if (room < offset) /* Not enough room */
5685 offset = room; /* Move as far as we can */
5686 if (offset <= 0) /* No room at all, quit. */
5687 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005688 if (fr == NULL)
5689 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690
5691 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005692 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693
5694 /* shrink other frames: current and at the left or at the right */
5695 if (left)
5696 fr = curfr; /* current frame gets smaller */
5697 else
5698 fr = curfr->fr_next; /* next frame gets smaller */
5699
5700 while (fr != NULL && offset > 0)
5701 {
5702 n = frame_minwidth(fr, NULL);
5703 if (fr->fr_width - offset <= n)
5704 {
5705 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005706 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 }
5708 else
5709 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005710 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 break;
5712 }
5713 if (left)
5714 fr = fr->fr_prev;
5715 else
5716 fr = fr->fr_next;
5717 }
5718 (void)win_comp_pos();
5719 redraw_all_later(NOT_VALID);
5720}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721#endif /* FEAT_MOUSE */
5722
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005723#define FRACTION_MULT 16384L
5724
5725/*
5726 * Set wp->w_fraction for the current w_wrow and w_height.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005727 * Has no effect when the window is less than two lines.
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005728 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005729 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005730set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005731{
Bram Moolenaar3679c172017-11-22 22:22:11 +01005732 if (wp->w_height > 1)
5733 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005734 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005735}
5736
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737/*
5738 * Set the height of a window.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005739 * "height" excludes any window toolbar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 * This takes care of the things inside the window, not what happens to the
5741 * window position, the frame or to other windows.
5742 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005743 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005744win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745{
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005746 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
5748 /* Don't want a negative height. Happens when splitting a tiny window.
5749 * Will equalize heights soon to fix it. */
5750 if (height < 0)
5751 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005752 if (wp->w_height == height)
5753 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005755 if (wp->w_height > 0)
5756 {
5757 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005758 /* w_wrow needs to be valid. When setting 'laststatus' this may
5759 * call win_new_height() recursively. */
5760 validate_cursor();
5761 if (wp->w_height != prev_height)
5762 return; /* Recursive call already changed the size, bail out here
5763 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005764 if (wp->w_wrow != wp->w_prev_fraction_row)
5765 set_fraction(wp);
5766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
5768 wp->w_height = height;
5769 wp->w_skipcol = 0;
5770
Bram Moolenaar955f1982017-02-05 15:10:51 +01005771 /* There is no point in adjusting the scroll position when exiting. Some
5772 * values might be invalid. */
5773 if (!exiting)
5774 scroll_to_fraction(wp, prev_height);
Bram Moolenaar46328f92016-08-28 15:39:57 +02005775}
5776
5777 void
5778scroll_to_fraction(win_T *wp, int prev_height)
5779{
5780 linenr_T lnum;
5781 int sline, line_size;
5782 int height = wp->w_height;
5783
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 /* Don't change w_topline when height is zero. Don't set w_topline when
5785 * 'scrollbind' is set and this isn't the current window. */
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005786 if (height > 0 && (!wp->w_p_scb || wp == curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005788 /*
5789 * Find a value for w_topline that shows the cursor at the same
5790 * relative position in the window as before (more or less).
5791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 lnum = wp->w_cursor.lnum;
5793 if (lnum < 1) /* can happen when starting up */
5794 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005795 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5796 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5798 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005799
5800 if (sline >= 0)
5801 {
5802 /* Make sure the whole cursor line is visible, if possible. */
5803 int rows = plines_win(wp, lnum, FALSE);
5804
5805 if (sline > wp->w_height - rows)
5806 {
5807 sline = wp->w_height - rows;
5808 wp->w_wrow -= rows - line_size;
5809 }
5810 }
5811
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 if (sline < 0)
5813 {
5814 /*
5815 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005816 * Make cursor line the first line in the window. If not enough
5817 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 */
5819 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005820 if (wp->w_wrow >= wp->w_height
Bram Moolenaar02631462017-09-22 15:20:32 +02005821 && (wp->w_width - win_col_off(wp)) > 0)
Bram Moolenaar26470632006-10-24 19:12:40 +00005822 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005823 wp->w_skipcol += wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00005824 --wp->w_wrow;
5825 while (wp->w_wrow >= wp->w_height)
5826 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005827 wp->w_skipcol += wp->w_width - win_col_off(wp)
Bram Moolenaar26470632006-10-24 19:12:40 +00005828 + win_col_off2(wp);
5829 --wp->w_wrow;
5830 }
5831 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005832 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005834 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005836 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 {
5838#ifdef FEAT_FOLDING
5839 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5840 if (lnum == 1)
5841 {
5842 /* first line in buffer is folded */
5843 line_size = 1;
5844 --sline;
5845 break;
5846 }
5847#endif
5848 --lnum;
5849#ifdef FEAT_DIFF
5850 if (lnum == wp->w_topline)
5851 line_size = plines_win_nofill(wp, lnum, TRUE)
5852 + wp->w_topfill;
5853 else
5854#endif
5855 line_size = plines_win(wp, lnum, TRUE);
5856 sline -= line_size;
5857 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005858
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 if (sline < 0)
5860 {
5861 /*
5862 * Line we want at top would go off top of screen. Use next
5863 * line instead.
5864 */
5865#ifdef FEAT_FOLDING
5866 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5867#endif
5868 lnum++;
5869 wp->w_wrow -= line_size + sline;
5870 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005871 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 {
5873 /* First line of file reached, use that as topline. */
5874 lnum = 1;
5875 wp->w_wrow -= sline;
5876 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005877
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005878 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 }
5881
5882 if (wp == curwin)
5883 {
5884 if (p_so)
5885 update_topline();
5886 curs_columns(FALSE); /* validate w_wrow */
5887 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005888 if (prev_height > 0)
5889 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890
5891 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005892 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 invalidate_botline_win(wp);
5895}
5896
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897/*
5898 * Set the width of a window.
5899 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005900 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005901win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902{
5903 wp->w_width = width;
5904 wp->w_lines_valid = 0;
5905 changed_line_abv_curs_win(wp);
5906 invalidate_botline_win(wp);
5907 if (wp == curwin)
5908 {
5909 update_topline();
5910 curs_columns(TRUE); /* validate w_wrow */
5911 }
5912 redraw_win_later(wp, NOT_VALID);
5913 wp->w_redr_status = TRUE;
5914}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915
5916 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005917win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918{
5919 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5920 if (wp->w_p_scr == 0)
5921 wp->w_p_scr = 1;
5922}
5923
5924/*
5925 * command_height: called whenever p_ch has been changed
5926 */
5927 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005928command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 int h;
5931 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005932 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005934 /* Use the value of p_ch that we remembered. This is needed for when the
5935 * GUI starts up, we can't be sure in what order things happen. And when
5936 * p_ch was changed in another tab page. */
5937 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005938
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 /* Find bottom frame with width of screen. */
5940 frp = lastwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5942 frp = frp->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943
5944 /* Avoid changing the height of a window with 'winfixheight' set. */
5945 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5946 && frp->fr_win->w_p_wfh)
5947 frp = frp->fr_prev;
5948
5949 if (starting != NO_SCREEN)
5950 {
5951 cmdline_row = Rows - p_ch;
5952
5953 if (p_ch > old_p_ch) /* p_ch got bigger */
5954 {
5955 while (p_ch > old_p_ch)
5956 {
5957 if (frp == NULL)
5958 {
5959 EMSG(_(e_noroom));
5960 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005961 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 cmdline_row = Rows - p_ch;
5963 break;
5964 }
5965 h = frp->fr_height - frame_minheight(frp, NULL);
5966 if (h > p_ch - old_p_ch)
5967 h = p_ch - old_p_ch;
5968 old_p_ch += h;
5969 frame_add_height(frp, -h);
5970 frp = frp->fr_prev;
5971 }
5972
5973 /* Recompute window positions. */
5974 (void)win_comp_pos();
5975
5976 /* clear the lines added to cmdline */
5977 if (full_screen)
5978 screen_fill((int)(cmdline_row), (int)Rows, 0,
5979 (int)Columns, ' ', ' ', 0);
5980 msg_row = cmdline_row;
5981 redraw_cmdline = TRUE;
5982 return;
5983 }
5984
5985 if (msg_row < cmdline_row)
5986 msg_row = cmdline_row;
5987 redraw_cmdline = TRUE;
5988 }
5989 frame_add_height(frp, (int)(old_p_ch - p_ch));
5990
5991 /* Recompute window positions. */
5992 if (frp != lastwin->w_frame)
5993 (void)win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994}
5995
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996/*
5997 * Resize frame "frp" to be "n" lines higher (negative for less high).
5998 * Also resize the frames it is contained in.
5999 */
6000 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006001frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002{
6003 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
6004 for (;;)
6005 {
6006 frp = frp->fr_parent;
6007 if (frp == NULL)
6008 break;
6009 frp->fr_height += n;
6010 }
6011}
6012
6013/*
6014 * Add or remove a status line for the bottom window(s), according to the
6015 * value of 'laststatus'.
6016 */
6017 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006018last_status(
6019 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020{
6021 /* Don't make a difference between horizontal or vertical split. */
6022 last_status_rec(topframe, (p_ls == 2
Bram Moolenaar459ca562016-11-10 18:16:33 +01006023 || (p_ls == 1 && (morewin || !ONE_WINDOW))));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024}
6025
6026 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006027last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028{
6029 frame_T *fp;
6030 win_T *wp;
6031
6032 if (fr->fr_layout == FR_LEAF)
6033 {
6034 wp = fr->fr_win;
6035 if (wp->w_status_height != 0 && !statusline)
6036 {
6037 /* remove status line */
6038 win_new_height(wp, wp->w_height + 1);
6039 wp->w_status_height = 0;
6040 comp_col();
6041 }
6042 else if (wp->w_status_height == 0 && statusline)
6043 {
6044 /* Find a frame to take a line from. */
6045 fp = fr;
6046 while (fp->fr_height <= frame_minheight(fp, NULL))
6047 {
6048 if (fp == topframe)
6049 {
6050 EMSG(_(e_noroom));
6051 return;
6052 }
6053 /* In a column of frames: go to frame above. If already at
6054 * the top or in a row of frames: go to parent. */
6055 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6056 fp = fp->fr_prev;
6057 else
6058 fp = fp->fr_parent;
6059 }
6060 wp->w_status_height = 1;
6061 if (fp != fr)
6062 {
6063 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6064 frame_fix_height(wp);
6065 (void)win_comp_pos();
6066 }
6067 else
6068 win_new_height(wp, wp->w_height - 1);
6069 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006070 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 }
6072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 else if (fr->fr_layout == FR_ROW)
6074 {
6075 /* vertically split windows, set status line for each one */
6076 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6077 last_status_rec(fp, statusline);
6078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 else
6080 {
6081 /* horizontally split window, set status line for last one */
6082 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6083 ;
6084 last_status_rec(fp, statusline);
6085 }
6086}
6087
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006088/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006089 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006090 */
6091 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006092tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006093{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006094#ifdef FEAT_GUI_TABLINE
6095 /* When the GUI has the tabline then this always returns zero. */
6096 if (gui_use_tabline())
6097 return 0;
6098#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006099 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006100 {
6101 case 0: return 0;
6102 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6103 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006104 return 1;
6105}
6106
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6108/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006109 * Get the file name at the cursor.
6110 * If Visual mode is active, use the selected text if it's in one line.
6111 * Returns the name in allocated memory, NULL for failure.
6112 */
6113 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006114grab_file_name(long count, linenr_T *file_lnum)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006115{
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006116 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
6117
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006118 if (VIsual_active)
6119 {
6120 int len;
6121 char_u *ptr;
6122
6123 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6124 return NULL;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006125 return find_file_name_in_path(ptr, len, options,
6126 count, curbuf->b_ffname);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006127 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006128 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006129}
6130
6131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 * Return the file name under or after the cursor.
6133 *
6134 * The 'path' option is searched if the file name is not absolute.
6135 * The string returned has been alloc'ed and should be freed by the caller.
6136 * NULL is returned if the file name or file is not found.
6137 *
6138 * options:
6139 * FNAME_MESS give error messages
6140 * FNAME_EXP expand to path
6141 * FNAME_HYP check for hypertext link
6142 * FNAME_INCL apply "includeexpr"
6143 */
6144 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006145file_name_at_cursor(int options, long count, linenr_T *file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146{
6147 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006148 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6149 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150}
6151
6152/*
6153 * Return the name of the file under or after ptr[col].
6154 * Otherwise like file_name_at_cursor().
6155 */
6156 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006157file_name_in_line(
6158 char_u *line,
6159 int col,
6160 int options,
6161 long count,
6162 char_u *rel_fname, /* file we are searching relative to */
6163 linenr_T *file_lnum) /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164{
6165 char_u *ptr;
6166 int len;
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +01006167 int in_type = TRUE;
6168 int is_url = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169
6170 /*
6171 * search forward for what could be the start of a file name
6172 */
6173 ptr = line + col;
6174 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006175 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 if (*ptr == NUL) /* nothing found */
6177 {
6178 if (options & FNAME_MESS)
6179 EMSG(_("E446: No file name under cursor"));
6180 return NULL;
6181 }
6182
6183 /*
6184 * Search backward for first char of the file name.
6185 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6186 */
6187 while (ptr > line)
6188 {
6189#ifdef FEAT_MBYTE
6190 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6191 ptr -= len + 1;
6192 else
6193#endif
6194 if (vim_isfilec(ptr[-1])
6195 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6196 --ptr;
6197 else
6198 break;
6199 }
6200
6201 /*
6202 * Search forward for the last char of the file name.
6203 * Also allow "://" when ':' is not in 'isfname'.
6204 */
6205 len = 0;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006206 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +01006207 || ((options & FNAME_HYP) && path_is_url(ptr + len))
6208 || (is_url && vim_strchr((char_u *)"?&=", ptr[len]) != NULL))
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006209 {
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +01006210 /* After type:// we also include ?, & and = as valid characters, so that
6211 * http://google.com?q=this&that=ok works. */
6212 if ((ptr[len] >= 'A' && ptr[len] <= 'Z') || (ptr[len] >= 'a' && ptr[len] <= 'z'))
6213 {
6214 if (in_type && path_is_url(ptr + len + 1))
6215 is_url = TRUE;
6216 }
6217 else
6218 in_type = FALSE;
6219
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006220 if (ptr[len] == '\\')
6221 /* Skip over the "\" in "\ ". */
6222 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223#ifdef FEAT_MBYTE
6224 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006225 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 else
6227#endif
6228 ++len;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230
6231 /*
6232 * If there is trailing punctuation, remove it.
6233 * But don't remove "..", could be a directory name.
6234 */
6235 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6236 && ptr[len - 2] != '.')
6237 --len;
6238
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006239 if (file_lnum != NULL)
6240 {
6241 char_u *p;
6242
6243 /* Get the number after the file name and a separator character */
6244 p = ptr + len;
6245 p = skipwhite(p);
6246 if (*p != NUL)
6247 {
6248 if (!isdigit(*p))
6249 ++p; /* skip the separator */
6250 p = skipwhite(p);
6251 if (isdigit(*p))
6252 *file_lnum = (int)getdigits(&p);
6253 }
6254 }
6255
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6257}
6258
6259# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 static char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006261eval_includeexpr(char_u *ptr, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 char_u *res;
6264
6265 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006266 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006267 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 set_vim_var_string(VV_FNAME, NULL, 0);
6269 return res;
6270}
6271#endif
6272
6273/*
6274 * Return the name of the file ptr[len] in 'path'.
6275 * Otherwise like file_name_at_cursor().
6276 */
6277 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006278find_file_name_in_path(
6279 char_u *ptr,
6280 int len,
6281 int options,
6282 long count,
6283 char_u *rel_fname) /* file we are searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284{
6285 char_u *file_name;
6286 int c;
6287# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6288 char_u *tofree = NULL;
6289
6290 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6291 {
6292 tofree = eval_includeexpr(ptr, len);
6293 if (tofree != NULL)
6294 {
6295 ptr = tofree;
6296 len = (int)STRLEN(ptr);
6297 }
6298 }
6299# endif
6300
6301 if (options & FNAME_EXP)
6302 {
6303 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6304 TRUE, rel_fname);
6305
6306# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6307 /*
6308 * If the file could not be found in a normal way, try applying
6309 * 'includeexpr' (unless done already).
6310 */
6311 if (file_name == NULL
6312 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6313 {
6314 tofree = eval_includeexpr(ptr, len);
6315 if (tofree != NULL)
6316 {
6317 ptr = tofree;
6318 len = (int)STRLEN(ptr);
6319 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6320 TRUE, rel_fname);
6321 }
6322 }
6323# endif
6324 if (file_name == NULL && (options & FNAME_MESS))
6325 {
6326 c = ptr[len];
6327 ptr[len] = NUL;
6328 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6329 ptr[len] = c;
6330 }
6331
6332 /* Repeat finding the file "count" times. This matters when it
6333 * appears several times in the path. */
6334 while (file_name != NULL && --count > 0)
6335 {
6336 vim_free(file_name);
6337 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6338 }
6339 }
6340 else
6341 file_name = vim_strnsave(ptr, len);
6342
6343# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6344 vim_free(tofree);
6345# endif
6346
6347 return file_name;
6348}
6349#endif /* FEAT_SEARCHPATH */
6350
6351/*
6352 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6353 * Also check for ":\\", which MS Internet Explorer accepts, return
6354 * URL_BACKSLASH.
6355 */
6356 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006357path_is_url(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
6359 if (STRNCMP(p, "://", (size_t)3) == 0)
6360 return URL_SLASH;
6361 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6362 return URL_BACKSLASH;
6363 return 0;
6364}
6365
6366/*
6367 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6368 * Return URL_BACKSLASH for "name:\\".
6369 * Return zero otherwise.
6370 */
6371 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006372path_with_url(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373{
6374 char_u *p;
6375
6376 for (p = fname; isalpha(*p); ++p)
6377 ;
6378 return path_is_url(p);
6379}
6380
6381/*
6382 * Return TRUE if "name" is a full (absolute) path name or URL.
6383 */
6384 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006385vim_isAbsName(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 return (path_with_url(name) != 0 || mch_isFullName(name));
6388}
6389
6390/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006391 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 *
6393 * return FAIL for failure, OK otherwise
6394 */
6395 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006396vim_FullName(
6397 char_u *fname,
6398 char_u *buf,
6399 int len,
6400 int force) /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401{
6402 int retval = OK;
6403 int url;
6404
6405 *buf = NUL;
6406 if (fname == NULL)
6407 return FAIL;
6408
6409 url = path_with_url(fname);
6410 if (!url)
6411 retval = mch_FullName(fname, buf, len, force);
6412 if (url || retval == FAIL)
6413 {
6414 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006415 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 }
Bram Moolenaard0573012017-10-28 21:11:06 +02006417#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 slash_adjust(buf);
6419#endif
6420 return retval;
6421}
6422
6423/*
6424 * Return the minimal number of rows that is needed on the screen to display
6425 * the current number of windows.
6426 */
6427 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006428min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429{
6430 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006431 tabpage_T *tp;
6432 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433
6434 if (firstwin == NULL) /* not initialized yet */
6435 return MIN_LINES;
6436
Bram Moolenaarf740b292006-02-16 22:11:02 +00006437 total = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02006438 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006439 {
6440 n = frame_minheight(tp->tp_topframe, NULL);
6441 if (total < n)
6442 total = n;
6443 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006444 total += tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006445 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 return total;
6447}
6448
6449/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006450 * Return TRUE if there is only one window (in the current tab page), not
6451 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006452 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 */
6454 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006455only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 int count = 0;
6458 win_T *wp;
6459
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006460 /* If there is another tab page there always is another window. */
6461 if (first_tabpage->tp_next != NULL)
6462 return FALSE;
6463
Bram Moolenaar29323592016-07-24 22:04:11 +02006464 FOR_ALL_WINDOWS(wp)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006465 if (wp->w_buffer != NULL
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006466 && (!((bt_help(wp->w_buffer) && !bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467# ifdef FEAT_QUICKFIX
6468 || wp->w_p_pvw
6469# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006470 ) || wp == curwin) && wp != aucmd_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 ++count;
6472 return (count <= 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473}
6474
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475/*
6476 * Correct the cursor line number in other windows. Used after changing the
6477 * current buffer, and before applying autocommands.
6478 * When "do_curwin" is TRUE, also check current window.
6479 */
6480 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006481check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482{
6483 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006484 tabpage_T *tp;
6485
6486 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 {
6489 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6490 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6491 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6492 wp->w_topline = curbuf->b_ml.ml_line_count;
6493 }
6494}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495
6496/*
6497 * A snapshot of the window sizes, to restore them after closing the help
6498 * window.
6499 * Only these fields are used:
6500 * fr_layout
6501 * fr_width
6502 * fr_height
6503 * fr_next
6504 * fr_child
6505 * fr_win (only valid for the old curwin, NULL otherwise)
6506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
6508/*
6509 * Create a snapshot of the current frame sizes.
6510 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006511 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006512make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006514 clear_snapshot(curtab, idx);
6515 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516}
6517
6518 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006519make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520{
6521 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6522 if (*frp == NULL)
6523 return;
6524 (*frp)->fr_layout = fr->fr_layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 (*frp)->fr_width = fr->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 (*frp)->fr_height = fr->fr_height;
6527 if (fr->fr_next != NULL)
6528 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6529 if (fr->fr_child != NULL)
6530 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6531 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6532 (*frp)->fr_win = curwin;
6533}
6534
6535/*
6536 * Remove any existing snapshot.
6537 */
6538 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006539clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006541 clear_snapshot_rec(tp->tp_snapshot[idx]);
6542 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543}
6544
6545 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006546clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547{
6548 if (fr != NULL)
6549 {
6550 clear_snapshot_rec(fr->fr_next);
6551 clear_snapshot_rec(fr->fr_child);
6552 vim_free(fr);
6553 }
6554}
6555
6556/*
6557 * Restore a previously created snapshot, if there is any.
6558 * This is only done if the screen size didn't change and the window layout is
6559 * still the same.
6560 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006561 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006562restore_snapshot(
6563 int idx,
6564 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565{
6566 win_T *wp;
6567
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006568 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006569 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006570 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6571 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006573 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 win_comp_pos();
6575 if (wp != NULL && close_curwin)
6576 win_goto(wp);
6577 redraw_all_later(CLEAR);
6578 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006579 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580}
6581
6582/*
6583 * Check if frames "sn" and "fr" have the same layout, same following frames
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006584 * and same children. And the window pointer is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 */
6586 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006587check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588{
6589 if (sn->fr_layout != fr->fr_layout
6590 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6591 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6592 || (sn->fr_next != NULL
6593 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6594 || (sn->fr_child != NULL
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006595 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01006596 || (sn->fr_win != NULL && !win_valid(sn->fr_win)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 return FAIL;
6598 return OK;
6599}
6600
6601/*
6602 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6603 * following frames and children.
6604 * Returns a pointer to the old current window, or NULL.
6605 */
6606 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006607restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608{
6609 win_T *wp = NULL;
6610 win_T *wp2;
6611
6612 fr->fr_height = sn->fr_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 fr->fr_width = sn->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 if (fr->fr_layout == FR_LEAF)
6615 {
6616 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006617 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 wp = sn->fr_win;
6619 }
6620 if (sn->fr_next != NULL)
6621 {
6622 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6623 if (wp2 != NULL)
6624 wp = wp2;
6625 }
6626 if (sn->fr_child != NULL)
6627 {
6628 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6629 if (wp2 != NULL)
6630 wp = wp2;
6631 }
6632 return wp;
6633}
6634
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006635#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6636 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006637/*
6638 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006639 * restore_win() MUST be called to undo, also when FAIL is returned.
6640 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006641 * When "no_display" is TRUE the display won't be affected, no redraw is
6642 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006643 * Returns FAIL if switching to "win" failed.
6644 */
6645 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006646switch_win(
Bram Moolenaar816968d2017-09-29 21:29:18 +02006647 win_T **save_curwin,
6648 tabpage_T **save_curtab,
6649 win_T *win,
6650 tabpage_T *tp,
6651 int no_display)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006652{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006653 block_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006654 *save_curwin = curwin;
6655 if (tp != NULL)
6656 {
6657 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006658 if (no_display)
6659 {
6660 curtab->tp_firstwin = firstwin;
6661 curtab->tp_lastwin = lastwin;
6662 curtab = tp;
6663 firstwin = curtab->tp_firstwin;
6664 lastwin = curtab->tp_lastwin;
6665 }
6666 else
6667 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006668 }
6669 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006670 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006671 curwin = win;
6672 curbuf = curwin->w_buffer;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006673 return OK;
6674}
6675
6676/*
6677 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006678 * When "no_display" is TRUE the display won't be affected, no redraw is
6679 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006680 */
6681 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006682restore_win(
6683 win_T *save_curwin UNUSED,
6684 tabpage_T *save_curtab UNUSED,
6685 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006686{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006687 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006688 {
6689 if (no_display)
6690 {
6691 curtab->tp_firstwin = firstwin;
6692 curtab->tp_lastwin = lastwin;
6693 curtab = save_curtab;
6694 firstwin = curtab->tp_firstwin;
6695 lastwin = curtab->tp_lastwin;
6696 }
6697 else
6698 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6699 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006700 if (win_valid(save_curwin))
6701 {
6702 curwin = save_curwin;
6703 curbuf = curwin->w_buffer;
6704 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006705 unblock_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006706}
6707
6708/*
6709 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6710 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6711 */
6712 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006713switch_buffer(bufref_T *save_curbuf, buf_T *buf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006714{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006715 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006716 set_bufref(save_curbuf, curbuf);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006717 --curbuf->b_nwindows;
6718 curbuf = buf;
6719 curwin->w_buffer = buf;
6720 ++curbuf->b_nwindows;
6721}
6722
6723/*
6724 * Restore the current buffer after using switch_buffer().
6725 */
6726 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006727restore_buffer(bufref_T *save_curbuf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006728{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006729 unblock_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006730 /* Check for valid buffer, just in case. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006731 if (bufref_valid(save_curbuf))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006732 {
6733 --curbuf->b_nwindows;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006734 curwin->w_buffer = save_curbuf->br_buf;
6735 curbuf = save_curbuf->br_buf;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006736 ++curbuf->b_nwindows;
6737 }
6738}
6739#endif
6740
Bram Moolenaar4033c552017-09-16 20:54:51 +02006741#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742/*
6743 * Return TRUE if there is any vertically split window.
6744 */
6745 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006746win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747{
6748 frame_T *fr;
6749
6750 if (topframe->fr_layout == FR_ROW)
6751 return TRUE;
6752
6753 if (topframe->fr_layout == FR_COL)
6754 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6755 if (fr->fr_layout == FR_ROW)
6756 return TRUE;
6757
6758 return FALSE;
6759}
6760#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006761
6762#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6763/*
6764 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006765 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006766 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6767 * If no particular ID is desired, -1 must be specified for 'id'.
6768 * Return ID of added match, -1 on failure.
6769 */
6770 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006771match_add(
6772 win_T *wp,
6773 char_u *grp,
6774 char_u *pat,
6775 int prio,
6776 int id,
6777 list_T *pos_list,
6778 char_u *conceal_char UNUSED) /* pointer to conceal replacement char */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006779{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006780 matchitem_T *cur;
6781 matchitem_T *prev;
6782 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006783 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006784 regprog_T *regprog = NULL;
6785 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006786
Bram Moolenaarb3414592014-06-17 17:48:32 +02006787 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006788 return -1;
6789 if (id < -1 || id == 0)
6790 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02006791 EMSGN(_("E799: Invalid ID: %ld (must be greater than or equal to 1)"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006792 return -1;
6793 }
6794 if (id != -1)
6795 {
6796 cur = wp->w_match_head;
6797 while (cur != NULL)
6798 {
6799 if (cur->id == id)
6800 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02006801 EMSGN(_("E801: ID already taken: %ld"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006802 return -1;
6803 }
6804 cur = cur->next;
6805 }
6806 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006807 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006808 {
6809 EMSG2(_(e_nogroup), grp);
6810 return -1;
6811 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006812 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006813 {
6814 EMSG2(_(e_invarg2), pat);
6815 return -1;
6816 }
6817
6818 /* Find available match ID. */
6819 while (id == -1)
6820 {
6821 cur = wp->w_match_head;
6822 while (cur != NULL && cur->id != wp->w_next_match_id)
6823 cur = cur->next;
6824 if (cur == NULL)
6825 id = wp->w_next_match_id;
6826 wp->w_next_match_id++;
6827 }
6828
6829 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006830 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006831 m->id = id;
6832 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006833 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006834 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006835 m->match.regprog = regprog;
6836 m->match.rmm_ic = FALSE;
6837 m->match.rmm_maxcol = 0;
Bram Moolenaar42356152016-03-31 22:27:40 +02006838# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +02006839 m->conceal_char = 0;
6840 if (conceal_char != NULL)
6841 m->conceal_char = (*mb_ptr2char)(conceal_char);
Bram Moolenaar42356152016-03-31 22:27:40 +02006842# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006843
Bram Moolenaarb3414592014-06-17 17:48:32 +02006844 /* Set up position matches */
6845 if (pos_list != NULL)
6846 {
6847 linenr_T toplnum = 0;
6848 linenr_T botlnum = 0;
6849 listitem_T *li;
6850 int i;
6851
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006852 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006853 i++, li = li->li_next)
6854 {
6855 linenr_T lnum = 0;
6856 colnr_T col = 0;
6857 int len = 1;
6858 list_T *subl;
6859 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006860 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006861
Bram Moolenaarb3414592014-06-17 17:48:32 +02006862 if (li->li_tv.v_type == VAR_LIST)
6863 {
6864 subl = li->li_tv.vval.v_list;
6865 if (subl == NULL)
6866 goto fail;
6867 subli = subl->lv_first;
6868 if (subli == NULL)
6869 goto fail;
6870 lnum = get_tv_number_chk(&subli->li_tv, &error);
6871 if (error == TRUE)
6872 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006873 if (lnum == 0)
6874 {
6875 --i;
6876 continue;
6877 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006878 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006879 subli = subli->li_next;
6880 if (subli != NULL)
6881 {
6882 col = get_tv_number_chk(&subli->li_tv, &error);
6883 if (error == TRUE)
6884 goto fail;
6885 subli = subli->li_next;
6886 if (subli != NULL)
6887 {
6888 len = get_tv_number_chk(&subli->li_tv, &error);
6889 if (error == TRUE)
6890 goto fail;
6891 }
6892 }
6893 m->pos.pos[i].col = col;
6894 m->pos.pos[i].len = len;
6895 }
6896 else if (li->li_tv.v_type == VAR_NUMBER)
6897 {
6898 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006899 {
6900 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006901 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006902 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006903 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6904 m->pos.pos[i].col = 0;
6905 m->pos.pos[i].len = 0;
6906 }
6907 else
6908 {
6909 EMSG(_("List or number required"));
6910 goto fail;
6911 }
6912 if (toplnum == 0 || lnum < toplnum)
6913 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006914 if (botlnum == 0 || lnum >= botlnum)
6915 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006916 }
6917
6918 /* Calculate top and bottom lines for redrawing area */
6919 if (toplnum != 0)
6920 {
6921 if (wp->w_buffer->b_mod_set)
6922 {
6923 if (wp->w_buffer->b_mod_top > toplnum)
6924 wp->w_buffer->b_mod_top = toplnum;
6925 if (wp->w_buffer->b_mod_bot < botlnum)
6926 wp->w_buffer->b_mod_bot = botlnum;
6927 }
6928 else
6929 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006930 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006931 wp->w_buffer->b_mod_top = toplnum;
6932 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006933 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006934 }
6935 m->pos.toplnum = toplnum;
6936 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006937 rtype = VALID;
6938 }
6939 }
6940
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006941 /* Insert new match. The match list is in ascending order with regard to
6942 * the match priorities. */
6943 cur = wp->w_match_head;
6944 prev = cur;
6945 while (cur != NULL && prio >= cur->priority)
6946 {
6947 prev = cur;
6948 cur = cur->next;
6949 }
6950 if (cur == prev)
6951 wp->w_match_head = m;
6952 else
6953 prev->next = m;
6954 m->next = cur;
6955
Bram Moolenaarb3414592014-06-17 17:48:32 +02006956 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006957 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006958
6959fail:
6960 vim_free(m);
6961 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006962}
6963
6964/*
6965 * Delete match with ID 'id' in the match list of window 'wp'.
6966 * Print error messages if 'perr' is TRUE.
6967 */
6968 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006969match_delete(win_T *wp, int id, int perr)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006970{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006971 matchitem_T *cur = wp->w_match_head;
6972 matchitem_T *prev = cur;
6973 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006974
6975 if (id < 1)
6976 {
6977 if (perr == TRUE)
Bram Moolenaar5b302912016-08-24 22:11:55 +02006978 EMSGN(_("E802: Invalid ID: %ld (must be greater than or equal to 1)"),
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006979 id);
6980 return -1;
6981 }
6982 while (cur != NULL && cur->id != id)
6983 {
6984 prev = cur;
6985 cur = cur->next;
6986 }
6987 if (cur == NULL)
6988 {
6989 if (perr == TRUE)
Bram Moolenaar5b302912016-08-24 22:11:55 +02006990 EMSGN(_("E803: ID not found: %ld"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006991 return -1;
6992 }
6993 if (cur == prev)
6994 wp->w_match_head = cur->next;
6995 else
6996 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006997 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006998 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006999 if (cur->pos.toplnum != 0)
7000 {
7001 if (wp->w_buffer->b_mod_set)
7002 {
7003 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
7004 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7005 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
7006 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
7007 }
7008 else
7009 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007010 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007011 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7012 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007013 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007014 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007015 rtype = VALID;
7016 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007017 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007018 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007019 return 0;
7020}
7021
7022/*
7023 * Delete all matches in the match list of window 'wp'.
7024 */
7025 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007026clear_matches(win_T *wp)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007027{
7028 matchitem_T *m;
7029
7030 while (wp->w_match_head != NULL)
7031 {
7032 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007033 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007034 vim_free(wp->w_match_head->pattern);
7035 vim_free(wp->w_match_head);
7036 wp->w_match_head = m;
7037 }
7038 redraw_later(SOME_VALID);
7039}
7040
7041/*
7042 * Get match from ID 'id' in window 'wp'.
7043 * Return NULL if match not found.
7044 */
7045 matchitem_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007046get_match(win_T *wp, int id)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007047{
7048 matchitem_T *cur = wp->w_match_head;
7049
7050 while (cur != NULL && cur->id != id)
7051 cur = cur->next;
7052 return cur;
7053}
7054#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007055
7056#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7057 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007058get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007059{
7060 int i = 1;
7061 win_T *w;
7062
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007063 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007064 ++i;
7065
7066 if (w == NULL)
7067 return 0;
7068 else
7069 return i;
7070}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007071
7072 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007073get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007074{
7075 int i = 1;
7076 tabpage_T *t;
7077
7078 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7079 ++i;
7080
7081 if (t == NULL)
7082 return 0;
7083 else
7084 return i;
7085}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007086#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007087
7088/*
7089 * Return TRUE if "topfrp" and its children are at the right height.
7090 */
7091 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007092frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007093{
7094 frame_T *frp;
7095
7096 if (topfrp->fr_height != height)
7097 return FALSE;
7098
7099 if (topfrp->fr_layout == FR_ROW)
7100 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7101 if (frp->fr_height != height)
7102 return FALSE;
7103
7104 return TRUE;
7105}
7106
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007107/*
7108 * Return TRUE if "topfrp" and its children are at the right width.
7109 */
7110 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007111frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007112{
7113 frame_T *frp;
7114
7115 if (topfrp->fr_width != width)
7116 return FALSE;
7117
7118 if (topfrp->fr_layout == FR_COL)
7119 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7120 if (frp->fr_width != width)
7121 return FALSE;
7122
7123 return TRUE;
7124}
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007125
Bram Moolenaar86edef62016-03-13 18:07:30 +01007126#if defined(FEAT_EVAL) || defined(PROTO)
7127 int
7128win_getid(typval_T *argvars)
7129{
7130 int winnr;
7131 win_T *wp;
7132
7133 if (argvars[0].v_type == VAR_UNKNOWN)
7134 return curwin->w_id;
7135 winnr = get_tv_number(&argvars[0]);
7136 if (winnr > 0)
7137 {
7138 if (argvars[1].v_type == VAR_UNKNOWN)
7139 wp = firstwin;
7140 else
7141 {
7142 tabpage_T *tp;
7143 int tabnr = get_tv_number(&argvars[1]);
7144
Bram Moolenaar29323592016-07-24 22:04:11 +02007145 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007146 if (--tabnr == 0)
7147 break;
7148 if (tp == NULL)
7149 return -1;
Bram Moolenaar8e639052016-11-13 14:31:40 +01007150 if (tp == curtab)
7151 wp = firstwin;
7152 else
7153 wp = tp->tp_firstwin;
Bram Moolenaar86edef62016-03-13 18:07:30 +01007154 }
7155 for ( ; wp != NULL; wp = wp->w_next)
7156 if (--winnr == 0)
7157 return wp->w_id;
7158 }
7159 return 0;
7160}
7161
7162 int
7163win_gotoid(typval_T *argvars)
7164{
7165 win_T *wp;
7166 tabpage_T *tp;
7167 int id = get_tv_number(&argvars[0]);
7168
Bram Moolenaar29323592016-07-24 22:04:11 +02007169 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007170 if (wp->w_id == id)
7171 {
7172 goto_tabpage_win(tp, wp);
7173 return 1;
7174 }
7175 return 0;
7176}
7177
7178 void
7179win_id2tabwin(typval_T *argvars, list_T *list)
7180{
7181 win_T *wp;
7182 tabpage_T *tp;
7183 int winnr = 1;
7184 int tabnr = 1;
7185 int id = get_tv_number(&argvars[0]);
7186
Bram Moolenaar29323592016-07-24 22:04:11 +02007187 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007188 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007189 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007190 {
7191 if (wp->w_id == id)
7192 {
7193 list_append_number(list, tabnr);
7194 list_append_number(list, winnr);
7195 return;
7196 }
7197 ++winnr;
7198 }
7199 ++tabnr;
7200 winnr = 1;
7201 }
7202 list_append_number(list, 0);
7203 list_append_number(list, 0);
7204}
7205
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007206 win_T *
7207win_id2wp(typval_T *argvars)
7208{
7209 win_T *wp;
7210 tabpage_T *tp;
7211 int id = get_tv_number(&argvars[0]);
7212
7213 FOR_ALL_TAB_WINDOWS(tp, wp)
7214 if (wp->w_id == id)
7215 return wp;
7216
7217 return NULL;
7218}
7219
Bram Moolenaar86edef62016-03-13 18:07:30 +01007220 int
7221win_id2win(typval_T *argvars)
7222{
7223 win_T *wp;
7224 int nr = 1;
7225 int id = get_tv_number(&argvars[0]);
7226
Bram Moolenaar29323592016-07-24 22:04:11 +02007227 FOR_ALL_WINDOWS(wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007228 {
7229 if (wp->w_id == id)
7230 return nr;
7231 ++nr;
7232 }
7233 return 0;
7234}
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007235
7236 void
7237win_findbuf(typval_T *argvars, list_T *list)
7238{
7239 win_T *wp;
7240 tabpage_T *tp;
7241 int bufnr = get_tv_number(&argvars[0]);
7242
Bram Moolenaar29323592016-07-24 22:04:11 +02007243 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007244 if (wp->w_buffer->b_fnum == bufnr)
7245 list_append_number(list, wp->w_id);
7246}
7247
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02007248/*
7249 * Get the layout of the given tab page for winlayout().
7250 */
7251 void
7252get_framelayout(frame_T *fr, list_T *l, int outer)
7253{
7254 frame_T *child;
7255 list_T *fr_list;
7256 list_T *win_list;
7257
7258 if (fr == NULL)
7259 return;
7260
7261 if (outer)
7262 // outermost call from f_winlayout()
7263 fr_list = l;
7264 else
7265 {
7266 fr_list = list_alloc();
7267 if (fr_list == NULL)
7268 return;
7269 list_append_list(l, fr_list);
7270 }
7271
7272 if (fr->fr_layout == FR_LEAF)
7273 {
7274 if (fr->fr_win != NULL)
7275 {
7276 list_append_string(fr_list, (char_u *)"leaf", -1);
7277 list_append_number(fr_list, fr->fr_win->w_id);
7278 }
7279 }
7280 else
7281 {
7282 list_append_string(fr_list,
7283 fr->fr_layout == FR_ROW ? (char_u *)"row" : (char_u *)"col", -1);
7284
7285 win_list = list_alloc();
7286 if (win_list == NULL)
7287 return;
7288 list_append_list(fr_list, win_list);
7289 child = fr->fr_child;
7290 while (child != NULL)
7291 {
7292 get_framelayout(child, win_list, FALSE);
7293 child = child->fr_next;
7294 }
7295 }
7296}
Bram Moolenaar86edef62016-03-13 18:07:30 +01007297#endif