blob: e16570f4e3e8321c6eae8abb55c709830536ab23 [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 Moolenaar2f1e51a2014-12-13 03:58:09 +0100154 cmd_with_count("split #", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 do_cmdline_cmd(cbuf);
156 break;
157
158/* open new window */
159 case Ctrl_N:
160 case 'n':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200161 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000163#ifdef FEAT_QUICKFIX
164newwindow:
165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000167 /* window height */
168 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 else
170 cbuf[0] = NUL;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100171#if defined(FEAT_QUICKFIX)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000172 if (nchar == 'v' || nchar == Ctrl_V)
173 STRCAT(cbuf, "v");
174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 STRCAT(cbuf, "new");
176 do_cmdline_cmd(cbuf);
177 break;
178
179/* quit current window */
180 case Ctrl_Q:
181 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100183 cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100184 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 break;
186
187/* close current window */
188 case Ctrl_C:
189 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100191 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100192 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 break;
194
Bram Moolenaar4033c552017-09-16 20:54:51 +0200195#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/* close preview window */
197 case Ctrl_Z:
198 case 'z':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200199 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 do_cmdline_cmd((char_u *)"pclose");
202 break;
203
204/* cursor to preview window */
205 case 'P':
Bram Moolenaar29323592016-07-24 22:04:11 +0200206 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 if (wp->w_p_pvw)
208 break;
209 if (wp == NULL)
210 EMSG(_("E441: There is no preview window"));
211 else
212 win_goto(wp);
213 break;
214#endif
215
216/* close all but current window */
217 case Ctrl_O:
218 case 'o':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200219 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100221 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100222 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 break;
224
225/* cursor to next window with wrap around */
226 case Ctrl_W:
227 case 'w':
228/* cursor to previous window with wrap around */
229 case 'W':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200230 CHECK_CMDWIN;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +0100231 if (ONE_WINDOW && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 beep_flush();
233 else
234 {
235 if (Prenum) /* go to specified window */
236 {
237 for (wp = firstwin; --Prenum > 0; )
238 {
239 if (wp->w_next == NULL)
240 break;
241 else
242 wp = wp->w_next;
243 }
244 }
245 else
246 {
247 if (nchar == 'W') /* go to previous window */
248 {
249 wp = curwin->w_prev;
250 if (wp == NULL)
251 wp = lastwin; /* wrap around */
252 }
253 else /* go to next window */
254 {
255 wp = curwin->w_next;
256 if (wp == NULL)
257 wp = firstwin; /* wrap around */
258 }
259 }
260 win_goto(wp);
261 }
262 break;
263
264/* cursor to window below */
265 case 'j':
266 case K_DOWN:
267 case Ctrl_J:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200268 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 win_goto_ver(FALSE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 break;
271
272/* cursor to window above */
273 case 'k':
274 case K_UP:
275 case Ctrl_K:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200276 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 win_goto_ver(TRUE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 break;
279
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280/* cursor to left window */
281 case 'h':
282 case K_LEFT:
283 case Ctrl_H:
284 case K_BS:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200285 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 win_goto_hor(TRUE, Prenum1);
287 break;
288
289/* cursor to right window */
290 case 'l':
291 case K_RIGHT:
292 case Ctrl_L:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200293 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 win_goto_hor(FALSE, Prenum1);
295 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000297/* move window to new tab page */
298 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000299 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000300 MSG(_(m_onlyone));
301 else
302 {
303 tabpage_T *oldtab = curtab;
304 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000305
306 /* First create a new tab with the window, then go back to
307 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000308 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000309 if (win_new_tabpage((int)Prenum) == OK
310 && valid_tabpage(oldtab))
311 {
312 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200313 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000314 if (curwin == wp)
315 win_close(curwin, FALSE);
316 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200317 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000318 }
319 }
320 break;
321
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322/* cursor to top-left window */
323 case 't':
324 case Ctrl_T:
325 win_goto(firstwin);
326 break;
327
328/* cursor to bottom-right window */
329 case 'b':
330 case Ctrl_B:
331 win_goto(lastwin);
332 break;
333
334/* cursor to last accessed (previous) window */
335 case 'p':
336 case Ctrl_P:
Bram Moolenaar3dda7db2016-04-03 21:22:58 +0200337 if (!win_valid(prevwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 beep_flush();
339 else
340 win_goto(prevwin);
341 break;
342
343/* exchange current and next window */
344 case 'x':
345 case Ctrl_X:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200346 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 win_exchange(Prenum);
348 break;
349
350/* rotate windows downwards */
351 case Ctrl_R:
352 case 'r':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200353 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 win_rotate(FALSE, (int)Prenum1); /* downwards */
356 break;
357
358/* rotate windows upwards */
359 case 'R':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200360 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 win_rotate(TRUE, (int)Prenum1); /* upwards */
363 break;
364
365/* move window to the very top/bottom/left/right */
366 case 'K':
367 case 'J':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 case 'H':
369 case 'L':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200370 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 win_totop((int)Prenum,
372 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
373 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
374 break;
375
376/* make all windows the same height */
377 case '=':
378#ifdef FEAT_GUI
379 need_mouse_correct = TRUE;
380#endif
381 win_equal(NULL, FALSE, 'b');
382 break;
383
384/* increase current window height */
385 case '+':
386#ifdef FEAT_GUI
387 need_mouse_correct = TRUE;
388#endif
389 win_setheight(curwin->w_height + (int)Prenum1);
390 break;
391
392/* decrease current window height */
393 case '-':
394#ifdef FEAT_GUI
395 need_mouse_correct = TRUE;
396#endif
397 win_setheight(curwin->w_height - (int)Prenum1);
398 break;
399
400/* set current window height */
401 case Ctrl__:
402 case '_':
403#ifdef FEAT_GUI
404 need_mouse_correct = TRUE;
405#endif
406 win_setheight(Prenum ? (int)Prenum : 9999);
407 break;
408
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409/* increase current window width */
410 case '>':
411#ifdef FEAT_GUI
412 need_mouse_correct = TRUE;
413#endif
414 win_setwidth(curwin->w_width + (int)Prenum1);
415 break;
416
417/* decrease current window width */
418 case '<':
419#ifdef FEAT_GUI
420 need_mouse_correct = TRUE;
421#endif
422 win_setwidth(curwin->w_width - (int)Prenum1);
423 break;
424
425/* set current window width */
426 case '|':
427#ifdef FEAT_GUI
428 need_mouse_correct = TRUE;
429#endif
430 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
431 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432
433/* jump to tag and split window if tag exists (in preview window) */
434#if defined(FEAT_QUICKFIX)
435 case '}':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200436 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 if (Prenum)
438 g_do_tagpreview = Prenum;
439 else
440 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200442 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 case ']':
444 case Ctrl_RSB:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200445 CHECK_CMDWIN;
Bram Moolenaard355c502014-09-23 13:48:43 +0200446 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 if (Prenum)
448 postponed_split = Prenum;
449 else
450 postponed_split = -1;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200451#ifdef FEAT_QUICKFIX
Bram Moolenaar56095e12014-10-09 10:44:37 +0200452 if (nchar != '}')
453 g_do_tagpreview = 0;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455
Bram Moolenaard355c502014-09-23 13:48:43 +0200456 /* Execute the command right here, required when "wincmd ]"
457 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 do_nv_ident(Ctrl_RSB, NUL);
459 break;
460
461#ifdef FEAT_SEARCHPATH
462/* edit file name under cursor in a new window */
463 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000464 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000466wingotofile:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200467 CHECK_CMDWIN;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000468
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000469 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 if (ptr != NULL)
471 {
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200472 tabpage_T *oldtab = curtab;
473 win_T *oldwin = curwin;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000474# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000476# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 setpcmark();
478 if (win_split(0, 0) == OK)
479 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200480 RESET_BINDING(curwin);
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200481 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
482 ECMD_HIDE, NULL) == FAIL)
483 {
484 /* Failed to open the file, close the window
485 * opened for it. */
486 win_close(curwin, FALSE);
487 goto_tabpage_win(oldtab, oldwin);
488 }
489 else if (nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000490 {
491 curwin->w_cursor.lnum = lnum;
492 check_cursor_lnum();
493 beginline(BL_SOL | BL_FIX);
494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 }
496 vim_free(ptr);
497 }
498 break;
499#endif
500
501#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000502/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 * new window -- webb
504 */
505 case 'i': /* Go to any match */
506 case Ctrl_I:
507 type = FIND_ANY;
508 /* FALLTHROUGH */
509 case 'd': /* Go to definition, using 'define' */
510 case Ctrl_D:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200511 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
513 break;
514 find_pattern_in_path(ptr, 0, len, TRUE,
515 Prenum == 0 ? TRUE : FALSE, type,
516 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
517 curwin->w_set_curswant = TRUE;
518 break;
519#endif
520
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200521/* Quickfix window only: view the result under the cursor in a new split. */
522#if defined(FEAT_QUICKFIX)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000523 case K_KENTER:
524 case CAR:
Bram Moolenaar05159a02005-02-26 23:04:13 +0000525 if (bt_quickfix(curbuf))
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200526 qf_view_result(TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000527 break;
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200528#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000529
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530/* CTRL-W g extended commands */
531 case 'g':
532 case Ctrl_G:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200533 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534#ifdef USE_ON_FLY_SCROLL
535 dont_scroll = TRUE; /* disallow scrolling here */
536#endif
537 ++no_mapping;
538 ++allow_keys; /* no mapping for xchar, but allow key codes */
539 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000540 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 --no_mapping;
543 --allow_keys;
544#ifdef FEAT_CMDL_INFO
545 (void)add_to_showcmd(xchar);
546#endif
547 switch (xchar)
548 {
549#if defined(FEAT_QUICKFIX)
550 case '}':
551 xchar = Ctrl_RSB;
552 if (Prenum)
553 g_do_tagpreview = Prenum;
554 else
555 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200557 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 case ']':
559 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200560 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 if (Prenum)
562 postponed_split = Prenum;
563 else
564 postponed_split = -1;
565
566 /* Execute the command right here, required when
567 * "wincmd g}" was used in a function. */
568 do_nv_ident('g', xchar);
569 break;
570
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000571#ifdef FEAT_SEARCHPATH
572 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000573 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100574 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000575 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000576 goto wingotofile;
577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 default:
579 beep_flush();
580 break;
581 }
582 break;
583
584 default: beep_flush();
585 break;
586 }
587}
588
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100589/*
590 * Figure out the address type for ":wnncmd".
591 */
592 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100593get_wincmd_addr_type(char_u *arg, exarg_T *eap)
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100594{
595 switch (*arg)
596 {
597 case 'S':
598 case Ctrl_S:
599 case 's':
600 case Ctrl_N:
601 case 'n':
602 case 'j':
603 case Ctrl_J:
604 case 'k':
605 case Ctrl_K:
606 case 'T':
607 case Ctrl_R:
608 case 'r':
609 case 'R':
610 case 'K':
611 case 'J':
612 case '+':
613 case '-':
614 case Ctrl__:
615 case '_':
616 case '|':
617 case ']':
618 case Ctrl_RSB:
619 case 'g':
620 case Ctrl_G:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100621 case Ctrl_V:
622 case 'v':
623 case 'h':
624 case Ctrl_H:
625 case 'l':
626 case Ctrl_L:
627 case 'H':
628 case 'L':
629 case '>':
630 case '<':
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100631#if defined(FEAT_QUICKFIX)
632 case '}':
633#endif
634#ifdef FEAT_SEARCHPATH
635 case 'f':
636 case 'F':
637 case Ctrl_F:
638#endif
639#ifdef FEAT_FIND_ID
640 case 'i':
641 case Ctrl_I:
642 case 'd':
643 case Ctrl_D:
644#endif
645 /* window size or any count */
646 eap->addr_type = ADDR_LINES;
647 break;
648
649 case Ctrl_HAT:
650 case '^':
651 /* buffer number */
652 eap->addr_type = ADDR_BUFFERS;
653 break;
654
655 case Ctrl_Q:
656 case 'q':
657 case Ctrl_C:
658 case 'c':
659 case Ctrl_O:
660 case 'o':
661 case Ctrl_W:
662 case 'w':
663 case 'W':
664 case 'x':
665 case Ctrl_X:
666 /* window number */
667 eap->addr_type = ADDR_WINDOWS;
668 break;
669
670#if defined(FEAT_QUICKFIX)
671 case Ctrl_Z:
672 case 'z':
673 case 'P':
674#endif
675 case 't':
676 case Ctrl_T:
677 case 'b':
678 case Ctrl_B:
679 case 'p':
680 case Ctrl_P:
681 case '=':
682 case CAR:
683 /* no count */
684 eap->addr_type = 0;
685 break;
686 }
687}
688
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100689 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100690cmd_with_count(
691 char *cmd,
692 char_u *bufp,
693 size_t bufsize,
694 long Prenum)
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100695{
696 size_t len = STRLEN(cmd);
697
698 STRCPY(bufp, cmd);
699 if (Prenum > 0)
700 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
701}
702
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703/*
704 * split the current window, implements CTRL-W s and :split
705 *
706 * "size" is the height or width for the new window, 0 to use half of current
707 * height or width.
708 *
709 * "flags":
710 * WSP_ROOM: require enough room for new window
711 * WSP_VERT: vertical split.
712 * WSP_TOP: open window at the top-left of the shell (help window).
713 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
714 * WSP_HELP: creating the help window, keep layout snapshot
715 *
716 * return FAIL for failure, OK otherwise
717 */
718 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100719win_split(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000721 /* When the ":tab" modifier was used open a new tab page instead. */
722 if (may_open_tabpage() == OK)
723 return OK;
724
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 /* Add flags from ":vertical", ":topleft" and ":botright". */
726 flags |= cmdmod.split;
727 if ((flags & WSP_TOP) && (flags & WSP_BOT))
728 {
729 EMSG(_("E442: Can't split topleft and botright at the same time"));
730 return FAIL;
731 }
732
733 /* When creating the help window make a snapshot of the window layout.
734 * Otherwise clear the snapshot, it's now invalid. */
735 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000736 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000738 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
740 return win_split_ins(size, flags, NULL, 0);
741}
742
743/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100744 * When "new_wp" is NULL: split the current window in two.
745 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 * top/left/right/bottom.
747 * return FAIL for failure, OK otherwise
748 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000749 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100750win_split_ins(
751 int size,
752 int flags,
753 win_T *new_wp,
754 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100756 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 win_T *oldwin;
758 int new_size = size;
759 int i;
760 int need_status = 0;
761 int do_equal = FALSE;
762 int needed;
763 int available;
764 int oldwin_height = 0;
765 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200766 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200768 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200769 int wmh1;
Bram Moolenaar98da6ec2018-04-13 22:15:46 +0200770 int did_set_fraction = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
772 if (flags & WSP_TOP)
773 oldwin = firstwin;
774 else if (flags & WSP_BOT)
775 oldwin = lastwin;
776 else
777 oldwin = curwin;
778
779 /* add a status line when p_ls == 1 and splitting the first window */
Bram Moolenaar459ca562016-11-10 18:16:33 +0100780 if (ONE_WINDOW && p_ls == 1 && oldwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 {
Bram Moolenaar415a6932017-12-05 20:31:07 +0100782 if (VISIBLE_HEIGHT(oldwin) <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 {
784 EMSG(_(e_noroom));
785 return FAIL;
786 }
787 need_status = STATUS_HEIGHT;
788 }
789
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000790#ifdef FEAT_GUI
791 /* May be needed for the scrollbars that are going to change. */
792 if (gui.in_use)
793 out_flush();
794#endif
795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 if (flags & WSP_VERT)
797 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200798 int wmw1;
799 int minwidth;
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
803 /*
804 * Check if we are able to split the current window and compute its
805 * width.
806 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200807 /* Current window requires at least 1 space. */
808 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
809 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200811 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200812 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200814 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200816 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200818 else if (p_ea)
819 {
820 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
821 prevfrp = oldwin->w_frame;
822 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
823 frp = frp->fr_parent)
824 {
825 if (frp->fr_layout == FR_ROW)
826 for (frp2 = frp->fr_child; frp2 != NULL;
827 frp2 = frp2->fr_next)
828 if (frp2 != prevfrp)
829 minwidth += frame_minwidth(frp2, NOWIN);
830 prevfrp = frp;
831 }
832 available = topframe->fr_width;
833 needed += minwidth;
834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200836 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200837 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
838 available = oldwin->w_frame->fr_width;
839 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200840 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100841 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {
843 EMSG(_(e_noroom));
844 return FAIL;
845 }
846 if (new_size == 0)
847 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200848 if (new_size > available - minwidth - 1)
849 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200850 if (new_size < wmw1)
851 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
853 /* if it doesn't fit in the current window, need win_equal() */
854 if (oldwin->w_width - new_size - 1 < p_wmw)
855 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000856
857 /* We don't like to take lines for the new window from a
858 * 'winfixwidth' window. Take them from a window to the left or right
Bram Moolenaar38e34832017-03-19 20:22:36 +0100859 * instead, if possible. Add one for the separator. */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000860 if (oldwin->w_p_wfw)
Bram Moolenaar38e34832017-03-19 20:22:36 +0100861 win_setwidth_win(oldwin->w_width + new_size + 1, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000862
863 /* Only make all windows the same width if one of them (except oldwin)
864 * is wider than one of the split windows. */
865 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
866 && oldwin->w_frame->fr_parent != NULL)
867 {
868 frp = oldwin->w_frame->fr_parent->fr_child;
869 while (frp != NULL)
870 {
871 if (frp->fr_win != oldwin && frp->fr_win != NULL
872 && (frp->fr_win->w_width > new_size
873 || frp->fr_win->w_width > oldwin->w_width
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100874 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000875 {
876 do_equal = TRUE;
877 break;
878 }
879 frp = frp->fr_next;
880 }
881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 }
883 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 {
885 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886
887 /*
888 * Check if we are able to split the current window and compute its
889 * height.
890 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200891 /* Current window requires at least 1 space. */
Bram Moolenaar415a6932017-12-05 20:31:07 +0100892 wmh1 = (p_wmh == 0 ? 1 : p_wmh) + WINBAR_HEIGHT(curwin);
Bram Moolenaar1f538352014-07-16 18:19:27 +0200893 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200895 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200896 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200898 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200900 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200902 else if (p_ea)
903 {
904 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
905 prevfrp = oldwin->w_frame;
906 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
907 frp = frp->fr_parent)
908 {
909 if (frp->fr_layout == FR_COL)
910 for (frp2 = frp->fr_child; frp2 != NULL;
911 frp2 = frp2->fr_next)
912 if (frp2 != prevfrp)
913 minheight += frame_minheight(frp2, NOWIN);
914 prevfrp = frp;
915 }
916 available = topframe->fr_height;
917 needed += minheight;
918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 else
920 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200921 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
922 available = oldwin->w_frame->fr_height;
923 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100925 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
927 EMSG(_(e_noroom));
928 return FAIL;
929 }
930 oldwin_height = oldwin->w_height;
931 if (need_status)
932 {
933 oldwin->w_status_height = STATUS_HEIGHT;
934 oldwin_height -= STATUS_HEIGHT;
935 }
936 if (new_size == 0)
937 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200938 if (new_size > available - minheight - STATUS_HEIGHT)
939 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200940 if (new_size < wmh1)
941 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
943 /* if it doesn't fit in the current window, need win_equal() */
944 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
945 do_equal = TRUE;
946
947 /* We don't like to take lines for the new window from a
948 * 'winfixheight' window. Take them from a window above or below
949 * instead, if possible. */
950 if (oldwin->w_p_wfh)
951 {
Bram Moolenaar98da6ec2018-04-13 22:15:46 +0200952 /* Set w_fraction now so that the cursor keeps the same relative
953 * vertical position using the old height. */
954 set_fraction(oldwin);
955 did_set_fraction = TRUE;
956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
958 oldwin);
959 oldwin_height = oldwin->w_height;
960 if (need_status)
961 oldwin_height -= STATUS_HEIGHT;
962 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000963
964 /* Only make all windows the same height if one of them (except oldwin)
965 * is higher than one of the split windows. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100966 if (!do_equal && p_ea && size == 0 && *p_ead != 'h'
Bram Moolenaar67f71312007-08-12 14:55:56 +0000967 && oldwin->w_frame->fr_parent != NULL)
968 {
969 frp = oldwin->w_frame->fr_parent->fr_child;
970 while (frp != NULL)
971 {
972 if (frp->fr_win != oldwin && frp->fr_win != NULL
973 && (frp->fr_win->w_height > new_size
974 || frp->fr_win->w_height > oldwin_height - new_size
975 - STATUS_HEIGHT))
976 {
977 do_equal = TRUE;
978 break;
979 }
980 frp = frp->fr_next;
981 }
982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 }
984
985 /*
986 * allocate new window structure and link it in the window list
987 */
988 if ((flags & WSP_TOP) == 0
989 && ((flags & WSP_BOT)
990 || (flags & WSP_BELOW)
991 || (!(flags & WSP_ABOVE)
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100992 && ( (flags & WSP_VERT) ? p_spr : p_sb))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 {
994 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100995 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000996 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 else
998 win_append(oldwin, wp);
999 }
1000 else
1001 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001002 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001003 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 else
1005 win_append(oldwin->w_prev, wp);
1006 }
1007
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001008 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 {
1010 if (wp == NULL)
1011 return FAIL;
1012
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001013 new_frame(wp);
1014 if (wp->w_frame == NULL)
1015 {
1016 win_free(wp, NULL);
1017 return FAIL;
1018 }
1019
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001020 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001021 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
1023
1024 /*
1025 * Reorganise the tree of frames to insert the new window.
1026 */
1027 if (flags & (WSP_TOP | WSP_BOT))
1028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1030 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 {
1032 curfrp = topframe->fr_child;
1033 if (flags & WSP_BOT)
1034 while (curfrp->fr_next != NULL)
1035 curfrp = curfrp->fr_next;
1036 }
1037 else
1038 curfrp = topframe;
1039 before = (flags & WSP_TOP);
1040 }
1041 else
1042 {
1043 curfrp = oldwin->w_frame;
1044 if (flags & WSP_BELOW)
1045 before = FALSE;
1046 else if (flags & WSP_ABOVE)
1047 before = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001048 else if (flags & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 before = !p_spr;
1050 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 before = !p_sb;
1052 }
1053 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1054 {
1055 /* Need to create a new frame in the tree to make a branch. */
1056 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
1057 *frp = *curfrp;
1058 curfrp->fr_layout = layout;
1059 frp->fr_parent = curfrp;
1060 frp->fr_next = NULL;
1061 frp->fr_prev = NULL;
1062 curfrp->fr_child = frp;
1063 curfrp->fr_win = NULL;
1064 curfrp = frp;
1065 if (frp->fr_win != NULL)
1066 oldwin->w_frame = frp;
1067 else
1068 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1069 frp->fr_parent = curfrp;
1070 }
1071
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001072 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001073 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001075 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 frp->fr_parent = curfrp->fr_parent;
1077
1078 /* Insert the new frame at the right place in the frame list. */
1079 if (before)
1080 frame_insert(curfrp, frp);
1081 else
1082 frame_append(curfrp, frp);
1083
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001084 /* Set w_fraction now so that the cursor keeps the same relative
1085 * vertical position. */
Bram Moolenaar98da6ec2018-04-13 22:15:46 +02001086 if (!did_set_fraction)
1087 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001088 wp->w_fraction = oldwin->w_fraction;
1089
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 if (flags & WSP_VERT)
1091 {
1092 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001093
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (need_status)
1095 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001096 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 oldwin->w_status_height = need_status;
1098 }
1099 if (flags & (WSP_TOP | WSP_BOT))
1100 {
1101 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001102 wp->w_winrow = tabline_height();
Bram Moolenaard326ad62017-09-18 20:31:41 +02001103 win_new_height(wp, curfrp->fr_height - (p_ls > 0)
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001104 - WINBAR_HEIGHT(wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 wp->w_status_height = (p_ls > 0);
1106 }
1107 else
1108 {
1109 /* height and row of new window is same as current window */
1110 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01001111 win_new_height(wp, VISIBLE_HEIGHT(oldwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 wp->w_status_height = oldwin->w_status_height;
1113 }
1114 frp->fr_height = curfrp->fr_height;
1115
1116 /* "new_size" of the current window goes to the new window, use
1117 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001118 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 if (before)
1120 wp->w_vsep_width = 1;
1121 else
1122 {
1123 wp->w_vsep_width = oldwin->w_vsep_width;
1124 oldwin->w_vsep_width = 1;
1125 }
1126 if (flags & (WSP_TOP | WSP_BOT))
1127 {
1128 if (flags & WSP_BOT)
1129 frame_add_vsep(curfrp);
1130 /* Set width of neighbor frame */
1131 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001132 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1133 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
1135 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001136 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 if (before) /* new window left of current one */
1138 {
1139 wp->w_wincol = oldwin->w_wincol;
1140 oldwin->w_wincol += new_size + 1;
1141 }
1142 else /* new window right of current one */
1143 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1144 frame_fix_width(oldwin);
1145 frame_fix_width(wp);
1146 }
1147 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {
1149 /* width and column of new window is same as current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 if (flags & (WSP_TOP | WSP_BOT))
1151 {
1152 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001153 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 wp->w_vsep_width = 0;
1155 }
1156 else
1157 {
1158 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001159 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 wp->w_vsep_width = oldwin->w_vsep_width;
1161 }
1162 frp->fr_width = curfrp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
1164 /* "new_size" of the current window goes to the new window, use
1165 * one row for the status line */
1166 win_new_height(wp, new_size);
1167 if (flags & (WSP_TOP | WSP_BOT))
Bram Moolenaar991dea32016-05-24 11:31:32 +02001168 {
Bram Moolenaard326ad62017-09-18 20:31:41 +02001169 int new_fr_height = curfrp->fr_height - new_size
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001170 + WINBAR_HEIGHT(wp) ;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001171
1172 if (!((flags & WSP_BOT) && p_ls == 0))
1173 new_fr_height -= STATUS_HEIGHT;
1174 frame_new_height(curfrp, new_fr_height, flags & WSP_TOP, FALSE);
1175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 else
1177 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1178 if (before) /* new window above current one */
1179 {
1180 wp->w_winrow = oldwin->w_winrow;
1181 wp->w_status_height = STATUS_HEIGHT;
1182 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1183 }
1184 else /* new window below current one */
1185 {
Bram Moolenaar415a6932017-12-05 20:31:07 +01001186 wp->w_winrow = oldwin->w_winrow + VISIBLE_HEIGHT(oldwin)
1187 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 wp->w_status_height = oldwin->w_status_height;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001189 if (!(flags & WSP_BOT))
1190 oldwin->w_status_height = STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 if (flags & WSP_BOT)
1193 frame_add_statusline(curfrp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 frame_fix_height(wp);
1195 frame_fix_height(oldwin);
1196 }
1197
1198 if (flags & (WSP_TOP | WSP_BOT))
1199 (void)win_comp_pos();
1200
1201 /*
1202 * Both windows need redrawing
1203 */
1204 redraw_win_later(wp, NOT_VALID);
1205 wp->w_redr_status = TRUE;
1206 redraw_win_later(oldwin, NOT_VALID);
1207 oldwin->w_redr_status = TRUE;
1208
1209 if (need_status)
1210 {
1211 msg_row = Rows - 1;
1212 msg_col = sc_col;
1213 msg_clr_eos_force(); /* Old command/ruler may still be there */
1214 comp_col();
1215 msg_row = Rows - 1;
1216 msg_col = 0; /* put position back at start of line */
1217 }
1218
1219 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001220 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 */
1222 if (do_equal || dir != 0)
1223 win_equal(wp, TRUE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001225 : dir == 'h' ? 'b' : 'v');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
1227 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1228 * size was given. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (flags & WSP_VERT)
1230 {
1231 i = p_wiw;
1232 if (size != 0)
1233 p_wiw = size;
1234
1235# ifdef FEAT_GUI
1236 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1237 if (gui.in_use)
1238 gui_init_which_components(NULL);
1239# endif
1240 }
1241 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
1243 i = p_wh;
1244 if (size != 0)
1245 p_wh = size;
1246 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001247
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001248#ifdef FEAT_JUMPLIST
1249 /* Keep same changelist position in new window. */
1250 wp->w_changelistidx = oldwin->w_changelistidx;
1251#endif
1252
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001253 /*
1254 * make the new window the current window
1255 */
Bram Moolenaarc917da42016-07-19 22:31:36 +02001256 win_enter_ext(wp, FALSE, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 if (flags & WSP_VERT)
1258 p_wiw = i;
1259 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 p_wh = i;
1261
1262 return OK;
1263}
1264
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001265
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001266/*
1267 * Initialize window "newp" from window "oldp".
1268 * Used when splitting a window and when creating a new tab page.
1269 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001270 * WSP_NEWLOC may be specified in flags to prevent the location list from
1271 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001272 */
1273 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001274win_init(win_T *newp, win_T *oldp, int flags UNUSED)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001275{
1276 int i;
1277
1278 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001279#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001280 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001281#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001282 oldp->w_buffer->b_nwindows++;
1283 newp->w_cursor = oldp->w_cursor;
1284 newp->w_valid = 0;
1285 newp->w_curswant = oldp->w_curswant;
1286 newp->w_set_curswant = oldp->w_set_curswant;
1287 newp->w_topline = oldp->w_topline;
1288#ifdef FEAT_DIFF
1289 newp->w_topfill = oldp->w_topfill;
1290#endif
1291 newp->w_leftcol = oldp->w_leftcol;
1292 newp->w_pcmark = oldp->w_pcmark;
1293 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1294 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001295 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001296 newp->w_fraction = oldp->w_fraction;
1297 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1298#ifdef FEAT_JUMPLIST
1299 copy_jumplist(oldp, newp);
1300#endif
1301#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001302 if (flags & WSP_NEWLOC)
1303 {
1304 /* Don't copy the location list. */
1305 newp->w_llist = NULL;
1306 newp->w_llist_ref = NULL;
1307 }
1308 else
Bram Moolenaar09037502018-09-25 22:08:14 +02001309 copy_loclist_stack(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001310#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001311 newp->w_localdir = (oldp->w_localdir == NULL)
1312 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001313
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001314 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001315 for (i = 0; i < oldp->w_tagstacklen; i++)
1316 {
1317 newp->w_tagstack[i] = oldp->w_tagstack[i];
1318 if (newp->w_tagstack[i].tagname != NULL)
1319 newp->w_tagstack[i].tagname =
1320 vim_strsave(newp->w_tagstack[i].tagname);
1321 }
1322 newp->w_tagstackidx = oldp->w_tagstackidx;
1323 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001324#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001325 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001326#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001327
1328 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001329
Bram Moolenaara971b822011-09-14 14:43:25 +02001330#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001331 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001332#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001333}
1334
1335/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001336 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001337 * Only the essential things are copied.
1338 */
1339 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001340win_init_some(win_T *newp, win_T *oldp)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001341{
1342 /* Use the same argument list. */
1343 newp->w_alist = oldp->w_alist;
1344 ++newp->w_alist->al_refcount;
1345 newp->w_arg_idx = oldp->w_arg_idx;
1346
1347 /* copy options from existing window */
1348 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001349}
1350
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001353 * Check if "win" is a pointer to an existing window in the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 */
1355 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001356win_valid(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357{
1358 win_T *wp;
1359
1360 if (win == NULL)
1361 return FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02001362 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 if (wp == win)
1364 return TRUE;
1365 return FALSE;
1366}
1367
1368/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001369 * Check if "win" is a pointer to an existing window in any tab page.
1370 */
1371 int
1372win_valid_any_tab(win_T *win)
1373{
1374 win_T *wp;
1375 tabpage_T *tp;
1376
1377 if (win == NULL)
1378 return FALSE;
1379 FOR_ALL_TABPAGES(tp)
1380 {
1381 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
1382 {
1383 if (wp == win)
1384 return TRUE;
1385 }
1386 }
1387 return FALSE;
1388}
1389
1390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 * Return the number of windows.
1392 */
1393 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001394win_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
1396 win_T *wp;
1397 int count = 0;
1398
Bram Moolenaar29323592016-07-24 22:04:11 +02001399 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 ++count;
1401 return count;
1402}
1403
1404/*
1405 * Make "count" windows on the screen.
1406 * Return actual number of windows on the screen.
1407 * Must be called when there is just one window, filling the whole screen
1408 * (excluding the command line).
1409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001411make_windows(
1412 int count,
1413 int vertical UNUSED) /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414{
1415 int maxcount;
1416 int todo;
1417
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 if (vertical)
1419 {
1420 /* Each windows needs at least 'winminwidth' lines and a separator
1421 * column. */
1422 maxcount = (curwin->w_width + curwin->w_vsep_width
1423 - (p_wiw - p_wmw)) / (p_wmw + 1);
1424 }
1425 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 {
1427 /* Each window needs at least 'winminheight' lines and a status line. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001428 maxcount = (VISIBLE_HEIGHT(curwin) + curwin->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1430 }
1431
1432 if (maxcount < 2)
1433 maxcount = 2;
1434 if (count > maxcount)
1435 count = maxcount;
1436
1437 /*
1438 * add status line now, otherwise first window will be too big
1439 */
1440 if (count > 1)
1441 last_status(TRUE);
1442
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 /*
1444 * Don't execute autocommands while creating the windows. Must do that
1445 * when putting the buffers in the windows.
1446 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001447 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
1449 /* todo is number of windows left to create */
1450 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 if (vertical)
1452 {
1453 if (win_split(curwin->w_width - (curwin->w_width - todo)
1454 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1455 break;
1456 }
1457 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 {
1459 if (win_split(curwin->w_height - (curwin->w_height - todo
1460 * STATUS_HEIGHT) / (todo + 1)
1461 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1462 break;
1463 }
1464
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001465 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
1467 /* return actual number of windows */
1468 return (count - todo);
1469}
1470
1471/*
1472 * Exchange current and next window
1473 */
1474 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001475win_exchange(long Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476{
1477 frame_T *frp;
1478 frame_T *frp2;
1479 win_T *wp;
1480 win_T *wp2;
1481 int temp;
1482
Bram Moolenaar459ca562016-11-10 18:16:33 +01001483 if (ONE_WINDOW) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
1485 beep_flush();
1486 return;
1487 }
1488
1489#ifdef FEAT_GUI
1490 need_mouse_correct = TRUE;
1491#endif
1492
1493 /*
1494 * find window to exchange with
1495 */
1496 if (Prenum)
1497 {
1498 frp = curwin->w_frame->fr_parent->fr_child;
1499 while (frp != NULL && --Prenum > 0)
1500 frp = frp->fr_next;
1501 }
1502 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1503 frp = curwin->w_frame->fr_next;
1504 else /* Swap last window in row/col with previous */
1505 frp = curwin->w_frame->fr_prev;
1506
1507 /* We can only exchange a window with another window, not with a frame
1508 * containing windows. */
1509 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1510 return;
1511 wp = frp->fr_win;
1512
1513/*
1514 * 1. remove curwin from the list. Remember after which window it was in wp2
1515 * 2. insert curwin before wp in the list
1516 * if wp != wp2
1517 * 3. remove wp from the list
1518 * 4. insert wp after wp2
1519 * 5. exchange the status line height and vsep width.
1520 */
1521 wp2 = curwin->w_prev;
1522 frp2 = curwin->w_frame->fr_prev;
1523 if (wp->w_prev != curwin)
1524 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001525 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 frame_remove(curwin->w_frame);
1527 win_append(wp->w_prev, curwin);
1528 frame_insert(frp, curwin->w_frame);
1529 }
1530 if (wp != wp2)
1531 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001532 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 frame_remove(wp->w_frame);
1534 win_append(wp2, wp);
1535 if (frp2 == NULL)
1536 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1537 else
1538 frame_append(frp2, wp->w_frame);
1539 }
1540 temp = curwin->w_status_height;
1541 curwin->w_status_height = wp->w_status_height;
1542 wp->w_status_height = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 temp = curwin->w_vsep_width;
1544 curwin->w_vsep_width = wp->w_vsep_width;
1545 wp->w_vsep_width = temp;
1546
1547 /* If the windows are not in the same frame, exchange the sizes to avoid
1548 * messing up the window layout. Otherwise fix the frame sizes. */
1549 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1550 {
1551 temp = curwin->w_height;
1552 curwin->w_height = wp->w_height;
1553 wp->w_height = temp;
1554 temp = curwin->w_width;
1555 curwin->w_width = wp->w_width;
1556 wp->w_width = temp;
1557 }
1558 else
1559 {
1560 frame_fix_height(curwin);
1561 frame_fix_height(wp);
1562 frame_fix_width(curwin);
1563 frame_fix_width(wp);
1564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565
1566 (void)win_comp_pos(); /* recompute window positions */
1567
1568 win_enter(wp, TRUE);
1569 redraw_later(CLEAR);
1570}
1571
1572/*
1573 * rotate windows: if upwards TRUE the second window becomes the first one
1574 * if upwards FALSE the first window becomes the second one
1575 */
1576 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001577win_rotate(int upwards, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
1579 win_T *wp1;
1580 win_T *wp2;
1581 frame_T *frp;
1582 int n;
1583
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001584 if (ONE_WINDOW) /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 {
1586 beep_flush();
1587 return;
1588 }
1589
1590#ifdef FEAT_GUI
1591 need_mouse_correct = TRUE;
1592#endif
1593
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 /* Check if all frames in this row/col have one window. */
1595 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1596 frp = frp->fr_next)
1597 if (frp->fr_win == NULL)
1598 {
1599 EMSG(_("E443: Cannot rotate when another window is split"));
1600 return;
1601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602
1603 while (count--)
1604 {
1605 if (upwards) /* first window becomes last window */
1606 {
1607 /* remove first window/frame from the list */
1608 frp = curwin->w_frame->fr_parent->fr_child;
1609 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001610 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 frame_remove(frp);
1612
1613 /* find last frame and append removed window/frame after it */
1614 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1615 ;
1616 win_append(frp->fr_win, wp1);
1617 frame_append(frp, wp1->w_frame);
1618
1619 wp2 = frp->fr_win; /* previously last window */
1620 }
1621 else /* last window becomes first window */
1622 {
1623 /* find last window/frame in the list and remove it */
1624 for (frp = curwin->w_frame; frp->fr_next != NULL;
1625 frp = frp->fr_next)
1626 ;
1627 wp1 = frp->fr_win;
1628 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001629 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 frame_remove(frp);
1631
1632 /* append the removed window/frame before the first in the list */
1633 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1634 frame_insert(frp->fr_parent->fr_child, frp);
1635 }
1636
1637 /* exchange status height and vsep width of old and new last window */
1638 n = wp2->w_status_height;
1639 wp2->w_status_height = wp1->w_status_height;
1640 wp1->w_status_height = n;
1641 frame_fix_height(wp1);
1642 frame_fix_height(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 n = wp2->w_vsep_width;
1644 wp2->w_vsep_width = wp1->w_vsep_width;
1645 wp1->w_vsep_width = n;
1646 frame_fix_width(wp1);
1647 frame_fix_width(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001649 /* recompute w_winrow and w_wincol for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 (void)win_comp_pos();
1651 }
1652
1653 redraw_later(CLEAR);
1654}
1655
1656/*
1657 * Move the current window to the very top/bottom/left/right of the screen.
1658 */
1659 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001660win_totop(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661{
1662 int dir;
1663 int height = curwin->w_height;
1664
Bram Moolenaar459ca562016-11-10 18:16:33 +01001665 if (ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 {
1667 beep_flush();
1668 return;
1669 }
1670
1671 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001672 (void)winframe_remove(curwin, &dir, NULL);
1673 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 last_status(FALSE); /* may need to remove last status line */
1675 (void)win_comp_pos(); /* recompute window positions */
1676
1677 /* Split a window on the desired side and put the window there. */
1678 (void)win_split_ins(size, flags, curwin, dir);
1679 if (!(flags & WSP_VERT))
1680 {
1681 win_setheight(height);
1682 if (p_ea)
1683 win_equal(curwin, TRUE, 'v');
1684 }
1685
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001686#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1688 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001689 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691}
1692
1693/*
1694 * Move window "win1" to below/right of "win2" and make "win1" the current
1695 * window. Only works within the same frame!
1696 */
1697 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001698win_move_after(win_T *win1, win_T *win2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
1700 int height;
1701
1702 /* check if the arguments are reasonable */
1703 if (win1 == win2)
1704 return;
1705
1706 /* check if there is something to do */
1707 if (win2->w_next != win1)
1708 {
1709 /* may need move the status line/vertical separator of the last window
1710 * */
1711 if (win1 == lastwin)
1712 {
1713 height = win1->w_prev->w_status_height;
1714 win1->w_prev->w_status_height = win1->w_status_height;
1715 win1->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001716 if (win1->w_prev->w_vsep_width == 1)
1717 {
1718 /* Remove the vertical separator from the last-but-one window,
1719 * add it to the last window. Adjust the frame widths. */
1720 win1->w_prev->w_vsep_width = 0;
1721 win1->w_prev->w_frame->fr_width -= 1;
1722 win1->w_vsep_width = 1;
1723 win1->w_frame->fr_width += 1;
1724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 }
1726 else if (win2 == lastwin)
1727 {
1728 height = win1->w_status_height;
1729 win1->w_status_height = win2->w_status_height;
1730 win2->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001731 if (win1->w_vsep_width == 1)
1732 {
1733 /* Remove the vertical separator from win1, add it to the last
1734 * window, win2. Adjust the frame widths. */
1735 win2->w_vsep_width = 1;
1736 win2->w_frame->fr_width += 1;
1737 win1->w_vsep_width = 0;
1738 win1->w_frame->fr_width -= 1;
1739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001741 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 frame_remove(win1->w_frame);
1743 win_append(win2, win1);
1744 frame_append(win2->w_frame, win1->w_frame);
1745
1746 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1747 redraw_later(NOT_VALID);
1748 }
1749 win_enter(win1, FALSE);
1750}
1751
1752/*
1753 * Make all windows the same height.
1754 * 'next_curwin' will soon be the current window, make sure it has enough
1755 * rows.
1756 */
1757 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001758win_equal(
1759 win_T *next_curwin, /* pointer to current window to be or NULL */
1760 int current, /* do only frame with current window */
1761 int dir) /* 'v' for vertically, 'h' for horizontally,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 'b' for both, 0 for using p_ead */
1763{
1764 if (dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 dir = *p_ead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001767 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001768 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769}
1770
1771/*
1772 * Set a frame to a new position and height, spreading the available room
1773 * equally over contained frames.
1774 * The window "next_curwin" (if not NULL) should at least get the size from
1775 * 'winheight' and 'winwidth' if possible.
1776 */
1777 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001778win_equal_rec(
1779 win_T *next_curwin, /* pointer to current window to be or NULL */
1780 int current, /* do only frame with current window */
1781 frame_T *topfr, /* frame to set size off */
1782 int dir, /* 'v', 'h' or 'b', see win_equal() */
1783 int col, /* horizontal position for frame */
1784 int row, /* vertical position for frame */
1785 int width, /* new width of frame */
1786 int height) /* new height of frame */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
1788 int n, m;
1789 int extra_sep = 0;
1790 int wincount, totwincount = 0;
1791 frame_T *fr;
1792 int next_curwin_size = 0;
1793 int room = 0;
1794 int new_size;
1795 int has_next_curwin = 0;
1796 int hnc;
1797
1798 if (topfr->fr_layout == FR_LEAF)
1799 {
1800 /* Set the width/height of this frame.
1801 * Redraw when size or position changes */
1802 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 )
1805 {
1806 topfr->fr_win->w_winrow = row;
1807 frame_new_height(topfr, height, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001809 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 redraw_all_later(CLEAR);
1811 }
1812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 else if (topfr->fr_layout == FR_ROW)
1814 {
1815 topfr->fr_width = width;
1816 topfr->fr_height = height;
1817
1818 if (dir != 'v') /* equalize frame widths */
1819 {
1820 /* Compute the maximum number of windows horizontally in this
1821 * frame. */
1822 n = frame_minwidth(topfr, NOWIN);
1823 /* add one for the rightmost window, it doesn't have a separator */
1824 if (col + width == Columns)
1825 extra_sep = 1;
1826 else
1827 extra_sep = 0;
1828 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001829 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001831 /*
1832 * Compute width for "next_curwin" window and room available for
1833 * other windows.
1834 * "m" is the minimal width when counting p_wiw for "next_curwin".
1835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 m = frame_minwidth(topfr, next_curwin);
1837 room = width - m;
1838 if (room < 0)
1839 {
1840 next_curwin_size = p_wiw + room;
1841 room = 0;
1842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 else
1844 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001845 next_curwin_size = -1;
1846 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1847 {
1848 /* If 'winfixwidth' set keep the window width if
1849 * possible.
1850 * Watch out for this window being the next_curwin. */
1851 if (frame_fixed_width(fr))
1852 {
1853 n = frame_minwidth(fr, NOWIN);
1854 new_size = fr->fr_width;
1855 if (frame_has_win(fr, next_curwin))
1856 {
1857 room += p_wiw - p_wmw;
1858 next_curwin_size = 0;
1859 if (new_size < p_wiw)
1860 new_size = p_wiw;
1861 }
1862 else
1863 /* These windows don't use up room. */
1864 totwincount -= (n + (fr->fr_next == NULL
1865 ? extra_sep : 0)) / (p_wmw + 1);
1866 room -= new_size - n;
1867 if (room < 0)
1868 {
1869 new_size += room;
1870 room = 0;
1871 }
1872 fr->fr_newwidth = new_size;
1873 }
1874 }
1875 if (next_curwin_size == -1)
1876 {
1877 if (!has_next_curwin)
1878 next_curwin_size = 0;
1879 else if (totwincount > 1
1880 && (room + (totwincount - 2))
1881 / (totwincount - 1) > p_wiw)
1882 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001883 /* Can make all windows wider than 'winwidth', spread
1884 * the room equally. */
1885 next_curwin_size = (room + p_wiw
1886 + (totwincount - 1) * p_wmw
1887 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001888 room -= next_curwin_size - p_wiw;
1889 }
1890 else
1891 next_curwin_size = p_wiw;
1892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001894
1895 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 --totwincount; /* don't count curwin */
1897 }
1898
1899 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1900 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 wincount = 1;
1902 if (fr->fr_next == NULL)
1903 /* last frame gets all that remains (avoid roundoff error) */
1904 new_size = width;
1905 else if (dir == 'v')
1906 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001907 else if (frame_fixed_width(fr))
1908 {
1909 new_size = fr->fr_newwidth;
1910 wincount = 0; /* doesn't count as a sizeable window */
1911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 else
1913 {
1914 /* Compute the maximum number of windows horiz. in "fr". */
1915 n = frame_minwidth(fr, NOWIN);
1916 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1917 / (p_wmw + 1);
1918 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001919 if (has_next_curwin)
1920 hnc = frame_has_win(fr, next_curwin);
1921 else
1922 hnc = FALSE;
1923 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001925 if (totwincount == 0)
1926 new_size = room;
1927 else
1928 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001930 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 {
1932 next_curwin_size -= p_wiw - (m - n);
1933 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001934 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001936 else
1937 room -= new_size;
1938 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
1940
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001941 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 * window, unless equalizing all frames. */
1943 if (!current || dir != 'v' || topfr->fr_parent != NULL
1944 || (new_size != fr->fr_width)
1945 || frame_has_win(fr, next_curwin))
1946 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001947 new_size, height);
1948 col += new_size;
1949 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 totwincount -= wincount;
1951 }
1952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 else /* topfr->fr_layout == FR_COL */
1954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 topfr->fr_width = width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 topfr->fr_height = height;
1957
1958 if (dir != 'h') /* equalize frame heights */
1959 {
1960 /* Compute maximum number of windows vertically in this frame. */
1961 n = frame_minheight(topfr, NOWIN);
1962 /* add one for the bottom window if it doesn't have a statusline */
1963 if (row + height == cmdline_row && p_ls == 0)
1964 extra_sep = 1;
1965 else
1966 extra_sep = 0;
1967 totwincount = (n + extra_sep) / (p_wmh + 1);
1968 has_next_curwin = frame_has_win(topfr, next_curwin);
1969
1970 /*
1971 * Compute height for "next_curwin" window and room available for
1972 * other windows.
1973 * "m" is the minimal height when counting p_wh for "next_curwin".
1974 */
1975 m = frame_minheight(topfr, next_curwin);
1976 room = height - m;
1977 if (room < 0)
1978 {
1979 /* The room is less then 'winheight', use all space for the
1980 * current window. */
1981 next_curwin_size = p_wh + room;
1982 room = 0;
1983 }
1984 else
1985 {
1986 next_curwin_size = -1;
1987 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1988 {
1989 /* If 'winfixheight' set keep the window height if
1990 * possible.
1991 * Watch out for this window being the next_curwin. */
1992 if (frame_fixed_height(fr))
1993 {
1994 n = frame_minheight(fr, NOWIN);
1995 new_size = fr->fr_height;
1996 if (frame_has_win(fr, next_curwin))
1997 {
1998 room += p_wh - p_wmh;
1999 next_curwin_size = 0;
2000 if (new_size < p_wh)
2001 new_size = p_wh;
2002 }
2003 else
2004 /* These windows don't use up room. */
2005 totwincount -= (n + (fr->fr_next == NULL
2006 ? extra_sep : 0)) / (p_wmh + 1);
2007 room -= new_size - n;
2008 if (room < 0)
2009 {
2010 new_size += room;
2011 room = 0;
2012 }
2013 fr->fr_newheight = new_size;
2014 }
2015 }
2016 if (next_curwin_size == -1)
2017 {
2018 if (!has_next_curwin)
2019 next_curwin_size = 0;
2020 else if (totwincount > 1
2021 && (room + (totwincount - 2))
2022 / (totwincount - 1) > p_wh)
2023 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002024 /* can make all windows higher than 'winheight',
2025 * spread the room equally. */
2026 next_curwin_size = (room + p_wh
2027 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 + (totwincount - 1)) / totwincount;
2029 room -= next_curwin_size - p_wh;
2030 }
2031 else
2032 next_curwin_size = p_wh;
2033 }
2034 }
2035
2036 if (has_next_curwin)
2037 --totwincount; /* don't count curwin */
2038 }
2039
2040 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2041 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 wincount = 1;
2043 if (fr->fr_next == NULL)
2044 /* last frame gets all that remains (avoid roundoff error) */
2045 new_size = height;
2046 else if (dir == 'h')
2047 new_size = fr->fr_height;
2048 else if (frame_fixed_height(fr))
2049 {
2050 new_size = fr->fr_newheight;
2051 wincount = 0; /* doesn't count as a sizeable window */
2052 }
2053 else
2054 {
2055 /* Compute the maximum number of windows vert. in "fr". */
2056 n = frame_minheight(fr, NOWIN);
2057 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2058 / (p_wmh + 1);
2059 m = frame_minheight(fr, next_curwin);
2060 if (has_next_curwin)
2061 hnc = frame_has_win(fr, next_curwin);
2062 else
2063 hnc = FALSE;
2064 if (hnc) /* don't count next_curwin */
2065 --wincount;
2066 if (totwincount == 0)
2067 new_size = room;
2068 else
2069 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2070 / totwincount;
2071 if (hnc) /* add next_curwin size */
2072 {
2073 next_curwin_size -= p_wh - (m - n);
2074 new_size += next_curwin_size;
2075 room -= new_size - next_curwin_size;
2076 }
2077 else
2078 room -= new_size;
2079 new_size += n;
2080 }
2081 /* Skip frame that is full width when splitting or closing a
2082 * window, unless equalizing all frames. */
2083 if (!current || dir != 'h' || topfr->fr_parent != NULL
2084 || (new_size != fr->fr_height)
2085 || frame_has_win(fr, next_curwin))
2086 win_equal_rec(next_curwin, current, fr, dir, col, row,
2087 width, new_size);
2088 row += new_size;
2089 height -= new_size;
2090 totwincount -= wincount;
2091 }
2092 }
2093}
2094
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002095#ifdef FEAT_JOB_CHANNEL
2096 static void
2097leaving_window(win_T *win)
2098{
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002099 // Only matters for a prompt window.
2100 if (!bt_prompt(win->w_buffer))
2101 return;
2102
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002103 // When leaving a prompt window stop Insert mode and perhaps restart
2104 // it when entering that window again.
2105 win->w_buffer->b_prompt_insert = restart_edit;
Bram Moolenaar942b4542018-06-17 16:23:34 +02002106 if (restart_edit != 0 && mode_displayed)
2107 clear_cmdline = TRUE; /* unshow mode later */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002108 restart_edit = NUL;
2109
2110 // When leaving the window (or closing the window) was done from a
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002111 // callback we need to break out of the Insert mode loop and restart Insert
2112 // mode when entering the window again.
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002113 if (State & INSERT)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002114 {
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002115 stop_insert_mode = TRUE;
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002116 if (win->w_buffer->b_prompt_insert == NUL)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002117 win->w_buffer->b_prompt_insert = 'A';
2118 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002119}
2120
2121 static void
2122entering_window(win_T *win)
2123{
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002124 // Only matters for a prompt window.
2125 if (!bt_prompt(win->w_buffer))
2126 return;
2127
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002128 // When switching to a prompt buffer that was in Insert mode, don't stop
2129 // Insert mode, it may have been set in leaving_window().
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002130 if (win->w_buffer->b_prompt_insert != NUL)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002131 stop_insert_mode = FALSE;
2132
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002133 // When entering the prompt window restart Insert mode if we were in Insert
2134 // mode when we left it.
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002135 restart_edit = win->w_buffer->b_prompt_insert;
2136}
2137#endif
2138
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002140 * Close all windows for buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 */
2142 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002143close_windows(
2144 buf_T *buf,
2145 int keep_curwin) /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002147 win_T *wp;
2148 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002149 int h = tabline_height();
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002150 int count = tabpage_index(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
2152 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002153
Bram Moolenaar459ca562016-11-10 18:16:33 +01002154 for (wp = firstwin; wp != NULL && !ONE_WINDOW; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002156 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002157 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002159 if (win_close(wp, FALSE) == FAIL)
2160 /* If closing the window fails give up, to avoid looping
2161 * forever. */
2162 break;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002163
2164 /* Start all over, autocommands may change the window layout. */
2165 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 }
2167 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002168 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002170
2171 /* Also check windows in other tab pages. */
2172 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2173 {
2174 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002175 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002176 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002177 if (wp->w_buffer == buf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002178 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaarf740b292006-02-16 22:11:02 +00002179 {
2180 win_close_othertab(wp, FALSE, tp);
2181
2182 /* Start all over, the tab page may be closed and
2183 * autocommands may change the window layout. */
2184 nexttp = first_tabpage;
2185 break;
2186 }
2187 }
2188
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002190
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002191 if (count != tabpage_index(NULL))
2192 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002193
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002194 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002195 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002196 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197}
2198
2199/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002200 * Return TRUE if the current window is the only window that exists (ignoring
2201 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002202 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002203 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002204 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002205last_window(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002206{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002207 return (one_window() && first_tabpage->tp_next == NULL);
2208}
2209
2210/*
2211 * Return TRUE if there is only one window other than "aucmd_win" in the
2212 * current tab page.
2213 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002214 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002215one_window(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002216{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002217 win_T *wp;
2218 int seen_one = FALSE;
2219
2220 FOR_ALL_WINDOWS(wp)
2221 {
2222 if (wp != aucmd_win)
2223 {
2224 if (seen_one)
2225 return FALSE;
2226 seen_one = TRUE;
2227 }
2228 }
2229 return TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002230}
2231
2232/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002233 * Close the possibly last window in a tab page.
2234 * Returns TRUE when the window was closed already.
2235 */
2236 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002237close_last_window_tabpage(
2238 win_T *win,
2239 int free_buf,
2240 tabpage_T *prev_curtab)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002241{
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002242 if (ONE_WINDOW)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002243 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002244 buf_T *old_curbuf = curbuf;
2245
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002246 /*
2247 * Closing the last window in a tab page. First go to another tab
2248 * page and then close the window and the tab page. This avoids that
2249 * curwin and curtab are invalid while we are freeing memory, they may
2250 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002251 * Don't trigger autocommands yet, they may use wrong values, so do
2252 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002253 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002254 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002255 redraw_tabline = TRUE;
2256
2257 /* Safety check: Autocommands may have closed the window when jumping
2258 * to the other tab page. */
2259 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2260 {
2261 int h = tabline_height();
2262
2263 win_close_othertab(win, free_buf, prev_curtab);
2264 if (h != tabline_height())
2265 shell_new_rows();
2266 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002267#ifdef FEAT_JOB_CHANNEL
2268 entering_window(curwin);
2269#endif
Bram Moolenaara8596c42012-06-13 14:28:20 +02002270 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2271 * that now. */
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002272 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002273 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002274 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2275 if (old_curbuf != curbuf)
2276 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002277 return TRUE;
2278 }
2279 return FALSE;
2280}
2281
2282/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002283 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 * If "free_buf" is TRUE related buffer may be unloaded.
2285 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002286 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002287 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002289 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002290win_close(win_T *win, int free_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291{
2292 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 int dir;
2296 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002297 tabpage_T *prev_curtab = curtab;
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002298 frame_T *win_frame = win->w_frame->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002300 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
2302 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002303 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 }
2305
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002306 if (win->w_closing || (win->w_buffer != NULL
2307 && win->w_buffer->b_locked > 0))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002308 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002309 if (win == aucmd_win)
2310 {
2311 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002312 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002313 }
2314 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2315 {
2316 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002317 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002318 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002319
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002320 /* When closing the last window in a tab page first go to another tab page
2321 * and then close the window and the tab page to avoid that curwin and
2322 * curtab are invalid while we are freeing memory. */
2323 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002324 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002325
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 /* When closing the help window, try restoring a snapshot after closing
2327 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002328 if (bt_help(win->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 help_window = TRUE;
2330 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002331 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 if (win == curwin)
2334 {
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002335#ifdef FEAT_JOB_CHANNEL
2336 leaving_window(curwin);
2337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 /*
2339 * Guess which window is going to be the new current window.
2340 * This may change because of the autocommands (sigh).
2341 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002342 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
2344 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002345 * Be careful: If autocommands delete the window or cause this window
2346 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 */
2348 if (wp->w_buffer != curbuf)
2349 {
2350 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002351 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002353 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002354 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002355 win->w_closing = FALSE;
2356 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002357 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002359 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002361 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002362 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002363 win->w_closing = FALSE;
2364 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002365 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002366#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 /* autocmds may abort script processing */
2368 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002369 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002373#ifdef FEAT_GUI
2374 /* Avoid trouble with scrollbars that are going to be deleted in
2375 * win_free(). */
2376 if (gui.in_use)
2377 out_flush();
2378#endif
2379
Bram Moolenaara971b822011-09-14 14:43:25 +02002380#ifdef FEAT_SYN_HL
2381 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002382 if (win->w_buffer != NULL)
2383 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002384#endif
2385
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 /*
2387 * Close the link to the buffer.
2388 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002389 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002390 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002391 bufref_T bufref;
2392
2393 set_bufref(&bufref, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002394 win->w_closing = TRUE;
Bram Moolenaar8f913992012-08-29 15:50:26 +02002395 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002396 if (win_valid_any_tab(win))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002397 win->w_closing = FALSE;
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002398 /* Make sure curbuf is valid. It can become invalid if 'bufhidden' is
2399 * "wipe". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002400 if (!bufref_valid(&bufref))
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002401 curbuf = firstbuf;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002402 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002403
Bram Moolenaar802418d2013-01-17 14:00:11 +01002404 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2405 && (last_window() || curtab != prev_curtab
2406 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002407 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002408 /* Autocommands have closed all windows, quit now. Restore
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002409 * curwin->w_buffer, otherwise writing viminfo may fail. */
2410 if (curwin->w_buffer == NULL)
2411 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002412 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002413 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002414
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002415 /* Autocommands may have moved to another tab page. */
2416 if (curtab != prev_curtab && win_valid_any_tab(win)
2417 && win->w_buffer == NULL)
2418 {
2419 /* Need to close the window anyway, since the buffer is NULL. */
2420 win_close_othertab(win, FALSE, prev_curtab);
2421 return FAIL;
2422 }
2423
2424 /* Autocommands may have closed the window already or closed the only
2425 * other window. */
2426 if (!win_valid(win) || last_window()
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002427 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002428 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
Bram Moolenaara971b822011-09-14 14:43:25 +02002430 /* Free the memory used for the window and get the window that received
2431 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002432 wp = win_free_mem(win, &dir, NULL);
2433
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 /* Make sure curwin isn't invalid. It can cause severe trouble when
2435 * printing an error message. For win_equal() curbuf needs to be valid
2436 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002437 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 {
2439 curwin = wp;
2440#ifdef FEAT_QUICKFIX
2441 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2442 {
2443 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002444 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 * finding another window to go to.
2446 */
2447 for (;;)
2448 {
2449 if (wp->w_next == NULL)
2450 wp = firstwin;
2451 else
2452 wp = wp->w_next;
2453 if (wp == curwin)
2454 break;
2455 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2456 {
2457 curwin = wp;
2458 break;
2459 }
2460 }
2461 }
2462#endif
2463 curbuf = curwin->w_buffer;
2464 close_curwin = TRUE;
Bram Moolenaarf79225e2017-03-18 23:11:04 +01002465
2466 /* The cursor position may be invalid if the buffer changed after last
2467 * using the window. */
2468 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002470 if (p_ea && (*p_ead == 'b' || *p_ead == dir))
Bram Moolenaar8eeeba82017-06-25 22:45:39 +02002471 /* If the frame of the closed window contains the new current window,
2472 * only resize that frame. Otherwise resize all windows. */
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002473 win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 else
2475 win_comp_pos();
2476 if (close_curwin)
2477 {
Bram Moolenaarc917da42016-07-19 22:31:36 +02002478 win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 if (other_buffer)
2480 /* careful: after this wp and win may be invalid! */
2481 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 }
2483
2484 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002485 * If last window has a status line now and we don't want one,
2486 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 */
2488 last_status(FALSE);
2489
2490 /* After closing the help window, try restoring the window layout from
2491 * before it was opened. */
2492 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002493 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002495#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2497 if (gui.in_use && !win_hasvertsplit())
2498 gui_init_which_components(NULL);
2499#endif
2500
2501 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002502 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503}
2504
2505/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002506 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002507 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002508 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002509 * Caller must check if buffer is hidden and whether the tabline needs to be
2510 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002511 */
2512 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002513win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002514{
2515 win_T *wp;
2516 int dir;
2517 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002518 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002519
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002520 /* Get here with win->w_buffer == NULL when win_close() detects the tab
2521 * page changed. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002522 if (win->w_closing || (win->w_buffer != NULL
2523 && win->w_buffer->b_locked > 0))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002524 return; /* window is already being closed */
Bram Moolenaar362ce482012-06-06 19:02:45 +02002525
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002526 if (win->w_buffer != NULL)
2527 /* Close the link to the buffer. */
2528 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002529
2530 /* Careful: Autocommands may have closed the tab page or made it the
2531 * current tab page. */
2532 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2533 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002534 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002535 return;
2536
2537 /* Autocommands may have closed the window already. */
2538 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2539 ;
2540 if (wp == NULL)
2541 return;
2542
Bram Moolenaarf740b292006-02-16 22:11:02 +00002543 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002544 if (tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002545 {
2546 if (tp == first_tabpage)
2547 first_tabpage = tp->tp_next;
2548 else
2549 {
2550 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2551 ptp = ptp->tp_next)
2552 ;
2553 if (ptp == NULL)
2554 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002555 internal_error("win_close_othertab()");
Bram Moolenaarf740b292006-02-16 22:11:02 +00002556 return;
2557 }
2558 ptp->tp_next = tp->tp_next;
2559 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002560 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002561 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002562
2563 /* Free the memory used for the window. */
2564 win_free_mem(win, &dir, tp);
2565
2566 if (free_tp)
2567 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002568}
2569
2570/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002571 * Free the memory used for a window.
2572 * Returns a pointer to the window that got the freed up space.
2573 */
2574 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002575win_free_mem(
2576 win_T *win,
2577 int *dirp, /* set to 'v' or 'h' for direction if 'ea' */
2578 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002579{
2580 frame_T *frp;
2581 win_T *wp;
2582
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002583 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002584 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002585 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002586 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002587 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002588
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002589 /* When deleting the current window of another tab page select a new
2590 * current window. */
2591 if (tp != NULL && win == tp->tp_curwin)
2592 tp->tp_curwin = wp;
2593
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002594 return wp;
2595}
2596
2597#if defined(EXITFREE) || defined(PROTO)
2598 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002599win_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002600{
2601 int dummy;
2602
Bram Moolenaarf740b292006-02-16 22:11:02 +00002603 while (first_tabpage->tp_next != NULL)
2604 tabpage_close(TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002605
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002606 if (aucmd_win != NULL)
2607 {
2608 (void)win_free_mem(aucmd_win, &dummy, NULL);
2609 aucmd_win = NULL;
2610 }
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002611
2612 while (firstwin != NULL)
2613 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002614
2615 /* No window should be used after this. Set curwin to NULL to crash
2616 * instead of using freed memory. */
2617 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002618}
2619#endif
2620
2621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 * Remove a window and its frame from the tree of frames.
2623 * Returns a pointer to the window that got the freed up space.
2624 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002625 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002626winframe_remove(
2627 win_T *win,
2628 int *dirp UNUSED, /* set to 'v' or 'h' for direction if 'ea' */
2629 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630{
2631 frame_T *frp, *frp2, *frp3;
2632 frame_T *frp_close = win->w_frame;
2633 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
2635 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002636 * If there is only one window there is nothing to remove.
2637 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002638 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002639 return NULL;
2640
2641 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 * Remove the window from its frame.
2643 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002644 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 wp = frame2win(frp2);
2646
2647 /* Remove this frame from the list of frames. */
2648 frame_remove(frp_close);
2649
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if (frp_close->fr_parent->fr_layout == FR_COL)
2651 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002652 /* When 'winfixheight' is set, try to find another frame in the column
2653 * (as close to the closed frame as possible) to distribute the height
2654 * to. */
2655 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2656 {
2657 frp = frp_close->fr_prev;
2658 frp3 = frp_close->fr_next;
2659 while (frp != NULL || frp3 != NULL)
2660 {
2661 if (frp != NULL)
2662 {
2663 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2664 {
2665 frp2 = frp;
2666 wp = frp->fr_win;
2667 break;
2668 }
2669 frp = frp->fr_prev;
2670 }
2671 if (frp3 != NULL)
2672 {
2673 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2674 {
2675 frp2 = frp3;
2676 wp = frp3->fr_win;
2677 break;
2678 }
2679 frp3 = frp3->fr_next;
2680 }
2681 }
2682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2684 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 *dirp = 'v';
2686 }
2687 else
2688 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002689 /* When 'winfixwidth' is set, try to find another frame in the column
2690 * (as close to the closed frame as possible) to distribute the width
2691 * to. */
2692 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2693 {
2694 frp = frp_close->fr_prev;
2695 frp3 = frp_close->fr_next;
2696 while (frp != NULL || frp3 != NULL)
2697 {
2698 if (frp != NULL)
2699 {
2700 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2701 {
2702 frp2 = frp;
2703 wp = frp->fr_win;
2704 break;
2705 }
2706 frp = frp->fr_prev;
2707 }
2708 if (frp3 != NULL)
2709 {
2710 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2711 {
2712 frp2 = frp3;
2713 wp = frp3->fr_win;
2714 break;
2715 }
2716 frp3 = frp3->fr_next;
2717 }
2718 }
2719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002721 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 *dirp = 'h';
2723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
2725 /* If rows/columns go to a window below/right its positions need to be
2726 * updated. Can only be done after the sizes have been updated. */
2727 if (frp2 == frp_close->fr_next)
2728 {
2729 int row = win->w_winrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02002730 int col = win->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 frame_comp_pos(frp2, &row, &col);
2733 }
2734
2735 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2736 {
2737 /* There is no other frame in this list, move its info to the parent
2738 * and remove it. */
2739 frp2->fr_parent->fr_layout = frp2->fr_layout;
2740 frp2->fr_parent->fr_child = frp2->fr_child;
2741 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2742 frp->fr_parent = frp2->fr_parent;
2743 frp2->fr_parent->fr_win = frp2->fr_win;
2744 if (frp2->fr_win != NULL)
2745 frp2->fr_win->w_frame = frp2->fr_parent;
2746 frp = frp2->fr_parent;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002747 if (topframe->fr_child == frp2)
2748 topframe->fr_child = frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 vim_free(frp2);
2750
2751 frp2 = frp->fr_parent;
2752 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2753 {
2754 /* The frame above the parent has the same layout, have to merge
2755 * the frames into this list. */
2756 if (frp2->fr_child == frp)
2757 frp2->fr_child = frp->fr_child;
2758 frp->fr_child->fr_prev = frp->fr_prev;
2759 if (frp->fr_prev != NULL)
2760 frp->fr_prev->fr_next = frp->fr_child;
2761 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2762 {
2763 frp3->fr_parent = frp2;
2764 if (frp3->fr_next == NULL)
2765 {
2766 frp3->fr_next = frp->fr_next;
2767 if (frp->fr_next != NULL)
2768 frp->fr_next->fr_prev = frp3;
2769 break;
2770 }
2771 }
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002772 if (topframe->fr_child == frp)
2773 topframe->fr_child = frp2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 vim_free(frp);
2775 }
2776 }
2777
2778 return wp;
2779}
2780
2781/*
Bram Moolenaarc136af22018-05-04 20:15:38 +02002782 * Return a pointer to the frame that will receive the empty screen space that
2783 * is left over after "win" is closed.
2784 *
2785 * If 'splitbelow' or 'splitright' is set, the space goes above or to the left
2786 * by default. Otherwise, the free space goes below or to the right. The
2787 * result is that opening a window and then immediately closing it will
2788 * preserve the initial window layout. The 'wfh' and 'wfw' settings are
2789 * respected when possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 */
2791 static frame_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002792win_altframe(
2793 win_T *win,
2794 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
2796 frame_T *frp;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002797 frame_T *other_fr, *target_fr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002799 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002800 return alt_tabpage()->tp_curwin->w_frame;
2801
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 frp = win->w_frame;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002803
2804 if (frp->fr_prev == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 return frp->fr_next;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002806 if (frp->fr_next == NULL)
2807 return frp->fr_prev;
2808
2809 target_fr = frp->fr_next;
2810 other_fr = frp->fr_prev;
2811 if (p_spr || p_sb)
2812 {
2813 target_fr = frp->fr_prev;
2814 other_fr = frp->fr_next;
2815 }
2816
2817 /* If 'wfh' or 'wfw' is set for the target and not for the alternate
2818 * window, reverse the selection. */
2819 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2820 {
2821 if (frame_fixed_width(target_fr) && !frame_fixed_width(other_fr))
2822 target_fr = other_fr;
2823 }
2824 else
2825 {
2826 if (frame_fixed_height(target_fr) && !frame_fixed_height(other_fr))
2827 target_fr = other_fr;
2828 }
2829
2830 return target_fr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831}
2832
2833/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002834 * Return the tabpage that will be used if the current one is closed.
2835 */
2836 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002837alt_tabpage(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002838{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002839 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002840
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002841 /* Use the next tab page if possible. */
2842 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002843 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002844
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002845 /* Find the last but one tab page. */
2846 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2847 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002848 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002849}
2850
2851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 * Find the left-upper window in frame "frp".
2853 */
2854 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002855frame2win(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856{
2857 while (frp->fr_win == NULL)
2858 frp = frp->fr_child;
2859 return frp->fr_win;
2860}
2861
2862/*
2863 * Return TRUE if frame "frp" contains window "wp".
2864 */
2865 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002866frame_has_win(frame_T *frp, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867{
2868 frame_T *p;
2869
2870 if (frp->fr_layout == FR_LEAF)
2871 return frp->fr_win == wp;
2872
2873 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2874 if (frame_has_win(p, wp))
2875 return TRUE;
2876 return FALSE;
2877}
2878
2879/*
2880 * Set a new height for a frame. Recursively sets the height for contained
2881 * frames and windows. Caller must take care of positions.
2882 */
2883 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002884frame_new_height(
2885 frame_T *topfrp,
2886 int height,
2887 int topfirst, /* resize topmost contained frame first */
2888 int wfh) /* obey 'winfixheight' when there is a choice;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 may cause the height not to be set */
2890{
2891 frame_T *frp;
2892 int extra_lines;
2893 int h;
2894
2895 if (topfrp->fr_win != NULL)
2896 {
2897 /* Simple case: just one window. */
2898 win_new_height(topfrp->fr_win,
Bram Moolenaard326ad62017-09-18 20:31:41 +02002899 height - topfrp->fr_win->w_status_height
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01002900 - WINBAR_HEIGHT(topfrp->fr_win));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 else if (topfrp->fr_layout == FR_ROW)
2903 {
2904 do
2905 {
2906 /* All frames in this row get the same new height. */
2907 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2908 {
2909 frame_new_height(frp, height, topfirst, wfh);
2910 if (frp->fr_height > height)
2911 {
2912 /* Could not fit the windows, make the whole row higher. */
2913 height = frp->fr_height;
2914 break;
2915 }
2916 }
2917 }
2918 while (frp != NULL);
2919 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002920 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
2922 /* Complicated case: Resize a column of frames. Resize the bottom
2923 * frame first, frames above that when needed. */
2924
2925 frp = topfrp->fr_child;
2926 if (wfh)
2927 /* Advance past frames with one window with 'wfh' set. */
2928 while (frame_fixed_height(frp))
2929 {
2930 frp = frp->fr_next;
2931 if (frp == NULL)
2932 return; /* no frame without 'wfh', give up */
2933 }
2934 if (!topfirst)
2935 {
2936 /* Find the bottom frame of this column */
2937 while (frp->fr_next != NULL)
2938 frp = frp->fr_next;
2939 if (wfh)
2940 /* Advance back for frames with one window with 'wfh' set. */
2941 while (frame_fixed_height(frp))
2942 frp = frp->fr_prev;
2943 }
2944
2945 extra_lines = height - topfrp->fr_height;
2946 if (extra_lines < 0)
2947 {
2948 /* reduce height of contained frames, bottom or top frame first */
2949 while (frp != NULL)
2950 {
2951 h = frame_minheight(frp, NULL);
2952 if (frp->fr_height + extra_lines < h)
2953 {
2954 extra_lines += frp->fr_height - h;
2955 frame_new_height(frp, h, topfirst, wfh);
2956 }
2957 else
2958 {
2959 frame_new_height(frp, frp->fr_height + extra_lines,
2960 topfirst, wfh);
2961 break;
2962 }
2963 if (topfirst)
2964 {
2965 do
2966 frp = frp->fr_next;
2967 while (wfh && frp != NULL && frame_fixed_height(frp));
2968 }
2969 else
2970 {
2971 do
2972 frp = frp->fr_prev;
2973 while (wfh && frp != NULL && frame_fixed_height(frp));
2974 }
2975 /* Increase "height" if we could not reduce enough frames. */
2976 if (frp == NULL)
2977 height -= extra_lines;
2978 }
2979 }
2980 else if (extra_lines > 0)
2981 {
2982 /* increase height of bottom or top frame */
2983 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2984 }
2985 }
2986 topfrp->fr_height = height;
2987}
2988
2989/*
2990 * Return TRUE if height of frame "frp" should not be changed because of
2991 * the 'winfixheight' option.
2992 */
2993 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002994frame_fixed_height(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995{
2996 /* frame with one window: fixed height if 'winfixheight' set. */
2997 if (frp->fr_win != NULL)
2998 return frp->fr_win->w_p_wfh;
2999
3000 if (frp->fr_layout == FR_ROW)
3001 {
3002 /* The frame is fixed height if one of the frames in the row is fixed
3003 * height. */
3004 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3005 if (frame_fixed_height(frp))
3006 return TRUE;
3007 return FALSE;
3008 }
3009
3010 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
3011 * frames in the row are fixed height. */
3012 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3013 if (!frame_fixed_height(frp))
3014 return FALSE;
3015 return TRUE;
3016}
3017
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003019 * Return TRUE if width of frame "frp" should not be changed because of
3020 * the 'winfixwidth' option.
3021 */
3022 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003023frame_fixed_width(frame_T *frp)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003024{
3025 /* frame with one window: fixed width if 'winfixwidth' set. */
3026 if (frp->fr_win != NULL)
3027 return frp->fr_win->w_p_wfw;
3028
3029 if (frp->fr_layout == FR_COL)
3030 {
3031 /* The frame is fixed width if one of the frames in the row is fixed
3032 * width. */
3033 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3034 if (frame_fixed_width(frp))
3035 return TRUE;
3036 return FALSE;
3037 }
3038
3039 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
3040 * frames in the row are fixed width. */
3041 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3042 if (!frame_fixed_width(frp))
3043 return FALSE;
3044 return TRUE;
3045}
3046
3047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 * Add a status line to windows at the bottom of "frp".
3049 * Note: Does not check if there is room!
3050 */
3051 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003052frame_add_statusline(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 win_T *wp;
3055
3056 if (frp->fr_layout == FR_LEAF)
3057 {
3058 wp = frp->fr_win;
3059 if (wp->w_status_height == 0)
3060 {
3061 if (wp->w_height > 0) /* don't make it negative */
3062 --wp->w_height;
3063 wp->w_status_height = STATUS_HEIGHT;
3064 }
3065 }
3066 else if (frp->fr_layout == FR_ROW)
3067 {
3068 /* Handle all the frames in the row. */
3069 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3070 frame_add_statusline(frp);
3071 }
3072 else /* frp->fr_layout == FR_COL */
3073 {
3074 /* Only need to handle the last frame in the column. */
3075 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3076 ;
3077 frame_add_statusline(frp);
3078 }
3079}
3080
3081/*
3082 * Set width of a frame. Handles recursively going through contained frames.
3083 * May remove separator line for windows at the right side (for win_close()).
3084 */
3085 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003086frame_new_width(
3087 frame_T *topfrp,
3088 int width,
3089 int leftfirst, /* resize leftmost contained frame first */
3090 int wfw) /* obey 'winfixwidth' when there is a choice;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003091 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
3093 frame_T *frp;
3094 int extra_cols;
3095 int w;
3096 win_T *wp;
3097
3098 if (topfrp->fr_layout == FR_LEAF)
3099 {
3100 /* Simple case: just one window. */
3101 wp = topfrp->fr_win;
3102 /* Find out if there are any windows right of this one. */
3103 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3104 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3105 break;
3106 if (frp->fr_parent == NULL)
3107 wp->w_vsep_width = 0;
3108 win_new_width(wp, width - wp->w_vsep_width);
3109 }
3110 else if (topfrp->fr_layout == FR_COL)
3111 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003112 do
3113 {
3114 /* All frames in this column get the same new width. */
3115 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3116 {
3117 frame_new_width(frp, width, leftfirst, wfw);
3118 if (frp->fr_width > width)
3119 {
3120 /* Could not fit the windows, make whole column wider. */
3121 width = frp->fr_width;
3122 break;
3123 }
3124 }
3125 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 }
3127 else /* fr_layout == FR_ROW */
3128 {
3129 /* Complicated case: Resize a row of frames. Resize the rightmost
3130 * frame first, frames left of it when needed. */
3131
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003133 if (wfw)
3134 /* Advance past frames with one window with 'wfw' set. */
3135 while (frame_fixed_width(frp))
3136 {
3137 frp = frp->fr_next;
3138 if (frp == NULL)
3139 return; /* no frame without 'wfw', give up */
3140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003142 {
3143 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 while (frp->fr_next != NULL)
3145 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003146 if (wfw)
3147 /* Advance back for frames with one window with 'wfw' set. */
3148 while (frame_fixed_width(frp))
3149 frp = frp->fr_prev;
3150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
3152 extra_cols = width - topfrp->fr_width;
3153 if (extra_cols < 0)
3154 {
3155 /* reduce frame width, rightmost frame first */
3156 while (frp != NULL)
3157 {
3158 w = frame_minwidth(frp, NULL);
3159 if (frp->fr_width + extra_cols < w)
3160 {
3161 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003162 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 }
3164 else
3165 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003166 frame_new_width(frp, frp->fr_width + extra_cols,
3167 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 break;
3169 }
3170 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003171 {
3172 do
3173 frp = frp->fr_next;
3174 while (wfw && frp != NULL && frame_fixed_width(frp));
3175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003177 {
3178 do
3179 frp = frp->fr_prev;
3180 while (wfw && frp != NULL && frame_fixed_width(frp));
3181 }
3182 /* Increase "width" if we could not reduce enough frames. */
3183 if (frp == NULL)
3184 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 }
3186 }
3187 else if (extra_cols > 0)
3188 {
3189 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003190 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 }
3192 }
3193 topfrp->fr_width = width;
3194}
3195
3196/*
3197 * Add the vertical separator to windows at the right side of "frp".
3198 * Note: Does not check if there is room!
3199 */
3200 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003201frame_add_vsep(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202{
3203 win_T *wp;
3204
3205 if (frp->fr_layout == FR_LEAF)
3206 {
3207 wp = frp->fr_win;
3208 if (wp->w_vsep_width == 0)
3209 {
3210 if (wp->w_width > 0) /* don't make it negative */
3211 --wp->w_width;
3212 wp->w_vsep_width = 1;
3213 }
3214 }
3215 else if (frp->fr_layout == FR_COL)
3216 {
3217 /* Handle all the frames in the column. */
3218 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3219 frame_add_vsep(frp);
3220 }
3221 else /* frp->fr_layout == FR_ROW */
3222 {
3223 /* Only need to handle the last frame in the row. */
3224 frp = frp->fr_child;
3225 while (frp->fr_next != NULL)
3226 frp = frp->fr_next;
3227 frame_add_vsep(frp);
3228 }
3229}
3230
3231/*
3232 * Set frame width from the window it contains.
3233 */
3234 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003235frame_fix_width(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236{
3237 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3238}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239
3240/*
3241 * Set frame height from the window it contains.
3242 */
3243 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003244frame_fix_height(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
Bram Moolenaar415a6932017-12-05 20:31:07 +01003246 wp->w_frame->fr_height = VISIBLE_HEIGHT(wp) + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247}
3248
3249/*
3250 * Compute the minimal height for frame "topfrp".
3251 * Uses the 'winminheight' option.
3252 * When "next_curwin" isn't NULL, use p_wh for this window.
3253 * When "next_curwin" is NOWIN, don't use at least one line for the current
3254 * window.
3255 */
3256 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003257frame_minheight(frame_T *topfrp, win_T *next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258{
3259 frame_T *frp;
3260 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262
3263 if (topfrp->fr_win != NULL)
3264 {
3265 if (topfrp->fr_win == next_curwin)
3266 m = p_wh + topfrp->fr_win->w_status_height;
3267 else
3268 {
3269 /* window: minimal height of the window plus status line */
3270 m = p_wmh + topfrp->fr_win->w_status_height;
Bram Moolenaar415a6932017-12-05 20:31:07 +01003271 if (topfrp->fr_win == curwin && next_curwin == NULL)
3272 {
3273 /* Current window is minimal one line high and WinBar is
3274 * visible. */
3275 if (p_wmh == 0)
3276 ++m;
3277 m += WINBAR_HEIGHT(curwin);
3278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
3280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 else if (topfrp->fr_layout == FR_ROW)
3282 {
3283 /* get the minimal height from each frame in this row */
3284 m = 0;
3285 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3286 {
3287 n = frame_minheight(frp, next_curwin);
3288 if (n > m)
3289 m = n;
3290 }
3291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 else
3293 {
3294 /* Add up the minimal heights for all frames in this column. */
3295 m = 0;
3296 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3297 m += frame_minheight(frp, next_curwin);
3298 }
3299
3300 return m;
3301}
3302
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303/*
3304 * Compute the minimal width for frame "topfrp".
3305 * When "next_curwin" isn't NULL, use p_wiw for this window.
3306 * When "next_curwin" is NOWIN, don't use at least one column for the current
3307 * window.
3308 */
3309 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003310frame_minwidth(
3311 frame_T *topfrp,
3312 win_T *next_curwin) /* use p_wh and p_wiw for next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313{
3314 frame_T *frp;
3315 int m, n;
3316
3317 if (topfrp->fr_win != NULL)
3318 {
3319 if (topfrp->fr_win == next_curwin)
3320 m = p_wiw + topfrp->fr_win->w_vsep_width;
3321 else
3322 {
3323 /* window: minimal width of the window plus separator column */
3324 m = p_wmw + topfrp->fr_win->w_vsep_width;
3325 /* Current window is minimal one column wide */
3326 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3327 ++m;
3328 }
3329 }
3330 else if (topfrp->fr_layout == FR_COL)
3331 {
3332 /* get the minimal width from each frame in this column */
3333 m = 0;
3334 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3335 {
3336 n = frame_minwidth(frp, next_curwin);
3337 if (n > m)
3338 m = n;
3339 }
3340 }
3341 else
3342 {
3343 /* Add up the minimal widths for all frames in this row. */
3344 m = 0;
3345 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3346 m += frame_minwidth(frp, next_curwin);
3347 }
3348
3349 return m;
3350}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351
3352
3353/*
3354 * Try to close all windows except current one.
3355 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3356 * used and the buffer was modified.
3357 *
3358 * Used by ":bdel" and ":only".
3359 */
3360 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003361close_others(
3362 int message,
3363 int forceit) /* always hide all other windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
3365 win_T *wp;
3366 win_T *nextwp;
3367 int r;
3368
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003369 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003371 if (message && !autocmd_busy)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003372 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 return;
3374 }
3375
3376 /* Be very careful here: autocommands may change the window layout. */
3377 for (wp = firstwin; win_valid(wp); wp = nextwp)
3378 {
3379 nextwp = wp->w_next;
3380 if (wp != curwin) /* don't close current window */
3381 {
3382
3383 /* Check if it's allowed to abandon this window */
3384 r = can_abandon(wp->w_buffer, forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 if (!win_valid(wp)) /* autocommands messed wp up */
3386 {
3387 nextwp = firstwin;
3388 continue;
3389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 if (!r)
3391 {
3392#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3393 if (message && (p_confirm || cmdmod.confirm) && p_write)
3394 {
3395 dialog_changed(wp->w_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 if (!win_valid(wp)) /* autocommands messed wp up */
3397 {
3398 nextwp = firstwin;
3399 continue;
3400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 }
3402 if (bufIsChanged(wp->w_buffer))
3403#endif
3404 continue;
3405 }
Bram Moolenaareb44a682017-08-03 22:44:55 +02003406 win_close(wp, !buf_hide(wp->w_buffer)
3407 && !bufIsChanged(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 }
3409 }
3410
Bram Moolenaar459ca562016-11-10 18:16:33 +01003411 if (message && !ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 EMSG(_("E445: Other window contains changes"));
3413}
3414
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003416 * Init the current window "curwin".
3417 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 */
3419 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003420curwin_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003422 win_init_empty(curwin);
3423}
3424
3425 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003426win_init_empty(win_T *wp)
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003427{
3428 redraw_win_later(wp, NOT_VALID);
3429 wp->w_lines_valid = 0;
3430 wp->w_cursor.lnum = 1;
3431 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003433 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003435 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3436 wp->w_pcmark.col = 0;
3437 wp->w_prev_pcmark.lnum = 0;
3438 wp->w_prev_pcmark.col = 0;
3439 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003441 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003443 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003445 if (wp->w_p_rl)
3446 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003448 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003450#ifdef FEAT_SYN_HL
3451 wp->w_s = &wp->w_buffer->b_s;
3452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453}
3454
3455/*
3456 * Allocate the first window and put an empty buffer in it.
3457 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003458 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003460 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003461win_alloc_first(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003463 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003464 return FAIL;
3465
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003466 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003467 if (first_tabpage == NULL)
3468 return FAIL;
3469 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003470 curtab = first_tabpage;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003471
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003472 return OK;
3473}
3474
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003475/*
3476 * Init "aucmd_win". This can only be done after the first
3477 * window is fully initialized, thus it can't be in win_alloc_first().
3478 */
3479 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003480win_alloc_aucmd_win(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003481{
3482 aucmd_win = win_alloc(NULL, TRUE);
3483 if (aucmd_win != NULL)
3484 {
3485 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003486 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003487 new_frame(aucmd_win);
3488 }
3489}
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003490
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003491/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003492 * Allocate the first window or the first window in a new tab page.
3493 * When "oldwin" is NULL create an empty buffer for it.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003494 * When "oldwin" is not NULL copy info from it to the new window.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003495 * Return FAIL when something goes wrong (out of memory).
3496 */
3497 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003498win_alloc_firstwin(win_T *oldwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003499{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003500 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003501 if (oldwin == NULL)
3502 {
3503 /* Very first window, need to create an empty buffer for it and
3504 * initialize from scratch. */
3505 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3506 if (curwin == NULL || curbuf == NULL)
3507 return FAIL;
3508 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003509#ifdef FEAT_SYN_HL
3510 curwin->w_s = &(curbuf->b_s);
3511#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003512 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003513 curwin->w_alist = &global_alist;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003514 curwin_init(); /* init current window */
3515 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003516 else
3517 {
3518 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003519 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003520
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003521 /* We don't want cursor- and scroll-binding in the first window. */
3522 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003525 new_frame(curwin);
3526 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003527 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003528 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 topframe->fr_height = Rows - p_ch;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003531
3532 return OK;
3533}
3534
3535/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003536 * Create a frame for window "wp".
3537 */
3538 static void
3539new_frame(win_T *wp)
3540{
3541 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3542
3543 wp->w_frame = frp;
3544 if (frp != NULL)
3545 {
3546 frp->fr_layout = FR_LEAF;
3547 frp->fr_win = wp;
3548 }
3549}
3550
3551/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003552 * Initialize the window and frame size to the maximum.
3553 */
3554 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003555win_init_size(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003556{
3557 firstwin->w_height = ROWS_AVAIL;
3558 topframe->fr_height = ROWS_AVAIL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003559 firstwin->w_width = Columns;
3560 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561}
3562
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003563/*
3564 * Allocate a new tabpage_T and init the values.
3565 * Returns NULL when out of memory.
3566 */
3567 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003568alloc_tabpage(void)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003569{
3570 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003571# ifdef FEAT_GUI
3572 int i;
3573# endif
3574
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003575
3576 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003577 if (tp == NULL)
3578 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003579
Bram Moolenaar429fa852013-04-15 12:27:36 +02003580# ifdef FEAT_EVAL
3581 /* init t: variables */
3582 tp->tp_vars = dict_alloc();
3583 if (tp->tp_vars == NULL)
3584 {
3585 vim_free(tp);
3586 return NULL;
3587 }
3588 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3589# endif
3590
3591# ifdef FEAT_GUI
3592 for (i = 0; i < 3; i++)
3593 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003594# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003595# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003596 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003597# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003598 tp->tp_ch_used = p_ch;
3599
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003600 return tp;
3601}
3602
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003603 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003604free_tabpage(tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003605{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003606 int idx;
3607
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003608# ifdef FEAT_DIFF
3609 diff_clear(tp);
3610# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003611 for (idx = 0; idx < SNAP_COUNT; ++idx)
3612 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003613#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003614 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3615 hash_init(&tp->tp_vars->dv_hashtab);
3616 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003617#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003618
3619#ifdef FEAT_PYTHON
3620 python_tabpage_free(tp);
3621#endif
3622
3623#ifdef FEAT_PYTHON3
3624 python3_tabpage_free(tp);
3625#endif
3626
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003627 vim_free(tp);
3628}
3629
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003630/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003631 * Create a new Tab page with one window.
3632 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003633 * When "after" is 0 put it just after the current Tab page.
3634 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003635 * Return FAIL or OK.
3636 */
3637 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003638win_new_tabpage(int after)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003639{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003640 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003641 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003642 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003643
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003644 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003645 if (newtp == NULL)
3646 return FAIL;
3647
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003648 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003649 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003650 {
3651 vim_free(newtp);
3652 return FAIL;
3653 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003654 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003655
3656 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003657 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003658 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003659 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003660 if (after == 1)
3661 {
3662 /* New tab page becomes the first one. */
3663 newtp->tp_next = first_tabpage;
3664 first_tabpage = newtp;
3665 }
3666 else
3667 {
3668 if (after > 0)
3669 {
3670 /* Put new tab page before tab page "after". */
3671 n = 2;
3672 for (tp = first_tabpage; tp->tp_next != NULL
3673 && n < after; tp = tp->tp_next)
3674 ++n;
3675 }
3676 newtp->tp_next = tp->tp_next;
3677 tp->tp_next = newtp;
3678 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003679 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003680 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003681 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003682
3683 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003684 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003685
3686#if defined(FEAT_GUI)
3687 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3688 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003689 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003690#endif
Bram Moolenaar6d41c782018-06-06 09:11:12 +02003691#ifdef FEAT_JOB_CHANNEL
3692 entering_window(curwin);
3693#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003694
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003695 redraw_all_later(CLEAR);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003696 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003697 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003698 apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003699 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003700 return OK;
3701 }
3702
3703 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003704 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003705 return FAIL;
3706}
3707
3708/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003709 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3710 * like with ":split".
3711 * Returns OK if a new tab page was created, FAIL otherwise.
3712 */
3713 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003714may_open_tabpage(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003715{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003716 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003717
Bram Moolenaard326ce82007-03-11 14:48:29 +00003718 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003719 {
3720 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003721 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003722 return win_new_tabpage(n);
3723 }
3724 return FAIL;
3725}
3726
3727/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003728 * Create up to "maxcount" tabpages with empty windows.
3729 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003730 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003731 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003732make_tabpages(int maxcount)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003733{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003734 int count = maxcount;
3735 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003736
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003737 /* Limit to 'tabpagemax' tabs. */
3738 if (count > p_tpm)
3739 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003740
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003741 /*
3742 * Don't execute autocommands while creating the tab pages. Must do that
3743 * when putting the buffers in the windows.
3744 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003745 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003746
3747 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003748 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003749 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003750
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003751 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003752
3753 /* return actual number of tab pages */
3754 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003755}
3756
3757/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003758 * Return TRUE when "tpc" points to a valid tab page.
3759 */
3760 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003761valid_tabpage(tabpage_T *tpc)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003762{
3763 tabpage_T *tp;
3764
Bram Moolenaar29323592016-07-24 22:04:11 +02003765 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003766 if (tp == tpc)
3767 return TRUE;
3768 return FALSE;
3769}
3770
3771/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003772 * Return TRUE when "tpc" points to a valid tab page and at least one window is
3773 * valid.
3774 */
3775 int
3776valid_tabpage_win(tabpage_T *tpc)
3777{
3778 tabpage_T *tp;
3779 win_T *wp;
3780
3781 FOR_ALL_TABPAGES(tp)
3782 {
3783 if (tp == tpc)
3784 {
3785 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
3786 {
3787 if (win_valid_any_tab(wp))
3788 return TRUE;
3789 }
3790 return FALSE;
3791 }
3792 }
3793 /* shouldn't happen */
3794 return FALSE;
3795}
3796
3797/*
3798 * Close tabpage "tab", assuming it has no windows in it.
3799 * There must be another tabpage or this will crash.
3800 */
3801 void
3802close_tabpage(tabpage_T *tab)
3803{
3804 tabpage_T *ptp;
3805
3806 if (tab == first_tabpage)
3807 {
3808 first_tabpage = tab->tp_next;
3809 ptp = first_tabpage;
3810 }
3811 else
3812 {
3813 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tab;
3814 ptp = ptp->tp_next)
3815 ;
Bram Moolenaara37ffaa2017-03-21 21:58:00 +01003816 assert(ptp != NULL);
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003817 ptp->tp_next = tab->tp_next;
3818 }
3819
3820 goto_tabpage_tp(ptp, FALSE, FALSE);
3821 free_tabpage(tab);
3822}
3823
3824/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003825 * Find tab page "n" (first one is 1). Returns NULL when not found.
3826 */
3827 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003828find_tabpage(int n)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003829{
3830 tabpage_T *tp;
3831 int i = 1;
3832
3833 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3834 ++i;
3835 return tp;
3836}
3837
3838/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003839 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003840 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003841 */
3842 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003843tabpage_index(tabpage_T *ftp)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003844{
3845 int i = 1;
3846 tabpage_T *tp;
3847
3848 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3849 ++i;
3850 return i;
3851}
3852
3853/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003854 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003855 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003856 * FAIL.
3857 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003858 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003859 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003860leave_tabpage(
3861 buf_T *new_curbuf UNUSED, /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003862 NULL if unknown */
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003863 int trigger_leave_autocmds UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003864{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003865 tabpage_T *tp = curtab;
3866
Bram Moolenaar6d41c782018-06-06 09:11:12 +02003867#ifdef FEAT_JOB_CHANNEL
3868 leaving_window(curwin);
3869#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003870 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003871 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003872 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003873 if (new_curbuf != curbuf)
3874 {
3875 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3876 if (curtab != tp)
3877 return FAIL;
3878 }
3879 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3880 if (curtab != tp)
3881 return FAIL;
3882 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003883 if (curtab != tp)
3884 return FAIL;
3885 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003886#if defined(FEAT_GUI)
3887 /* Remove the scrollbars. They may be added back later. */
3888 if (gui.in_use)
3889 gui_remove_scrollbars();
3890#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003891 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003892 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003893 tp->tp_firstwin = firstwin;
3894 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003895 tp->tp_old_Rows = Rows;
3896 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003897 firstwin = NULL;
3898 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003899 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003900}
3901
3902/*
3903 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003904 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003905 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3906 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003907 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003908 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003909enter_tabpage(
3910 tabpage_T *tp,
3911 buf_T *old_curbuf UNUSED,
Bram Moolenaarf1d25012016-03-03 12:22:53 +01003912 int trigger_enter_autocmds,
3913 int trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003914{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003915 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003916 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003917
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003918 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003919 firstwin = tp->tp_firstwin;
3920 lastwin = tp->tp_lastwin;
3921 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003922
3923 /* We would like doing the TabEnter event first, but we don't have a
3924 * valid current window yet, which may break some commands.
3925 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaarc917da42016-07-19 22:31:36 +02003926 win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE,
Bram Moolenaard6949742013-06-16 14:18:28 +02003927 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003928 prevwin = next_prevwin;
3929
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003930 last_status(FALSE); /* status line may appear or disappear */
3931 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003932 must_redraw = CLEAR; /* need to redraw everything */
3933#ifdef FEAT_DIFF
3934 diff_need_scrollbind = TRUE;
3935#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003936
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003937 /* The tabpage line may have appeared or disappeared, may need to resize
3938 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003939 * frame sizes too. Use the stored value of p_ch, so that it can be
3940 * different for each tab page. */
3941 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003942 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3943#ifdef FEAT_GUI_TABLINE
3944 && !gui_use_tabline()
3945#endif
3946 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003947 shell_new_rows();
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003948 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003949 shell_new_columns(); /* update window widths */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003950
3951#if defined(FEAT_GUI)
3952 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3953 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003954 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003955#endif
3956
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003957 /* Apply autocommands after updating the display, when 'rows' and
3958 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003959 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003960 {
3961 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3962 if (old_curbuf != curbuf)
3963 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3964 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003965
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003966 redraw_all_later(CLEAR);
3967}
3968
3969/*
3970 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003971 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003972 */
3973 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003974goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003975{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003976 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003977 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003978 int i;
3979
Bram Moolenaard68071d2006-05-02 22:08:30 +00003980 if (text_locked())
3981 {
3982 /* Not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02003983 text_locked_msg();
Bram Moolenaard68071d2006-05-02 22:08:30 +00003984 return;
3985 }
3986
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003987 /* If there is only one it can't work. */
3988 if (first_tabpage->tp_next == NULL)
3989 {
3990 if (n > 1)
3991 beep_flush();
3992 return;
3993 }
3994
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003995 if (n == 0)
3996 {
3997 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003998 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003999 tp = first_tabpage;
4000 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004001 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004002 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004003 else if (n < 0)
4004 {
4005 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
4006 * this N times. */
4007 ttp = curtab;
4008 for (i = n; i < 0; ++i)
4009 {
4010 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
4011 tp = tp->tp_next)
4012 ;
4013 ttp = tp;
4014 }
4015 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004016 else if (n == 9999)
4017 {
4018 /* Go to last tab page. */
4019 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
4020 ;
4021 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004022 else
4023 {
4024 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004025 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004026 if (tp == NULL)
4027 {
4028 beep_flush();
4029 return;
4030 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004031 }
4032
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004033 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004034
4035#ifdef FEAT_GUI_TABLINE
4036 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004037 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004038#endif
4039}
4040
4041/*
4042 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004043 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4044 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004045 * Note: doesn't update the GUI tab.
4046 */
4047 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004048goto_tabpage_tp(
4049 tabpage_T *tp,
4050 int trigger_enter_autocmds,
4051 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004052{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02004053 /* Don't repeat a message in another tab page. */
4054 set_keep_msg(NULL, 0);
4055
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004056 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4057 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004058 {
4059 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004060 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4061 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004062 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004063 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4064 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004065 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004066}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
4068/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004069 * Enter window "wp" in tab page "tp".
4070 * Also updates the GUI tab.
4071 */
4072 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004073goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004074{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004075 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004076 if (curtab == tp && win_valid(wp))
4077 {
4078 win_enter(wp, TRUE);
4079# ifdef FEAT_GUI_TABLINE
4080 if (gui_use_tabline())
4081 gui_mch_set_curtab(tabpage_index(curtab));
4082# endif
4083 }
4084}
4085
4086/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004087 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004088 */
4089 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004090tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004091{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004092 int n = 1;
4093 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004094
4095 if (first_tabpage->tp_next == NULL)
4096 return;
4097
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004098 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4099 ++n;
4100
4101 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4102 && tp->tp_next == curtab))
4103 return;
4104
4105 tp_dst = tp;
4106
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004107 /* Remove the current tab page from the list of tab pages. */
4108 if (curtab == first_tabpage)
4109 first_tabpage = curtab->tp_next;
4110 else
4111 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004112 FOR_ALL_TABPAGES(tp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004113 if (tp->tp_next == curtab)
4114 break;
4115 if (tp == NULL) /* "cannot happen" */
4116 return;
4117 tp->tp_next = curtab->tp_next;
4118 }
4119
4120 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004121 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004122 {
4123 curtab->tp_next = first_tabpage;
4124 first_tabpage = curtab;
4125 }
4126 else
4127 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004128 curtab->tp_next = tp_dst->tp_next;
4129 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004130 }
4131
4132 /* Need to redraw the tabline. Tab page contents doesn't change. */
4133 redraw_tabline = TRUE;
4134}
4135
4136
4137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 * Go to another window.
4139 * When jumping to another buffer, stop Visual mode. Do this before
4140 * changing windows so we can yank the selection into the '*' register.
4141 * When jumping to another window on the same buffer, adjust its cursor
4142 * position to keep the same Visual area.
4143 */
4144 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004145win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004147#ifdef FEAT_CONCEAL
4148 win_T *owp = curwin;
4149#endif
4150
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004151 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 {
4153 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004154 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 return;
4156 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004157 if (curbuf_locked())
4158 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004159
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 if (wp->w_buffer != curbuf)
4161 reset_VIsual_and_resel();
4162 else if (VIsual_active)
4163 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164
4165#ifdef FEAT_GUI
4166 need_mouse_correct = TRUE;
4167#endif
4168 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004169
4170#ifdef FEAT_CONCEAL
4171 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004172 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004173 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004174 if (curwin->w_p_cole > 0 && !msg_scrolled)
4175 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177}
4178
4179#if defined(FEAT_PERL) || defined(PROTO)
4180/*
4181 * Find window number "winnr" (counting top to bottom).
4182 */
4183 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004184win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185{
4186 win_T *wp;
4187
Bram Moolenaar29323592016-07-24 22:04:11 +02004188 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 if (--winnr == 0)
4190 break;
4191 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192}
4193#endif
4194
Bram Moolenaar4033c552017-09-16 20:54:51 +02004195#if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004196/*
4197 * Find the tabpage for window "win".
4198 */
4199 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004200win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004201{
4202 win_T *wp;
4203 tabpage_T *tp;
4204
Bram Moolenaar29323592016-07-24 22:04:11 +02004205 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004206 if (wp == win)
4207 return tp;
4208 return NULL;
4209}
4210#endif
4211
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212/*
4213 * Move to window above or below "count" times.
4214 */
4215 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004216win_goto_ver(
4217 int up, /* TRUE to go to win above */
4218 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219{
4220 frame_T *fr;
4221 frame_T *nfr;
4222 frame_T *foundfr;
4223
4224 foundfr = curwin->w_frame;
4225 while (count--)
4226 {
4227 /*
4228 * First go upwards in the tree of frames until we find a upwards or
4229 * downwards neighbor.
4230 */
4231 fr = foundfr;
4232 for (;;)
4233 {
4234 if (fr == topframe)
4235 goto end;
4236 if (up)
4237 nfr = fr->fr_prev;
4238 else
4239 nfr = fr->fr_next;
4240 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4241 break;
4242 fr = fr->fr_parent;
4243 }
4244
4245 /*
4246 * Now go downwards to find the bottom or top frame in it.
4247 */
4248 for (;;)
4249 {
4250 if (nfr->fr_layout == FR_LEAF)
4251 {
4252 foundfr = nfr;
4253 break;
4254 }
4255 fr = nfr->fr_child;
4256 if (nfr->fr_layout == FR_ROW)
4257 {
4258 /* Find the frame at the cursor row. */
4259 while (fr->fr_next != NULL
4260 && frame2win(fr)->w_wincol + fr->fr_width
4261 <= curwin->w_wincol + curwin->w_wcol)
4262 fr = fr->fr_next;
4263 }
4264 if (nfr->fr_layout == FR_COL && up)
4265 while (fr->fr_next != NULL)
4266 fr = fr->fr_next;
4267 nfr = fr;
4268 }
4269 }
4270end:
4271 if (foundfr != NULL)
4272 win_goto(foundfr->fr_win);
4273}
4274
4275/*
4276 * Move to left or right window.
4277 */
4278 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004279win_goto_hor(
4280 int left, /* TRUE to go to left win */
4281 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282{
4283 frame_T *fr;
4284 frame_T *nfr;
4285 frame_T *foundfr;
4286
4287 foundfr = curwin->w_frame;
4288 while (count--)
4289 {
4290 /*
4291 * First go upwards in the tree of frames until we find a left or
4292 * right neighbor.
4293 */
4294 fr = foundfr;
4295 for (;;)
4296 {
4297 if (fr == topframe)
4298 goto end;
4299 if (left)
4300 nfr = fr->fr_prev;
4301 else
4302 nfr = fr->fr_next;
4303 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4304 break;
4305 fr = fr->fr_parent;
4306 }
4307
4308 /*
4309 * Now go downwards to find the leftmost or rightmost frame in it.
4310 */
4311 for (;;)
4312 {
4313 if (nfr->fr_layout == FR_LEAF)
4314 {
4315 foundfr = nfr;
4316 break;
4317 }
4318 fr = nfr->fr_child;
4319 if (nfr->fr_layout == FR_COL)
4320 {
4321 /* Find the frame at the cursor row. */
4322 while (fr->fr_next != NULL
4323 && frame2win(fr)->w_winrow + fr->fr_height
4324 <= curwin->w_winrow + curwin->w_wrow)
4325 fr = fr->fr_next;
4326 }
4327 if (nfr->fr_layout == FR_ROW && left)
4328 while (fr->fr_next != NULL)
4329 fr = fr->fr_next;
4330 nfr = fr;
4331 }
4332 }
4333end:
4334 if (foundfr != NULL)
4335 win_goto(foundfr->fr_win);
4336}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337
4338/*
4339 * Make window "wp" the current window.
4340 */
4341 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004342win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343{
Bram Moolenaarc917da42016-07-19 22:31:36 +02004344 win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345}
4346
4347/*
4348 * Make window wp the current window.
4349 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4350 * been closed and isn't valid.
4351 */
4352 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004353win_enter_ext(
4354 win_T *wp,
4355 int undo_sync,
4356 int curwin_invalid,
Bram Moolenaar6f470022018-04-10 18:47:20 +02004357 int trigger_new_autocmds,
4358 int trigger_enter_autocmds,
4359 int trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
4363 if (wp == curwin && !curwin_invalid) /* nothing to do */
4364 return;
4365
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004366#ifdef FEAT_JOB_CHANNEL
4367 if (!curwin_invalid)
4368 leaving_window(curwin);
4369#endif
4370
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004371 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 {
4373 /*
4374 * Be careful: If autocommands delete the window, return now.
4375 */
4376 if (wp->w_buffer != curbuf)
4377 {
4378 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4379 other_buffer = TRUE;
4380 if (!win_valid(wp))
4381 return;
4382 }
4383 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4384 if (!win_valid(wp))
4385 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004386#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 /* autocmds may abort script processing */
4388 if (aborting())
4389 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393 /* sync undo before leaving the current buffer */
4394 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004395 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004396
4397 /* Might need to scroll the old window before switching, e.g., when the
4398 * cursor was moved. */
4399 update_topline();
4400
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 /* may have to copy the buffer options when 'cpo' contains 'S' */
4402 if (wp->w_buffer != curbuf)
4403 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4404 if (!curwin_invalid)
4405 {
4406 prevwin = curwin; /* remember for CTRL-W p */
4407 curwin->w_redr_status = TRUE;
4408 }
4409 curwin = wp;
4410 curbuf = wp->w_buffer;
4411 check_cursor();
4412#ifdef FEAT_VIRTUALEDIT
4413 if (!virtual_active())
4414 curwin->w_cursor.coladd = 0;
4415#endif
4416 changed_line_abv_curs(); /* assume cursor position needs updating */
4417
4418 if (curwin->w_localdir != NULL)
4419 {
4420 /* Window has a local directory: Save current directory as global
4421 * directory (unless that was done already) and change to the local
4422 * directory. */
4423 if (globaldir == NULL)
4424 {
4425 char_u cwd[MAXPATHL];
4426
4427 if (mch_dirname(cwd, MAXPATHL) == OK)
4428 globaldir = vim_strsave(cwd);
4429 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004430 if (mch_chdir((char *)curwin->w_localdir) == 0)
4431 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 }
4433 else if (globaldir != NULL)
4434 {
4435 /* Window doesn't have a local directory and we are not in the global
4436 * directory: Change to the global directory. */
Bram Moolenaar42335f52018-09-13 15:33:43 +02004437 vim_ignored = mch_chdir((char *)globaldir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004438 VIM_CLEAR(globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 shorten_fnames(TRUE);
4440 }
4441
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004442#ifdef FEAT_JOB_CHANNEL
4443 entering_window(curwin);
4444#endif
Bram Moolenaarc917da42016-07-19 22:31:36 +02004445 if (trigger_new_autocmds)
4446 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004447 if (trigger_enter_autocmds)
4448 {
4449 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4450 if (other_buffer)
4451 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454#ifdef FEAT_TITLE
4455 maketitle();
4456#endif
4457 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004458 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 if (restart_edit)
4460 redraw_later(VALID); /* causes status line redraw */
4461
4462 /* set window height to desired minimal value */
4463 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4464 win_setheight((int)p_wh);
4465 else if (curwin->w_height == 0)
4466 win_setheight(1);
4467
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004469 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 win_setwidth((int)p_wiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
4472#ifdef FEAT_MOUSE
4473 setmouse(); /* in case jumped to/from help buffer */
4474#endif
4475
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004476 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02004477 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478}
4479
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004482 * Jump to the first open window that contains buffer "buf", if one exists.
4483 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 */
4485 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004486buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004488 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004490 if (curwin->w_buffer == buf)
4491 wp = curwin;
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004492 else
Bram Moolenaar29323592016-07-24 22:04:11 +02004493 FOR_ALL_WINDOWS(wp)
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004494 if (wp->w_buffer == buf)
4495 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 if (wp != NULL)
4497 win_enter(wp, FALSE);
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004498 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004500
4501/*
4502 * Jump to the first open window in any tab page that contains buffer "buf",
4503 * if one exists.
4504 * Returns a pointer to the window found, otherwise NULL.
4505 */
4506 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004507buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004508{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004509 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004510 tabpage_T *tp;
4511
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004512 if (wp != NULL)
4513 return wp;
4514
Bram Moolenaar29323592016-07-24 22:04:11 +02004515 FOR_ALL_TABPAGES(tp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004516 if (tp != curtab)
4517 {
4518 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4519 if (wp->w_buffer == buf)
4520 break;
4521 if (wp != NULL)
4522 {
4523 goto_tabpage_win(tp, wp);
4524 if (curwin != wp)
4525 wp = NULL; /* something went wrong */
4526 break;
4527 }
4528 }
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004529 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004530}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
Bram Moolenaar888ccac2016-06-04 18:49:36 +02004532static int last_win_id = LOWEST_WIN_ID - 1;
Bram Moolenaar86edef62016-03-13 18:07:30 +01004533
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004535 * Allocate a window structure and link it in the window list when "hidden" is
4536 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004539win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004541 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542
4543 /*
4544 * allocate window structure and linesizes arrays
4545 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004546 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004547 if (new_wp == NULL)
4548 return NULL;
4549
4550 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004552 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004553 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
4555
Bram Moolenaar86edef62016-03-13 18:07:30 +01004556 new_wp->w_id = ++last_win_id;
4557
Bram Moolenaar429fa852013-04-15 12:27:36 +02004558#ifdef FEAT_EVAL
4559 /* init w: variables */
4560 new_wp->w_vars = dict_alloc();
4561 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004563 win_free_lsize(new_wp);
4564 vim_free(new_wp);
4565 return NULL;
4566 }
4567 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004568#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004569
Bram Moolenaar429fa852013-04-15 12:27:36 +02004570 /* Don't execute autocommands while the window is not properly
4571 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4572 * event. */
4573 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004574
Bram Moolenaar429fa852013-04-15 12:27:36 +02004575 /*
4576 * link the window in the window list
4577 */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004578 if (!hidden)
4579 win_append(after, new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004580 new_wp->w_wincol = 0;
4581 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582
Bram Moolenaar429fa852013-04-15 12:27:36 +02004583 /* position the display and the cursor at the top of the file. */
4584 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004586 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004588 new_wp->w_botline = 2;
4589 new_wp->w_cursor.lnum = 1;
Bram Moolenaar429fa852013-04-15 12:27:36 +02004590 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
Bram Moolenaar429fa852013-04-15 12:27:36 +02004592 /* We won't calculate w_fraction until resizing the window */
4593 new_wp->w_fraction = 0;
4594 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595
4596#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004597 if (gui.in_use)
4598 {
4599 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4600 SBAR_LEFT, new_wp);
4601 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4602 SBAR_RIGHT, new_wp);
4603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604#endif
4605#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004606 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004608 unblock_autocmds();
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004609#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004610 new_wp->w_match_head = NULL;
4611 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004612#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004613 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614}
4615
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004617 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 */
4619 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004620win_free(
4621 win_T *wp,
4622 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623{
4624 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004625 buf_T *buf;
4626 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004628#ifdef FEAT_FOLDING
4629 clearFolding(wp);
4630#endif
4631
4632 /* reduce the reference count to the argument list. */
4633 alist_unlink(wp->w_alist);
4634
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004635 /* Don't execute autocommands while the window is halfway being deleted.
4636 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004637 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004638
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004639#ifdef FEAT_LUA
4640 lua_window_free(wp);
4641#endif
4642
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004643#ifdef FEAT_MZSCHEME
4644 mzscheme_window_free(wp);
4645#endif
4646
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647#ifdef FEAT_PERL
4648 perl_win_free(wp);
4649#endif
4650
4651#ifdef FEAT_PYTHON
4652 python_window_free(wp);
4653#endif
4654
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004655#ifdef FEAT_PYTHON3
4656 python3_window_free(wp);
4657#endif
4658
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659#ifdef FEAT_TCL
4660 tcl_window_free(wp);
4661#endif
4662
4663#ifdef FEAT_RUBY
4664 ruby_window_free(wp);
4665#endif
4666
4667 clear_winopt(&wp->w_onebuf_opt);
4668 clear_winopt(&wp->w_allbuf_opt);
4669
4670#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004671 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4672 hash_init(&wp->w_vars->dv_hashtab);
4673 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674#endif
4675
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004676 {
4677 tabpage_T *ttp;
4678
4679 if (prevwin == wp)
4680 prevwin = NULL;
Bram Moolenaar29323592016-07-24 22:04:11 +02004681 FOR_ALL_TABPAGES(ttp)
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004682 if (ttp->tp_prevwin == wp)
4683 ttp->tp_prevwin = NULL;
4684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 win_free_lsize(wp);
4686
4687 for (i = 0; i < wp->w_tagstacklen; ++i)
4688 vim_free(wp->w_tagstack[i].tagname);
4689
4690 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004691
Bram Moolenaarff18df02013-07-24 17:51:57 +02004692 /* Remove the window from the b_wininfo lists, it may happen that the
4693 * freed memory is re-used for another window. */
Bram Moolenaar29323592016-07-24 22:04:11 +02004694 FOR_ALL_BUFFERS(buf)
Bram Moolenaarff18df02013-07-24 17:51:57 +02004695 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4696 if (wip->wi_win == wp)
4697 wip->wi_win = NULL;
4698
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004700 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004702
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703#ifdef FEAT_JUMPLIST
4704 free_jumplist(wp);
4705#endif
4706
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004707#ifdef FEAT_QUICKFIX
4708 qf_free_all(wp);
4709#endif
4710
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711#ifdef FEAT_GUI
4712 if (gui.in_use)
4713 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4715 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4716 }
4717#endif /* FEAT_GUI */
4718
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004719#ifdef FEAT_MENU
4720 remove_winbar(wp);
4721#endif
4722
Bram Moolenaar860cae12010-06-05 23:22:07 +02004723#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004724 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004725#endif
4726
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004727 if (wp != aucmd_win)
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004728 win_remove(wp, tp);
Bram Moolenaar3be85852014-06-12 14:01:31 +02004729 if (autocmd_busy)
4730 {
4731 wp->w_next = au_pending_free_win;
4732 au_pending_free_win = wp;
4733 }
4734 else
4735 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004736
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004737 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738}
4739
4740/*
4741 * Append window "wp" in the window list after window "after".
4742 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004743 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004744win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
4746 win_T *before;
4747
4748 if (after == NULL) /* after NULL is in front of the first */
4749 before = firstwin;
4750 else
4751 before = after->w_next;
4752
4753 wp->w_next = before;
4754 wp->w_prev = after;
4755 if (after == NULL)
4756 firstwin = wp;
4757 else
4758 after->w_next = wp;
4759 if (before == NULL)
4760 lastwin = wp;
4761 else
4762 before->w_prev = wp;
4763}
4764
4765/*
4766 * Remove a window from the window list.
4767 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004768 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004769win_remove(
4770 win_T *wp,
4771 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772{
4773 if (wp->w_prev != NULL)
4774 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004775 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004776 firstwin = curtab->tp_firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004777 else
4778 tp->tp_firstwin = wp->w_next;
Bram Moolenaar816968d2017-09-29 21:29:18 +02004779
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 if (wp->w_next != NULL)
4781 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004782 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004783 lastwin = curtab->tp_lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004784 else
4785 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786}
4787
4788/*
4789 * Append frame "frp" in a frame list after frame "after".
4790 */
4791 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004792frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793{
4794 frp->fr_next = after->fr_next;
4795 after->fr_next = frp;
4796 if (frp->fr_next != NULL)
4797 frp->fr_next->fr_prev = frp;
4798 frp->fr_prev = after;
4799}
4800
4801/*
4802 * Insert frame "frp" in a frame list before frame "before".
4803 */
4804 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004805frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806{
4807 frp->fr_next = before;
4808 frp->fr_prev = before->fr_prev;
4809 before->fr_prev = frp;
4810 if (frp->fr_prev != NULL)
4811 frp->fr_prev->fr_next = frp;
4812 else
4813 frp->fr_parent->fr_child = frp;
4814}
4815
4816/*
4817 * Remove a frame from a frame list.
4818 */
4819 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004820frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821{
4822 if (frp->fr_prev != NULL)
4823 frp->fr_prev->fr_next = frp->fr_next;
4824 else
Bram Moolenaar6f361c92018-01-31 19:06:50 +01004825 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 frp->fr_parent->fr_child = frp->fr_next;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01004827 /* special case: topframe->fr_child == frp */
4828 if (topframe->fr_child == frp)
4829 topframe->fr_child = frp->fr_next;
4830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 if (frp->fr_next != NULL)
4832 frp->fr_next->fr_prev = frp->fr_prev;
4833}
4834
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835/*
4836 * Allocate w_lines[] for window "wp".
4837 * Return FAIL for failure, OK for success.
4838 */
4839 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004840win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841{
4842 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004843 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 if (wp->w_lines == NULL)
4845 return FAIL;
4846 return OK;
4847}
4848
4849/*
4850 * free lsize arrays for a window
4851 */
4852 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004853win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004855 /* TODO: why would wp be NULL here? */
4856 if (wp != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004857 VIM_CLEAR(wp->w_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858}
4859
4860/*
4861 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004862 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 */
4864 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004865shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004867 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
4869 if (firstwin == NULL) /* not initialized yet */
4870 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 if (h < frame_minheight(topframe, NULL))
4872 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004873
4874 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 * that doesn't result in the right height, forget about that option. */
4876 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004877 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 frame_new_height(topframe, h, FALSE, FALSE);
4879
4880 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 compute_cmdrow();
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004882 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004883
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884#if 0
4885 /* Disabled: don't want making the screen smaller make a window larger. */
4886 if (p_ea)
4887 win_equal(curwin, FALSE, 'v');
4888#endif
4889}
4890
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891/*
4892 * Called from win_new_shellsize() after Columns changed.
4893 */
4894 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004895shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896{
4897 if (firstwin == NULL) /* not initialized yet */
4898 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004899
4900 /* First try setting the widths of windows with 'winfixwidth'. If that
4901 * doesn't result in the right width, forget about that option. */
4902 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004903 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004904 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4905
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4907#if 0
4908 /* Disabled: don't want making the screen smaller make a window larger. */
4909 if (p_ea)
4910 win_equal(curwin, FALSE, 'h');
4911#endif
4912}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
4914#if defined(FEAT_CMDWIN) || defined(PROTO)
4915/*
4916 * Save the size of all windows in "gap".
4917 */
4918 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004919win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920
4921{
4922 win_T *wp;
4923
4924 ga_init2(gap, (int)sizeof(int), 1);
4925 if (ga_grow(gap, win_count() * 2) == OK)
Bram Moolenaar29323592016-07-24 22:04:11 +02004926 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
4928 ((int *)gap->ga_data)[gap->ga_len++] =
4929 wp->w_width + wp->w_vsep_width;
4930 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4931 }
4932}
4933
4934/*
4935 * Restore window sizes, but only if the number of windows is still the same.
4936 * Does not free the growarray.
4937 */
4938 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004939win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940{
4941 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004942 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943
4944 if (win_count() * 2 == gap->ga_len)
4945 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004946 /* The order matters, because frames contain other frames, but it's
4947 * difficult to get right. The easy way out is to do it twice. */
4948 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004950 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004951 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb643e772014-07-16 15:18:26 +02004952 {
4953 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4954 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 }
4957 /* recompute the window positions */
4958 (void)win_comp_pos();
4959 }
4960}
4961#endif /* FEAT_CMDWIN */
4962
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963/*
4964 * Update the position for all windows, using the width and height of the
4965 * frames.
4966 * Returns the row just after the last window.
4967 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004968 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004969win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004971 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 int col = 0;
4973
4974 frame_comp_pos(topframe, &row, &col);
4975 return row;
4976}
4977
4978/*
4979 * Update the position of the windows in frame "topfrp", using the width and
4980 * height of the frames.
4981 * "*row" and "*col" are the top-left position of the frame. They are updated
4982 * to the bottom-right position plus one.
4983 */
4984 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004985frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
4987 win_T *wp;
4988 frame_T *frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 int startcol;
4990 int startrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01004991 int h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
4993 wp = topfrp->fr_win;
4994 if (wp != NULL)
4995 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004996 if (wp->w_winrow != *row || wp->w_wincol != *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
4998 /* position changed, redraw */
4999 wp->w_winrow = *row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 wp->w_wincol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 redraw_win_later(wp, NOT_VALID);
5002 wp->w_redr_status = TRUE;
5003 }
Bram Moolenaar415a6932017-12-05 20:31:07 +01005004 /* WinBar will not show if the window height is zero */
5005 h = VISIBLE_HEIGHT(wp) + wp->w_status_height;
5006 *row += h > topfrp->fr_height ? topfrp->fr_height : h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 *col += wp->w_width + wp->w_vsep_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
5009 else
5010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 startrow = *row;
5012 startcol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
5014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 if (topfrp->fr_layout == FR_ROW)
5016 *row = startrow; /* all frames are at the same row */
5017 else
5018 *col = startcol; /* all frames are at the same col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 frame_comp_pos(frp, row, col);
5020 }
5021 }
5022}
5023
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024/*
5025 * Set current window height and take care of repositioning other windows to
5026 * fit around it.
5027 */
5028 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005029win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030{
5031 win_setheight_win(height, curwin);
5032}
5033
5034/*
5035 * Set the window height of window "win" and take care of repositioning other
5036 * windows to fit around it.
5037 */
5038 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005039win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040{
5041 int row;
5042
5043 if (win == curwin)
5044 {
5045 /* Always keep current window at least one line high, even when
5046 * 'winminheight' is zero. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 if (height < p_wmh)
5048 height = p_wmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 if (height == 0)
5050 height = 1;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005051 height += WINBAR_HEIGHT(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
5053
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 frame_setheight(win->w_frame, height + win->w_status_height);
5055
5056 /* recompute the window positions */
5057 row = win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058
5059 /*
5060 * If there is extra space created between the last window and the command
5061 * line, clear it.
5062 */
5063 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5064 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5065 cmdline_row = row;
5066 msg_row = row;
5067 msg_col = 0;
5068
5069 redraw_all_later(NOT_VALID);
5070}
5071
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072/*
5073 * Set the height of a frame to "height" and take care that all frames and
5074 * windows inside it are resized. Also resize frames on the left and right if
5075 * the are in the same FR_ROW frame.
5076 *
5077 * Strategy:
5078 * If the frame is part of a FR_COL frame, try fitting the frame in that
5079 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5080 * go to containing frames to resize them and make room.
5081 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5082 * Check for the minimal height of the FR_ROW frame.
5083 * At the top level we can also use change the command line height.
5084 */
5085 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005086frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087{
5088 int room; /* total number of lines available */
5089 int take; /* number of lines taken from other windows */
5090 int room_cmdline; /* lines available from cmdline */
5091 int run;
5092 frame_T *frp;
5093 int h;
5094 int room_reserved;
5095
5096 /* If the height already is the desired value, nothing to do. */
5097 if (curfrp->fr_height == height)
5098 return;
5099
5100 if (curfrp->fr_parent == NULL)
5101 {
5102 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005103 if (height > ROWS_AVAIL)
5104 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 if (height > 0)
5106 frame_new_height(curfrp, height, FALSE, FALSE);
5107 }
5108 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5109 {
5110 /* Row of frames: Also need to resize frames left and right of this
5111 * one. First check for the minimal height of these. */
5112 h = frame_minheight(curfrp->fr_parent, NULL);
5113 if (height < h)
5114 height = h;
5115 frame_setheight(curfrp->fr_parent, height);
5116 }
5117 else
5118 {
5119 /*
5120 * Column of frames: try to change only frames in this column.
5121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 /*
5123 * Do this twice:
5124 * 1: compute room available, if it's not enough try resizing the
5125 * containing frame.
5126 * 2: compute the room available and adjust the height to it.
5127 * Try not to reduce the height of a window with 'winfixheight' set.
5128 */
5129 for (run = 1; run <= 2; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
5131 room = 0;
5132 room_reserved = 0;
5133 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5134 frp = frp->fr_next)
5135 {
5136 if (frp != curfrp
5137 && frp->fr_win != NULL
5138 && frp->fr_win->w_p_wfh)
5139 room_reserved += frp->fr_height;
5140 room += frp->fr_height;
5141 if (frp != curfrp)
5142 room -= frame_minheight(frp, NULL);
5143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 if (curfrp->fr_width != Columns)
5145 room_cmdline = 0;
5146 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
5148 room_cmdline = Rows - p_ch - (lastwin->w_winrow
Bram Moolenaar415a6932017-12-05 20:31:07 +01005149 + VISIBLE_HEIGHT(lastwin)
5150 + lastwin->w_status_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 if (room_cmdline < 0)
5152 room_cmdline = 0;
5153 }
5154
5155 if (height <= room + room_cmdline)
5156 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (run == 2 || curfrp->fr_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 {
5159 if (height > room + room_cmdline)
5160 height = room + room_cmdline;
5161 break;
5162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 frame_setheight(curfrp->fr_parent, height
5164 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 }
5166
5167 /*
5168 * Compute the number of lines we will take from others frames (can be
5169 * negative!).
5170 */
5171 take = height - curfrp->fr_height;
5172
5173 /* If there is not enough room, also reduce the height of a window
5174 * with 'winfixheight' set. */
5175 if (height > room + room_cmdline - room_reserved)
5176 room_reserved = room + room_cmdline - height;
5177 /* If there is only a 'winfixheight' window and making the
5178 * window smaller, need to make the other window taller. */
5179 if (take < 0 && room - curfrp->fr_height < room_reserved)
5180 room_reserved = 0;
5181
5182 if (take > 0 && room_cmdline > 0)
5183 {
5184 /* use lines from cmdline first */
5185 if (take < room_cmdline)
5186 room_cmdline = take;
5187 take -= room_cmdline;
5188 topframe->fr_height += room_cmdline;
5189 }
5190
5191 /*
5192 * set the current frame to the new height
5193 */
5194 frame_new_height(curfrp, height, FALSE, FALSE);
5195
5196 /*
5197 * First take lines from the frames after the current frame. If
5198 * that is not enough, takes lines from frames above the current
5199 * frame.
5200 */
5201 for (run = 0; run < 2; ++run)
5202 {
5203 if (run == 0)
5204 frp = curfrp->fr_next; /* 1st run: start with next window */
5205 else
5206 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5207 while (frp != NULL && take != 0)
5208 {
5209 h = frame_minheight(frp, NULL);
5210 if (room_reserved > 0
5211 && frp->fr_win != NULL
5212 && frp->fr_win->w_p_wfh)
5213 {
5214 if (room_reserved >= frp->fr_height)
5215 room_reserved -= frp->fr_height;
5216 else
5217 {
5218 if (frp->fr_height - room_reserved > take)
5219 room_reserved = frp->fr_height - take;
5220 take -= frp->fr_height - room_reserved;
5221 frame_new_height(frp, room_reserved, FALSE, FALSE);
5222 room_reserved = 0;
5223 }
5224 }
5225 else
5226 {
5227 if (frp->fr_height - take < h)
5228 {
5229 take -= frp->fr_height - h;
5230 frame_new_height(frp, h, FALSE, FALSE);
5231 }
5232 else
5233 {
5234 frame_new_height(frp, frp->fr_height - take,
5235 FALSE, FALSE);
5236 take = 0;
5237 }
5238 }
5239 if (run == 0)
5240 frp = frp->fr_next;
5241 else
5242 frp = frp->fr_prev;
5243 }
5244 }
5245 }
5246}
5247
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248/*
5249 * Set current window width and take care of repositioning other windows to
5250 * fit around it.
5251 */
5252 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005253win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254{
5255 win_setwidth_win(width, curwin);
5256}
5257
5258 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005259win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260{
5261 /* Always keep current window at least one column wide, even when
5262 * 'winminwidth' is zero. */
5263 if (wp == curwin)
5264 {
5265 if (width < p_wmw)
5266 width = p_wmw;
5267 if (width == 0)
5268 width = 1;
5269 }
5270
5271 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5272
5273 /* recompute the window positions */
5274 (void)win_comp_pos();
5275
5276 redraw_all_later(NOT_VALID);
5277}
5278
5279/*
5280 * Set the width of a frame to "width" and take care that all frames and
5281 * windows inside it are resized. Also resize frames above and below if the
5282 * are in the same FR_ROW frame.
5283 *
5284 * Strategy is similar to frame_setheight().
5285 */
5286 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005287frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288{
5289 int room; /* total number of lines available */
5290 int take; /* number of lines taken from other windows */
5291 int run;
5292 frame_T *frp;
5293 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005294 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295
5296 /* If the width already is the desired value, nothing to do. */
5297 if (curfrp->fr_width == width)
5298 return;
5299
5300 if (curfrp->fr_parent == NULL)
5301 /* topframe: can't change width */
5302 return;
5303
5304 if (curfrp->fr_parent->fr_layout == FR_COL)
5305 {
5306 /* Column of frames: Also need to resize frames above and below of
5307 * this one. First check for the minimal width of these. */
5308 w = frame_minwidth(curfrp->fr_parent, NULL);
5309 if (width < w)
5310 width = w;
5311 frame_setwidth(curfrp->fr_parent, width);
5312 }
5313 else
5314 {
5315 /*
5316 * Row of frames: try to change only frames in this row.
5317 *
5318 * Do this twice:
5319 * 1: compute room available, if it's not enough try resizing the
5320 * containing frame.
5321 * 2: compute the room available and adjust the width to it.
5322 */
5323 for (run = 1; run <= 2; ++run)
5324 {
5325 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005326 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5328 frp = frp->fr_next)
5329 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005330 if (frp != curfrp
5331 && frp->fr_win != NULL
5332 && frp->fr_win->w_p_wfw)
5333 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 room += frp->fr_width;
5335 if (frp != curfrp)
5336 room -= frame_minwidth(frp, NULL);
5337 }
5338
5339 if (width <= room)
5340 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005341 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 {
5343 if (width > room)
5344 width = room;
5345 break;
5346 }
5347 frame_setwidth(curfrp->fr_parent, width
5348 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5349 }
5350
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 /*
5352 * Compute the number of lines we will take from others frames (can be
5353 * negative!).
5354 */
5355 take = width - curfrp->fr_width;
5356
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005357 /* If there is not enough room, also reduce the width of a window
5358 * with 'winfixwidth' set. */
5359 if (width > room - room_reserved)
5360 room_reserved = room - width;
5361 /* If there is only a 'winfixwidth' window and making the
5362 * window smaller, need to make the other window narrower. */
5363 if (take < 0 && room - curfrp->fr_width < room_reserved)
5364 room_reserved = 0;
5365
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 /*
5367 * set the current frame to the new width
5368 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005369 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370
5371 /*
5372 * First take lines from the frames right of the current frame. If
5373 * that is not enough, takes lines from frames left of the current
5374 * frame.
5375 */
5376 for (run = 0; run < 2; ++run)
5377 {
5378 if (run == 0)
5379 frp = curfrp->fr_next; /* 1st run: start with next window */
5380 else
5381 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5382 while (frp != NULL && take != 0)
5383 {
5384 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005385 if (room_reserved > 0
5386 && frp->fr_win != NULL
5387 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005389 if (room_reserved >= frp->fr_width)
5390 room_reserved -= frp->fr_width;
5391 else
5392 {
5393 if (frp->fr_width - room_reserved > take)
5394 room_reserved = frp->fr_width - take;
5395 take -= frp->fr_width - room_reserved;
5396 frame_new_width(frp, room_reserved, FALSE, FALSE);
5397 room_reserved = 0;
5398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 }
5400 else
5401 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005402 if (frp->fr_width - take < w)
5403 {
5404 take -= frp->fr_width - w;
5405 frame_new_width(frp, w, FALSE, FALSE);
5406 }
5407 else
5408 {
5409 frame_new_width(frp, frp->fr_width - take,
5410 FALSE, FALSE);
5411 take = 0;
5412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 }
5414 if (run == 0)
5415 frp = frp->fr_next;
5416 else
5417 frp = frp->fr_prev;
5418 }
5419 }
5420 }
5421}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422
5423/*
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005424 * Check 'winminheight' for a valid value and reduce it if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 */
5426 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005427win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
5429 int room;
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005430 int needed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005433 // loop until there is a 'winminheight' that is possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 while (p_wmh > 0)
5435 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005436 room = Rows - p_ch;
5437 needed = frame_minheight(topframe, NULL);
5438 if (room >= needed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 break;
5440 --p_wmh;
5441 if (first)
5442 {
5443 EMSG(_(e_noroom));
5444 first = FALSE;
5445 }
5446 }
5447}
5448
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005449/*
5450 * Check 'winminwidth' for a valid value and reduce it if needed.
5451 */
5452 void
5453win_setminwidth(void)
5454{
5455 int room;
5456 int needed;
5457 int first = TRUE;
5458
5459 // loop until there is a 'winminheight' that is possible
5460 while (p_wmw > 0)
5461 {
5462 room = Columns;
5463 needed = frame_minwidth(topframe, NULL);
5464 if (room >= needed)
5465 break;
5466 --p_wmw;
5467 if (first)
5468 {
5469 EMSG(_(e_noroom));
5470 first = FALSE;
5471 }
5472 }
5473}
5474
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005475#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
5477/*
5478 * Status line of dragwin is dragged "offset" lines down (negative is up).
5479 */
5480 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005481win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482{
5483 frame_T *curfr;
5484 frame_T *fr;
5485 int room;
5486 int row;
5487 int up; /* if TRUE, drag status line up, otherwise down */
5488 int n;
5489
5490 fr = dragwin->w_frame;
5491 curfr = fr;
5492 if (fr != topframe) /* more than one window */
5493 {
5494 fr = fr->fr_parent;
5495 /* When the parent frame is not a column of frames, its parent should
5496 * be. */
5497 if (fr->fr_layout != FR_COL)
5498 {
5499 curfr = fr;
5500 if (fr != topframe) /* only a row of windows, may drag statusline */
5501 fr = fr->fr_parent;
5502 }
5503 }
5504
5505 /* If this is the last frame in a column, may want to resize the parent
5506 * frame instead (go two up to skip a row of frames). */
5507 while (curfr != topframe && curfr->fr_next == NULL)
5508 {
5509 if (fr != topframe)
5510 fr = fr->fr_parent;
5511 curfr = fr;
5512 if (fr != topframe)
5513 fr = fr->fr_parent;
5514 }
5515
5516 if (offset < 0) /* drag up */
5517 {
5518 up = TRUE;
5519 offset = -offset;
5520 /* sum up the room of the current frame and above it */
5521 if (fr == curfr)
5522 {
5523 /* only one window */
5524 room = fr->fr_height - frame_minheight(fr, NULL);
5525 }
5526 else
5527 {
5528 room = 0;
5529 for (fr = fr->fr_child; ; fr = fr->fr_next)
5530 {
5531 room += fr->fr_height - frame_minheight(fr, NULL);
5532 if (fr == curfr)
5533 break;
5534 }
5535 }
5536 fr = curfr->fr_next; /* put fr at frame that grows */
5537 }
5538 else /* drag down */
5539 {
5540 up = FALSE;
5541 /*
5542 * Only dragging the last status line can reduce p_ch.
5543 */
5544 room = Rows - cmdline_row;
5545 if (curfr->fr_next == NULL)
5546 room -= 1;
5547 else
5548 room -= p_ch;
5549 if (room < 0)
5550 room = 0;
5551 /* sum up the room of frames below of the current one */
5552 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5553 room += fr->fr_height - frame_minheight(fr, NULL);
5554 fr = curfr; /* put fr at window that grows */
5555 }
5556
5557 if (room < offset) /* Not enough room */
5558 offset = room; /* Move as far as we can */
5559 if (offset <= 0)
5560 return;
5561
5562 /*
5563 * Grow frame fr by "offset" lines.
5564 * Doesn't happen when dragging the last status line up.
5565 */
5566 if (fr != NULL)
5567 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5568
5569 if (up)
5570 fr = curfr; /* current frame gets smaller */
5571 else
5572 fr = curfr->fr_next; /* next frame gets smaller */
5573
5574 /*
5575 * Now make the other frames smaller.
5576 */
5577 while (fr != NULL && offset > 0)
5578 {
5579 n = frame_minheight(fr, NULL);
5580 if (fr->fr_height - offset <= n)
5581 {
5582 offset -= fr->fr_height - n;
5583 frame_new_height(fr, n, !up, FALSE);
5584 }
5585 else
5586 {
5587 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5588 break;
5589 }
5590 if (up)
5591 fr = fr->fr_prev;
5592 else
5593 fr = fr->fr_next;
5594 }
5595 row = win_comp_pos();
5596 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5597 cmdline_row = row;
5598 p_ch = Rows - cmdline_row;
5599 if (p_ch < 1)
5600 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005601 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005602 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 showmode();
5604}
5605
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606/*
5607 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5608 */
5609 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005610win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611{
5612 frame_T *curfr;
5613 frame_T *fr;
5614 int room;
5615 int left; /* if TRUE, drag separator line left, otherwise right */
5616 int n;
5617
5618 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005619 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 return;
5621 curfr = fr;
5622 fr = fr->fr_parent;
5623 /* When the parent frame is not a row of frames, its parent should be. */
5624 if (fr->fr_layout != FR_ROW)
5625 {
5626 if (fr == topframe) /* only a column of windows (cannot happen?) */
5627 return;
5628 curfr = fr;
5629 fr = fr->fr_parent;
5630 }
5631
5632 /* If this is the last frame in a row, may want to resize a parent
5633 * frame instead. */
5634 while (curfr->fr_next == NULL)
5635 {
5636 if (fr == topframe)
5637 break;
5638 curfr = fr;
5639 fr = fr->fr_parent;
5640 if (fr != topframe)
5641 {
5642 curfr = fr;
5643 fr = fr->fr_parent;
5644 }
5645 }
5646
5647 if (offset < 0) /* drag left */
5648 {
5649 left = TRUE;
5650 offset = -offset;
5651 /* sum up the room of the current frame and left of it */
5652 room = 0;
5653 for (fr = fr->fr_child; ; fr = fr->fr_next)
5654 {
5655 room += fr->fr_width - frame_minwidth(fr, NULL);
5656 if (fr == curfr)
5657 break;
5658 }
5659 fr = curfr->fr_next; /* put fr at frame that grows */
5660 }
5661 else /* drag right */
5662 {
5663 left = FALSE;
5664 /* sum up the room of frames right of the current one */
5665 room = 0;
5666 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5667 room += fr->fr_width - frame_minwidth(fr, NULL);
5668 fr = curfr; /* put fr at window that grows */
5669 }
5670
5671 if (room < offset) /* Not enough room */
5672 offset = room; /* Move as far as we can */
5673 if (offset <= 0) /* No room at all, quit. */
5674 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005675 if (fr == NULL)
5676 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677
5678 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005679 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680
5681 /* shrink other frames: current and at the left or at the right */
5682 if (left)
5683 fr = curfr; /* current frame gets smaller */
5684 else
5685 fr = curfr->fr_next; /* next frame gets smaller */
5686
5687 while (fr != NULL && offset > 0)
5688 {
5689 n = frame_minwidth(fr, NULL);
5690 if (fr->fr_width - offset <= n)
5691 {
5692 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005693 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
5695 else
5696 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005697 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 break;
5699 }
5700 if (left)
5701 fr = fr->fr_prev;
5702 else
5703 fr = fr->fr_next;
5704 }
5705 (void)win_comp_pos();
5706 redraw_all_later(NOT_VALID);
5707}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708#endif /* FEAT_MOUSE */
5709
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005710#define FRACTION_MULT 16384L
5711
5712/*
5713 * Set wp->w_fraction for the current w_wrow and w_height.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005714 * Has no effect when the window is less than two lines.
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005715 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005716 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005717set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005718{
Bram Moolenaar3679c172017-11-22 22:22:11 +01005719 if (wp->w_height > 1)
5720 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005721 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005722}
5723
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724/*
5725 * Set the height of a window.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005726 * "height" excludes any window toolbar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 * This takes care of the things inside the window, not what happens to the
5728 * window position, the frame or to other windows.
5729 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005730 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005731win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732{
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005733 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
5735 /* Don't want a negative height. Happens when splitting a tiny window.
5736 * Will equalize heights soon to fix it. */
5737 if (height < 0)
5738 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005739 if (wp->w_height == height)
5740 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005742 if (wp->w_height > 0)
5743 {
5744 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005745 /* w_wrow needs to be valid. When setting 'laststatus' this may
5746 * call win_new_height() recursively. */
5747 validate_cursor();
5748 if (wp->w_height != prev_height)
5749 return; /* Recursive call already changed the size, bail out here
5750 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005751 if (wp->w_wrow != wp->w_prev_fraction_row)
5752 set_fraction(wp);
5753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
5755 wp->w_height = height;
5756 wp->w_skipcol = 0;
5757
Bram Moolenaar955f1982017-02-05 15:10:51 +01005758 /* There is no point in adjusting the scroll position when exiting. Some
5759 * values might be invalid. */
5760 if (!exiting)
5761 scroll_to_fraction(wp, prev_height);
Bram Moolenaar46328f92016-08-28 15:39:57 +02005762}
5763
5764 void
5765scroll_to_fraction(win_T *wp, int prev_height)
5766{
5767 linenr_T lnum;
5768 int sline, line_size;
5769 int height = wp->w_height;
5770
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 /* Don't change w_topline when height is zero. Don't set w_topline when
5772 * 'scrollbind' is set and this isn't the current window. */
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005773 if (height > 0 && (!wp->w_p_scb || wp == curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005775 /*
5776 * Find a value for w_topline that shows the cursor at the same
5777 * relative position in the window as before (more or less).
5778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 lnum = wp->w_cursor.lnum;
5780 if (lnum < 1) /* can happen when starting up */
5781 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005782 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5783 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5785 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005786
5787 if (sline >= 0)
5788 {
5789 /* Make sure the whole cursor line is visible, if possible. */
5790 int rows = plines_win(wp, lnum, FALSE);
5791
5792 if (sline > wp->w_height - rows)
5793 {
5794 sline = wp->w_height - rows;
5795 wp->w_wrow -= rows - line_size;
5796 }
5797 }
5798
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 if (sline < 0)
5800 {
5801 /*
5802 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005803 * Make cursor line the first line in the window. If not enough
5804 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 */
5806 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005807 if (wp->w_wrow >= wp->w_height
Bram Moolenaar02631462017-09-22 15:20:32 +02005808 && (wp->w_width - win_col_off(wp)) > 0)
Bram Moolenaar26470632006-10-24 19:12:40 +00005809 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005810 wp->w_skipcol += wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00005811 --wp->w_wrow;
5812 while (wp->w_wrow >= wp->w_height)
5813 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005814 wp->w_skipcol += wp->w_width - win_col_off(wp)
Bram Moolenaar26470632006-10-24 19:12:40 +00005815 + win_col_off2(wp);
5816 --wp->w_wrow;
5817 }
5818 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005819 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005821 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005823 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 {
5825#ifdef FEAT_FOLDING
5826 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5827 if (lnum == 1)
5828 {
5829 /* first line in buffer is folded */
5830 line_size = 1;
5831 --sline;
5832 break;
5833 }
5834#endif
5835 --lnum;
5836#ifdef FEAT_DIFF
5837 if (lnum == wp->w_topline)
5838 line_size = plines_win_nofill(wp, lnum, TRUE)
5839 + wp->w_topfill;
5840 else
5841#endif
5842 line_size = plines_win(wp, lnum, TRUE);
5843 sline -= line_size;
5844 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005845
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 if (sline < 0)
5847 {
5848 /*
5849 * Line we want at top would go off top of screen. Use next
5850 * line instead.
5851 */
5852#ifdef FEAT_FOLDING
5853 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5854#endif
5855 lnum++;
5856 wp->w_wrow -= line_size + sline;
5857 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005858 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 {
5860 /* First line of file reached, use that as topline. */
5861 lnum = 1;
5862 wp->w_wrow -= sline;
5863 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005864
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005865 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 }
5868
5869 if (wp == curwin)
5870 {
5871 if (p_so)
5872 update_topline();
5873 curs_columns(FALSE); /* validate w_wrow */
5874 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005875 if (prev_height > 0)
5876 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877
5878 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005879 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 invalidate_botline_win(wp);
5882}
5883
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884/*
5885 * Set the width of a window.
5886 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005887 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005888win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889{
5890 wp->w_width = width;
5891 wp->w_lines_valid = 0;
5892 changed_line_abv_curs_win(wp);
5893 invalidate_botline_win(wp);
5894 if (wp == curwin)
5895 {
5896 update_topline();
5897 curs_columns(TRUE); /* validate w_wrow */
5898 }
5899 redraw_win_later(wp, NOT_VALID);
5900 wp->w_redr_status = TRUE;
5901}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902
5903 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005904win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905{
5906 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5907 if (wp->w_p_scr == 0)
5908 wp->w_p_scr = 1;
5909}
5910
5911/*
5912 * command_height: called whenever p_ch has been changed
5913 */
5914 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005915command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 int h;
5918 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005919 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005921 /* Use the value of p_ch that we remembered. This is needed for when the
5922 * GUI starts up, we can't be sure in what order things happen. And when
5923 * p_ch was changed in another tab page. */
5924 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005925
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 /* Find bottom frame with width of screen. */
5927 frp = lastwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5929 frp = frp->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930
5931 /* Avoid changing the height of a window with 'winfixheight' set. */
5932 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5933 && frp->fr_win->w_p_wfh)
5934 frp = frp->fr_prev;
5935
5936 if (starting != NO_SCREEN)
5937 {
5938 cmdline_row = Rows - p_ch;
5939
5940 if (p_ch > old_p_ch) /* p_ch got bigger */
5941 {
5942 while (p_ch > old_p_ch)
5943 {
5944 if (frp == NULL)
5945 {
5946 EMSG(_(e_noroom));
5947 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005948 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 cmdline_row = Rows - p_ch;
5950 break;
5951 }
5952 h = frp->fr_height - frame_minheight(frp, NULL);
5953 if (h > p_ch - old_p_ch)
5954 h = p_ch - old_p_ch;
5955 old_p_ch += h;
5956 frame_add_height(frp, -h);
5957 frp = frp->fr_prev;
5958 }
5959
5960 /* Recompute window positions. */
5961 (void)win_comp_pos();
5962
5963 /* clear the lines added to cmdline */
5964 if (full_screen)
5965 screen_fill((int)(cmdline_row), (int)Rows, 0,
5966 (int)Columns, ' ', ' ', 0);
5967 msg_row = cmdline_row;
5968 redraw_cmdline = TRUE;
5969 return;
5970 }
5971
5972 if (msg_row < cmdline_row)
5973 msg_row = cmdline_row;
5974 redraw_cmdline = TRUE;
5975 }
5976 frame_add_height(frp, (int)(old_p_ch - p_ch));
5977
5978 /* Recompute window positions. */
5979 if (frp != lastwin->w_frame)
5980 (void)win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981}
5982
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983/*
5984 * Resize frame "frp" to be "n" lines higher (negative for less high).
5985 * Also resize the frames it is contained in.
5986 */
5987 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005988frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989{
5990 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5991 for (;;)
5992 {
5993 frp = frp->fr_parent;
5994 if (frp == NULL)
5995 break;
5996 frp->fr_height += n;
5997 }
5998}
5999
6000/*
6001 * Add or remove a status line for the bottom window(s), according to the
6002 * value of 'laststatus'.
6003 */
6004 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006005last_status(
6006 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007{
6008 /* Don't make a difference between horizontal or vertical split. */
6009 last_status_rec(topframe, (p_ls == 2
Bram Moolenaar459ca562016-11-10 18:16:33 +01006010 || (p_ls == 1 && (morewin || !ONE_WINDOW))));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011}
6012
6013 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006014last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015{
6016 frame_T *fp;
6017 win_T *wp;
6018
6019 if (fr->fr_layout == FR_LEAF)
6020 {
6021 wp = fr->fr_win;
6022 if (wp->w_status_height != 0 && !statusline)
6023 {
6024 /* remove status line */
6025 win_new_height(wp, wp->w_height + 1);
6026 wp->w_status_height = 0;
6027 comp_col();
6028 }
6029 else if (wp->w_status_height == 0 && statusline)
6030 {
6031 /* Find a frame to take a line from. */
6032 fp = fr;
6033 while (fp->fr_height <= frame_minheight(fp, NULL))
6034 {
6035 if (fp == topframe)
6036 {
6037 EMSG(_(e_noroom));
6038 return;
6039 }
6040 /* In a column of frames: go to frame above. If already at
6041 * the top or in a row of frames: go to parent. */
6042 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6043 fp = fp->fr_prev;
6044 else
6045 fp = fp->fr_parent;
6046 }
6047 wp->w_status_height = 1;
6048 if (fp != fr)
6049 {
6050 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6051 frame_fix_height(wp);
6052 (void)win_comp_pos();
6053 }
6054 else
6055 win_new_height(wp, wp->w_height - 1);
6056 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006057 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 }
6059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 else if (fr->fr_layout == FR_ROW)
6061 {
6062 /* vertically split windows, set status line for each one */
6063 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6064 last_status_rec(fp, statusline);
6065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 else
6067 {
6068 /* horizontally split window, set status line for last one */
6069 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6070 ;
6071 last_status_rec(fp, statusline);
6072 }
6073}
6074
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006075/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006076 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006077 */
6078 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006079tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006080{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006081#ifdef FEAT_GUI_TABLINE
6082 /* When the GUI has the tabline then this always returns zero. */
6083 if (gui_use_tabline())
6084 return 0;
6085#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006086 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006087 {
6088 case 0: return 0;
6089 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6090 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006091 return 1;
6092}
6093
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6095/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006096 * Get the file name at the cursor.
6097 * If Visual mode is active, use the selected text if it's in one line.
6098 * Returns the name in allocated memory, NULL for failure.
6099 */
6100 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006101grab_file_name(long count, linenr_T *file_lnum)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006102{
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006103 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
6104
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006105 if (VIsual_active)
6106 {
6107 int len;
6108 char_u *ptr;
6109
6110 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6111 return NULL;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006112 return find_file_name_in_path(ptr, len, options,
6113 count, curbuf->b_ffname);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006114 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006115 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006116}
6117
6118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 * Return the file name under or after the cursor.
6120 *
6121 * The 'path' option is searched if the file name is not absolute.
6122 * The string returned has been alloc'ed and should be freed by the caller.
6123 * NULL is returned if the file name or file is not found.
6124 *
6125 * options:
6126 * FNAME_MESS give error messages
6127 * FNAME_EXP expand to path
6128 * FNAME_HYP check for hypertext link
6129 * FNAME_INCL apply "includeexpr"
6130 */
6131 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006132file_name_at_cursor(int options, long count, linenr_T *file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133{
6134 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006135 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6136 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137}
6138
6139/*
6140 * Return the name of the file under or after ptr[col].
6141 * Otherwise like file_name_at_cursor().
6142 */
6143 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006144file_name_in_line(
6145 char_u *line,
6146 int col,
6147 int options,
6148 long count,
6149 char_u *rel_fname, /* file we are searching relative to */
6150 linenr_T *file_lnum) /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151{
6152 char_u *ptr;
6153 int len;
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +01006154 int in_type = TRUE;
6155 int is_url = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156
6157 /*
6158 * search forward for what could be the start of a file name
6159 */
6160 ptr = line + col;
6161 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006162 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 if (*ptr == NUL) /* nothing found */
6164 {
6165 if (options & FNAME_MESS)
6166 EMSG(_("E446: No file name under cursor"));
6167 return NULL;
6168 }
6169
6170 /*
6171 * Search backward for first char of the file name.
6172 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6173 */
6174 while (ptr > line)
6175 {
6176#ifdef FEAT_MBYTE
6177 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6178 ptr -= len + 1;
6179 else
6180#endif
6181 if (vim_isfilec(ptr[-1])
6182 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6183 --ptr;
6184 else
6185 break;
6186 }
6187
6188 /*
6189 * Search forward for the last char of the file name.
6190 * Also allow "://" when ':' is not in 'isfname'.
6191 */
6192 len = 0;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006193 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +01006194 || ((options & FNAME_HYP) && path_is_url(ptr + len))
6195 || (is_url && vim_strchr((char_u *)"?&=", ptr[len]) != NULL))
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006196 {
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +01006197 /* After type:// we also include ?, & and = as valid characters, so that
6198 * http://google.com?q=this&that=ok works. */
6199 if ((ptr[len] >= 'A' && ptr[len] <= 'Z') || (ptr[len] >= 'a' && ptr[len] <= 'z'))
6200 {
6201 if (in_type && path_is_url(ptr + len + 1))
6202 is_url = TRUE;
6203 }
6204 else
6205 in_type = FALSE;
6206
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006207 if (ptr[len] == '\\')
6208 /* Skip over the "\" in "\ ". */
6209 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210#ifdef FEAT_MBYTE
6211 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006212 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 else
6214#endif
6215 ++len;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
6218 /*
6219 * If there is trailing punctuation, remove it.
6220 * But don't remove "..", could be a directory name.
6221 */
6222 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6223 && ptr[len - 2] != '.')
6224 --len;
6225
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006226 if (file_lnum != NULL)
6227 {
6228 char_u *p;
6229
6230 /* Get the number after the file name and a separator character */
6231 p = ptr + len;
6232 p = skipwhite(p);
6233 if (*p != NUL)
6234 {
6235 if (!isdigit(*p))
6236 ++p; /* skip the separator */
6237 p = skipwhite(p);
6238 if (isdigit(*p))
6239 *file_lnum = (int)getdigits(&p);
6240 }
6241 }
6242
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6244}
6245
6246# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 static char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006248eval_includeexpr(char_u *ptr, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249{
6250 char_u *res;
6251
6252 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006253 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006254 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 set_vim_var_string(VV_FNAME, NULL, 0);
6256 return res;
6257}
6258#endif
6259
6260/*
6261 * Return the name of the file ptr[len] in 'path'.
6262 * Otherwise like file_name_at_cursor().
6263 */
6264 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006265find_file_name_in_path(
6266 char_u *ptr,
6267 int len,
6268 int options,
6269 long count,
6270 char_u *rel_fname) /* file we are searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271{
6272 char_u *file_name;
6273 int c;
6274# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6275 char_u *tofree = NULL;
6276
6277 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6278 {
6279 tofree = eval_includeexpr(ptr, len);
6280 if (tofree != NULL)
6281 {
6282 ptr = tofree;
6283 len = (int)STRLEN(ptr);
6284 }
6285 }
6286# endif
6287
6288 if (options & FNAME_EXP)
6289 {
6290 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6291 TRUE, rel_fname);
6292
6293# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6294 /*
6295 * If the file could not be found in a normal way, try applying
6296 * 'includeexpr' (unless done already).
6297 */
6298 if (file_name == NULL
6299 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6300 {
6301 tofree = eval_includeexpr(ptr, len);
6302 if (tofree != NULL)
6303 {
6304 ptr = tofree;
6305 len = (int)STRLEN(ptr);
6306 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6307 TRUE, rel_fname);
6308 }
6309 }
6310# endif
6311 if (file_name == NULL && (options & FNAME_MESS))
6312 {
6313 c = ptr[len];
6314 ptr[len] = NUL;
6315 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6316 ptr[len] = c;
6317 }
6318
6319 /* Repeat finding the file "count" times. This matters when it
6320 * appears several times in the path. */
6321 while (file_name != NULL && --count > 0)
6322 {
6323 vim_free(file_name);
6324 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6325 }
6326 }
6327 else
6328 file_name = vim_strnsave(ptr, len);
6329
6330# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6331 vim_free(tofree);
6332# endif
6333
6334 return file_name;
6335}
6336#endif /* FEAT_SEARCHPATH */
6337
6338/*
6339 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6340 * Also check for ":\\", which MS Internet Explorer accepts, return
6341 * URL_BACKSLASH.
6342 */
6343 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006344path_is_url(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345{
6346 if (STRNCMP(p, "://", (size_t)3) == 0)
6347 return URL_SLASH;
6348 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6349 return URL_BACKSLASH;
6350 return 0;
6351}
6352
6353/*
6354 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6355 * Return URL_BACKSLASH for "name:\\".
6356 * Return zero otherwise.
6357 */
6358 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006359path_with_url(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360{
6361 char_u *p;
6362
6363 for (p = fname; isalpha(*p); ++p)
6364 ;
6365 return path_is_url(p);
6366}
6367
6368/*
6369 * Return TRUE if "name" is a full (absolute) path name or URL.
6370 */
6371 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006372vim_isAbsName(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373{
6374 return (path_with_url(name) != 0 || mch_isFullName(name));
6375}
6376
6377/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006378 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 *
6380 * return FAIL for failure, OK otherwise
6381 */
6382 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006383vim_FullName(
6384 char_u *fname,
6385 char_u *buf,
6386 int len,
6387 int force) /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389 int retval = OK;
6390 int url;
6391
6392 *buf = NUL;
6393 if (fname == NULL)
6394 return FAIL;
6395
6396 url = path_with_url(fname);
6397 if (!url)
6398 retval = mch_FullName(fname, buf, len, force);
6399 if (url || retval == FAIL)
6400 {
6401 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006402 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 }
Bram Moolenaard0573012017-10-28 21:11:06 +02006404#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 slash_adjust(buf);
6406#endif
6407 return retval;
6408}
6409
6410/*
6411 * Return the minimal number of rows that is needed on the screen to display
6412 * the current number of windows.
6413 */
6414 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006415min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416{
6417 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006418 tabpage_T *tp;
6419 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420
6421 if (firstwin == NULL) /* not initialized yet */
6422 return MIN_LINES;
6423
Bram Moolenaarf740b292006-02-16 22:11:02 +00006424 total = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02006425 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006426 {
6427 n = frame_minheight(tp->tp_topframe, NULL);
6428 if (total < n)
6429 total = n;
6430 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006431 total += tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006432 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 return total;
6434}
6435
6436/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006437 * Return TRUE if there is only one window (in the current tab page), not
6438 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006439 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 */
6441 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006442only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 int count = 0;
6445 win_T *wp;
6446
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006447 /* If there is another tab page there always is another window. */
6448 if (first_tabpage->tp_next != NULL)
6449 return FALSE;
6450
Bram Moolenaar29323592016-07-24 22:04:11 +02006451 FOR_ALL_WINDOWS(wp)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006452 if (wp->w_buffer != NULL
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006453 && (!((bt_help(wp->w_buffer) && !bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454# ifdef FEAT_QUICKFIX
6455 || wp->w_p_pvw
6456# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006457 ) || wp == curwin) && wp != aucmd_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 ++count;
6459 return (count <= 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460}
6461
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462/*
6463 * Correct the cursor line number in other windows. Used after changing the
6464 * current buffer, and before applying autocommands.
6465 * When "do_curwin" is TRUE, also check current window.
6466 */
6467 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006468check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
6470 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006471 tabpage_T *tp;
6472
6473 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 {
6476 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6477 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6478 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6479 wp->w_topline = curbuf->b_ml.ml_line_count;
6480 }
6481}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482
6483/*
6484 * A snapshot of the window sizes, to restore them after closing the help
6485 * window.
6486 * Only these fields are used:
6487 * fr_layout
6488 * fr_width
6489 * fr_height
6490 * fr_next
6491 * fr_child
6492 * fr_win (only valid for the old curwin, NULL otherwise)
6493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494
6495/*
6496 * Create a snapshot of the current frame sizes.
6497 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006498 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006499make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006501 clear_snapshot(curtab, idx);
6502 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503}
6504
6505 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006506make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507{
6508 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6509 if (*frp == NULL)
6510 return;
6511 (*frp)->fr_layout = fr->fr_layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 (*frp)->fr_width = fr->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 (*frp)->fr_height = fr->fr_height;
6514 if (fr->fr_next != NULL)
6515 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6516 if (fr->fr_child != NULL)
6517 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6518 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6519 (*frp)->fr_win = curwin;
6520}
6521
6522/*
6523 * Remove any existing snapshot.
6524 */
6525 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006526clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006528 clear_snapshot_rec(tp->tp_snapshot[idx]);
6529 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530}
6531
6532 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006533clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534{
6535 if (fr != NULL)
6536 {
6537 clear_snapshot_rec(fr->fr_next);
6538 clear_snapshot_rec(fr->fr_child);
6539 vim_free(fr);
6540 }
6541}
6542
6543/*
6544 * Restore a previously created snapshot, if there is any.
6545 * This is only done if the screen size didn't change and the window layout is
6546 * still the same.
6547 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006548 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006549restore_snapshot(
6550 int idx,
6551 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552{
6553 win_T *wp;
6554
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006555 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006556 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006557 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6558 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006560 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 win_comp_pos();
6562 if (wp != NULL && close_curwin)
6563 win_goto(wp);
6564 redraw_all_later(CLEAR);
6565 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006566 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567}
6568
6569/*
6570 * Check if frames "sn" and "fr" have the same layout, same following frames
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006571 * and same children. And the window pointer is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 */
6573 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006574check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575{
6576 if (sn->fr_layout != fr->fr_layout
6577 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6578 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6579 || (sn->fr_next != NULL
6580 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6581 || (sn->fr_child != NULL
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006582 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01006583 || (sn->fr_win != NULL && !win_valid(sn->fr_win)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 return FAIL;
6585 return OK;
6586}
6587
6588/*
6589 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6590 * following frames and children.
6591 * Returns a pointer to the old current window, or NULL.
6592 */
6593 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006594restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595{
6596 win_T *wp = NULL;
6597 win_T *wp2;
6598
6599 fr->fr_height = sn->fr_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 fr->fr_width = sn->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 if (fr->fr_layout == FR_LEAF)
6602 {
6603 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006604 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 wp = sn->fr_win;
6606 }
6607 if (sn->fr_next != NULL)
6608 {
6609 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6610 if (wp2 != NULL)
6611 wp = wp2;
6612 }
6613 if (sn->fr_child != NULL)
6614 {
6615 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6616 if (wp2 != NULL)
6617 wp = wp2;
6618 }
6619 return wp;
6620}
6621
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006622#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6623 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006624/*
6625 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006626 * restore_win() MUST be called to undo, also when FAIL is returned.
6627 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006628 * When "no_display" is TRUE the display won't be affected, no redraw is
6629 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006630 * Returns FAIL if switching to "win" failed.
6631 */
6632 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006633switch_win(
Bram Moolenaar816968d2017-09-29 21:29:18 +02006634 win_T **save_curwin,
6635 tabpage_T **save_curtab,
6636 win_T *win,
6637 tabpage_T *tp,
6638 int no_display)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006639{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006640 block_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006641 *save_curwin = curwin;
6642 if (tp != NULL)
6643 {
6644 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006645 if (no_display)
6646 {
6647 curtab->tp_firstwin = firstwin;
6648 curtab->tp_lastwin = lastwin;
6649 curtab = tp;
6650 firstwin = curtab->tp_firstwin;
6651 lastwin = curtab->tp_lastwin;
6652 }
6653 else
6654 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006655 }
6656 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006657 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006658 curwin = win;
6659 curbuf = curwin->w_buffer;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006660 return OK;
6661}
6662
6663/*
6664 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006665 * When "no_display" is TRUE the display won't be affected, no redraw is
6666 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006667 */
6668 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006669restore_win(
6670 win_T *save_curwin UNUSED,
6671 tabpage_T *save_curtab UNUSED,
6672 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006673{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006674 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006675 {
6676 if (no_display)
6677 {
6678 curtab->tp_firstwin = firstwin;
6679 curtab->tp_lastwin = lastwin;
6680 curtab = save_curtab;
6681 firstwin = curtab->tp_firstwin;
6682 lastwin = curtab->tp_lastwin;
6683 }
6684 else
6685 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6686 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006687 if (win_valid(save_curwin))
6688 {
6689 curwin = save_curwin;
6690 curbuf = curwin->w_buffer;
6691 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006692 unblock_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006693}
6694
6695/*
6696 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6697 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6698 */
6699 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006700switch_buffer(bufref_T *save_curbuf, buf_T *buf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006701{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006702 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006703 set_bufref(save_curbuf, curbuf);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006704 --curbuf->b_nwindows;
6705 curbuf = buf;
6706 curwin->w_buffer = buf;
6707 ++curbuf->b_nwindows;
6708}
6709
6710/*
6711 * Restore the current buffer after using switch_buffer().
6712 */
6713 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006714restore_buffer(bufref_T *save_curbuf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006715{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006716 unblock_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006717 /* Check for valid buffer, just in case. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006718 if (bufref_valid(save_curbuf))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006719 {
6720 --curbuf->b_nwindows;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006721 curwin->w_buffer = save_curbuf->br_buf;
6722 curbuf = save_curbuf->br_buf;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006723 ++curbuf->b_nwindows;
6724 }
6725}
6726#endif
6727
Bram Moolenaar4033c552017-09-16 20:54:51 +02006728#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729/*
6730 * Return TRUE if there is any vertically split window.
6731 */
6732 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006733win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
6735 frame_T *fr;
6736
6737 if (topframe->fr_layout == FR_ROW)
6738 return TRUE;
6739
6740 if (topframe->fr_layout == FR_COL)
6741 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6742 if (fr->fr_layout == FR_ROW)
6743 return TRUE;
6744
6745 return FALSE;
6746}
6747#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006748
6749#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6750/*
6751 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006752 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006753 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6754 * If no particular ID is desired, -1 must be specified for 'id'.
6755 * Return ID of added match, -1 on failure.
6756 */
6757 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006758match_add(
6759 win_T *wp,
6760 char_u *grp,
6761 char_u *pat,
6762 int prio,
6763 int id,
6764 list_T *pos_list,
6765 char_u *conceal_char UNUSED) /* pointer to conceal replacement char */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006766{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006767 matchitem_T *cur;
6768 matchitem_T *prev;
6769 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006770 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006771 regprog_T *regprog = NULL;
6772 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006773
Bram Moolenaarb3414592014-06-17 17:48:32 +02006774 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006775 return -1;
6776 if (id < -1 || id == 0)
6777 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02006778 EMSGN(_("E799: Invalid ID: %ld (must be greater than or equal to 1)"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006779 return -1;
6780 }
6781 if (id != -1)
6782 {
6783 cur = wp->w_match_head;
6784 while (cur != NULL)
6785 {
6786 if (cur->id == id)
6787 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02006788 EMSGN(_("E801: ID already taken: %ld"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006789 return -1;
6790 }
6791 cur = cur->next;
6792 }
6793 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006794 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006795 {
6796 EMSG2(_(e_nogroup), grp);
6797 return -1;
6798 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006799 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006800 {
6801 EMSG2(_(e_invarg2), pat);
6802 return -1;
6803 }
6804
6805 /* Find available match ID. */
6806 while (id == -1)
6807 {
6808 cur = wp->w_match_head;
6809 while (cur != NULL && cur->id != wp->w_next_match_id)
6810 cur = cur->next;
6811 if (cur == NULL)
6812 id = wp->w_next_match_id;
6813 wp->w_next_match_id++;
6814 }
6815
6816 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006817 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006818 m->id = id;
6819 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006820 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006821 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006822 m->match.regprog = regprog;
6823 m->match.rmm_ic = FALSE;
6824 m->match.rmm_maxcol = 0;
Bram Moolenaar42356152016-03-31 22:27:40 +02006825# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +02006826 m->conceal_char = 0;
6827 if (conceal_char != NULL)
6828 m->conceal_char = (*mb_ptr2char)(conceal_char);
Bram Moolenaar42356152016-03-31 22:27:40 +02006829# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006830
Bram Moolenaarb3414592014-06-17 17:48:32 +02006831 /* Set up position matches */
6832 if (pos_list != NULL)
6833 {
6834 linenr_T toplnum = 0;
6835 linenr_T botlnum = 0;
6836 listitem_T *li;
6837 int i;
6838
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006839 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006840 i++, li = li->li_next)
6841 {
6842 linenr_T lnum = 0;
6843 colnr_T col = 0;
6844 int len = 1;
6845 list_T *subl;
6846 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006847 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006848
Bram Moolenaarb3414592014-06-17 17:48:32 +02006849 if (li->li_tv.v_type == VAR_LIST)
6850 {
6851 subl = li->li_tv.vval.v_list;
6852 if (subl == NULL)
6853 goto fail;
6854 subli = subl->lv_first;
6855 if (subli == NULL)
6856 goto fail;
6857 lnum = get_tv_number_chk(&subli->li_tv, &error);
6858 if (error == TRUE)
6859 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006860 if (lnum == 0)
6861 {
6862 --i;
6863 continue;
6864 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006865 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006866 subli = subli->li_next;
6867 if (subli != NULL)
6868 {
6869 col = get_tv_number_chk(&subli->li_tv, &error);
6870 if (error == TRUE)
6871 goto fail;
6872 subli = subli->li_next;
6873 if (subli != NULL)
6874 {
6875 len = get_tv_number_chk(&subli->li_tv, &error);
6876 if (error == TRUE)
6877 goto fail;
6878 }
6879 }
6880 m->pos.pos[i].col = col;
6881 m->pos.pos[i].len = len;
6882 }
6883 else if (li->li_tv.v_type == VAR_NUMBER)
6884 {
6885 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006886 {
6887 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006888 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006889 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006890 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6891 m->pos.pos[i].col = 0;
6892 m->pos.pos[i].len = 0;
6893 }
6894 else
6895 {
6896 EMSG(_("List or number required"));
6897 goto fail;
6898 }
6899 if (toplnum == 0 || lnum < toplnum)
6900 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006901 if (botlnum == 0 || lnum >= botlnum)
6902 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006903 }
6904
6905 /* Calculate top and bottom lines for redrawing area */
6906 if (toplnum != 0)
6907 {
6908 if (wp->w_buffer->b_mod_set)
6909 {
6910 if (wp->w_buffer->b_mod_top > toplnum)
6911 wp->w_buffer->b_mod_top = toplnum;
6912 if (wp->w_buffer->b_mod_bot < botlnum)
6913 wp->w_buffer->b_mod_bot = botlnum;
6914 }
6915 else
6916 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006917 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006918 wp->w_buffer->b_mod_top = toplnum;
6919 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006920 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006921 }
6922 m->pos.toplnum = toplnum;
6923 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006924 rtype = VALID;
6925 }
6926 }
6927
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006928 /* Insert new match. The match list is in ascending order with regard to
6929 * the match priorities. */
6930 cur = wp->w_match_head;
6931 prev = cur;
6932 while (cur != NULL && prio >= cur->priority)
6933 {
6934 prev = cur;
6935 cur = cur->next;
6936 }
6937 if (cur == prev)
6938 wp->w_match_head = m;
6939 else
6940 prev->next = m;
6941 m->next = cur;
6942
Bram Moolenaarb3414592014-06-17 17:48:32 +02006943 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006944 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006945
6946fail:
6947 vim_free(m);
6948 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006949}
6950
6951/*
6952 * Delete match with ID 'id' in the match list of window 'wp'.
6953 * Print error messages if 'perr' is TRUE.
6954 */
6955 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006956match_delete(win_T *wp, int id, int perr)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006957{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006958 matchitem_T *cur = wp->w_match_head;
6959 matchitem_T *prev = cur;
6960 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006961
6962 if (id < 1)
6963 {
6964 if (perr == TRUE)
Bram Moolenaar5b302912016-08-24 22:11:55 +02006965 EMSGN(_("E802: Invalid ID: %ld (must be greater than or equal to 1)"),
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006966 id);
6967 return -1;
6968 }
6969 while (cur != NULL && cur->id != id)
6970 {
6971 prev = cur;
6972 cur = cur->next;
6973 }
6974 if (cur == NULL)
6975 {
6976 if (perr == TRUE)
Bram Moolenaar5b302912016-08-24 22:11:55 +02006977 EMSGN(_("E803: ID not found: %ld"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006978 return -1;
6979 }
6980 if (cur == prev)
6981 wp->w_match_head = cur->next;
6982 else
6983 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006984 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006985 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006986 if (cur->pos.toplnum != 0)
6987 {
6988 if (wp->w_buffer->b_mod_set)
6989 {
6990 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
6991 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6992 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
6993 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
6994 }
6995 else
6996 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006997 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006998 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6999 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007000 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007001 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007002 rtype = VALID;
7003 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007004 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007005 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007006 return 0;
7007}
7008
7009/*
7010 * Delete all matches in the match list of window 'wp'.
7011 */
7012 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007013clear_matches(win_T *wp)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007014{
7015 matchitem_T *m;
7016
7017 while (wp->w_match_head != NULL)
7018 {
7019 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007020 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007021 vim_free(wp->w_match_head->pattern);
7022 vim_free(wp->w_match_head);
7023 wp->w_match_head = m;
7024 }
7025 redraw_later(SOME_VALID);
7026}
7027
7028/*
7029 * Get match from ID 'id' in window 'wp'.
7030 * Return NULL if match not found.
7031 */
7032 matchitem_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007033get_match(win_T *wp, int id)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007034{
7035 matchitem_T *cur = wp->w_match_head;
7036
7037 while (cur != NULL && cur->id != id)
7038 cur = cur->next;
7039 return cur;
7040}
7041#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007042
7043#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7044 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007045get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007046{
7047 int i = 1;
7048 win_T *w;
7049
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007050 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007051 ++i;
7052
7053 if (w == NULL)
7054 return 0;
7055 else
7056 return i;
7057}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007058
7059 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007060get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007061{
7062 int i = 1;
7063 tabpage_T *t;
7064
7065 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7066 ++i;
7067
7068 if (t == NULL)
7069 return 0;
7070 else
7071 return i;
7072}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007073#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007074
7075/*
7076 * Return TRUE if "topfrp" and its children are at the right height.
7077 */
7078 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007079frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007080{
7081 frame_T *frp;
7082
7083 if (topfrp->fr_height != height)
7084 return FALSE;
7085
7086 if (topfrp->fr_layout == FR_ROW)
7087 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7088 if (frp->fr_height != height)
7089 return FALSE;
7090
7091 return TRUE;
7092}
7093
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007094/*
7095 * Return TRUE if "topfrp" and its children are at the right width.
7096 */
7097 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007098frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007099{
7100 frame_T *frp;
7101
7102 if (topfrp->fr_width != width)
7103 return FALSE;
7104
7105 if (topfrp->fr_layout == FR_COL)
7106 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7107 if (frp->fr_width != width)
7108 return FALSE;
7109
7110 return TRUE;
7111}
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007112
Bram Moolenaar86edef62016-03-13 18:07:30 +01007113#if defined(FEAT_EVAL) || defined(PROTO)
7114 int
7115win_getid(typval_T *argvars)
7116{
7117 int winnr;
7118 win_T *wp;
7119
7120 if (argvars[0].v_type == VAR_UNKNOWN)
7121 return curwin->w_id;
7122 winnr = get_tv_number(&argvars[0]);
7123 if (winnr > 0)
7124 {
7125 if (argvars[1].v_type == VAR_UNKNOWN)
7126 wp = firstwin;
7127 else
7128 {
7129 tabpage_T *tp;
7130 int tabnr = get_tv_number(&argvars[1]);
7131
Bram Moolenaar29323592016-07-24 22:04:11 +02007132 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007133 if (--tabnr == 0)
7134 break;
7135 if (tp == NULL)
7136 return -1;
Bram Moolenaar8e639052016-11-13 14:31:40 +01007137 if (tp == curtab)
7138 wp = firstwin;
7139 else
7140 wp = tp->tp_firstwin;
Bram Moolenaar86edef62016-03-13 18:07:30 +01007141 }
7142 for ( ; wp != NULL; wp = wp->w_next)
7143 if (--winnr == 0)
7144 return wp->w_id;
7145 }
7146 return 0;
7147}
7148
7149 int
7150win_gotoid(typval_T *argvars)
7151{
7152 win_T *wp;
7153 tabpage_T *tp;
7154 int id = get_tv_number(&argvars[0]);
7155
Bram Moolenaar29323592016-07-24 22:04:11 +02007156 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007157 if (wp->w_id == id)
7158 {
7159 goto_tabpage_win(tp, wp);
7160 return 1;
7161 }
7162 return 0;
7163}
7164
7165 void
7166win_id2tabwin(typval_T *argvars, list_T *list)
7167{
7168 win_T *wp;
7169 tabpage_T *tp;
7170 int winnr = 1;
7171 int tabnr = 1;
7172 int id = get_tv_number(&argvars[0]);
7173
Bram Moolenaar29323592016-07-24 22:04:11 +02007174 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007175 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007176 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007177 {
7178 if (wp->w_id == id)
7179 {
7180 list_append_number(list, tabnr);
7181 list_append_number(list, winnr);
7182 return;
7183 }
7184 ++winnr;
7185 }
7186 ++tabnr;
7187 winnr = 1;
7188 }
7189 list_append_number(list, 0);
7190 list_append_number(list, 0);
7191}
7192
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007193 win_T *
7194win_id2wp(typval_T *argvars)
7195{
7196 win_T *wp;
7197 tabpage_T *tp;
7198 int id = get_tv_number(&argvars[0]);
7199
7200 FOR_ALL_TAB_WINDOWS(tp, wp)
7201 if (wp->w_id == id)
7202 return wp;
7203
7204 return NULL;
7205}
7206
Bram Moolenaar86edef62016-03-13 18:07:30 +01007207 int
7208win_id2win(typval_T *argvars)
7209{
7210 win_T *wp;
7211 int nr = 1;
7212 int id = get_tv_number(&argvars[0]);
7213
Bram Moolenaar29323592016-07-24 22:04:11 +02007214 FOR_ALL_WINDOWS(wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007215 {
7216 if (wp->w_id == id)
7217 return nr;
7218 ++nr;
7219 }
7220 return 0;
7221}
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007222
7223 void
7224win_findbuf(typval_T *argvars, list_T *list)
7225{
7226 win_T *wp;
7227 tabpage_T *tp;
7228 int bufnr = get_tv_number(&argvars[0]);
7229
Bram Moolenaar29323592016-07-24 22:04:11 +02007230 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007231 if (wp->w_buffer->b_fnum == bufnr)
7232 list_append_number(list, wp->w_id);
7233}
7234
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02007235/*
7236 * Get the layout of the given tab page for winlayout().
7237 */
7238 void
7239get_framelayout(frame_T *fr, list_T *l, int outer)
7240{
7241 frame_T *child;
7242 list_T *fr_list;
7243 list_T *win_list;
7244
7245 if (fr == NULL)
7246 return;
7247
7248 if (outer)
7249 // outermost call from f_winlayout()
7250 fr_list = l;
7251 else
7252 {
7253 fr_list = list_alloc();
7254 if (fr_list == NULL)
7255 return;
7256 list_append_list(l, fr_list);
7257 }
7258
7259 if (fr->fr_layout == FR_LEAF)
7260 {
7261 if (fr->fr_win != NULL)
7262 {
7263 list_append_string(fr_list, (char_u *)"leaf", -1);
7264 list_append_number(fr_list, fr->fr_win->w_id);
7265 }
7266 }
7267 else
7268 {
7269 list_append_string(fr_list,
7270 fr->fr_layout == FR_ROW ? (char_u *)"row" : (char_u *)"col", -1);
7271
7272 win_list = list_alloc();
7273 if (win_list == NULL)
7274 return;
7275 list_append_list(fr_list, win_list);
7276 child = fr->fr_child;
7277 while (child != NULL)
7278 {
7279 get_framelayout(child, win_list, FALSE);
7280 child = child->fr_next;
7281 }
7282 }
7283}
Bram Moolenaar86edef62016-03-13 18:07:30 +01007284#endif