blob: de85739a0e28cd7e854be27dde849188056817e3 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 Moolenaar071d4272004-06-13 20:20:40 +000012static int path_is_url __ARGS((char_u *p));
13#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar884ae642009-02-22 01:37:59 +000014static void win_init __ARGS((win_T *newp, win_T *oldp, int flags));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000015static void win_init_some __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
17static void frame_setheight __ARGS((frame_T *curfrp, int height));
18#ifdef FEAT_VERTSPLIT
19static void frame_setwidth __ARGS((frame_T *curfrp, int width));
20#endif
21static void win_exchange __ARGS((long));
22static void win_rotate __ARGS((int, int));
23static void win_totop __ARGS((int size, int flags));
24static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000025static int last_window __ARGS((void));
Bram Moolenaarbef1c362012-05-25 12:39:00 +020026static int close_last_window_tabpage __ARGS((win_T *win, int free_buf, tabpage_T *prev_curtab));
Bram Moolenaarf740b292006-02-16 22:11:02 +000027static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
Bram Moolenaarf740b292006-02-16 22:11:02 +000028static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000029static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000030static win_T *frame2win __ARGS((frame_T *frp));
31static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
32static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
33static int frame_fixed_height __ARGS((frame_T *frp));
34#ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000035static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000036static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000037static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000038static void frame_add_vsep __ARGS((frame_T *frp));
39static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
40static void frame_fix_width __ARGS((win_T *wp));
41#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000042#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000043static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000044static void new_frame __ARGS((win_T *wp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000045#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000046static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar49e649f2013-05-06 04:50:35 +020047static int leave_tabpage __ARGS((buf_T *new_curbuf, int trigger_leave_autocmds));
48static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds));
Bram Moolenaar071d4272004-06-13 20:20:40 +000049static void frame_fix_height __ARGS((win_T *wp));
50static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
Bram Moolenaar49e649f2013-05-06 04:50:35 +020051static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin, int trigger_enter_autocmds, int trigger_leave_autocmds));
Bram Moolenaarf740b292006-02-16 22:11:02 +000052static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000053static void frame_append __ARGS((frame_T *after, frame_T *frp));
54static void frame_insert __ARGS((frame_T *before, frame_T *frp));
55static void frame_remove __ARGS((frame_T *frp));
Bram Moolenaarf0327f62013-06-28 20:16:55 +020056# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +000057static void win_goto_ver __ARGS((int up, long count));
58static void win_goto_hor __ARGS((int left, long count));
Bram Moolenaarf0327f62013-06-28 20:16:55 +020059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000060static void frame_add_height __ARGS((frame_T *frp, int n));
61static void last_status_rec __ARGS((frame_T *fr, int statusline));
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000064static void clear_snapshot __ARGS((tabpage_T *tp, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static void clear_snapshot_rec __ARGS((frame_T *fr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
67static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
68
Bram Moolenaarb893ac22013-06-26 14:04:47 +020069static int frame_check_height __ARGS((frame_T *topfrp, int height));
70#ifdef FEAT_VERTSPLIT
71static int frame_check_width __ARGS((frame_T *topfrp, int width));
72#endif
73
Bram Moolenaar071d4272004-06-13 20:20:40 +000074#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000075
Bram Moolenaar746ebd32009-06-16 14:01:43 +000076static win_T *win_alloc __ARGS((win_T *after, int hidden));
Bram Moolenaar0215e8e2010-12-17 17:35:10 +010077static void set_fraction __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
79#define URL_SLASH 1 /* path_is_url() has found "://" */
80#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
81
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000082#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar05159a02005-02-26 23:04:13 +000084#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000085# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000086#else
87# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000088#endif
89
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000091
92static char *m_onlyone = N_("Already only one window");
93
Bram Moolenaar071d4272004-06-13 20:20:40 +000094/*
95 * all CTRL-W window commands are handled here, called from normal_cmd().
96 */
97 void
98do_window(nchar, Prenum, xchar)
99 int nchar;
100 long Prenum;
101 int xchar; /* extra char from ":wincmd gx" or NUL */
102{
103 long Prenum1;
104 win_T *wp;
105#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
106 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000107 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109#ifdef FEAT_FIND_ID
110 int type = FIND_DEFINE;
111 int len;
112#endif
113 char_u cbuf[40];
114
115 if (Prenum == 0)
116 Prenum1 = 1;
117 else
118 Prenum1 = Prenum;
119
120#ifdef FEAT_CMDWIN
121# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
122#else
123# define CHECK_CMDWIN
124#endif
125
126 switch (nchar)
127 {
128/* split current window in two parts, horizontally */
129 case 'S':
130 case Ctrl_S:
131 case 's':
132 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000134#ifdef FEAT_QUICKFIX
135 /* When splitting the quickfix window open a new buffer in it,
136 * don't replicate the quickfix buffer. */
137 if (bt_quickfix(curbuf))
138 goto newwindow;
139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_GUI
141 need_mouse_correct = TRUE;
142#endif
143 win_split((int)Prenum, 0);
144 break;
145
146#ifdef FEAT_VERTSPLIT
147/* split current window in two parts, vertically */
148 case Ctrl_V:
149 case 'v':
150 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000152# ifdef FEAT_QUICKFIX
153 /* When splitting the quickfix window open a new buffer in it,
154 * don't replicate the quickfix buffer. */
155 if (bt_quickfix(curbuf))
156 goto newwindow;
157# endif
158# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 win_split((int)Prenum, WSP_VERT);
162 break;
163#endif
164
165/* split current window and edit alternate file */
166 case Ctrl_HAT:
167 case '^':
168 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 STRCPY(cbuf, "split #");
171 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000172 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
173 "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 do_cmdline_cmd(cbuf);
175 break;
176
177/* open new window */
178 case Ctrl_N:
179 case 'n':
180 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000182#ifdef FEAT_QUICKFIX
183newwindow:
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000186 /* window height */
187 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 else
189 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000190#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
191 if (nchar == 'v' || nchar == Ctrl_V)
192 STRCAT(cbuf, "v");
193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 STRCAT(cbuf, "new");
195 do_cmdline_cmd(cbuf);
196 break;
197
198/* quit current window */
199 case Ctrl_Q:
200 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 do_cmdline_cmd((char_u *)"quit");
203 break;
204
205/* close current window */
206 case Ctrl_C:
207 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 do_cmdline_cmd((char_u *)"close");
210 break;
211
212#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
213/* close preview window */
214 case Ctrl_Z:
215 case 'z':
216 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 do_cmdline_cmd((char_u *)"pclose");
219 break;
220
221/* cursor to preview window */
222 case 'P':
223 for (wp = firstwin; wp != NULL; wp = wp->w_next)
224 if (wp->w_p_pvw)
225 break;
226 if (wp == NULL)
227 EMSG(_("E441: There is no preview window"));
228 else
229 win_goto(wp);
230 break;
231#endif
232
233/* close all but current window */
234 case Ctrl_O:
235 case 'o':
236 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 do_cmdline_cmd((char_u *)"only");
239 break;
240
241/* cursor to next window with wrap around */
242 case Ctrl_W:
243 case 'w':
244/* cursor to previous window with wrap around */
245 case 'W':
246 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000247 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 beep_flush();
249 else
250 {
251 if (Prenum) /* go to specified window */
252 {
253 for (wp = firstwin; --Prenum > 0; )
254 {
255 if (wp->w_next == NULL)
256 break;
257 else
258 wp = wp->w_next;
259 }
260 }
261 else
262 {
263 if (nchar == 'W') /* go to previous window */
264 {
265 wp = curwin->w_prev;
266 if (wp == NULL)
267 wp = lastwin; /* wrap around */
268 }
269 else /* go to next window */
270 {
271 wp = curwin->w_next;
272 if (wp == NULL)
273 wp = firstwin; /* wrap around */
274 }
275 }
276 win_goto(wp);
277 }
278 break;
279
280/* cursor to window below */
281 case 'j':
282 case K_DOWN:
283 case Ctrl_J:
284 CHECK_CMDWIN
285#ifdef FEAT_VERTSPLIT
286 win_goto_ver(FALSE, Prenum1);
287#else
288 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
289 wp = wp->w_next)
290 ;
291 win_goto(wp);
292#endif
293 break;
294
295/* cursor to window above */
296 case 'k':
297 case K_UP:
298 case Ctrl_K:
299 CHECK_CMDWIN
300#ifdef FEAT_VERTSPLIT
301 win_goto_ver(TRUE, Prenum1);
302#else
303 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
304 wp = wp->w_prev)
305 ;
306 win_goto(wp);
307#endif
308 break;
309
310#ifdef FEAT_VERTSPLIT
311/* cursor to left window */
312 case 'h':
313 case K_LEFT:
314 case Ctrl_H:
315 case K_BS:
316 CHECK_CMDWIN
317 win_goto_hor(TRUE, Prenum1);
318 break;
319
320/* cursor to right window */
321 case 'l':
322 case K_RIGHT:
323 case Ctrl_L:
324 CHECK_CMDWIN
325 win_goto_hor(FALSE, Prenum1);
326 break;
327#endif
328
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000329/* move window to new tab page */
330 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000331 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000332 MSG(_(m_onlyone));
333 else
334 {
335 tabpage_T *oldtab = curtab;
336 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000337
338 /* First create a new tab with the window, then go back to
339 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000340 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000341 if (win_new_tabpage((int)Prenum) == OK
342 && valid_tabpage(oldtab))
343 {
344 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200345 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000346 if (curwin == wp)
347 win_close(curwin, FALSE);
348 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200349 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000350 }
351 }
352 break;
353
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354/* cursor to top-left window */
355 case 't':
356 case Ctrl_T:
357 win_goto(firstwin);
358 break;
359
360/* cursor to bottom-right window */
361 case 'b':
362 case Ctrl_B:
363 win_goto(lastwin);
364 break;
365
366/* cursor to last accessed (previous) window */
367 case 'p':
368 case Ctrl_P:
369 if (prevwin == NULL)
370 beep_flush();
371 else
372 win_goto(prevwin);
373 break;
374
375/* exchange current and next window */
376 case 'x':
377 case Ctrl_X:
378 CHECK_CMDWIN
379 win_exchange(Prenum);
380 break;
381
382/* rotate windows downwards */
383 case Ctrl_R:
384 case 'r':
385 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 win_rotate(FALSE, (int)Prenum1); /* downwards */
388 break;
389
390/* rotate windows upwards */
391 case 'R':
392 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 win_rotate(TRUE, (int)Prenum1); /* upwards */
395 break;
396
397/* move window to the very top/bottom/left/right */
398 case 'K':
399 case 'J':
400#ifdef FEAT_VERTSPLIT
401 case 'H':
402 case 'L':
403#endif
404 CHECK_CMDWIN
405 win_totop((int)Prenum,
406 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
407 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
408 break;
409
410/* make all windows the same height */
411 case '=':
412#ifdef FEAT_GUI
413 need_mouse_correct = TRUE;
414#endif
415 win_equal(NULL, FALSE, 'b');
416 break;
417
418/* increase current window height */
419 case '+':
420#ifdef FEAT_GUI
421 need_mouse_correct = TRUE;
422#endif
423 win_setheight(curwin->w_height + (int)Prenum1);
424 break;
425
426/* decrease current window height */
427 case '-':
428#ifdef FEAT_GUI
429 need_mouse_correct = TRUE;
430#endif
431 win_setheight(curwin->w_height - (int)Prenum1);
432 break;
433
434/* set current window height */
435 case Ctrl__:
436 case '_':
437#ifdef FEAT_GUI
438 need_mouse_correct = TRUE;
439#endif
440 win_setheight(Prenum ? (int)Prenum : 9999);
441 break;
442
443#ifdef FEAT_VERTSPLIT
444/* increase current window width */
445 case '>':
446#ifdef FEAT_GUI
447 need_mouse_correct = TRUE;
448#endif
449 win_setwidth(curwin->w_width + (int)Prenum1);
450 break;
451
452/* decrease current window width */
453 case '<':
454#ifdef FEAT_GUI
455 need_mouse_correct = TRUE;
456#endif
457 win_setwidth(curwin->w_width - (int)Prenum1);
458 break;
459
460/* set current window width */
461 case '|':
462#ifdef FEAT_GUI
463 need_mouse_correct = TRUE;
464#endif
465 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
466 break;
467#endif
468
469/* jump to tag and split window if tag exists (in preview window) */
470#if defined(FEAT_QUICKFIX)
471 case '}':
472 CHECK_CMDWIN
473 if (Prenum)
474 g_do_tagpreview = Prenum;
475 else
476 g_do_tagpreview = p_pvh;
477 /*FALLTHROUGH*/
478#endif
479 case ']':
480 case Ctrl_RSB:
481 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 if (Prenum)
484 postponed_split = Prenum;
485 else
486 postponed_split = -1;
487
488 /* Execute the command right here, required when
489 * "wincmd ]" was used in a function. */
490 do_nv_ident(Ctrl_RSB, NUL);
491 break;
492
493#ifdef FEAT_SEARCHPATH
494/* edit file name under cursor in a new window */
495 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000496 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000498wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000500
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000501 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 if (ptr != NULL)
503 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000504# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000506# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 setpcmark();
508 if (win_split(0, 0) == OK)
509 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200510 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000511 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
512 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000513 if (nchar == 'F' && lnum >= 0)
514 {
515 curwin->w_cursor.lnum = lnum;
516 check_cursor_lnum();
517 beginline(BL_SOL | BL_FIX);
518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 }
520 vim_free(ptr);
521 }
522 break;
523#endif
524
525#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000526/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 * new window -- webb
528 */
529 case 'i': /* Go to any match */
530 case Ctrl_I:
531 type = FIND_ANY;
532 /* FALLTHROUGH */
533 case 'd': /* Go to definition, using 'define' */
534 case Ctrl_D:
535 CHECK_CMDWIN
536 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
537 break;
538 find_pattern_in_path(ptr, 0, len, TRUE,
539 Prenum == 0 ? TRUE : FALSE, type,
540 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
541 curwin->w_set_curswant = TRUE;
542 break;
543#endif
544
Bram Moolenaar05159a02005-02-26 23:04:13 +0000545 case K_KENTER:
546 case CAR:
547#if defined(FEAT_QUICKFIX)
548 /*
549 * In a quickfix window a <CR> jumps to the error under the
550 * cursor in a new window.
551 */
552 if (bt_quickfix(curbuf))
553 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000554 sprintf((char *)cbuf, "split +%ld%s",
555 (long)curwin->w_cursor.lnum,
556 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000557 do_cmdline_cmd(cbuf);
558 }
559#endif
560 break;
561
562
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563/* CTRL-W g extended commands */
564 case 'g':
565 case Ctrl_G:
566 CHECK_CMDWIN
567#ifdef USE_ON_FLY_SCROLL
568 dont_scroll = TRUE; /* disallow scrolling here */
569#endif
570 ++no_mapping;
571 ++allow_keys; /* no mapping for xchar, but allow key codes */
572 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000573 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 --no_mapping;
576 --allow_keys;
577#ifdef FEAT_CMDL_INFO
578 (void)add_to_showcmd(xchar);
579#endif
580 switch (xchar)
581 {
582#if defined(FEAT_QUICKFIX)
583 case '}':
584 xchar = Ctrl_RSB;
585 if (Prenum)
586 g_do_tagpreview = Prenum;
587 else
588 g_do_tagpreview = p_pvh;
589 /*FALLTHROUGH*/
590#endif
591 case ']':
592 case Ctrl_RSB:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 if (Prenum)
595 postponed_split = Prenum;
596 else
597 postponed_split = -1;
598
599 /* Execute the command right here, required when
600 * "wincmd g}" was used in a function. */
601 do_nv_ident('g', xchar);
602 break;
603
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000604#ifdef FEAT_SEARCHPATH
605 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000606 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100607 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000608 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000609 goto wingotofile;
610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 default:
612 beep_flush();
613 break;
614 }
615 break;
616
617 default: beep_flush();
618 break;
619 }
620}
621
622/*
623 * split the current window, implements CTRL-W s and :split
624 *
625 * "size" is the height or width for the new window, 0 to use half of current
626 * height or width.
627 *
628 * "flags":
629 * WSP_ROOM: require enough room for new window
630 * WSP_VERT: vertical split.
631 * WSP_TOP: open window at the top-left of the shell (help window).
632 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
633 * WSP_HELP: creating the help window, keep layout snapshot
634 *
635 * return FAIL for failure, OK otherwise
636 */
637 int
638win_split(size, flags)
639 int size;
640 int flags;
641{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000642 /* When the ":tab" modifier was used open a new tab page instead. */
643 if (may_open_tabpage() == OK)
644 return OK;
645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 /* Add flags from ":vertical", ":topleft" and ":botright". */
647 flags |= cmdmod.split;
648 if ((flags & WSP_TOP) && (flags & WSP_BOT))
649 {
650 EMSG(_("E442: Can't split topleft and botright at the same time"));
651 return FAIL;
652 }
653
654 /* When creating the help window make a snapshot of the window layout.
655 * Otherwise clear the snapshot, it's now invalid. */
656 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000657 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000659 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661 return win_split_ins(size, flags, NULL, 0);
662}
663
664/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100665 * When "new_wp" is NULL: split the current window in two.
666 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 * top/left/right/bottom.
668 * return FAIL for failure, OK otherwise
669 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000670 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100671win_split_ins(size, flags, new_wp, dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 int size;
673 int flags;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100674 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 int dir;
676{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100677 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 win_T *oldwin;
679 int new_size = size;
680 int i;
681 int need_status = 0;
682 int do_equal = FALSE;
683 int needed;
684 int available;
685 int oldwin_height = 0;
686 int layout;
687 frame_T *frp, *curfrp;
688 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200689 int minwidth;
690 int minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
692 if (flags & WSP_TOP)
693 oldwin = firstwin;
694 else if (flags & WSP_BOT)
695 oldwin = lastwin;
696 else
697 oldwin = curwin;
698
699 /* add a status line when p_ls == 1 and splitting the first window */
700 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
701 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100702 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 EMSG(_(e_noroom));
705 return FAIL;
706 }
707 need_status = STATUS_HEIGHT;
708 }
709
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000710#ifdef FEAT_GUI
711 /* May be needed for the scrollbars that are going to change. */
712 if (gui.in_use)
713 out_flush();
714#endif
715
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716#ifdef FEAT_VERTSPLIT
717 if (flags & WSP_VERT)
718 {
719 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720
721 /*
722 * Check if we are able to split the current window and compute its
723 * width.
724 */
725 needed = p_wmw + 1;
726 if (flags & WSP_ROOM)
727 needed += p_wiw - p_wmw;
728 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
729 {
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200730 minwidth = frame_minwidth(topframe, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200732 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 }
734 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200735 {
736 minwidth = frame_minwidth(oldwin->w_frame, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 available = oldwin->w_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200738 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100739 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 {
741 EMSG(_(e_noroom));
742 return FAIL;
743 }
744 if (new_size == 0)
745 new_size = oldwin->w_width / 2;
746 if (new_size > oldwin->w_width - p_wmw - 1)
747 new_size = oldwin->w_width - p_wmw - 1;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200748 if (new_size > available - minwidth - 1)
749 new_size = available - minwidth - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 if (new_size < p_wmw)
751 new_size = p_wmw;
752
753 /* if it doesn't fit in the current window, need win_equal() */
754 if (oldwin->w_width - new_size - 1 < p_wmw)
755 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000756
757 /* We don't like to take lines for the new window from a
758 * 'winfixwidth' window. Take them from a window to the left or right
759 * instead, if possible. */
760 if (oldwin->w_p_wfw)
761 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000762
763 /* Only make all windows the same width if one of them (except oldwin)
764 * is wider than one of the split windows. */
765 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
766 && oldwin->w_frame->fr_parent != NULL)
767 {
768 frp = oldwin->w_frame->fr_parent->fr_child;
769 while (frp != NULL)
770 {
771 if (frp->fr_win != oldwin && frp->fr_win != NULL
772 && (frp->fr_win->w_width > new_size
773 || frp->fr_win->w_width > oldwin->w_width
774 - new_size - STATUS_HEIGHT))
775 {
776 do_equal = TRUE;
777 break;
778 }
779 frp = frp->fr_next;
780 }
781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
783 else
784#endif
785 {
786 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
788 /*
789 * Check if we are able to split the current window and compute its
790 * height.
791 */
792 needed = p_wmh + STATUS_HEIGHT + need_status;
793 if (flags & WSP_ROOM)
794 needed += p_wh - p_wmh;
795 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
796 {
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200797 minheight = frame_minheight(topframe, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200799 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 }
801 else
802 {
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200803 minheight = frame_minheight(oldwin->w_frame, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 available = oldwin->w_height;
805 needed += p_wmh;
806 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100807 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {
809 EMSG(_(e_noroom));
810 return FAIL;
811 }
812 oldwin_height = oldwin->w_height;
813 if (need_status)
814 {
815 oldwin->w_status_height = STATUS_HEIGHT;
816 oldwin_height -= STATUS_HEIGHT;
817 }
818 if (new_size == 0)
819 new_size = oldwin_height / 2;
820
821 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
822 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200823 if (new_size > available - minheight - STATUS_HEIGHT)
824 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 if (new_size < p_wmh)
826 new_size = p_wmh;
827
828 /* if it doesn't fit in the current window, need win_equal() */
829 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
830 do_equal = TRUE;
831
832 /* We don't like to take lines for the new window from a
833 * 'winfixheight' window. Take them from a window above or below
834 * instead, if possible. */
835 if (oldwin->w_p_wfh)
836 {
837 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
838 oldwin);
839 oldwin_height = oldwin->w_height;
840 if (need_status)
841 oldwin_height -= STATUS_HEIGHT;
842 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000843
844 /* Only make all windows the same height if one of them (except oldwin)
845 * is higher than one of the split windows. */
846 if (!do_equal && p_ea && size == 0
847#ifdef FEAT_VERTSPLIT
848 && *p_ead != 'h'
849#endif
850 && oldwin->w_frame->fr_parent != NULL)
851 {
852 frp = oldwin->w_frame->fr_parent->fr_child;
853 while (frp != NULL)
854 {
855 if (frp->fr_win != oldwin && frp->fr_win != NULL
856 && (frp->fr_win->w_height > new_size
857 || frp->fr_win->w_height > oldwin_height - new_size
858 - STATUS_HEIGHT))
859 {
860 do_equal = TRUE;
861 break;
862 }
863 frp = frp->fr_next;
864 }
865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 }
867
868 /*
869 * allocate new window structure and link it in the window list
870 */
871 if ((flags & WSP_TOP) == 0
872 && ((flags & WSP_BOT)
873 || (flags & WSP_BELOW)
874 || (!(flags & WSP_ABOVE)
875 && (
876#ifdef FEAT_VERTSPLIT
877 (flags & WSP_VERT) ? p_spr :
878#endif
879 p_sb))))
880 {
881 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100882 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000883 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 else
885 win_append(oldwin, wp);
886 }
887 else
888 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100889 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000890 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 else
892 win_append(oldwin->w_prev, wp);
893 }
894
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100895 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 {
897 if (wp == NULL)
898 return FAIL;
899
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000900 new_frame(wp);
901 if (wp->w_frame == NULL)
902 {
903 win_free(wp, NULL);
904 return FAIL;
905 }
906
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000907 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000908 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 }
910
911 /*
912 * Reorganise the tree of frames to insert the new window.
913 */
914 if (flags & (WSP_TOP | WSP_BOT))
915 {
916#ifdef FEAT_VERTSPLIT
917 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
918 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
919#else
920 if (topframe->fr_layout == FR_COL)
921#endif
922 {
923 curfrp = topframe->fr_child;
924 if (flags & WSP_BOT)
925 while (curfrp->fr_next != NULL)
926 curfrp = curfrp->fr_next;
927 }
928 else
929 curfrp = topframe;
930 before = (flags & WSP_TOP);
931 }
932 else
933 {
934 curfrp = oldwin->w_frame;
935 if (flags & WSP_BELOW)
936 before = FALSE;
937 else if (flags & WSP_ABOVE)
938 before = TRUE;
939 else
940#ifdef FEAT_VERTSPLIT
941 if (flags & WSP_VERT)
942 before = !p_spr;
943 else
944#endif
945 before = !p_sb;
946 }
947 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
948 {
949 /* Need to create a new frame in the tree to make a branch. */
950 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
951 *frp = *curfrp;
952 curfrp->fr_layout = layout;
953 frp->fr_parent = curfrp;
954 frp->fr_next = NULL;
955 frp->fr_prev = NULL;
956 curfrp->fr_child = frp;
957 curfrp->fr_win = NULL;
958 curfrp = frp;
959 if (frp->fr_win != NULL)
960 oldwin->w_frame = frp;
961 else
962 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
963 frp->fr_parent = curfrp;
964 }
965
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100966 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000967 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100969 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 frp->fr_parent = curfrp->fr_parent;
971
972 /* Insert the new frame at the right place in the frame list. */
973 if (before)
974 frame_insert(curfrp, frp);
975 else
976 frame_append(curfrp, frp);
977
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100978 /* Set w_fraction now so that the cursor keeps the same relative
979 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +0100980 if (oldwin->w_height > 0)
981 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100982 wp->w_fraction = oldwin->w_fraction;
983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984#ifdef FEAT_VERTSPLIT
985 if (flags & WSP_VERT)
986 {
987 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 if (need_status)
990 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +0100991 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 oldwin->w_status_height = need_status;
993 }
994 if (flags & (WSP_TOP | WSP_BOT))
995 {
996 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000997 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +0100998 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 wp->w_status_height = (p_ls > 0);
1000 }
1001 else
1002 {
1003 /* height and row of new window is same as current window */
1004 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001005 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 wp->w_status_height = oldwin->w_status_height;
1007 }
1008 frp->fr_height = curfrp->fr_height;
1009
1010 /* "new_size" of the current window goes to the new window, use
1011 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001012 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 if (before)
1014 wp->w_vsep_width = 1;
1015 else
1016 {
1017 wp->w_vsep_width = oldwin->w_vsep_width;
1018 oldwin->w_vsep_width = 1;
1019 }
1020 if (flags & (WSP_TOP | WSP_BOT))
1021 {
1022 if (flags & WSP_BOT)
1023 frame_add_vsep(curfrp);
1024 /* Set width of neighbor frame */
1025 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001026 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1027 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
1029 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001030 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (before) /* new window left of current one */
1032 {
1033 wp->w_wincol = oldwin->w_wincol;
1034 oldwin->w_wincol += new_size + 1;
1035 }
1036 else /* new window right of current one */
1037 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1038 frame_fix_width(oldwin);
1039 frame_fix_width(wp);
1040 }
1041 else
1042#endif
1043 {
1044 /* width and column of new window is same as current window */
1045#ifdef FEAT_VERTSPLIT
1046 if (flags & (WSP_TOP | WSP_BOT))
1047 {
1048 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001049 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 wp->w_vsep_width = 0;
1051 }
1052 else
1053 {
1054 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001055 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 wp->w_vsep_width = oldwin->w_vsep_width;
1057 }
1058 frp->fr_width = curfrp->fr_width;
1059#endif
1060
1061 /* "new_size" of the current window goes to the new window, use
1062 * one row for the status line */
1063 win_new_height(wp, new_size);
1064 if (flags & (WSP_TOP | WSP_BOT))
1065 frame_new_height(curfrp, curfrp->fr_height
1066 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1067 else
1068 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1069 if (before) /* new window above current one */
1070 {
1071 wp->w_winrow = oldwin->w_winrow;
1072 wp->w_status_height = STATUS_HEIGHT;
1073 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1074 }
1075 else /* new window below current one */
1076 {
1077 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1078 wp->w_status_height = oldwin->w_status_height;
1079 oldwin->w_status_height = STATUS_HEIGHT;
1080 }
1081#ifdef FEAT_VERTSPLIT
1082 if (flags & WSP_BOT)
1083 frame_add_statusline(curfrp);
1084#endif
1085 frame_fix_height(wp);
1086 frame_fix_height(oldwin);
1087 }
1088
1089 if (flags & (WSP_TOP | WSP_BOT))
1090 (void)win_comp_pos();
1091
1092 /*
1093 * Both windows need redrawing
1094 */
1095 redraw_win_later(wp, NOT_VALID);
1096 wp->w_redr_status = TRUE;
1097 redraw_win_later(oldwin, NOT_VALID);
1098 oldwin->w_redr_status = TRUE;
1099
1100 if (need_status)
1101 {
1102 msg_row = Rows - 1;
1103 msg_col = sc_col;
1104 msg_clr_eos_force(); /* Old command/ruler may still be there */
1105 comp_col();
1106 msg_row = Rows - 1;
1107 msg_col = 0; /* put position back at start of line */
1108 }
1109
1110 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001111 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 */
1113 if (do_equal || dir != 0)
1114 win_equal(wp, TRUE,
1115#ifdef FEAT_VERTSPLIT
1116 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1117 : dir == 'h' ? 'b' :
1118#endif
1119 'v');
1120
1121 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1122 * size was given. */
1123#ifdef FEAT_VERTSPLIT
1124 if (flags & WSP_VERT)
1125 {
1126 i = p_wiw;
1127 if (size != 0)
1128 p_wiw = size;
1129
1130# ifdef FEAT_GUI
1131 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1132 if (gui.in_use)
1133 gui_init_which_components(NULL);
1134# endif
1135 }
1136 else
1137#endif
1138 {
1139 i = p_wh;
1140 if (size != 0)
1141 p_wh = size;
1142 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001143
1144 /*
1145 * make the new window the current window
1146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 win_enter(wp, FALSE);
1148#ifdef FEAT_VERTSPLIT
1149 if (flags & WSP_VERT)
1150 p_wiw = i;
1151 else
1152#endif
1153 p_wh = i;
1154
1155 return OK;
1156}
1157
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001158
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001159/*
1160 * Initialize window "newp" from window "oldp".
1161 * Used when splitting a window and when creating a new tab page.
1162 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001163 * WSP_NEWLOC may be specified in flags to prevent the location list from
1164 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001165 */
1166 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001167win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001168 win_T *newp;
1169 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001170 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001171{
1172 int i;
1173
1174 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001175#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001176 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001177#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001178 oldp->w_buffer->b_nwindows++;
1179 newp->w_cursor = oldp->w_cursor;
1180 newp->w_valid = 0;
1181 newp->w_curswant = oldp->w_curswant;
1182 newp->w_set_curswant = oldp->w_set_curswant;
1183 newp->w_topline = oldp->w_topline;
1184#ifdef FEAT_DIFF
1185 newp->w_topfill = oldp->w_topfill;
1186#endif
1187 newp->w_leftcol = oldp->w_leftcol;
1188 newp->w_pcmark = oldp->w_pcmark;
1189 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1190 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001191 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001192 newp->w_fraction = oldp->w_fraction;
1193 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1194#ifdef FEAT_JUMPLIST
1195 copy_jumplist(oldp, newp);
1196#endif
1197#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001198 if (flags & WSP_NEWLOC)
1199 {
1200 /* Don't copy the location list. */
1201 newp->w_llist = NULL;
1202 newp->w_llist_ref = NULL;
1203 }
1204 else
1205 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001206#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001207 newp->w_localdir = (oldp->w_localdir == NULL)
1208 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001209
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001210 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001211 for (i = 0; i < oldp->w_tagstacklen; i++)
1212 {
1213 newp->w_tagstack[i] = oldp->w_tagstack[i];
1214 if (newp->w_tagstack[i].tagname != NULL)
1215 newp->w_tagstack[i].tagname =
1216 vim_strsave(newp->w_tagstack[i].tagname);
1217 }
1218 newp->w_tagstackidx = oldp->w_tagstackidx;
1219 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001220#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001221 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001222#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001223
1224 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001225
Bram Moolenaara971b822011-09-14 14:43:25 +02001226#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001227 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001228#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001229}
1230
1231/*
1232 * Initialize window "newp" from window"old".
1233 * Only the essential things are copied.
1234 */
1235 static void
1236win_init_some(newp, oldp)
1237 win_T *newp;
1238 win_T *oldp;
1239{
1240 /* Use the same argument list. */
1241 newp->w_alist = oldp->w_alist;
1242 ++newp->w_alist->al_refcount;
1243 newp->w_arg_idx = oldp->w_arg_idx;
1244
1245 /* copy options from existing window */
1246 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001247}
1248
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249#endif /* FEAT_WINDOWS */
1250
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251#if defined(FEAT_WINDOWS) || defined(PROTO)
1252/*
1253 * Check if "win" is a pointer to an existing window.
1254 */
1255 int
1256win_valid(win)
1257 win_T *win;
1258{
1259 win_T *wp;
1260
1261 if (win == NULL)
1262 return FALSE;
1263 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1264 if (wp == win)
1265 return TRUE;
1266 return FALSE;
1267}
1268
1269/*
1270 * Return the number of windows.
1271 */
1272 int
1273win_count()
1274{
1275 win_T *wp;
1276 int count = 0;
1277
1278 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1279 ++count;
1280 return count;
1281}
1282
1283/*
1284 * Make "count" windows on the screen.
1285 * Return actual number of windows on the screen.
1286 * Must be called when there is just one window, filling the whole screen
1287 * (excluding the command line).
1288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 int
1290make_windows(count, vertical)
1291 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001292 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293{
1294 int maxcount;
1295 int todo;
1296
1297#ifdef FEAT_VERTSPLIT
1298 if (vertical)
1299 {
1300 /* Each windows needs at least 'winminwidth' lines and a separator
1301 * column. */
1302 maxcount = (curwin->w_width + curwin->w_vsep_width
1303 - (p_wiw - p_wmw)) / (p_wmw + 1);
1304 }
1305 else
1306#endif
1307 {
1308 /* Each window needs at least 'winminheight' lines and a status line. */
1309 maxcount = (curwin->w_height + curwin->w_status_height
1310 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1311 }
1312
1313 if (maxcount < 2)
1314 maxcount = 2;
1315 if (count > maxcount)
1316 count = maxcount;
1317
1318 /*
1319 * add status line now, otherwise first window will be too big
1320 */
1321 if (count > 1)
1322 last_status(TRUE);
1323
1324#ifdef FEAT_AUTOCMD
1325 /*
1326 * Don't execute autocommands while creating the windows. Must do that
1327 * when putting the buffers in the windows.
1328 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001329 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330#endif
1331
1332 /* todo is number of windows left to create */
1333 for (todo = count - 1; todo > 0; --todo)
1334#ifdef FEAT_VERTSPLIT
1335 if (vertical)
1336 {
1337 if (win_split(curwin->w_width - (curwin->w_width - todo)
1338 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1339 break;
1340 }
1341 else
1342#endif
1343 {
1344 if (win_split(curwin->w_height - (curwin->w_height - todo
1345 * STATUS_HEIGHT) / (todo + 1)
1346 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1347 break;
1348 }
1349
1350#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001351 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352#endif
1353
1354 /* return actual number of windows */
1355 return (count - todo);
1356}
1357
1358/*
1359 * Exchange current and next window
1360 */
1361 static void
1362win_exchange(Prenum)
1363 long Prenum;
1364{
1365 frame_T *frp;
1366 frame_T *frp2;
1367 win_T *wp;
1368 win_T *wp2;
1369 int temp;
1370
1371 if (lastwin == firstwin) /* just one window */
1372 {
1373 beep_flush();
1374 return;
1375 }
1376
1377#ifdef FEAT_GUI
1378 need_mouse_correct = TRUE;
1379#endif
1380
1381 /*
1382 * find window to exchange with
1383 */
1384 if (Prenum)
1385 {
1386 frp = curwin->w_frame->fr_parent->fr_child;
1387 while (frp != NULL && --Prenum > 0)
1388 frp = frp->fr_next;
1389 }
1390 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1391 frp = curwin->w_frame->fr_next;
1392 else /* Swap last window in row/col with previous */
1393 frp = curwin->w_frame->fr_prev;
1394
1395 /* We can only exchange a window with another window, not with a frame
1396 * containing windows. */
1397 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1398 return;
1399 wp = frp->fr_win;
1400
1401/*
1402 * 1. remove curwin from the list. Remember after which window it was in wp2
1403 * 2. insert curwin before wp in the list
1404 * if wp != wp2
1405 * 3. remove wp from the list
1406 * 4. insert wp after wp2
1407 * 5. exchange the status line height and vsep width.
1408 */
1409 wp2 = curwin->w_prev;
1410 frp2 = curwin->w_frame->fr_prev;
1411 if (wp->w_prev != curwin)
1412 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001413 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 frame_remove(curwin->w_frame);
1415 win_append(wp->w_prev, curwin);
1416 frame_insert(frp, curwin->w_frame);
1417 }
1418 if (wp != wp2)
1419 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001420 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 frame_remove(wp->w_frame);
1422 win_append(wp2, wp);
1423 if (frp2 == NULL)
1424 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1425 else
1426 frame_append(frp2, wp->w_frame);
1427 }
1428 temp = curwin->w_status_height;
1429 curwin->w_status_height = wp->w_status_height;
1430 wp->w_status_height = temp;
1431#ifdef FEAT_VERTSPLIT
1432 temp = curwin->w_vsep_width;
1433 curwin->w_vsep_width = wp->w_vsep_width;
1434 wp->w_vsep_width = temp;
1435
1436 /* If the windows are not in the same frame, exchange the sizes to avoid
1437 * messing up the window layout. Otherwise fix the frame sizes. */
1438 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1439 {
1440 temp = curwin->w_height;
1441 curwin->w_height = wp->w_height;
1442 wp->w_height = temp;
1443 temp = curwin->w_width;
1444 curwin->w_width = wp->w_width;
1445 wp->w_width = temp;
1446 }
1447 else
1448 {
1449 frame_fix_height(curwin);
1450 frame_fix_height(wp);
1451 frame_fix_width(curwin);
1452 frame_fix_width(wp);
1453 }
1454#endif
1455
1456 (void)win_comp_pos(); /* recompute window positions */
1457
1458 win_enter(wp, TRUE);
1459 redraw_later(CLEAR);
1460}
1461
1462/*
1463 * rotate windows: if upwards TRUE the second window becomes the first one
1464 * if upwards FALSE the first window becomes the second one
1465 */
1466 static void
1467win_rotate(upwards, count)
1468 int upwards;
1469 int count;
1470{
1471 win_T *wp1;
1472 win_T *wp2;
1473 frame_T *frp;
1474 int n;
1475
1476 if (firstwin == lastwin) /* nothing to do */
1477 {
1478 beep_flush();
1479 return;
1480 }
1481
1482#ifdef FEAT_GUI
1483 need_mouse_correct = TRUE;
1484#endif
1485
1486#ifdef FEAT_VERTSPLIT
1487 /* Check if all frames in this row/col have one window. */
1488 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1489 frp = frp->fr_next)
1490 if (frp->fr_win == NULL)
1491 {
1492 EMSG(_("E443: Cannot rotate when another window is split"));
1493 return;
1494 }
1495#endif
1496
1497 while (count--)
1498 {
1499 if (upwards) /* first window becomes last window */
1500 {
1501 /* remove first window/frame from the list */
1502 frp = curwin->w_frame->fr_parent->fr_child;
1503 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001504 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 frame_remove(frp);
1506
1507 /* find last frame and append removed window/frame after it */
1508 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1509 ;
1510 win_append(frp->fr_win, wp1);
1511 frame_append(frp, wp1->w_frame);
1512
1513 wp2 = frp->fr_win; /* previously last window */
1514 }
1515 else /* last window becomes first window */
1516 {
1517 /* find last window/frame in the list and remove it */
1518 for (frp = curwin->w_frame; frp->fr_next != NULL;
1519 frp = frp->fr_next)
1520 ;
1521 wp1 = frp->fr_win;
1522 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001523 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 frame_remove(frp);
1525
1526 /* append the removed window/frame before the first in the list */
1527 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1528 frame_insert(frp->fr_parent->fr_child, frp);
1529 }
1530
1531 /* exchange status height and vsep width of old and new last window */
1532 n = wp2->w_status_height;
1533 wp2->w_status_height = wp1->w_status_height;
1534 wp1->w_status_height = n;
1535 frame_fix_height(wp1);
1536 frame_fix_height(wp2);
1537#ifdef FEAT_VERTSPLIT
1538 n = wp2->w_vsep_width;
1539 wp2->w_vsep_width = wp1->w_vsep_width;
1540 wp1->w_vsep_width = n;
1541 frame_fix_width(wp1);
1542 frame_fix_width(wp2);
1543#endif
1544
1545 /* recompute w_winrow and w_wincol for all windows */
1546 (void)win_comp_pos();
1547 }
1548
1549 redraw_later(CLEAR);
1550}
1551
1552/*
1553 * Move the current window to the very top/bottom/left/right of the screen.
1554 */
1555 static void
1556win_totop(size, flags)
1557 int size;
1558 int flags;
1559{
1560 int dir;
1561 int height = curwin->w_height;
1562
1563 if (lastwin == firstwin)
1564 {
1565 beep_flush();
1566 return;
1567 }
1568
1569 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001570 (void)winframe_remove(curwin, &dir, NULL);
1571 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 last_status(FALSE); /* may need to remove last status line */
1573 (void)win_comp_pos(); /* recompute window positions */
1574
1575 /* Split a window on the desired side and put the window there. */
1576 (void)win_split_ins(size, flags, curwin, dir);
1577 if (!(flags & WSP_VERT))
1578 {
1579 win_setheight(height);
1580 if (p_ea)
1581 win_equal(curwin, TRUE, 'v');
1582 }
1583
1584#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1585 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1586 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001587 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589}
1590
1591/*
1592 * Move window "win1" to below/right of "win2" and make "win1" the current
1593 * window. Only works within the same frame!
1594 */
1595 void
1596win_move_after(win1, win2)
1597 win_T *win1, *win2;
1598{
1599 int height;
1600
1601 /* check if the arguments are reasonable */
1602 if (win1 == win2)
1603 return;
1604
1605 /* check if there is something to do */
1606 if (win2->w_next != win1)
1607 {
1608 /* may need move the status line/vertical separator of the last window
1609 * */
1610 if (win1 == lastwin)
1611 {
1612 height = win1->w_prev->w_status_height;
1613 win1->w_prev->w_status_height = win1->w_status_height;
1614 win1->w_status_height = height;
1615#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001616 if (win1->w_prev->w_vsep_width == 1)
1617 {
1618 /* Remove the vertical separator from the last-but-one window,
1619 * add it to the last window. Adjust the frame widths. */
1620 win1->w_prev->w_vsep_width = 0;
1621 win1->w_prev->w_frame->fr_width -= 1;
1622 win1->w_vsep_width = 1;
1623 win1->w_frame->fr_width += 1;
1624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625#endif
1626 }
1627 else if (win2 == lastwin)
1628 {
1629 height = win1->w_status_height;
1630 win1->w_status_height = win2->w_status_height;
1631 win2->w_status_height = height;
1632#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001633 if (win1->w_vsep_width == 1)
1634 {
1635 /* Remove the vertical separator from win1, add it to the last
1636 * window, win2. Adjust the frame widths. */
1637 win2->w_vsep_width = 1;
1638 win2->w_frame->fr_width += 1;
1639 win1->w_vsep_width = 0;
1640 win1->w_frame->fr_width -= 1;
1641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642#endif
1643 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001644 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 frame_remove(win1->w_frame);
1646 win_append(win2, win1);
1647 frame_append(win2->w_frame, win1->w_frame);
1648
1649 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1650 redraw_later(NOT_VALID);
1651 }
1652 win_enter(win1, FALSE);
1653}
1654
1655/*
1656 * Make all windows the same height.
1657 * 'next_curwin' will soon be the current window, make sure it has enough
1658 * rows.
1659 */
1660 void
1661win_equal(next_curwin, current, dir)
1662 win_T *next_curwin; /* pointer to current window to be or NULL */
1663 int current; /* do only frame with current window */
1664 int dir; /* 'v' for vertically, 'h' for horizontally,
1665 'b' for both, 0 for using p_ead */
1666{
1667 if (dir == 0)
1668#ifdef FEAT_VERTSPLIT
1669 dir = *p_ead;
1670#else
1671 dir = 'b';
1672#endif
1673 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001674 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001675 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676}
1677
1678/*
1679 * Set a frame to a new position and height, spreading the available room
1680 * equally over contained frames.
1681 * The window "next_curwin" (if not NULL) should at least get the size from
1682 * 'winheight' and 'winwidth' if possible.
1683 */
1684 static void
1685win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1686 win_T *next_curwin; /* pointer to current window to be or NULL */
1687 int current; /* do only frame with current window */
1688 frame_T *topfr; /* frame to set size off */
1689 int dir; /* 'v', 'h' or 'b', see win_equal() */
1690 int col; /* horizontal position for frame */
1691 int row; /* vertical position for frame */
1692 int width; /* new width of frame */
1693 int height; /* new height of frame */
1694{
1695 int n, m;
1696 int extra_sep = 0;
1697 int wincount, totwincount = 0;
1698 frame_T *fr;
1699 int next_curwin_size = 0;
1700 int room = 0;
1701 int new_size;
1702 int has_next_curwin = 0;
1703 int hnc;
1704
1705 if (topfr->fr_layout == FR_LEAF)
1706 {
1707 /* Set the width/height of this frame.
1708 * Redraw when size or position changes */
1709 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1710#ifdef FEAT_VERTSPLIT
1711 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1712#endif
1713 )
1714 {
1715 topfr->fr_win->w_winrow = row;
1716 frame_new_height(topfr, height, FALSE, FALSE);
1717#ifdef FEAT_VERTSPLIT
1718 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001719 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720#endif
1721 redraw_all_later(CLEAR);
1722 }
1723 }
1724#ifdef FEAT_VERTSPLIT
1725 else if (topfr->fr_layout == FR_ROW)
1726 {
1727 topfr->fr_width = width;
1728 topfr->fr_height = height;
1729
1730 if (dir != 'v') /* equalize frame widths */
1731 {
1732 /* Compute the maximum number of windows horizontally in this
1733 * frame. */
1734 n = frame_minwidth(topfr, NOWIN);
1735 /* add one for the rightmost window, it doesn't have a separator */
1736 if (col + width == Columns)
1737 extra_sep = 1;
1738 else
1739 extra_sep = 0;
1740 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001741 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001743 /*
1744 * Compute width for "next_curwin" window and room available for
1745 * other windows.
1746 * "m" is the minimal width when counting p_wiw for "next_curwin".
1747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 m = frame_minwidth(topfr, next_curwin);
1749 room = width - m;
1750 if (room < 0)
1751 {
1752 next_curwin_size = p_wiw + room;
1753 room = 0;
1754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 else
1756 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001757 next_curwin_size = -1;
1758 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1759 {
1760 /* If 'winfixwidth' set keep the window width if
1761 * possible.
1762 * Watch out for this window being the next_curwin. */
1763 if (frame_fixed_width(fr))
1764 {
1765 n = frame_minwidth(fr, NOWIN);
1766 new_size = fr->fr_width;
1767 if (frame_has_win(fr, next_curwin))
1768 {
1769 room += p_wiw - p_wmw;
1770 next_curwin_size = 0;
1771 if (new_size < p_wiw)
1772 new_size = p_wiw;
1773 }
1774 else
1775 /* These windows don't use up room. */
1776 totwincount -= (n + (fr->fr_next == NULL
1777 ? extra_sep : 0)) / (p_wmw + 1);
1778 room -= new_size - n;
1779 if (room < 0)
1780 {
1781 new_size += room;
1782 room = 0;
1783 }
1784 fr->fr_newwidth = new_size;
1785 }
1786 }
1787 if (next_curwin_size == -1)
1788 {
1789 if (!has_next_curwin)
1790 next_curwin_size = 0;
1791 else if (totwincount > 1
1792 && (room + (totwincount - 2))
1793 / (totwincount - 1) > p_wiw)
1794 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001795 /* Can make all windows wider than 'winwidth', spread
1796 * the room equally. */
1797 next_curwin_size = (room + p_wiw
1798 + (totwincount - 1) * p_wmw
1799 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001800 room -= next_curwin_size - p_wiw;
1801 }
1802 else
1803 next_curwin_size = p_wiw;
1804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001806
1807 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 --totwincount; /* don't count curwin */
1809 }
1810
1811 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1812 {
1813 n = m = 0;
1814 wincount = 1;
1815 if (fr->fr_next == NULL)
1816 /* last frame gets all that remains (avoid roundoff error) */
1817 new_size = width;
1818 else if (dir == 'v')
1819 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001820 else if (frame_fixed_width(fr))
1821 {
1822 new_size = fr->fr_newwidth;
1823 wincount = 0; /* doesn't count as a sizeable window */
1824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 else
1826 {
1827 /* Compute the maximum number of windows horiz. in "fr". */
1828 n = frame_minwidth(fr, NOWIN);
1829 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1830 / (p_wmw + 1);
1831 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001832 if (has_next_curwin)
1833 hnc = frame_has_win(fr, next_curwin);
1834 else
1835 hnc = FALSE;
1836 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001838 if (totwincount == 0)
1839 new_size = room;
1840 else
1841 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001843 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 {
1845 next_curwin_size -= p_wiw - (m - n);
1846 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001847 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001849 else
1850 room -= new_size;
1851 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 }
1853
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001854 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 * window, unless equalizing all frames. */
1856 if (!current || dir != 'v' || topfr->fr_parent != NULL
1857 || (new_size != fr->fr_width)
1858 || frame_has_win(fr, next_curwin))
1859 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001860 new_size, height);
1861 col += new_size;
1862 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 totwincount -= wincount;
1864 }
1865 }
1866#endif
1867 else /* topfr->fr_layout == FR_COL */
1868 {
1869#ifdef FEAT_VERTSPLIT
1870 topfr->fr_width = width;
1871#endif
1872 topfr->fr_height = height;
1873
1874 if (dir != 'h') /* equalize frame heights */
1875 {
1876 /* Compute maximum number of windows vertically in this frame. */
1877 n = frame_minheight(topfr, NOWIN);
1878 /* add one for the bottom window if it doesn't have a statusline */
1879 if (row + height == cmdline_row && p_ls == 0)
1880 extra_sep = 1;
1881 else
1882 extra_sep = 0;
1883 totwincount = (n + extra_sep) / (p_wmh + 1);
1884 has_next_curwin = frame_has_win(topfr, next_curwin);
1885
1886 /*
1887 * Compute height for "next_curwin" window and room available for
1888 * other windows.
1889 * "m" is the minimal height when counting p_wh for "next_curwin".
1890 */
1891 m = frame_minheight(topfr, next_curwin);
1892 room = height - m;
1893 if (room < 0)
1894 {
1895 /* The room is less then 'winheight', use all space for the
1896 * current window. */
1897 next_curwin_size = p_wh + room;
1898 room = 0;
1899 }
1900 else
1901 {
1902 next_curwin_size = -1;
1903 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1904 {
1905 /* If 'winfixheight' set keep the window height if
1906 * possible.
1907 * Watch out for this window being the next_curwin. */
1908 if (frame_fixed_height(fr))
1909 {
1910 n = frame_minheight(fr, NOWIN);
1911 new_size = fr->fr_height;
1912 if (frame_has_win(fr, next_curwin))
1913 {
1914 room += p_wh - p_wmh;
1915 next_curwin_size = 0;
1916 if (new_size < p_wh)
1917 new_size = p_wh;
1918 }
1919 else
1920 /* These windows don't use up room. */
1921 totwincount -= (n + (fr->fr_next == NULL
1922 ? extra_sep : 0)) / (p_wmh + 1);
1923 room -= new_size - n;
1924 if (room < 0)
1925 {
1926 new_size += room;
1927 room = 0;
1928 }
1929 fr->fr_newheight = new_size;
1930 }
1931 }
1932 if (next_curwin_size == -1)
1933 {
1934 if (!has_next_curwin)
1935 next_curwin_size = 0;
1936 else if (totwincount > 1
1937 && (room + (totwincount - 2))
1938 / (totwincount - 1) > p_wh)
1939 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001940 /* can make all windows higher than 'winheight',
1941 * spread the room equally. */
1942 next_curwin_size = (room + p_wh
1943 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 + (totwincount - 1)) / totwincount;
1945 room -= next_curwin_size - p_wh;
1946 }
1947 else
1948 next_curwin_size = p_wh;
1949 }
1950 }
1951
1952 if (has_next_curwin)
1953 --totwincount; /* don't count curwin */
1954 }
1955
1956 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1957 {
1958 n = m = 0;
1959 wincount = 1;
1960 if (fr->fr_next == NULL)
1961 /* last frame gets all that remains (avoid roundoff error) */
1962 new_size = height;
1963 else if (dir == 'h')
1964 new_size = fr->fr_height;
1965 else if (frame_fixed_height(fr))
1966 {
1967 new_size = fr->fr_newheight;
1968 wincount = 0; /* doesn't count as a sizeable window */
1969 }
1970 else
1971 {
1972 /* Compute the maximum number of windows vert. in "fr". */
1973 n = frame_minheight(fr, NOWIN);
1974 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1975 / (p_wmh + 1);
1976 m = frame_minheight(fr, next_curwin);
1977 if (has_next_curwin)
1978 hnc = frame_has_win(fr, next_curwin);
1979 else
1980 hnc = FALSE;
1981 if (hnc) /* don't count next_curwin */
1982 --wincount;
1983 if (totwincount == 0)
1984 new_size = room;
1985 else
1986 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1987 / totwincount;
1988 if (hnc) /* add next_curwin size */
1989 {
1990 next_curwin_size -= p_wh - (m - n);
1991 new_size += next_curwin_size;
1992 room -= new_size - next_curwin_size;
1993 }
1994 else
1995 room -= new_size;
1996 new_size += n;
1997 }
1998 /* Skip frame that is full width when splitting or closing a
1999 * window, unless equalizing all frames. */
2000 if (!current || dir != 'h' || topfr->fr_parent != NULL
2001 || (new_size != fr->fr_height)
2002 || frame_has_win(fr, next_curwin))
2003 win_equal_rec(next_curwin, current, fr, dir, col, row,
2004 width, new_size);
2005 row += new_size;
2006 height -= new_size;
2007 totwincount -= wincount;
2008 }
2009 }
2010}
2011
2012/*
2013 * close all windows for buffer 'buf'
2014 */
2015 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002016close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002018 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002020 win_T *wp;
2021 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002022 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023
2024 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002025
2026 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002028 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2029#ifdef FEAT_AUTOCMD
2030 && !(wp->w_closing || wp->w_buffer->b_closing)
2031#endif
2032 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002034 win_close(wp, FALSE);
2035
2036 /* Start all over, autocommands may change the window layout. */
2037 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
2039 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002040 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002042
2043 /* Also check windows in other tab pages. */
2044 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2045 {
2046 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002047 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002048 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002049 if (wp->w_buffer == buf
2050#ifdef FEAT_AUTOCMD
2051 && !(wp->w_closing || wp->w_buffer->b_closing)
2052#endif
2053 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002054 {
2055 win_close_othertab(wp, FALSE, tp);
2056
2057 /* Start all over, the tab page may be closed and
2058 * autocommands may change the window layout. */
2059 nexttp = first_tabpage;
2060 break;
2061 }
2062 }
2063
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002065
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002066 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002067 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002068 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069}
2070
2071/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002072 * Return TRUE if the current window is the only window that exists (ignoring
2073 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002074 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002075 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002076 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002077last_window()
2078{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002079 return (one_window() && first_tabpage->tp_next == NULL);
2080}
2081
2082/*
2083 * Return TRUE if there is only one window other than "aucmd_win" in the
2084 * current tab page.
2085 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002086 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002087one_window()
2088{
2089#ifdef FEAT_AUTOCMD
2090 win_T *wp;
2091 int seen_one = FALSE;
2092
2093 FOR_ALL_WINDOWS(wp)
2094 {
2095 if (wp != aucmd_win)
2096 {
2097 if (seen_one)
2098 return FALSE;
2099 seen_one = TRUE;
2100 }
2101 }
2102 return TRUE;
2103#else
2104 return firstwin == lastwin;
2105#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002106}
2107
2108/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002109 * Close the possibly last window in a tab page.
2110 * Returns TRUE when the window was closed already.
2111 */
2112 static int
2113close_last_window_tabpage(win, free_buf, prev_curtab)
2114 win_T *win;
2115 int free_buf;
2116 tabpage_T *prev_curtab;
2117{
2118 if (firstwin == lastwin)
2119 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002120#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002121 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002122#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002123
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002124 /*
2125 * Closing the last window in a tab page. First go to another tab
2126 * page and then close the window and the tab page. This avoids that
2127 * curwin and curtab are invalid while we are freeing memory, they may
2128 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002129 * Don't trigger autocommands yet, they may use wrong values, so do
2130 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002131 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002132 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002133 redraw_tabline = TRUE;
2134
2135 /* Safety check: Autocommands may have closed the window when jumping
2136 * to the other tab page. */
2137 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2138 {
2139 int h = tabline_height();
2140
2141 win_close_othertab(win, free_buf, prev_curtab);
2142 if (h != tabline_height())
2143 shell_new_rows();
2144 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002145 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2146 * that now. */
2147#ifdef FEAT_AUTOCMD
Bram Moolenaara8596c42012-06-13 14:28:20 +02002148 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002149 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2150 if (old_curbuf != curbuf)
2151 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002152#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002153 return TRUE;
2154 }
2155 return FALSE;
2156}
2157
2158/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002159 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 * If "free_buf" is TRUE related buffer may be unloaded.
2161 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002162 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002163 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002165 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166win_close(win, free_buf)
2167 win_T *win;
2168 int free_buf;
2169{
2170 win_T *wp;
2171#ifdef FEAT_AUTOCMD
2172 int other_buffer = FALSE;
2173#endif
2174 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 int dir;
2176 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002177 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002179 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 {
2181 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002182 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 }
2184
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002185#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002186 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002187 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002188 if (win == aucmd_win)
2189 {
2190 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002191 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002192 }
2193 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2194 {
2195 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002196 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002197 }
2198#endif
2199
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002200 /* When closing the last window in a tab page first go to another tab page
2201 * and then close the window and the tab page to avoid that curwin and
2202 * curtab are invalid while we are freeing memory. */
2203 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002204 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002205
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 /* When closing the help window, try restoring a snapshot after closing
2207 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002208 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 help_window = TRUE;
2210 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002211 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212
2213#ifdef FEAT_AUTOCMD
2214 if (win == curwin)
2215 {
2216 /*
2217 * Guess which window is going to be the new current window.
2218 * This may change because of the autocommands (sigh).
2219 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002220 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221
2222 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002223 * Be careful: If autocommands delete the window or cause this window
2224 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 */
2226 if (wp->w_buffer != curbuf)
2227 {
2228 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002229 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002231 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002232 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002233 win->w_closing = FALSE;
2234 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002235 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002237 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002239 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002240 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002241 win->w_closing = FALSE;
2242 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002243 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244# ifdef FEAT_EVAL
2245 /* autocmds may abort script processing */
2246 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248# endif
2249 }
2250#endif
2251
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002252#ifdef FEAT_GUI
2253 /* Avoid trouble with scrollbars that are going to be deleted in
2254 * win_free(). */
2255 if (gui.in_use)
2256 out_flush();
2257#endif
2258
Bram Moolenaara971b822011-09-14 14:43:25 +02002259#ifdef FEAT_SYN_HL
2260 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002261 if (win->w_buffer != NULL)
2262 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002263#endif
2264
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 /*
2266 * Close the link to the buffer.
2267 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002268 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002269 {
2270#ifdef FEAT_AUTOCMD
2271 win->w_closing = TRUE;
2272#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002273 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002274#ifdef FEAT_AUTOCMD
2275 if (win_valid(win))
2276 win->w_closing = FALSE;
2277#endif
2278 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002279
Bram Moolenaar802418d2013-01-17 14:00:11 +01002280 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2281 && (last_window() || curtab != prev_curtab
2282 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002283 {
2284 /* Autocommands have close all windows, quit now. Restore
2285 * curwin->w_buffer, otherwise writing viminfo may fail. */
2286 if (curwin->w_buffer == NULL)
2287 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002288 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002289 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002290
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002292 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002293 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002294 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002295 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
Bram Moolenaara971b822011-09-14 14:43:25 +02002297 /* Free the memory used for the window and get the window that received
2298 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002299 wp = win_free_mem(win, &dir, NULL);
2300
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 /* Make sure curwin isn't invalid. It can cause severe trouble when
2302 * printing an error message. For win_equal() curbuf needs to be valid
2303 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002304 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
2306 curwin = wp;
2307#ifdef FEAT_QUICKFIX
2308 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2309 {
2310 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002311 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 * finding another window to go to.
2313 */
2314 for (;;)
2315 {
2316 if (wp->w_next == NULL)
2317 wp = firstwin;
2318 else
2319 wp = wp->w_next;
2320 if (wp == curwin)
2321 break;
2322 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2323 {
2324 curwin = wp;
2325 break;
2326 }
2327 }
2328 }
2329#endif
2330 curbuf = curwin->w_buffer;
2331 close_curwin = TRUE;
2332 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002333 if (p_ea
2334#ifdef FEAT_VERTSPLIT
2335 && (*p_ead == 'b' || *p_ead == dir)
2336#endif
2337 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 win_equal(curwin, TRUE,
2339#ifdef FEAT_VERTSPLIT
2340 dir
2341#else
2342 0
2343#endif
2344 );
2345 else
2346 win_comp_pos();
2347 if (close_curwin)
2348 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002349 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350#ifdef FEAT_AUTOCMD
2351 if (other_buffer)
2352 /* careful: after this wp and win may be invalid! */
2353 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2354#endif
2355 }
2356
2357 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002358 * If last window has a status line now and we don't want one,
2359 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 */
2361 last_status(FALSE);
2362
2363 /* After closing the help window, try restoring the window layout from
2364 * before it was opened. */
2365 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002366 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
2368#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2369 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2370 if (gui.in_use && !win_hasvertsplit())
2371 gui_init_which_components(NULL);
2372#endif
2373
2374 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002375 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376}
2377
2378/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002379 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002380 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002381 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002382 * Caller must check if buffer is hidden and whether the tabline needs to be
2383 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002384 */
2385 void
2386win_close_othertab(win, free_buf, tp)
2387 win_T *win;
2388 int free_buf;
2389 tabpage_T *tp;
2390{
2391 win_T *wp;
2392 int dir;
2393 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002394 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002395
Bram Moolenaar362ce482012-06-06 19:02:45 +02002396#ifdef FEAT_AUTOCMD
2397 if (win->w_closing || win->w_buffer->b_closing)
2398 return; /* window is already being closed */
2399#endif
2400
Bram Moolenaarf740b292006-02-16 22:11:02 +00002401 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002402 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002403
2404 /* Careful: Autocommands may have closed the tab page or made it the
2405 * current tab page. */
2406 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2407 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002408 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002409 return;
2410
2411 /* Autocommands may have closed the window already. */
2412 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2413 ;
2414 if (wp == NULL)
2415 return;
2416
Bram Moolenaarf740b292006-02-16 22:11:02 +00002417 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002418 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002419 {
2420 if (tp == first_tabpage)
2421 first_tabpage = tp->tp_next;
2422 else
2423 {
2424 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2425 ptp = ptp->tp_next)
2426 ;
2427 if (ptp == NULL)
2428 {
2429 EMSG2(_(e_intern2), "win_close_othertab()");
2430 return;
2431 }
2432 ptp->tp_next = tp->tp_next;
2433 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002434 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002435 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002436
2437 /* Free the memory used for the window. */
2438 win_free_mem(win, &dir, tp);
2439
2440 if (free_tp)
2441 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002442}
2443
2444/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002445 * Free the memory used for a window.
2446 * Returns a pointer to the window that got the freed up space.
2447 */
2448 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002449win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002450 win_T *win;
2451 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002452 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002453{
2454 frame_T *frp;
2455 win_T *wp;
2456
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002457 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002458 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002459 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002460 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002461 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002462
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002463 /* When deleting the current window of another tab page select a new
2464 * current window. */
2465 if (tp != NULL && win == tp->tp_curwin)
2466 tp->tp_curwin = wp;
2467
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002468 return wp;
2469}
2470
2471#if defined(EXITFREE) || defined(PROTO)
2472 void
2473win_free_all()
2474{
2475 int dummy;
2476
Bram Moolenaarf740b292006-02-16 22:11:02 +00002477# ifdef FEAT_WINDOWS
2478 while (first_tabpage->tp_next != NULL)
2479 tabpage_close(TRUE);
2480# endif
2481
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002482# ifdef FEAT_AUTOCMD
2483 if (aucmd_win != NULL)
2484 {
2485 (void)win_free_mem(aucmd_win, &dummy, NULL);
2486 aucmd_win = NULL;
2487 }
2488# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002489
2490 while (firstwin != NULL)
2491 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002492
2493 /* No window should be used after this. Set curwin to NULL to crash
2494 * instead of using freed memory. */
2495 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002496}
2497#endif
2498
2499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 * Remove a window and its frame from the tree of frames.
2501 * Returns a pointer to the window that got the freed up space.
2502 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002503 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002504winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002506 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002507 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
2509 frame_T *frp, *frp2, *frp3;
2510 frame_T *frp_close = win->w_frame;
2511 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
2513 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002514 * If there is only one window there is nothing to remove.
2515 */
2516 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2517 return NULL;
2518
2519 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 * Remove the window from its frame.
2521 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002522 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 wp = frame2win(frp2);
2524
2525 /* Remove this frame from the list of frames. */
2526 frame_remove(frp_close);
2527
2528#ifdef FEAT_VERTSPLIT
2529 if (frp_close->fr_parent->fr_layout == FR_COL)
2530 {
2531#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002532 /* When 'winfixheight' is set, try to find another frame in the column
2533 * (as close to the closed frame as possible) to distribute the height
2534 * to. */
2535 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2536 {
2537 frp = frp_close->fr_prev;
2538 frp3 = frp_close->fr_next;
2539 while (frp != NULL || frp3 != NULL)
2540 {
2541 if (frp != NULL)
2542 {
2543 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2544 {
2545 frp2 = frp;
2546 wp = frp->fr_win;
2547 break;
2548 }
2549 frp = frp->fr_prev;
2550 }
2551 if (frp3 != NULL)
2552 {
2553 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2554 {
2555 frp2 = frp3;
2556 wp = frp3->fr_win;
2557 break;
2558 }
2559 frp3 = frp3->fr_next;
2560 }
2561 }
2562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2564 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565#ifdef FEAT_VERTSPLIT
2566 *dirp = 'v';
2567 }
2568 else
2569 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002570 /* When 'winfixwidth' is set, try to find another frame in the column
2571 * (as close to the closed frame as possible) to distribute the width
2572 * to. */
2573 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2574 {
2575 frp = frp_close->fr_prev;
2576 frp3 = frp_close->fr_next;
2577 while (frp != NULL || frp3 != NULL)
2578 {
2579 if (frp != NULL)
2580 {
2581 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2582 {
2583 frp2 = frp;
2584 wp = frp->fr_win;
2585 break;
2586 }
2587 frp = frp->fr_prev;
2588 }
2589 if (frp3 != NULL)
2590 {
2591 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2592 {
2593 frp2 = frp3;
2594 wp = frp3->fr_win;
2595 break;
2596 }
2597 frp3 = frp3->fr_next;
2598 }
2599 }
2600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002602 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 *dirp = 'h';
2604 }
2605#endif
2606
2607 /* If rows/columns go to a window below/right its positions need to be
2608 * updated. Can only be done after the sizes have been updated. */
2609 if (frp2 == frp_close->fr_next)
2610 {
2611 int row = win->w_winrow;
2612 int col = W_WINCOL(win);
2613
2614 frame_comp_pos(frp2, &row, &col);
2615 }
2616
2617 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2618 {
2619 /* There is no other frame in this list, move its info to the parent
2620 * and remove it. */
2621 frp2->fr_parent->fr_layout = frp2->fr_layout;
2622 frp2->fr_parent->fr_child = frp2->fr_child;
2623 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2624 frp->fr_parent = frp2->fr_parent;
2625 frp2->fr_parent->fr_win = frp2->fr_win;
2626 if (frp2->fr_win != NULL)
2627 frp2->fr_win->w_frame = frp2->fr_parent;
2628 frp = frp2->fr_parent;
2629 vim_free(frp2);
2630
2631 frp2 = frp->fr_parent;
2632 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2633 {
2634 /* The frame above the parent has the same layout, have to merge
2635 * the frames into this list. */
2636 if (frp2->fr_child == frp)
2637 frp2->fr_child = frp->fr_child;
2638 frp->fr_child->fr_prev = frp->fr_prev;
2639 if (frp->fr_prev != NULL)
2640 frp->fr_prev->fr_next = frp->fr_child;
2641 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2642 {
2643 frp3->fr_parent = frp2;
2644 if (frp3->fr_next == NULL)
2645 {
2646 frp3->fr_next = frp->fr_next;
2647 if (frp->fr_next != NULL)
2648 frp->fr_next->fr_prev = frp3;
2649 break;
2650 }
2651 }
2652 vim_free(frp);
2653 }
2654 }
2655
2656 return wp;
2657}
2658
2659/*
2660 * Find out which frame is going to get the freed up space when "win" is
2661 * closed.
2662 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2663 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2664 * This makes opening a window and closing it immediately keep the same window
2665 * layout.
2666 */
2667 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002668win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002670 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671{
2672 frame_T *frp;
2673 int b;
2674
Bram Moolenaarf740b292006-02-16 22:11:02 +00002675 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002676 /* Last window in this tab page, will go to next tab page. */
2677 return alt_tabpage()->tp_curwin->w_frame;
2678
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 frp = win->w_frame;
2680#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002681 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 b = p_spr;
2683 else
2684#endif
2685 b = p_sb;
2686 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2687 return frp->fr_next;
2688 return frp->fr_prev;
2689}
2690
2691/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002692 * Return the tabpage that will be used if the current one is closed.
2693 */
2694 static tabpage_T *
2695alt_tabpage()
2696{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002697 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002698
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002699 /* Use the next tab page if possible. */
2700 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002701 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002702
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002703 /* Find the last but one tab page. */
2704 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2705 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002706 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002707}
2708
2709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 * Find the left-upper window in frame "frp".
2711 */
2712 static win_T *
2713frame2win(frp)
2714 frame_T *frp;
2715{
2716 while (frp->fr_win == NULL)
2717 frp = frp->fr_child;
2718 return frp->fr_win;
2719}
2720
2721/*
2722 * Return TRUE if frame "frp" contains window "wp".
2723 */
2724 static int
2725frame_has_win(frp, wp)
2726 frame_T *frp;
2727 win_T *wp;
2728{
2729 frame_T *p;
2730
2731 if (frp->fr_layout == FR_LEAF)
2732 return frp->fr_win == wp;
2733
2734 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2735 if (frame_has_win(p, wp))
2736 return TRUE;
2737 return FALSE;
2738}
2739
2740/*
2741 * Set a new height for a frame. Recursively sets the height for contained
2742 * frames and windows. Caller must take care of positions.
2743 */
2744 static void
2745frame_new_height(topfrp, height, topfirst, wfh)
2746 frame_T *topfrp;
2747 int height;
2748 int topfirst; /* resize topmost contained frame first */
2749 int wfh; /* obey 'winfixheight' when there is a choice;
2750 may cause the height not to be set */
2751{
2752 frame_T *frp;
2753 int extra_lines;
2754 int h;
2755
2756 if (topfrp->fr_win != NULL)
2757 {
2758 /* Simple case: just one window. */
2759 win_new_height(topfrp->fr_win,
2760 height - topfrp->fr_win->w_status_height);
2761 }
2762#ifdef FEAT_VERTSPLIT
2763 else if (topfrp->fr_layout == FR_ROW)
2764 {
2765 do
2766 {
2767 /* All frames in this row get the same new height. */
2768 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2769 {
2770 frame_new_height(frp, height, topfirst, wfh);
2771 if (frp->fr_height > height)
2772 {
2773 /* Could not fit the windows, make the whole row higher. */
2774 height = frp->fr_height;
2775 break;
2776 }
2777 }
2778 }
2779 while (frp != NULL);
2780 }
2781#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002782 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 {
2784 /* Complicated case: Resize a column of frames. Resize the bottom
2785 * frame first, frames above that when needed. */
2786
2787 frp = topfrp->fr_child;
2788 if (wfh)
2789 /* Advance past frames with one window with 'wfh' set. */
2790 while (frame_fixed_height(frp))
2791 {
2792 frp = frp->fr_next;
2793 if (frp == NULL)
2794 return; /* no frame without 'wfh', give up */
2795 }
2796 if (!topfirst)
2797 {
2798 /* Find the bottom frame of this column */
2799 while (frp->fr_next != NULL)
2800 frp = frp->fr_next;
2801 if (wfh)
2802 /* Advance back for frames with one window with 'wfh' set. */
2803 while (frame_fixed_height(frp))
2804 frp = frp->fr_prev;
2805 }
2806
2807 extra_lines = height - topfrp->fr_height;
2808 if (extra_lines < 0)
2809 {
2810 /* reduce height of contained frames, bottom or top frame first */
2811 while (frp != NULL)
2812 {
2813 h = frame_minheight(frp, NULL);
2814 if (frp->fr_height + extra_lines < h)
2815 {
2816 extra_lines += frp->fr_height - h;
2817 frame_new_height(frp, h, topfirst, wfh);
2818 }
2819 else
2820 {
2821 frame_new_height(frp, frp->fr_height + extra_lines,
2822 topfirst, wfh);
2823 break;
2824 }
2825 if (topfirst)
2826 {
2827 do
2828 frp = frp->fr_next;
2829 while (wfh && frp != NULL && frame_fixed_height(frp));
2830 }
2831 else
2832 {
2833 do
2834 frp = frp->fr_prev;
2835 while (wfh && frp != NULL && frame_fixed_height(frp));
2836 }
2837 /* Increase "height" if we could not reduce enough frames. */
2838 if (frp == NULL)
2839 height -= extra_lines;
2840 }
2841 }
2842 else if (extra_lines > 0)
2843 {
2844 /* increase height of bottom or top frame */
2845 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2846 }
2847 }
2848 topfrp->fr_height = height;
2849}
2850
2851/*
2852 * Return TRUE if height of frame "frp" should not be changed because of
2853 * the 'winfixheight' option.
2854 */
2855 static int
2856frame_fixed_height(frp)
2857 frame_T *frp;
2858{
2859 /* frame with one window: fixed height if 'winfixheight' set. */
2860 if (frp->fr_win != NULL)
2861 return frp->fr_win->w_p_wfh;
2862
2863 if (frp->fr_layout == FR_ROW)
2864 {
2865 /* The frame is fixed height if one of the frames in the row is fixed
2866 * height. */
2867 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2868 if (frame_fixed_height(frp))
2869 return TRUE;
2870 return FALSE;
2871 }
2872
2873 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2874 * frames in the row are fixed height. */
2875 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2876 if (!frame_fixed_height(frp))
2877 return FALSE;
2878 return TRUE;
2879}
2880
2881#ifdef FEAT_VERTSPLIT
2882/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002883 * Return TRUE if width of frame "frp" should not be changed because of
2884 * the 'winfixwidth' option.
2885 */
2886 static int
2887frame_fixed_width(frp)
2888 frame_T *frp;
2889{
2890 /* frame with one window: fixed width if 'winfixwidth' set. */
2891 if (frp->fr_win != NULL)
2892 return frp->fr_win->w_p_wfw;
2893
2894 if (frp->fr_layout == FR_COL)
2895 {
2896 /* The frame is fixed width if one of the frames in the row is fixed
2897 * width. */
2898 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2899 if (frame_fixed_width(frp))
2900 return TRUE;
2901 return FALSE;
2902 }
2903
2904 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2905 * frames in the row are fixed width. */
2906 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2907 if (!frame_fixed_width(frp))
2908 return FALSE;
2909 return TRUE;
2910}
2911
2912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 * Add a status line to windows at the bottom of "frp".
2914 * Note: Does not check if there is room!
2915 */
2916 static void
2917frame_add_statusline(frp)
2918 frame_T *frp;
2919{
2920 win_T *wp;
2921
2922 if (frp->fr_layout == FR_LEAF)
2923 {
2924 wp = frp->fr_win;
2925 if (wp->w_status_height == 0)
2926 {
2927 if (wp->w_height > 0) /* don't make it negative */
2928 --wp->w_height;
2929 wp->w_status_height = STATUS_HEIGHT;
2930 }
2931 }
2932 else if (frp->fr_layout == FR_ROW)
2933 {
2934 /* Handle all the frames in the row. */
2935 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2936 frame_add_statusline(frp);
2937 }
2938 else /* frp->fr_layout == FR_COL */
2939 {
2940 /* Only need to handle the last frame in the column. */
2941 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2942 ;
2943 frame_add_statusline(frp);
2944 }
2945}
2946
2947/*
2948 * Set width of a frame. Handles recursively going through contained frames.
2949 * May remove separator line for windows at the right side (for win_close()).
2950 */
2951 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002952frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 frame_T *topfrp;
2954 int width;
2955 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002956 int wfw; /* obey 'winfixwidth' when there is a choice;
2957 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958{
2959 frame_T *frp;
2960 int extra_cols;
2961 int w;
2962 win_T *wp;
2963
2964 if (topfrp->fr_layout == FR_LEAF)
2965 {
2966 /* Simple case: just one window. */
2967 wp = topfrp->fr_win;
2968 /* Find out if there are any windows right of this one. */
2969 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2970 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2971 break;
2972 if (frp->fr_parent == NULL)
2973 wp->w_vsep_width = 0;
2974 win_new_width(wp, width - wp->w_vsep_width);
2975 }
2976 else if (topfrp->fr_layout == FR_COL)
2977 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002978 do
2979 {
2980 /* All frames in this column get the same new width. */
2981 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2982 {
2983 frame_new_width(frp, width, leftfirst, wfw);
2984 if (frp->fr_width > width)
2985 {
2986 /* Could not fit the windows, make whole column wider. */
2987 width = frp->fr_width;
2988 break;
2989 }
2990 }
2991 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 }
2993 else /* fr_layout == FR_ROW */
2994 {
2995 /* Complicated case: Resize a row of frames. Resize the rightmost
2996 * frame first, frames left of it when needed. */
2997
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002999 if (wfw)
3000 /* Advance past frames with one window with 'wfw' set. */
3001 while (frame_fixed_width(frp))
3002 {
3003 frp = frp->fr_next;
3004 if (frp == NULL)
3005 return; /* no frame without 'wfw', give up */
3006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003008 {
3009 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 while (frp->fr_next != NULL)
3011 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003012 if (wfw)
3013 /* Advance back for frames with one window with 'wfw' set. */
3014 while (frame_fixed_width(frp))
3015 frp = frp->fr_prev;
3016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017
3018 extra_cols = width - topfrp->fr_width;
3019 if (extra_cols < 0)
3020 {
3021 /* reduce frame width, rightmost frame first */
3022 while (frp != NULL)
3023 {
3024 w = frame_minwidth(frp, NULL);
3025 if (frp->fr_width + extra_cols < w)
3026 {
3027 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003028 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 }
3030 else
3031 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003032 frame_new_width(frp, frp->fr_width + extra_cols,
3033 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 break;
3035 }
3036 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003037 {
3038 do
3039 frp = frp->fr_next;
3040 while (wfw && frp != NULL && frame_fixed_width(frp));
3041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003043 {
3044 do
3045 frp = frp->fr_prev;
3046 while (wfw && frp != NULL && frame_fixed_width(frp));
3047 }
3048 /* Increase "width" if we could not reduce enough frames. */
3049 if (frp == NULL)
3050 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 }
3052 }
3053 else if (extra_cols > 0)
3054 {
3055 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003056 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 }
3058 }
3059 topfrp->fr_width = width;
3060}
3061
3062/*
3063 * Add the vertical separator to windows at the right side of "frp".
3064 * Note: Does not check if there is room!
3065 */
3066 static void
3067frame_add_vsep(frp)
3068 frame_T *frp;
3069{
3070 win_T *wp;
3071
3072 if (frp->fr_layout == FR_LEAF)
3073 {
3074 wp = frp->fr_win;
3075 if (wp->w_vsep_width == 0)
3076 {
3077 if (wp->w_width > 0) /* don't make it negative */
3078 --wp->w_width;
3079 wp->w_vsep_width = 1;
3080 }
3081 }
3082 else if (frp->fr_layout == FR_COL)
3083 {
3084 /* Handle all the frames in the column. */
3085 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3086 frame_add_vsep(frp);
3087 }
3088 else /* frp->fr_layout == FR_ROW */
3089 {
3090 /* Only need to handle the last frame in the row. */
3091 frp = frp->fr_child;
3092 while (frp->fr_next != NULL)
3093 frp = frp->fr_next;
3094 frame_add_vsep(frp);
3095 }
3096}
3097
3098/*
3099 * Set frame width from the window it contains.
3100 */
3101 static void
3102frame_fix_width(wp)
3103 win_T *wp;
3104{
3105 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3106}
3107#endif
3108
3109/*
3110 * Set frame height from the window it contains.
3111 */
3112 static void
3113frame_fix_height(wp)
3114 win_T *wp;
3115{
3116 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3117}
3118
3119/*
3120 * Compute the minimal height for frame "topfrp".
3121 * Uses the 'winminheight' option.
3122 * When "next_curwin" isn't NULL, use p_wh for this window.
3123 * When "next_curwin" is NOWIN, don't use at least one line for the current
3124 * window.
3125 */
3126 static int
3127frame_minheight(topfrp, next_curwin)
3128 frame_T *topfrp;
3129 win_T *next_curwin;
3130{
3131 frame_T *frp;
3132 int m;
3133#ifdef FEAT_VERTSPLIT
3134 int n;
3135#endif
3136
3137 if (topfrp->fr_win != NULL)
3138 {
3139 if (topfrp->fr_win == next_curwin)
3140 m = p_wh + topfrp->fr_win->w_status_height;
3141 else
3142 {
3143 /* window: minimal height of the window plus status line */
3144 m = p_wmh + topfrp->fr_win->w_status_height;
3145 /* Current window is minimal one line high */
3146 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3147 ++m;
3148 }
3149 }
3150#ifdef FEAT_VERTSPLIT
3151 else if (topfrp->fr_layout == FR_ROW)
3152 {
3153 /* get the minimal height from each frame in this row */
3154 m = 0;
3155 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3156 {
3157 n = frame_minheight(frp, next_curwin);
3158 if (n > m)
3159 m = n;
3160 }
3161 }
3162#endif
3163 else
3164 {
3165 /* Add up the minimal heights for all frames in this column. */
3166 m = 0;
3167 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3168 m += frame_minheight(frp, next_curwin);
3169 }
3170
3171 return m;
3172}
3173
3174#ifdef FEAT_VERTSPLIT
3175/*
3176 * Compute the minimal width for frame "topfrp".
3177 * When "next_curwin" isn't NULL, use p_wiw for this window.
3178 * When "next_curwin" is NOWIN, don't use at least one column for the current
3179 * window.
3180 */
3181 static int
3182frame_minwidth(topfrp, next_curwin)
3183 frame_T *topfrp;
3184 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3185{
3186 frame_T *frp;
3187 int m, n;
3188
3189 if (topfrp->fr_win != NULL)
3190 {
3191 if (topfrp->fr_win == next_curwin)
3192 m = p_wiw + topfrp->fr_win->w_vsep_width;
3193 else
3194 {
3195 /* window: minimal width of the window plus separator column */
3196 m = p_wmw + topfrp->fr_win->w_vsep_width;
3197 /* Current window is minimal one column wide */
3198 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3199 ++m;
3200 }
3201 }
3202 else if (topfrp->fr_layout == FR_COL)
3203 {
3204 /* get the minimal width from each frame in this column */
3205 m = 0;
3206 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3207 {
3208 n = frame_minwidth(frp, next_curwin);
3209 if (n > m)
3210 m = n;
3211 }
3212 }
3213 else
3214 {
3215 /* Add up the minimal widths for all frames in this row. */
3216 m = 0;
3217 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3218 m += frame_minwidth(frp, next_curwin);
3219 }
3220
3221 return m;
3222}
3223#endif
3224
3225
3226/*
3227 * Try to close all windows except current one.
3228 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3229 * used and the buffer was modified.
3230 *
3231 * Used by ":bdel" and ":only".
3232 */
3233 void
3234close_others(message, forceit)
3235 int message;
3236 int forceit; /* always hide all other windows */
3237{
3238 win_T *wp;
3239 win_T *nextwp;
3240 int r;
3241
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003242 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 {
3244 if (message
3245#ifdef FEAT_AUTOCMD
3246 && !autocmd_busy
3247#endif
3248 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003249 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 return;
3251 }
3252
3253 /* Be very careful here: autocommands may change the window layout. */
3254 for (wp = firstwin; win_valid(wp); wp = nextwp)
3255 {
3256 nextwp = wp->w_next;
3257 if (wp != curwin) /* don't close current window */
3258 {
3259
3260 /* Check if it's allowed to abandon this window */
3261 r = can_abandon(wp->w_buffer, forceit);
3262#ifdef FEAT_AUTOCMD
3263 if (!win_valid(wp)) /* autocommands messed wp up */
3264 {
3265 nextwp = firstwin;
3266 continue;
3267 }
3268#endif
3269 if (!r)
3270 {
3271#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3272 if (message && (p_confirm || cmdmod.confirm) && p_write)
3273 {
3274 dialog_changed(wp->w_buffer, FALSE);
3275# ifdef FEAT_AUTOCMD
3276 if (!win_valid(wp)) /* autocommands messed wp up */
3277 {
3278 nextwp = firstwin;
3279 continue;
3280 }
3281# endif
3282 }
3283 if (bufIsChanged(wp->w_buffer))
3284#endif
3285 continue;
3286 }
3287 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3288 }
3289 }
3290
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003291 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 EMSG(_("E445: Other window contains changes"));
3293}
3294
3295#endif /* FEAT_WINDOWS */
3296
3297/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003298 * Init the current window "curwin".
3299 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 */
3301 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003302curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003304 win_init_empty(curwin);
3305}
3306
3307 void
3308win_init_empty(wp)
3309 win_T *wp;
3310{
3311 redraw_win_later(wp, NOT_VALID);
3312 wp->w_lines_valid = 0;
3313 wp->w_cursor.lnum = 1;
3314 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003316 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003318 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3319 wp->w_pcmark.col = 0;
3320 wp->w_prev_pcmark.lnum = 0;
3321 wp->w_prev_pcmark.col = 0;
3322 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003324 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003326 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003328 if (wp->w_p_rl)
3329 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003331 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003333#ifdef FEAT_SYN_HL
3334 wp->w_s = &wp->w_buffer->b_s;
3335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336}
3337
3338/*
3339 * Allocate the first window and put an empty buffer in it.
3340 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003341 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003343 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344win_alloc_first()
3345{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003346 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003347 return FAIL;
3348
3349#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003350 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003351 if (first_tabpage == NULL)
3352 return FAIL;
3353 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003354 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003355#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003356
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003357 return OK;
3358}
3359
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003360#if defined(FEAT_AUTOCMD) || defined(PROTO)
3361/*
3362 * Init "aucmd_win". This can only be done after the first
3363 * window is fully initialized, thus it can't be in win_alloc_first().
3364 */
3365 void
3366win_alloc_aucmd_win()
3367{
3368 aucmd_win = win_alloc(NULL, TRUE);
3369 if (aucmd_win != NULL)
3370 {
3371 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003372 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003373 new_frame(aucmd_win);
3374 }
3375}
3376#endif
3377
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003378/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003379 * Allocate the first window or the first window in a new tab page.
3380 * When "oldwin" is NULL create an empty buffer for it.
3381 * When "oldwin" is not NULL copy info from it to the new window (only with
3382 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003383 * Return FAIL when something goes wrong (out of memory).
3384 */
3385 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003386win_alloc_firstwin(oldwin)
3387 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003388{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003389 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003390 if (oldwin == NULL)
3391 {
3392 /* Very first window, need to create an empty buffer for it and
3393 * initialize from scratch. */
3394 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3395 if (curwin == NULL || curbuf == NULL)
3396 return FAIL;
3397 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003398#ifdef FEAT_SYN_HL
3399 curwin->w_s = &(curbuf->b_s);
3400#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003401 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003403 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003405 curwin_init(); /* init current window */
3406 }
3407#ifdef FEAT_WINDOWS
3408 else
3409 {
3410 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003411 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003412
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003413 /* We don't want cursor- and scroll-binding in the first window. */
3414 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003415 }
3416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003418 new_frame(curwin);
3419 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003420 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003421 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422#ifdef FEAT_VERTSPLIT
3423 topframe->fr_width = Columns;
3424#endif
3425 topframe->fr_height = Rows - p_ch;
3426 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003427
3428 return OK;
3429}
3430
3431/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003432 * Create a frame for window "wp".
3433 */
3434 static void
3435new_frame(win_T *wp)
3436{
3437 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3438
3439 wp->w_frame = frp;
3440 if (frp != NULL)
3441 {
3442 frp->fr_layout = FR_LEAF;
3443 frp->fr_win = wp;
3444 }
3445}
3446
3447/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003448 * Initialize the window and frame size to the maximum.
3449 */
3450 void
3451win_init_size()
3452{
3453 firstwin->w_height = ROWS_AVAIL;
3454 topframe->fr_height = ROWS_AVAIL;
3455#ifdef FEAT_VERTSPLIT
3456 firstwin->w_width = Columns;
3457 topframe->fr_width = Columns;
3458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459}
3460
3461#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003462
3463/*
3464 * Allocate a new tabpage_T and init the values.
3465 * Returns NULL when out of memory.
3466 */
3467 static tabpage_T *
3468alloc_tabpage()
3469{
3470 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003471# ifdef FEAT_GUI
3472 int i;
3473# endif
3474
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003475
3476 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003477 if (tp == NULL)
3478 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003479
Bram Moolenaar429fa852013-04-15 12:27:36 +02003480# ifdef FEAT_EVAL
3481 /* init t: variables */
3482 tp->tp_vars = dict_alloc();
3483 if (tp->tp_vars == NULL)
3484 {
3485 vim_free(tp);
3486 return NULL;
3487 }
3488 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3489# endif
3490
3491# ifdef FEAT_GUI
3492 for (i = 0; i < 3; i++)
3493 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003494# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003495# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003496 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003497# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003498 tp->tp_ch_used = p_ch;
3499
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003500 return tp;
3501}
3502
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003503 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003504free_tabpage(tp)
3505 tabpage_T *tp;
3506{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003507 int idx;
3508
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003509# ifdef FEAT_DIFF
3510 diff_clear(tp);
3511# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003512 for (idx = 0; idx < SNAP_COUNT; ++idx)
3513 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003514#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003515 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3516 hash_init(&tp->tp_vars->dv_hashtab);
3517 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003518#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003519
3520#ifdef FEAT_PYTHON
3521 python_tabpage_free(tp);
3522#endif
3523
3524#ifdef FEAT_PYTHON3
3525 python3_tabpage_free(tp);
3526#endif
3527
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003528 vim_free(tp);
3529}
3530
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003531/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003532 * Create a new Tab page with one window.
3533 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003534 * When "after" is 0 put it just after the current Tab page.
3535 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003536 * Return FAIL or OK.
3537 */
3538 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003539win_new_tabpage(after)
3540 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003541{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003542 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003543 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003544 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003545
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003546 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003547 if (newtp == NULL)
3548 return FAIL;
3549
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003550 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003551 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003552 {
3553 vim_free(newtp);
3554 return FAIL;
3555 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003556 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003557
3558 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003559 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003560 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003561 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003562 if (after == 1)
3563 {
3564 /* New tab page becomes the first one. */
3565 newtp->tp_next = first_tabpage;
3566 first_tabpage = newtp;
3567 }
3568 else
3569 {
3570 if (after > 0)
3571 {
3572 /* Put new tab page before tab page "after". */
3573 n = 2;
3574 for (tp = first_tabpage; tp->tp_next != NULL
3575 && n < after; tp = tp->tp_next)
3576 ++n;
3577 }
3578 newtp->tp_next = tp->tp_next;
3579 tp->tp_next = newtp;
3580 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003581 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003582 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003583 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003584
3585 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003586 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003587
3588#if defined(FEAT_GUI)
3589 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3590 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003591 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003592#endif
3593
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003594 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003595#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003596 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003597 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003598#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003599 return OK;
3600 }
3601
3602 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003603 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003604 return FAIL;
3605}
3606
3607/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003608 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3609 * like with ":split".
3610 * Returns OK if a new tab page was created, FAIL otherwise.
3611 */
3612 int
3613may_open_tabpage()
3614{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003615 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003616
Bram Moolenaard326ce82007-03-11 14:48:29 +00003617 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003618 {
3619 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003620 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003621 return win_new_tabpage(n);
3622 }
3623 return FAIL;
3624}
3625
3626/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003627 * Create up to "maxcount" tabpages with empty windows.
3628 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003629 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003630 int
3631make_tabpages(maxcount)
3632 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003633{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003634 int count = maxcount;
3635 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003636
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003637 /* Limit to 'tabpagemax' tabs. */
3638 if (count > p_tpm)
3639 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003640
3641#ifdef FEAT_AUTOCMD
3642 /*
3643 * Don't execute autocommands while creating the tab pages. Must do that
3644 * when putting the buffers in the windows.
3645 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003646 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003647#endif
3648
3649 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003650 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003651 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003652
3653#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003654 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003655#endif
3656
3657 /* return actual number of tab pages */
3658 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003659}
3660
3661/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003662 * Return TRUE when "tpc" points to a valid tab page.
3663 */
3664 int
3665valid_tabpage(tpc)
3666 tabpage_T *tpc;
3667{
3668 tabpage_T *tp;
3669
3670 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3671 if (tp == tpc)
3672 return TRUE;
3673 return FALSE;
3674}
3675
3676/*
3677 * Find tab page "n" (first one is 1). Returns NULL when not found.
3678 */
3679 tabpage_T *
3680find_tabpage(n)
3681 int n;
3682{
3683 tabpage_T *tp;
3684 int i = 1;
3685
3686 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3687 ++i;
3688 return tp;
3689}
3690
3691/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003692 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003693 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003694 */
3695 int
3696tabpage_index(ftp)
3697 tabpage_T *ftp;
3698{
3699 int i = 1;
3700 tabpage_T *tp;
3701
3702 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3703 ++i;
3704 return i;
3705}
3706
3707/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003708 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003709 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003710 * FAIL.
3711 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003712 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003713 static int
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003714leave_tabpage(new_curbuf, trigger_leave_autocmds)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003715 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003716 NULL if unknown */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003717 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003718{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003719 tabpage_T *tp = curtab;
3720
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003721 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003722#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003723 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003724 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003725 if (new_curbuf != curbuf)
3726 {
3727 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3728 if (curtab != tp)
3729 return FAIL;
3730 }
3731 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3732 if (curtab != tp)
3733 return FAIL;
3734 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003735 if (curtab != tp)
3736 return FAIL;
3737 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003738#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003739#if defined(FEAT_GUI)
3740 /* Remove the scrollbars. They may be added back later. */
3741 if (gui.in_use)
3742 gui_remove_scrollbars();
3743#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003744 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003745 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003746 tp->tp_firstwin = firstwin;
3747 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003748 tp->tp_old_Rows = Rows;
3749 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003750 firstwin = NULL;
3751 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003752 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003753}
3754
3755/*
3756 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003757 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003758 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3759 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003760 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003761 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003762enter_tabpage(tp, old_curbuf, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003763 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003764 buf_T *old_curbuf UNUSED;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003765 int trigger_enter_autocmds UNUSED;
3766 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003767{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003768 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003769 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003770
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003771 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003772 firstwin = tp->tp_firstwin;
3773 lastwin = tp->tp_lastwin;
3774 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003775
3776 /* We would like doing the TabEnter event first, but we don't have a
3777 * valid current window yet, which may break some commands.
3778 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003779 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3780 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003781 prevwin = next_prevwin;
3782
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003783 last_status(FALSE); /* status line may appear or disappear */
3784 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003785 must_redraw = CLEAR; /* need to redraw everything */
3786#ifdef FEAT_DIFF
3787 diff_need_scrollbind = TRUE;
3788#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003789
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003790 /* The tabpage line may have appeared or disappeared, may need to resize
3791 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003792 * frame sizes too. Use the stored value of p_ch, so that it can be
3793 * different for each tab page. */
3794 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003795 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3796#ifdef FEAT_GUI_TABLINE
3797 && !gui_use_tabline()
3798#endif
3799 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003800 shell_new_rows();
3801#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003802 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003803 shell_new_columns(); /* update window widths */
3804#endif
3805
3806#if defined(FEAT_GUI)
3807 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3808 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003809 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003810#endif
3811
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003812#ifdef FEAT_AUTOCMD
3813 /* Apply autocommands after updating the display, when 'rows' and
3814 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003815 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003816 {
3817 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3818 if (old_curbuf != curbuf)
3819 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3820 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003821#endif
3822
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003823 redraw_all_later(CLEAR);
3824}
3825
3826/*
3827 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003828 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003829 */
3830 void
3831goto_tabpage(n)
3832 int n;
3833{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003834 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003835 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003836 int i;
3837
Bram Moolenaard68071d2006-05-02 22:08:30 +00003838 if (text_locked())
3839 {
3840 /* Not allowed when editing the command line. */
3841#ifdef FEAT_CMDWIN
3842 if (cmdwin_type != 0)
3843 EMSG(_(e_cmdwin));
3844 else
3845#endif
3846 EMSG(_(e_secure));
3847 return;
3848 }
3849
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003850 /* If there is only one it can't work. */
3851 if (first_tabpage->tp_next == NULL)
3852 {
3853 if (n > 1)
3854 beep_flush();
3855 return;
3856 }
3857
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003858 if (n == 0)
3859 {
3860 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003861 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003862 tp = first_tabpage;
3863 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003864 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003865 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003866 else if (n < 0)
3867 {
3868 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3869 * this N times. */
3870 ttp = curtab;
3871 for (i = n; i < 0; ++i)
3872 {
3873 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3874 tp = tp->tp_next)
3875 ;
3876 ttp = tp;
3877 }
3878 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003879 else if (n == 9999)
3880 {
3881 /* Go to last tab page. */
3882 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3883 ;
3884 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003885 else
3886 {
3887 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003888 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003889 if (tp == NULL)
3890 {
3891 beep_flush();
3892 return;
3893 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003894 }
3895
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003896 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003897
3898#ifdef FEAT_GUI_TABLINE
3899 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003900 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003901#endif
3902}
3903
3904/*
3905 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003906 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3907 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003908 * Note: doesn't update the GUI tab.
3909 */
3910 void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003911goto_tabpage_tp(tp, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003912 tabpage_T *tp;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003913 int trigger_enter_autocmds;
3914 int trigger_leave_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003915{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003916 /* Don't repeat a message in another tab page. */
3917 set_keep_msg(NULL, 0);
3918
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003919 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3920 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003921 {
3922 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003923 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3924 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003925 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003926 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3927 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003928 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003929}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
3931/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003932 * Enter window "wp" in tab page "tp".
3933 * Also updates the GUI tab.
3934 */
3935 void
3936goto_tabpage_win(tp, wp)
3937 tabpage_T *tp;
3938 win_T *wp;
3939{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003940 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003941 if (curtab == tp && win_valid(wp))
3942 {
3943 win_enter(wp, TRUE);
3944# ifdef FEAT_GUI_TABLINE
3945 if (gui_use_tabline())
3946 gui_mch_set_curtab(tabpage_index(curtab));
3947# endif
3948 }
3949}
3950
3951/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003952 * Move the current tab page to before tab page "nr".
3953 */
3954 void
3955tabpage_move(nr)
3956 int nr;
3957{
3958 int n = nr;
3959 tabpage_T *tp;
3960
3961 if (first_tabpage->tp_next == NULL)
3962 return;
3963
3964 /* Remove the current tab page from the list of tab pages. */
3965 if (curtab == first_tabpage)
3966 first_tabpage = curtab->tp_next;
3967 else
3968 {
3969 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3970 if (tp->tp_next == curtab)
3971 break;
3972 if (tp == NULL) /* "cannot happen" */
3973 return;
3974 tp->tp_next = curtab->tp_next;
3975 }
3976
3977 /* Re-insert it at the specified position. */
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02003978 if (n <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003979 {
3980 curtab->tp_next = first_tabpage;
3981 first_tabpage = curtab;
3982 }
3983 else
3984 {
3985 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3986 --n;
3987 curtab->tp_next = tp->tp_next;
3988 tp->tp_next = curtab;
3989 }
3990
3991 /* Need to redraw the tabline. Tab page contents doesn't change. */
3992 redraw_tabline = TRUE;
3993}
3994
3995
3996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 * Go to another window.
3998 * When jumping to another buffer, stop Visual mode. Do this before
3999 * changing windows so we can yank the selection into the '*' register.
4000 * When jumping to another window on the same buffer, adjust its cursor
4001 * position to keep the same Visual area.
4002 */
4003 void
4004win_goto(wp)
4005 win_T *wp;
4006{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004007#ifdef FEAT_CONCEAL
4008 win_T *owp = curwin;
4009#endif
4010
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004011 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 {
4013 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004014 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 return;
4016 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004017#ifdef FEAT_AUTOCMD
4018 if (curbuf_locked())
4019 return;
4020#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004021
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 if (wp->w_buffer != curbuf)
4023 reset_VIsual_and_resel();
4024 else if (VIsual_active)
4025 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
4027#ifdef FEAT_GUI
4028 need_mouse_correct = TRUE;
4029#endif
4030 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004031
4032#ifdef FEAT_CONCEAL
4033 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004034 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004035 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004036 if (curwin->w_p_cole > 0 && !msg_scrolled)
4037 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039}
4040
4041#if defined(FEAT_PERL) || defined(PROTO)
4042/*
4043 * Find window number "winnr" (counting top to bottom).
4044 */
4045 win_T *
4046win_find_nr(winnr)
4047 int winnr;
4048{
4049 win_T *wp;
4050
4051# ifdef FEAT_WINDOWS
4052 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4053 if (--winnr == 0)
4054 break;
4055 return wp;
4056# else
4057 return curwin;
4058# endif
4059}
4060#endif
4061
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004062#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4063 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004064/*
4065 * Find the tabpage for window "win".
4066 */
4067 tabpage_T *
4068win_find_tabpage(win)
4069 win_T *win;
4070{
4071 win_T *wp;
4072 tabpage_T *tp;
4073
4074 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004075 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4076 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004077 if (wp == win)
4078 return tp;
4079 return NULL;
4080}
4081#endif
4082
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083#ifdef FEAT_VERTSPLIT
4084/*
4085 * Move to window above or below "count" times.
4086 */
4087 static void
4088win_goto_ver(up, count)
4089 int up; /* TRUE to go to win above */
4090 long count;
4091{
4092 frame_T *fr;
4093 frame_T *nfr;
4094 frame_T *foundfr;
4095
4096 foundfr = curwin->w_frame;
4097 while (count--)
4098 {
4099 /*
4100 * First go upwards in the tree of frames until we find a upwards or
4101 * downwards neighbor.
4102 */
4103 fr = foundfr;
4104 for (;;)
4105 {
4106 if (fr == topframe)
4107 goto end;
4108 if (up)
4109 nfr = fr->fr_prev;
4110 else
4111 nfr = fr->fr_next;
4112 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4113 break;
4114 fr = fr->fr_parent;
4115 }
4116
4117 /*
4118 * Now go downwards to find the bottom or top frame in it.
4119 */
4120 for (;;)
4121 {
4122 if (nfr->fr_layout == FR_LEAF)
4123 {
4124 foundfr = nfr;
4125 break;
4126 }
4127 fr = nfr->fr_child;
4128 if (nfr->fr_layout == FR_ROW)
4129 {
4130 /* Find the frame at the cursor row. */
4131 while (fr->fr_next != NULL
4132 && frame2win(fr)->w_wincol + fr->fr_width
4133 <= curwin->w_wincol + curwin->w_wcol)
4134 fr = fr->fr_next;
4135 }
4136 if (nfr->fr_layout == FR_COL && up)
4137 while (fr->fr_next != NULL)
4138 fr = fr->fr_next;
4139 nfr = fr;
4140 }
4141 }
4142end:
4143 if (foundfr != NULL)
4144 win_goto(foundfr->fr_win);
4145}
4146
4147/*
4148 * Move to left or right window.
4149 */
4150 static void
4151win_goto_hor(left, count)
4152 int left; /* TRUE to go to left win */
4153 long count;
4154{
4155 frame_T *fr;
4156 frame_T *nfr;
4157 frame_T *foundfr;
4158
4159 foundfr = curwin->w_frame;
4160 while (count--)
4161 {
4162 /*
4163 * First go upwards in the tree of frames until we find a left or
4164 * right neighbor.
4165 */
4166 fr = foundfr;
4167 for (;;)
4168 {
4169 if (fr == topframe)
4170 goto end;
4171 if (left)
4172 nfr = fr->fr_prev;
4173 else
4174 nfr = fr->fr_next;
4175 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4176 break;
4177 fr = fr->fr_parent;
4178 }
4179
4180 /*
4181 * Now go downwards to find the leftmost or rightmost frame in it.
4182 */
4183 for (;;)
4184 {
4185 if (nfr->fr_layout == FR_LEAF)
4186 {
4187 foundfr = nfr;
4188 break;
4189 }
4190 fr = nfr->fr_child;
4191 if (nfr->fr_layout == FR_COL)
4192 {
4193 /* Find the frame at the cursor row. */
4194 while (fr->fr_next != NULL
4195 && frame2win(fr)->w_winrow + fr->fr_height
4196 <= curwin->w_winrow + curwin->w_wrow)
4197 fr = fr->fr_next;
4198 }
4199 if (nfr->fr_layout == FR_ROW && left)
4200 while (fr->fr_next != NULL)
4201 fr = fr->fr_next;
4202 nfr = fr;
4203 }
4204 }
4205end:
4206 if (foundfr != NULL)
4207 win_goto(foundfr->fr_win);
4208}
4209#endif
4210
4211/*
4212 * Make window "wp" the current window.
4213 */
4214 void
4215win_enter(wp, undo_sync)
4216 win_T *wp;
4217 int undo_sync;
4218{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004219 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220}
4221
4222/*
4223 * Make window wp the current window.
4224 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4225 * been closed and isn't valid.
4226 */
4227 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004228win_enter_ext(wp, undo_sync, curwin_invalid, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 win_T *wp;
4230 int undo_sync;
4231 int curwin_invalid;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004232 int trigger_enter_autocmds UNUSED;
4233 int trigger_leave_autocmds UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
4235#ifdef FEAT_AUTOCMD
4236 int other_buffer = FALSE;
4237#endif
4238
4239 if (wp == curwin && !curwin_invalid) /* nothing to do */
4240 return;
4241
4242#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004243 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
4245 /*
4246 * Be careful: If autocommands delete the window, return now.
4247 */
4248 if (wp->w_buffer != curbuf)
4249 {
4250 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4251 other_buffer = TRUE;
4252 if (!win_valid(wp))
4253 return;
4254 }
4255 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4256 if (!win_valid(wp))
4257 return;
4258# ifdef FEAT_EVAL
4259 /* autocmds may abort script processing */
4260 if (aborting())
4261 return;
4262# endif
4263 }
4264#endif
4265
4266 /* sync undo before leaving the current buffer */
4267 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004268 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004269
4270 /* Might need to scroll the old window before switching, e.g., when the
4271 * cursor was moved. */
4272 update_topline();
4273
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 /* may have to copy the buffer options when 'cpo' contains 'S' */
4275 if (wp->w_buffer != curbuf)
4276 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4277 if (!curwin_invalid)
4278 {
4279 prevwin = curwin; /* remember for CTRL-W p */
4280 curwin->w_redr_status = TRUE;
4281 }
4282 curwin = wp;
4283 curbuf = wp->w_buffer;
4284 check_cursor();
4285#ifdef FEAT_VIRTUALEDIT
4286 if (!virtual_active())
4287 curwin->w_cursor.coladd = 0;
4288#endif
4289 changed_line_abv_curs(); /* assume cursor position needs updating */
4290
4291 if (curwin->w_localdir != NULL)
4292 {
4293 /* Window has a local directory: Save current directory as global
4294 * directory (unless that was done already) and change to the local
4295 * directory. */
4296 if (globaldir == NULL)
4297 {
4298 char_u cwd[MAXPATHL];
4299
4300 if (mch_dirname(cwd, MAXPATHL) == OK)
4301 globaldir = vim_strsave(cwd);
4302 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004303 if (mch_chdir((char *)curwin->w_localdir) == 0)
4304 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306 else if (globaldir != NULL)
4307 {
4308 /* Window doesn't have a local directory and we are not in the global
4309 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004310 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 vim_free(globaldir);
4312 globaldir = NULL;
4313 shorten_fnames(TRUE);
4314 }
4315
4316#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004317 if (trigger_enter_autocmds)
4318 {
4319 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4320 if (other_buffer)
4321 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323#endif
4324
4325#ifdef FEAT_TITLE
4326 maketitle();
4327#endif
4328 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004329 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 if (restart_edit)
4331 redraw_later(VALID); /* causes status line redraw */
4332
4333 /* set window height to desired minimal value */
4334 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4335 win_setheight((int)p_wh);
4336 else if (curwin->w_height == 0)
4337 win_setheight(1);
4338
4339#ifdef FEAT_VERTSPLIT
4340 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004341 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 win_setwidth((int)p_wiw);
4343#endif
4344
4345#ifdef FEAT_MOUSE
4346 setmouse(); /* in case jumped to/from help buffer */
4347#endif
4348
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004349 /* Change directories when the 'acd' option is set. */
4350 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351}
4352
4353#endif /* FEAT_WINDOWS */
4354
4355#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4356/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004357 * Jump to the first open window that contains buffer "buf", if one exists.
4358 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 */
4360 win_T *
4361buf_jump_open_win(buf)
4362 buf_T *buf;
4363{
4364# ifdef FEAT_WINDOWS
4365 win_T *wp;
4366
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004367 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 if (wp->w_buffer == buf)
4369 break;
4370 if (wp != NULL)
4371 win_enter(wp, FALSE);
4372 return wp;
4373# else
4374 if (curwin->w_buffer == buf)
4375 return curwin;
4376 return NULL;
4377# endif
4378}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004379
4380/*
4381 * Jump to the first open window in any tab page that contains buffer "buf",
4382 * if one exists.
4383 * Returns a pointer to the window found, otherwise NULL.
4384 */
4385 win_T *
4386buf_jump_open_tab(buf)
4387 buf_T *buf;
4388{
4389# ifdef FEAT_WINDOWS
4390 win_T *wp;
4391 tabpage_T *tp;
4392
4393 /* First try the current tab page. */
4394 wp = buf_jump_open_win(buf);
4395 if (wp != NULL)
4396 return wp;
4397
4398 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4399 if (tp != curtab)
4400 {
4401 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4402 if (wp->w_buffer == buf)
4403 break;
4404 if (wp != NULL)
4405 {
4406 goto_tabpage_win(tp, wp);
4407 if (curwin != wp)
4408 wp = NULL; /* something went wrong */
4409 break;
4410 }
4411 }
4412
4413 return wp;
4414# else
4415 if (curwin->w_buffer == buf)
4416 return curwin;
4417 return NULL;
4418# endif
4419}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420#endif
4421
4422/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004423 * Allocate a window structure and link it in the window list when "hidden" is
4424 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004427win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004428 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004429 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004431 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
4433 /*
4434 * allocate window structure and linesizes arrays
4435 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004436 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004437 if (new_wp == NULL)
4438 return NULL;
4439
4440 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004442 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004443 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 }
4445
Bram Moolenaar429fa852013-04-15 12:27:36 +02004446#ifdef FEAT_EVAL
4447 /* init w: variables */
4448 new_wp->w_vars = dict_alloc();
4449 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004451 win_free_lsize(new_wp);
4452 vim_free(new_wp);
4453 return NULL;
4454 }
4455 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004456#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004457
4458#ifdef FEAT_AUTOCMD
4459 /* Don't execute autocommands while the window is not properly
4460 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4461 * event. */
4462 block_autocmds();
4463#endif
4464 /*
4465 * link the window in the window list
4466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004468 if (!hidden)
4469 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470#endif
4471#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004472 new_wp->w_wincol = 0;
4473 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474#endif
4475
Bram Moolenaar429fa852013-04-15 12:27:36 +02004476 /* position the display and the cursor at the top of the file. */
4477 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004479 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004481 new_wp->w_botline = 2;
4482 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004484 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485#endif
4486
Bram Moolenaar429fa852013-04-15 12:27:36 +02004487 /* We won't calculate w_fraction until resizing the window */
4488 new_wp->w_fraction = 0;
4489 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490
4491#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004492 if (gui.in_use)
4493 {
4494 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4495 SBAR_LEFT, new_wp);
4496 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4497 SBAR_RIGHT, new_wp);
4498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499#endif
4500#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004501 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004503#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004504 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004505#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004506#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004507 new_wp->w_match_head = NULL;
4508 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004509#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004510 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511}
4512
4513#if defined(FEAT_WINDOWS) || defined(PROTO)
4514
4515/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004516 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 */
4518 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004519win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004521 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522{
4523 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004524 buf_T *buf;
4525 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004527#ifdef FEAT_FOLDING
4528 clearFolding(wp);
4529#endif
4530
4531 /* reduce the reference count to the argument list. */
4532 alist_unlink(wp->w_alist);
4533
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004534#ifdef FEAT_AUTOCMD
4535 /* Don't execute autocommands while the window is halfway being deleted.
4536 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004537 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004538#endif
4539
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004540#ifdef FEAT_LUA
4541 lua_window_free(wp);
4542#endif
4543
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004544#ifdef FEAT_MZSCHEME
4545 mzscheme_window_free(wp);
4546#endif
4547
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548#ifdef FEAT_PERL
4549 perl_win_free(wp);
4550#endif
4551
4552#ifdef FEAT_PYTHON
4553 python_window_free(wp);
4554#endif
4555
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004556#ifdef FEAT_PYTHON3
4557 python3_window_free(wp);
4558#endif
4559
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560#ifdef FEAT_TCL
4561 tcl_window_free(wp);
4562#endif
4563
4564#ifdef FEAT_RUBY
4565 ruby_window_free(wp);
4566#endif
4567
4568 clear_winopt(&wp->w_onebuf_opt);
4569 clear_winopt(&wp->w_allbuf_opt);
4570
4571#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004572 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4573 hash_init(&wp->w_vars->dv_hashtab);
4574 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575#endif
4576
4577 if (prevwin == wp)
4578 prevwin = NULL;
4579 win_free_lsize(wp);
4580
4581 for (i = 0; i < wp->w_tagstacklen; ++i)
4582 vim_free(wp->w_tagstack[i].tagname);
4583
4584 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004585
Bram Moolenaarff18df02013-07-24 17:51:57 +02004586 /* Remove the window from the b_wininfo lists, it may happen that the
4587 * freed memory is re-used for another window. */
4588 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4589 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4590 if (wip->wi_win == wp)
4591 wip->wi_win = NULL;
4592
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004594 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004596
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597#ifdef FEAT_JUMPLIST
4598 free_jumplist(wp);
4599#endif
4600
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004601#ifdef FEAT_QUICKFIX
4602 qf_free_all(wp);
4603#endif
4604
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605#ifdef FEAT_GUI
4606 if (gui.in_use)
4607 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4609 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4610 }
4611#endif /* FEAT_GUI */
4612
Bram Moolenaar860cae12010-06-05 23:22:07 +02004613#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004614 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004615#endif
4616
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004617#ifdef FEAT_AUTOCMD
4618 if (wp != aucmd_win)
4619#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004620 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004621#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004622 if (autocmd_busy)
4623 {
4624 wp->w_next = au_pending_free_win;
4625 au_pending_free_win = wp;
4626 }
4627 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004628#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004629 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004630
4631#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004632 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634}
4635
4636/*
4637 * Append window "wp" in the window list after window "after".
4638 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004639 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640win_append(after, wp)
4641 win_T *after, *wp;
4642{
4643 win_T *before;
4644
4645 if (after == NULL) /* after NULL is in front of the first */
4646 before = firstwin;
4647 else
4648 before = after->w_next;
4649
4650 wp->w_next = before;
4651 wp->w_prev = after;
4652 if (after == NULL)
4653 firstwin = wp;
4654 else
4655 after->w_next = wp;
4656 if (before == NULL)
4657 lastwin = wp;
4658 else
4659 before->w_prev = wp;
4660}
4661
4662/*
4663 * Remove a window from the window list.
4664 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004665 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004666win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004668 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669{
4670 if (wp->w_prev != NULL)
4671 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004672 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004674 else
4675 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 if (wp->w_next != NULL)
4677 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004678 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004680 else
4681 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682}
4683
4684/*
4685 * Append frame "frp" in a frame list after frame "after".
4686 */
4687 static void
4688frame_append(after, frp)
4689 frame_T *after, *frp;
4690{
4691 frp->fr_next = after->fr_next;
4692 after->fr_next = frp;
4693 if (frp->fr_next != NULL)
4694 frp->fr_next->fr_prev = frp;
4695 frp->fr_prev = after;
4696}
4697
4698/*
4699 * Insert frame "frp" in a frame list before frame "before".
4700 */
4701 static void
4702frame_insert(before, frp)
4703 frame_T *before, *frp;
4704{
4705 frp->fr_next = before;
4706 frp->fr_prev = before->fr_prev;
4707 before->fr_prev = frp;
4708 if (frp->fr_prev != NULL)
4709 frp->fr_prev->fr_next = frp;
4710 else
4711 frp->fr_parent->fr_child = frp;
4712}
4713
4714/*
4715 * Remove a frame from a frame list.
4716 */
4717 static void
4718frame_remove(frp)
4719 frame_T *frp;
4720{
4721 if (frp->fr_prev != NULL)
4722 frp->fr_prev->fr_next = frp->fr_next;
4723 else
4724 frp->fr_parent->fr_child = frp->fr_next;
4725 if (frp->fr_next != NULL)
4726 frp->fr_next->fr_prev = frp->fr_prev;
4727}
4728
4729#endif /* FEAT_WINDOWS */
4730
4731/*
4732 * Allocate w_lines[] for window "wp".
4733 * Return FAIL for failure, OK for success.
4734 */
4735 int
4736win_alloc_lines(wp)
4737 win_T *wp;
4738{
4739 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004740 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 if (wp->w_lines == NULL)
4742 return FAIL;
4743 return OK;
4744}
4745
4746/*
4747 * free lsize arrays for a window
4748 */
4749 void
4750win_free_lsize(wp)
4751 win_T *wp;
4752{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004753 /* TODO: why would wp be NULL here? */
4754 if (wp != NULL)
4755 {
4756 vim_free(wp->w_lines);
4757 wp->w_lines = NULL;
4758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759}
4760
4761/*
4762 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004763 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 */
4765 void
4766shell_new_rows()
4767{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004768 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769
4770 if (firstwin == NULL) /* not initialized yet */
4771 return;
4772#ifdef FEAT_WINDOWS
4773 if (h < frame_minheight(topframe, NULL))
4774 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004775
4776 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 * that doesn't result in the right height, forget about that option. */
4778 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004779 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 frame_new_height(topframe, h, FALSE, FALSE);
4781
4782 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4783#else
4784 if (h < 1)
4785 h = 1;
4786 win_new_height(firstwin, h);
4787#endif
4788 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004789#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004790 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004791#endif
4792
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793#if 0
4794 /* Disabled: don't want making the screen smaller make a window larger. */
4795 if (p_ea)
4796 win_equal(curwin, FALSE, 'v');
4797#endif
4798}
4799
4800#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4801/*
4802 * Called from win_new_shellsize() after Columns changed.
4803 */
4804 void
4805shell_new_columns()
4806{
4807 if (firstwin == NULL) /* not initialized yet */
4808 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004809
4810 /* First try setting the widths of windows with 'winfixwidth'. If that
4811 * doesn't result in the right width, forget about that option. */
4812 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004813 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004814 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4815
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4817#if 0
4818 /* Disabled: don't want making the screen smaller make a window larger. */
4819 if (p_ea)
4820 win_equal(curwin, FALSE, 'h');
4821#endif
4822}
4823#endif
4824
4825#if defined(FEAT_CMDWIN) || defined(PROTO)
4826/*
4827 * Save the size of all windows in "gap".
4828 */
4829 void
4830win_size_save(gap)
4831 garray_T *gap;
4832
4833{
4834 win_T *wp;
4835
4836 ga_init2(gap, (int)sizeof(int), 1);
4837 if (ga_grow(gap, win_count() * 2) == OK)
4838 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4839 {
4840 ((int *)gap->ga_data)[gap->ga_len++] =
4841 wp->w_width + wp->w_vsep_width;
4842 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4843 }
4844}
4845
4846/*
4847 * Restore window sizes, but only if the number of windows is still the same.
4848 * Does not free the growarray.
4849 */
4850 void
4851win_size_restore(gap)
4852 garray_T *gap;
4853{
4854 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004855 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856
4857 if (win_count() * 2 == gap->ga_len)
4858 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004859 /* The order matters, because frames contain other frames, but it's
4860 * difficult to get right. The easy way out is to do it twice. */
4861 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004863 i = 0;
4864 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4865 {
4866 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4867 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 }
4870 /* recompute the window positions */
4871 (void)win_comp_pos();
4872 }
4873}
4874#endif /* FEAT_CMDWIN */
4875
4876#if defined(FEAT_WINDOWS) || defined(PROTO)
4877/*
4878 * Update the position for all windows, using the width and height of the
4879 * frames.
4880 * Returns the row just after the last window.
4881 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004882 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883win_comp_pos()
4884{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004885 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 int col = 0;
4887
4888 frame_comp_pos(topframe, &row, &col);
4889 return row;
4890}
4891
4892/*
4893 * Update the position of the windows in frame "topfrp", using the width and
4894 * height of the frames.
4895 * "*row" and "*col" are the top-left position of the frame. They are updated
4896 * to the bottom-right position plus one.
4897 */
4898 static void
4899frame_comp_pos(topfrp, row, col)
4900 frame_T *topfrp;
4901 int *row;
4902 int *col;
4903{
4904 win_T *wp;
4905 frame_T *frp;
4906#ifdef FEAT_VERTSPLIT
4907 int startcol;
4908 int startrow;
4909#endif
4910
4911 wp = topfrp->fr_win;
4912 if (wp != NULL)
4913 {
4914 if (wp->w_winrow != *row
4915#ifdef FEAT_VERTSPLIT
4916 || wp->w_wincol != *col
4917#endif
4918 )
4919 {
4920 /* position changed, redraw */
4921 wp->w_winrow = *row;
4922#ifdef FEAT_VERTSPLIT
4923 wp->w_wincol = *col;
4924#endif
4925 redraw_win_later(wp, NOT_VALID);
4926 wp->w_redr_status = TRUE;
4927 }
4928 *row += wp->w_height + wp->w_status_height;
4929#ifdef FEAT_VERTSPLIT
4930 *col += wp->w_width + wp->w_vsep_width;
4931#endif
4932 }
4933 else
4934 {
4935#ifdef FEAT_VERTSPLIT
4936 startrow = *row;
4937 startcol = *col;
4938#endif
4939 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4940 {
4941#ifdef FEAT_VERTSPLIT
4942 if (topfrp->fr_layout == FR_ROW)
4943 *row = startrow; /* all frames are at the same row */
4944 else
4945 *col = startcol; /* all frames are at the same col */
4946#endif
4947 frame_comp_pos(frp, row, col);
4948 }
4949 }
4950}
4951
4952#endif /* FEAT_WINDOWS */
4953
4954/*
4955 * Set current window height and take care of repositioning other windows to
4956 * fit around it.
4957 */
4958 void
4959win_setheight(height)
4960 int height;
4961{
4962 win_setheight_win(height, curwin);
4963}
4964
4965/*
4966 * Set the window height of window "win" and take care of repositioning other
4967 * windows to fit around it.
4968 */
4969 void
4970win_setheight_win(height, win)
4971 int height;
4972 win_T *win;
4973{
4974 int row;
4975
4976 if (win == curwin)
4977 {
4978 /* Always keep current window at least one line high, even when
4979 * 'winminheight' is zero. */
4980#ifdef FEAT_WINDOWS
4981 if (height < p_wmh)
4982 height = p_wmh;
4983#endif
4984 if (height == 0)
4985 height = 1;
4986 }
4987
4988#ifdef FEAT_WINDOWS
4989 frame_setheight(win->w_frame, height + win->w_status_height);
4990
4991 /* recompute the window positions */
4992 row = win_comp_pos();
4993#else
4994 if (height > topframe->fr_height)
4995 height = topframe->fr_height;
4996 win->w_height = height;
4997 row = height;
4998#endif
4999
5000 /*
5001 * If there is extra space created between the last window and the command
5002 * line, clear it.
5003 */
5004 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5005 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5006 cmdline_row = row;
5007 msg_row = row;
5008 msg_col = 0;
5009
5010 redraw_all_later(NOT_VALID);
5011}
5012
5013#if defined(FEAT_WINDOWS) || defined(PROTO)
5014
5015/*
5016 * Set the height of a frame to "height" and take care that all frames and
5017 * windows inside it are resized. Also resize frames on the left and right if
5018 * the are in the same FR_ROW frame.
5019 *
5020 * Strategy:
5021 * If the frame is part of a FR_COL frame, try fitting the frame in that
5022 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5023 * go to containing frames to resize them and make room.
5024 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5025 * Check for the minimal height of the FR_ROW frame.
5026 * At the top level we can also use change the command line height.
5027 */
5028 static void
5029frame_setheight(curfrp, height)
5030 frame_T *curfrp;
5031 int height;
5032{
5033 int room; /* total number of lines available */
5034 int take; /* number of lines taken from other windows */
5035 int room_cmdline; /* lines available from cmdline */
5036 int run;
5037 frame_T *frp;
5038 int h;
5039 int room_reserved;
5040
5041 /* If the height already is the desired value, nothing to do. */
5042 if (curfrp->fr_height == height)
5043 return;
5044
5045 if (curfrp->fr_parent == NULL)
5046 {
5047 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005048 if (height > ROWS_AVAIL)
5049 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 if (height > 0)
5051 frame_new_height(curfrp, height, FALSE, FALSE);
5052 }
5053 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5054 {
5055 /* Row of frames: Also need to resize frames left and right of this
5056 * one. First check for the minimal height of these. */
5057 h = frame_minheight(curfrp->fr_parent, NULL);
5058 if (height < h)
5059 height = h;
5060 frame_setheight(curfrp->fr_parent, height);
5061 }
5062 else
5063 {
5064 /*
5065 * Column of frames: try to change only frames in this column.
5066 */
5067#ifdef FEAT_VERTSPLIT
5068 /*
5069 * Do this twice:
5070 * 1: compute room available, if it's not enough try resizing the
5071 * containing frame.
5072 * 2: compute the room available and adjust the height to it.
5073 * Try not to reduce the height of a window with 'winfixheight' set.
5074 */
5075 for (run = 1; run <= 2; ++run)
5076#else
5077 for (;;)
5078#endif
5079 {
5080 room = 0;
5081 room_reserved = 0;
5082 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5083 frp = frp->fr_next)
5084 {
5085 if (frp != curfrp
5086 && frp->fr_win != NULL
5087 && frp->fr_win->w_p_wfh)
5088 room_reserved += frp->fr_height;
5089 room += frp->fr_height;
5090 if (frp != curfrp)
5091 room -= frame_minheight(frp, NULL);
5092 }
5093#ifdef FEAT_VERTSPLIT
5094 if (curfrp->fr_width != Columns)
5095 room_cmdline = 0;
5096 else
5097#endif
5098 {
5099 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5100 + lastwin->w_height + lastwin->w_status_height);
5101 if (room_cmdline < 0)
5102 room_cmdline = 0;
5103 }
5104
5105 if (height <= room + room_cmdline)
5106 break;
5107#ifdef FEAT_VERTSPLIT
5108 if (run == 2 || curfrp->fr_width == Columns)
5109#endif
5110 {
5111 if (height > room + room_cmdline)
5112 height = room + room_cmdline;
5113 break;
5114 }
5115#ifdef FEAT_VERTSPLIT
5116 frame_setheight(curfrp->fr_parent, height
5117 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5118#endif
5119 /*NOTREACHED*/
5120 }
5121
5122 /*
5123 * Compute the number of lines we will take from others frames (can be
5124 * negative!).
5125 */
5126 take = height - curfrp->fr_height;
5127
5128 /* If there is not enough room, also reduce the height of a window
5129 * with 'winfixheight' set. */
5130 if (height > room + room_cmdline - room_reserved)
5131 room_reserved = room + room_cmdline - height;
5132 /* If there is only a 'winfixheight' window and making the
5133 * window smaller, need to make the other window taller. */
5134 if (take < 0 && room - curfrp->fr_height < room_reserved)
5135 room_reserved = 0;
5136
5137 if (take > 0 && room_cmdline > 0)
5138 {
5139 /* use lines from cmdline first */
5140 if (take < room_cmdline)
5141 room_cmdline = take;
5142 take -= room_cmdline;
5143 topframe->fr_height += room_cmdline;
5144 }
5145
5146 /*
5147 * set the current frame to the new height
5148 */
5149 frame_new_height(curfrp, height, FALSE, FALSE);
5150
5151 /*
5152 * First take lines from the frames after the current frame. If
5153 * that is not enough, takes lines from frames above the current
5154 * frame.
5155 */
5156 for (run = 0; run < 2; ++run)
5157 {
5158 if (run == 0)
5159 frp = curfrp->fr_next; /* 1st run: start with next window */
5160 else
5161 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5162 while (frp != NULL && take != 0)
5163 {
5164 h = frame_minheight(frp, NULL);
5165 if (room_reserved > 0
5166 && frp->fr_win != NULL
5167 && frp->fr_win->w_p_wfh)
5168 {
5169 if (room_reserved >= frp->fr_height)
5170 room_reserved -= frp->fr_height;
5171 else
5172 {
5173 if (frp->fr_height - room_reserved > take)
5174 room_reserved = frp->fr_height - take;
5175 take -= frp->fr_height - room_reserved;
5176 frame_new_height(frp, room_reserved, FALSE, FALSE);
5177 room_reserved = 0;
5178 }
5179 }
5180 else
5181 {
5182 if (frp->fr_height - take < h)
5183 {
5184 take -= frp->fr_height - h;
5185 frame_new_height(frp, h, FALSE, FALSE);
5186 }
5187 else
5188 {
5189 frame_new_height(frp, frp->fr_height - take,
5190 FALSE, FALSE);
5191 take = 0;
5192 }
5193 }
5194 if (run == 0)
5195 frp = frp->fr_next;
5196 else
5197 frp = frp->fr_prev;
5198 }
5199 }
5200 }
5201}
5202
5203#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5204/*
5205 * Set current window width and take care of repositioning other windows to
5206 * fit around it.
5207 */
5208 void
5209win_setwidth(width)
5210 int width;
5211{
5212 win_setwidth_win(width, curwin);
5213}
5214
5215 void
5216win_setwidth_win(width, wp)
5217 int width;
5218 win_T *wp;
5219{
5220 /* Always keep current window at least one column wide, even when
5221 * 'winminwidth' is zero. */
5222 if (wp == curwin)
5223 {
5224 if (width < p_wmw)
5225 width = p_wmw;
5226 if (width == 0)
5227 width = 1;
5228 }
5229
5230 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5231
5232 /* recompute the window positions */
5233 (void)win_comp_pos();
5234
5235 redraw_all_later(NOT_VALID);
5236}
5237
5238/*
5239 * Set the width of a frame to "width" and take care that all frames and
5240 * windows inside it are resized. Also resize frames above and below if the
5241 * are in the same FR_ROW frame.
5242 *
5243 * Strategy is similar to frame_setheight().
5244 */
5245 static void
5246frame_setwidth(curfrp, width)
5247 frame_T *curfrp;
5248 int width;
5249{
5250 int room; /* total number of lines available */
5251 int take; /* number of lines taken from other windows */
5252 int run;
5253 frame_T *frp;
5254 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005255 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256
5257 /* If the width already is the desired value, nothing to do. */
5258 if (curfrp->fr_width == width)
5259 return;
5260
5261 if (curfrp->fr_parent == NULL)
5262 /* topframe: can't change width */
5263 return;
5264
5265 if (curfrp->fr_parent->fr_layout == FR_COL)
5266 {
5267 /* Column of frames: Also need to resize frames above and below of
5268 * this one. First check for the minimal width of these. */
5269 w = frame_minwidth(curfrp->fr_parent, NULL);
5270 if (width < w)
5271 width = w;
5272 frame_setwidth(curfrp->fr_parent, width);
5273 }
5274 else
5275 {
5276 /*
5277 * Row of frames: try to change only frames in this row.
5278 *
5279 * Do this twice:
5280 * 1: compute room available, if it's not enough try resizing the
5281 * containing frame.
5282 * 2: compute the room available and adjust the width to it.
5283 */
5284 for (run = 1; run <= 2; ++run)
5285 {
5286 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005287 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5289 frp = frp->fr_next)
5290 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005291 if (frp != curfrp
5292 && frp->fr_win != NULL
5293 && frp->fr_win->w_p_wfw)
5294 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 room += frp->fr_width;
5296 if (frp != curfrp)
5297 room -= frame_minwidth(frp, NULL);
5298 }
5299
5300 if (width <= room)
5301 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005302 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 {
5304 if (width > room)
5305 width = room;
5306 break;
5307 }
5308 frame_setwidth(curfrp->fr_parent, width
5309 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5310 }
5311
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 /*
5313 * Compute the number of lines we will take from others frames (can be
5314 * negative!).
5315 */
5316 take = width - curfrp->fr_width;
5317
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005318 /* If there is not enough room, also reduce the width of a window
5319 * with 'winfixwidth' set. */
5320 if (width > room - room_reserved)
5321 room_reserved = room - width;
5322 /* If there is only a 'winfixwidth' window and making the
5323 * window smaller, need to make the other window narrower. */
5324 if (take < 0 && room - curfrp->fr_width < room_reserved)
5325 room_reserved = 0;
5326
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 /*
5328 * set the current frame to the new width
5329 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005330 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331
5332 /*
5333 * First take lines from the frames right of the current frame. If
5334 * that is not enough, takes lines from frames left of the current
5335 * frame.
5336 */
5337 for (run = 0; run < 2; ++run)
5338 {
5339 if (run == 0)
5340 frp = curfrp->fr_next; /* 1st run: start with next window */
5341 else
5342 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5343 while (frp != NULL && take != 0)
5344 {
5345 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005346 if (room_reserved > 0
5347 && frp->fr_win != NULL
5348 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005350 if (room_reserved >= frp->fr_width)
5351 room_reserved -= frp->fr_width;
5352 else
5353 {
5354 if (frp->fr_width - room_reserved > take)
5355 room_reserved = frp->fr_width - take;
5356 take -= frp->fr_width - room_reserved;
5357 frame_new_width(frp, room_reserved, FALSE, FALSE);
5358 room_reserved = 0;
5359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
5361 else
5362 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005363 if (frp->fr_width - take < w)
5364 {
5365 take -= frp->fr_width - w;
5366 frame_new_width(frp, w, FALSE, FALSE);
5367 }
5368 else
5369 {
5370 frame_new_width(frp, frp->fr_width - take,
5371 FALSE, FALSE);
5372 take = 0;
5373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
5375 if (run == 0)
5376 frp = frp->fr_next;
5377 else
5378 frp = frp->fr_prev;
5379 }
5380 }
5381 }
5382}
5383#endif /* FEAT_VERTSPLIT */
5384
5385/*
5386 * Check 'winminheight' for a valid value.
5387 */
5388 void
5389win_setminheight()
5390{
5391 int room;
5392 int first = TRUE;
5393 win_T *wp;
5394
5395 /* loop until there is a 'winminheight' that is possible */
5396 while (p_wmh > 0)
5397 {
5398 /* TODO: handle vertical splits */
5399 room = -p_wh;
5400 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5401 room += wp->w_height - p_wmh;
5402 if (room >= 0)
5403 break;
5404 --p_wmh;
5405 if (first)
5406 {
5407 EMSG(_(e_noroom));
5408 first = FALSE;
5409 }
5410 }
5411}
5412
5413#ifdef FEAT_MOUSE
5414
5415/*
5416 * Status line of dragwin is dragged "offset" lines down (negative is up).
5417 */
5418 void
5419win_drag_status_line(dragwin, offset)
5420 win_T *dragwin;
5421 int offset;
5422{
5423 frame_T *curfr;
5424 frame_T *fr;
5425 int room;
5426 int row;
5427 int up; /* if TRUE, drag status line up, otherwise down */
5428 int n;
5429
5430 fr = dragwin->w_frame;
5431 curfr = fr;
5432 if (fr != topframe) /* more than one window */
5433 {
5434 fr = fr->fr_parent;
5435 /* When the parent frame is not a column of frames, its parent should
5436 * be. */
5437 if (fr->fr_layout != FR_COL)
5438 {
5439 curfr = fr;
5440 if (fr != topframe) /* only a row of windows, may drag statusline */
5441 fr = fr->fr_parent;
5442 }
5443 }
5444
5445 /* If this is the last frame in a column, may want to resize the parent
5446 * frame instead (go two up to skip a row of frames). */
5447 while (curfr != topframe && curfr->fr_next == NULL)
5448 {
5449 if (fr != topframe)
5450 fr = fr->fr_parent;
5451 curfr = fr;
5452 if (fr != topframe)
5453 fr = fr->fr_parent;
5454 }
5455
5456 if (offset < 0) /* drag up */
5457 {
5458 up = TRUE;
5459 offset = -offset;
5460 /* sum up the room of the current frame and above it */
5461 if (fr == curfr)
5462 {
5463 /* only one window */
5464 room = fr->fr_height - frame_minheight(fr, NULL);
5465 }
5466 else
5467 {
5468 room = 0;
5469 for (fr = fr->fr_child; ; fr = fr->fr_next)
5470 {
5471 room += fr->fr_height - frame_minheight(fr, NULL);
5472 if (fr == curfr)
5473 break;
5474 }
5475 }
5476 fr = curfr->fr_next; /* put fr at frame that grows */
5477 }
5478 else /* drag down */
5479 {
5480 up = FALSE;
5481 /*
5482 * Only dragging the last status line can reduce p_ch.
5483 */
5484 room = Rows - cmdline_row;
5485 if (curfr->fr_next == NULL)
5486 room -= 1;
5487 else
5488 room -= p_ch;
5489 if (room < 0)
5490 room = 0;
5491 /* sum up the room of frames below of the current one */
5492 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5493 room += fr->fr_height - frame_minheight(fr, NULL);
5494 fr = curfr; /* put fr at window that grows */
5495 }
5496
5497 if (room < offset) /* Not enough room */
5498 offset = room; /* Move as far as we can */
5499 if (offset <= 0)
5500 return;
5501
5502 /*
5503 * Grow frame fr by "offset" lines.
5504 * Doesn't happen when dragging the last status line up.
5505 */
5506 if (fr != NULL)
5507 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5508
5509 if (up)
5510 fr = curfr; /* current frame gets smaller */
5511 else
5512 fr = curfr->fr_next; /* next frame gets smaller */
5513
5514 /*
5515 * Now make the other frames smaller.
5516 */
5517 while (fr != NULL && offset > 0)
5518 {
5519 n = frame_minheight(fr, NULL);
5520 if (fr->fr_height - offset <= n)
5521 {
5522 offset -= fr->fr_height - n;
5523 frame_new_height(fr, n, !up, FALSE);
5524 }
5525 else
5526 {
5527 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5528 break;
5529 }
5530 if (up)
5531 fr = fr->fr_prev;
5532 else
5533 fr = fr->fr_next;
5534 }
5535 row = win_comp_pos();
5536 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5537 cmdline_row = row;
5538 p_ch = Rows - cmdline_row;
5539 if (p_ch < 1)
5540 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005541 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005542 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 showmode();
5544}
5545
5546#ifdef FEAT_VERTSPLIT
5547/*
5548 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5549 */
5550 void
5551win_drag_vsep_line(dragwin, offset)
5552 win_T *dragwin;
5553 int offset;
5554{
5555 frame_T *curfr;
5556 frame_T *fr;
5557 int room;
5558 int left; /* if TRUE, drag separator line left, otherwise right */
5559 int n;
5560
5561 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005562 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 return;
5564 curfr = fr;
5565 fr = fr->fr_parent;
5566 /* When the parent frame is not a row of frames, its parent should be. */
5567 if (fr->fr_layout != FR_ROW)
5568 {
5569 if (fr == topframe) /* only a column of windows (cannot happen?) */
5570 return;
5571 curfr = fr;
5572 fr = fr->fr_parent;
5573 }
5574
5575 /* If this is the last frame in a row, may want to resize a parent
5576 * frame instead. */
5577 while (curfr->fr_next == NULL)
5578 {
5579 if (fr == topframe)
5580 break;
5581 curfr = fr;
5582 fr = fr->fr_parent;
5583 if (fr != topframe)
5584 {
5585 curfr = fr;
5586 fr = fr->fr_parent;
5587 }
5588 }
5589
5590 if (offset < 0) /* drag left */
5591 {
5592 left = TRUE;
5593 offset = -offset;
5594 /* sum up the room of the current frame and left of it */
5595 room = 0;
5596 for (fr = fr->fr_child; ; fr = fr->fr_next)
5597 {
5598 room += fr->fr_width - frame_minwidth(fr, NULL);
5599 if (fr == curfr)
5600 break;
5601 }
5602 fr = curfr->fr_next; /* put fr at frame that grows */
5603 }
5604 else /* drag right */
5605 {
5606 left = FALSE;
5607 /* sum up the room of frames right of the current one */
5608 room = 0;
5609 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5610 room += fr->fr_width - frame_minwidth(fr, NULL);
5611 fr = curfr; /* put fr at window that grows */
5612 }
5613
5614 if (room < offset) /* Not enough room */
5615 offset = room; /* Move as far as we can */
5616 if (offset <= 0) /* No room at all, quit. */
5617 return;
5618
5619 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005620 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
5622 /* shrink other frames: current and at the left or at the right */
5623 if (left)
5624 fr = curfr; /* current frame gets smaller */
5625 else
5626 fr = curfr->fr_next; /* next frame gets smaller */
5627
5628 while (fr != NULL && offset > 0)
5629 {
5630 n = frame_minwidth(fr, NULL);
5631 if (fr->fr_width - offset <= n)
5632 {
5633 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005634 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 }
5636 else
5637 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005638 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 break;
5640 }
5641 if (left)
5642 fr = fr->fr_prev;
5643 else
5644 fr = fr->fr_next;
5645 }
5646 (void)win_comp_pos();
5647 redraw_all_later(NOT_VALID);
5648}
5649#endif /* FEAT_VERTSPLIT */
5650#endif /* FEAT_MOUSE */
5651
5652#endif /* FEAT_WINDOWS */
5653
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005654#define FRACTION_MULT 16384L
5655
5656/*
5657 * Set wp->w_fraction for the current w_wrow and w_height.
5658 */
5659 static void
5660set_fraction(wp)
5661 win_T *wp;
5662{
5663 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005664 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005665}
5666
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667/*
5668 * Set the height of a window.
5669 * This takes care of the things inside the window, not what happens to the
5670 * window position, the frame or to other windows.
5671 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005672 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673win_new_height(wp, height)
5674 win_T *wp;
5675 int height;
5676{
5677 linenr_T lnum;
5678 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005679 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680
5681 /* Don't want a negative height. Happens when splitting a tiny window.
5682 * Will equalize heights soon to fix it. */
5683 if (height < 0)
5684 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005685 if (wp->w_height == height)
5686 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005688 if (wp->w_height > 0)
5689 {
5690 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005691 /* w_wrow needs to be valid. When setting 'laststatus' this may
5692 * call win_new_height() recursively. */
5693 validate_cursor();
5694 if (wp->w_height != prev_height)
5695 return; /* Recursive call already changed the size, bail out here
5696 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005697 if (wp->w_wrow != wp->w_prev_fraction_row)
5698 set_fraction(wp);
5699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700
5701 wp->w_height = height;
5702 wp->w_skipcol = 0;
5703
5704 /* Don't change w_topline when height is zero. Don't set w_topline when
5705 * 'scrollbind' is set and this isn't the current window. */
5706 if (height > 0
5707#ifdef FEAT_SCROLLBIND
5708 && (!wp->w_p_scb || wp == curwin)
5709#endif
5710 )
5711 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005712 /*
5713 * Find a value for w_topline that shows the cursor at the same
5714 * relative position in the window as before (more or less).
5715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 lnum = wp->w_cursor.lnum;
5717 if (lnum < 1) /* can happen when starting up */
5718 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005719 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5720 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5722 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005723
5724 if (sline >= 0)
5725 {
5726 /* Make sure the whole cursor line is visible, if possible. */
5727 int rows = plines_win(wp, lnum, FALSE);
5728
5729 if (sline > wp->w_height - rows)
5730 {
5731 sline = wp->w_height - rows;
5732 wp->w_wrow -= rows - line_size;
5733 }
5734 }
5735
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 if (sline < 0)
5737 {
5738 /*
5739 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005740 * Make cursor line the first line in the window. If not enough
5741 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 */
5743 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005744 if (wp->w_wrow >= wp->w_height
5745 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5746 {
5747 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5748 --wp->w_wrow;
5749 while (wp->w_wrow >= wp->w_height)
5750 {
5751 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5752 + win_col_off2(wp);
5753 --wp->w_wrow;
5754 }
5755 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005756 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005758 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005760 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 {
5762#ifdef FEAT_FOLDING
5763 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5764 if (lnum == 1)
5765 {
5766 /* first line in buffer is folded */
5767 line_size = 1;
5768 --sline;
5769 break;
5770 }
5771#endif
5772 --lnum;
5773#ifdef FEAT_DIFF
5774 if (lnum == wp->w_topline)
5775 line_size = plines_win_nofill(wp, lnum, TRUE)
5776 + wp->w_topfill;
5777 else
5778#endif
5779 line_size = plines_win(wp, lnum, TRUE);
5780 sline -= line_size;
5781 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005782
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 if (sline < 0)
5784 {
5785 /*
5786 * Line we want at top would go off top of screen. Use next
5787 * line instead.
5788 */
5789#ifdef FEAT_FOLDING
5790 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5791#endif
5792 lnum++;
5793 wp->w_wrow -= line_size + sline;
5794 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005795 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 {
5797 /* First line of file reached, use that as topline. */
5798 lnum = 1;
5799 wp->w_wrow -= sline;
5800 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005801
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005802 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 }
5805
5806 if (wp == curwin)
5807 {
5808 if (p_so)
5809 update_topline();
5810 curs_columns(FALSE); /* validate w_wrow */
5811 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005812 if (prev_height > 0)
5813 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814
5815 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005816 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817#ifdef FEAT_WINDOWS
5818 wp->w_redr_status = TRUE;
5819#endif
5820 invalidate_botline_win(wp);
5821}
5822
5823#ifdef FEAT_VERTSPLIT
5824/*
5825 * Set the width of a window.
5826 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005827 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828win_new_width(wp, width)
5829 win_T *wp;
5830 int width;
5831{
5832 wp->w_width = width;
5833 wp->w_lines_valid = 0;
5834 changed_line_abv_curs_win(wp);
5835 invalidate_botline_win(wp);
5836 if (wp == curwin)
5837 {
5838 update_topline();
5839 curs_columns(TRUE); /* validate w_wrow */
5840 }
5841 redraw_win_later(wp, NOT_VALID);
5842 wp->w_redr_status = TRUE;
5843}
5844#endif
5845
5846 void
5847win_comp_scroll(wp)
5848 win_T *wp;
5849{
5850 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5851 if (wp->w_p_scr == 0)
5852 wp->w_p_scr = 1;
5853}
5854
5855/*
5856 * command_height: called whenever p_ch has been changed
5857 */
5858 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005859command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860{
5861#ifdef FEAT_WINDOWS
5862 int h;
5863 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005864 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005866 /* Use the value of p_ch that we remembered. This is needed for when the
5867 * GUI starts up, we can't be sure in what order things happen. And when
5868 * p_ch was changed in another tab page. */
5869 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005870
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 /* Find bottom frame with width of screen. */
5872 frp = lastwin->w_frame;
5873# ifdef FEAT_VERTSPLIT
5874 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5875 frp = frp->fr_parent;
5876# endif
5877
5878 /* Avoid changing the height of a window with 'winfixheight' set. */
5879 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5880 && frp->fr_win->w_p_wfh)
5881 frp = frp->fr_prev;
5882
5883 if (starting != NO_SCREEN)
5884 {
5885 cmdline_row = Rows - p_ch;
5886
5887 if (p_ch > old_p_ch) /* p_ch got bigger */
5888 {
5889 while (p_ch > old_p_ch)
5890 {
5891 if (frp == NULL)
5892 {
5893 EMSG(_(e_noroom));
5894 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005895 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 cmdline_row = Rows - p_ch;
5897 break;
5898 }
5899 h = frp->fr_height - frame_minheight(frp, NULL);
5900 if (h > p_ch - old_p_ch)
5901 h = p_ch - old_p_ch;
5902 old_p_ch += h;
5903 frame_add_height(frp, -h);
5904 frp = frp->fr_prev;
5905 }
5906
5907 /* Recompute window positions. */
5908 (void)win_comp_pos();
5909
5910 /* clear the lines added to cmdline */
5911 if (full_screen)
5912 screen_fill((int)(cmdline_row), (int)Rows, 0,
5913 (int)Columns, ' ', ' ', 0);
5914 msg_row = cmdline_row;
5915 redraw_cmdline = TRUE;
5916 return;
5917 }
5918
5919 if (msg_row < cmdline_row)
5920 msg_row = cmdline_row;
5921 redraw_cmdline = TRUE;
5922 }
5923 frame_add_height(frp, (int)(old_p_ch - p_ch));
5924
5925 /* Recompute window positions. */
5926 if (frp != lastwin->w_frame)
5927 (void)win_comp_pos();
5928#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005930 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931#endif
5932}
5933
5934#if defined(FEAT_WINDOWS) || defined(PROTO)
5935/*
5936 * Resize frame "frp" to be "n" lines higher (negative for less high).
5937 * Also resize the frames it is contained in.
5938 */
5939 static void
5940frame_add_height(frp, n)
5941 frame_T *frp;
5942 int n;
5943{
5944 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5945 for (;;)
5946 {
5947 frp = frp->fr_parent;
5948 if (frp == NULL)
5949 break;
5950 frp->fr_height += n;
5951 }
5952}
5953
5954/*
5955 * Add or remove a status line for the bottom window(s), according to the
5956 * value of 'laststatus'.
5957 */
5958 void
5959last_status(morewin)
5960 int morewin; /* pretend there are two or more windows */
5961{
5962 /* Don't make a difference between horizontal or vertical split. */
5963 last_status_rec(topframe, (p_ls == 2
5964 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5965}
5966
5967 static void
5968last_status_rec(fr, statusline)
5969 frame_T *fr;
5970 int statusline;
5971{
5972 frame_T *fp;
5973 win_T *wp;
5974
5975 if (fr->fr_layout == FR_LEAF)
5976 {
5977 wp = fr->fr_win;
5978 if (wp->w_status_height != 0 && !statusline)
5979 {
5980 /* remove status line */
5981 win_new_height(wp, wp->w_height + 1);
5982 wp->w_status_height = 0;
5983 comp_col();
5984 }
5985 else if (wp->w_status_height == 0 && statusline)
5986 {
5987 /* Find a frame to take a line from. */
5988 fp = fr;
5989 while (fp->fr_height <= frame_minheight(fp, NULL))
5990 {
5991 if (fp == topframe)
5992 {
5993 EMSG(_(e_noroom));
5994 return;
5995 }
5996 /* In a column of frames: go to frame above. If already at
5997 * the top or in a row of frames: go to parent. */
5998 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5999 fp = fp->fr_prev;
6000 else
6001 fp = fp->fr_parent;
6002 }
6003 wp->w_status_height = 1;
6004 if (fp != fr)
6005 {
6006 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6007 frame_fix_height(wp);
6008 (void)win_comp_pos();
6009 }
6010 else
6011 win_new_height(wp, wp->w_height - 1);
6012 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006013 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 }
6015 }
6016#ifdef FEAT_VERTSPLIT
6017 else if (fr->fr_layout == FR_ROW)
6018 {
6019 /* vertically split windows, set status line for each one */
6020 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6021 last_status_rec(fp, statusline);
6022 }
6023#endif
6024 else
6025 {
6026 /* horizontally split window, set status line for last one */
6027 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6028 ;
6029 last_status_rec(fp, statusline);
6030 }
6031}
6032
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006033/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006034 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006035 */
6036 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006037tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006038{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006039#ifdef FEAT_GUI_TABLINE
6040 /* When the GUI has the tabline then this always returns zero. */
6041 if (gui_use_tabline())
6042 return 0;
6043#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006044 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006045 {
6046 case 0: return 0;
6047 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6048 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006049 return 1;
6050}
6051
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052#endif /* FEAT_WINDOWS */
6053
6054#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6055/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006056 * Get the file name at the cursor.
6057 * If Visual mode is active, use the selected text if it's in one line.
6058 * Returns the name in allocated memory, NULL for failure.
6059 */
6060 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006061grab_file_name(count, file_lnum)
6062 long count;
6063 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006064{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006065 if (VIsual_active)
6066 {
6067 int len;
6068 char_u *ptr;
6069
6070 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6071 return NULL;
6072 return find_file_name_in_path(ptr, len,
6073 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
6074 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006075 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
6076 file_lnum);
6077
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006078}
6079
6080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 * Return the file name under or after the cursor.
6082 *
6083 * The 'path' option is searched if the file name is not absolute.
6084 * The string returned has been alloc'ed and should be freed by the caller.
6085 * NULL is returned if the file name or file is not found.
6086 *
6087 * options:
6088 * FNAME_MESS give error messages
6089 * FNAME_EXP expand to path
6090 * FNAME_HYP check for hypertext link
6091 * FNAME_INCL apply "includeexpr"
6092 */
6093 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006094file_name_at_cursor(options, count, file_lnum)
6095 int options;
6096 long count;
6097 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098{
6099 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006100 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6101 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102}
6103
6104/*
6105 * Return the name of the file under or after ptr[col].
6106 * Otherwise like file_name_at_cursor().
6107 */
6108 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006109file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 char_u *line;
6111 int col;
6112 int options;
6113 long count;
6114 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006115 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116{
6117 char_u *ptr;
6118 int len;
6119
6120 /*
6121 * search forward for what could be the start of a file name
6122 */
6123 ptr = line + col;
6124 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006125 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 if (*ptr == NUL) /* nothing found */
6127 {
6128 if (options & FNAME_MESS)
6129 EMSG(_("E446: No file name under cursor"));
6130 return NULL;
6131 }
6132
6133 /*
6134 * Search backward for first char of the file name.
6135 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6136 */
6137 while (ptr > line)
6138 {
6139#ifdef FEAT_MBYTE
6140 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6141 ptr -= len + 1;
6142 else
6143#endif
6144 if (vim_isfilec(ptr[-1])
6145 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6146 --ptr;
6147 else
6148 break;
6149 }
6150
6151 /*
6152 * Search forward for the last char of the file name.
6153 * Also allow "://" when ':' is not in 'isfname'.
6154 */
6155 len = 0;
6156 while (vim_isfilec(ptr[len])
6157 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6158#ifdef FEAT_MBYTE
6159 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006160 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 else
6162#endif
6163 ++len;
6164
6165 /*
6166 * If there is trailing punctuation, remove it.
6167 * But don't remove "..", could be a directory name.
6168 */
6169 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6170 && ptr[len - 2] != '.')
6171 --len;
6172
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006173 if (file_lnum != NULL)
6174 {
6175 char_u *p;
6176
6177 /* Get the number after the file name and a separator character */
6178 p = ptr + len;
6179 p = skipwhite(p);
6180 if (*p != NUL)
6181 {
6182 if (!isdigit(*p))
6183 ++p; /* skip the separator */
6184 p = skipwhite(p);
6185 if (isdigit(*p))
6186 *file_lnum = (int)getdigits(&p);
6187 }
6188 }
6189
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6191}
6192
6193# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6194static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6195
6196 static char_u *
6197eval_includeexpr(ptr, len)
6198 char_u *ptr;
6199 int len;
6200{
6201 char_u *res;
6202
6203 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006204 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006205 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 set_vim_var_string(VV_FNAME, NULL, 0);
6207 return res;
6208}
6209#endif
6210
6211/*
6212 * Return the name of the file ptr[len] in 'path'.
6213 * Otherwise like file_name_at_cursor().
6214 */
6215 char_u *
6216find_file_name_in_path(ptr, len, options, count, rel_fname)
6217 char_u *ptr;
6218 int len;
6219 int options;
6220 long count;
6221 char_u *rel_fname; /* file we are searching relative to */
6222{
6223 char_u *file_name;
6224 int c;
6225# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6226 char_u *tofree = NULL;
6227
6228 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6229 {
6230 tofree = eval_includeexpr(ptr, len);
6231 if (tofree != NULL)
6232 {
6233 ptr = tofree;
6234 len = (int)STRLEN(ptr);
6235 }
6236 }
6237# endif
6238
6239 if (options & FNAME_EXP)
6240 {
6241 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6242 TRUE, rel_fname);
6243
6244# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6245 /*
6246 * If the file could not be found in a normal way, try applying
6247 * 'includeexpr' (unless done already).
6248 */
6249 if (file_name == NULL
6250 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6251 {
6252 tofree = eval_includeexpr(ptr, len);
6253 if (tofree != NULL)
6254 {
6255 ptr = tofree;
6256 len = (int)STRLEN(ptr);
6257 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6258 TRUE, rel_fname);
6259 }
6260 }
6261# endif
6262 if (file_name == NULL && (options & FNAME_MESS))
6263 {
6264 c = ptr[len];
6265 ptr[len] = NUL;
6266 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6267 ptr[len] = c;
6268 }
6269
6270 /* Repeat finding the file "count" times. This matters when it
6271 * appears several times in the path. */
6272 while (file_name != NULL && --count > 0)
6273 {
6274 vim_free(file_name);
6275 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6276 }
6277 }
6278 else
6279 file_name = vim_strnsave(ptr, len);
6280
6281# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6282 vim_free(tofree);
6283# endif
6284
6285 return file_name;
6286}
6287#endif /* FEAT_SEARCHPATH */
6288
6289/*
6290 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6291 * Also check for ":\\", which MS Internet Explorer accepts, return
6292 * URL_BACKSLASH.
6293 */
6294 static int
6295path_is_url(p)
6296 char_u *p;
6297{
6298 if (STRNCMP(p, "://", (size_t)3) == 0)
6299 return URL_SLASH;
6300 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6301 return URL_BACKSLASH;
6302 return 0;
6303}
6304
6305/*
6306 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6307 * Return URL_BACKSLASH for "name:\\".
6308 * Return zero otherwise.
6309 */
6310 int
6311path_with_url(fname)
6312 char_u *fname;
6313{
6314 char_u *p;
6315
6316 for (p = fname; isalpha(*p); ++p)
6317 ;
6318 return path_is_url(p);
6319}
6320
6321/*
6322 * Return TRUE if "name" is a full (absolute) path name or URL.
6323 */
6324 int
6325vim_isAbsName(name)
6326 char_u *name;
6327{
6328 return (path_with_url(name) != 0 || mch_isFullName(name));
6329}
6330
6331/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006332 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 *
6334 * return FAIL for failure, OK otherwise
6335 */
6336 int
6337vim_FullName(fname, buf, len, force)
6338 char_u *fname, *buf;
6339 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006340 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341{
6342 int retval = OK;
6343 int url;
6344
6345 *buf = NUL;
6346 if (fname == NULL)
6347 return FAIL;
6348
6349 url = path_with_url(fname);
6350 if (!url)
6351 retval = mch_FullName(fname, buf, len, force);
6352 if (url || retval == FAIL)
6353 {
6354 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006355 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 }
6357#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6358 slash_adjust(buf);
6359#endif
6360 return retval;
6361}
6362
6363/*
6364 * Return the minimal number of rows that is needed on the screen to display
6365 * the current number of windows.
6366 */
6367 int
6368min_rows()
6369{
6370 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006371#ifdef FEAT_WINDOWS
6372 tabpage_T *tp;
6373 int n;
6374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
6376 if (firstwin == NULL) /* not initialized yet */
6377 return MIN_LINES;
6378
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006380 total = 0;
6381 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6382 {
6383 n = frame_minheight(tp->tp_topframe, NULL);
6384 if (total < n)
6385 total = n;
6386 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006387 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006389 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006391 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 return total;
6393}
6394
6395/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006396 * Return TRUE if there is only one window (in the current tab page), not
6397 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006398 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 */
6400 int
6401only_one_window()
6402{
6403#ifdef FEAT_WINDOWS
6404 int count = 0;
6405 win_T *wp;
6406
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006407 /* If there is another tab page there always is another window. */
6408 if (first_tabpage->tp_next != NULL)
6409 return FALSE;
6410
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006412 if (wp->w_buffer != NULL
6413 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414# ifdef FEAT_QUICKFIX
6415 || wp->w_p_pvw
6416# endif
6417 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006418# ifdef FEAT_AUTOCMD
6419 && wp != aucmd_win
6420# endif
6421 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 ++count;
6423 return (count <= 1);
6424#else
6425 return TRUE;
6426#endif
6427}
6428
6429#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6430/*
6431 * Correct the cursor line number in other windows. Used after changing the
6432 * current buffer, and before applying autocommands.
6433 * When "do_curwin" is TRUE, also check current window.
6434 */
6435 void
6436check_lnums(do_curwin)
6437 int do_curwin;
6438{
6439 win_T *wp;
6440
6441#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006442 tabpage_T *tp;
6443
6444 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6446#else
6447 wp = curwin;
6448 if (do_curwin)
6449#endif
6450 {
6451 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6452 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6453 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6454 wp->w_topline = curbuf->b_ml.ml_line_count;
6455 }
6456}
6457#endif
6458
6459#if defined(FEAT_WINDOWS) || defined(PROTO)
6460
6461/*
6462 * A snapshot of the window sizes, to restore them after closing the help
6463 * window.
6464 * Only these fields are used:
6465 * fr_layout
6466 * fr_width
6467 * fr_height
6468 * fr_next
6469 * fr_child
6470 * fr_win (only valid for the old curwin, NULL otherwise)
6471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472
6473/*
6474 * Create a snapshot of the current frame sizes.
6475 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006476 void
6477make_snapshot(idx)
6478 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006480 clear_snapshot(curtab, idx);
6481 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482}
6483
6484 static void
6485make_snapshot_rec(fr, frp)
6486 frame_T *fr;
6487 frame_T **frp;
6488{
6489 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6490 if (*frp == NULL)
6491 return;
6492 (*frp)->fr_layout = fr->fr_layout;
6493# ifdef FEAT_VERTSPLIT
6494 (*frp)->fr_width = fr->fr_width;
6495# endif
6496 (*frp)->fr_height = fr->fr_height;
6497 if (fr->fr_next != NULL)
6498 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6499 if (fr->fr_child != NULL)
6500 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6501 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6502 (*frp)->fr_win = curwin;
6503}
6504
6505/*
6506 * Remove any existing snapshot.
6507 */
6508 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006509clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006510 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006511 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006513 clear_snapshot_rec(tp->tp_snapshot[idx]);
6514 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515}
6516
6517 static void
6518clear_snapshot_rec(fr)
6519 frame_T *fr;
6520{
6521 if (fr != NULL)
6522 {
6523 clear_snapshot_rec(fr->fr_next);
6524 clear_snapshot_rec(fr->fr_child);
6525 vim_free(fr);
6526 }
6527}
6528
6529/*
6530 * Restore a previously created snapshot, if there is any.
6531 * This is only done if the screen size didn't change and the window layout is
6532 * still the same.
6533 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006534 void
6535restore_snapshot(idx, close_curwin)
6536 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 int close_curwin; /* closing current window */
6538{
6539 win_T *wp;
6540
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006541 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006543 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006545 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6546 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006548 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 win_comp_pos();
6550 if (wp != NULL && close_curwin)
6551 win_goto(wp);
6552 redraw_all_later(CLEAR);
6553 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006554 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555}
6556
6557/*
6558 * Check if frames "sn" and "fr" have the same layout, same following frames
6559 * and same children.
6560 */
6561 static int
6562check_snapshot_rec(sn, fr)
6563 frame_T *sn;
6564 frame_T *fr;
6565{
6566 if (sn->fr_layout != fr->fr_layout
6567 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6568 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6569 || (sn->fr_next != NULL
6570 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6571 || (sn->fr_child != NULL
6572 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6573 return FAIL;
6574 return OK;
6575}
6576
6577/*
6578 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6579 * following frames and children.
6580 * Returns a pointer to the old current window, or NULL.
6581 */
6582 static win_T *
6583restore_snapshot_rec(sn, fr)
6584 frame_T *sn;
6585 frame_T *fr;
6586{
6587 win_T *wp = NULL;
6588 win_T *wp2;
6589
6590 fr->fr_height = sn->fr_height;
6591# ifdef FEAT_VERTSPLIT
6592 fr->fr_width = sn->fr_width;
6593# endif
6594 if (fr->fr_layout == FR_LEAF)
6595 {
6596 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6597# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006598 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599# endif
6600 wp = sn->fr_win;
6601 }
6602 if (sn->fr_next != NULL)
6603 {
6604 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6605 if (wp2 != NULL)
6606 wp = wp2;
6607 }
6608 if (sn->fr_child != NULL)
6609 {
6610 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6611 if (wp2 != NULL)
6612 wp = wp2;
6613 }
6614 return wp;
6615}
6616
6617#endif
6618
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006619#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6620 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006621/*
6622 * Set "win" to be the curwin and "tp" to be the current tab page.
6623 * restore_win() MUST be called to undo.
6624 * No autocommands will be executed.
Bram Moolenaard6949742013-06-16 14:18:28 +02006625 * When "no_display" is TRUE the display won't be affected, no redraw is
6626 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006627 * Returns FAIL if switching to "win" failed.
6628 */
6629 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006630switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006631 win_T **save_curwin UNUSED;
6632 tabpage_T **save_curtab UNUSED;
6633 win_T *win UNUSED;
6634 tabpage_T *tp UNUSED;
6635 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006636{
6637# ifdef FEAT_AUTOCMD
6638 block_autocmds();
6639# endif
6640# ifdef FEAT_WINDOWS
6641 *save_curwin = curwin;
6642 if (tp != NULL)
6643 {
6644 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006645 if (no_display)
6646 {
6647 curtab->tp_firstwin = firstwin;
6648 curtab->tp_lastwin = lastwin;
6649 curtab = tp;
6650 firstwin = curtab->tp_firstwin;
6651 lastwin = curtab->tp_lastwin;
6652 }
6653 else
6654 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006655 }
6656 if (!win_valid(win))
6657 {
6658# ifdef FEAT_AUTOCMD
6659 unblock_autocmds();
6660# endif
6661 return FAIL;
6662 }
6663 curwin = win;
6664 curbuf = curwin->w_buffer;
6665# endif
6666 return OK;
6667}
6668
6669/*
6670 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006671 * When "no_display" is TRUE the display won't be affected, no redraw is
6672 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006673 */
6674 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006675restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006676 win_T *save_curwin UNUSED;
6677 tabpage_T *save_curtab UNUSED;
6678 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006679{
6680# ifdef FEAT_WINDOWS
6681 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006682 {
6683 if (no_display)
6684 {
6685 curtab->tp_firstwin = firstwin;
6686 curtab->tp_lastwin = lastwin;
6687 curtab = save_curtab;
6688 firstwin = curtab->tp_firstwin;
6689 lastwin = curtab->tp_lastwin;
6690 }
6691 else
6692 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6693 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006694 if (win_valid(save_curwin))
6695 {
6696 curwin = save_curwin;
6697 curbuf = curwin->w_buffer;
6698 }
6699# endif
6700# ifdef FEAT_AUTOCMD
6701 unblock_autocmds();
6702# endif
6703}
6704
6705/*
6706 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6707 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6708 */
6709 void
6710switch_buffer(save_curbuf, buf)
6711 buf_T *buf;
6712 buf_T **save_curbuf;
6713{
6714# ifdef FEAT_AUTOCMD
6715 block_autocmds();
6716# endif
6717 *save_curbuf = curbuf;
6718 --curbuf->b_nwindows;
6719 curbuf = buf;
6720 curwin->w_buffer = buf;
6721 ++curbuf->b_nwindows;
6722}
6723
6724/*
6725 * Restore the current buffer after using switch_buffer().
6726 */
6727 void
6728restore_buffer(save_curbuf)
6729 buf_T *save_curbuf;
6730{
6731# ifdef FEAT_AUTOCMD
6732 unblock_autocmds();
6733# endif
6734 /* Check for valid buffer, just in case. */
6735 if (buf_valid(save_curbuf))
6736 {
6737 --curbuf->b_nwindows;
6738 curwin->w_buffer = save_curbuf;
6739 curbuf = save_curbuf;
6740 ++curbuf->b_nwindows;
6741 }
6742}
6743#endif
6744
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6746/*
6747 * Return TRUE if there is any vertically split window.
6748 */
6749 int
6750win_hasvertsplit()
6751{
6752 frame_T *fr;
6753
6754 if (topframe->fr_layout == FR_ROW)
6755 return TRUE;
6756
6757 if (topframe->fr_layout == FR_COL)
6758 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6759 if (fr->fr_layout == FR_ROW)
6760 return TRUE;
6761
6762 return FALSE;
6763}
6764#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006765
6766#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6767/*
6768 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006769 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006770 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6771 * If no particular ID is desired, -1 must be specified for 'id'.
6772 * Return ID of added match, -1 on failure.
6773 */
6774 int
Bram Moolenaarb3414592014-06-17 17:48:32 +02006775match_add(wp, grp, pat, prio, id, pos_list)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006776 win_T *wp;
6777 char_u *grp;
6778 char_u *pat;
6779 int prio;
6780 int id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006781 list_T *pos_list;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006782{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006783 matchitem_T *cur;
6784 matchitem_T *prev;
6785 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006786 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006787 regprog_T *regprog = NULL;
6788 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006789
Bram Moolenaarb3414592014-06-17 17:48:32 +02006790 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006791 return -1;
6792 if (id < -1 || id == 0)
6793 {
6794 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6795 return -1;
6796 }
6797 if (id != -1)
6798 {
6799 cur = wp->w_match_head;
6800 while (cur != NULL)
6801 {
6802 if (cur->id == id)
6803 {
6804 EMSGN("E801: ID already taken: %ld", id);
6805 return -1;
6806 }
6807 cur = cur->next;
6808 }
6809 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006810 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006811 {
6812 EMSG2(_(e_nogroup), grp);
6813 return -1;
6814 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006815 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006816 {
6817 EMSG2(_(e_invarg2), pat);
6818 return -1;
6819 }
6820
6821 /* Find available match ID. */
6822 while (id == -1)
6823 {
6824 cur = wp->w_match_head;
6825 while (cur != NULL && cur->id != wp->w_next_match_id)
6826 cur = cur->next;
6827 if (cur == NULL)
6828 id = wp->w_next_match_id;
6829 wp->w_next_match_id++;
6830 }
6831
6832 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006833 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006834 m->id = id;
6835 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006836 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006837 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006838 m->match.regprog = regprog;
6839 m->match.rmm_ic = FALSE;
6840 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006841
Bram Moolenaarb3414592014-06-17 17:48:32 +02006842 /* Set up position matches */
6843 if (pos_list != NULL)
6844 {
6845 linenr_T toplnum = 0;
6846 linenr_T botlnum = 0;
6847 listitem_T *li;
6848 int i;
6849
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006850 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006851 i++, li = li->li_next)
6852 {
6853 linenr_T lnum = 0;
6854 colnr_T col = 0;
6855 int len = 1;
6856 list_T *subl;
6857 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006858 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006859
Bram Moolenaarb3414592014-06-17 17:48:32 +02006860 if (li->li_tv.v_type == VAR_LIST)
6861 {
6862 subl = li->li_tv.vval.v_list;
6863 if (subl == NULL)
6864 goto fail;
6865 subli = subl->lv_first;
6866 if (subli == NULL)
6867 goto fail;
6868 lnum = get_tv_number_chk(&subli->li_tv, &error);
6869 if (error == TRUE)
6870 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006871 if (lnum == 0)
6872 {
6873 --i;
6874 continue;
6875 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006876 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006877 subli = subli->li_next;
6878 if (subli != NULL)
6879 {
6880 col = get_tv_number_chk(&subli->li_tv, &error);
6881 if (error == TRUE)
6882 goto fail;
6883 subli = subli->li_next;
6884 if (subli != NULL)
6885 {
6886 len = get_tv_number_chk(&subli->li_tv, &error);
6887 if (error == TRUE)
6888 goto fail;
6889 }
6890 }
6891 m->pos.pos[i].col = col;
6892 m->pos.pos[i].len = len;
6893 }
6894 else if (li->li_tv.v_type == VAR_NUMBER)
6895 {
6896 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006897 {
6898 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006899 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006900 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006901 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6902 m->pos.pos[i].col = 0;
6903 m->pos.pos[i].len = 0;
6904 }
6905 else
6906 {
6907 EMSG(_("List or number required"));
6908 goto fail;
6909 }
6910 if (toplnum == 0 || lnum < toplnum)
6911 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006912 if (botlnum == 0 || lnum >= botlnum)
6913 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006914 }
6915
6916 /* Calculate top and bottom lines for redrawing area */
6917 if (toplnum != 0)
6918 {
6919 if (wp->w_buffer->b_mod_set)
6920 {
6921 if (wp->w_buffer->b_mod_top > toplnum)
6922 wp->w_buffer->b_mod_top = toplnum;
6923 if (wp->w_buffer->b_mod_bot < botlnum)
6924 wp->w_buffer->b_mod_bot = botlnum;
6925 }
6926 else
6927 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006928 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006929 wp->w_buffer->b_mod_top = toplnum;
6930 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006931 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006932 }
6933 m->pos.toplnum = toplnum;
6934 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006935 rtype = VALID;
6936 }
6937 }
6938
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006939 /* Insert new match. The match list is in ascending order with regard to
6940 * the match priorities. */
6941 cur = wp->w_match_head;
6942 prev = cur;
6943 while (cur != NULL && prio >= cur->priority)
6944 {
6945 prev = cur;
6946 cur = cur->next;
6947 }
6948 if (cur == prev)
6949 wp->w_match_head = m;
6950 else
6951 prev->next = m;
6952 m->next = cur;
6953
Bram Moolenaarb3414592014-06-17 17:48:32 +02006954 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006955 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006956
6957fail:
6958 vim_free(m);
6959 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006960}
6961
6962/*
6963 * Delete match with ID 'id' in the match list of window 'wp'.
6964 * Print error messages if 'perr' is TRUE.
6965 */
6966 int
6967match_delete(wp, id, perr)
6968 win_T *wp;
6969 int id;
6970 int perr;
6971{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006972 matchitem_T *cur = wp->w_match_head;
6973 matchitem_T *prev = cur;
6974 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006975
6976 if (id < 1)
6977 {
6978 if (perr == TRUE)
6979 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6980 id);
6981 return -1;
6982 }
6983 while (cur != NULL && cur->id != id)
6984 {
6985 prev = cur;
6986 cur = cur->next;
6987 }
6988 if (cur == NULL)
6989 {
6990 if (perr == TRUE)
6991 EMSGN("E803: ID not found: %ld", id);
6992 return -1;
6993 }
6994 if (cur == prev)
6995 wp->w_match_head = cur->next;
6996 else
6997 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006998 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006999 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007000 if (cur->pos.toplnum != 0)
7001 {
7002 if (wp->w_buffer->b_mod_set)
7003 {
7004 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
7005 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7006 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
7007 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
7008 }
7009 else
7010 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007011 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007012 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7013 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007014 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007015 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007016 rtype = VALID;
7017 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007018 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007019 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007020 return 0;
7021}
7022
7023/*
7024 * Delete all matches in the match list of window 'wp'.
7025 */
7026 void
7027clear_matches(wp)
7028 win_T *wp;
7029{
7030 matchitem_T *m;
7031
7032 while (wp->w_match_head != NULL)
7033 {
7034 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007035 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007036 vim_free(wp->w_match_head->pattern);
7037 vim_free(wp->w_match_head);
7038 wp->w_match_head = m;
7039 }
7040 redraw_later(SOME_VALID);
7041}
7042
7043/*
7044 * Get match from ID 'id' in window 'wp'.
7045 * Return NULL if match not found.
7046 */
7047 matchitem_T *
7048get_match(wp, id)
7049 win_T *wp;
7050 int id;
7051{
7052 matchitem_T *cur = wp->w_match_head;
7053
7054 while (cur != NULL && cur->id != id)
7055 cur = cur->next;
7056 return cur;
7057}
7058#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007059
7060#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7061 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007062get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007063{
7064 int i = 1;
7065 win_T *w;
7066
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007067 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007068 ++i;
7069
7070 if (w == NULL)
7071 return 0;
7072 else
7073 return i;
7074}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007075
7076 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007077get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007078{
7079 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007080# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007081 tabpage_T *t;
7082
7083 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7084 ++i;
7085
7086 if (t == NULL)
7087 return 0;
7088 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007089# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007090 return i;
7091}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007092#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007093
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007094#ifdef FEAT_WINDOWS
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007095/*
7096 * Return TRUE if "topfrp" and its children are at the right height.
7097 */
7098 static int
7099frame_check_height(topfrp, height)
7100 frame_T *topfrp;
7101 int height;
7102{
7103 frame_T *frp;
7104
7105 if (topfrp->fr_height != height)
7106 return FALSE;
7107
7108 if (topfrp->fr_layout == FR_ROW)
7109 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7110 if (frp->fr_height != height)
7111 return FALSE;
7112
7113 return TRUE;
7114}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007115#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007116
7117#ifdef FEAT_VERTSPLIT
7118/*
7119 * Return TRUE if "topfrp" and its children are at the right width.
7120 */
7121 static int
7122frame_check_width(topfrp, width)
7123 frame_T *topfrp;
7124 int width;
7125{
7126 frame_T *frp;
7127
7128 if (topfrp->fr_width != width)
7129 return FALSE;
7130
7131 if (topfrp->fr_layout == FR_COL)
7132 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7133 if (frp->fr_width != width)
7134 return FALSE;
7135
7136 return TRUE;
7137}
7138#endif
7139