blob: a1b2a45cc08a73754412755d6c55072d20a05a41 [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);
23static int last_window(void);
24static int close_last_window_tabpage(win_T *win, int free_buf, tabpage_T *prev_curtab);
25static win_T *win_free_mem(win_T *win, int *dirp, tabpage_T *tp);
26static frame_T *win_altframe(win_T *win, tabpage_T *tp);
27static tabpage_T *alt_tabpage(void);
28static win_T *frame2win(frame_T *frp);
29static int frame_has_win(frame_T *frp, win_T *wp);
30static void frame_new_height(frame_T *topfrp, int height, int topfirst, int wfh);
31static int frame_fixed_height(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010032static int frame_fixed_width(frame_T *frp);
33static void frame_add_statusline(frame_T *frp);
34static void frame_new_width(frame_T *topfrp, int width, int leftfirst, int wfw);
35static void frame_add_vsep(frame_T *frp);
36static int frame_minwidth(frame_T *topfrp, win_T *next_curwin);
37static void frame_fix_width(win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010038static int win_alloc_firstwin(win_T *oldwin);
39static void new_frame(win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010040static tabpage_T *alloc_tabpage(void);
41static int leave_tabpage(buf_T *new_curbuf, int trigger_leave_autocmds);
42static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds);
43static void frame_fix_height(win_T *wp);
44static int frame_minheight(frame_T *topfrp, win_T *next_curwin);
Bram Moolenaarc917da42016-07-19 22:31:36 +020045static 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 +010046static void win_free(win_T *wp, tabpage_T *tp);
47static void frame_append(frame_T *after, frame_T *frp);
48static void frame_insert(frame_T *before, frame_T *frp);
49static void frame_remove(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010050static void win_goto_ver(int up, long count);
51static void win_goto_hor(int left, long count);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void frame_add_height(frame_T *frp, int n);
53static void last_status_rec(frame_T *fr, int statusline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010055static void make_snapshot_rec(frame_T *fr, frame_T **frp);
56static void clear_snapshot(tabpage_T *tp, int idx);
57static void clear_snapshot_rec(frame_T *fr);
58static int check_snapshot_rec(frame_T *sn, frame_T *fr);
59static win_T *restore_snapshot_rec(frame_T *sn, frame_T *fr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010061static int frame_check_height(frame_T *topfrp, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010062static int frame_check_width(frame_T *topfrp, int width);
Bram Moolenaarb893ac22013-06-26 14:04:47 +020063
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static win_T *win_alloc(win_T *after, int hidden);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66#define URL_SLASH 1 /* path_is_url() has found "://" */
67#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
68
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000069#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaar4033c552017-09-16 20:54:51 +020071#define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000072
73static char *m_onlyone = N_("Already only one window");
74
Bram Moolenaar071d4272004-06-13 20:20:40 +000075/*
76 * all CTRL-W window commands are handled here, called from normal_cmd().
77 */
78 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +010079do_window(
80 int nchar,
81 long Prenum,
82 int xchar) /* extra char from ":wincmd gx" or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083{
84 long Prenum1;
85 win_T *wp;
86#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
87 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000088 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000089#endif
90#ifdef FEAT_FIND_ID
91 int type = FIND_DEFINE;
92 int len;
93#endif
94 char_u cbuf[40];
95
96 if (Prenum == 0)
97 Prenum1 = 1;
98 else
99 Prenum1 = Prenum;
100
101#ifdef FEAT_CMDWIN
Bram Moolenaar6f470022018-04-10 18:47:20 +0200102# define CHECK_CMDWIN \
103 do { \
104 if (cmdwin_type != 0) \
105 { \
106 EMSG(_(e_cmdwin)); \
107 return; \
108 } \
109 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110#else
Bram Moolenaar6f470022018-04-10 18:47:20 +0200111# define CHECK_CMDWIN do { /**/ } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#endif
113
114 switch (nchar)
115 {
116/* split current window in two parts, horizontally */
117 case 'S':
118 case Ctrl_S:
119 case 's':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200120 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000122#ifdef FEAT_QUICKFIX
123 /* When splitting the quickfix window open a new buffer in it,
124 * don't replicate the quickfix buffer. */
125 if (bt_quickfix(curbuf))
126 goto newwindow;
127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#ifdef FEAT_GUI
129 need_mouse_correct = TRUE;
130#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200131 (void)win_split((int)Prenum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 break;
133
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134/* split current window in two parts, vertically */
135 case Ctrl_V:
136 case 'v':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200137 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100139#ifdef FEAT_QUICKFIX
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000140 /* When splitting the quickfix window open a new buffer in it,
141 * don't replicate the quickfix buffer. */
142 if (bt_quickfix(curbuf))
143 goto newwindow;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100144#endif
145#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100147#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200148 (void)win_split((int)Prenum, WSP_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151/* split current window and edit alternate file */
152 case Ctrl_HAT:
153 case '^':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200154 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100156 cmd_with_count("split #", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 do_cmdline_cmd(cbuf);
158 break;
159
160/* open new window */
161 case Ctrl_N:
162 case 'n':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200163 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000165#ifdef FEAT_QUICKFIX
166newwindow:
167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000169 /* window height */
170 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 else
172 cbuf[0] = NUL;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100173#if defined(FEAT_QUICKFIX)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000174 if (nchar == 'v' || nchar == Ctrl_V)
175 STRCAT(cbuf, "v");
176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 STRCAT(cbuf, "new");
178 do_cmdline_cmd(cbuf);
179 break;
180
181/* quit current window */
182 case Ctrl_Q:
183 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100185 cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100186 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 break;
188
189/* close current window */
190 case Ctrl_C:
191 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100193 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100194 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 break;
196
Bram Moolenaar4033c552017-09-16 20:54:51 +0200197#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198/* close preview window */
199 case Ctrl_Z:
200 case 'z':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200201 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 do_cmdline_cmd((char_u *)"pclose");
204 break;
205
206/* cursor to preview window */
207 case 'P':
Bram Moolenaar29323592016-07-24 22:04:11 +0200208 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 if (wp->w_p_pvw)
210 break;
211 if (wp == NULL)
212 EMSG(_("E441: There is no preview window"));
213 else
214 win_goto(wp);
215 break;
216#endif
217
218/* close all but current window */
219 case Ctrl_O:
220 case 'o':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200221 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100223 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100224 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 break;
226
227/* cursor to next window with wrap around */
228 case Ctrl_W:
229 case 'w':
230/* cursor to previous window with wrap around */
231 case 'W':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200232 CHECK_CMDWIN;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +0100233 if (ONE_WINDOW && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 beep_flush();
235 else
236 {
237 if (Prenum) /* go to specified window */
238 {
239 for (wp = firstwin; --Prenum > 0; )
240 {
241 if (wp->w_next == NULL)
242 break;
243 else
244 wp = wp->w_next;
245 }
246 }
247 else
248 {
249 if (nchar == 'W') /* go to previous window */
250 {
251 wp = curwin->w_prev;
252 if (wp == NULL)
253 wp = lastwin; /* wrap around */
254 }
255 else /* go to next window */
256 {
257 wp = curwin->w_next;
258 if (wp == NULL)
259 wp = firstwin; /* wrap around */
260 }
261 }
262 win_goto(wp);
263 }
264 break;
265
266/* cursor to window below */
267 case 'j':
268 case K_DOWN:
269 case Ctrl_J:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200270 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 win_goto_ver(FALSE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 break;
273
274/* cursor to window above */
275 case 'k':
276 case K_UP:
277 case Ctrl_K:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200278 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 win_goto_ver(TRUE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 break;
281
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282/* cursor to left window */
283 case 'h':
284 case K_LEFT:
285 case Ctrl_H:
286 case K_BS:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200287 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 win_goto_hor(TRUE, Prenum1);
289 break;
290
291/* cursor to right window */
292 case 'l':
293 case K_RIGHT:
294 case Ctrl_L:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200295 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 win_goto_hor(FALSE, Prenum1);
297 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000299/* move window to new tab page */
300 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000301 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000302 MSG(_(m_onlyone));
303 else
304 {
305 tabpage_T *oldtab = curtab;
306 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000307
308 /* First create a new tab with the window, then go back to
309 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000310 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000311 if (win_new_tabpage((int)Prenum) == OK
312 && valid_tabpage(oldtab))
313 {
314 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200315 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000316 if (curwin == wp)
317 win_close(curwin, FALSE);
318 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200319 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000320 }
321 }
322 break;
323
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324/* cursor to top-left window */
325 case 't':
326 case Ctrl_T:
327 win_goto(firstwin);
328 break;
329
330/* cursor to bottom-right window */
331 case 'b':
332 case Ctrl_B:
333 win_goto(lastwin);
334 break;
335
336/* cursor to last accessed (previous) window */
337 case 'p':
338 case Ctrl_P:
Bram Moolenaar3dda7db2016-04-03 21:22:58 +0200339 if (!win_valid(prevwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 beep_flush();
341 else
342 win_goto(prevwin);
343 break;
344
345/* exchange current and next window */
346 case 'x':
347 case Ctrl_X:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200348 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 win_exchange(Prenum);
350 break;
351
352/* rotate windows downwards */
353 case Ctrl_R:
354 case 'r':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200355 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 win_rotate(FALSE, (int)Prenum1); /* downwards */
358 break;
359
360/* rotate windows upwards */
361 case 'R':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200362 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 win_rotate(TRUE, (int)Prenum1); /* upwards */
365 break;
366
367/* move window to the very top/bottom/left/right */
368 case 'K':
369 case 'J':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 case 'H':
371 case 'L':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200372 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 win_totop((int)Prenum,
374 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
375 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
376 break;
377
378/* make all windows the same height */
379 case '=':
380#ifdef FEAT_GUI
381 need_mouse_correct = TRUE;
382#endif
383 win_equal(NULL, FALSE, 'b');
384 break;
385
386/* increase current window height */
387 case '+':
388#ifdef FEAT_GUI
389 need_mouse_correct = TRUE;
390#endif
391 win_setheight(curwin->w_height + (int)Prenum1);
392 break;
393
394/* decrease current window height */
395 case '-':
396#ifdef FEAT_GUI
397 need_mouse_correct = TRUE;
398#endif
399 win_setheight(curwin->w_height - (int)Prenum1);
400 break;
401
402/* set current window height */
403 case Ctrl__:
404 case '_':
405#ifdef FEAT_GUI
406 need_mouse_correct = TRUE;
407#endif
408 win_setheight(Prenum ? (int)Prenum : 9999);
409 break;
410
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411/* increase current window width */
412 case '>':
413#ifdef FEAT_GUI
414 need_mouse_correct = TRUE;
415#endif
416 win_setwidth(curwin->w_width + (int)Prenum1);
417 break;
418
419/* decrease current window width */
420 case '<':
421#ifdef FEAT_GUI
422 need_mouse_correct = TRUE;
423#endif
424 win_setwidth(curwin->w_width - (int)Prenum1);
425 break;
426
427/* set current window width */
428 case '|':
429#ifdef FEAT_GUI
430 need_mouse_correct = TRUE;
431#endif
432 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
433 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434
435/* jump to tag and split window if tag exists (in preview window) */
436#if defined(FEAT_QUICKFIX)
437 case '}':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200438 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 if (Prenum)
440 g_do_tagpreview = Prenum;
441 else
442 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200444 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 case ']':
446 case Ctrl_RSB:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200447 CHECK_CMDWIN;
Bram Moolenaard355c502014-09-23 13:48:43 +0200448 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 if (Prenum)
450 postponed_split = Prenum;
451 else
452 postponed_split = -1;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200453#ifdef FEAT_QUICKFIX
Bram Moolenaar56095e12014-10-09 10:44:37 +0200454 if (nchar != '}')
455 g_do_tagpreview = 0;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457
Bram Moolenaard355c502014-09-23 13:48:43 +0200458 /* Execute the command right here, required when "wincmd ]"
459 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 do_nv_ident(Ctrl_RSB, NUL);
461 break;
462
463#ifdef FEAT_SEARCHPATH
464/* edit file name under cursor in a new window */
465 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000466 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000468wingotofile:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200469 CHECK_CMDWIN;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000470
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000471 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 if (ptr != NULL)
473 {
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200474 tabpage_T *oldtab = curtab;
475 win_T *oldwin = curwin;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000476# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000478# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 setpcmark();
480 if (win_split(0, 0) == OK)
481 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200482 RESET_BINDING(curwin);
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200483 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
484 ECMD_HIDE, NULL) == FAIL)
485 {
486 /* Failed to open the file, close the window
487 * opened for it. */
488 win_close(curwin, FALSE);
489 goto_tabpage_win(oldtab, oldwin);
490 }
491 else if (nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000492 {
493 curwin->w_cursor.lnum = lnum;
494 check_cursor_lnum();
495 beginline(BL_SOL | BL_FIX);
496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 }
498 vim_free(ptr);
499 }
500 break;
501#endif
502
503#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000504/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 * new window -- webb
506 */
507 case 'i': /* Go to any match */
508 case Ctrl_I:
509 type = FIND_ANY;
510 /* FALLTHROUGH */
511 case 'd': /* Go to definition, using 'define' */
512 case Ctrl_D:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200513 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
515 break;
516 find_pattern_in_path(ptr, 0, len, TRUE,
517 Prenum == 0 ? TRUE : FALSE, type,
518 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
519 curwin->w_set_curswant = TRUE;
520 break;
521#endif
522
Bram Moolenaar05159a02005-02-26 23:04:13 +0000523 case K_KENTER:
524 case CAR:
525#if defined(FEAT_QUICKFIX)
526 /*
527 * In a quickfix window a <CR> jumps to the error under the
528 * cursor in a new window.
529 */
530 if (bt_quickfix(curbuf))
531 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000532 sprintf((char *)cbuf, "split +%ld%s",
533 (long)curwin->w_cursor.lnum,
534 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000535 do_cmdline_cmd(cbuf);
536 }
537#endif
538 break;
539
540
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541/* CTRL-W g extended commands */
542 case 'g':
543 case Ctrl_G:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200544 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545#ifdef USE_ON_FLY_SCROLL
546 dont_scroll = TRUE; /* disallow scrolling here */
547#endif
548 ++no_mapping;
549 ++allow_keys; /* no mapping for xchar, but allow key codes */
550 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000551 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 --no_mapping;
554 --allow_keys;
555#ifdef FEAT_CMDL_INFO
556 (void)add_to_showcmd(xchar);
557#endif
558 switch (xchar)
559 {
560#if defined(FEAT_QUICKFIX)
561 case '}':
562 xchar = Ctrl_RSB;
563 if (Prenum)
564 g_do_tagpreview = Prenum;
565 else
566 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200568 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 case ']':
570 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200571 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 if (Prenum)
573 postponed_split = Prenum;
574 else
575 postponed_split = -1;
576
577 /* Execute the command right here, required when
578 * "wincmd g}" was used in a function. */
579 do_nv_ident('g', xchar);
580 break;
581
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000582#ifdef FEAT_SEARCHPATH
583 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000584 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100585 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000586 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000587 goto wingotofile;
588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 default:
590 beep_flush();
591 break;
592 }
593 break;
594
595 default: beep_flush();
596 break;
597 }
598}
599
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100600/*
601 * Figure out the address type for ":wnncmd".
602 */
603 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100604get_wincmd_addr_type(char_u *arg, exarg_T *eap)
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100605{
606 switch (*arg)
607 {
608 case 'S':
609 case Ctrl_S:
610 case 's':
611 case Ctrl_N:
612 case 'n':
613 case 'j':
614 case Ctrl_J:
615 case 'k':
616 case Ctrl_K:
617 case 'T':
618 case Ctrl_R:
619 case 'r':
620 case 'R':
621 case 'K':
622 case 'J':
623 case '+':
624 case '-':
625 case Ctrl__:
626 case '_':
627 case '|':
628 case ']':
629 case Ctrl_RSB:
630 case 'g':
631 case Ctrl_G:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100632 case Ctrl_V:
633 case 'v':
634 case 'h':
635 case Ctrl_H:
636 case 'l':
637 case Ctrl_L:
638 case 'H':
639 case 'L':
640 case '>':
641 case '<':
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100642#if defined(FEAT_QUICKFIX)
643 case '}':
644#endif
645#ifdef FEAT_SEARCHPATH
646 case 'f':
647 case 'F':
648 case Ctrl_F:
649#endif
650#ifdef FEAT_FIND_ID
651 case 'i':
652 case Ctrl_I:
653 case 'd':
654 case Ctrl_D:
655#endif
656 /* window size or any count */
657 eap->addr_type = ADDR_LINES;
658 break;
659
660 case Ctrl_HAT:
661 case '^':
662 /* buffer number */
663 eap->addr_type = ADDR_BUFFERS;
664 break;
665
666 case Ctrl_Q:
667 case 'q':
668 case Ctrl_C:
669 case 'c':
670 case Ctrl_O:
671 case 'o':
672 case Ctrl_W:
673 case 'w':
674 case 'W':
675 case 'x':
676 case Ctrl_X:
677 /* window number */
678 eap->addr_type = ADDR_WINDOWS;
679 break;
680
681#if defined(FEAT_QUICKFIX)
682 case Ctrl_Z:
683 case 'z':
684 case 'P':
685#endif
686 case 't':
687 case Ctrl_T:
688 case 'b':
689 case Ctrl_B:
690 case 'p':
691 case Ctrl_P:
692 case '=':
693 case CAR:
694 /* no count */
695 eap->addr_type = 0;
696 break;
697 }
698}
699
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100700 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100701cmd_with_count(
702 char *cmd,
703 char_u *bufp,
704 size_t bufsize,
705 long Prenum)
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100706{
707 size_t len = STRLEN(cmd);
708
709 STRCPY(bufp, cmd);
710 if (Prenum > 0)
711 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
712}
713
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714/*
715 * split the current window, implements CTRL-W s and :split
716 *
717 * "size" is the height or width for the new window, 0 to use half of current
718 * height or width.
719 *
720 * "flags":
721 * WSP_ROOM: require enough room for new window
722 * WSP_VERT: vertical split.
723 * WSP_TOP: open window at the top-left of the shell (help window).
724 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
725 * WSP_HELP: creating the help window, keep layout snapshot
726 *
727 * return FAIL for failure, OK otherwise
728 */
729 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100730win_split(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000732 /* When the ":tab" modifier was used open a new tab page instead. */
733 if (may_open_tabpage() == OK)
734 return OK;
735
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 /* Add flags from ":vertical", ":topleft" and ":botright". */
737 flags |= cmdmod.split;
738 if ((flags & WSP_TOP) && (flags & WSP_BOT))
739 {
740 EMSG(_("E442: Can't split topleft and botright at the same time"));
741 return FAIL;
742 }
743
744 /* When creating the help window make a snapshot of the window layout.
745 * Otherwise clear the snapshot, it's now invalid. */
746 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000747 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000749 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750
751 return win_split_ins(size, flags, NULL, 0);
752}
753
754/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100755 * When "new_wp" is NULL: split the current window in two.
756 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 * top/left/right/bottom.
758 * return FAIL for failure, OK otherwise
759 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000760 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100761win_split_ins(
762 int size,
763 int flags,
764 win_T *new_wp,
765 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100767 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 win_T *oldwin;
769 int new_size = size;
770 int i;
771 int need_status = 0;
772 int do_equal = FALSE;
773 int needed;
774 int available;
775 int oldwin_height = 0;
776 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200777 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200779 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200780 int wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781
782 if (flags & WSP_TOP)
783 oldwin = firstwin;
784 else if (flags & WSP_BOT)
785 oldwin = lastwin;
786 else
787 oldwin = curwin;
788
789 /* add a status line when p_ls == 1 and splitting the first window */
Bram Moolenaar459ca562016-11-10 18:16:33 +0100790 if (ONE_WINDOW && p_ls == 1 && oldwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 {
Bram Moolenaar415a6932017-12-05 20:31:07 +0100792 if (VISIBLE_HEIGHT(oldwin) <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {
794 EMSG(_(e_noroom));
795 return FAIL;
796 }
797 need_status = STATUS_HEIGHT;
798 }
799
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000800#ifdef FEAT_GUI
801 /* May be needed for the scrollbars that are going to change. */
802 if (gui.in_use)
803 out_flush();
804#endif
805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (flags & WSP_VERT)
807 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200808 int wmw1;
809 int minwidth;
810
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
813 /*
814 * Check if we are able to split the current window and compute its
815 * width.
816 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200817 /* Current window requires at least 1 space. */
818 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
819 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200821 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200822 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200824 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200826 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200828 else if (p_ea)
829 {
830 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
831 prevfrp = oldwin->w_frame;
832 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
833 frp = frp->fr_parent)
834 {
835 if (frp->fr_layout == FR_ROW)
836 for (frp2 = frp->fr_child; frp2 != NULL;
837 frp2 = frp2->fr_next)
838 if (frp2 != prevfrp)
839 minwidth += frame_minwidth(frp2, NOWIN);
840 prevfrp = frp;
841 }
842 available = topframe->fr_width;
843 needed += minwidth;
844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200846 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200847 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
848 available = oldwin->w_frame->fr_width;
849 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200850 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100851 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
853 EMSG(_(e_noroom));
854 return FAIL;
855 }
856 if (new_size == 0)
857 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200858 if (new_size > available - minwidth - 1)
859 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200860 if (new_size < wmw1)
861 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862
863 /* if it doesn't fit in the current window, need win_equal() */
864 if (oldwin->w_width - new_size - 1 < p_wmw)
865 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000866
867 /* We don't like to take lines for the new window from a
868 * 'winfixwidth' window. Take them from a window to the left or right
Bram Moolenaar38e34832017-03-19 20:22:36 +0100869 * instead, if possible. Add one for the separator. */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000870 if (oldwin->w_p_wfw)
Bram Moolenaar38e34832017-03-19 20:22:36 +0100871 win_setwidth_win(oldwin->w_width + new_size + 1, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000872
873 /* Only make all windows the same width if one of them (except oldwin)
874 * is wider than one of the split windows. */
875 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
876 && oldwin->w_frame->fr_parent != NULL)
877 {
878 frp = oldwin->w_frame->fr_parent->fr_child;
879 while (frp != NULL)
880 {
881 if (frp->fr_win != oldwin && frp->fr_win != NULL
882 && (frp->fr_win->w_width > new_size
883 || frp->fr_win->w_width > oldwin->w_width
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100884 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000885 {
886 do_equal = TRUE;
887 break;
888 }
889 frp = frp->fr_next;
890 }
891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 }
893 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 {
895 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
897 /*
898 * Check if we are able to split the current window and compute its
899 * height.
900 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200901 /* Current window requires at least 1 space. */
Bram Moolenaar415a6932017-12-05 20:31:07 +0100902 wmh1 = (p_wmh == 0 ? 1 : p_wmh) + WINBAR_HEIGHT(curwin);
Bram Moolenaar1f538352014-07-16 18:19:27 +0200903 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200905 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200906 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200908 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200910 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200912 else if (p_ea)
913 {
914 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
915 prevfrp = oldwin->w_frame;
916 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
917 frp = frp->fr_parent)
918 {
919 if (frp->fr_layout == FR_COL)
920 for (frp2 = frp->fr_child; frp2 != NULL;
921 frp2 = frp2->fr_next)
922 if (frp2 != prevfrp)
923 minheight += frame_minheight(frp2, NOWIN);
924 prevfrp = frp;
925 }
926 available = topframe->fr_height;
927 needed += minheight;
928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 else
930 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200931 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
932 available = oldwin->w_frame->fr_height;
933 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100935 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 {
937 EMSG(_(e_noroom));
938 return FAIL;
939 }
940 oldwin_height = oldwin->w_height;
941 if (need_status)
942 {
943 oldwin->w_status_height = STATUS_HEIGHT;
944 oldwin_height -= STATUS_HEIGHT;
945 }
946 if (new_size == 0)
947 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200948 if (new_size > available - minheight - STATUS_HEIGHT)
949 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200950 if (new_size < wmh1)
951 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952
953 /* if it doesn't fit in the current window, need win_equal() */
954 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
955 do_equal = TRUE;
956
957 /* We don't like to take lines for the new window from a
958 * 'winfixheight' window. Take them from a window above or below
959 * instead, if possible. */
960 if (oldwin->w_p_wfh)
961 {
962 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
963 oldwin);
964 oldwin_height = oldwin->w_height;
965 if (need_status)
966 oldwin_height -= STATUS_HEIGHT;
967 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000968
969 /* Only make all windows the same height if one of them (except oldwin)
970 * is higher than one of the split windows. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100971 if (!do_equal && p_ea && size == 0 && *p_ead != 'h'
Bram Moolenaar67f71312007-08-12 14:55:56 +0000972 && oldwin->w_frame->fr_parent != NULL)
973 {
974 frp = oldwin->w_frame->fr_parent->fr_child;
975 while (frp != NULL)
976 {
977 if (frp->fr_win != oldwin && frp->fr_win != NULL
978 && (frp->fr_win->w_height > new_size
979 || frp->fr_win->w_height > oldwin_height - new_size
980 - STATUS_HEIGHT))
981 {
982 do_equal = TRUE;
983 break;
984 }
985 frp = frp->fr_next;
986 }
987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 }
989
990 /*
991 * allocate new window structure and link it in the window list
992 */
993 if ((flags & WSP_TOP) == 0
994 && ((flags & WSP_BOT)
995 || (flags & WSP_BELOW)
996 || (!(flags & WSP_ABOVE)
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100997 && ( (flags & WSP_VERT) ? p_spr : p_sb))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 {
999 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001000 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001001 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 else
1003 win_append(oldwin, wp);
1004 }
1005 else
1006 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001007 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001008 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 else
1010 win_append(oldwin->w_prev, wp);
1011 }
1012
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001013 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 {
1015 if (wp == NULL)
1016 return FAIL;
1017
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001018 new_frame(wp);
1019 if (wp->w_frame == NULL)
1020 {
1021 win_free(wp, NULL);
1022 return FAIL;
1023 }
1024
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001025 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001026 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 }
1028
1029 /*
1030 * Reorganise the tree of frames to insert the new window.
1031 */
1032 if (flags & (WSP_TOP | WSP_BOT))
1033 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1035 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 {
1037 curfrp = topframe->fr_child;
1038 if (flags & WSP_BOT)
1039 while (curfrp->fr_next != NULL)
1040 curfrp = curfrp->fr_next;
1041 }
1042 else
1043 curfrp = topframe;
1044 before = (flags & WSP_TOP);
1045 }
1046 else
1047 {
1048 curfrp = oldwin->w_frame;
1049 if (flags & WSP_BELOW)
1050 before = FALSE;
1051 else if (flags & WSP_ABOVE)
1052 before = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001053 else if (flags & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 before = !p_spr;
1055 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 before = !p_sb;
1057 }
1058 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1059 {
1060 /* Need to create a new frame in the tree to make a branch. */
1061 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
1062 *frp = *curfrp;
1063 curfrp->fr_layout = layout;
1064 frp->fr_parent = curfrp;
1065 frp->fr_next = NULL;
1066 frp->fr_prev = NULL;
1067 curfrp->fr_child = frp;
1068 curfrp->fr_win = NULL;
1069 curfrp = frp;
1070 if (frp->fr_win != NULL)
1071 oldwin->w_frame = frp;
1072 else
1073 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1074 frp->fr_parent = curfrp;
1075 }
1076
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001077 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001078 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001080 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 frp->fr_parent = curfrp->fr_parent;
1082
1083 /* Insert the new frame at the right place in the frame list. */
1084 if (before)
1085 frame_insert(curfrp, frp);
1086 else
1087 frame_append(curfrp, frp);
1088
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001089 /* Set w_fraction now so that the cursor keeps the same relative
1090 * vertical position. */
Bram Moolenaar3679c172017-11-22 22:22:11 +01001091 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001092 wp->w_fraction = oldwin->w_fraction;
1093
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (flags & WSP_VERT)
1095 {
1096 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001097
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 if (need_status)
1099 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001100 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 oldwin->w_status_height = need_status;
1102 }
1103 if (flags & (WSP_TOP | WSP_BOT))
1104 {
1105 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001106 wp->w_winrow = tabline_height();
Bram Moolenaard326ad62017-09-18 20:31:41 +02001107 win_new_height(wp, curfrp->fr_height - (p_ls > 0)
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001108 - WINBAR_HEIGHT(wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 wp->w_status_height = (p_ls > 0);
1110 }
1111 else
1112 {
1113 /* height and row of new window is same as current window */
1114 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01001115 win_new_height(wp, VISIBLE_HEIGHT(oldwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 wp->w_status_height = oldwin->w_status_height;
1117 }
1118 frp->fr_height = curfrp->fr_height;
1119
1120 /* "new_size" of the current window goes to the new window, use
1121 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001122 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 if (before)
1124 wp->w_vsep_width = 1;
1125 else
1126 {
1127 wp->w_vsep_width = oldwin->w_vsep_width;
1128 oldwin->w_vsep_width = 1;
1129 }
1130 if (flags & (WSP_TOP | WSP_BOT))
1131 {
1132 if (flags & WSP_BOT)
1133 frame_add_vsep(curfrp);
1134 /* Set width of neighbor frame */
1135 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001136 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1137 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001140 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 if (before) /* new window left of current one */
1142 {
1143 wp->w_wincol = oldwin->w_wincol;
1144 oldwin->w_wincol += new_size + 1;
1145 }
1146 else /* new window right of current one */
1147 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1148 frame_fix_width(oldwin);
1149 frame_fix_width(wp);
1150 }
1151 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 {
1153 /* width and column of new window is same as current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 if (flags & (WSP_TOP | WSP_BOT))
1155 {
1156 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001157 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 wp->w_vsep_width = 0;
1159 }
1160 else
1161 {
1162 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001163 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 wp->w_vsep_width = oldwin->w_vsep_width;
1165 }
1166 frp->fr_width = curfrp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167
1168 /* "new_size" of the current window goes to the new window, use
1169 * one row for the status line */
1170 win_new_height(wp, new_size);
1171 if (flags & (WSP_TOP | WSP_BOT))
Bram Moolenaar991dea32016-05-24 11:31:32 +02001172 {
Bram Moolenaard326ad62017-09-18 20:31:41 +02001173 int new_fr_height = curfrp->fr_height - new_size
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001174 + WINBAR_HEIGHT(wp) ;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001175
1176 if (!((flags & WSP_BOT) && p_ls == 0))
1177 new_fr_height -= STATUS_HEIGHT;
1178 frame_new_height(curfrp, new_fr_height, flags & WSP_TOP, FALSE);
1179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 else
1181 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1182 if (before) /* new window above current one */
1183 {
1184 wp->w_winrow = oldwin->w_winrow;
1185 wp->w_status_height = STATUS_HEIGHT;
1186 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1187 }
1188 else /* new window below current one */
1189 {
Bram Moolenaar415a6932017-12-05 20:31:07 +01001190 wp->w_winrow = oldwin->w_winrow + VISIBLE_HEIGHT(oldwin)
1191 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 wp->w_status_height = oldwin->w_status_height;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001193 if (!(flags & WSP_BOT))
1194 oldwin->w_status_height = STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 if (flags & WSP_BOT)
1197 frame_add_statusline(curfrp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 frame_fix_height(wp);
1199 frame_fix_height(oldwin);
1200 }
1201
1202 if (flags & (WSP_TOP | WSP_BOT))
1203 (void)win_comp_pos();
1204
1205 /*
1206 * Both windows need redrawing
1207 */
1208 redraw_win_later(wp, NOT_VALID);
1209 wp->w_redr_status = TRUE;
1210 redraw_win_later(oldwin, NOT_VALID);
1211 oldwin->w_redr_status = TRUE;
1212
1213 if (need_status)
1214 {
1215 msg_row = Rows - 1;
1216 msg_col = sc_col;
1217 msg_clr_eos_force(); /* Old command/ruler may still be there */
1218 comp_col();
1219 msg_row = Rows - 1;
1220 msg_col = 0; /* put position back at start of line */
1221 }
1222
1223 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001224 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 */
1226 if (do_equal || dir != 0)
1227 win_equal(wp, TRUE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001229 : dir == 'h' ? 'b' : 'v');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
1231 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1232 * size was given. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (flags & WSP_VERT)
1234 {
1235 i = p_wiw;
1236 if (size != 0)
1237 p_wiw = size;
1238
1239# ifdef FEAT_GUI
1240 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1241 if (gui.in_use)
1242 gui_init_which_components(NULL);
1243# endif
1244 }
1245 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {
1247 i = p_wh;
1248 if (size != 0)
1249 p_wh = size;
1250 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001251
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001252#ifdef FEAT_JUMPLIST
1253 /* Keep same changelist position in new window. */
1254 wp->w_changelistidx = oldwin->w_changelistidx;
1255#endif
1256
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001257 /*
1258 * make the new window the current window
1259 */
Bram Moolenaarc917da42016-07-19 22:31:36 +02001260 win_enter_ext(wp, FALSE, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 if (flags & WSP_VERT)
1262 p_wiw = i;
1263 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 p_wh = i;
1265
1266 return OK;
1267}
1268
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001269
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001270/*
1271 * Initialize window "newp" from window "oldp".
1272 * Used when splitting a window and when creating a new tab page.
1273 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001274 * WSP_NEWLOC may be specified in flags to prevent the location list from
1275 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001276 */
1277 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001278win_init(win_T *newp, win_T *oldp, int flags UNUSED)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001279{
1280 int i;
1281
1282 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001283#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001284 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001285#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001286 oldp->w_buffer->b_nwindows++;
1287 newp->w_cursor = oldp->w_cursor;
1288 newp->w_valid = 0;
1289 newp->w_curswant = oldp->w_curswant;
1290 newp->w_set_curswant = oldp->w_set_curswant;
1291 newp->w_topline = oldp->w_topline;
1292#ifdef FEAT_DIFF
1293 newp->w_topfill = oldp->w_topfill;
1294#endif
1295 newp->w_leftcol = oldp->w_leftcol;
1296 newp->w_pcmark = oldp->w_pcmark;
1297 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1298 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001299 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001300 newp->w_fraction = oldp->w_fraction;
1301 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1302#ifdef FEAT_JUMPLIST
1303 copy_jumplist(oldp, newp);
1304#endif
1305#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001306 if (flags & WSP_NEWLOC)
1307 {
1308 /* Don't copy the location list. */
1309 newp->w_llist = NULL;
1310 newp->w_llist_ref = NULL;
1311 }
1312 else
1313 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001314#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001315 newp->w_localdir = (oldp->w_localdir == NULL)
1316 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001317
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001318 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001319 for (i = 0; i < oldp->w_tagstacklen; i++)
1320 {
1321 newp->w_tagstack[i] = oldp->w_tagstack[i];
1322 if (newp->w_tagstack[i].tagname != NULL)
1323 newp->w_tagstack[i].tagname =
1324 vim_strsave(newp->w_tagstack[i].tagname);
1325 }
1326 newp->w_tagstackidx = oldp->w_tagstackidx;
1327 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001328#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001329 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001330#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001331
1332 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001333
Bram Moolenaara971b822011-09-14 14:43:25 +02001334#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001335 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001336#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001337}
1338
1339/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001340 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001341 * Only the essential things are copied.
1342 */
1343 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001344win_init_some(win_T *newp, win_T *oldp)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001345{
1346 /* Use the same argument list. */
1347 newp->w_alist = oldp->w_alist;
1348 ++newp->w_alist->al_refcount;
1349 newp->w_arg_idx = oldp->w_arg_idx;
1350
1351 /* copy options from existing window */
1352 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001353}
1354
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001357 * Check if "win" is a pointer to an existing window in the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 */
1359 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001360win_valid(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361{
1362 win_T *wp;
1363
1364 if (win == NULL)
1365 return FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02001366 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 if (wp == win)
1368 return TRUE;
1369 return FALSE;
1370}
1371
1372/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001373 * Check if "win" is a pointer to an existing window in any tab page.
1374 */
1375 int
1376win_valid_any_tab(win_T *win)
1377{
1378 win_T *wp;
1379 tabpage_T *tp;
1380
1381 if (win == NULL)
1382 return FALSE;
1383 FOR_ALL_TABPAGES(tp)
1384 {
1385 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
1386 {
1387 if (wp == win)
1388 return TRUE;
1389 }
1390 }
1391 return FALSE;
1392}
1393
1394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 * Return the number of windows.
1396 */
1397 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001398win_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399{
1400 win_T *wp;
1401 int count = 0;
1402
Bram Moolenaar29323592016-07-24 22:04:11 +02001403 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 ++count;
1405 return count;
1406}
1407
1408/*
1409 * Make "count" windows on the screen.
1410 * Return actual number of windows on the screen.
1411 * Must be called when there is just one window, filling the whole screen
1412 * (excluding the command line).
1413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001415make_windows(
1416 int count,
1417 int vertical UNUSED) /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
1419 int maxcount;
1420 int todo;
1421
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 if (vertical)
1423 {
1424 /* Each windows needs at least 'winminwidth' lines and a separator
1425 * column. */
1426 maxcount = (curwin->w_width + curwin->w_vsep_width
1427 - (p_wiw - p_wmw)) / (p_wmw + 1);
1428 }
1429 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 {
1431 /* Each window needs at least 'winminheight' lines and a status line. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001432 maxcount = (VISIBLE_HEIGHT(curwin) + curwin->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1434 }
1435
1436 if (maxcount < 2)
1437 maxcount = 2;
1438 if (count > maxcount)
1439 count = maxcount;
1440
1441 /*
1442 * add status line now, otherwise first window will be too big
1443 */
1444 if (count > 1)
1445 last_status(TRUE);
1446
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 /*
1448 * Don't execute autocommands while creating the windows. Must do that
1449 * when putting the buffers in the windows.
1450 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001451 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
1453 /* todo is number of windows left to create */
1454 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 if (vertical)
1456 {
1457 if (win_split(curwin->w_width - (curwin->w_width - todo)
1458 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1459 break;
1460 }
1461 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 {
1463 if (win_split(curwin->w_height - (curwin->w_height - todo
1464 * STATUS_HEIGHT) / (todo + 1)
1465 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1466 break;
1467 }
1468
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001469 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470
1471 /* return actual number of windows */
1472 return (count - todo);
1473}
1474
1475/*
1476 * Exchange current and next window
1477 */
1478 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001479win_exchange(long Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480{
1481 frame_T *frp;
1482 frame_T *frp2;
1483 win_T *wp;
1484 win_T *wp2;
1485 int temp;
1486
Bram Moolenaar459ca562016-11-10 18:16:33 +01001487 if (ONE_WINDOW) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 {
1489 beep_flush();
1490 return;
1491 }
1492
1493#ifdef FEAT_GUI
1494 need_mouse_correct = TRUE;
1495#endif
1496
1497 /*
1498 * find window to exchange with
1499 */
1500 if (Prenum)
1501 {
1502 frp = curwin->w_frame->fr_parent->fr_child;
1503 while (frp != NULL && --Prenum > 0)
1504 frp = frp->fr_next;
1505 }
1506 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1507 frp = curwin->w_frame->fr_next;
1508 else /* Swap last window in row/col with previous */
1509 frp = curwin->w_frame->fr_prev;
1510
1511 /* We can only exchange a window with another window, not with a frame
1512 * containing windows. */
1513 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1514 return;
1515 wp = frp->fr_win;
1516
1517/*
1518 * 1. remove curwin from the list. Remember after which window it was in wp2
1519 * 2. insert curwin before wp in the list
1520 * if wp != wp2
1521 * 3. remove wp from the list
1522 * 4. insert wp after wp2
1523 * 5. exchange the status line height and vsep width.
1524 */
1525 wp2 = curwin->w_prev;
1526 frp2 = curwin->w_frame->fr_prev;
1527 if (wp->w_prev != curwin)
1528 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001529 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 frame_remove(curwin->w_frame);
1531 win_append(wp->w_prev, curwin);
1532 frame_insert(frp, curwin->w_frame);
1533 }
1534 if (wp != wp2)
1535 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001536 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 frame_remove(wp->w_frame);
1538 win_append(wp2, wp);
1539 if (frp2 == NULL)
1540 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1541 else
1542 frame_append(frp2, wp->w_frame);
1543 }
1544 temp = curwin->w_status_height;
1545 curwin->w_status_height = wp->w_status_height;
1546 wp->w_status_height = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 temp = curwin->w_vsep_width;
1548 curwin->w_vsep_width = wp->w_vsep_width;
1549 wp->w_vsep_width = temp;
1550
1551 /* If the windows are not in the same frame, exchange the sizes to avoid
1552 * messing up the window layout. Otherwise fix the frame sizes. */
1553 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1554 {
1555 temp = curwin->w_height;
1556 curwin->w_height = wp->w_height;
1557 wp->w_height = temp;
1558 temp = curwin->w_width;
1559 curwin->w_width = wp->w_width;
1560 wp->w_width = temp;
1561 }
1562 else
1563 {
1564 frame_fix_height(curwin);
1565 frame_fix_height(wp);
1566 frame_fix_width(curwin);
1567 frame_fix_width(wp);
1568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
1570 (void)win_comp_pos(); /* recompute window positions */
1571
1572 win_enter(wp, TRUE);
1573 redraw_later(CLEAR);
1574}
1575
1576/*
1577 * rotate windows: if upwards TRUE the second window becomes the first one
1578 * if upwards FALSE the first window becomes the second one
1579 */
1580 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001581win_rotate(int upwards, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582{
1583 win_T *wp1;
1584 win_T *wp2;
1585 frame_T *frp;
1586 int n;
1587
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001588 if (ONE_WINDOW) /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 {
1590 beep_flush();
1591 return;
1592 }
1593
1594#ifdef FEAT_GUI
1595 need_mouse_correct = TRUE;
1596#endif
1597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 /* Check if all frames in this row/col have one window. */
1599 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1600 frp = frp->fr_next)
1601 if (frp->fr_win == NULL)
1602 {
1603 EMSG(_("E443: Cannot rotate when another window is split"));
1604 return;
1605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
1607 while (count--)
1608 {
1609 if (upwards) /* first window becomes last window */
1610 {
1611 /* remove first window/frame from the list */
1612 frp = curwin->w_frame->fr_parent->fr_child;
1613 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001614 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 frame_remove(frp);
1616
1617 /* find last frame and append removed window/frame after it */
1618 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1619 ;
1620 win_append(frp->fr_win, wp1);
1621 frame_append(frp, wp1->w_frame);
1622
1623 wp2 = frp->fr_win; /* previously last window */
1624 }
1625 else /* last window becomes first window */
1626 {
1627 /* find last window/frame in the list and remove it */
1628 for (frp = curwin->w_frame; frp->fr_next != NULL;
1629 frp = frp->fr_next)
1630 ;
1631 wp1 = frp->fr_win;
1632 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001633 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 frame_remove(frp);
1635
1636 /* append the removed window/frame before the first in the list */
1637 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1638 frame_insert(frp->fr_parent->fr_child, frp);
1639 }
1640
1641 /* exchange status height and vsep width of old and new last window */
1642 n = wp2->w_status_height;
1643 wp2->w_status_height = wp1->w_status_height;
1644 wp1->w_status_height = n;
1645 frame_fix_height(wp1);
1646 frame_fix_height(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 n = wp2->w_vsep_width;
1648 wp2->w_vsep_width = wp1->w_vsep_width;
1649 wp1->w_vsep_width = n;
1650 frame_fix_width(wp1);
1651 frame_fix_width(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001653 /* recompute w_winrow and w_wincol for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 (void)win_comp_pos();
1655 }
1656
1657 redraw_later(CLEAR);
1658}
1659
1660/*
1661 * Move the current window to the very top/bottom/left/right of the screen.
1662 */
1663 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001664win_totop(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665{
1666 int dir;
1667 int height = curwin->w_height;
1668
Bram Moolenaar459ca562016-11-10 18:16:33 +01001669 if (ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 {
1671 beep_flush();
1672 return;
1673 }
1674
1675 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001676 (void)winframe_remove(curwin, &dir, NULL);
1677 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 last_status(FALSE); /* may need to remove last status line */
1679 (void)win_comp_pos(); /* recompute window positions */
1680
1681 /* Split a window on the desired side and put the window there. */
1682 (void)win_split_ins(size, flags, curwin, dir);
1683 if (!(flags & WSP_VERT))
1684 {
1685 win_setheight(height);
1686 if (p_ea)
1687 win_equal(curwin, TRUE, 'v');
1688 }
1689
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001690#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1692 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001693 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695}
1696
1697/*
1698 * Move window "win1" to below/right of "win2" and make "win1" the current
1699 * window. Only works within the same frame!
1700 */
1701 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001702win_move_after(win_T *win1, win_T *win2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703{
1704 int height;
1705
1706 /* check if the arguments are reasonable */
1707 if (win1 == win2)
1708 return;
1709
1710 /* check if there is something to do */
1711 if (win2->w_next != win1)
1712 {
1713 /* may need move the status line/vertical separator of the last window
1714 * */
1715 if (win1 == lastwin)
1716 {
1717 height = win1->w_prev->w_status_height;
1718 win1->w_prev->w_status_height = win1->w_status_height;
1719 win1->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001720 if (win1->w_prev->w_vsep_width == 1)
1721 {
1722 /* Remove the vertical separator from the last-but-one window,
1723 * add it to the last window. Adjust the frame widths. */
1724 win1->w_prev->w_vsep_width = 0;
1725 win1->w_prev->w_frame->fr_width -= 1;
1726 win1->w_vsep_width = 1;
1727 win1->w_frame->fr_width += 1;
1728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 }
1730 else if (win2 == lastwin)
1731 {
1732 height = win1->w_status_height;
1733 win1->w_status_height = win2->w_status_height;
1734 win2->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001735 if (win1->w_vsep_width == 1)
1736 {
1737 /* Remove the vertical separator from win1, add it to the last
1738 * window, win2. Adjust the frame widths. */
1739 win2->w_vsep_width = 1;
1740 win2->w_frame->fr_width += 1;
1741 win1->w_vsep_width = 0;
1742 win1->w_frame->fr_width -= 1;
1743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001745 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 frame_remove(win1->w_frame);
1747 win_append(win2, win1);
1748 frame_append(win2->w_frame, win1->w_frame);
1749
1750 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1751 redraw_later(NOT_VALID);
1752 }
1753 win_enter(win1, FALSE);
1754}
1755
1756/*
1757 * Make all windows the same height.
1758 * 'next_curwin' will soon be the current window, make sure it has enough
1759 * rows.
1760 */
1761 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001762win_equal(
1763 win_T *next_curwin, /* pointer to current window to be or NULL */
1764 int current, /* do only frame with current window */
1765 int dir) /* 'v' for vertically, 'h' for horizontally,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 'b' for both, 0 for using p_ead */
1767{
1768 if (dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 dir = *p_ead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001771 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001772 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773}
1774
1775/*
1776 * Set a frame to a new position and height, spreading the available room
1777 * equally over contained frames.
1778 * The window "next_curwin" (if not NULL) should at least get the size from
1779 * 'winheight' and 'winwidth' if possible.
1780 */
1781 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001782win_equal_rec(
1783 win_T *next_curwin, /* pointer to current window to be or NULL */
1784 int current, /* do only frame with current window */
1785 frame_T *topfr, /* frame to set size off */
1786 int dir, /* 'v', 'h' or 'b', see win_equal() */
1787 int col, /* horizontal position for frame */
1788 int row, /* vertical position for frame */
1789 int width, /* new width of frame */
1790 int height) /* new height of frame */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791{
1792 int n, m;
1793 int extra_sep = 0;
1794 int wincount, totwincount = 0;
1795 frame_T *fr;
1796 int next_curwin_size = 0;
1797 int room = 0;
1798 int new_size;
1799 int has_next_curwin = 0;
1800 int hnc;
1801
1802 if (topfr->fr_layout == FR_LEAF)
1803 {
1804 /* Set the width/height of this frame.
1805 * Redraw when size or position changes */
1806 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 )
1809 {
1810 topfr->fr_win->w_winrow = row;
1811 frame_new_height(topfr, height, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001813 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 redraw_all_later(CLEAR);
1815 }
1816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 else if (topfr->fr_layout == FR_ROW)
1818 {
1819 topfr->fr_width = width;
1820 topfr->fr_height = height;
1821
1822 if (dir != 'v') /* equalize frame widths */
1823 {
1824 /* Compute the maximum number of windows horizontally in this
1825 * frame. */
1826 n = frame_minwidth(topfr, NOWIN);
1827 /* add one for the rightmost window, it doesn't have a separator */
1828 if (col + width == Columns)
1829 extra_sep = 1;
1830 else
1831 extra_sep = 0;
1832 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001833 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001835 /*
1836 * Compute width for "next_curwin" window and room available for
1837 * other windows.
1838 * "m" is the minimal width when counting p_wiw for "next_curwin".
1839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 m = frame_minwidth(topfr, next_curwin);
1841 room = width - m;
1842 if (room < 0)
1843 {
1844 next_curwin_size = p_wiw + room;
1845 room = 0;
1846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 else
1848 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001849 next_curwin_size = -1;
1850 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1851 {
1852 /* If 'winfixwidth' set keep the window width if
1853 * possible.
1854 * Watch out for this window being the next_curwin. */
1855 if (frame_fixed_width(fr))
1856 {
1857 n = frame_minwidth(fr, NOWIN);
1858 new_size = fr->fr_width;
1859 if (frame_has_win(fr, next_curwin))
1860 {
1861 room += p_wiw - p_wmw;
1862 next_curwin_size = 0;
1863 if (new_size < p_wiw)
1864 new_size = p_wiw;
1865 }
1866 else
1867 /* These windows don't use up room. */
1868 totwincount -= (n + (fr->fr_next == NULL
1869 ? extra_sep : 0)) / (p_wmw + 1);
1870 room -= new_size - n;
1871 if (room < 0)
1872 {
1873 new_size += room;
1874 room = 0;
1875 }
1876 fr->fr_newwidth = new_size;
1877 }
1878 }
1879 if (next_curwin_size == -1)
1880 {
1881 if (!has_next_curwin)
1882 next_curwin_size = 0;
1883 else if (totwincount > 1
1884 && (room + (totwincount - 2))
1885 / (totwincount - 1) > p_wiw)
1886 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001887 /* Can make all windows wider than 'winwidth', spread
1888 * the room equally. */
1889 next_curwin_size = (room + p_wiw
1890 + (totwincount - 1) * p_wmw
1891 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001892 room -= next_curwin_size - p_wiw;
1893 }
1894 else
1895 next_curwin_size = p_wiw;
1896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001898
1899 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 --totwincount; /* don't count curwin */
1901 }
1902
1903 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 wincount = 1;
1906 if (fr->fr_next == NULL)
1907 /* last frame gets all that remains (avoid roundoff error) */
1908 new_size = width;
1909 else if (dir == 'v')
1910 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001911 else if (frame_fixed_width(fr))
1912 {
1913 new_size = fr->fr_newwidth;
1914 wincount = 0; /* doesn't count as a sizeable window */
1915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 else
1917 {
1918 /* Compute the maximum number of windows horiz. in "fr". */
1919 n = frame_minwidth(fr, NOWIN);
1920 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1921 / (p_wmw + 1);
1922 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001923 if (has_next_curwin)
1924 hnc = frame_has_win(fr, next_curwin);
1925 else
1926 hnc = FALSE;
1927 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001929 if (totwincount == 0)
1930 new_size = room;
1931 else
1932 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001934 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 {
1936 next_curwin_size -= p_wiw - (m - n);
1937 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001938 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001940 else
1941 room -= new_size;
1942 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 }
1944
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001945 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 * window, unless equalizing all frames. */
1947 if (!current || dir != 'v' || topfr->fr_parent != NULL
1948 || (new_size != fr->fr_width)
1949 || frame_has_win(fr, next_curwin))
1950 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001951 new_size, height);
1952 col += new_size;
1953 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 totwincount -= wincount;
1955 }
1956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 else /* topfr->fr_layout == FR_COL */
1958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 topfr->fr_width = width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 topfr->fr_height = height;
1961
1962 if (dir != 'h') /* equalize frame heights */
1963 {
1964 /* Compute maximum number of windows vertically in this frame. */
1965 n = frame_minheight(topfr, NOWIN);
1966 /* add one for the bottom window if it doesn't have a statusline */
1967 if (row + height == cmdline_row && p_ls == 0)
1968 extra_sep = 1;
1969 else
1970 extra_sep = 0;
1971 totwincount = (n + extra_sep) / (p_wmh + 1);
1972 has_next_curwin = frame_has_win(topfr, next_curwin);
1973
1974 /*
1975 * Compute height for "next_curwin" window and room available for
1976 * other windows.
1977 * "m" is the minimal height when counting p_wh for "next_curwin".
1978 */
1979 m = frame_minheight(topfr, next_curwin);
1980 room = height - m;
1981 if (room < 0)
1982 {
1983 /* The room is less then 'winheight', use all space for the
1984 * current window. */
1985 next_curwin_size = p_wh + room;
1986 room = 0;
1987 }
1988 else
1989 {
1990 next_curwin_size = -1;
1991 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1992 {
1993 /* If 'winfixheight' set keep the window height if
1994 * possible.
1995 * Watch out for this window being the next_curwin. */
1996 if (frame_fixed_height(fr))
1997 {
1998 n = frame_minheight(fr, NOWIN);
1999 new_size = fr->fr_height;
2000 if (frame_has_win(fr, next_curwin))
2001 {
2002 room += p_wh - p_wmh;
2003 next_curwin_size = 0;
2004 if (new_size < p_wh)
2005 new_size = p_wh;
2006 }
2007 else
2008 /* These windows don't use up room. */
2009 totwincount -= (n + (fr->fr_next == NULL
2010 ? extra_sep : 0)) / (p_wmh + 1);
2011 room -= new_size - n;
2012 if (room < 0)
2013 {
2014 new_size += room;
2015 room = 0;
2016 }
2017 fr->fr_newheight = new_size;
2018 }
2019 }
2020 if (next_curwin_size == -1)
2021 {
2022 if (!has_next_curwin)
2023 next_curwin_size = 0;
2024 else if (totwincount > 1
2025 && (room + (totwincount - 2))
2026 / (totwincount - 1) > p_wh)
2027 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002028 /* can make all windows higher than 'winheight',
2029 * spread the room equally. */
2030 next_curwin_size = (room + p_wh
2031 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 + (totwincount - 1)) / totwincount;
2033 room -= next_curwin_size - p_wh;
2034 }
2035 else
2036 next_curwin_size = p_wh;
2037 }
2038 }
2039
2040 if (has_next_curwin)
2041 --totwincount; /* don't count curwin */
2042 }
2043
2044 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2045 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 wincount = 1;
2047 if (fr->fr_next == NULL)
2048 /* last frame gets all that remains (avoid roundoff error) */
2049 new_size = height;
2050 else if (dir == 'h')
2051 new_size = fr->fr_height;
2052 else if (frame_fixed_height(fr))
2053 {
2054 new_size = fr->fr_newheight;
2055 wincount = 0; /* doesn't count as a sizeable window */
2056 }
2057 else
2058 {
2059 /* Compute the maximum number of windows vert. in "fr". */
2060 n = frame_minheight(fr, NOWIN);
2061 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2062 / (p_wmh + 1);
2063 m = frame_minheight(fr, next_curwin);
2064 if (has_next_curwin)
2065 hnc = frame_has_win(fr, next_curwin);
2066 else
2067 hnc = FALSE;
2068 if (hnc) /* don't count next_curwin */
2069 --wincount;
2070 if (totwincount == 0)
2071 new_size = room;
2072 else
2073 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2074 / totwincount;
2075 if (hnc) /* add next_curwin size */
2076 {
2077 next_curwin_size -= p_wh - (m - n);
2078 new_size += next_curwin_size;
2079 room -= new_size - next_curwin_size;
2080 }
2081 else
2082 room -= new_size;
2083 new_size += n;
2084 }
2085 /* Skip frame that is full width when splitting or closing a
2086 * window, unless equalizing all frames. */
2087 if (!current || dir != 'h' || topfr->fr_parent != NULL
2088 || (new_size != fr->fr_height)
2089 || frame_has_win(fr, next_curwin))
2090 win_equal_rec(next_curwin, current, fr, dir, col, row,
2091 width, new_size);
2092 row += new_size;
2093 height -= new_size;
2094 totwincount -= wincount;
2095 }
2096 }
2097}
2098
2099/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002100 * Close all windows for buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 */
2102 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002103close_windows(
2104 buf_T *buf,
2105 int keep_curwin) /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002107 win_T *wp;
2108 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002109 int h = tabline_height();
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002110 int count = tabpage_index(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111
2112 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002113
Bram Moolenaar459ca562016-11-10 18:16:33 +01002114 for (wp = firstwin; wp != NULL && !ONE_WINDOW; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002116 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002117 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 {
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002119 if (win_close(wp, FALSE) == FAIL)
2120 /* If closing the window fails give up, to avoid looping
2121 * forever. */
2122 break;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002123
2124 /* Start all over, autocommands may change the window layout. */
2125 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 }
2127 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002128 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002130
2131 /* Also check windows in other tab pages. */
2132 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2133 {
2134 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002135 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002136 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002137 if (wp->w_buffer == buf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002138 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaarf740b292006-02-16 22:11:02 +00002139 {
2140 win_close_othertab(wp, FALSE, tp);
2141
2142 /* Start all over, the tab page may be closed and
2143 * autocommands may change the window layout. */
2144 nexttp = first_tabpage;
2145 break;
2146 }
2147 }
2148
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002150
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002151 if (count != tabpage_index(NULL))
2152 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002153
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002154 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002155 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002156 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157}
2158
2159/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002160 * Return TRUE if the current window is the only window that exists (ignoring
2161 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002162 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002163 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002164 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002165last_window(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002166{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002167 return (one_window() && first_tabpage->tp_next == NULL);
2168}
2169
2170/*
2171 * Return TRUE if there is only one window other than "aucmd_win" in the
2172 * current tab page.
2173 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002174 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002175one_window(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002176{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002177 win_T *wp;
2178 int seen_one = FALSE;
2179
2180 FOR_ALL_WINDOWS(wp)
2181 {
2182 if (wp != aucmd_win)
2183 {
2184 if (seen_one)
2185 return FALSE;
2186 seen_one = TRUE;
2187 }
2188 }
2189 return TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002190}
2191
2192/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002193 * Close the possibly last window in a tab page.
2194 * Returns TRUE when the window was closed already.
2195 */
2196 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002197close_last_window_tabpage(
2198 win_T *win,
2199 int free_buf,
2200 tabpage_T *prev_curtab)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002201{
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002202 if (ONE_WINDOW)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002203 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002204 buf_T *old_curbuf = curbuf;
2205
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002206 /*
2207 * Closing the last window in a tab page. First go to another tab
2208 * page and then close the window and the tab page. This avoids that
2209 * curwin and curtab are invalid while we are freeing memory, they may
2210 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002211 * Don't trigger autocommands yet, they may use wrong values, so do
2212 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002213 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002214 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002215 redraw_tabline = TRUE;
2216
2217 /* Safety check: Autocommands may have closed the window when jumping
2218 * to the other tab page. */
2219 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2220 {
2221 int h = tabline_height();
2222
2223 win_close_othertab(win, free_buf, prev_curtab);
2224 if (h != tabline_height())
2225 shell_new_rows();
2226 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002227 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2228 * that now. */
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002229 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002230 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002231 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2232 if (old_curbuf != curbuf)
2233 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002234 return TRUE;
2235 }
2236 return FALSE;
2237}
2238
2239/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002240 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 * If "free_buf" is TRUE related buffer may be unloaded.
2242 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002243 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002244 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002246 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002247win_close(win_T *win, int free_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248{
2249 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 int dir;
2253 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002254 tabpage_T *prev_curtab = curtab;
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002255 frame_T *win_frame = win->w_frame->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002257 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
2259 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002260 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 }
2262
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002263 if (win->w_closing || (win->w_buffer != NULL
2264 && win->w_buffer->b_locked > 0))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002265 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002266 if (win == aucmd_win)
2267 {
2268 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002269 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002270 }
2271 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2272 {
2273 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002274 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002275 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002276
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002277 /* When closing the last window in a tab page first go to another tab page
2278 * and then close the window and the tab page to avoid that curwin and
2279 * curtab are invalid while we are freeing memory. */
2280 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002281 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002282
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 /* When closing the help window, try restoring a snapshot after closing
2284 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002285 if (bt_help(win->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 help_window = TRUE;
2287 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002288 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 if (win == curwin)
2291 {
2292 /*
2293 * Guess which window is going to be the new current window.
2294 * This may change because of the autocommands (sigh).
2295 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002296 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002299 * Be careful: If autocommands delete the window or cause this window
2300 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 */
2302 if (wp->w_buffer != curbuf)
2303 {
2304 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002305 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002307 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002308 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002309 win->w_closing = FALSE;
2310 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002311 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002313 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002315 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002316 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002317 win->w_closing = FALSE;
2318 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002319 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002320#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 /* autocmds may abort script processing */
2322 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002323 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002327#ifdef FEAT_GUI
2328 /* Avoid trouble with scrollbars that are going to be deleted in
2329 * win_free(). */
2330 if (gui.in_use)
2331 out_flush();
2332#endif
2333
Bram Moolenaara971b822011-09-14 14:43:25 +02002334#ifdef FEAT_SYN_HL
2335 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002336 if (win->w_buffer != NULL)
2337 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002338#endif
2339
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 /*
2341 * Close the link to the buffer.
2342 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002343 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002344 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002345 bufref_T bufref;
2346
2347 set_bufref(&bufref, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002348 win->w_closing = TRUE;
Bram Moolenaar8f913992012-08-29 15:50:26 +02002349 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002350 if (win_valid_any_tab(win))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002351 win->w_closing = FALSE;
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002352 /* Make sure curbuf is valid. It can become invalid if 'bufhidden' is
2353 * "wipe". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002354 if (!bufref_valid(&bufref))
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002355 curbuf = firstbuf;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002356 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002357
Bram Moolenaar802418d2013-01-17 14:00:11 +01002358 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2359 && (last_window() || curtab != prev_curtab
2360 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002361 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002362 /* Autocommands have closed all windows, quit now. Restore
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002363 * curwin->w_buffer, otherwise writing viminfo may fail. */
2364 if (curwin->w_buffer == NULL)
2365 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002366 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002367 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002368
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002369 /* Autocommands may have moved to another tab page. */
2370 if (curtab != prev_curtab && win_valid_any_tab(win)
2371 && win->w_buffer == NULL)
2372 {
2373 /* Need to close the window anyway, since the buffer is NULL. */
2374 win_close_othertab(win, FALSE, prev_curtab);
2375 return FAIL;
2376 }
2377
2378 /* Autocommands may have closed the window already or closed the only
2379 * other window. */
2380 if (!win_valid(win) || last_window()
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002381 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002382 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
Bram Moolenaara971b822011-09-14 14:43:25 +02002384 /* Free the memory used for the window and get the window that received
2385 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002386 wp = win_free_mem(win, &dir, NULL);
2387
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 /* Make sure curwin isn't invalid. It can cause severe trouble when
2389 * printing an error message. For win_equal() curbuf needs to be valid
2390 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002391 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
2393 curwin = wp;
2394#ifdef FEAT_QUICKFIX
2395 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2396 {
2397 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002398 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 * finding another window to go to.
2400 */
2401 for (;;)
2402 {
2403 if (wp->w_next == NULL)
2404 wp = firstwin;
2405 else
2406 wp = wp->w_next;
2407 if (wp == curwin)
2408 break;
2409 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2410 {
2411 curwin = wp;
2412 break;
2413 }
2414 }
2415 }
2416#endif
2417 curbuf = curwin->w_buffer;
2418 close_curwin = TRUE;
Bram Moolenaarf79225e2017-03-18 23:11:04 +01002419
2420 /* The cursor position may be invalid if the buffer changed after last
2421 * using the window. */
2422 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002424 if (p_ea && (*p_ead == 'b' || *p_ead == dir))
Bram Moolenaar8eeeba82017-06-25 22:45:39 +02002425 /* If the frame of the closed window contains the new current window,
2426 * only resize that frame. Otherwise resize all windows. */
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002427 win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 else
2429 win_comp_pos();
2430 if (close_curwin)
2431 {
Bram Moolenaarc917da42016-07-19 22:31:36 +02002432 win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 if (other_buffer)
2434 /* careful: after this wp and win may be invalid! */
2435 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
2437
2438 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002439 * If last window has a status line now and we don't want one,
2440 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 */
2442 last_status(FALSE);
2443
2444 /* After closing the help window, try restoring the window layout from
2445 * before it was opened. */
2446 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002447 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002449#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2451 if (gui.in_use && !win_hasvertsplit())
2452 gui_init_which_components(NULL);
2453#endif
2454
2455 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002456 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457}
2458
2459/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002460 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002461 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002462 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002463 * Caller must check if buffer is hidden and whether the tabline needs to be
2464 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002465 */
2466 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002467win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002468{
2469 win_T *wp;
2470 int dir;
2471 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002472 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002473
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002474 /* Get here with win->w_buffer == NULL when win_close() detects the tab
2475 * page changed. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002476 if (win->w_closing || (win->w_buffer != NULL
2477 && win->w_buffer->b_locked > 0))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002478 return; /* window is already being closed */
Bram Moolenaar362ce482012-06-06 19:02:45 +02002479
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002480 if (win->w_buffer != NULL)
2481 /* Close the link to the buffer. */
2482 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002483
2484 /* Careful: Autocommands may have closed the tab page or made it the
2485 * current tab page. */
2486 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2487 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002488 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002489 return;
2490
2491 /* Autocommands may have closed the window already. */
2492 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2493 ;
2494 if (wp == NULL)
2495 return;
2496
Bram Moolenaarf740b292006-02-16 22:11:02 +00002497 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002498 if (tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002499 {
2500 if (tp == first_tabpage)
2501 first_tabpage = tp->tp_next;
2502 else
2503 {
2504 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2505 ptp = ptp->tp_next)
2506 ;
2507 if (ptp == NULL)
2508 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002509 internal_error("win_close_othertab()");
Bram Moolenaarf740b292006-02-16 22:11:02 +00002510 return;
2511 }
2512 ptp->tp_next = tp->tp_next;
2513 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002514 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002515 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002516
2517 /* Free the memory used for the window. */
2518 win_free_mem(win, &dir, tp);
2519
2520 if (free_tp)
2521 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002522}
2523
2524/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002525 * Free the memory used for a window.
2526 * Returns a pointer to the window that got the freed up space.
2527 */
2528 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002529win_free_mem(
2530 win_T *win,
2531 int *dirp, /* set to 'v' or 'h' for direction if 'ea' */
2532 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002533{
2534 frame_T *frp;
2535 win_T *wp;
2536
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002537 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002538 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002539 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002540 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002541 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002542
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002543 /* When deleting the current window of another tab page select a new
2544 * current window. */
2545 if (tp != NULL && win == tp->tp_curwin)
2546 tp->tp_curwin = wp;
2547
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002548 return wp;
2549}
2550
2551#if defined(EXITFREE) || defined(PROTO)
2552 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002553win_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002554{
2555 int dummy;
2556
Bram Moolenaarf740b292006-02-16 22:11:02 +00002557 while (first_tabpage->tp_next != NULL)
2558 tabpage_close(TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002559
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002560 if (aucmd_win != NULL)
2561 {
2562 (void)win_free_mem(aucmd_win, &dummy, NULL);
2563 aucmd_win = NULL;
2564 }
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002565
2566 while (firstwin != NULL)
2567 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002568
2569 /* No window should be used after this. Set curwin to NULL to crash
2570 * instead of using freed memory. */
2571 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002572}
2573#endif
2574
2575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 * Remove a window and its frame from the tree of frames.
2577 * Returns a pointer to the window that got the freed up space.
2578 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002579 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002580winframe_remove(
2581 win_T *win,
2582 int *dirp UNUSED, /* set to 'v' or 'h' for direction if 'ea' */
2583 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584{
2585 frame_T *frp, *frp2, *frp3;
2586 frame_T *frp_close = win->w_frame;
2587 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588
2589 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002590 * If there is only one window there is nothing to remove.
2591 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002592 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002593 return NULL;
2594
2595 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 * Remove the window from its frame.
2597 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002598 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 wp = frame2win(frp2);
2600
2601 /* Remove this frame from the list of frames. */
2602 frame_remove(frp_close);
2603
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 if (frp_close->fr_parent->fr_layout == FR_COL)
2605 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002606 /* When 'winfixheight' is set, try to find another frame in the column
2607 * (as close to the closed frame as possible) to distribute the height
2608 * to. */
2609 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2610 {
2611 frp = frp_close->fr_prev;
2612 frp3 = frp_close->fr_next;
2613 while (frp != NULL || frp3 != NULL)
2614 {
2615 if (frp != NULL)
2616 {
2617 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2618 {
2619 frp2 = frp;
2620 wp = frp->fr_win;
2621 break;
2622 }
2623 frp = frp->fr_prev;
2624 }
2625 if (frp3 != NULL)
2626 {
2627 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2628 {
2629 frp2 = frp3;
2630 wp = frp3->fr_win;
2631 break;
2632 }
2633 frp3 = frp3->fr_next;
2634 }
2635 }
2636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2638 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 *dirp = 'v';
2640 }
2641 else
2642 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002643 /* When 'winfixwidth' is set, try to find another frame in the column
2644 * (as close to the closed frame as possible) to distribute the width
2645 * to. */
2646 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2647 {
2648 frp = frp_close->fr_prev;
2649 frp3 = frp_close->fr_next;
2650 while (frp != NULL || frp3 != NULL)
2651 {
2652 if (frp != NULL)
2653 {
2654 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2655 {
2656 frp2 = frp;
2657 wp = frp->fr_win;
2658 break;
2659 }
2660 frp = frp->fr_prev;
2661 }
2662 if (frp3 != NULL)
2663 {
2664 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2665 {
2666 frp2 = frp3;
2667 wp = frp3->fr_win;
2668 break;
2669 }
2670 frp3 = frp3->fr_next;
2671 }
2672 }
2673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002675 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 *dirp = 'h';
2677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
2679 /* If rows/columns go to a window below/right its positions need to be
2680 * updated. Can only be done after the sizes have been updated. */
2681 if (frp2 == frp_close->fr_next)
2682 {
2683 int row = win->w_winrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02002684 int col = win->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685
2686 frame_comp_pos(frp2, &row, &col);
2687 }
2688
2689 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2690 {
2691 /* There is no other frame in this list, move its info to the parent
2692 * and remove it. */
2693 frp2->fr_parent->fr_layout = frp2->fr_layout;
2694 frp2->fr_parent->fr_child = frp2->fr_child;
2695 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2696 frp->fr_parent = frp2->fr_parent;
2697 frp2->fr_parent->fr_win = frp2->fr_win;
2698 if (frp2->fr_win != NULL)
2699 frp2->fr_win->w_frame = frp2->fr_parent;
2700 frp = frp2->fr_parent;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002701 if (topframe->fr_child == frp2)
2702 topframe->fr_child = frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 vim_free(frp2);
2704
2705 frp2 = frp->fr_parent;
2706 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2707 {
2708 /* The frame above the parent has the same layout, have to merge
2709 * the frames into this list. */
2710 if (frp2->fr_child == frp)
2711 frp2->fr_child = frp->fr_child;
2712 frp->fr_child->fr_prev = frp->fr_prev;
2713 if (frp->fr_prev != NULL)
2714 frp->fr_prev->fr_next = frp->fr_child;
2715 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2716 {
2717 frp3->fr_parent = frp2;
2718 if (frp3->fr_next == NULL)
2719 {
2720 frp3->fr_next = frp->fr_next;
2721 if (frp->fr_next != NULL)
2722 frp->fr_next->fr_prev = frp3;
2723 break;
2724 }
2725 }
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002726 if (topframe->fr_child == frp)
2727 topframe->fr_child = frp2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 vim_free(frp);
2729 }
2730 }
2731
2732 return wp;
2733}
2734
2735/*
2736 * Find out which frame is going to get the freed up space when "win" is
2737 * closed.
2738 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2739 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2740 * This makes opening a window and closing it immediately keep the same window
2741 * layout.
2742 */
2743 static frame_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002744win_altframe(
2745 win_T *win,
2746 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747{
2748 frame_T *frp;
2749 int b;
2750
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002751 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002752 /* Last window in this tab page, will go to next tab page. */
2753 return alt_tabpage()->tp_curwin->w_frame;
2754
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 frp = win->w_frame;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002756 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 b = p_spr;
2758 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 b = p_sb;
2760 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2761 return frp->fr_next;
2762 return frp->fr_prev;
2763}
2764
2765/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002766 * Return the tabpage that will be used if the current one is closed.
2767 */
2768 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002769alt_tabpage(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002770{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002771 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002772
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002773 /* Use the next tab page if possible. */
2774 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002775 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002776
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002777 /* Find the last but one tab page. */
2778 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2779 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002780 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002781}
2782
2783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 * Find the left-upper window in frame "frp".
2785 */
2786 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002787frame2win(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788{
2789 while (frp->fr_win == NULL)
2790 frp = frp->fr_child;
2791 return frp->fr_win;
2792}
2793
2794/*
2795 * Return TRUE if frame "frp" contains window "wp".
2796 */
2797 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002798frame_has_win(frame_T *frp, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
2800 frame_T *p;
2801
2802 if (frp->fr_layout == FR_LEAF)
2803 return frp->fr_win == wp;
2804
2805 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2806 if (frame_has_win(p, wp))
2807 return TRUE;
2808 return FALSE;
2809}
2810
2811/*
2812 * Set a new height for a frame. Recursively sets the height for contained
2813 * frames and windows. Caller must take care of positions.
2814 */
2815 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002816frame_new_height(
2817 frame_T *topfrp,
2818 int height,
2819 int topfirst, /* resize topmost contained frame first */
2820 int wfh) /* obey 'winfixheight' when there is a choice;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 may cause the height not to be set */
2822{
2823 frame_T *frp;
2824 int extra_lines;
2825 int h;
2826
2827 if (topfrp->fr_win != NULL)
2828 {
2829 /* Simple case: just one window. */
2830 win_new_height(topfrp->fr_win,
Bram Moolenaard326ad62017-09-18 20:31:41 +02002831 height - topfrp->fr_win->w_status_height
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01002832 - WINBAR_HEIGHT(topfrp->fr_win));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 else if (topfrp->fr_layout == FR_ROW)
2835 {
2836 do
2837 {
2838 /* All frames in this row get the same new height. */
2839 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2840 {
2841 frame_new_height(frp, height, topfirst, wfh);
2842 if (frp->fr_height > height)
2843 {
2844 /* Could not fit the windows, make the whole row higher. */
2845 height = frp->fr_height;
2846 break;
2847 }
2848 }
2849 }
2850 while (frp != NULL);
2851 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002852 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
2854 /* Complicated case: Resize a column of frames. Resize the bottom
2855 * frame first, frames above that when needed. */
2856
2857 frp = topfrp->fr_child;
2858 if (wfh)
2859 /* Advance past frames with one window with 'wfh' set. */
2860 while (frame_fixed_height(frp))
2861 {
2862 frp = frp->fr_next;
2863 if (frp == NULL)
2864 return; /* no frame without 'wfh', give up */
2865 }
2866 if (!topfirst)
2867 {
2868 /* Find the bottom frame of this column */
2869 while (frp->fr_next != NULL)
2870 frp = frp->fr_next;
2871 if (wfh)
2872 /* Advance back for frames with one window with 'wfh' set. */
2873 while (frame_fixed_height(frp))
2874 frp = frp->fr_prev;
2875 }
2876
2877 extra_lines = height - topfrp->fr_height;
2878 if (extra_lines < 0)
2879 {
2880 /* reduce height of contained frames, bottom or top frame first */
2881 while (frp != NULL)
2882 {
2883 h = frame_minheight(frp, NULL);
2884 if (frp->fr_height + extra_lines < h)
2885 {
2886 extra_lines += frp->fr_height - h;
2887 frame_new_height(frp, h, topfirst, wfh);
2888 }
2889 else
2890 {
2891 frame_new_height(frp, frp->fr_height + extra_lines,
2892 topfirst, wfh);
2893 break;
2894 }
2895 if (topfirst)
2896 {
2897 do
2898 frp = frp->fr_next;
2899 while (wfh && frp != NULL && frame_fixed_height(frp));
2900 }
2901 else
2902 {
2903 do
2904 frp = frp->fr_prev;
2905 while (wfh && frp != NULL && frame_fixed_height(frp));
2906 }
2907 /* Increase "height" if we could not reduce enough frames. */
2908 if (frp == NULL)
2909 height -= extra_lines;
2910 }
2911 }
2912 else if (extra_lines > 0)
2913 {
2914 /* increase height of bottom or top frame */
2915 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2916 }
2917 }
2918 topfrp->fr_height = height;
2919}
2920
2921/*
2922 * Return TRUE if height of frame "frp" should not be changed because of
2923 * the 'winfixheight' option.
2924 */
2925 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002926frame_fixed_height(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927{
2928 /* frame with one window: fixed height if 'winfixheight' set. */
2929 if (frp->fr_win != NULL)
2930 return frp->fr_win->w_p_wfh;
2931
2932 if (frp->fr_layout == FR_ROW)
2933 {
2934 /* The frame is fixed height if one of the frames in the row is fixed
2935 * height. */
2936 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2937 if (frame_fixed_height(frp))
2938 return TRUE;
2939 return FALSE;
2940 }
2941
2942 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2943 * frames in the row are fixed height. */
2944 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2945 if (!frame_fixed_height(frp))
2946 return FALSE;
2947 return TRUE;
2948}
2949
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002951 * Return TRUE if width of frame "frp" should not be changed because of
2952 * the 'winfixwidth' option.
2953 */
2954 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002955frame_fixed_width(frame_T *frp)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002956{
2957 /* frame with one window: fixed width if 'winfixwidth' set. */
2958 if (frp->fr_win != NULL)
2959 return frp->fr_win->w_p_wfw;
2960
2961 if (frp->fr_layout == FR_COL)
2962 {
2963 /* The frame is fixed width if one of the frames in the row is fixed
2964 * width. */
2965 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2966 if (frame_fixed_width(frp))
2967 return TRUE;
2968 return FALSE;
2969 }
2970
2971 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2972 * frames in the row are fixed width. */
2973 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2974 if (!frame_fixed_width(frp))
2975 return FALSE;
2976 return TRUE;
2977}
2978
2979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 * Add a status line to windows at the bottom of "frp".
2981 * Note: Does not check if there is room!
2982 */
2983 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002984frame_add_statusline(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985{
2986 win_T *wp;
2987
2988 if (frp->fr_layout == FR_LEAF)
2989 {
2990 wp = frp->fr_win;
2991 if (wp->w_status_height == 0)
2992 {
2993 if (wp->w_height > 0) /* don't make it negative */
2994 --wp->w_height;
2995 wp->w_status_height = STATUS_HEIGHT;
2996 }
2997 }
2998 else if (frp->fr_layout == FR_ROW)
2999 {
3000 /* Handle all the frames in the row. */
3001 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3002 frame_add_statusline(frp);
3003 }
3004 else /* frp->fr_layout == FR_COL */
3005 {
3006 /* Only need to handle the last frame in the column. */
3007 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3008 ;
3009 frame_add_statusline(frp);
3010 }
3011}
3012
3013/*
3014 * Set width of a frame. Handles recursively going through contained frames.
3015 * May remove separator line for windows at the right side (for win_close()).
3016 */
3017 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003018frame_new_width(
3019 frame_T *topfrp,
3020 int width,
3021 int leftfirst, /* resize leftmost contained frame first */
3022 int wfw) /* obey 'winfixwidth' when there is a choice;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003023 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
3025 frame_T *frp;
3026 int extra_cols;
3027 int w;
3028 win_T *wp;
3029
3030 if (topfrp->fr_layout == FR_LEAF)
3031 {
3032 /* Simple case: just one window. */
3033 wp = topfrp->fr_win;
3034 /* Find out if there are any windows right of this one. */
3035 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3036 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3037 break;
3038 if (frp->fr_parent == NULL)
3039 wp->w_vsep_width = 0;
3040 win_new_width(wp, width - wp->w_vsep_width);
3041 }
3042 else if (topfrp->fr_layout == FR_COL)
3043 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003044 do
3045 {
3046 /* All frames in this column get the same new width. */
3047 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3048 {
3049 frame_new_width(frp, width, leftfirst, wfw);
3050 if (frp->fr_width > width)
3051 {
3052 /* Could not fit the windows, make whole column wider. */
3053 width = frp->fr_width;
3054 break;
3055 }
3056 }
3057 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 }
3059 else /* fr_layout == FR_ROW */
3060 {
3061 /* Complicated case: Resize a row of frames. Resize the rightmost
3062 * frame first, frames left of it when needed. */
3063
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003065 if (wfw)
3066 /* Advance past frames with one window with 'wfw' set. */
3067 while (frame_fixed_width(frp))
3068 {
3069 frp = frp->fr_next;
3070 if (frp == NULL)
3071 return; /* no frame without 'wfw', give up */
3072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003074 {
3075 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 while (frp->fr_next != NULL)
3077 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003078 if (wfw)
3079 /* Advance back for frames with one window with 'wfw' set. */
3080 while (frame_fixed_width(frp))
3081 frp = frp->fr_prev;
3082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
3084 extra_cols = width - topfrp->fr_width;
3085 if (extra_cols < 0)
3086 {
3087 /* reduce frame width, rightmost frame first */
3088 while (frp != NULL)
3089 {
3090 w = frame_minwidth(frp, NULL);
3091 if (frp->fr_width + extra_cols < w)
3092 {
3093 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003094 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 }
3096 else
3097 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003098 frame_new_width(frp, frp->fr_width + extra_cols,
3099 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 break;
3101 }
3102 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003103 {
3104 do
3105 frp = frp->fr_next;
3106 while (wfw && frp != NULL && frame_fixed_width(frp));
3107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003109 {
3110 do
3111 frp = frp->fr_prev;
3112 while (wfw && frp != NULL && frame_fixed_width(frp));
3113 }
3114 /* Increase "width" if we could not reduce enough frames. */
3115 if (frp == NULL)
3116 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 }
3118 }
3119 else if (extra_cols > 0)
3120 {
3121 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003122 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 }
3124 }
3125 topfrp->fr_width = width;
3126}
3127
3128/*
3129 * Add the vertical separator to windows at the right side of "frp".
3130 * Note: Does not check if there is room!
3131 */
3132 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003133frame_add_vsep(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134{
3135 win_T *wp;
3136
3137 if (frp->fr_layout == FR_LEAF)
3138 {
3139 wp = frp->fr_win;
3140 if (wp->w_vsep_width == 0)
3141 {
3142 if (wp->w_width > 0) /* don't make it negative */
3143 --wp->w_width;
3144 wp->w_vsep_width = 1;
3145 }
3146 }
3147 else if (frp->fr_layout == FR_COL)
3148 {
3149 /* Handle all the frames in the column. */
3150 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3151 frame_add_vsep(frp);
3152 }
3153 else /* frp->fr_layout == FR_ROW */
3154 {
3155 /* Only need to handle the last frame in the row. */
3156 frp = frp->fr_child;
3157 while (frp->fr_next != NULL)
3158 frp = frp->fr_next;
3159 frame_add_vsep(frp);
3160 }
3161}
3162
3163/*
3164 * Set frame width from the window it contains.
3165 */
3166 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003167frame_fix_width(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168{
3169 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3170}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171
3172/*
3173 * Set frame height from the window it contains.
3174 */
3175 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003176frame_fix_height(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177{
Bram Moolenaar415a6932017-12-05 20:31:07 +01003178 wp->w_frame->fr_height = VISIBLE_HEIGHT(wp) + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179}
3180
3181/*
3182 * Compute the minimal height for frame "topfrp".
3183 * Uses the 'winminheight' option.
3184 * When "next_curwin" isn't NULL, use p_wh for this window.
3185 * When "next_curwin" is NOWIN, don't use at least one line for the current
3186 * window.
3187 */
3188 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003189frame_minheight(frame_T *topfrp, win_T *next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190{
3191 frame_T *frp;
3192 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195 if (topfrp->fr_win != NULL)
3196 {
3197 if (topfrp->fr_win == next_curwin)
3198 m = p_wh + topfrp->fr_win->w_status_height;
3199 else
3200 {
3201 /* window: minimal height of the window plus status line */
3202 m = p_wmh + topfrp->fr_win->w_status_height;
Bram Moolenaar415a6932017-12-05 20:31:07 +01003203 if (topfrp->fr_win == curwin && next_curwin == NULL)
3204 {
3205 /* Current window is minimal one line high and WinBar is
3206 * visible. */
3207 if (p_wmh == 0)
3208 ++m;
3209 m += WINBAR_HEIGHT(curwin);
3210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 }
3212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 else if (topfrp->fr_layout == FR_ROW)
3214 {
3215 /* get the minimal height from each frame in this row */
3216 m = 0;
3217 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3218 {
3219 n = frame_minheight(frp, next_curwin);
3220 if (n > m)
3221 m = n;
3222 }
3223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 else
3225 {
3226 /* Add up the minimal heights for all frames in this column. */
3227 m = 0;
3228 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3229 m += frame_minheight(frp, next_curwin);
3230 }
3231
3232 return m;
3233}
3234
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235/*
3236 * Compute the minimal width for frame "topfrp".
3237 * When "next_curwin" isn't NULL, use p_wiw for this window.
3238 * When "next_curwin" is NOWIN, don't use at least one column for the current
3239 * window.
3240 */
3241 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003242frame_minwidth(
3243 frame_T *topfrp,
3244 win_T *next_curwin) /* use p_wh and p_wiw for next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
3246 frame_T *frp;
3247 int m, n;
3248
3249 if (topfrp->fr_win != NULL)
3250 {
3251 if (topfrp->fr_win == next_curwin)
3252 m = p_wiw + topfrp->fr_win->w_vsep_width;
3253 else
3254 {
3255 /* window: minimal width of the window plus separator column */
3256 m = p_wmw + topfrp->fr_win->w_vsep_width;
3257 /* Current window is minimal one column wide */
3258 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3259 ++m;
3260 }
3261 }
3262 else if (topfrp->fr_layout == FR_COL)
3263 {
3264 /* get the minimal width from each frame in this column */
3265 m = 0;
3266 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3267 {
3268 n = frame_minwidth(frp, next_curwin);
3269 if (n > m)
3270 m = n;
3271 }
3272 }
3273 else
3274 {
3275 /* Add up the minimal widths for all frames in this row. */
3276 m = 0;
3277 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3278 m += frame_minwidth(frp, next_curwin);
3279 }
3280
3281 return m;
3282}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
3284
3285/*
3286 * Try to close all windows except current one.
3287 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3288 * used and the buffer was modified.
3289 *
3290 * Used by ":bdel" and ":only".
3291 */
3292 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003293close_others(
3294 int message,
3295 int forceit) /* always hide all other windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
3297 win_T *wp;
3298 win_T *nextwp;
3299 int r;
3300
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003301 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003303 if (message && !autocmd_busy)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003304 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 return;
3306 }
3307
3308 /* Be very careful here: autocommands may change the window layout. */
3309 for (wp = firstwin; win_valid(wp); wp = nextwp)
3310 {
3311 nextwp = wp->w_next;
3312 if (wp != curwin) /* don't close current window */
3313 {
3314
3315 /* Check if it's allowed to abandon this window */
3316 r = can_abandon(wp->w_buffer, forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 if (!win_valid(wp)) /* autocommands messed wp up */
3318 {
3319 nextwp = firstwin;
3320 continue;
3321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 if (!r)
3323 {
3324#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3325 if (message && (p_confirm || cmdmod.confirm) && p_write)
3326 {
3327 dialog_changed(wp->w_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 if (!win_valid(wp)) /* autocommands messed wp up */
3329 {
3330 nextwp = firstwin;
3331 continue;
3332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
3334 if (bufIsChanged(wp->w_buffer))
3335#endif
3336 continue;
3337 }
Bram Moolenaareb44a682017-08-03 22:44:55 +02003338 win_close(wp, !buf_hide(wp->w_buffer)
3339 && !bufIsChanged(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 }
3341 }
3342
Bram Moolenaar459ca562016-11-10 18:16:33 +01003343 if (message && !ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 EMSG(_("E445: Other window contains changes"));
3345}
3346
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003348 * Init the current window "curwin".
3349 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 */
3351 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003352curwin_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003354 win_init_empty(curwin);
3355}
3356
3357 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003358win_init_empty(win_T *wp)
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003359{
3360 redraw_win_later(wp, NOT_VALID);
3361 wp->w_lines_valid = 0;
3362 wp->w_cursor.lnum = 1;
3363 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003365 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003367 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3368 wp->w_pcmark.col = 0;
3369 wp->w_prev_pcmark.lnum = 0;
3370 wp->w_prev_pcmark.col = 0;
3371 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003373 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003375 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003377 if (wp->w_p_rl)
3378 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003380 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003382#ifdef FEAT_SYN_HL
3383 wp->w_s = &wp->w_buffer->b_s;
3384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385}
3386
3387/*
3388 * Allocate the first window and put an empty buffer in it.
3389 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003390 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003392 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003393win_alloc_first(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003395 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003396 return FAIL;
3397
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003398 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003399 if (first_tabpage == NULL)
3400 return FAIL;
3401 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003402 curtab = first_tabpage;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003403
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003404 return OK;
3405}
3406
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003407/*
3408 * Init "aucmd_win". This can only be done after the first
3409 * window is fully initialized, thus it can't be in win_alloc_first().
3410 */
3411 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003412win_alloc_aucmd_win(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003413{
3414 aucmd_win = win_alloc(NULL, TRUE);
3415 if (aucmd_win != NULL)
3416 {
3417 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003418 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003419 new_frame(aucmd_win);
3420 }
3421}
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003422
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003423/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003424 * Allocate the first window or the first window in a new tab page.
3425 * When "oldwin" is NULL create an empty buffer for it.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003426 * When "oldwin" is not NULL copy info from it to the new window.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003427 * Return FAIL when something goes wrong (out of memory).
3428 */
3429 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003430win_alloc_firstwin(win_T *oldwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003431{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003432 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003433 if (oldwin == NULL)
3434 {
3435 /* Very first window, need to create an empty buffer for it and
3436 * initialize from scratch. */
3437 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3438 if (curwin == NULL || curbuf == NULL)
3439 return FAIL;
3440 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003441#ifdef FEAT_SYN_HL
3442 curwin->w_s = &(curbuf->b_s);
3443#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003444 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003445 curwin->w_alist = &global_alist;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003446 curwin_init(); /* init current window */
3447 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003448 else
3449 {
3450 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003451 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003452
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003453 /* We don't want cursor- and scroll-binding in the first window. */
3454 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003457 new_frame(curwin);
3458 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003459 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003460 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 topframe->fr_height = Rows - p_ch;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003463
3464 return OK;
3465}
3466
3467/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003468 * Create a frame for window "wp".
3469 */
3470 static void
3471new_frame(win_T *wp)
3472{
3473 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3474
3475 wp->w_frame = frp;
3476 if (frp != NULL)
3477 {
3478 frp->fr_layout = FR_LEAF;
3479 frp->fr_win = wp;
3480 }
3481}
3482
3483/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003484 * Initialize the window and frame size to the maximum.
3485 */
3486 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003487win_init_size(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003488{
3489 firstwin->w_height = ROWS_AVAIL;
3490 topframe->fr_height = ROWS_AVAIL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003491 firstwin->w_width = Columns;
3492 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493}
3494
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003495/*
3496 * Allocate a new tabpage_T and init the values.
3497 * Returns NULL when out of memory.
3498 */
3499 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003500alloc_tabpage(void)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003501{
3502 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003503# ifdef FEAT_GUI
3504 int i;
3505# endif
3506
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003507
3508 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003509 if (tp == NULL)
3510 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003511
Bram Moolenaar429fa852013-04-15 12:27:36 +02003512# ifdef FEAT_EVAL
3513 /* init t: variables */
3514 tp->tp_vars = dict_alloc();
3515 if (tp->tp_vars == NULL)
3516 {
3517 vim_free(tp);
3518 return NULL;
3519 }
3520 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3521# endif
3522
3523# ifdef FEAT_GUI
3524 for (i = 0; i < 3; i++)
3525 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003526# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003527# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003528 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003529# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003530 tp->tp_ch_used = p_ch;
3531
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003532 return tp;
3533}
3534
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003535 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003536free_tabpage(tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003537{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003538 int idx;
3539
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003540# ifdef FEAT_DIFF
3541 diff_clear(tp);
3542# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003543 for (idx = 0; idx < SNAP_COUNT; ++idx)
3544 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003545#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003546 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3547 hash_init(&tp->tp_vars->dv_hashtab);
3548 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003549#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003550
3551#ifdef FEAT_PYTHON
3552 python_tabpage_free(tp);
3553#endif
3554
3555#ifdef FEAT_PYTHON3
3556 python3_tabpage_free(tp);
3557#endif
3558
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003559 vim_free(tp);
3560}
3561
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003562/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003563 * Create a new Tab page with one window.
3564 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003565 * When "after" is 0 put it just after the current Tab page.
3566 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003567 * Return FAIL or OK.
3568 */
3569 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003570win_new_tabpage(int after)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003571{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003572 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003573 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003574 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003575
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003576 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003577 if (newtp == NULL)
3578 return FAIL;
3579
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003580 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003581 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003582 {
3583 vim_free(newtp);
3584 return FAIL;
3585 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003586 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003587
3588 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003589 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003590 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003591 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003592 if (after == 1)
3593 {
3594 /* New tab page becomes the first one. */
3595 newtp->tp_next = first_tabpage;
3596 first_tabpage = newtp;
3597 }
3598 else
3599 {
3600 if (after > 0)
3601 {
3602 /* Put new tab page before tab page "after". */
3603 n = 2;
3604 for (tp = first_tabpage; tp->tp_next != NULL
3605 && n < after; tp = tp->tp_next)
3606 ++n;
3607 }
3608 newtp->tp_next = tp->tp_next;
3609 tp->tp_next = newtp;
3610 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003611 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003612 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003613 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003614
3615 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003616 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003617
3618#if defined(FEAT_GUI)
3619 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3620 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003621 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003622#endif
3623
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003624 redraw_all_later(CLEAR);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003625 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003626 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003627 apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003628 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003629 return OK;
3630 }
3631
3632 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003633 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003634 return FAIL;
3635}
3636
3637/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003638 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3639 * like with ":split".
3640 * Returns OK if a new tab page was created, FAIL otherwise.
3641 */
3642 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003643may_open_tabpage(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003644{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003645 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003646
Bram Moolenaard326ce82007-03-11 14:48:29 +00003647 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003648 {
3649 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003650 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003651 return win_new_tabpage(n);
3652 }
3653 return FAIL;
3654}
3655
3656/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003657 * Create up to "maxcount" tabpages with empty windows.
3658 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003659 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003660 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003661make_tabpages(int maxcount)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003662{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003663 int count = maxcount;
3664 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003665
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003666 /* Limit to 'tabpagemax' tabs. */
3667 if (count > p_tpm)
3668 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003669
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003670 /*
3671 * Don't execute autocommands while creating the tab pages. Must do that
3672 * when putting the buffers in the windows.
3673 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003674 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003675
3676 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003677 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003678 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003679
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003680 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003681
3682 /* return actual number of tab pages */
3683 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003684}
3685
3686/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003687 * Return TRUE when "tpc" points to a valid tab page.
3688 */
3689 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003690valid_tabpage(tabpage_T *tpc)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003691{
3692 tabpage_T *tp;
3693
Bram Moolenaar29323592016-07-24 22:04:11 +02003694 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003695 if (tp == tpc)
3696 return TRUE;
3697 return FALSE;
3698}
3699
3700/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003701 * Return TRUE when "tpc" points to a valid tab page and at least one window is
3702 * valid.
3703 */
3704 int
3705valid_tabpage_win(tabpage_T *tpc)
3706{
3707 tabpage_T *tp;
3708 win_T *wp;
3709
3710 FOR_ALL_TABPAGES(tp)
3711 {
3712 if (tp == tpc)
3713 {
3714 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
3715 {
3716 if (win_valid_any_tab(wp))
3717 return TRUE;
3718 }
3719 return FALSE;
3720 }
3721 }
3722 /* shouldn't happen */
3723 return FALSE;
3724}
3725
3726/*
3727 * Close tabpage "tab", assuming it has no windows in it.
3728 * There must be another tabpage or this will crash.
3729 */
3730 void
3731close_tabpage(tabpage_T *tab)
3732{
3733 tabpage_T *ptp;
3734
3735 if (tab == first_tabpage)
3736 {
3737 first_tabpage = tab->tp_next;
3738 ptp = first_tabpage;
3739 }
3740 else
3741 {
3742 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tab;
3743 ptp = ptp->tp_next)
3744 ;
Bram Moolenaara37ffaa2017-03-21 21:58:00 +01003745 assert(ptp != NULL);
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003746 ptp->tp_next = tab->tp_next;
3747 }
3748
3749 goto_tabpage_tp(ptp, FALSE, FALSE);
3750 free_tabpage(tab);
3751}
3752
3753/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003754 * Find tab page "n" (first one is 1). Returns NULL when not found.
3755 */
3756 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003757find_tabpage(int n)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003758{
3759 tabpage_T *tp;
3760 int i = 1;
3761
3762 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3763 ++i;
3764 return tp;
3765}
3766
3767/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003768 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003769 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003770 */
3771 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003772tabpage_index(tabpage_T *ftp)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003773{
3774 int i = 1;
3775 tabpage_T *tp;
3776
3777 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3778 ++i;
3779 return i;
3780}
3781
3782/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003783 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003784 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003785 * FAIL.
3786 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003787 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003788 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003789leave_tabpage(
3790 buf_T *new_curbuf UNUSED, /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003791 NULL if unknown */
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003792 int trigger_leave_autocmds UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003793{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003794 tabpage_T *tp = curtab;
3795
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003796 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003797 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003798 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003799 if (new_curbuf != curbuf)
3800 {
3801 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3802 if (curtab != tp)
3803 return FAIL;
3804 }
3805 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3806 if (curtab != tp)
3807 return FAIL;
3808 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003809 if (curtab != tp)
3810 return FAIL;
3811 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003812#if defined(FEAT_GUI)
3813 /* Remove the scrollbars. They may be added back later. */
3814 if (gui.in_use)
3815 gui_remove_scrollbars();
3816#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003817 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003818 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003819 tp->tp_firstwin = firstwin;
3820 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003821 tp->tp_old_Rows = Rows;
3822 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003823 firstwin = NULL;
3824 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003825 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003826}
3827
3828/*
3829 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003830 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003831 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3832 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003833 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003834 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003835enter_tabpage(
3836 tabpage_T *tp,
3837 buf_T *old_curbuf UNUSED,
Bram Moolenaarf1d25012016-03-03 12:22:53 +01003838 int trigger_enter_autocmds,
3839 int trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003840{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003841 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003842 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003843
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003844 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003845 firstwin = tp->tp_firstwin;
3846 lastwin = tp->tp_lastwin;
3847 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003848
3849 /* We would like doing the TabEnter event first, but we don't have a
3850 * valid current window yet, which may break some commands.
3851 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaarc917da42016-07-19 22:31:36 +02003852 win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE,
Bram Moolenaard6949742013-06-16 14:18:28 +02003853 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003854 prevwin = next_prevwin;
3855
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003856 last_status(FALSE); /* status line may appear or disappear */
3857 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003858 must_redraw = CLEAR; /* need to redraw everything */
3859#ifdef FEAT_DIFF
3860 diff_need_scrollbind = TRUE;
3861#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003862
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003863 /* The tabpage line may have appeared or disappeared, may need to resize
3864 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003865 * frame sizes too. Use the stored value of p_ch, so that it can be
3866 * different for each tab page. */
3867 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003868 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3869#ifdef FEAT_GUI_TABLINE
3870 && !gui_use_tabline()
3871#endif
3872 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003873 shell_new_rows();
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003874 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003875 shell_new_columns(); /* update window widths */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003876
3877#if defined(FEAT_GUI)
3878 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3879 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003880 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003881#endif
3882
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003883 /* Apply autocommands after updating the display, when 'rows' and
3884 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003885 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003886 {
3887 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3888 if (old_curbuf != curbuf)
3889 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3890 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003891
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003892 redraw_all_later(CLEAR);
3893}
3894
3895/*
3896 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003897 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003898 */
3899 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003900goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003901{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003902 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003903 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003904 int i;
3905
Bram Moolenaard68071d2006-05-02 22:08:30 +00003906 if (text_locked())
3907 {
3908 /* Not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02003909 text_locked_msg();
Bram Moolenaard68071d2006-05-02 22:08:30 +00003910 return;
3911 }
3912
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003913 /* If there is only one it can't work. */
3914 if (first_tabpage->tp_next == NULL)
3915 {
3916 if (n > 1)
3917 beep_flush();
3918 return;
3919 }
3920
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003921 if (n == 0)
3922 {
3923 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003924 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003925 tp = first_tabpage;
3926 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003927 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003928 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003929 else if (n < 0)
3930 {
3931 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3932 * this N times. */
3933 ttp = curtab;
3934 for (i = n; i < 0; ++i)
3935 {
3936 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3937 tp = tp->tp_next)
3938 ;
3939 ttp = tp;
3940 }
3941 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003942 else if (n == 9999)
3943 {
3944 /* Go to last tab page. */
3945 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3946 ;
3947 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003948 else
3949 {
3950 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003951 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003952 if (tp == NULL)
3953 {
3954 beep_flush();
3955 return;
3956 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003957 }
3958
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003959 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003960
3961#ifdef FEAT_GUI_TABLINE
3962 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003963 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003964#endif
3965}
3966
3967/*
3968 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003969 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3970 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003971 * Note: doesn't update the GUI tab.
3972 */
3973 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003974goto_tabpage_tp(
3975 tabpage_T *tp,
3976 int trigger_enter_autocmds,
3977 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003978{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003979 /* Don't repeat a message in another tab page. */
3980 set_keep_msg(NULL, 0);
3981
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003982 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3983 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003984 {
3985 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003986 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3987 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003988 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003989 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3990 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003991 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003992}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
3994/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003995 * Enter window "wp" in tab page "tp".
3996 * Also updates the GUI tab.
3997 */
3998 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003999goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004000{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004001 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004002 if (curtab == tp && win_valid(wp))
4003 {
4004 win_enter(wp, TRUE);
4005# ifdef FEAT_GUI_TABLINE
4006 if (gui_use_tabline())
4007 gui_mch_set_curtab(tabpage_index(curtab));
4008# endif
4009 }
4010}
4011
4012/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004013 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004014 */
4015 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004016tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004017{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004018 int n = 1;
4019 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004020
4021 if (first_tabpage->tp_next == NULL)
4022 return;
4023
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004024 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4025 ++n;
4026
4027 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4028 && tp->tp_next == curtab))
4029 return;
4030
4031 tp_dst = tp;
4032
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004033 /* Remove the current tab page from the list of tab pages. */
4034 if (curtab == first_tabpage)
4035 first_tabpage = curtab->tp_next;
4036 else
4037 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004038 FOR_ALL_TABPAGES(tp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004039 if (tp->tp_next == curtab)
4040 break;
4041 if (tp == NULL) /* "cannot happen" */
4042 return;
4043 tp->tp_next = curtab->tp_next;
4044 }
4045
4046 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004047 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004048 {
4049 curtab->tp_next = first_tabpage;
4050 first_tabpage = curtab;
4051 }
4052 else
4053 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004054 curtab->tp_next = tp_dst->tp_next;
4055 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004056 }
4057
4058 /* Need to redraw the tabline. Tab page contents doesn't change. */
4059 redraw_tabline = TRUE;
4060}
4061
4062
4063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 * Go to another window.
4065 * When jumping to another buffer, stop Visual mode. Do this before
4066 * changing windows so we can yank the selection into the '*' register.
4067 * When jumping to another window on the same buffer, adjust its cursor
4068 * position to keep the same Visual area.
4069 */
4070 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004071win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004073#ifdef FEAT_CONCEAL
4074 win_T *owp = curwin;
4075#endif
4076
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004077 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 {
4079 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004080 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 return;
4082 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004083 if (curbuf_locked())
4084 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004085
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 if (wp->w_buffer != curbuf)
4087 reset_VIsual_and_resel();
4088 else if (VIsual_active)
4089 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090
4091#ifdef FEAT_GUI
4092 need_mouse_correct = TRUE;
4093#endif
4094 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004095
4096#ifdef FEAT_CONCEAL
4097 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004098 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004099 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004100 if (curwin->w_p_cole > 0 && !msg_scrolled)
4101 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103}
4104
4105#if defined(FEAT_PERL) || defined(PROTO)
4106/*
4107 * Find window number "winnr" (counting top to bottom).
4108 */
4109 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004110win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111{
4112 win_T *wp;
4113
Bram Moolenaar29323592016-07-24 22:04:11 +02004114 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 if (--winnr == 0)
4116 break;
4117 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118}
4119#endif
4120
Bram Moolenaar4033c552017-09-16 20:54:51 +02004121#if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004122/*
4123 * Find the tabpage for window "win".
4124 */
4125 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004126win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004127{
4128 win_T *wp;
4129 tabpage_T *tp;
4130
Bram Moolenaar29323592016-07-24 22:04:11 +02004131 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004132 if (wp == win)
4133 return tp;
4134 return NULL;
4135}
4136#endif
4137
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138/*
4139 * Move to window above or below "count" times.
4140 */
4141 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004142win_goto_ver(
4143 int up, /* TRUE to go to win above */
4144 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145{
4146 frame_T *fr;
4147 frame_T *nfr;
4148 frame_T *foundfr;
4149
4150 foundfr = curwin->w_frame;
4151 while (count--)
4152 {
4153 /*
4154 * First go upwards in the tree of frames until we find a upwards or
4155 * downwards neighbor.
4156 */
4157 fr = foundfr;
4158 for (;;)
4159 {
4160 if (fr == topframe)
4161 goto end;
4162 if (up)
4163 nfr = fr->fr_prev;
4164 else
4165 nfr = fr->fr_next;
4166 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4167 break;
4168 fr = fr->fr_parent;
4169 }
4170
4171 /*
4172 * Now go downwards to find the bottom or top frame in it.
4173 */
4174 for (;;)
4175 {
4176 if (nfr->fr_layout == FR_LEAF)
4177 {
4178 foundfr = nfr;
4179 break;
4180 }
4181 fr = nfr->fr_child;
4182 if (nfr->fr_layout == FR_ROW)
4183 {
4184 /* Find the frame at the cursor row. */
4185 while (fr->fr_next != NULL
4186 && frame2win(fr)->w_wincol + fr->fr_width
4187 <= curwin->w_wincol + curwin->w_wcol)
4188 fr = fr->fr_next;
4189 }
4190 if (nfr->fr_layout == FR_COL && up)
4191 while (fr->fr_next != NULL)
4192 fr = fr->fr_next;
4193 nfr = fr;
4194 }
4195 }
4196end:
4197 if (foundfr != NULL)
4198 win_goto(foundfr->fr_win);
4199}
4200
4201/*
4202 * Move to left or right window.
4203 */
4204 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004205win_goto_hor(
4206 int left, /* TRUE to go to left win */
4207 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208{
4209 frame_T *fr;
4210 frame_T *nfr;
4211 frame_T *foundfr;
4212
4213 foundfr = curwin->w_frame;
4214 while (count--)
4215 {
4216 /*
4217 * First go upwards in the tree of frames until we find a left or
4218 * right neighbor.
4219 */
4220 fr = foundfr;
4221 for (;;)
4222 {
4223 if (fr == topframe)
4224 goto end;
4225 if (left)
4226 nfr = fr->fr_prev;
4227 else
4228 nfr = fr->fr_next;
4229 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4230 break;
4231 fr = fr->fr_parent;
4232 }
4233
4234 /*
4235 * Now go downwards to find the leftmost or rightmost frame in it.
4236 */
4237 for (;;)
4238 {
4239 if (nfr->fr_layout == FR_LEAF)
4240 {
4241 foundfr = nfr;
4242 break;
4243 }
4244 fr = nfr->fr_child;
4245 if (nfr->fr_layout == FR_COL)
4246 {
4247 /* Find the frame at the cursor row. */
4248 while (fr->fr_next != NULL
4249 && frame2win(fr)->w_winrow + fr->fr_height
4250 <= curwin->w_winrow + curwin->w_wrow)
4251 fr = fr->fr_next;
4252 }
4253 if (nfr->fr_layout == FR_ROW && left)
4254 while (fr->fr_next != NULL)
4255 fr = fr->fr_next;
4256 nfr = fr;
4257 }
4258 }
4259end:
4260 if (foundfr != NULL)
4261 win_goto(foundfr->fr_win);
4262}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
4264/*
4265 * Make window "wp" the current window.
4266 */
4267 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004268win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269{
Bram Moolenaarc917da42016-07-19 22:31:36 +02004270 win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271}
4272
4273/*
4274 * Make window wp the current window.
4275 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4276 * been closed and isn't valid.
4277 */
4278 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004279win_enter_ext(
4280 win_T *wp,
4281 int undo_sync,
4282 int curwin_invalid,
Bram Moolenaar6f470022018-04-10 18:47:20 +02004283 int trigger_new_autocmds,
4284 int trigger_enter_autocmds,
4285 int trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288
4289 if (wp == curwin && !curwin_invalid) /* nothing to do */
4290 return;
4291
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004292 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 {
4294 /*
4295 * Be careful: If autocommands delete the window, return now.
4296 */
4297 if (wp->w_buffer != curbuf)
4298 {
4299 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4300 other_buffer = TRUE;
4301 if (!win_valid(wp))
4302 return;
4303 }
4304 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4305 if (!win_valid(wp))
4306 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004307#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 /* autocmds may abort script processing */
4309 if (aborting())
4310 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313
4314 /* sync undo before leaving the current buffer */
4315 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004316 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004317
4318 /* Might need to scroll the old window before switching, e.g., when the
4319 * cursor was moved. */
4320 update_topline();
4321
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 /* may have to copy the buffer options when 'cpo' contains 'S' */
4323 if (wp->w_buffer != curbuf)
4324 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4325 if (!curwin_invalid)
4326 {
4327 prevwin = curwin; /* remember for CTRL-W p */
4328 curwin->w_redr_status = TRUE;
4329 }
4330 curwin = wp;
4331 curbuf = wp->w_buffer;
4332 check_cursor();
4333#ifdef FEAT_VIRTUALEDIT
4334 if (!virtual_active())
4335 curwin->w_cursor.coladd = 0;
4336#endif
4337 changed_line_abv_curs(); /* assume cursor position needs updating */
4338
4339 if (curwin->w_localdir != NULL)
4340 {
4341 /* Window has a local directory: Save current directory as global
4342 * directory (unless that was done already) and change to the local
4343 * directory. */
4344 if (globaldir == NULL)
4345 {
4346 char_u cwd[MAXPATHL];
4347
4348 if (mch_dirname(cwd, MAXPATHL) == OK)
4349 globaldir = vim_strsave(cwd);
4350 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004351 if (mch_chdir((char *)curwin->w_localdir) == 0)
4352 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354 else if (globaldir != NULL)
4355 {
4356 /* Window doesn't have a local directory and we are not in the global
4357 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004358 ignored = mch_chdir((char *)globaldir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004359 VIM_CLEAR(globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 shorten_fnames(TRUE);
4361 }
4362
Bram Moolenaarc917da42016-07-19 22:31:36 +02004363 if (trigger_new_autocmds)
4364 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004365 if (trigger_enter_autocmds)
4366 {
4367 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4368 if (other_buffer)
4369 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371
4372#ifdef FEAT_TITLE
4373 maketitle();
4374#endif
4375 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004376 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 if (restart_edit)
4378 redraw_later(VALID); /* causes status line redraw */
4379
4380 /* set window height to desired minimal value */
4381 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4382 win_setheight((int)p_wh);
4383 else if (curwin->w_height == 0)
4384 win_setheight(1);
4385
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004387 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 win_setwidth((int)p_wiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389
4390#ifdef FEAT_MOUSE
4391 setmouse(); /* in case jumped to/from help buffer */
4392#endif
4393
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004394 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02004395 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396}
4397
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004400 * Jump to the first open window that contains buffer "buf", if one exists.
4401 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 */
4403 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004404buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004406 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004408 if (curwin->w_buffer == buf)
4409 wp = curwin;
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004410 else
Bram Moolenaar29323592016-07-24 22:04:11 +02004411 FOR_ALL_WINDOWS(wp)
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004412 if (wp->w_buffer == buf)
4413 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 if (wp != NULL)
4415 win_enter(wp, FALSE);
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004416 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004418
4419/*
4420 * Jump to the first open window in any tab page that contains buffer "buf",
4421 * if one exists.
4422 * Returns a pointer to the window found, otherwise NULL.
4423 */
4424 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004425buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004426{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004427 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004428 tabpage_T *tp;
4429
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004430 if (wp != NULL)
4431 return wp;
4432
Bram Moolenaar29323592016-07-24 22:04:11 +02004433 FOR_ALL_TABPAGES(tp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004434 if (tp != curtab)
4435 {
4436 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4437 if (wp->w_buffer == buf)
4438 break;
4439 if (wp != NULL)
4440 {
4441 goto_tabpage_win(tp, wp);
4442 if (curwin != wp)
4443 wp = NULL; /* something went wrong */
4444 break;
4445 }
4446 }
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004447 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004448}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
Bram Moolenaar888ccac2016-06-04 18:49:36 +02004450static int last_win_id = LOWEST_WIN_ID - 1;
Bram Moolenaar86edef62016-03-13 18:07:30 +01004451
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004453 * Allocate a window structure and link it in the window list when "hidden" is
4454 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004457win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004459 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
4461 /*
4462 * allocate window structure and linesizes arrays
4463 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004464 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004465 if (new_wp == NULL)
4466 return NULL;
4467
4468 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004470 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004471 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473
Bram Moolenaar86edef62016-03-13 18:07:30 +01004474 new_wp->w_id = ++last_win_id;
4475
Bram Moolenaar429fa852013-04-15 12:27:36 +02004476#ifdef FEAT_EVAL
4477 /* init w: variables */
4478 new_wp->w_vars = dict_alloc();
4479 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004481 win_free_lsize(new_wp);
4482 vim_free(new_wp);
4483 return NULL;
4484 }
4485 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004486#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004487
Bram Moolenaar429fa852013-04-15 12:27:36 +02004488 /* Don't execute autocommands while the window is not properly
4489 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4490 * event. */
4491 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004492
Bram Moolenaar429fa852013-04-15 12:27:36 +02004493 /*
4494 * link the window in the window list
4495 */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004496 if (!hidden)
4497 win_append(after, new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004498 new_wp->w_wincol = 0;
4499 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
Bram Moolenaar429fa852013-04-15 12:27:36 +02004501 /* position the display and the cursor at the top of the file. */
4502 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004504 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004506 new_wp->w_botline = 2;
4507 new_wp->w_cursor.lnum = 1;
Bram Moolenaar429fa852013-04-15 12:27:36 +02004508 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509
Bram Moolenaar429fa852013-04-15 12:27:36 +02004510 /* We won't calculate w_fraction until resizing the window */
4511 new_wp->w_fraction = 0;
4512 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
4514#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004515 if (gui.in_use)
4516 {
4517 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4518 SBAR_LEFT, new_wp);
4519 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4520 SBAR_RIGHT, new_wp);
4521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522#endif
4523#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004524 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004526 unblock_autocmds();
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004527#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004528 new_wp->w_match_head = NULL;
4529 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004530#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004531 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532}
4533
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004535 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 */
4537 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004538win_free(
4539 win_T *wp,
4540 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541{
4542 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004543 buf_T *buf;
4544 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004546#ifdef FEAT_FOLDING
4547 clearFolding(wp);
4548#endif
4549
4550 /* reduce the reference count to the argument list. */
4551 alist_unlink(wp->w_alist);
4552
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004553 /* Don't execute autocommands while the window is halfway being deleted.
4554 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004555 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004556
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004557#ifdef FEAT_LUA
4558 lua_window_free(wp);
4559#endif
4560
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004561#ifdef FEAT_MZSCHEME
4562 mzscheme_window_free(wp);
4563#endif
4564
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565#ifdef FEAT_PERL
4566 perl_win_free(wp);
4567#endif
4568
4569#ifdef FEAT_PYTHON
4570 python_window_free(wp);
4571#endif
4572
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004573#ifdef FEAT_PYTHON3
4574 python3_window_free(wp);
4575#endif
4576
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577#ifdef FEAT_TCL
4578 tcl_window_free(wp);
4579#endif
4580
4581#ifdef FEAT_RUBY
4582 ruby_window_free(wp);
4583#endif
4584
4585 clear_winopt(&wp->w_onebuf_opt);
4586 clear_winopt(&wp->w_allbuf_opt);
4587
4588#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004589 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4590 hash_init(&wp->w_vars->dv_hashtab);
4591 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592#endif
4593
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004594 {
4595 tabpage_T *ttp;
4596
4597 if (prevwin == wp)
4598 prevwin = NULL;
Bram Moolenaar29323592016-07-24 22:04:11 +02004599 FOR_ALL_TABPAGES(ttp)
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004600 if (ttp->tp_prevwin == wp)
4601 ttp->tp_prevwin = NULL;
4602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 win_free_lsize(wp);
4604
4605 for (i = 0; i < wp->w_tagstacklen; ++i)
4606 vim_free(wp->w_tagstack[i].tagname);
4607
4608 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004609
Bram Moolenaarff18df02013-07-24 17:51:57 +02004610 /* Remove the window from the b_wininfo lists, it may happen that the
4611 * freed memory is re-used for another window. */
Bram Moolenaar29323592016-07-24 22:04:11 +02004612 FOR_ALL_BUFFERS(buf)
Bram Moolenaarff18df02013-07-24 17:51:57 +02004613 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4614 if (wip->wi_win == wp)
4615 wip->wi_win = NULL;
4616
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004618 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004620
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621#ifdef FEAT_JUMPLIST
4622 free_jumplist(wp);
4623#endif
4624
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004625#ifdef FEAT_QUICKFIX
4626 qf_free_all(wp);
4627#endif
4628
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629#ifdef FEAT_GUI
4630 if (gui.in_use)
4631 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4633 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4634 }
4635#endif /* FEAT_GUI */
4636
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004637#ifdef FEAT_MENU
4638 remove_winbar(wp);
4639#endif
4640
Bram Moolenaar860cae12010-06-05 23:22:07 +02004641#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004642 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004643#endif
4644
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004645 if (wp != aucmd_win)
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004646 win_remove(wp, tp);
Bram Moolenaar3be85852014-06-12 14:01:31 +02004647 if (autocmd_busy)
4648 {
4649 wp->w_next = au_pending_free_win;
4650 au_pending_free_win = wp;
4651 }
4652 else
4653 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004654
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004655 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656}
4657
4658/*
4659 * Append window "wp" in the window list after window "after".
4660 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004661 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004662win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663{
4664 win_T *before;
4665
4666 if (after == NULL) /* after NULL is in front of the first */
4667 before = firstwin;
4668 else
4669 before = after->w_next;
4670
4671 wp->w_next = before;
4672 wp->w_prev = after;
4673 if (after == NULL)
4674 firstwin = wp;
4675 else
4676 after->w_next = wp;
4677 if (before == NULL)
4678 lastwin = wp;
4679 else
4680 before->w_prev = wp;
4681}
4682
4683/*
4684 * Remove a window from the window list.
4685 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004686 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004687win_remove(
4688 win_T *wp,
4689 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690{
4691 if (wp->w_prev != NULL)
4692 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004693 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004694 firstwin = curtab->tp_firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004695 else
4696 tp->tp_firstwin = wp->w_next;
Bram Moolenaar816968d2017-09-29 21:29:18 +02004697
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 if (wp->w_next != NULL)
4699 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004700 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004701 lastwin = curtab->tp_lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004702 else
4703 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704}
4705
4706/*
4707 * Append frame "frp" in a frame list after frame "after".
4708 */
4709 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004710frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711{
4712 frp->fr_next = after->fr_next;
4713 after->fr_next = frp;
4714 if (frp->fr_next != NULL)
4715 frp->fr_next->fr_prev = frp;
4716 frp->fr_prev = after;
4717}
4718
4719/*
4720 * Insert frame "frp" in a frame list before frame "before".
4721 */
4722 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004723frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724{
4725 frp->fr_next = before;
4726 frp->fr_prev = before->fr_prev;
4727 before->fr_prev = frp;
4728 if (frp->fr_prev != NULL)
4729 frp->fr_prev->fr_next = frp;
4730 else
4731 frp->fr_parent->fr_child = frp;
4732}
4733
4734/*
4735 * Remove a frame from a frame list.
4736 */
4737 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004738frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739{
4740 if (frp->fr_prev != NULL)
4741 frp->fr_prev->fr_next = frp->fr_next;
4742 else
Bram Moolenaar6f361c92018-01-31 19:06:50 +01004743 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 frp->fr_parent->fr_child = frp->fr_next;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01004745 /* special case: topframe->fr_child == frp */
4746 if (topframe->fr_child == frp)
4747 topframe->fr_child = frp->fr_next;
4748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 if (frp->fr_next != NULL)
4750 frp->fr_next->fr_prev = frp->fr_prev;
4751}
4752
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753/*
4754 * Allocate w_lines[] for window "wp".
4755 * Return FAIL for failure, OK for success.
4756 */
4757 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004758win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759{
4760 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004761 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 if (wp->w_lines == NULL)
4763 return FAIL;
4764 return OK;
4765}
4766
4767/*
4768 * free lsize arrays for a window
4769 */
4770 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004771win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004773 /* TODO: why would wp be NULL here? */
4774 if (wp != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004775 VIM_CLEAR(wp->w_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776}
4777
4778/*
4779 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004780 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 */
4782 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004783shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004785 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786
4787 if (firstwin == NULL) /* not initialized yet */
4788 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 if (h < frame_minheight(topframe, NULL))
4790 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004791
4792 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 * that doesn't result in the right height, forget about that option. */
4794 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004795 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 frame_new_height(topframe, h, FALSE, FALSE);
4797
4798 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 compute_cmdrow();
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004800 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004801
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802#if 0
4803 /* Disabled: don't want making the screen smaller make a window larger. */
4804 if (p_ea)
4805 win_equal(curwin, FALSE, 'v');
4806#endif
4807}
4808
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809/*
4810 * Called from win_new_shellsize() after Columns changed.
4811 */
4812 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004813shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814{
4815 if (firstwin == NULL) /* not initialized yet */
4816 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004817
4818 /* First try setting the widths of windows with 'winfixwidth'. If that
4819 * doesn't result in the right width, forget about that option. */
4820 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004821 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004822 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4823
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4825#if 0
4826 /* Disabled: don't want making the screen smaller make a window larger. */
4827 if (p_ea)
4828 win_equal(curwin, FALSE, 'h');
4829#endif
4830}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831
4832#if defined(FEAT_CMDWIN) || defined(PROTO)
4833/*
4834 * Save the size of all windows in "gap".
4835 */
4836 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004837win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838
4839{
4840 win_T *wp;
4841
4842 ga_init2(gap, (int)sizeof(int), 1);
4843 if (ga_grow(gap, win_count() * 2) == OK)
Bram Moolenaar29323592016-07-24 22:04:11 +02004844 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 {
4846 ((int *)gap->ga_data)[gap->ga_len++] =
4847 wp->w_width + wp->w_vsep_width;
4848 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4849 }
4850}
4851
4852/*
4853 * Restore window sizes, but only if the number of windows is still the same.
4854 * Does not free the growarray.
4855 */
4856 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004857win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858{
4859 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004860 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861
4862 if (win_count() * 2 == gap->ga_len)
4863 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004864 /* The order matters, because frames contain other frames, but it's
4865 * difficult to get right. The easy way out is to do it twice. */
4866 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004868 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004869 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb643e772014-07-16 15:18:26 +02004870 {
4871 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4872 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 }
4875 /* recompute the window positions */
4876 (void)win_comp_pos();
4877 }
4878}
4879#endif /* FEAT_CMDWIN */
4880
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881/*
4882 * Update the position for all windows, using the width and height of the
4883 * frames.
4884 * Returns the row just after the last window.
4885 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004886 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004887win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004889 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 int col = 0;
4891
4892 frame_comp_pos(topframe, &row, &col);
4893 return row;
4894}
4895
4896/*
4897 * Update the position of the windows in frame "topfrp", using the width and
4898 * height of the frames.
4899 * "*row" and "*col" are the top-left position of the frame. They are updated
4900 * to the bottom-right position plus one.
4901 */
4902 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004903frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904{
4905 win_T *wp;
4906 frame_T *frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 int startcol;
4908 int startrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01004909 int h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910
4911 wp = topfrp->fr_win;
4912 if (wp != NULL)
4913 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004914 if (wp->w_winrow != *row || wp->w_wincol != *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
4916 /* position changed, redraw */
4917 wp->w_winrow = *row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 wp->w_wincol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 redraw_win_later(wp, NOT_VALID);
4920 wp->w_redr_status = TRUE;
4921 }
Bram Moolenaar415a6932017-12-05 20:31:07 +01004922 /* WinBar will not show if the window height is zero */
4923 h = VISIBLE_HEIGHT(wp) + wp->w_status_height;
4924 *row += h > topfrp->fr_height ? topfrp->fr_height : h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 *col += wp->w_width + wp->w_vsep_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
4927 else
4928 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 startrow = *row;
4930 startcol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 if (topfrp->fr_layout == FR_ROW)
4934 *row = startrow; /* all frames are at the same row */
4935 else
4936 *col = startcol; /* all frames are at the same col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 frame_comp_pos(frp, row, col);
4938 }
4939 }
4940}
4941
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942/*
4943 * Set current window height and take care of repositioning other windows to
4944 * fit around it.
4945 */
4946 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004947win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948{
4949 win_setheight_win(height, curwin);
4950}
4951
4952/*
4953 * Set the window height of window "win" and take care of repositioning other
4954 * windows to fit around it.
4955 */
4956 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004957win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958{
4959 int row;
4960
4961 if (win == curwin)
4962 {
4963 /* Always keep current window at least one line high, even when
4964 * 'winminheight' is zero. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 if (height < p_wmh)
4966 height = p_wmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 if (height == 0)
4968 height = 1;
Bram Moolenaar415a6932017-12-05 20:31:07 +01004969 height += WINBAR_HEIGHT(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 }
4971
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 frame_setheight(win->w_frame, height + win->w_status_height);
4973
4974 /* recompute the window positions */
4975 row = win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976
4977 /*
4978 * If there is extra space created between the last window and the command
4979 * line, clear it.
4980 */
4981 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4982 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4983 cmdline_row = row;
4984 msg_row = row;
4985 msg_col = 0;
4986
4987 redraw_all_later(NOT_VALID);
4988}
4989
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990/*
4991 * Set the height of a frame to "height" and take care that all frames and
4992 * windows inside it are resized. Also resize frames on the left and right if
4993 * the are in the same FR_ROW frame.
4994 *
4995 * Strategy:
4996 * If the frame is part of a FR_COL frame, try fitting the frame in that
4997 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4998 * go to containing frames to resize them and make room.
4999 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5000 * Check for the minimal height of the FR_ROW frame.
5001 * At the top level we can also use change the command line height.
5002 */
5003 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005004frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005{
5006 int room; /* total number of lines available */
5007 int take; /* number of lines taken from other windows */
5008 int room_cmdline; /* lines available from cmdline */
5009 int run;
5010 frame_T *frp;
5011 int h;
5012 int room_reserved;
5013
5014 /* If the height already is the desired value, nothing to do. */
5015 if (curfrp->fr_height == height)
5016 return;
5017
5018 if (curfrp->fr_parent == NULL)
5019 {
5020 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005021 if (height > ROWS_AVAIL)
5022 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 if (height > 0)
5024 frame_new_height(curfrp, height, FALSE, FALSE);
5025 }
5026 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5027 {
5028 /* Row of frames: Also need to resize frames left and right of this
5029 * one. First check for the minimal height of these. */
5030 h = frame_minheight(curfrp->fr_parent, NULL);
5031 if (height < h)
5032 height = h;
5033 frame_setheight(curfrp->fr_parent, height);
5034 }
5035 else
5036 {
5037 /*
5038 * Column of frames: try to change only frames in this column.
5039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 /*
5041 * Do this twice:
5042 * 1: compute room available, if it's not enough try resizing the
5043 * containing frame.
5044 * 2: compute the room available and adjust the height to it.
5045 * Try not to reduce the height of a window with 'winfixheight' set.
5046 */
5047 for (run = 1; run <= 2; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {
5049 room = 0;
5050 room_reserved = 0;
5051 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5052 frp = frp->fr_next)
5053 {
5054 if (frp != curfrp
5055 && frp->fr_win != NULL
5056 && frp->fr_win->w_p_wfh)
5057 room_reserved += frp->fr_height;
5058 room += frp->fr_height;
5059 if (frp != curfrp)
5060 room -= frame_minheight(frp, NULL);
5061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 if (curfrp->fr_width != Columns)
5063 room_cmdline = 0;
5064 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
5066 room_cmdline = Rows - p_ch - (lastwin->w_winrow
Bram Moolenaar415a6932017-12-05 20:31:07 +01005067 + VISIBLE_HEIGHT(lastwin)
5068 + lastwin->w_status_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 if (room_cmdline < 0)
5070 room_cmdline = 0;
5071 }
5072
5073 if (height <= room + room_cmdline)
5074 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 if (run == 2 || curfrp->fr_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 {
5077 if (height > room + room_cmdline)
5078 height = room + room_cmdline;
5079 break;
5080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 frame_setheight(curfrp->fr_parent, height
5082 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 }
5084
5085 /*
5086 * Compute the number of lines we will take from others frames (can be
5087 * negative!).
5088 */
5089 take = height - curfrp->fr_height;
5090
5091 /* If there is not enough room, also reduce the height of a window
5092 * with 'winfixheight' set. */
5093 if (height > room + room_cmdline - room_reserved)
5094 room_reserved = room + room_cmdline - height;
5095 /* If there is only a 'winfixheight' window and making the
5096 * window smaller, need to make the other window taller. */
5097 if (take < 0 && room - curfrp->fr_height < room_reserved)
5098 room_reserved = 0;
5099
5100 if (take > 0 && room_cmdline > 0)
5101 {
5102 /* use lines from cmdline first */
5103 if (take < room_cmdline)
5104 room_cmdline = take;
5105 take -= room_cmdline;
5106 topframe->fr_height += room_cmdline;
5107 }
5108
5109 /*
5110 * set the current frame to the new height
5111 */
5112 frame_new_height(curfrp, height, FALSE, FALSE);
5113
5114 /*
5115 * First take lines from the frames after the current frame. If
5116 * that is not enough, takes lines from frames above the current
5117 * frame.
5118 */
5119 for (run = 0; run < 2; ++run)
5120 {
5121 if (run == 0)
5122 frp = curfrp->fr_next; /* 1st run: start with next window */
5123 else
5124 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5125 while (frp != NULL && take != 0)
5126 {
5127 h = frame_minheight(frp, NULL);
5128 if (room_reserved > 0
5129 && frp->fr_win != NULL
5130 && frp->fr_win->w_p_wfh)
5131 {
5132 if (room_reserved >= frp->fr_height)
5133 room_reserved -= frp->fr_height;
5134 else
5135 {
5136 if (frp->fr_height - room_reserved > take)
5137 room_reserved = frp->fr_height - take;
5138 take -= frp->fr_height - room_reserved;
5139 frame_new_height(frp, room_reserved, FALSE, FALSE);
5140 room_reserved = 0;
5141 }
5142 }
5143 else
5144 {
5145 if (frp->fr_height - take < h)
5146 {
5147 take -= frp->fr_height - h;
5148 frame_new_height(frp, h, FALSE, FALSE);
5149 }
5150 else
5151 {
5152 frame_new_height(frp, frp->fr_height - take,
5153 FALSE, FALSE);
5154 take = 0;
5155 }
5156 }
5157 if (run == 0)
5158 frp = frp->fr_next;
5159 else
5160 frp = frp->fr_prev;
5161 }
5162 }
5163 }
5164}
5165
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166/*
5167 * Set current window width and take care of repositioning other windows to
5168 * fit around it.
5169 */
5170 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005171win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172{
5173 win_setwidth_win(width, curwin);
5174}
5175
5176 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005177win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178{
5179 /* Always keep current window at least one column wide, even when
5180 * 'winminwidth' is zero. */
5181 if (wp == curwin)
5182 {
5183 if (width < p_wmw)
5184 width = p_wmw;
5185 if (width == 0)
5186 width = 1;
5187 }
5188
5189 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5190
5191 /* recompute the window positions */
5192 (void)win_comp_pos();
5193
5194 redraw_all_later(NOT_VALID);
5195}
5196
5197/*
5198 * Set the width of a frame to "width" and take care that all frames and
5199 * windows inside it are resized. Also resize frames above and below if the
5200 * are in the same FR_ROW frame.
5201 *
5202 * Strategy is similar to frame_setheight().
5203 */
5204 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005205frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206{
5207 int room; /* total number of lines available */
5208 int take; /* number of lines taken from other windows */
5209 int run;
5210 frame_T *frp;
5211 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005212 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213
5214 /* If the width already is the desired value, nothing to do. */
5215 if (curfrp->fr_width == width)
5216 return;
5217
5218 if (curfrp->fr_parent == NULL)
5219 /* topframe: can't change width */
5220 return;
5221
5222 if (curfrp->fr_parent->fr_layout == FR_COL)
5223 {
5224 /* Column of frames: Also need to resize frames above and below of
5225 * this one. First check for the minimal width of these. */
5226 w = frame_minwidth(curfrp->fr_parent, NULL);
5227 if (width < w)
5228 width = w;
5229 frame_setwidth(curfrp->fr_parent, width);
5230 }
5231 else
5232 {
5233 /*
5234 * Row of frames: try to change only frames in this row.
5235 *
5236 * Do this twice:
5237 * 1: compute room available, if it's not enough try resizing the
5238 * containing frame.
5239 * 2: compute the room available and adjust the width to it.
5240 */
5241 for (run = 1; run <= 2; ++run)
5242 {
5243 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005244 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5246 frp = frp->fr_next)
5247 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005248 if (frp != curfrp
5249 && frp->fr_win != NULL
5250 && frp->fr_win->w_p_wfw)
5251 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 room += frp->fr_width;
5253 if (frp != curfrp)
5254 room -= frame_minwidth(frp, NULL);
5255 }
5256
5257 if (width <= room)
5258 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005259 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 {
5261 if (width > room)
5262 width = room;
5263 break;
5264 }
5265 frame_setwidth(curfrp->fr_parent, width
5266 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5267 }
5268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 /*
5270 * Compute the number of lines we will take from others frames (can be
5271 * negative!).
5272 */
5273 take = width - curfrp->fr_width;
5274
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005275 /* If there is not enough room, also reduce the width of a window
5276 * with 'winfixwidth' set. */
5277 if (width > room - room_reserved)
5278 room_reserved = room - width;
5279 /* If there is only a 'winfixwidth' window and making the
5280 * window smaller, need to make the other window narrower. */
5281 if (take < 0 && room - curfrp->fr_width < room_reserved)
5282 room_reserved = 0;
5283
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 /*
5285 * set the current frame to the new width
5286 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005287 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288
5289 /*
5290 * First take lines from the frames right of the current frame. If
5291 * that is not enough, takes lines from frames left of the current
5292 * frame.
5293 */
5294 for (run = 0; run < 2; ++run)
5295 {
5296 if (run == 0)
5297 frp = curfrp->fr_next; /* 1st run: start with next window */
5298 else
5299 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5300 while (frp != NULL && take != 0)
5301 {
5302 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005303 if (room_reserved > 0
5304 && frp->fr_win != NULL
5305 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005307 if (room_reserved >= frp->fr_width)
5308 room_reserved -= frp->fr_width;
5309 else
5310 {
5311 if (frp->fr_width - room_reserved > take)
5312 room_reserved = frp->fr_width - take;
5313 take -= frp->fr_width - room_reserved;
5314 frame_new_width(frp, room_reserved, FALSE, FALSE);
5315 room_reserved = 0;
5316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 }
5318 else
5319 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005320 if (frp->fr_width - take < w)
5321 {
5322 take -= frp->fr_width - w;
5323 frame_new_width(frp, w, FALSE, FALSE);
5324 }
5325 else
5326 {
5327 frame_new_width(frp, frp->fr_width - take,
5328 FALSE, FALSE);
5329 take = 0;
5330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 }
5332 if (run == 0)
5333 frp = frp->fr_next;
5334 else
5335 frp = frp->fr_prev;
5336 }
5337 }
5338 }
5339}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340
5341/*
5342 * Check 'winminheight' for a valid value.
5343 */
5344 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005345win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
5347 int room;
5348 int first = TRUE;
5349 win_T *wp;
5350
5351 /* loop until there is a 'winminheight' that is possible */
5352 while (p_wmh > 0)
5353 {
5354 /* TODO: handle vertical splits */
5355 room = -p_wh;
Bram Moolenaar29323592016-07-24 22:04:11 +02005356 FOR_ALL_WINDOWS(wp)
Bram Moolenaar415a6932017-12-05 20:31:07 +01005357 room += VISIBLE_HEIGHT(wp) - p_wmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 if (room >= 0)
5359 break;
5360 --p_wmh;
5361 if (first)
5362 {
5363 EMSG(_(e_noroom));
5364 first = FALSE;
5365 }
5366 }
5367}
5368
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005369#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370
5371/*
5372 * Status line of dragwin is dragged "offset" lines down (negative is up).
5373 */
5374 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005375win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
5377 frame_T *curfr;
5378 frame_T *fr;
5379 int room;
5380 int row;
5381 int up; /* if TRUE, drag status line up, otherwise down */
5382 int n;
5383
5384 fr = dragwin->w_frame;
5385 curfr = fr;
5386 if (fr != topframe) /* more than one window */
5387 {
5388 fr = fr->fr_parent;
5389 /* When the parent frame is not a column of frames, its parent should
5390 * be. */
5391 if (fr->fr_layout != FR_COL)
5392 {
5393 curfr = fr;
5394 if (fr != topframe) /* only a row of windows, may drag statusline */
5395 fr = fr->fr_parent;
5396 }
5397 }
5398
5399 /* If this is the last frame in a column, may want to resize the parent
5400 * frame instead (go two up to skip a row of frames). */
5401 while (curfr != topframe && curfr->fr_next == NULL)
5402 {
5403 if (fr != topframe)
5404 fr = fr->fr_parent;
5405 curfr = fr;
5406 if (fr != topframe)
5407 fr = fr->fr_parent;
5408 }
5409
5410 if (offset < 0) /* drag up */
5411 {
5412 up = TRUE;
5413 offset = -offset;
5414 /* sum up the room of the current frame and above it */
5415 if (fr == curfr)
5416 {
5417 /* only one window */
5418 room = fr->fr_height - frame_minheight(fr, NULL);
5419 }
5420 else
5421 {
5422 room = 0;
5423 for (fr = fr->fr_child; ; fr = fr->fr_next)
5424 {
5425 room += fr->fr_height - frame_minheight(fr, NULL);
5426 if (fr == curfr)
5427 break;
5428 }
5429 }
5430 fr = curfr->fr_next; /* put fr at frame that grows */
5431 }
5432 else /* drag down */
5433 {
5434 up = FALSE;
5435 /*
5436 * Only dragging the last status line can reduce p_ch.
5437 */
5438 room = Rows - cmdline_row;
5439 if (curfr->fr_next == NULL)
5440 room -= 1;
5441 else
5442 room -= p_ch;
5443 if (room < 0)
5444 room = 0;
5445 /* sum up the room of frames below of the current one */
5446 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5447 room += fr->fr_height - frame_minheight(fr, NULL);
5448 fr = curfr; /* put fr at window that grows */
5449 }
5450
5451 if (room < offset) /* Not enough room */
5452 offset = room; /* Move as far as we can */
5453 if (offset <= 0)
5454 return;
5455
5456 /*
5457 * Grow frame fr by "offset" lines.
5458 * Doesn't happen when dragging the last status line up.
5459 */
5460 if (fr != NULL)
5461 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5462
5463 if (up)
5464 fr = curfr; /* current frame gets smaller */
5465 else
5466 fr = curfr->fr_next; /* next frame gets smaller */
5467
5468 /*
5469 * Now make the other frames smaller.
5470 */
5471 while (fr != NULL && offset > 0)
5472 {
5473 n = frame_minheight(fr, NULL);
5474 if (fr->fr_height - offset <= n)
5475 {
5476 offset -= fr->fr_height - n;
5477 frame_new_height(fr, n, !up, FALSE);
5478 }
5479 else
5480 {
5481 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5482 break;
5483 }
5484 if (up)
5485 fr = fr->fr_prev;
5486 else
5487 fr = fr->fr_next;
5488 }
5489 row = win_comp_pos();
5490 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5491 cmdline_row = row;
5492 p_ch = Rows - cmdline_row;
5493 if (p_ch < 1)
5494 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005495 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005496 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 showmode();
5498}
5499
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500/*
5501 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5502 */
5503 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005504win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
5506 frame_T *curfr;
5507 frame_T *fr;
5508 int room;
5509 int left; /* if TRUE, drag separator line left, otherwise right */
5510 int n;
5511
5512 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005513 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 return;
5515 curfr = fr;
5516 fr = fr->fr_parent;
5517 /* When the parent frame is not a row of frames, its parent should be. */
5518 if (fr->fr_layout != FR_ROW)
5519 {
5520 if (fr == topframe) /* only a column of windows (cannot happen?) */
5521 return;
5522 curfr = fr;
5523 fr = fr->fr_parent;
5524 }
5525
5526 /* If this is the last frame in a row, may want to resize a parent
5527 * frame instead. */
5528 while (curfr->fr_next == NULL)
5529 {
5530 if (fr == topframe)
5531 break;
5532 curfr = fr;
5533 fr = fr->fr_parent;
5534 if (fr != topframe)
5535 {
5536 curfr = fr;
5537 fr = fr->fr_parent;
5538 }
5539 }
5540
5541 if (offset < 0) /* drag left */
5542 {
5543 left = TRUE;
5544 offset = -offset;
5545 /* sum up the room of the current frame and left of it */
5546 room = 0;
5547 for (fr = fr->fr_child; ; fr = fr->fr_next)
5548 {
5549 room += fr->fr_width - frame_minwidth(fr, NULL);
5550 if (fr == curfr)
5551 break;
5552 }
5553 fr = curfr->fr_next; /* put fr at frame that grows */
5554 }
5555 else /* drag right */
5556 {
5557 left = FALSE;
5558 /* sum up the room of frames right of the current one */
5559 room = 0;
5560 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5561 room += fr->fr_width - frame_minwidth(fr, NULL);
5562 fr = curfr; /* put fr at window that grows */
5563 }
5564
5565 if (room < offset) /* Not enough room */
5566 offset = room; /* Move as far as we can */
5567 if (offset <= 0) /* No room at all, quit. */
5568 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005569 if (fr == NULL)
5570 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571
5572 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005573 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
5575 /* shrink other frames: current and at the left or at the right */
5576 if (left)
5577 fr = curfr; /* current frame gets smaller */
5578 else
5579 fr = curfr->fr_next; /* next frame gets smaller */
5580
5581 while (fr != NULL && offset > 0)
5582 {
5583 n = frame_minwidth(fr, NULL);
5584 if (fr->fr_width - offset <= n)
5585 {
5586 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005587 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 }
5589 else
5590 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005591 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 break;
5593 }
5594 if (left)
5595 fr = fr->fr_prev;
5596 else
5597 fr = fr->fr_next;
5598 }
5599 (void)win_comp_pos();
5600 redraw_all_later(NOT_VALID);
5601}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602#endif /* FEAT_MOUSE */
5603
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005604#define FRACTION_MULT 16384L
5605
5606/*
5607 * Set wp->w_fraction for the current w_wrow and w_height.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005608 * Has no effect when the window is less than two lines.
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005609 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005610 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005611set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005612{
Bram Moolenaar3679c172017-11-22 22:22:11 +01005613 if (wp->w_height > 1)
5614 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005615 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005616}
5617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618/*
5619 * Set the height of a window.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005620 * "height" excludes any window toolbar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 * This takes care of the things inside the window, not what happens to the
5622 * window position, the frame or to other windows.
5623 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005624 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005625win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005627 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628
5629 /* Don't want a negative height. Happens when splitting a tiny window.
5630 * Will equalize heights soon to fix it. */
5631 if (height < 0)
5632 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005633 if (wp->w_height == height)
5634 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005636 if (wp->w_height > 0)
5637 {
5638 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005639 /* w_wrow needs to be valid. When setting 'laststatus' this may
5640 * call win_new_height() recursively. */
5641 validate_cursor();
5642 if (wp->w_height != prev_height)
5643 return; /* Recursive call already changed the size, bail out here
5644 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005645 if (wp->w_wrow != wp->w_prev_fraction_row)
5646 set_fraction(wp);
5647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
5649 wp->w_height = height;
5650 wp->w_skipcol = 0;
5651
Bram Moolenaar955f1982017-02-05 15:10:51 +01005652 /* There is no point in adjusting the scroll position when exiting. Some
5653 * values might be invalid. */
5654 if (!exiting)
5655 scroll_to_fraction(wp, prev_height);
Bram Moolenaar46328f92016-08-28 15:39:57 +02005656}
5657
5658 void
5659scroll_to_fraction(win_T *wp, int prev_height)
5660{
5661 linenr_T lnum;
5662 int sline, line_size;
5663 int height = wp->w_height;
5664
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 /* Don't change w_topline when height is zero. Don't set w_topline when
5666 * 'scrollbind' is set and this isn't the current window. */
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005667 if (height > 0 && (!wp->w_p_scb || wp == curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005669 /*
5670 * Find a value for w_topline that shows the cursor at the same
5671 * relative position in the window as before (more or less).
5672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 lnum = wp->w_cursor.lnum;
5674 if (lnum < 1) /* can happen when starting up */
5675 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005676 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5677 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5679 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005680
5681 if (sline >= 0)
5682 {
5683 /* Make sure the whole cursor line is visible, if possible. */
5684 int rows = plines_win(wp, lnum, FALSE);
5685
5686 if (sline > wp->w_height - rows)
5687 {
5688 sline = wp->w_height - rows;
5689 wp->w_wrow -= rows - line_size;
5690 }
5691 }
5692
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 if (sline < 0)
5694 {
5695 /*
5696 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005697 * Make cursor line the first line in the window. If not enough
5698 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 */
5700 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005701 if (wp->w_wrow >= wp->w_height
Bram Moolenaar02631462017-09-22 15:20:32 +02005702 && (wp->w_width - win_col_off(wp)) > 0)
Bram Moolenaar26470632006-10-24 19:12:40 +00005703 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005704 wp->w_skipcol += wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00005705 --wp->w_wrow;
5706 while (wp->w_wrow >= wp->w_height)
5707 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005708 wp->w_skipcol += wp->w_width - win_col_off(wp)
Bram Moolenaar26470632006-10-24 19:12:40 +00005709 + win_col_off2(wp);
5710 --wp->w_wrow;
5711 }
5712 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005713 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005715 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005717 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
5719#ifdef FEAT_FOLDING
5720 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5721 if (lnum == 1)
5722 {
5723 /* first line in buffer is folded */
5724 line_size = 1;
5725 --sline;
5726 break;
5727 }
5728#endif
5729 --lnum;
5730#ifdef FEAT_DIFF
5731 if (lnum == wp->w_topline)
5732 line_size = plines_win_nofill(wp, lnum, TRUE)
5733 + wp->w_topfill;
5734 else
5735#endif
5736 line_size = plines_win(wp, lnum, TRUE);
5737 sline -= line_size;
5738 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005739
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 if (sline < 0)
5741 {
5742 /*
5743 * Line we want at top would go off top of screen. Use next
5744 * line instead.
5745 */
5746#ifdef FEAT_FOLDING
5747 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5748#endif
5749 lnum++;
5750 wp->w_wrow -= line_size + sline;
5751 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005752 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
5754 /* First line of file reached, use that as topline. */
5755 lnum = 1;
5756 wp->w_wrow -= sline;
5757 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005758
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005759 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 }
5762
5763 if (wp == curwin)
5764 {
5765 if (p_so)
5766 update_topline();
5767 curs_columns(FALSE); /* validate w_wrow */
5768 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005769 if (prev_height > 0)
5770 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771
5772 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005773 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 invalidate_botline_win(wp);
5776}
5777
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778/*
5779 * Set the width of a window.
5780 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005781 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005782win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783{
5784 wp->w_width = width;
5785 wp->w_lines_valid = 0;
5786 changed_line_abv_curs_win(wp);
5787 invalidate_botline_win(wp);
5788 if (wp == curwin)
5789 {
5790 update_topline();
5791 curs_columns(TRUE); /* validate w_wrow */
5792 }
5793 redraw_win_later(wp, NOT_VALID);
5794 wp->w_redr_status = TRUE;
5795}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
5797 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005798win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
5800 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5801 if (wp->w_p_scr == 0)
5802 wp->w_p_scr = 1;
5803}
5804
5805/*
5806 * command_height: called whenever p_ch has been changed
5807 */
5808 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005809command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 int h;
5812 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005813 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005815 /* Use the value of p_ch that we remembered. This is needed for when the
5816 * GUI starts up, we can't be sure in what order things happen. And when
5817 * p_ch was changed in another tab page. */
5818 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 /* Find bottom frame with width of screen. */
5821 frp = lastwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5823 frp = frp->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824
5825 /* Avoid changing the height of a window with 'winfixheight' set. */
5826 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5827 && frp->fr_win->w_p_wfh)
5828 frp = frp->fr_prev;
5829
5830 if (starting != NO_SCREEN)
5831 {
5832 cmdline_row = Rows - p_ch;
5833
5834 if (p_ch > old_p_ch) /* p_ch got bigger */
5835 {
5836 while (p_ch > old_p_ch)
5837 {
5838 if (frp == NULL)
5839 {
5840 EMSG(_(e_noroom));
5841 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005842 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 cmdline_row = Rows - p_ch;
5844 break;
5845 }
5846 h = frp->fr_height - frame_minheight(frp, NULL);
5847 if (h > p_ch - old_p_ch)
5848 h = p_ch - old_p_ch;
5849 old_p_ch += h;
5850 frame_add_height(frp, -h);
5851 frp = frp->fr_prev;
5852 }
5853
5854 /* Recompute window positions. */
5855 (void)win_comp_pos();
5856
5857 /* clear the lines added to cmdline */
5858 if (full_screen)
5859 screen_fill((int)(cmdline_row), (int)Rows, 0,
5860 (int)Columns, ' ', ' ', 0);
5861 msg_row = cmdline_row;
5862 redraw_cmdline = TRUE;
5863 return;
5864 }
5865
5866 if (msg_row < cmdline_row)
5867 msg_row = cmdline_row;
5868 redraw_cmdline = TRUE;
5869 }
5870 frame_add_height(frp, (int)(old_p_ch - p_ch));
5871
5872 /* Recompute window positions. */
5873 if (frp != lastwin->w_frame)
5874 (void)win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875}
5876
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877/*
5878 * Resize frame "frp" to be "n" lines higher (negative for less high).
5879 * Also resize the frames it is contained in.
5880 */
5881 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005882frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883{
5884 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5885 for (;;)
5886 {
5887 frp = frp->fr_parent;
5888 if (frp == NULL)
5889 break;
5890 frp->fr_height += n;
5891 }
5892}
5893
5894/*
5895 * Add or remove a status line for the bottom window(s), according to the
5896 * value of 'laststatus'.
5897 */
5898 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005899last_status(
5900 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901{
5902 /* Don't make a difference between horizontal or vertical split. */
5903 last_status_rec(topframe, (p_ls == 2
Bram Moolenaar459ca562016-11-10 18:16:33 +01005904 || (p_ls == 1 && (morewin || !ONE_WINDOW))));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905}
5906
5907 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005908last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909{
5910 frame_T *fp;
5911 win_T *wp;
5912
5913 if (fr->fr_layout == FR_LEAF)
5914 {
5915 wp = fr->fr_win;
5916 if (wp->w_status_height != 0 && !statusline)
5917 {
5918 /* remove status line */
5919 win_new_height(wp, wp->w_height + 1);
5920 wp->w_status_height = 0;
5921 comp_col();
5922 }
5923 else if (wp->w_status_height == 0 && statusline)
5924 {
5925 /* Find a frame to take a line from. */
5926 fp = fr;
5927 while (fp->fr_height <= frame_minheight(fp, NULL))
5928 {
5929 if (fp == topframe)
5930 {
5931 EMSG(_(e_noroom));
5932 return;
5933 }
5934 /* In a column of frames: go to frame above. If already at
5935 * the top or in a row of frames: go to parent. */
5936 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5937 fp = fp->fr_prev;
5938 else
5939 fp = fp->fr_parent;
5940 }
5941 wp->w_status_height = 1;
5942 if (fp != fr)
5943 {
5944 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5945 frame_fix_height(wp);
5946 (void)win_comp_pos();
5947 }
5948 else
5949 win_new_height(wp, wp->w_height - 1);
5950 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005951 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 }
5953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 else if (fr->fr_layout == FR_ROW)
5955 {
5956 /* vertically split windows, set status line for each one */
5957 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5958 last_status_rec(fp, statusline);
5959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 else
5961 {
5962 /* horizontally split window, set status line for last one */
5963 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5964 ;
5965 last_status_rec(fp, statusline);
5966 }
5967}
5968
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005969/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005970 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005971 */
5972 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005973tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005974{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005975#ifdef FEAT_GUI_TABLINE
5976 /* When the GUI has the tabline then this always returns zero. */
5977 if (gui_use_tabline())
5978 return 0;
5979#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005980 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005981 {
5982 case 0: return 0;
5983 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5984 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005985 return 1;
5986}
5987
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5989/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005990 * Get the file name at the cursor.
5991 * If Visual mode is active, use the selected text if it's in one line.
5992 * Returns the name in allocated memory, NULL for failure.
5993 */
5994 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005995grab_file_name(long count, linenr_T *file_lnum)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005996{
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005997 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
5998
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005999 if (VIsual_active)
6000 {
6001 int len;
6002 char_u *ptr;
6003
6004 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6005 return NULL;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006006 return find_file_name_in_path(ptr, len, options,
6007 count, curbuf->b_ffname);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006008 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006009 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006010}
6011
6012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 * Return the file name under or after the cursor.
6014 *
6015 * The 'path' option is searched if the file name is not absolute.
6016 * The string returned has been alloc'ed and should be freed by the caller.
6017 * NULL is returned if the file name or file is not found.
6018 *
6019 * options:
6020 * FNAME_MESS give error messages
6021 * FNAME_EXP expand to path
6022 * FNAME_HYP check for hypertext link
6023 * FNAME_INCL apply "includeexpr"
6024 */
6025 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006026file_name_at_cursor(int options, long count, linenr_T *file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027{
6028 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006029 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6030 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031}
6032
6033/*
6034 * Return the name of the file under or after ptr[col].
6035 * Otherwise like file_name_at_cursor().
6036 */
6037 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006038file_name_in_line(
6039 char_u *line,
6040 int col,
6041 int options,
6042 long count,
6043 char_u *rel_fname, /* file we are searching relative to */
6044 linenr_T *file_lnum) /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045{
6046 char_u *ptr;
6047 int len;
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +01006048 int in_type = TRUE;
6049 int is_url = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050
6051 /*
6052 * search forward for what could be the start of a file name
6053 */
6054 ptr = line + col;
6055 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006056 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 if (*ptr == NUL) /* nothing found */
6058 {
6059 if (options & FNAME_MESS)
6060 EMSG(_("E446: No file name under cursor"));
6061 return NULL;
6062 }
6063
6064 /*
6065 * Search backward for first char of the file name.
6066 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6067 */
6068 while (ptr > line)
6069 {
6070#ifdef FEAT_MBYTE
6071 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6072 ptr -= len + 1;
6073 else
6074#endif
6075 if (vim_isfilec(ptr[-1])
6076 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6077 --ptr;
6078 else
6079 break;
6080 }
6081
6082 /*
6083 * Search forward for the last char of the file name.
6084 * Also allow "://" when ':' is not in 'isfname'.
6085 */
6086 len = 0;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006087 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +01006088 || ((options & FNAME_HYP) && path_is_url(ptr + len))
6089 || (is_url && vim_strchr((char_u *)"?&=", ptr[len]) != NULL))
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006090 {
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +01006091 /* After type:// we also include ?, & and = as valid characters, so that
6092 * http://google.com?q=this&that=ok works. */
6093 if ((ptr[len] >= 'A' && ptr[len] <= 'Z') || (ptr[len] >= 'a' && ptr[len] <= 'z'))
6094 {
6095 if (in_type && path_is_url(ptr + len + 1))
6096 is_url = TRUE;
6097 }
6098 else
6099 in_type = FALSE;
6100
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006101 if (ptr[len] == '\\')
6102 /* Skip over the "\" in "\ ". */
6103 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104#ifdef FEAT_MBYTE
6105 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006106 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 else
6108#endif
6109 ++len;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111
6112 /*
6113 * If there is trailing punctuation, remove it.
6114 * But don't remove "..", could be a directory name.
6115 */
6116 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6117 && ptr[len - 2] != '.')
6118 --len;
6119
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006120 if (file_lnum != NULL)
6121 {
6122 char_u *p;
6123
6124 /* Get the number after the file name and a separator character */
6125 p = ptr + len;
6126 p = skipwhite(p);
6127 if (*p != NUL)
6128 {
6129 if (!isdigit(*p))
6130 ++p; /* skip the separator */
6131 p = skipwhite(p);
6132 if (isdigit(*p))
6133 *file_lnum = (int)getdigits(&p);
6134 }
6135 }
6136
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6138}
6139
6140# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006141static char_u *eval_includeexpr(char_u *ptr, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142
6143 static char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006144eval_includeexpr(char_u *ptr, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145{
6146 char_u *res;
6147
6148 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006149 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006150 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 set_vim_var_string(VV_FNAME, NULL, 0);
6152 return res;
6153}
6154#endif
6155
6156/*
6157 * Return the name of the file ptr[len] in 'path'.
6158 * Otherwise like file_name_at_cursor().
6159 */
6160 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006161find_file_name_in_path(
6162 char_u *ptr,
6163 int len,
6164 int options,
6165 long count,
6166 char_u *rel_fname) /* file we are searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167{
6168 char_u *file_name;
6169 int c;
6170# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6171 char_u *tofree = NULL;
6172
6173 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6174 {
6175 tofree = eval_includeexpr(ptr, len);
6176 if (tofree != NULL)
6177 {
6178 ptr = tofree;
6179 len = (int)STRLEN(ptr);
6180 }
6181 }
6182# endif
6183
6184 if (options & FNAME_EXP)
6185 {
6186 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6187 TRUE, rel_fname);
6188
6189# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6190 /*
6191 * If the file could not be found in a normal way, try applying
6192 * 'includeexpr' (unless done already).
6193 */
6194 if (file_name == NULL
6195 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6196 {
6197 tofree = eval_includeexpr(ptr, len);
6198 if (tofree != NULL)
6199 {
6200 ptr = tofree;
6201 len = (int)STRLEN(ptr);
6202 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6203 TRUE, rel_fname);
6204 }
6205 }
6206# endif
6207 if (file_name == NULL && (options & FNAME_MESS))
6208 {
6209 c = ptr[len];
6210 ptr[len] = NUL;
6211 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6212 ptr[len] = c;
6213 }
6214
6215 /* Repeat finding the file "count" times. This matters when it
6216 * appears several times in the path. */
6217 while (file_name != NULL && --count > 0)
6218 {
6219 vim_free(file_name);
6220 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6221 }
6222 }
6223 else
6224 file_name = vim_strnsave(ptr, len);
6225
6226# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6227 vim_free(tofree);
6228# endif
6229
6230 return file_name;
6231}
6232#endif /* FEAT_SEARCHPATH */
6233
6234/*
6235 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6236 * Also check for ":\\", which MS Internet Explorer accepts, return
6237 * URL_BACKSLASH.
6238 */
6239 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006240path_is_url(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241{
6242 if (STRNCMP(p, "://", (size_t)3) == 0)
6243 return URL_SLASH;
6244 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6245 return URL_BACKSLASH;
6246 return 0;
6247}
6248
6249/*
6250 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6251 * Return URL_BACKSLASH for "name:\\".
6252 * Return zero otherwise.
6253 */
6254 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006255path_with_url(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
6257 char_u *p;
6258
6259 for (p = fname; isalpha(*p); ++p)
6260 ;
6261 return path_is_url(p);
6262}
6263
6264/*
6265 * Return TRUE if "name" is a full (absolute) path name or URL.
6266 */
6267 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006268vim_isAbsName(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269{
6270 return (path_with_url(name) != 0 || mch_isFullName(name));
6271}
6272
6273/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006274 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 *
6276 * return FAIL for failure, OK otherwise
6277 */
6278 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006279vim_FullName(
6280 char_u *fname,
6281 char_u *buf,
6282 int len,
6283 int force) /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284{
6285 int retval = OK;
6286 int url;
6287
6288 *buf = NUL;
6289 if (fname == NULL)
6290 return FAIL;
6291
6292 url = path_with_url(fname);
6293 if (!url)
6294 retval = mch_FullName(fname, buf, len, force);
6295 if (url || retval == FAIL)
6296 {
6297 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006298 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 }
Bram Moolenaard0573012017-10-28 21:11:06 +02006300#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 slash_adjust(buf);
6302#endif
6303 return retval;
6304}
6305
6306/*
6307 * Return the minimal number of rows that is needed on the screen to display
6308 * the current number of windows.
6309 */
6310 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006311min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312{
6313 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006314 tabpage_T *tp;
6315 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316
6317 if (firstwin == NULL) /* not initialized yet */
6318 return MIN_LINES;
6319
Bram Moolenaarf740b292006-02-16 22:11:02 +00006320 total = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02006321 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006322 {
6323 n = frame_minheight(tp->tp_topframe, NULL);
6324 if (total < n)
6325 total = n;
6326 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006327 total += tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006328 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 return total;
6330}
6331
6332/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006333 * Return TRUE if there is only one window (in the current tab page), not
6334 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006335 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 */
6337 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006338only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 int count = 0;
6341 win_T *wp;
6342
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006343 /* If there is another tab page there always is another window. */
6344 if (first_tabpage->tp_next != NULL)
6345 return FALSE;
6346
Bram Moolenaar29323592016-07-24 22:04:11 +02006347 FOR_ALL_WINDOWS(wp)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006348 if (wp->w_buffer != NULL
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006349 && (!((bt_help(wp->w_buffer) && !bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350# ifdef FEAT_QUICKFIX
6351 || wp->w_p_pvw
6352# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006353 ) || wp == curwin) && wp != aucmd_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 ++count;
6355 return (count <= 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356}
6357
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358/*
6359 * Correct the cursor line number in other windows. Used after changing the
6360 * current buffer, and before applying autocommands.
6361 * When "do_curwin" is TRUE, also check current window.
6362 */
6363 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006364check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365{
6366 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006367 tabpage_T *tp;
6368
6369 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 {
6372 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6373 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6374 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6375 wp->w_topline = curbuf->b_ml.ml_line_count;
6376 }
6377}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378
6379/*
6380 * A snapshot of the window sizes, to restore them after closing the help
6381 * window.
6382 * Only these fields are used:
6383 * fr_layout
6384 * fr_width
6385 * fr_height
6386 * fr_next
6387 * fr_child
6388 * fr_win (only valid for the old curwin, NULL otherwise)
6389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390
6391/*
6392 * Create a snapshot of the current frame sizes.
6393 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006394 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006395make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006397 clear_snapshot(curtab, idx);
6398 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399}
6400
6401 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006402make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403{
6404 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6405 if (*frp == NULL)
6406 return;
6407 (*frp)->fr_layout = fr->fr_layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 (*frp)->fr_width = fr->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 (*frp)->fr_height = fr->fr_height;
6410 if (fr->fr_next != NULL)
6411 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6412 if (fr->fr_child != NULL)
6413 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6414 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6415 (*frp)->fr_win = curwin;
6416}
6417
6418/*
6419 * Remove any existing snapshot.
6420 */
6421 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006422clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006424 clear_snapshot_rec(tp->tp_snapshot[idx]);
6425 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426}
6427
6428 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006429clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430{
6431 if (fr != NULL)
6432 {
6433 clear_snapshot_rec(fr->fr_next);
6434 clear_snapshot_rec(fr->fr_child);
6435 vim_free(fr);
6436 }
6437}
6438
6439/*
6440 * Restore a previously created snapshot, if there is any.
6441 * This is only done if the screen size didn't change and the window layout is
6442 * still the same.
6443 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006444 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006445restore_snapshot(
6446 int idx,
6447 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
6449 win_T *wp;
6450
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006451 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006452 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006453 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6454 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006456 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 win_comp_pos();
6458 if (wp != NULL && close_curwin)
6459 win_goto(wp);
6460 redraw_all_later(CLEAR);
6461 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006462 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463}
6464
6465/*
6466 * Check if frames "sn" and "fr" have the same layout, same following frames
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006467 * and same children. And the window pointer is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 */
6469 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006470check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
6472 if (sn->fr_layout != fr->fr_layout
6473 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6474 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6475 || (sn->fr_next != NULL
6476 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6477 || (sn->fr_child != NULL
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006478 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01006479 || (sn->fr_win != NULL && !win_valid(sn->fr_win)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 return FAIL;
6481 return OK;
6482}
6483
6484/*
6485 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6486 * following frames and children.
6487 * Returns a pointer to the old current window, or NULL.
6488 */
6489 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006490restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491{
6492 win_T *wp = NULL;
6493 win_T *wp2;
6494
6495 fr->fr_height = sn->fr_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 fr->fr_width = sn->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 if (fr->fr_layout == FR_LEAF)
6498 {
6499 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006500 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 wp = sn->fr_win;
6502 }
6503 if (sn->fr_next != NULL)
6504 {
6505 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6506 if (wp2 != NULL)
6507 wp = wp2;
6508 }
6509 if (sn->fr_child != NULL)
6510 {
6511 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6512 if (wp2 != NULL)
6513 wp = wp2;
6514 }
6515 return wp;
6516}
6517
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006518#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6519 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006520/*
6521 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006522 * restore_win() MUST be called to undo, also when FAIL is returned.
6523 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006524 * When "no_display" is TRUE the display won't be affected, no redraw is
6525 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006526 * Returns FAIL if switching to "win" failed.
6527 */
6528 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006529switch_win(
Bram Moolenaar816968d2017-09-29 21:29:18 +02006530 win_T **save_curwin,
6531 tabpage_T **save_curtab,
6532 win_T *win,
6533 tabpage_T *tp,
6534 int no_display)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006535{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006536 block_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006537 *save_curwin = curwin;
6538 if (tp != NULL)
6539 {
6540 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006541 if (no_display)
6542 {
6543 curtab->tp_firstwin = firstwin;
6544 curtab->tp_lastwin = lastwin;
6545 curtab = tp;
6546 firstwin = curtab->tp_firstwin;
6547 lastwin = curtab->tp_lastwin;
6548 }
6549 else
6550 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006551 }
6552 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006553 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006554 curwin = win;
6555 curbuf = curwin->w_buffer;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006556 return OK;
6557}
6558
6559/*
6560 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006561 * When "no_display" is TRUE the display won't be affected, no redraw is
6562 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006563 */
6564 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006565restore_win(
6566 win_T *save_curwin UNUSED,
6567 tabpage_T *save_curtab UNUSED,
6568 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006569{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006570 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006571 {
6572 if (no_display)
6573 {
6574 curtab->tp_firstwin = firstwin;
6575 curtab->tp_lastwin = lastwin;
6576 curtab = save_curtab;
6577 firstwin = curtab->tp_firstwin;
6578 lastwin = curtab->tp_lastwin;
6579 }
6580 else
6581 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6582 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006583 if (win_valid(save_curwin))
6584 {
6585 curwin = save_curwin;
6586 curbuf = curwin->w_buffer;
6587 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006588 unblock_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006589}
6590
6591/*
6592 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6593 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6594 */
6595 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006596switch_buffer(bufref_T *save_curbuf, buf_T *buf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006597{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006598 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006599 set_bufref(save_curbuf, curbuf);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006600 --curbuf->b_nwindows;
6601 curbuf = buf;
6602 curwin->w_buffer = buf;
6603 ++curbuf->b_nwindows;
6604}
6605
6606/*
6607 * Restore the current buffer after using switch_buffer().
6608 */
6609 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006610restore_buffer(bufref_T *save_curbuf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006611{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006612 unblock_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006613 /* Check for valid buffer, just in case. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006614 if (bufref_valid(save_curbuf))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006615 {
6616 --curbuf->b_nwindows;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006617 curwin->w_buffer = save_curbuf->br_buf;
6618 curbuf = save_curbuf->br_buf;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006619 ++curbuf->b_nwindows;
6620 }
6621}
6622#endif
6623
Bram Moolenaar4033c552017-09-16 20:54:51 +02006624#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625/*
6626 * Return TRUE if there is any vertically split window.
6627 */
6628 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006629win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630{
6631 frame_T *fr;
6632
6633 if (topframe->fr_layout == FR_ROW)
6634 return TRUE;
6635
6636 if (topframe->fr_layout == FR_COL)
6637 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6638 if (fr->fr_layout == FR_ROW)
6639 return TRUE;
6640
6641 return FALSE;
6642}
6643#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006644
6645#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6646/*
6647 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006648 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006649 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6650 * If no particular ID is desired, -1 must be specified for 'id'.
6651 * Return ID of added match, -1 on failure.
6652 */
6653 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006654match_add(
6655 win_T *wp,
6656 char_u *grp,
6657 char_u *pat,
6658 int prio,
6659 int id,
6660 list_T *pos_list,
6661 char_u *conceal_char UNUSED) /* pointer to conceal replacement char */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006662{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006663 matchitem_T *cur;
6664 matchitem_T *prev;
6665 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006666 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006667 regprog_T *regprog = NULL;
6668 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006669
Bram Moolenaarb3414592014-06-17 17:48:32 +02006670 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006671 return -1;
6672 if (id < -1 || id == 0)
6673 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02006674 EMSGN(_("E799: Invalid ID: %ld (must be greater than or equal to 1)"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006675 return -1;
6676 }
6677 if (id != -1)
6678 {
6679 cur = wp->w_match_head;
6680 while (cur != NULL)
6681 {
6682 if (cur->id == id)
6683 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02006684 EMSGN(_("E801: ID already taken: %ld"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006685 return -1;
6686 }
6687 cur = cur->next;
6688 }
6689 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006690 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006691 {
6692 EMSG2(_(e_nogroup), grp);
6693 return -1;
6694 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006695 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006696 {
6697 EMSG2(_(e_invarg2), pat);
6698 return -1;
6699 }
6700
6701 /* Find available match ID. */
6702 while (id == -1)
6703 {
6704 cur = wp->w_match_head;
6705 while (cur != NULL && cur->id != wp->w_next_match_id)
6706 cur = cur->next;
6707 if (cur == NULL)
6708 id = wp->w_next_match_id;
6709 wp->w_next_match_id++;
6710 }
6711
6712 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006713 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006714 m->id = id;
6715 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006716 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006717 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006718 m->match.regprog = regprog;
6719 m->match.rmm_ic = FALSE;
6720 m->match.rmm_maxcol = 0;
Bram Moolenaar42356152016-03-31 22:27:40 +02006721# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +02006722 m->conceal_char = 0;
6723 if (conceal_char != NULL)
6724 m->conceal_char = (*mb_ptr2char)(conceal_char);
Bram Moolenaar42356152016-03-31 22:27:40 +02006725# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006726
Bram Moolenaarb3414592014-06-17 17:48:32 +02006727 /* Set up position matches */
6728 if (pos_list != NULL)
6729 {
6730 linenr_T toplnum = 0;
6731 linenr_T botlnum = 0;
6732 listitem_T *li;
6733 int i;
6734
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006735 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006736 i++, li = li->li_next)
6737 {
6738 linenr_T lnum = 0;
6739 colnr_T col = 0;
6740 int len = 1;
6741 list_T *subl;
6742 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006743 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006744
Bram Moolenaarb3414592014-06-17 17:48:32 +02006745 if (li->li_tv.v_type == VAR_LIST)
6746 {
6747 subl = li->li_tv.vval.v_list;
6748 if (subl == NULL)
6749 goto fail;
6750 subli = subl->lv_first;
6751 if (subli == NULL)
6752 goto fail;
6753 lnum = get_tv_number_chk(&subli->li_tv, &error);
6754 if (error == TRUE)
6755 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006756 if (lnum == 0)
6757 {
6758 --i;
6759 continue;
6760 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006761 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006762 subli = subli->li_next;
6763 if (subli != NULL)
6764 {
6765 col = get_tv_number_chk(&subli->li_tv, &error);
6766 if (error == TRUE)
6767 goto fail;
6768 subli = subli->li_next;
6769 if (subli != NULL)
6770 {
6771 len = get_tv_number_chk(&subli->li_tv, &error);
6772 if (error == TRUE)
6773 goto fail;
6774 }
6775 }
6776 m->pos.pos[i].col = col;
6777 m->pos.pos[i].len = len;
6778 }
6779 else if (li->li_tv.v_type == VAR_NUMBER)
6780 {
6781 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006782 {
6783 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006784 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006785 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006786 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6787 m->pos.pos[i].col = 0;
6788 m->pos.pos[i].len = 0;
6789 }
6790 else
6791 {
6792 EMSG(_("List or number required"));
6793 goto fail;
6794 }
6795 if (toplnum == 0 || lnum < toplnum)
6796 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006797 if (botlnum == 0 || lnum >= botlnum)
6798 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006799 }
6800
6801 /* Calculate top and bottom lines for redrawing area */
6802 if (toplnum != 0)
6803 {
6804 if (wp->w_buffer->b_mod_set)
6805 {
6806 if (wp->w_buffer->b_mod_top > toplnum)
6807 wp->w_buffer->b_mod_top = toplnum;
6808 if (wp->w_buffer->b_mod_bot < botlnum)
6809 wp->w_buffer->b_mod_bot = botlnum;
6810 }
6811 else
6812 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006813 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006814 wp->w_buffer->b_mod_top = toplnum;
6815 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006816 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006817 }
6818 m->pos.toplnum = toplnum;
6819 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006820 rtype = VALID;
6821 }
6822 }
6823
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006824 /* Insert new match. The match list is in ascending order with regard to
6825 * the match priorities. */
6826 cur = wp->w_match_head;
6827 prev = cur;
6828 while (cur != NULL && prio >= cur->priority)
6829 {
6830 prev = cur;
6831 cur = cur->next;
6832 }
6833 if (cur == prev)
6834 wp->w_match_head = m;
6835 else
6836 prev->next = m;
6837 m->next = cur;
6838
Bram Moolenaarb3414592014-06-17 17:48:32 +02006839 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006840 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006841
6842fail:
6843 vim_free(m);
6844 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006845}
6846
6847/*
6848 * Delete match with ID 'id' in the match list of window 'wp'.
6849 * Print error messages if 'perr' is TRUE.
6850 */
6851 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006852match_delete(win_T *wp, int id, int perr)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006853{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006854 matchitem_T *cur = wp->w_match_head;
6855 matchitem_T *prev = cur;
6856 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006857
6858 if (id < 1)
6859 {
6860 if (perr == TRUE)
Bram Moolenaar5b302912016-08-24 22:11:55 +02006861 EMSGN(_("E802: Invalid ID: %ld (must be greater than or equal to 1)"),
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006862 id);
6863 return -1;
6864 }
6865 while (cur != NULL && cur->id != id)
6866 {
6867 prev = cur;
6868 cur = cur->next;
6869 }
6870 if (cur == NULL)
6871 {
6872 if (perr == TRUE)
Bram Moolenaar5b302912016-08-24 22:11:55 +02006873 EMSGN(_("E803: ID not found: %ld"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006874 return -1;
6875 }
6876 if (cur == prev)
6877 wp->w_match_head = cur->next;
6878 else
6879 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006880 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006881 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006882 if (cur->pos.toplnum != 0)
6883 {
6884 if (wp->w_buffer->b_mod_set)
6885 {
6886 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
6887 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6888 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
6889 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
6890 }
6891 else
6892 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006893 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006894 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6895 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006896 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006897 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006898 rtype = VALID;
6899 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006900 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006901 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006902 return 0;
6903}
6904
6905/*
6906 * Delete all matches in the match list of window 'wp'.
6907 */
6908 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006909clear_matches(win_T *wp)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006910{
6911 matchitem_T *m;
6912
6913 while (wp->w_match_head != NULL)
6914 {
6915 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006916 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006917 vim_free(wp->w_match_head->pattern);
6918 vim_free(wp->w_match_head);
6919 wp->w_match_head = m;
6920 }
6921 redraw_later(SOME_VALID);
6922}
6923
6924/*
6925 * Get match from ID 'id' in window 'wp'.
6926 * Return NULL if match not found.
6927 */
6928 matchitem_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006929get_match(win_T *wp, int id)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006930{
6931 matchitem_T *cur = wp->w_match_head;
6932
6933 while (cur != NULL && cur->id != id)
6934 cur = cur->next;
6935 return cur;
6936}
6937#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02006938
6939#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6940 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006941get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02006942{
6943 int i = 1;
6944 win_T *w;
6945
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006946 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02006947 ++i;
6948
6949 if (w == NULL)
6950 return 0;
6951 else
6952 return i;
6953}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006954
6955 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006956get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006957{
6958 int i = 1;
6959 tabpage_T *t;
6960
6961 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
6962 ++i;
6963
6964 if (t == NULL)
6965 return 0;
6966 else
6967 return i;
6968}
Bram Moolenaar6d216452013-05-12 19:00:41 +02006969#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006970
6971/*
6972 * Return TRUE if "topfrp" and its children are at the right height.
6973 */
6974 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006975frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006976{
6977 frame_T *frp;
6978
6979 if (topfrp->fr_height != height)
6980 return FALSE;
6981
6982 if (topfrp->fr_layout == FR_ROW)
6983 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
6984 if (frp->fr_height != height)
6985 return FALSE;
6986
6987 return TRUE;
6988}
6989
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006990/*
6991 * Return TRUE if "topfrp" and its children are at the right width.
6992 */
6993 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006994frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006995{
6996 frame_T *frp;
6997
6998 if (topfrp->fr_width != width)
6999 return FALSE;
7000
7001 if (topfrp->fr_layout == FR_COL)
7002 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7003 if (frp->fr_width != width)
7004 return FALSE;
7005
7006 return TRUE;
7007}
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007008
Bram Moolenaar86edef62016-03-13 18:07:30 +01007009#if defined(FEAT_EVAL) || defined(PROTO)
7010 int
7011win_getid(typval_T *argvars)
7012{
7013 int winnr;
7014 win_T *wp;
7015
7016 if (argvars[0].v_type == VAR_UNKNOWN)
7017 return curwin->w_id;
7018 winnr = get_tv_number(&argvars[0]);
7019 if (winnr > 0)
7020 {
7021 if (argvars[1].v_type == VAR_UNKNOWN)
7022 wp = firstwin;
7023 else
7024 {
7025 tabpage_T *tp;
7026 int tabnr = get_tv_number(&argvars[1]);
7027
Bram Moolenaar29323592016-07-24 22:04:11 +02007028 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007029 if (--tabnr == 0)
7030 break;
7031 if (tp == NULL)
7032 return -1;
Bram Moolenaar8e639052016-11-13 14:31:40 +01007033 if (tp == curtab)
7034 wp = firstwin;
7035 else
7036 wp = tp->tp_firstwin;
Bram Moolenaar86edef62016-03-13 18:07:30 +01007037 }
7038 for ( ; wp != NULL; wp = wp->w_next)
7039 if (--winnr == 0)
7040 return wp->w_id;
7041 }
7042 return 0;
7043}
7044
7045 int
7046win_gotoid(typval_T *argvars)
7047{
7048 win_T *wp;
7049 tabpage_T *tp;
7050 int id = get_tv_number(&argvars[0]);
7051
Bram Moolenaar29323592016-07-24 22:04:11 +02007052 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007053 if (wp->w_id == id)
7054 {
7055 goto_tabpage_win(tp, wp);
7056 return 1;
7057 }
7058 return 0;
7059}
7060
7061 void
7062win_id2tabwin(typval_T *argvars, list_T *list)
7063{
7064 win_T *wp;
7065 tabpage_T *tp;
7066 int winnr = 1;
7067 int tabnr = 1;
7068 int id = get_tv_number(&argvars[0]);
7069
Bram Moolenaar29323592016-07-24 22:04:11 +02007070 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007071 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007072 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007073 {
7074 if (wp->w_id == id)
7075 {
7076 list_append_number(list, tabnr);
7077 list_append_number(list, winnr);
7078 return;
7079 }
7080 ++winnr;
7081 }
7082 ++tabnr;
7083 winnr = 1;
7084 }
7085 list_append_number(list, 0);
7086 list_append_number(list, 0);
7087}
7088
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007089 win_T *
7090win_id2wp(typval_T *argvars)
7091{
7092 win_T *wp;
7093 tabpage_T *tp;
7094 int id = get_tv_number(&argvars[0]);
7095
7096 FOR_ALL_TAB_WINDOWS(tp, wp)
7097 if (wp->w_id == id)
7098 return wp;
7099
7100 return NULL;
7101}
7102
Bram Moolenaar86edef62016-03-13 18:07:30 +01007103 int
7104win_id2win(typval_T *argvars)
7105{
7106 win_T *wp;
7107 int nr = 1;
7108 int id = get_tv_number(&argvars[0]);
7109
Bram Moolenaar29323592016-07-24 22:04:11 +02007110 FOR_ALL_WINDOWS(wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007111 {
7112 if (wp->w_id == id)
7113 return nr;
7114 ++nr;
7115 }
7116 return 0;
7117}
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007118
7119 void
7120win_findbuf(typval_T *argvars, list_T *list)
7121{
7122 win_T *wp;
7123 tabpage_T *tp;
7124 int bufnr = get_tv_number(&argvars[0]);
7125
Bram Moolenaar29323592016-07-24 22:04:11 +02007126 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007127 if (wp->w_buffer->b_fnum == bufnr)
7128 list_append_number(list, wp->w_id);
7129}
7130
Bram Moolenaar86edef62016-03-13 18:07:30 +01007131#endif