blob: 6460684bc38d5dec6930585d2822694205541d23 [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 Moolenaar7e8fd632006-02-18 22:14:51 +000047static int leave_tabpage __ARGS((buf_T *new_curbuf));
Bram Moolenaara8596c42012-06-13 14:28:20 +020048static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf, int trigger_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));
51static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
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));
56#ifdef FEAT_VERTSPLIT
57static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static void win_goto_ver __ARGS((int up, long count));
59static void win_goto_hor __ARGS((int left, long count));
60#endif
61static void frame_add_height __ARGS((frame_T *frp, int n));
62static void last_status_rec __ARGS((frame_T *fr, int statusline));
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000065static void clear_snapshot __ARGS((tabpage_T *tp, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static void clear_snapshot_rec __ARGS((frame_T *fr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000067static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
68static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
69
70#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000071
Bram Moolenaar746ebd32009-06-16 14:01:43 +000072static win_T *win_alloc __ARGS((win_T *after, int hidden));
Bram Moolenaar0215e8e2010-12-17 17:35:10 +010073static void set_fraction __ARGS((win_T *wp));
74static void win_new_height __ARGS((win_T *wp, int height));
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
76#define URL_SLASH 1 /* path_is_url() has found "://" */
77#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
78
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000079#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaar05159a02005-02-26 23:04:13 +000081#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000082# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000083#else
84# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000085#endif
86
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000088
89static char *m_onlyone = N_("Already only one window");
90
Bram Moolenaar071d4272004-06-13 20:20:40 +000091/*
92 * all CTRL-W window commands are handled here, called from normal_cmd().
93 */
94 void
95do_window(nchar, Prenum, xchar)
96 int nchar;
97 long Prenum;
98 int xchar; /* extra char from ":wincmd gx" or NUL */
99{
100 long Prenum1;
101 win_T *wp;
102#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
103 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000104 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#endif
106#ifdef FEAT_FIND_ID
107 int type = FIND_DEFINE;
108 int len;
109#endif
110 char_u cbuf[40];
111
112 if (Prenum == 0)
113 Prenum1 = 1;
114 else
115 Prenum1 = Prenum;
116
117#ifdef FEAT_CMDWIN
118# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
119#else
120# define CHECK_CMDWIN
121#endif
122
123 switch (nchar)
124 {
125/* split current window in two parts, horizontally */
126 case 'S':
127 case Ctrl_S:
128 case 's':
129 CHECK_CMDWIN
130#ifdef FEAT_VISUAL
131 reset_VIsual_and_resel(); /* stop Visual mode */
132#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000133#ifdef FEAT_QUICKFIX
134 /* When splitting the quickfix window open a new buffer in it,
135 * don't replicate the quickfix buffer. */
136 if (bt_quickfix(curbuf))
137 goto newwindow;
138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#ifdef FEAT_GUI
140 need_mouse_correct = TRUE;
141#endif
142 win_split((int)Prenum, 0);
143 break;
144
145#ifdef FEAT_VERTSPLIT
146/* split current window in two parts, vertically */
147 case Ctrl_V:
148 case 'v':
149 CHECK_CMDWIN
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000150# ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000152# endif
153# ifdef FEAT_QUICKFIX
154 /* When splitting the quickfix window open a new buffer in it,
155 * don't replicate the quickfix buffer. */
156 if (bt_quickfix(curbuf))
157 goto newwindow;
158# endif
159# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 win_split((int)Prenum, WSP_VERT);
163 break;
164#endif
165
166/* split current window and edit alternate file */
167 case Ctrl_HAT:
168 case '^':
169 CHECK_CMDWIN
170#ifdef FEAT_VISUAL
171 reset_VIsual_and_resel(); /* stop Visual mode */
172#endif
173 STRCPY(cbuf, "split #");
174 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000175 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
176 "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 do_cmdline_cmd(cbuf);
178 break;
179
180/* open new window */
181 case Ctrl_N:
182 case 'n':
183 CHECK_CMDWIN
184#ifdef FEAT_VISUAL
185 reset_VIsual_and_resel(); /* stop Visual mode */
186#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000187#ifdef FEAT_QUICKFIX
188newwindow:
189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000191 /* window height */
192 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 else
194 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000195#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
196 if (nchar == 'v' || nchar == Ctrl_V)
197 STRCAT(cbuf, "v");
198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 STRCAT(cbuf, "new");
200 do_cmdline_cmd(cbuf);
201 break;
202
203/* quit current window */
204 case Ctrl_Q:
205 case 'q':
206#ifdef FEAT_VISUAL
207 reset_VIsual_and_resel(); /* stop Visual mode */
208#endif
209 do_cmdline_cmd((char_u *)"quit");
210 break;
211
212/* close current window */
213 case Ctrl_C:
214 case 'c':
215#ifdef FEAT_VISUAL
216 reset_VIsual_and_resel(); /* stop Visual mode */
217#endif
218 do_cmdline_cmd((char_u *)"close");
219 break;
220
221#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
222/* close preview window */
223 case Ctrl_Z:
224 case 'z':
225 CHECK_CMDWIN
226#ifdef FEAT_VISUAL
227 reset_VIsual_and_resel(); /* stop Visual mode */
228#endif
229 do_cmdline_cmd((char_u *)"pclose");
230 break;
231
232/* cursor to preview window */
233 case 'P':
234 for (wp = firstwin; wp != NULL; wp = wp->w_next)
235 if (wp->w_p_pvw)
236 break;
237 if (wp == NULL)
238 EMSG(_("E441: There is no preview window"));
239 else
240 win_goto(wp);
241 break;
242#endif
243
244/* close all but current window */
245 case Ctrl_O:
246 case 'o':
247 CHECK_CMDWIN
248#ifdef FEAT_VISUAL
249 reset_VIsual_and_resel(); /* stop Visual mode */
250#endif
251 do_cmdline_cmd((char_u *)"only");
252 break;
253
254/* cursor to next window with wrap around */
255 case Ctrl_W:
256 case 'w':
257/* cursor to previous window with wrap around */
258 case 'W':
259 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000260 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 beep_flush();
262 else
263 {
264 if (Prenum) /* go to specified window */
265 {
266 for (wp = firstwin; --Prenum > 0; )
267 {
268 if (wp->w_next == NULL)
269 break;
270 else
271 wp = wp->w_next;
272 }
273 }
274 else
275 {
276 if (nchar == 'W') /* go to previous window */
277 {
278 wp = curwin->w_prev;
279 if (wp == NULL)
280 wp = lastwin; /* wrap around */
281 }
282 else /* go to next window */
283 {
284 wp = curwin->w_next;
285 if (wp == NULL)
286 wp = firstwin; /* wrap around */
287 }
288 }
289 win_goto(wp);
290 }
291 break;
292
293/* cursor to window below */
294 case 'j':
295 case K_DOWN:
296 case Ctrl_J:
297 CHECK_CMDWIN
298#ifdef FEAT_VERTSPLIT
299 win_goto_ver(FALSE, Prenum1);
300#else
301 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
302 wp = wp->w_next)
303 ;
304 win_goto(wp);
305#endif
306 break;
307
308/* cursor to window above */
309 case 'k':
310 case K_UP:
311 case Ctrl_K:
312 CHECK_CMDWIN
313#ifdef FEAT_VERTSPLIT
314 win_goto_ver(TRUE, Prenum1);
315#else
316 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
317 wp = wp->w_prev)
318 ;
319 win_goto(wp);
320#endif
321 break;
322
323#ifdef FEAT_VERTSPLIT
324/* cursor to left window */
325 case 'h':
326 case K_LEFT:
327 case Ctrl_H:
328 case K_BS:
329 CHECK_CMDWIN
330 win_goto_hor(TRUE, Prenum1);
331 break;
332
333/* cursor to right window */
334 case 'l':
335 case K_RIGHT:
336 case Ctrl_L:
337 CHECK_CMDWIN
338 win_goto_hor(FALSE, Prenum1);
339 break;
340#endif
341
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000342/* move window to new tab page */
343 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000344 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000345 MSG(_(m_onlyone));
346 else
347 {
348 tabpage_T *oldtab = curtab;
349 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000350
351 /* First create a new tab with the window, then go back to
352 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000353 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000354 if (win_new_tabpage((int)Prenum) == OK
355 && valid_tabpage(oldtab))
356 {
357 newtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +0200358 goto_tabpage_tp(oldtab, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000359 if (curwin == wp)
360 win_close(curwin, FALSE);
361 if (valid_tabpage(newtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +0200362 goto_tabpage_tp(newtab, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000363 }
364 }
365 break;
366
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367/* cursor to top-left window */
368 case 't':
369 case Ctrl_T:
370 win_goto(firstwin);
371 break;
372
373/* cursor to bottom-right window */
374 case 'b':
375 case Ctrl_B:
376 win_goto(lastwin);
377 break;
378
379/* cursor to last accessed (previous) window */
380 case 'p':
381 case Ctrl_P:
382 if (prevwin == NULL)
383 beep_flush();
384 else
385 win_goto(prevwin);
386 break;
387
388/* exchange current and next window */
389 case 'x':
390 case Ctrl_X:
391 CHECK_CMDWIN
392 win_exchange(Prenum);
393 break;
394
395/* rotate windows downwards */
396 case Ctrl_R:
397 case 'r':
398 CHECK_CMDWIN
399#ifdef FEAT_VISUAL
400 reset_VIsual_and_resel(); /* stop Visual mode */
401#endif
402 win_rotate(FALSE, (int)Prenum1); /* downwards */
403 break;
404
405/* rotate windows upwards */
406 case 'R':
407 CHECK_CMDWIN
408#ifdef FEAT_VISUAL
409 reset_VIsual_and_resel(); /* stop Visual mode */
410#endif
411 win_rotate(TRUE, (int)Prenum1); /* upwards */
412 break;
413
414/* move window to the very top/bottom/left/right */
415 case 'K':
416 case 'J':
417#ifdef FEAT_VERTSPLIT
418 case 'H':
419 case 'L':
420#endif
421 CHECK_CMDWIN
422 win_totop((int)Prenum,
423 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
424 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
425 break;
426
427/* make all windows the same height */
428 case '=':
429#ifdef FEAT_GUI
430 need_mouse_correct = TRUE;
431#endif
432 win_equal(NULL, FALSE, 'b');
433 break;
434
435/* increase current window height */
436 case '+':
437#ifdef FEAT_GUI
438 need_mouse_correct = TRUE;
439#endif
440 win_setheight(curwin->w_height + (int)Prenum1);
441 break;
442
443/* decrease current window height */
444 case '-':
445#ifdef FEAT_GUI
446 need_mouse_correct = TRUE;
447#endif
448 win_setheight(curwin->w_height - (int)Prenum1);
449 break;
450
451/* set current window height */
452 case Ctrl__:
453 case '_':
454#ifdef FEAT_GUI
455 need_mouse_correct = TRUE;
456#endif
457 win_setheight(Prenum ? (int)Prenum : 9999);
458 break;
459
460#ifdef FEAT_VERTSPLIT
461/* increase current window width */
462 case '>':
463#ifdef FEAT_GUI
464 need_mouse_correct = TRUE;
465#endif
466 win_setwidth(curwin->w_width + (int)Prenum1);
467 break;
468
469/* decrease current window width */
470 case '<':
471#ifdef FEAT_GUI
472 need_mouse_correct = TRUE;
473#endif
474 win_setwidth(curwin->w_width - (int)Prenum1);
475 break;
476
477/* set current window width */
478 case '|':
479#ifdef FEAT_GUI
480 need_mouse_correct = TRUE;
481#endif
482 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
483 break;
484#endif
485
486/* jump to tag and split window if tag exists (in preview window) */
487#if defined(FEAT_QUICKFIX)
488 case '}':
489 CHECK_CMDWIN
490 if (Prenum)
491 g_do_tagpreview = Prenum;
492 else
493 g_do_tagpreview = p_pvh;
494 /*FALLTHROUGH*/
495#endif
496 case ']':
497 case Ctrl_RSB:
498 CHECK_CMDWIN
499#ifdef FEAT_VISUAL
500 reset_VIsual_and_resel(); /* stop Visual mode */
501#endif
502 if (Prenum)
503 postponed_split = Prenum;
504 else
505 postponed_split = -1;
506
507 /* Execute the command right here, required when
508 * "wincmd ]" was used in a function. */
509 do_nv_ident(Ctrl_RSB, NUL);
510 break;
511
512#ifdef FEAT_SEARCHPATH
513/* edit file name under cursor in a new window */
514 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000515 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000517wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000519
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000520 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 if (ptr != NULL)
522 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000523# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000525# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 setpcmark();
527 if (win_split(0, 0) == OK)
528 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200529 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000530 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
531 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000532 if (nchar == 'F' && lnum >= 0)
533 {
534 curwin->w_cursor.lnum = lnum;
535 check_cursor_lnum();
536 beginline(BL_SOL | BL_FIX);
537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 }
539 vim_free(ptr);
540 }
541 break;
542#endif
543
544#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000545/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 * new window -- webb
547 */
548 case 'i': /* Go to any match */
549 case Ctrl_I:
550 type = FIND_ANY;
551 /* FALLTHROUGH */
552 case 'd': /* Go to definition, using 'define' */
553 case Ctrl_D:
554 CHECK_CMDWIN
555 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
556 break;
557 find_pattern_in_path(ptr, 0, len, TRUE,
558 Prenum == 0 ? TRUE : FALSE, type,
559 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
560 curwin->w_set_curswant = TRUE;
561 break;
562#endif
563
Bram Moolenaar05159a02005-02-26 23:04:13 +0000564 case K_KENTER:
565 case CAR:
566#if defined(FEAT_QUICKFIX)
567 /*
568 * In a quickfix window a <CR> jumps to the error under the
569 * cursor in a new window.
570 */
571 if (bt_quickfix(curbuf))
572 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000573 sprintf((char *)cbuf, "split +%ld%s",
574 (long)curwin->w_cursor.lnum,
575 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000576 do_cmdline_cmd(cbuf);
577 }
578#endif
579 break;
580
581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582/* CTRL-W g extended commands */
583 case 'g':
584 case Ctrl_G:
585 CHECK_CMDWIN
586#ifdef USE_ON_FLY_SCROLL
587 dont_scroll = TRUE; /* disallow scrolling here */
588#endif
589 ++no_mapping;
590 ++allow_keys; /* no mapping for xchar, but allow key codes */
591 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000592 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 --no_mapping;
595 --allow_keys;
596#ifdef FEAT_CMDL_INFO
597 (void)add_to_showcmd(xchar);
598#endif
599 switch (xchar)
600 {
601#if defined(FEAT_QUICKFIX)
602 case '}':
603 xchar = Ctrl_RSB;
604 if (Prenum)
605 g_do_tagpreview = Prenum;
606 else
607 g_do_tagpreview = p_pvh;
608 /*FALLTHROUGH*/
609#endif
610 case ']':
611 case Ctrl_RSB:
612#ifdef FEAT_VISUAL
613 reset_VIsual_and_resel(); /* stop Visual mode */
614#endif
615 if (Prenum)
616 postponed_split = Prenum;
617 else
618 postponed_split = -1;
619
620 /* Execute the command right here, required when
621 * "wincmd g}" was used in a function. */
622 do_nv_ident('g', xchar);
623 break;
624
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000625#ifdef FEAT_SEARCHPATH
626 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000627 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100628 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000629 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000630 goto wingotofile;
631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 default:
633 beep_flush();
634 break;
635 }
636 break;
637
638 default: beep_flush();
639 break;
640 }
641}
642
643/*
644 * split the current window, implements CTRL-W s and :split
645 *
646 * "size" is the height or width for the new window, 0 to use half of current
647 * height or width.
648 *
649 * "flags":
650 * WSP_ROOM: require enough room for new window
651 * WSP_VERT: vertical split.
652 * WSP_TOP: open window at the top-left of the shell (help window).
653 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
654 * WSP_HELP: creating the help window, keep layout snapshot
655 *
656 * return FAIL for failure, OK otherwise
657 */
658 int
659win_split(size, flags)
660 int size;
661 int flags;
662{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000663 /* When the ":tab" modifier was used open a new tab page instead. */
664 if (may_open_tabpage() == OK)
665 return OK;
666
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 /* Add flags from ":vertical", ":topleft" and ":botright". */
668 flags |= cmdmod.split;
669 if ((flags & WSP_TOP) && (flags & WSP_BOT))
670 {
671 EMSG(_("E442: Can't split topleft and botright at the same time"));
672 return FAIL;
673 }
674
675 /* When creating the help window make a snapshot of the window layout.
676 * Otherwise clear the snapshot, it's now invalid. */
677 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000678 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000680 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
682 return win_split_ins(size, flags, NULL, 0);
683}
684
685/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100686 * When "new_wp" is NULL: split the current window in two.
687 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 * top/left/right/bottom.
689 * return FAIL for failure, OK otherwise
690 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000691 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100692win_split_ins(size, flags, new_wp, dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 int size;
694 int flags;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100695 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 int dir;
697{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100698 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 win_T *oldwin;
700 int new_size = size;
701 int i;
702 int need_status = 0;
703 int do_equal = FALSE;
704 int needed;
705 int available;
706 int oldwin_height = 0;
707 int layout;
708 frame_T *frp, *curfrp;
709 int before;
710
711 if (flags & WSP_TOP)
712 oldwin = firstwin;
713 else if (flags & WSP_BOT)
714 oldwin = lastwin;
715 else
716 oldwin = curwin;
717
718 /* add a status line when p_ls == 1 and splitting the first window */
719 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
720 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100721 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {
723 EMSG(_(e_noroom));
724 return FAIL;
725 }
726 need_status = STATUS_HEIGHT;
727 }
728
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000729#ifdef FEAT_GUI
730 /* May be needed for the scrollbars that are going to change. */
731 if (gui.in_use)
732 out_flush();
733#endif
734
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735#ifdef FEAT_VERTSPLIT
736 if (flags & WSP_VERT)
737 {
738 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
740 /*
741 * Check if we are able to split the current window and compute its
742 * width.
743 */
744 needed = p_wmw + 1;
745 if (flags & WSP_ROOM)
746 needed += p_wiw - p_wmw;
747 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
748 {
749 available = topframe->fr_width;
750 needed += frame_minwidth(topframe, NULL);
751 }
752 else
753 available = oldwin->w_width;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100754 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
756 EMSG(_(e_noroom));
757 return FAIL;
758 }
759 if (new_size == 0)
760 new_size = oldwin->w_width / 2;
761 if (new_size > oldwin->w_width - p_wmw - 1)
762 new_size = oldwin->w_width - p_wmw - 1;
763 if (new_size < p_wmw)
764 new_size = p_wmw;
765
766 /* if it doesn't fit in the current window, need win_equal() */
767 if (oldwin->w_width - new_size - 1 < p_wmw)
768 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000769
770 /* We don't like to take lines for the new window from a
771 * 'winfixwidth' window. Take them from a window to the left or right
772 * instead, if possible. */
773 if (oldwin->w_p_wfw)
774 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000775
776 /* Only make all windows the same width if one of them (except oldwin)
777 * is wider than one of the split windows. */
778 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
779 && oldwin->w_frame->fr_parent != NULL)
780 {
781 frp = oldwin->w_frame->fr_parent->fr_child;
782 while (frp != NULL)
783 {
784 if (frp->fr_win != oldwin && frp->fr_win != NULL
785 && (frp->fr_win->w_width > new_size
786 || frp->fr_win->w_width > oldwin->w_width
787 - new_size - STATUS_HEIGHT))
788 {
789 do_equal = TRUE;
790 break;
791 }
792 frp = frp->fr_next;
793 }
794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 }
796 else
797#endif
798 {
799 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801 /*
802 * Check if we are able to split the current window and compute its
803 * height.
804 */
805 needed = p_wmh + STATUS_HEIGHT + need_status;
806 if (flags & WSP_ROOM)
807 needed += p_wh - p_wmh;
808 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
809 {
810 available = topframe->fr_height;
811 needed += frame_minheight(topframe, NULL);
812 }
813 else
814 {
815 available = oldwin->w_height;
816 needed += p_wmh;
817 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100818 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 {
820 EMSG(_(e_noroom));
821 return FAIL;
822 }
823 oldwin_height = oldwin->w_height;
824 if (need_status)
825 {
826 oldwin->w_status_height = STATUS_HEIGHT;
827 oldwin_height -= STATUS_HEIGHT;
828 }
829 if (new_size == 0)
830 new_size = oldwin_height / 2;
831
832 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
833 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
834 if (new_size < p_wmh)
835 new_size = p_wmh;
836
837 /* if it doesn't fit in the current window, need win_equal() */
838 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
839 do_equal = TRUE;
840
841 /* We don't like to take lines for the new window from a
842 * 'winfixheight' window. Take them from a window above or below
843 * instead, if possible. */
844 if (oldwin->w_p_wfh)
845 {
846 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
847 oldwin);
848 oldwin_height = oldwin->w_height;
849 if (need_status)
850 oldwin_height -= STATUS_HEIGHT;
851 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000852
853 /* Only make all windows the same height if one of them (except oldwin)
854 * is higher than one of the split windows. */
855 if (!do_equal && p_ea && size == 0
856#ifdef FEAT_VERTSPLIT
857 && *p_ead != 'h'
858#endif
859 && oldwin->w_frame->fr_parent != NULL)
860 {
861 frp = oldwin->w_frame->fr_parent->fr_child;
862 while (frp != NULL)
863 {
864 if (frp->fr_win != oldwin && frp->fr_win != NULL
865 && (frp->fr_win->w_height > new_size
866 || frp->fr_win->w_height > oldwin_height - new_size
867 - STATUS_HEIGHT))
868 {
869 do_equal = TRUE;
870 break;
871 }
872 frp = frp->fr_next;
873 }
874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 }
876
877 /*
878 * allocate new window structure and link it in the window list
879 */
880 if ((flags & WSP_TOP) == 0
881 && ((flags & WSP_BOT)
882 || (flags & WSP_BELOW)
883 || (!(flags & WSP_ABOVE)
884 && (
885#ifdef FEAT_VERTSPLIT
886 (flags & WSP_VERT) ? p_spr :
887#endif
888 p_sb))))
889 {
890 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100891 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000892 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 else
894 win_append(oldwin, wp);
895 }
896 else
897 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100898 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000899 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 else
901 win_append(oldwin->w_prev, wp);
902 }
903
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100904 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 {
906 if (wp == NULL)
907 return FAIL;
908
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000909 new_frame(wp);
910 if (wp->w_frame == NULL)
911 {
912 win_free(wp, NULL);
913 return FAIL;
914 }
915
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000916 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000917 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 }
919
920 /*
921 * Reorganise the tree of frames to insert the new window.
922 */
923 if (flags & (WSP_TOP | WSP_BOT))
924 {
925#ifdef FEAT_VERTSPLIT
926 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
927 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
928#else
929 if (topframe->fr_layout == FR_COL)
930#endif
931 {
932 curfrp = topframe->fr_child;
933 if (flags & WSP_BOT)
934 while (curfrp->fr_next != NULL)
935 curfrp = curfrp->fr_next;
936 }
937 else
938 curfrp = topframe;
939 before = (flags & WSP_TOP);
940 }
941 else
942 {
943 curfrp = oldwin->w_frame;
944 if (flags & WSP_BELOW)
945 before = FALSE;
946 else if (flags & WSP_ABOVE)
947 before = TRUE;
948 else
949#ifdef FEAT_VERTSPLIT
950 if (flags & WSP_VERT)
951 before = !p_spr;
952 else
953#endif
954 before = !p_sb;
955 }
956 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
957 {
958 /* Need to create a new frame in the tree to make a branch. */
959 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
960 *frp = *curfrp;
961 curfrp->fr_layout = layout;
962 frp->fr_parent = curfrp;
963 frp->fr_next = NULL;
964 frp->fr_prev = NULL;
965 curfrp->fr_child = frp;
966 curfrp->fr_win = NULL;
967 curfrp = frp;
968 if (frp->fr_win != NULL)
969 oldwin->w_frame = frp;
970 else
971 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
972 frp->fr_parent = curfrp;
973 }
974
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100975 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000976 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100978 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 frp->fr_parent = curfrp->fr_parent;
980
981 /* Insert the new frame at the right place in the frame list. */
982 if (before)
983 frame_insert(curfrp, frp);
984 else
985 frame_append(curfrp, frp);
986
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100987 /* Set w_fraction now so that the cursor keeps the same relative
988 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +0100989 if (oldwin->w_height > 0)
990 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100991 wp->w_fraction = oldwin->w_fraction;
992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#ifdef FEAT_VERTSPLIT
994 if (flags & WSP_VERT)
995 {
996 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (need_status)
999 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001000 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 oldwin->w_status_height = need_status;
1002 }
1003 if (flags & (WSP_TOP | WSP_BOT))
1004 {
1005 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001006 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001007 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 wp->w_status_height = (p_ls > 0);
1009 }
1010 else
1011 {
1012 /* height and row of new window is same as current window */
1013 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001014 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 wp->w_status_height = oldwin->w_status_height;
1016 }
1017 frp->fr_height = curfrp->fr_height;
1018
1019 /* "new_size" of the current window goes to the new window, use
1020 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001021 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (before)
1023 wp->w_vsep_width = 1;
1024 else
1025 {
1026 wp->w_vsep_width = oldwin->w_vsep_width;
1027 oldwin->w_vsep_width = 1;
1028 }
1029 if (flags & (WSP_TOP | WSP_BOT))
1030 {
1031 if (flags & WSP_BOT)
1032 frame_add_vsep(curfrp);
1033 /* Set width of neighbor frame */
1034 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001035 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1036 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 }
1038 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001039 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (before) /* new window left of current one */
1041 {
1042 wp->w_wincol = oldwin->w_wincol;
1043 oldwin->w_wincol += new_size + 1;
1044 }
1045 else /* new window right of current one */
1046 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1047 frame_fix_width(oldwin);
1048 frame_fix_width(wp);
1049 }
1050 else
1051#endif
1052 {
1053 /* width and column of new window is same as current window */
1054#ifdef FEAT_VERTSPLIT
1055 if (flags & (WSP_TOP | WSP_BOT))
1056 {
1057 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001058 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 wp->w_vsep_width = 0;
1060 }
1061 else
1062 {
1063 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001064 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 wp->w_vsep_width = oldwin->w_vsep_width;
1066 }
1067 frp->fr_width = curfrp->fr_width;
1068#endif
1069
1070 /* "new_size" of the current window goes to the new window, use
1071 * one row for the status line */
1072 win_new_height(wp, new_size);
1073 if (flags & (WSP_TOP | WSP_BOT))
1074 frame_new_height(curfrp, curfrp->fr_height
1075 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1076 else
1077 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1078 if (before) /* new window above current one */
1079 {
1080 wp->w_winrow = oldwin->w_winrow;
1081 wp->w_status_height = STATUS_HEIGHT;
1082 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1083 }
1084 else /* new window below current one */
1085 {
1086 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1087 wp->w_status_height = oldwin->w_status_height;
1088 oldwin->w_status_height = STATUS_HEIGHT;
1089 }
1090#ifdef FEAT_VERTSPLIT
1091 if (flags & WSP_BOT)
1092 frame_add_statusline(curfrp);
1093#endif
1094 frame_fix_height(wp);
1095 frame_fix_height(oldwin);
1096 }
1097
1098 if (flags & (WSP_TOP | WSP_BOT))
1099 (void)win_comp_pos();
1100
1101 /*
1102 * Both windows need redrawing
1103 */
1104 redraw_win_later(wp, NOT_VALID);
1105 wp->w_redr_status = TRUE;
1106 redraw_win_later(oldwin, NOT_VALID);
1107 oldwin->w_redr_status = TRUE;
1108
1109 if (need_status)
1110 {
1111 msg_row = Rows - 1;
1112 msg_col = sc_col;
1113 msg_clr_eos_force(); /* Old command/ruler may still be there */
1114 comp_col();
1115 msg_row = Rows - 1;
1116 msg_col = 0; /* put position back at start of line */
1117 }
1118
1119 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001120 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 */
1122 if (do_equal || dir != 0)
1123 win_equal(wp, TRUE,
1124#ifdef FEAT_VERTSPLIT
1125 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1126 : dir == 'h' ? 'b' :
1127#endif
1128 'v');
1129
1130 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1131 * size was given. */
1132#ifdef FEAT_VERTSPLIT
1133 if (flags & WSP_VERT)
1134 {
1135 i = p_wiw;
1136 if (size != 0)
1137 p_wiw = size;
1138
1139# ifdef FEAT_GUI
1140 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1141 if (gui.in_use)
1142 gui_init_which_components(NULL);
1143# endif
1144 }
1145 else
1146#endif
1147 {
1148 i = p_wh;
1149 if (size != 0)
1150 p_wh = size;
1151 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001152
1153 /*
1154 * make the new window the current window
1155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 win_enter(wp, FALSE);
1157#ifdef FEAT_VERTSPLIT
1158 if (flags & WSP_VERT)
1159 p_wiw = i;
1160 else
1161#endif
1162 p_wh = i;
1163
1164 return OK;
1165}
1166
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001167
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001168/*
1169 * Initialize window "newp" from window "oldp".
1170 * Used when splitting a window and when creating a new tab page.
1171 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001172 * WSP_NEWLOC may be specified in flags to prevent the location list from
1173 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001174 */
1175 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001176win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001177 win_T *newp;
1178 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001179 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001180{
1181 int i;
1182
1183 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001184#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001185 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001186#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001187 oldp->w_buffer->b_nwindows++;
1188 newp->w_cursor = oldp->w_cursor;
1189 newp->w_valid = 0;
1190 newp->w_curswant = oldp->w_curswant;
1191 newp->w_set_curswant = oldp->w_set_curswant;
1192 newp->w_topline = oldp->w_topline;
1193#ifdef FEAT_DIFF
1194 newp->w_topfill = oldp->w_topfill;
1195#endif
1196 newp->w_leftcol = oldp->w_leftcol;
1197 newp->w_pcmark = oldp->w_pcmark;
1198 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1199 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001200 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001201 newp->w_fraction = oldp->w_fraction;
1202 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1203#ifdef FEAT_JUMPLIST
1204 copy_jumplist(oldp, newp);
1205#endif
1206#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001207 if (flags & WSP_NEWLOC)
1208 {
1209 /* Don't copy the location list. */
1210 newp->w_llist = NULL;
1211 newp->w_llist_ref = NULL;
1212 }
1213 else
1214 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001215#endif
1216 if (oldp->w_localdir != NULL)
1217 newp->w_localdir = vim_strsave(oldp->w_localdir);
1218
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001219 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001220 for (i = 0; i < oldp->w_tagstacklen; i++)
1221 {
1222 newp->w_tagstack[i] = oldp->w_tagstack[i];
1223 if (newp->w_tagstack[i].tagname != NULL)
1224 newp->w_tagstack[i].tagname =
1225 vim_strsave(newp->w_tagstack[i].tagname);
1226 }
1227 newp->w_tagstackidx = oldp->w_tagstackidx;
1228 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001229#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001230 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001231#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001232
1233 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001234
Bram Moolenaara971b822011-09-14 14:43:25 +02001235#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001236 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001237#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001238}
1239
1240/*
1241 * Initialize window "newp" from window"old".
1242 * Only the essential things are copied.
1243 */
1244 static void
1245win_init_some(newp, oldp)
1246 win_T *newp;
1247 win_T *oldp;
1248{
1249 /* Use the same argument list. */
1250 newp->w_alist = oldp->w_alist;
1251 ++newp->w_alist->al_refcount;
1252 newp->w_arg_idx = oldp->w_arg_idx;
1253
1254 /* copy options from existing window */
1255 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001256}
1257
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258#endif /* FEAT_WINDOWS */
1259
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260#if defined(FEAT_WINDOWS) || defined(PROTO)
1261/*
1262 * Check if "win" is a pointer to an existing window.
1263 */
1264 int
1265win_valid(win)
1266 win_T *win;
1267{
1268 win_T *wp;
1269
1270 if (win == NULL)
1271 return FALSE;
1272 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1273 if (wp == win)
1274 return TRUE;
1275 return FALSE;
1276}
1277
1278/*
1279 * Return the number of windows.
1280 */
1281 int
1282win_count()
1283{
1284 win_T *wp;
1285 int count = 0;
1286
1287 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1288 ++count;
1289 return count;
1290}
1291
1292/*
1293 * Make "count" windows on the screen.
1294 * Return actual number of windows on the screen.
1295 * Must be called when there is just one window, filling the whole screen
1296 * (excluding the command line).
1297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 int
1299make_windows(count, vertical)
1300 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001301 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302{
1303 int maxcount;
1304 int todo;
1305
1306#ifdef FEAT_VERTSPLIT
1307 if (vertical)
1308 {
1309 /* Each windows needs at least 'winminwidth' lines and a separator
1310 * column. */
1311 maxcount = (curwin->w_width + curwin->w_vsep_width
1312 - (p_wiw - p_wmw)) / (p_wmw + 1);
1313 }
1314 else
1315#endif
1316 {
1317 /* Each window needs at least 'winminheight' lines and a status line. */
1318 maxcount = (curwin->w_height + curwin->w_status_height
1319 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1320 }
1321
1322 if (maxcount < 2)
1323 maxcount = 2;
1324 if (count > maxcount)
1325 count = maxcount;
1326
1327 /*
1328 * add status line now, otherwise first window will be too big
1329 */
1330 if (count > 1)
1331 last_status(TRUE);
1332
1333#ifdef FEAT_AUTOCMD
1334 /*
1335 * Don't execute autocommands while creating the windows. Must do that
1336 * when putting the buffers in the windows.
1337 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001338 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339#endif
1340
1341 /* todo is number of windows left to create */
1342 for (todo = count - 1; todo > 0; --todo)
1343#ifdef FEAT_VERTSPLIT
1344 if (vertical)
1345 {
1346 if (win_split(curwin->w_width - (curwin->w_width - todo)
1347 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1348 break;
1349 }
1350 else
1351#endif
1352 {
1353 if (win_split(curwin->w_height - (curwin->w_height - todo
1354 * STATUS_HEIGHT) / (todo + 1)
1355 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1356 break;
1357 }
1358
1359#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001360 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361#endif
1362
1363 /* return actual number of windows */
1364 return (count - todo);
1365}
1366
1367/*
1368 * Exchange current and next window
1369 */
1370 static void
1371win_exchange(Prenum)
1372 long Prenum;
1373{
1374 frame_T *frp;
1375 frame_T *frp2;
1376 win_T *wp;
1377 win_T *wp2;
1378 int temp;
1379
1380 if (lastwin == firstwin) /* just one window */
1381 {
1382 beep_flush();
1383 return;
1384 }
1385
1386#ifdef FEAT_GUI
1387 need_mouse_correct = TRUE;
1388#endif
1389
1390 /*
1391 * find window to exchange with
1392 */
1393 if (Prenum)
1394 {
1395 frp = curwin->w_frame->fr_parent->fr_child;
1396 while (frp != NULL && --Prenum > 0)
1397 frp = frp->fr_next;
1398 }
1399 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1400 frp = curwin->w_frame->fr_next;
1401 else /* Swap last window in row/col with previous */
1402 frp = curwin->w_frame->fr_prev;
1403
1404 /* We can only exchange a window with another window, not with a frame
1405 * containing windows. */
1406 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1407 return;
1408 wp = frp->fr_win;
1409
1410/*
1411 * 1. remove curwin from the list. Remember after which window it was in wp2
1412 * 2. insert curwin before wp in the list
1413 * if wp != wp2
1414 * 3. remove wp from the list
1415 * 4. insert wp after wp2
1416 * 5. exchange the status line height and vsep width.
1417 */
1418 wp2 = curwin->w_prev;
1419 frp2 = curwin->w_frame->fr_prev;
1420 if (wp->w_prev != curwin)
1421 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001422 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 frame_remove(curwin->w_frame);
1424 win_append(wp->w_prev, curwin);
1425 frame_insert(frp, curwin->w_frame);
1426 }
1427 if (wp != wp2)
1428 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001429 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 frame_remove(wp->w_frame);
1431 win_append(wp2, wp);
1432 if (frp2 == NULL)
1433 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1434 else
1435 frame_append(frp2, wp->w_frame);
1436 }
1437 temp = curwin->w_status_height;
1438 curwin->w_status_height = wp->w_status_height;
1439 wp->w_status_height = temp;
1440#ifdef FEAT_VERTSPLIT
1441 temp = curwin->w_vsep_width;
1442 curwin->w_vsep_width = wp->w_vsep_width;
1443 wp->w_vsep_width = temp;
1444
1445 /* If the windows are not in the same frame, exchange the sizes to avoid
1446 * messing up the window layout. Otherwise fix the frame sizes. */
1447 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1448 {
1449 temp = curwin->w_height;
1450 curwin->w_height = wp->w_height;
1451 wp->w_height = temp;
1452 temp = curwin->w_width;
1453 curwin->w_width = wp->w_width;
1454 wp->w_width = temp;
1455 }
1456 else
1457 {
1458 frame_fix_height(curwin);
1459 frame_fix_height(wp);
1460 frame_fix_width(curwin);
1461 frame_fix_width(wp);
1462 }
1463#endif
1464
1465 (void)win_comp_pos(); /* recompute window positions */
1466
1467 win_enter(wp, TRUE);
1468 redraw_later(CLEAR);
1469}
1470
1471/*
1472 * rotate windows: if upwards TRUE the second window becomes the first one
1473 * if upwards FALSE the first window becomes the second one
1474 */
1475 static void
1476win_rotate(upwards, count)
1477 int upwards;
1478 int count;
1479{
1480 win_T *wp1;
1481 win_T *wp2;
1482 frame_T *frp;
1483 int n;
1484
1485 if (firstwin == lastwin) /* nothing to do */
1486 {
1487 beep_flush();
1488 return;
1489 }
1490
1491#ifdef FEAT_GUI
1492 need_mouse_correct = TRUE;
1493#endif
1494
1495#ifdef FEAT_VERTSPLIT
1496 /* Check if all frames in this row/col have one window. */
1497 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1498 frp = frp->fr_next)
1499 if (frp->fr_win == NULL)
1500 {
1501 EMSG(_("E443: Cannot rotate when another window is split"));
1502 return;
1503 }
1504#endif
1505
1506 while (count--)
1507 {
1508 if (upwards) /* first window becomes last window */
1509 {
1510 /* remove first window/frame from the list */
1511 frp = curwin->w_frame->fr_parent->fr_child;
1512 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001513 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 frame_remove(frp);
1515
1516 /* find last frame and append removed window/frame after it */
1517 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1518 ;
1519 win_append(frp->fr_win, wp1);
1520 frame_append(frp, wp1->w_frame);
1521
1522 wp2 = frp->fr_win; /* previously last window */
1523 }
1524 else /* last window becomes first window */
1525 {
1526 /* find last window/frame in the list and remove it */
1527 for (frp = curwin->w_frame; frp->fr_next != NULL;
1528 frp = frp->fr_next)
1529 ;
1530 wp1 = frp->fr_win;
1531 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001532 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 frame_remove(frp);
1534
1535 /* append the removed window/frame before the first in the list */
1536 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1537 frame_insert(frp->fr_parent->fr_child, frp);
1538 }
1539
1540 /* exchange status height and vsep width of old and new last window */
1541 n = wp2->w_status_height;
1542 wp2->w_status_height = wp1->w_status_height;
1543 wp1->w_status_height = n;
1544 frame_fix_height(wp1);
1545 frame_fix_height(wp2);
1546#ifdef FEAT_VERTSPLIT
1547 n = wp2->w_vsep_width;
1548 wp2->w_vsep_width = wp1->w_vsep_width;
1549 wp1->w_vsep_width = n;
1550 frame_fix_width(wp1);
1551 frame_fix_width(wp2);
1552#endif
1553
1554 /* recompute w_winrow and w_wincol for all windows */
1555 (void)win_comp_pos();
1556 }
1557
1558 redraw_later(CLEAR);
1559}
1560
1561/*
1562 * Move the current window to the very top/bottom/left/right of the screen.
1563 */
1564 static void
1565win_totop(size, flags)
1566 int size;
1567 int flags;
1568{
1569 int dir;
1570 int height = curwin->w_height;
1571
1572 if (lastwin == firstwin)
1573 {
1574 beep_flush();
1575 return;
1576 }
1577
1578 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001579 (void)winframe_remove(curwin, &dir, NULL);
1580 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 last_status(FALSE); /* may need to remove last status line */
1582 (void)win_comp_pos(); /* recompute window positions */
1583
1584 /* Split a window on the desired side and put the window there. */
1585 (void)win_split_ins(size, flags, curwin, dir);
1586 if (!(flags & WSP_VERT))
1587 {
1588 win_setheight(height);
1589 if (p_ea)
1590 win_equal(curwin, TRUE, 'v');
1591 }
1592
1593#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1594 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1595 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001596 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598}
1599
1600/*
1601 * Move window "win1" to below/right of "win2" and make "win1" the current
1602 * window. Only works within the same frame!
1603 */
1604 void
1605win_move_after(win1, win2)
1606 win_T *win1, *win2;
1607{
1608 int height;
1609
1610 /* check if the arguments are reasonable */
1611 if (win1 == win2)
1612 return;
1613
1614 /* check if there is something to do */
1615 if (win2->w_next != win1)
1616 {
1617 /* may need move the status line/vertical separator of the last window
1618 * */
1619 if (win1 == lastwin)
1620 {
1621 height = win1->w_prev->w_status_height;
1622 win1->w_prev->w_status_height = win1->w_status_height;
1623 win1->w_status_height = height;
1624#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001625 if (win1->w_prev->w_vsep_width == 1)
1626 {
1627 /* Remove the vertical separator from the last-but-one window,
1628 * add it to the last window. Adjust the frame widths. */
1629 win1->w_prev->w_vsep_width = 0;
1630 win1->w_prev->w_frame->fr_width -= 1;
1631 win1->w_vsep_width = 1;
1632 win1->w_frame->fr_width += 1;
1633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634#endif
1635 }
1636 else if (win2 == lastwin)
1637 {
1638 height = win1->w_status_height;
1639 win1->w_status_height = win2->w_status_height;
1640 win2->w_status_height = height;
1641#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001642 if (win1->w_vsep_width == 1)
1643 {
1644 /* Remove the vertical separator from win1, add it to the last
1645 * window, win2. Adjust the frame widths. */
1646 win2->w_vsep_width = 1;
1647 win2->w_frame->fr_width += 1;
1648 win1->w_vsep_width = 0;
1649 win1->w_frame->fr_width -= 1;
1650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651#endif
1652 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001653 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 frame_remove(win1->w_frame);
1655 win_append(win2, win1);
1656 frame_append(win2->w_frame, win1->w_frame);
1657
1658 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1659 redraw_later(NOT_VALID);
1660 }
1661 win_enter(win1, FALSE);
1662}
1663
1664/*
1665 * Make all windows the same height.
1666 * 'next_curwin' will soon be the current window, make sure it has enough
1667 * rows.
1668 */
1669 void
1670win_equal(next_curwin, current, dir)
1671 win_T *next_curwin; /* pointer to current window to be or NULL */
1672 int current; /* do only frame with current window */
1673 int dir; /* 'v' for vertically, 'h' for horizontally,
1674 'b' for both, 0 for using p_ead */
1675{
1676 if (dir == 0)
1677#ifdef FEAT_VERTSPLIT
1678 dir = *p_ead;
1679#else
1680 dir = 'b';
1681#endif
1682 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001683 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001684 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685}
1686
1687/*
1688 * Set a frame to a new position and height, spreading the available room
1689 * equally over contained frames.
1690 * The window "next_curwin" (if not NULL) should at least get the size from
1691 * 'winheight' and 'winwidth' if possible.
1692 */
1693 static void
1694win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1695 win_T *next_curwin; /* pointer to current window to be or NULL */
1696 int current; /* do only frame with current window */
1697 frame_T *topfr; /* frame to set size off */
1698 int dir; /* 'v', 'h' or 'b', see win_equal() */
1699 int col; /* horizontal position for frame */
1700 int row; /* vertical position for frame */
1701 int width; /* new width of frame */
1702 int height; /* new height of frame */
1703{
1704 int n, m;
1705 int extra_sep = 0;
1706 int wincount, totwincount = 0;
1707 frame_T *fr;
1708 int next_curwin_size = 0;
1709 int room = 0;
1710 int new_size;
1711 int has_next_curwin = 0;
1712 int hnc;
1713
1714 if (topfr->fr_layout == FR_LEAF)
1715 {
1716 /* Set the width/height of this frame.
1717 * Redraw when size or position changes */
1718 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1719#ifdef FEAT_VERTSPLIT
1720 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1721#endif
1722 )
1723 {
1724 topfr->fr_win->w_winrow = row;
1725 frame_new_height(topfr, height, FALSE, FALSE);
1726#ifdef FEAT_VERTSPLIT
1727 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001728 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729#endif
1730 redraw_all_later(CLEAR);
1731 }
1732 }
1733#ifdef FEAT_VERTSPLIT
1734 else if (topfr->fr_layout == FR_ROW)
1735 {
1736 topfr->fr_width = width;
1737 topfr->fr_height = height;
1738
1739 if (dir != 'v') /* equalize frame widths */
1740 {
1741 /* Compute the maximum number of windows horizontally in this
1742 * frame. */
1743 n = frame_minwidth(topfr, NOWIN);
1744 /* add one for the rightmost window, it doesn't have a separator */
1745 if (col + width == Columns)
1746 extra_sep = 1;
1747 else
1748 extra_sep = 0;
1749 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001750 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001752 /*
1753 * Compute width for "next_curwin" window and room available for
1754 * other windows.
1755 * "m" is the minimal width when counting p_wiw for "next_curwin".
1756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 m = frame_minwidth(topfr, next_curwin);
1758 room = width - m;
1759 if (room < 0)
1760 {
1761 next_curwin_size = p_wiw + room;
1762 room = 0;
1763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 else
1765 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001766 next_curwin_size = -1;
1767 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1768 {
1769 /* If 'winfixwidth' set keep the window width if
1770 * possible.
1771 * Watch out for this window being the next_curwin. */
1772 if (frame_fixed_width(fr))
1773 {
1774 n = frame_minwidth(fr, NOWIN);
1775 new_size = fr->fr_width;
1776 if (frame_has_win(fr, next_curwin))
1777 {
1778 room += p_wiw - p_wmw;
1779 next_curwin_size = 0;
1780 if (new_size < p_wiw)
1781 new_size = p_wiw;
1782 }
1783 else
1784 /* These windows don't use up room. */
1785 totwincount -= (n + (fr->fr_next == NULL
1786 ? extra_sep : 0)) / (p_wmw + 1);
1787 room -= new_size - n;
1788 if (room < 0)
1789 {
1790 new_size += room;
1791 room = 0;
1792 }
1793 fr->fr_newwidth = new_size;
1794 }
1795 }
1796 if (next_curwin_size == -1)
1797 {
1798 if (!has_next_curwin)
1799 next_curwin_size = 0;
1800 else if (totwincount > 1
1801 && (room + (totwincount - 2))
1802 / (totwincount - 1) > p_wiw)
1803 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001804 /* Can make all windows wider than 'winwidth', spread
1805 * the room equally. */
1806 next_curwin_size = (room + p_wiw
1807 + (totwincount - 1) * p_wmw
1808 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001809 room -= next_curwin_size - p_wiw;
1810 }
1811 else
1812 next_curwin_size = p_wiw;
1813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001815
1816 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 --totwincount; /* don't count curwin */
1818 }
1819
1820 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1821 {
1822 n = m = 0;
1823 wincount = 1;
1824 if (fr->fr_next == NULL)
1825 /* last frame gets all that remains (avoid roundoff error) */
1826 new_size = width;
1827 else if (dir == 'v')
1828 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001829 else if (frame_fixed_width(fr))
1830 {
1831 new_size = fr->fr_newwidth;
1832 wincount = 0; /* doesn't count as a sizeable window */
1833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 else
1835 {
1836 /* Compute the maximum number of windows horiz. in "fr". */
1837 n = frame_minwidth(fr, NOWIN);
1838 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1839 / (p_wmw + 1);
1840 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001841 if (has_next_curwin)
1842 hnc = frame_has_win(fr, next_curwin);
1843 else
1844 hnc = FALSE;
1845 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001847 if (totwincount == 0)
1848 new_size = room;
1849 else
1850 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001852 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
1854 next_curwin_size -= p_wiw - (m - n);
1855 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001856 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001858 else
1859 room -= new_size;
1860 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 }
1862
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001863 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 * window, unless equalizing all frames. */
1865 if (!current || dir != 'v' || topfr->fr_parent != NULL
1866 || (new_size != fr->fr_width)
1867 || frame_has_win(fr, next_curwin))
1868 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001869 new_size, height);
1870 col += new_size;
1871 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 totwincount -= wincount;
1873 }
1874 }
1875#endif
1876 else /* topfr->fr_layout == FR_COL */
1877 {
1878#ifdef FEAT_VERTSPLIT
1879 topfr->fr_width = width;
1880#endif
1881 topfr->fr_height = height;
1882
1883 if (dir != 'h') /* equalize frame heights */
1884 {
1885 /* Compute maximum number of windows vertically in this frame. */
1886 n = frame_minheight(topfr, NOWIN);
1887 /* add one for the bottom window if it doesn't have a statusline */
1888 if (row + height == cmdline_row && p_ls == 0)
1889 extra_sep = 1;
1890 else
1891 extra_sep = 0;
1892 totwincount = (n + extra_sep) / (p_wmh + 1);
1893 has_next_curwin = frame_has_win(topfr, next_curwin);
1894
1895 /*
1896 * Compute height for "next_curwin" window and room available for
1897 * other windows.
1898 * "m" is the minimal height when counting p_wh for "next_curwin".
1899 */
1900 m = frame_minheight(topfr, next_curwin);
1901 room = height - m;
1902 if (room < 0)
1903 {
1904 /* The room is less then 'winheight', use all space for the
1905 * current window. */
1906 next_curwin_size = p_wh + room;
1907 room = 0;
1908 }
1909 else
1910 {
1911 next_curwin_size = -1;
1912 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1913 {
1914 /* If 'winfixheight' set keep the window height if
1915 * possible.
1916 * Watch out for this window being the next_curwin. */
1917 if (frame_fixed_height(fr))
1918 {
1919 n = frame_minheight(fr, NOWIN);
1920 new_size = fr->fr_height;
1921 if (frame_has_win(fr, next_curwin))
1922 {
1923 room += p_wh - p_wmh;
1924 next_curwin_size = 0;
1925 if (new_size < p_wh)
1926 new_size = p_wh;
1927 }
1928 else
1929 /* These windows don't use up room. */
1930 totwincount -= (n + (fr->fr_next == NULL
1931 ? extra_sep : 0)) / (p_wmh + 1);
1932 room -= new_size - n;
1933 if (room < 0)
1934 {
1935 new_size += room;
1936 room = 0;
1937 }
1938 fr->fr_newheight = new_size;
1939 }
1940 }
1941 if (next_curwin_size == -1)
1942 {
1943 if (!has_next_curwin)
1944 next_curwin_size = 0;
1945 else if (totwincount > 1
1946 && (room + (totwincount - 2))
1947 / (totwincount - 1) > p_wh)
1948 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001949 /* can make all windows higher than 'winheight',
1950 * spread the room equally. */
1951 next_curwin_size = (room + p_wh
1952 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 + (totwincount - 1)) / totwincount;
1954 room -= next_curwin_size - p_wh;
1955 }
1956 else
1957 next_curwin_size = p_wh;
1958 }
1959 }
1960
1961 if (has_next_curwin)
1962 --totwincount; /* don't count curwin */
1963 }
1964
1965 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1966 {
1967 n = m = 0;
1968 wincount = 1;
1969 if (fr->fr_next == NULL)
1970 /* last frame gets all that remains (avoid roundoff error) */
1971 new_size = height;
1972 else if (dir == 'h')
1973 new_size = fr->fr_height;
1974 else if (frame_fixed_height(fr))
1975 {
1976 new_size = fr->fr_newheight;
1977 wincount = 0; /* doesn't count as a sizeable window */
1978 }
1979 else
1980 {
1981 /* Compute the maximum number of windows vert. in "fr". */
1982 n = frame_minheight(fr, NOWIN);
1983 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1984 / (p_wmh + 1);
1985 m = frame_minheight(fr, next_curwin);
1986 if (has_next_curwin)
1987 hnc = frame_has_win(fr, next_curwin);
1988 else
1989 hnc = FALSE;
1990 if (hnc) /* don't count next_curwin */
1991 --wincount;
1992 if (totwincount == 0)
1993 new_size = room;
1994 else
1995 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1996 / totwincount;
1997 if (hnc) /* add next_curwin size */
1998 {
1999 next_curwin_size -= p_wh - (m - n);
2000 new_size += next_curwin_size;
2001 room -= new_size - next_curwin_size;
2002 }
2003 else
2004 room -= new_size;
2005 new_size += n;
2006 }
2007 /* Skip frame that is full width when splitting or closing a
2008 * window, unless equalizing all frames. */
2009 if (!current || dir != 'h' || topfr->fr_parent != NULL
2010 || (new_size != fr->fr_height)
2011 || frame_has_win(fr, next_curwin))
2012 win_equal_rec(next_curwin, current, fr, dir, col, row,
2013 width, new_size);
2014 row += new_size;
2015 height -= new_size;
2016 totwincount -= wincount;
2017 }
2018 }
2019}
2020
2021/*
2022 * close all windows for buffer 'buf'
2023 */
2024 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002025close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002027 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002029 win_T *wp;
2030 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002031 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032
2033 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002034
2035 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002037 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2038#ifdef FEAT_AUTOCMD
2039 && !(wp->w_closing || wp->w_buffer->b_closing)
2040#endif
2041 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002043 win_close(wp, FALSE);
2044
2045 /* Start all over, autocommands may change the window layout. */
2046 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 }
2048 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002049 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002051
2052 /* Also check windows in other tab pages. */
2053 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2054 {
2055 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002056 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002057 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002058 if (wp->w_buffer == buf
2059#ifdef FEAT_AUTOCMD
2060 && !(wp->w_closing || wp->w_buffer->b_closing)
2061#endif
2062 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002063 {
2064 win_close_othertab(wp, FALSE, tp);
2065
2066 /* Start all over, the tab page may be closed and
2067 * autocommands may change the window layout. */
2068 nexttp = first_tabpage;
2069 break;
2070 }
2071 }
2072
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002074
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002075 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002076 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077}
2078
2079/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002080 * Return TRUE if the current window is the only window that exists (ignoring
2081 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002082 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002083 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002084 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002085last_window()
2086{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002087 return (one_window() && first_tabpage->tp_next == NULL);
2088}
2089
2090/*
2091 * Return TRUE if there is only one window other than "aucmd_win" in the
2092 * current tab page.
2093 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002094 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002095one_window()
2096{
2097#ifdef FEAT_AUTOCMD
2098 win_T *wp;
2099 int seen_one = FALSE;
2100
2101 FOR_ALL_WINDOWS(wp)
2102 {
2103 if (wp != aucmd_win)
2104 {
2105 if (seen_one)
2106 return FALSE;
2107 seen_one = TRUE;
2108 }
2109 }
2110 return TRUE;
2111#else
2112 return firstwin == lastwin;
2113#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002114}
2115
2116/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002117 * Close the possibly last window in a tab page.
2118 * Returns TRUE when the window was closed already.
2119 */
2120 static int
2121close_last_window_tabpage(win, free_buf, prev_curtab)
2122 win_T *win;
2123 int free_buf;
2124 tabpage_T *prev_curtab;
2125{
2126 if (firstwin == lastwin)
2127 {
2128 /*
2129 * Closing the last window in a tab page. First go to another tab
2130 * page and then close the window and the tab page. This avoids that
2131 * curwin and curtab are invalid while we are freeing memory, they may
2132 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002133 * Don't trigger autocommands yet, they may use wrong values, so do
2134 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002135 */
Bram Moolenaara8596c42012-06-13 14:28:20 +02002136 goto_tabpage_tp(alt_tabpage(), FALSE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002137 redraw_tabline = TRUE;
2138
2139 /* Safety check: Autocommands may have closed the window when jumping
2140 * to the other tab page. */
2141 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2142 {
2143 int h = tabline_height();
2144
2145 win_close_othertab(win, free_buf, prev_curtab);
2146 if (h != tabline_height())
2147 shell_new_rows();
2148 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002149 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2150 * that now. */
2151#ifdef FEAT_AUTOCMD
2152 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2153 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
2154#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002155 return TRUE;
2156 }
2157 return FALSE;
2158}
2159
2160/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002161 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 * If "free_buf" is TRUE related buffer may be unloaded.
2163 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002164 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 */
2166 void
2167win_close(win, free_buf)
2168 win_T *win;
2169 int free_buf;
2170{
2171 win_T *wp;
2172#ifdef FEAT_AUTOCMD
2173 int other_buffer = FALSE;
2174#endif
2175 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 int dir;
2177 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002178 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002180 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
2182 EMSG(_("E444: Cannot close last window"));
2183 return;
2184 }
2185
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002186#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002187 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002188 return; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002189 if (win == aucmd_win)
2190 {
2191 EMSG(_("E813: Cannot close autocmd window"));
2192 return;
2193 }
2194 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2195 {
2196 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2197 return;
2198 }
2199#endif
2200
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002201 /* When closing the last window in a tab page first go to another tab page
2202 * and then close the window and the tab page to avoid that curwin and
2203 * curtab are invalid while we are freeing memory. */
2204 if (close_last_window_tabpage(win, free_buf, prev_curtab))
2205 return;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002206
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 /* When closing the help window, try restoring a snapshot after closing
2208 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002209 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 help_window = TRUE;
2211 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002212 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213
2214#ifdef FEAT_AUTOCMD
2215 if (win == curwin)
2216 {
2217 /*
2218 * Guess which window is going to be the new current window.
2219 * This may change because of the autocommands (sigh).
2220 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002221 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002224 * Be careful: If autocommands delete the window or cause this window
2225 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 */
2227 if (wp->w_buffer != curbuf)
2228 {
2229 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002230 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002232 if (!win_valid(win))
2233 return;
2234 win->w_closing = FALSE;
2235 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 return;
2237 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002238 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002240 if (!win_valid(win))
2241 return;
2242 win->w_closing = FALSE;
2243 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 return;
2245# ifdef FEAT_EVAL
2246 /* autocmds may abort script processing */
2247 if (aborting())
2248 return;
2249# endif
2250 }
2251#endif
2252
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002253#ifdef FEAT_GUI
2254 /* Avoid trouble with scrollbars that are going to be deleted in
2255 * win_free(). */
2256 if (gui.in_use)
2257 out_flush();
2258#endif
2259
Bram Moolenaara971b822011-09-14 14:43:25 +02002260#ifdef FEAT_SYN_HL
2261 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002262 if (win->w_buffer != NULL)
2263 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002264#endif
2265
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 /*
2267 * Close the link to the buffer.
2268 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002269 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002270 {
2271#ifdef FEAT_AUTOCMD
2272 win->w_closing = TRUE;
2273#endif
2274 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
2275#ifdef FEAT_AUTOCMD
2276 if (win_valid(win))
2277 win->w_closing = FALSE;
2278#endif
2279 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002280
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002282 * other window or moved to another tab page. */
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002283 if (!win_valid(win) || last_window() || curtab != prev_curtab
2284 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 return;
2286
Bram Moolenaara971b822011-09-14 14:43:25 +02002287 /* Free the memory used for the window and get the window that received
2288 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002289 wp = win_free_mem(win, &dir, NULL);
2290
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 /* Make sure curwin isn't invalid. It can cause severe trouble when
2292 * printing an error message. For win_equal() curbuf needs to be valid
2293 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002294 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 curwin = wp;
2297#ifdef FEAT_QUICKFIX
2298 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2299 {
2300 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002301 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 * finding another window to go to.
2303 */
2304 for (;;)
2305 {
2306 if (wp->w_next == NULL)
2307 wp = firstwin;
2308 else
2309 wp = wp->w_next;
2310 if (wp == curwin)
2311 break;
2312 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2313 {
2314 curwin = wp;
2315 break;
2316 }
2317 }
2318 }
2319#endif
2320 curbuf = curwin->w_buffer;
2321 close_curwin = TRUE;
2322 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002323 if (p_ea
2324#ifdef FEAT_VERTSPLIT
2325 && (*p_ead == 'b' || *p_ead == dir)
2326#endif
2327 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 win_equal(curwin, TRUE,
2329#ifdef FEAT_VERTSPLIT
2330 dir
2331#else
2332 0
2333#endif
2334 );
2335 else
2336 win_comp_pos();
2337 if (close_curwin)
2338 {
2339 win_enter_ext(wp, FALSE, TRUE);
2340#ifdef FEAT_AUTOCMD
2341 if (other_buffer)
2342 /* careful: after this wp and win may be invalid! */
2343 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2344#endif
2345 }
2346
2347 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002348 * If last window has a status line now and we don't want one,
2349 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 */
2351 last_status(FALSE);
2352
2353 /* After closing the help window, try restoring the window layout from
2354 * before it was opened. */
2355 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002356 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2359 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2360 if (gui.in_use && !win_hasvertsplit())
2361 gui_init_which_components(NULL);
2362#endif
2363
2364 redraw_all_later(NOT_VALID);
2365}
2366
2367/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002368 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002369 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002370 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002371 * Caller must check if buffer is hidden and whether the tabline needs to be
2372 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002373 */
2374 void
2375win_close_othertab(win, free_buf, tp)
2376 win_T *win;
2377 int free_buf;
2378 tabpage_T *tp;
2379{
2380 win_T *wp;
2381 int dir;
2382 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002383 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002384
Bram Moolenaar362ce482012-06-06 19:02:45 +02002385#ifdef FEAT_AUTOCMD
2386 if (win->w_closing || win->w_buffer->b_closing)
2387 return; /* window is already being closed */
2388#endif
2389
Bram Moolenaarf740b292006-02-16 22:11:02 +00002390 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002391 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002392
2393 /* Careful: Autocommands may have closed the tab page or made it the
2394 * current tab page. */
2395 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2396 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002397 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002398 return;
2399
2400 /* Autocommands may have closed the window already. */
2401 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2402 ;
2403 if (wp == NULL)
2404 return;
2405
Bram Moolenaarf740b292006-02-16 22:11:02 +00002406 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002407 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002408 {
2409 if (tp == first_tabpage)
2410 first_tabpage = tp->tp_next;
2411 else
2412 {
2413 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2414 ptp = ptp->tp_next)
2415 ;
2416 if (ptp == NULL)
2417 {
2418 EMSG2(_(e_intern2), "win_close_othertab()");
2419 return;
2420 }
2421 ptp->tp_next = tp->tp_next;
2422 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002423 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002424 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002425
2426 /* Free the memory used for the window. */
2427 win_free_mem(win, &dir, tp);
2428
2429 if (free_tp)
2430 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002431}
2432
2433/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002434 * Free the memory used for a window.
2435 * Returns a pointer to the window that got the freed up space.
2436 */
2437 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002438win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002439 win_T *win;
2440 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002441 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002442{
2443 frame_T *frp;
2444 win_T *wp;
2445
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002446 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002447 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002448 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002449 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002450 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002451
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002452 /* When deleting the current window of another tab page select a new
2453 * current window. */
2454 if (tp != NULL && win == tp->tp_curwin)
2455 tp->tp_curwin = wp;
2456
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002457 return wp;
2458}
2459
2460#if defined(EXITFREE) || defined(PROTO)
2461 void
2462win_free_all()
2463{
2464 int dummy;
2465
Bram Moolenaarf740b292006-02-16 22:11:02 +00002466# ifdef FEAT_WINDOWS
2467 while (first_tabpage->tp_next != NULL)
2468 tabpage_close(TRUE);
2469# endif
2470
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002471# ifdef FEAT_AUTOCMD
2472 if (aucmd_win != NULL)
2473 {
2474 (void)win_free_mem(aucmd_win, &dummy, NULL);
2475 aucmd_win = NULL;
2476 }
2477# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002478
2479 while (firstwin != NULL)
2480 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002481}
2482#endif
2483
2484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 * Remove a window and its frame from the tree of frames.
2486 * Returns a pointer to the window that got the freed up space.
2487 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002488 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002489winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002491 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002492 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
2494 frame_T *frp, *frp2, *frp3;
2495 frame_T *frp_close = win->w_frame;
2496 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
2498 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002499 * If there is only one window there is nothing to remove.
2500 */
2501 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2502 return NULL;
2503
2504 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 * Remove the window from its frame.
2506 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002507 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 wp = frame2win(frp2);
2509
2510 /* Remove this frame from the list of frames. */
2511 frame_remove(frp_close);
2512
2513#ifdef FEAT_VERTSPLIT
2514 if (frp_close->fr_parent->fr_layout == FR_COL)
2515 {
2516#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002517 /* When 'winfixheight' is set, try to find another frame in the column
2518 * (as close to the closed frame as possible) to distribute the height
2519 * to. */
2520 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2521 {
2522 frp = frp_close->fr_prev;
2523 frp3 = frp_close->fr_next;
2524 while (frp != NULL || frp3 != NULL)
2525 {
2526 if (frp != NULL)
2527 {
2528 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2529 {
2530 frp2 = frp;
2531 wp = frp->fr_win;
2532 break;
2533 }
2534 frp = frp->fr_prev;
2535 }
2536 if (frp3 != NULL)
2537 {
2538 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2539 {
2540 frp2 = frp3;
2541 wp = frp3->fr_win;
2542 break;
2543 }
2544 frp3 = frp3->fr_next;
2545 }
2546 }
2547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2549 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550#ifdef FEAT_VERTSPLIT
2551 *dirp = 'v';
2552 }
2553 else
2554 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002555 /* When 'winfixwidth' is set, try to find another frame in the column
2556 * (as close to the closed frame as possible) to distribute the width
2557 * to. */
2558 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2559 {
2560 frp = frp_close->fr_prev;
2561 frp3 = frp_close->fr_next;
2562 while (frp != NULL || frp3 != NULL)
2563 {
2564 if (frp != NULL)
2565 {
2566 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2567 {
2568 frp2 = frp;
2569 wp = frp->fr_win;
2570 break;
2571 }
2572 frp = frp->fr_prev;
2573 }
2574 if (frp3 != NULL)
2575 {
2576 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2577 {
2578 frp2 = frp3;
2579 wp = frp3->fr_win;
2580 break;
2581 }
2582 frp3 = frp3->fr_next;
2583 }
2584 }
2585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002587 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 *dirp = 'h';
2589 }
2590#endif
2591
2592 /* If rows/columns go to a window below/right its positions need to be
2593 * updated. Can only be done after the sizes have been updated. */
2594 if (frp2 == frp_close->fr_next)
2595 {
2596 int row = win->w_winrow;
2597 int col = W_WINCOL(win);
2598
2599 frame_comp_pos(frp2, &row, &col);
2600 }
2601
2602 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2603 {
2604 /* There is no other frame in this list, move its info to the parent
2605 * and remove it. */
2606 frp2->fr_parent->fr_layout = frp2->fr_layout;
2607 frp2->fr_parent->fr_child = frp2->fr_child;
2608 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2609 frp->fr_parent = frp2->fr_parent;
2610 frp2->fr_parent->fr_win = frp2->fr_win;
2611 if (frp2->fr_win != NULL)
2612 frp2->fr_win->w_frame = frp2->fr_parent;
2613 frp = frp2->fr_parent;
2614 vim_free(frp2);
2615
2616 frp2 = frp->fr_parent;
2617 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2618 {
2619 /* The frame above the parent has the same layout, have to merge
2620 * the frames into this list. */
2621 if (frp2->fr_child == frp)
2622 frp2->fr_child = frp->fr_child;
2623 frp->fr_child->fr_prev = frp->fr_prev;
2624 if (frp->fr_prev != NULL)
2625 frp->fr_prev->fr_next = frp->fr_child;
2626 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2627 {
2628 frp3->fr_parent = frp2;
2629 if (frp3->fr_next == NULL)
2630 {
2631 frp3->fr_next = frp->fr_next;
2632 if (frp->fr_next != NULL)
2633 frp->fr_next->fr_prev = frp3;
2634 break;
2635 }
2636 }
2637 vim_free(frp);
2638 }
2639 }
2640
2641 return wp;
2642}
2643
2644/*
2645 * Find out which frame is going to get the freed up space when "win" is
2646 * closed.
2647 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2648 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2649 * This makes opening a window and closing it immediately keep the same window
2650 * layout.
2651 */
2652 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002653win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002655 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
2657 frame_T *frp;
2658 int b;
2659
Bram Moolenaarf740b292006-02-16 22:11:02 +00002660 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002661 /* Last window in this tab page, will go to next tab page. */
2662 return alt_tabpage()->tp_curwin->w_frame;
2663
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 frp = win->w_frame;
2665#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002666 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 b = p_spr;
2668 else
2669#endif
2670 b = p_sb;
2671 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2672 return frp->fr_next;
2673 return frp->fr_prev;
2674}
2675
2676/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002677 * Return the tabpage that will be used if the current one is closed.
2678 */
2679 static tabpage_T *
2680alt_tabpage()
2681{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002682 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002683
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002684 /* Use the next tab page if possible. */
2685 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002686 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002687
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002688 /* Find the last but one tab page. */
2689 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2690 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002691 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002692}
2693
2694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 * Find the left-upper window in frame "frp".
2696 */
2697 static win_T *
2698frame2win(frp)
2699 frame_T *frp;
2700{
2701 while (frp->fr_win == NULL)
2702 frp = frp->fr_child;
2703 return frp->fr_win;
2704}
2705
2706/*
2707 * Return TRUE if frame "frp" contains window "wp".
2708 */
2709 static int
2710frame_has_win(frp, wp)
2711 frame_T *frp;
2712 win_T *wp;
2713{
2714 frame_T *p;
2715
2716 if (frp->fr_layout == FR_LEAF)
2717 return frp->fr_win == wp;
2718
2719 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2720 if (frame_has_win(p, wp))
2721 return TRUE;
2722 return FALSE;
2723}
2724
2725/*
2726 * Set a new height for a frame. Recursively sets the height for contained
2727 * frames and windows. Caller must take care of positions.
2728 */
2729 static void
2730frame_new_height(topfrp, height, topfirst, wfh)
2731 frame_T *topfrp;
2732 int height;
2733 int topfirst; /* resize topmost contained frame first */
2734 int wfh; /* obey 'winfixheight' when there is a choice;
2735 may cause the height not to be set */
2736{
2737 frame_T *frp;
2738 int extra_lines;
2739 int h;
2740
2741 if (topfrp->fr_win != NULL)
2742 {
2743 /* Simple case: just one window. */
2744 win_new_height(topfrp->fr_win,
2745 height - topfrp->fr_win->w_status_height);
2746 }
2747#ifdef FEAT_VERTSPLIT
2748 else if (topfrp->fr_layout == FR_ROW)
2749 {
2750 do
2751 {
2752 /* All frames in this row get the same new height. */
2753 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2754 {
2755 frame_new_height(frp, height, topfirst, wfh);
2756 if (frp->fr_height > height)
2757 {
2758 /* Could not fit the windows, make the whole row higher. */
2759 height = frp->fr_height;
2760 break;
2761 }
2762 }
2763 }
2764 while (frp != NULL);
2765 }
2766#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002767 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
2769 /* Complicated case: Resize a column of frames. Resize the bottom
2770 * frame first, frames above that when needed. */
2771
2772 frp = topfrp->fr_child;
2773 if (wfh)
2774 /* Advance past frames with one window with 'wfh' set. */
2775 while (frame_fixed_height(frp))
2776 {
2777 frp = frp->fr_next;
2778 if (frp == NULL)
2779 return; /* no frame without 'wfh', give up */
2780 }
2781 if (!topfirst)
2782 {
2783 /* Find the bottom frame of this column */
2784 while (frp->fr_next != NULL)
2785 frp = frp->fr_next;
2786 if (wfh)
2787 /* Advance back for frames with one window with 'wfh' set. */
2788 while (frame_fixed_height(frp))
2789 frp = frp->fr_prev;
2790 }
2791
2792 extra_lines = height - topfrp->fr_height;
2793 if (extra_lines < 0)
2794 {
2795 /* reduce height of contained frames, bottom or top frame first */
2796 while (frp != NULL)
2797 {
2798 h = frame_minheight(frp, NULL);
2799 if (frp->fr_height + extra_lines < h)
2800 {
2801 extra_lines += frp->fr_height - h;
2802 frame_new_height(frp, h, topfirst, wfh);
2803 }
2804 else
2805 {
2806 frame_new_height(frp, frp->fr_height + extra_lines,
2807 topfirst, wfh);
2808 break;
2809 }
2810 if (topfirst)
2811 {
2812 do
2813 frp = frp->fr_next;
2814 while (wfh && frp != NULL && frame_fixed_height(frp));
2815 }
2816 else
2817 {
2818 do
2819 frp = frp->fr_prev;
2820 while (wfh && frp != NULL && frame_fixed_height(frp));
2821 }
2822 /* Increase "height" if we could not reduce enough frames. */
2823 if (frp == NULL)
2824 height -= extra_lines;
2825 }
2826 }
2827 else if (extra_lines > 0)
2828 {
2829 /* increase height of bottom or top frame */
2830 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2831 }
2832 }
2833 topfrp->fr_height = height;
2834}
2835
2836/*
2837 * Return TRUE if height of frame "frp" should not be changed because of
2838 * the 'winfixheight' option.
2839 */
2840 static int
2841frame_fixed_height(frp)
2842 frame_T *frp;
2843{
2844 /* frame with one window: fixed height if 'winfixheight' set. */
2845 if (frp->fr_win != NULL)
2846 return frp->fr_win->w_p_wfh;
2847
2848 if (frp->fr_layout == FR_ROW)
2849 {
2850 /* The frame is fixed height if one of the frames in the row is fixed
2851 * height. */
2852 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2853 if (frame_fixed_height(frp))
2854 return TRUE;
2855 return FALSE;
2856 }
2857
2858 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2859 * frames in the row are fixed height. */
2860 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2861 if (!frame_fixed_height(frp))
2862 return FALSE;
2863 return TRUE;
2864}
2865
2866#ifdef FEAT_VERTSPLIT
2867/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002868 * Return TRUE if width of frame "frp" should not be changed because of
2869 * the 'winfixwidth' option.
2870 */
2871 static int
2872frame_fixed_width(frp)
2873 frame_T *frp;
2874{
2875 /* frame with one window: fixed width if 'winfixwidth' set. */
2876 if (frp->fr_win != NULL)
2877 return frp->fr_win->w_p_wfw;
2878
2879 if (frp->fr_layout == FR_COL)
2880 {
2881 /* The frame is fixed width if one of the frames in the row is fixed
2882 * width. */
2883 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2884 if (frame_fixed_width(frp))
2885 return TRUE;
2886 return FALSE;
2887 }
2888
2889 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2890 * frames in the row are fixed width. */
2891 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2892 if (!frame_fixed_width(frp))
2893 return FALSE;
2894 return TRUE;
2895}
2896
2897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 * Add a status line to windows at the bottom of "frp".
2899 * Note: Does not check if there is room!
2900 */
2901 static void
2902frame_add_statusline(frp)
2903 frame_T *frp;
2904{
2905 win_T *wp;
2906
2907 if (frp->fr_layout == FR_LEAF)
2908 {
2909 wp = frp->fr_win;
2910 if (wp->w_status_height == 0)
2911 {
2912 if (wp->w_height > 0) /* don't make it negative */
2913 --wp->w_height;
2914 wp->w_status_height = STATUS_HEIGHT;
2915 }
2916 }
2917 else if (frp->fr_layout == FR_ROW)
2918 {
2919 /* Handle all the frames in the row. */
2920 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2921 frame_add_statusline(frp);
2922 }
2923 else /* frp->fr_layout == FR_COL */
2924 {
2925 /* Only need to handle the last frame in the column. */
2926 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2927 ;
2928 frame_add_statusline(frp);
2929 }
2930}
2931
2932/*
2933 * Set width of a frame. Handles recursively going through contained frames.
2934 * May remove separator line for windows at the right side (for win_close()).
2935 */
2936 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002937frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 frame_T *topfrp;
2939 int width;
2940 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002941 int wfw; /* obey 'winfixwidth' when there is a choice;
2942 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943{
2944 frame_T *frp;
2945 int extra_cols;
2946 int w;
2947 win_T *wp;
2948
2949 if (topfrp->fr_layout == FR_LEAF)
2950 {
2951 /* Simple case: just one window. */
2952 wp = topfrp->fr_win;
2953 /* Find out if there are any windows right of this one. */
2954 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2955 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2956 break;
2957 if (frp->fr_parent == NULL)
2958 wp->w_vsep_width = 0;
2959 win_new_width(wp, width - wp->w_vsep_width);
2960 }
2961 else if (topfrp->fr_layout == FR_COL)
2962 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002963 do
2964 {
2965 /* All frames in this column get the same new width. */
2966 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2967 {
2968 frame_new_width(frp, width, leftfirst, wfw);
2969 if (frp->fr_width > width)
2970 {
2971 /* Could not fit the windows, make whole column wider. */
2972 width = frp->fr_width;
2973 break;
2974 }
2975 }
2976 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 }
2978 else /* fr_layout == FR_ROW */
2979 {
2980 /* Complicated case: Resize a row of frames. Resize the rightmost
2981 * frame first, frames left of it when needed. */
2982
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002984 if (wfw)
2985 /* Advance past frames with one window with 'wfw' set. */
2986 while (frame_fixed_width(frp))
2987 {
2988 frp = frp->fr_next;
2989 if (frp == NULL)
2990 return; /* no frame without 'wfw', give up */
2991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002993 {
2994 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 while (frp->fr_next != NULL)
2996 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002997 if (wfw)
2998 /* Advance back for frames with one window with 'wfw' set. */
2999 while (frame_fixed_width(frp))
3000 frp = frp->fr_prev;
3001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
3003 extra_cols = width - topfrp->fr_width;
3004 if (extra_cols < 0)
3005 {
3006 /* reduce frame width, rightmost frame first */
3007 while (frp != NULL)
3008 {
3009 w = frame_minwidth(frp, NULL);
3010 if (frp->fr_width + extra_cols < w)
3011 {
3012 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003013 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 }
3015 else
3016 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003017 frame_new_width(frp, frp->fr_width + extra_cols,
3018 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 break;
3020 }
3021 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003022 {
3023 do
3024 frp = frp->fr_next;
3025 while (wfw && frp != NULL && frame_fixed_width(frp));
3026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003028 {
3029 do
3030 frp = frp->fr_prev;
3031 while (wfw && frp != NULL && frame_fixed_width(frp));
3032 }
3033 /* Increase "width" if we could not reduce enough frames. */
3034 if (frp == NULL)
3035 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 }
3037 }
3038 else if (extra_cols > 0)
3039 {
3040 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003041 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
3043 }
3044 topfrp->fr_width = width;
3045}
3046
3047/*
3048 * Add the vertical separator to windows at the right side of "frp".
3049 * Note: Does not check if there is room!
3050 */
3051 static void
3052frame_add_vsep(frp)
3053 frame_T *frp;
3054{
3055 win_T *wp;
3056
3057 if (frp->fr_layout == FR_LEAF)
3058 {
3059 wp = frp->fr_win;
3060 if (wp->w_vsep_width == 0)
3061 {
3062 if (wp->w_width > 0) /* don't make it negative */
3063 --wp->w_width;
3064 wp->w_vsep_width = 1;
3065 }
3066 }
3067 else if (frp->fr_layout == FR_COL)
3068 {
3069 /* Handle all the frames in the column. */
3070 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3071 frame_add_vsep(frp);
3072 }
3073 else /* frp->fr_layout == FR_ROW */
3074 {
3075 /* Only need to handle the last frame in the row. */
3076 frp = frp->fr_child;
3077 while (frp->fr_next != NULL)
3078 frp = frp->fr_next;
3079 frame_add_vsep(frp);
3080 }
3081}
3082
3083/*
3084 * Set frame width from the window it contains.
3085 */
3086 static void
3087frame_fix_width(wp)
3088 win_T *wp;
3089{
3090 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3091}
3092#endif
3093
3094/*
3095 * Set frame height from the window it contains.
3096 */
3097 static void
3098frame_fix_height(wp)
3099 win_T *wp;
3100{
3101 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3102}
3103
3104/*
3105 * Compute the minimal height for frame "topfrp".
3106 * Uses the 'winminheight' option.
3107 * When "next_curwin" isn't NULL, use p_wh for this window.
3108 * When "next_curwin" is NOWIN, don't use at least one line for the current
3109 * window.
3110 */
3111 static int
3112frame_minheight(topfrp, next_curwin)
3113 frame_T *topfrp;
3114 win_T *next_curwin;
3115{
3116 frame_T *frp;
3117 int m;
3118#ifdef FEAT_VERTSPLIT
3119 int n;
3120#endif
3121
3122 if (topfrp->fr_win != NULL)
3123 {
3124 if (topfrp->fr_win == next_curwin)
3125 m = p_wh + topfrp->fr_win->w_status_height;
3126 else
3127 {
3128 /* window: minimal height of the window plus status line */
3129 m = p_wmh + topfrp->fr_win->w_status_height;
3130 /* Current window is minimal one line high */
3131 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3132 ++m;
3133 }
3134 }
3135#ifdef FEAT_VERTSPLIT
3136 else if (topfrp->fr_layout == FR_ROW)
3137 {
3138 /* get the minimal height from each frame in this row */
3139 m = 0;
3140 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3141 {
3142 n = frame_minheight(frp, next_curwin);
3143 if (n > m)
3144 m = n;
3145 }
3146 }
3147#endif
3148 else
3149 {
3150 /* Add up the minimal heights for all frames in this column. */
3151 m = 0;
3152 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3153 m += frame_minheight(frp, next_curwin);
3154 }
3155
3156 return m;
3157}
3158
3159#ifdef FEAT_VERTSPLIT
3160/*
3161 * Compute the minimal width for frame "topfrp".
3162 * When "next_curwin" isn't NULL, use p_wiw for this window.
3163 * When "next_curwin" is NOWIN, don't use at least one column for the current
3164 * window.
3165 */
3166 static int
3167frame_minwidth(topfrp, next_curwin)
3168 frame_T *topfrp;
3169 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3170{
3171 frame_T *frp;
3172 int m, n;
3173
3174 if (topfrp->fr_win != NULL)
3175 {
3176 if (topfrp->fr_win == next_curwin)
3177 m = p_wiw + topfrp->fr_win->w_vsep_width;
3178 else
3179 {
3180 /* window: minimal width of the window plus separator column */
3181 m = p_wmw + topfrp->fr_win->w_vsep_width;
3182 /* Current window is minimal one column wide */
3183 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3184 ++m;
3185 }
3186 }
3187 else if (topfrp->fr_layout == FR_COL)
3188 {
3189 /* get the minimal width from each frame in this column */
3190 m = 0;
3191 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3192 {
3193 n = frame_minwidth(frp, next_curwin);
3194 if (n > m)
3195 m = n;
3196 }
3197 }
3198 else
3199 {
3200 /* Add up the minimal widths for all frames in this row. */
3201 m = 0;
3202 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3203 m += frame_minwidth(frp, next_curwin);
3204 }
3205
3206 return m;
3207}
3208#endif
3209
3210
3211/*
3212 * Try to close all windows except current one.
3213 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3214 * used and the buffer was modified.
3215 *
3216 * Used by ":bdel" and ":only".
3217 */
3218 void
3219close_others(message, forceit)
3220 int message;
3221 int forceit; /* always hide all other windows */
3222{
3223 win_T *wp;
3224 win_T *nextwp;
3225 int r;
3226
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003227 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 {
3229 if (message
3230#ifdef FEAT_AUTOCMD
3231 && !autocmd_busy
3232#endif
3233 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003234 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 return;
3236 }
3237
3238 /* Be very careful here: autocommands may change the window layout. */
3239 for (wp = firstwin; win_valid(wp); wp = nextwp)
3240 {
3241 nextwp = wp->w_next;
3242 if (wp != curwin) /* don't close current window */
3243 {
3244
3245 /* Check if it's allowed to abandon this window */
3246 r = can_abandon(wp->w_buffer, forceit);
3247#ifdef FEAT_AUTOCMD
3248 if (!win_valid(wp)) /* autocommands messed wp up */
3249 {
3250 nextwp = firstwin;
3251 continue;
3252 }
3253#endif
3254 if (!r)
3255 {
3256#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3257 if (message && (p_confirm || cmdmod.confirm) && p_write)
3258 {
3259 dialog_changed(wp->w_buffer, FALSE);
3260# ifdef FEAT_AUTOCMD
3261 if (!win_valid(wp)) /* autocommands messed wp up */
3262 {
3263 nextwp = firstwin;
3264 continue;
3265 }
3266# endif
3267 }
3268 if (bufIsChanged(wp->w_buffer))
3269#endif
3270 continue;
3271 }
3272 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3273 }
3274 }
3275
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003276 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 EMSG(_("E445: Other window contains changes"));
3278}
3279
3280#endif /* FEAT_WINDOWS */
3281
3282/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003283 * Init the current window "curwin".
3284 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 */
3286 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003287curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003289 win_init_empty(curwin);
3290}
3291
3292 void
3293win_init_empty(wp)
3294 win_T *wp;
3295{
3296 redraw_win_later(wp, NOT_VALID);
3297 wp->w_lines_valid = 0;
3298 wp->w_cursor.lnum = 1;
3299 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003301 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003303 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3304 wp->w_pcmark.col = 0;
3305 wp->w_prev_pcmark.lnum = 0;
3306 wp->w_prev_pcmark.col = 0;
3307 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003309 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003311 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003313 if (wp->w_p_rl)
3314 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003316 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003318#ifdef FEAT_SYN_HL
3319 wp->w_s = &wp->w_buffer->b_s;
3320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321}
3322
3323/*
3324 * Allocate the first window and put an empty buffer in it.
3325 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003326 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003328 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329win_alloc_first()
3330{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003331 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003332 return FAIL;
3333
3334#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003335 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003336 if (first_tabpage == NULL)
3337 return FAIL;
3338 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003339 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003340#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003341
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003342 return OK;
3343}
3344
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003345#if defined(FEAT_AUTOCMD) || defined(PROTO)
3346/*
3347 * Init "aucmd_win". This can only be done after the first
3348 * window is fully initialized, thus it can't be in win_alloc_first().
3349 */
3350 void
3351win_alloc_aucmd_win()
3352{
3353 aucmd_win = win_alloc(NULL, TRUE);
3354 if (aucmd_win != NULL)
3355 {
3356 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003357 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003358 new_frame(aucmd_win);
3359 }
3360}
3361#endif
3362
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003363/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003364 * Allocate the first window or the first window in a new tab page.
3365 * When "oldwin" is NULL create an empty buffer for it.
3366 * When "oldwin" is not NULL copy info from it to the new window (only with
3367 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003368 * Return FAIL when something goes wrong (out of memory).
3369 */
3370 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003371win_alloc_firstwin(oldwin)
3372 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003373{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003374 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003375 if (oldwin == NULL)
3376 {
3377 /* Very first window, need to create an empty buffer for it and
3378 * initialize from scratch. */
3379 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3380 if (curwin == NULL || curbuf == NULL)
3381 return FAIL;
3382 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003383#ifdef FEAT_SYN_HL
3384 curwin->w_s = &(curbuf->b_s);
3385#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003386 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003388 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003390 curwin_init(); /* init current window */
3391 }
3392#ifdef FEAT_WINDOWS
3393 else
3394 {
3395 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003396 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003397
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003398 /* We don't want cursor- and scroll-binding in the first window. */
3399 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003400 }
3401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003403 new_frame(curwin);
3404 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003405 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003406 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407#ifdef FEAT_VERTSPLIT
3408 topframe->fr_width = Columns;
3409#endif
3410 topframe->fr_height = Rows - p_ch;
3411 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003412
3413 return OK;
3414}
3415
3416/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003417 * Create a frame for window "wp".
3418 */
3419 static void
3420new_frame(win_T *wp)
3421{
3422 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3423
3424 wp->w_frame = frp;
3425 if (frp != NULL)
3426 {
3427 frp->fr_layout = FR_LEAF;
3428 frp->fr_win = wp;
3429 }
3430}
3431
3432/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003433 * Initialize the window and frame size to the maximum.
3434 */
3435 void
3436win_init_size()
3437{
3438 firstwin->w_height = ROWS_AVAIL;
3439 topframe->fr_height = ROWS_AVAIL;
3440#ifdef FEAT_VERTSPLIT
3441 firstwin->w_width = Columns;
3442 topframe->fr_width = Columns;
3443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444}
3445
3446#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003447
3448/*
3449 * Allocate a new tabpage_T and init the values.
3450 * Returns NULL when out of memory.
3451 */
3452 static tabpage_T *
3453alloc_tabpage()
3454{
3455 tabpage_T *tp;
3456
3457 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3458 if (tp != NULL)
3459 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003460# ifdef FEAT_GUI
3461 int i;
3462
3463 for (i = 0; i < 3; i++)
3464 tp->tp_prev_which_scrollbars[i] = -1;
3465# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003466# ifdef FEAT_DIFF
3467 tp->tp_diff_invalid = TRUE;
3468# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003469#ifdef FEAT_EVAL
3470 /* init t: variables */
3471 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3472#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003473 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003474 }
3475 return tp;
3476}
3477
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003478 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003479free_tabpage(tp)
3480 tabpage_T *tp;
3481{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003482 int idx;
3483
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003484# ifdef FEAT_DIFF
3485 diff_clear(tp);
3486# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003487 for (idx = 0; idx < SNAP_COUNT; ++idx)
3488 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003489#ifdef FEAT_EVAL
3490 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3491#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003492 vim_free(tp);
3493}
3494
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003495/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003496 * Create a new Tab page with one window.
3497 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003498 * When "after" is 0 put it just after the current Tab page.
3499 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003500 * Return FAIL or OK.
3501 */
3502 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003503win_new_tabpage(after)
3504 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003505{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003506 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003507 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003508 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003509
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003510 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003511 if (newtp == NULL)
3512 return FAIL;
3513
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003514 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003515 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003516 {
3517 vim_free(newtp);
3518 return FAIL;
3519 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003520 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003521
3522 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003523 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003524 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003525 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003526 if (after == 1)
3527 {
3528 /* New tab page becomes the first one. */
3529 newtp->tp_next = first_tabpage;
3530 first_tabpage = newtp;
3531 }
3532 else
3533 {
3534 if (after > 0)
3535 {
3536 /* Put new tab page before tab page "after". */
3537 n = 2;
3538 for (tp = first_tabpage; tp->tp_next != NULL
3539 && n < after; tp = tp->tp_next)
3540 ++n;
3541 }
3542 newtp->tp_next = tp->tp_next;
3543 tp->tp_next = newtp;
3544 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003545 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003546 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003547 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003548
3549 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003550 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003551
3552#if defined(FEAT_GUI)
3553 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3554 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003555 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003556#endif
3557
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003558 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003559#ifdef FEAT_AUTOCMD
3560 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3561 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3562#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003563 return OK;
3564 }
3565
3566 /* Failed, get back the previous Tab page */
Bram Moolenaara8596c42012-06-13 14:28:20 +02003567 enter_tabpage(curtab, curbuf, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003568 return FAIL;
3569}
3570
3571/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003572 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3573 * like with ":split".
3574 * Returns OK if a new tab page was created, FAIL otherwise.
3575 */
3576 int
3577may_open_tabpage()
3578{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003579 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003580
Bram Moolenaard326ce82007-03-11 14:48:29 +00003581 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003582 {
3583 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003584 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003585 return win_new_tabpage(n);
3586 }
3587 return FAIL;
3588}
3589
3590/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003591 * Create up to "maxcount" tabpages with empty windows.
3592 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003593 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003594 int
3595make_tabpages(maxcount)
3596 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003597{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003598 int count = maxcount;
3599 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003600
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003601 /* Limit to 'tabpagemax' tabs. */
3602 if (count > p_tpm)
3603 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003604
3605#ifdef FEAT_AUTOCMD
3606 /*
3607 * Don't execute autocommands while creating the tab pages. Must do that
3608 * when putting the buffers in the windows.
3609 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003610 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003611#endif
3612
3613 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003614 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003615 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003616
3617#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003618 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003619#endif
3620
3621 /* return actual number of tab pages */
3622 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003623}
3624
3625/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003626 * Return TRUE when "tpc" points to a valid tab page.
3627 */
3628 int
3629valid_tabpage(tpc)
3630 tabpage_T *tpc;
3631{
3632 tabpage_T *tp;
3633
3634 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3635 if (tp == tpc)
3636 return TRUE;
3637 return FALSE;
3638}
3639
3640/*
3641 * Find tab page "n" (first one is 1). Returns NULL when not found.
3642 */
3643 tabpage_T *
3644find_tabpage(n)
3645 int n;
3646{
3647 tabpage_T *tp;
3648 int i = 1;
3649
3650 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3651 ++i;
3652 return tp;
3653}
3654
3655/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003656 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003657 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003658 */
3659 int
3660tabpage_index(ftp)
3661 tabpage_T *ftp;
3662{
3663 int i = 1;
3664 tabpage_T *tp;
3665
3666 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3667 ++i;
3668 return i;
3669}
3670
3671/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003672 * Prepare for leaving the current tab page.
3673 * When autocomands change "curtab" we don't leave the tab page and return
3674 * FAIL.
3675 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003676 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003677 static int
3678leave_tabpage(new_curbuf)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003679 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003680 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003681{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003682 tabpage_T *tp = curtab;
3683
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003684#ifdef FEAT_VISUAL
3685 reset_VIsual_and_resel(); /* stop Visual mode */
3686#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003687#ifdef FEAT_AUTOCMD
3688 if (new_curbuf != curbuf)
3689 {
3690 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3691 if (curtab != tp)
3692 return FAIL;
3693 }
3694 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3695 if (curtab != tp)
3696 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003697 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003698 if (curtab != tp)
3699 return FAIL;
3700#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003701#if defined(FEAT_GUI)
3702 /* Remove the scrollbars. They may be added back later. */
3703 if (gui.in_use)
3704 gui_remove_scrollbars();
3705#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003706 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003707 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003708 tp->tp_firstwin = firstwin;
3709 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003710 tp->tp_old_Rows = Rows;
3711 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003712 firstwin = NULL;
3713 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003714 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003715}
3716
3717/*
3718 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003719 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaara8596c42012-06-13 14:28:20 +02003720 * Only trigger *Enter autocommands when trigger_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003721 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003722 static void
Bram Moolenaara8596c42012-06-13 14:28:20 +02003723enter_tabpage(tp, old_curbuf, trigger_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003724 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003725 buf_T *old_curbuf UNUSED;
Bram Moolenaar756287d2012-07-06 16:39:47 +02003726 int trigger_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003727{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003728 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003729 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003730
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003731 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003732 firstwin = tp->tp_firstwin;
3733 lastwin = tp->tp_lastwin;
3734 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003735
3736 /* We would like doing the TabEnter event first, but we don't have a
3737 * valid current window yet, which may break some commands.
3738 * This triggers autocommands, thus may make "tp" invalid. */
3739 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3740 prevwin = next_prevwin;
3741
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003742 last_status(FALSE); /* status line may appear or disappear */
3743 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003744 must_redraw = CLEAR; /* need to redraw everything */
3745#ifdef FEAT_DIFF
3746 diff_need_scrollbind = TRUE;
3747#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003748
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003749 /* The tabpage line may have appeared or disappeared, may need to resize
3750 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003751 * frame sizes too. Use the stored value of p_ch, so that it can be
3752 * different for each tab page. */
3753 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003754 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3755#ifdef FEAT_GUI_TABLINE
3756 && !gui_use_tabline()
3757#endif
3758 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003759 shell_new_rows();
3760#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003761 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003762 shell_new_columns(); /* update window widths */
3763#endif
3764
3765#if defined(FEAT_GUI)
3766 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3767 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003768 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003769#endif
3770
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003771#ifdef FEAT_AUTOCMD
3772 /* Apply autocommands after updating the display, when 'rows' and
3773 * 'columns' have been set correctly. */
Bram Moolenaara8596c42012-06-13 14:28:20 +02003774 if (trigger_autocmds)
3775 {
3776 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3777 if (old_curbuf != curbuf)
3778 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3779 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003780#endif
3781
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003782 redraw_all_later(CLEAR);
3783}
3784
3785/*
3786 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003787 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003788 */
3789 void
3790goto_tabpage(n)
3791 int n;
3792{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003793 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003794 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003795 int i;
3796
Bram Moolenaard68071d2006-05-02 22:08:30 +00003797 if (text_locked())
3798 {
3799 /* Not allowed when editing the command line. */
3800#ifdef FEAT_CMDWIN
3801 if (cmdwin_type != 0)
3802 EMSG(_(e_cmdwin));
3803 else
3804#endif
3805 EMSG(_(e_secure));
3806 return;
3807 }
3808
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003809 /* If there is only one it can't work. */
3810 if (first_tabpage->tp_next == NULL)
3811 {
3812 if (n > 1)
3813 beep_flush();
3814 return;
3815 }
3816
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003817 if (n == 0)
3818 {
3819 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003820 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003821 tp = first_tabpage;
3822 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003823 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003824 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003825 else if (n < 0)
3826 {
3827 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3828 * this N times. */
3829 ttp = curtab;
3830 for (i = n; i < 0; ++i)
3831 {
3832 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3833 tp = tp->tp_next)
3834 ;
3835 ttp = tp;
3836 }
3837 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003838 else if (n == 9999)
3839 {
3840 /* Go to last tab page. */
3841 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3842 ;
3843 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003844 else
3845 {
3846 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003847 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003848 if (tp == NULL)
3849 {
3850 beep_flush();
3851 return;
3852 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003853 }
3854
Bram Moolenaara8596c42012-06-13 14:28:20 +02003855 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003856
3857#ifdef FEAT_GUI_TABLINE
3858 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003859 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003860#endif
3861}
3862
3863/*
3864 * Go to tabpage "tp".
Bram Moolenaara8596c42012-06-13 14:28:20 +02003865 * Only trigger *Enter autocommands when trigger_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003866 * Note: doesn't update the GUI tab.
3867 */
3868 void
Bram Moolenaara8596c42012-06-13 14:28:20 +02003869goto_tabpage_tp(tp, trigger_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003870 tabpage_T *tp;
Bram Moolenaar756287d2012-07-06 16:39:47 +02003871 int trigger_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003872{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003873 /* Don't repeat a message in another tab page. */
3874 set_keep_msg(NULL, 0);
3875
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003876 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003877 {
3878 if (valid_tabpage(tp))
Bram Moolenaara8596c42012-06-13 14:28:20 +02003879 enter_tabpage(tp, curbuf, trigger_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003880 else
Bram Moolenaara8596c42012-06-13 14:28:20 +02003881 enter_tabpage(curtab, curbuf, trigger_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003882 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003883}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884
3885/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003886 * Enter window "wp" in tab page "tp".
3887 * Also updates the GUI tab.
3888 */
3889 void
3890goto_tabpage_win(tp, wp)
3891 tabpage_T *tp;
3892 win_T *wp;
3893{
Bram Moolenaara8596c42012-06-13 14:28:20 +02003894 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003895 if (curtab == tp && win_valid(wp))
3896 {
3897 win_enter(wp, TRUE);
3898# ifdef FEAT_GUI_TABLINE
3899 if (gui_use_tabline())
3900 gui_mch_set_curtab(tabpage_index(curtab));
3901# endif
3902 }
3903}
3904
3905/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003906 * Move the current tab page to before tab page "nr".
3907 */
3908 void
3909tabpage_move(nr)
3910 int nr;
3911{
3912 int n = nr;
3913 tabpage_T *tp;
3914
3915 if (first_tabpage->tp_next == NULL)
3916 return;
3917
3918 /* Remove the current tab page from the list of tab pages. */
3919 if (curtab == first_tabpage)
3920 first_tabpage = curtab->tp_next;
3921 else
3922 {
3923 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3924 if (tp->tp_next == curtab)
3925 break;
3926 if (tp == NULL) /* "cannot happen" */
3927 return;
3928 tp->tp_next = curtab->tp_next;
3929 }
3930
3931 /* Re-insert it at the specified position. */
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02003932 if (n <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003933 {
3934 curtab->tp_next = first_tabpage;
3935 first_tabpage = curtab;
3936 }
3937 else
3938 {
3939 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3940 --n;
3941 curtab->tp_next = tp->tp_next;
3942 tp->tp_next = curtab;
3943 }
3944
3945 /* Need to redraw the tabline. Tab page contents doesn't change. */
3946 redraw_tabline = TRUE;
3947}
3948
3949
3950/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 * Go to another window.
3952 * When jumping to another buffer, stop Visual mode. Do this before
3953 * changing windows so we can yank the selection into the '*' register.
3954 * When jumping to another window on the same buffer, adjust its cursor
3955 * position to keep the same Visual area.
3956 */
3957 void
3958win_goto(wp)
3959 win_T *wp;
3960{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02003961#ifdef FEAT_CONCEAL
3962 win_T *owp = curwin;
3963#endif
3964
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003965 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 {
3967 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003968 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 return;
3970 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003971#ifdef FEAT_AUTOCMD
3972 if (curbuf_locked())
3973 return;
3974#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003975
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976#ifdef FEAT_VISUAL
3977 if (wp->w_buffer != curbuf)
3978 reset_VIsual_and_resel();
3979 else if (VIsual_active)
3980 wp->w_cursor = curwin->w_cursor;
3981#endif
3982
3983#ifdef FEAT_GUI
3984 need_mouse_correct = TRUE;
3985#endif
3986 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02003987
3988#ifdef FEAT_CONCEAL
3989 /* Conceal cursor line in previous window, unconceal in current window. */
3990 if (win_valid(owp))
3991 update_single_line(owp, owp->w_cursor.lnum);
3992 update_single_line(curwin, curwin->w_cursor.lnum);
3993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994}
3995
3996#if defined(FEAT_PERL) || defined(PROTO)
3997/*
3998 * Find window number "winnr" (counting top to bottom).
3999 */
4000 win_T *
4001win_find_nr(winnr)
4002 int winnr;
4003{
4004 win_T *wp;
4005
4006# ifdef FEAT_WINDOWS
4007 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4008 if (--winnr == 0)
4009 break;
4010 return wp;
4011# else
4012 return curwin;
4013# endif
4014}
4015#endif
4016
4017#ifdef FEAT_VERTSPLIT
4018/*
4019 * Move to window above or below "count" times.
4020 */
4021 static void
4022win_goto_ver(up, count)
4023 int up; /* TRUE to go to win above */
4024 long count;
4025{
4026 frame_T *fr;
4027 frame_T *nfr;
4028 frame_T *foundfr;
4029
4030 foundfr = curwin->w_frame;
4031 while (count--)
4032 {
4033 /*
4034 * First go upwards in the tree of frames until we find a upwards or
4035 * downwards neighbor.
4036 */
4037 fr = foundfr;
4038 for (;;)
4039 {
4040 if (fr == topframe)
4041 goto end;
4042 if (up)
4043 nfr = fr->fr_prev;
4044 else
4045 nfr = fr->fr_next;
4046 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4047 break;
4048 fr = fr->fr_parent;
4049 }
4050
4051 /*
4052 * Now go downwards to find the bottom or top frame in it.
4053 */
4054 for (;;)
4055 {
4056 if (nfr->fr_layout == FR_LEAF)
4057 {
4058 foundfr = nfr;
4059 break;
4060 }
4061 fr = nfr->fr_child;
4062 if (nfr->fr_layout == FR_ROW)
4063 {
4064 /* Find the frame at the cursor row. */
4065 while (fr->fr_next != NULL
4066 && frame2win(fr)->w_wincol + fr->fr_width
4067 <= curwin->w_wincol + curwin->w_wcol)
4068 fr = fr->fr_next;
4069 }
4070 if (nfr->fr_layout == FR_COL && up)
4071 while (fr->fr_next != NULL)
4072 fr = fr->fr_next;
4073 nfr = fr;
4074 }
4075 }
4076end:
4077 if (foundfr != NULL)
4078 win_goto(foundfr->fr_win);
4079}
4080
4081/*
4082 * Move to left or right window.
4083 */
4084 static void
4085win_goto_hor(left, count)
4086 int left; /* TRUE to go to left win */
4087 long count;
4088{
4089 frame_T *fr;
4090 frame_T *nfr;
4091 frame_T *foundfr;
4092
4093 foundfr = curwin->w_frame;
4094 while (count--)
4095 {
4096 /*
4097 * First go upwards in the tree of frames until we find a left or
4098 * right neighbor.
4099 */
4100 fr = foundfr;
4101 for (;;)
4102 {
4103 if (fr == topframe)
4104 goto end;
4105 if (left)
4106 nfr = fr->fr_prev;
4107 else
4108 nfr = fr->fr_next;
4109 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4110 break;
4111 fr = fr->fr_parent;
4112 }
4113
4114 /*
4115 * Now go downwards to find the leftmost or rightmost frame in it.
4116 */
4117 for (;;)
4118 {
4119 if (nfr->fr_layout == FR_LEAF)
4120 {
4121 foundfr = nfr;
4122 break;
4123 }
4124 fr = nfr->fr_child;
4125 if (nfr->fr_layout == FR_COL)
4126 {
4127 /* Find the frame at the cursor row. */
4128 while (fr->fr_next != NULL
4129 && frame2win(fr)->w_winrow + fr->fr_height
4130 <= curwin->w_winrow + curwin->w_wrow)
4131 fr = fr->fr_next;
4132 }
4133 if (nfr->fr_layout == FR_ROW && left)
4134 while (fr->fr_next != NULL)
4135 fr = fr->fr_next;
4136 nfr = fr;
4137 }
4138 }
4139end:
4140 if (foundfr != NULL)
4141 win_goto(foundfr->fr_win);
4142}
4143#endif
4144
4145/*
4146 * Make window "wp" the current window.
4147 */
4148 void
4149win_enter(wp, undo_sync)
4150 win_T *wp;
4151 int undo_sync;
4152{
4153 win_enter_ext(wp, undo_sync, FALSE);
4154}
4155
4156/*
4157 * Make window wp the current window.
4158 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4159 * been closed and isn't valid.
4160 */
4161 static void
4162win_enter_ext(wp, undo_sync, curwin_invalid)
4163 win_T *wp;
4164 int undo_sync;
4165 int curwin_invalid;
4166{
4167#ifdef FEAT_AUTOCMD
4168 int other_buffer = FALSE;
4169#endif
4170
4171 if (wp == curwin && !curwin_invalid) /* nothing to do */
4172 return;
4173
4174#ifdef FEAT_AUTOCMD
4175 if (!curwin_invalid)
4176 {
4177 /*
4178 * Be careful: If autocommands delete the window, return now.
4179 */
4180 if (wp->w_buffer != curbuf)
4181 {
4182 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4183 other_buffer = TRUE;
4184 if (!win_valid(wp))
4185 return;
4186 }
4187 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4188 if (!win_valid(wp))
4189 return;
4190# ifdef FEAT_EVAL
4191 /* autocmds may abort script processing */
4192 if (aborting())
4193 return;
4194# endif
4195 }
4196#endif
4197
4198 /* sync undo before leaving the current buffer */
4199 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004200 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 /* may have to copy the buffer options when 'cpo' contains 'S' */
4202 if (wp->w_buffer != curbuf)
4203 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4204 if (!curwin_invalid)
4205 {
4206 prevwin = curwin; /* remember for CTRL-W p */
4207 curwin->w_redr_status = TRUE;
4208 }
4209 curwin = wp;
4210 curbuf = wp->w_buffer;
4211 check_cursor();
4212#ifdef FEAT_VIRTUALEDIT
4213 if (!virtual_active())
4214 curwin->w_cursor.coladd = 0;
4215#endif
4216 changed_line_abv_curs(); /* assume cursor position needs updating */
4217
4218 if (curwin->w_localdir != NULL)
4219 {
4220 /* Window has a local directory: Save current directory as global
4221 * directory (unless that was done already) and change to the local
4222 * directory. */
4223 if (globaldir == NULL)
4224 {
4225 char_u cwd[MAXPATHL];
4226
4227 if (mch_dirname(cwd, MAXPATHL) == OK)
4228 globaldir = vim_strsave(cwd);
4229 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004230 if (mch_chdir((char *)curwin->w_localdir) == 0)
4231 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233 else if (globaldir != NULL)
4234 {
4235 /* Window doesn't have a local directory and we are not in the global
4236 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004237 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 vim_free(globaldir);
4239 globaldir = NULL;
4240 shorten_fnames(TRUE);
4241 }
4242
4243#ifdef FEAT_AUTOCMD
4244 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4245 if (other_buffer)
4246 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4247#endif
4248
4249#ifdef FEAT_TITLE
4250 maketitle();
4251#endif
4252 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004253 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 if (restart_edit)
4255 redraw_later(VALID); /* causes status line redraw */
4256
4257 /* set window height to desired minimal value */
4258 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4259 win_setheight((int)p_wh);
4260 else if (curwin->w_height == 0)
4261 win_setheight(1);
4262
4263#ifdef FEAT_VERTSPLIT
4264 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004265 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 win_setwidth((int)p_wiw);
4267#endif
4268
4269#ifdef FEAT_MOUSE
4270 setmouse(); /* in case jumped to/from help buffer */
4271#endif
4272
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004273 /* Change directories when the 'acd' option is set. */
4274 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275}
4276
4277#endif /* FEAT_WINDOWS */
4278
4279#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4280/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004281 * Jump to the first open window that contains buffer "buf", if one exists.
4282 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 */
4284 win_T *
4285buf_jump_open_win(buf)
4286 buf_T *buf;
4287{
4288# ifdef FEAT_WINDOWS
4289 win_T *wp;
4290
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004291 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 if (wp->w_buffer == buf)
4293 break;
4294 if (wp != NULL)
4295 win_enter(wp, FALSE);
4296 return wp;
4297# else
4298 if (curwin->w_buffer == buf)
4299 return curwin;
4300 return NULL;
4301# endif
4302}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004303
4304/*
4305 * Jump to the first open window in any tab page that contains buffer "buf",
4306 * if one exists.
4307 * Returns a pointer to the window found, otherwise NULL.
4308 */
4309 win_T *
4310buf_jump_open_tab(buf)
4311 buf_T *buf;
4312{
4313# ifdef FEAT_WINDOWS
4314 win_T *wp;
4315 tabpage_T *tp;
4316
4317 /* First try the current tab page. */
4318 wp = buf_jump_open_win(buf);
4319 if (wp != NULL)
4320 return wp;
4321
4322 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4323 if (tp != curtab)
4324 {
4325 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4326 if (wp->w_buffer == buf)
4327 break;
4328 if (wp != NULL)
4329 {
4330 goto_tabpage_win(tp, wp);
4331 if (curwin != wp)
4332 wp = NULL; /* something went wrong */
4333 break;
4334 }
4335 }
4336
4337 return wp;
4338# else
4339 if (curwin->w_buffer == buf)
4340 return curwin;
4341 return NULL;
4342# endif
4343}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344#endif
4345
4346/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004347 * Allocate a window structure and link it in the window list when "hidden" is
4348 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004351win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004352 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004353 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004355 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
4357 /*
4358 * allocate window structure and linesizes arrays
4359 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004360 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4361 if (new_wp != NULL && win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004363 vim_free(new_wp);
4364 new_wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004367 if (new_wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004369#ifdef FEAT_AUTOCMD
4370 /* Don't execute autocommands while the window is not properly
4371 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4372 * event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004373 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 /*
4376 * link the window in the window list
4377 */
4378#ifdef FEAT_WINDOWS
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004379 if (!hidden)
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004380 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381#endif
4382#ifdef FEAT_VERTSPLIT
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004383 new_wp->w_wincol = 0;
4384 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385#endif
4386
4387 /* position the display and the cursor at the top of the file. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004388 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389#ifdef FEAT_DIFF
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004390 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004392 new_wp->w_botline = 2;
4393 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394#ifdef FEAT_SCROLLBIND
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004395 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396#endif
4397
4398 /* We won't calculate w_fraction until resizing the window */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004399 new_wp->w_fraction = 0;
4400 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
4402#ifdef FEAT_GUI
4403 if (gui.in_use)
4404 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004405 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4406 SBAR_LEFT, new_wp);
4407 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4408 SBAR_RIGHT, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 }
4410#endif
4411#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004412 /* init w: variables */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004413 init_var_dict(&new_wp->w_vars, &new_wp->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414#endif
4415#ifdef FEAT_FOLDING
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004416 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004418#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004419 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004420#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004421#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004422 new_wp->w_match_head = NULL;
4423 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004426 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427}
4428
4429#if defined(FEAT_WINDOWS) || defined(PROTO)
4430
4431/*
4432 * remove window 'wp' from the window list and free the structure
4433 */
4434 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004435win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004437 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438{
4439 int i;
4440
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004441#ifdef FEAT_FOLDING
4442 clearFolding(wp);
4443#endif
4444
4445 /* reduce the reference count to the argument list. */
4446 alist_unlink(wp->w_alist);
4447
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004448#ifdef FEAT_AUTOCMD
4449 /* Don't execute autocommands while the window is halfway being deleted.
4450 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004451 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004452#endif
4453
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004454#ifdef FEAT_LUA
4455 lua_window_free(wp);
4456#endif
4457
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004458#ifdef FEAT_MZSCHEME
4459 mzscheme_window_free(wp);
4460#endif
4461
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462#ifdef FEAT_PERL
4463 perl_win_free(wp);
4464#endif
4465
4466#ifdef FEAT_PYTHON
4467 python_window_free(wp);
4468#endif
4469
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004470#ifdef FEAT_PYTHON3
4471 python3_window_free(wp);
4472#endif
4473
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474#ifdef FEAT_TCL
4475 tcl_window_free(wp);
4476#endif
4477
4478#ifdef FEAT_RUBY
4479 ruby_window_free(wp);
4480#endif
4481
4482 clear_winopt(&wp->w_onebuf_opt);
4483 clear_winopt(&wp->w_allbuf_opt);
4484
4485#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004486 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487#endif
4488
4489 if (prevwin == wp)
4490 prevwin = NULL;
4491 win_free_lsize(wp);
4492
4493 for (i = 0; i < wp->w_tagstacklen; ++i)
4494 vim_free(wp->w_tagstack[i].tagname);
4495
4496 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004497
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004499 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004501
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502#ifdef FEAT_JUMPLIST
4503 free_jumplist(wp);
4504#endif
4505
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004506#ifdef FEAT_QUICKFIX
4507 qf_free_all(wp);
4508#endif
4509
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510#ifdef FEAT_GUI
4511 if (gui.in_use)
4512 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4514 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4515 }
4516#endif /* FEAT_GUI */
4517
Bram Moolenaar860cae12010-06-05 23:22:07 +02004518#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004519 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004520#endif
4521
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004522#ifdef FEAT_AUTOCMD
4523 if (wp != aucmd_win)
4524#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004525 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004527
4528#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004529 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531}
4532
4533/*
4534 * Append window "wp" in the window list after window "after".
4535 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004536 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537win_append(after, wp)
4538 win_T *after, *wp;
4539{
4540 win_T *before;
4541
4542 if (after == NULL) /* after NULL is in front of the first */
4543 before = firstwin;
4544 else
4545 before = after->w_next;
4546
4547 wp->w_next = before;
4548 wp->w_prev = after;
4549 if (after == NULL)
4550 firstwin = wp;
4551 else
4552 after->w_next = wp;
4553 if (before == NULL)
4554 lastwin = wp;
4555 else
4556 before->w_prev = wp;
4557}
4558
4559/*
4560 * Remove a window from the window list.
4561 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004562 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004563win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004565 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566{
4567 if (wp->w_prev != NULL)
4568 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004569 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004571 else
4572 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 if (wp->w_next != NULL)
4574 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004575 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004577 else
4578 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579}
4580
4581/*
4582 * Append frame "frp" in a frame list after frame "after".
4583 */
4584 static void
4585frame_append(after, frp)
4586 frame_T *after, *frp;
4587{
4588 frp->fr_next = after->fr_next;
4589 after->fr_next = frp;
4590 if (frp->fr_next != NULL)
4591 frp->fr_next->fr_prev = frp;
4592 frp->fr_prev = after;
4593}
4594
4595/*
4596 * Insert frame "frp" in a frame list before frame "before".
4597 */
4598 static void
4599frame_insert(before, frp)
4600 frame_T *before, *frp;
4601{
4602 frp->fr_next = before;
4603 frp->fr_prev = before->fr_prev;
4604 before->fr_prev = frp;
4605 if (frp->fr_prev != NULL)
4606 frp->fr_prev->fr_next = frp;
4607 else
4608 frp->fr_parent->fr_child = frp;
4609}
4610
4611/*
4612 * Remove a frame from a frame list.
4613 */
4614 static void
4615frame_remove(frp)
4616 frame_T *frp;
4617{
4618 if (frp->fr_prev != NULL)
4619 frp->fr_prev->fr_next = frp->fr_next;
4620 else
4621 frp->fr_parent->fr_child = frp->fr_next;
4622 if (frp->fr_next != NULL)
4623 frp->fr_next->fr_prev = frp->fr_prev;
4624}
4625
4626#endif /* FEAT_WINDOWS */
4627
4628/*
4629 * Allocate w_lines[] for window "wp".
4630 * Return FAIL for failure, OK for success.
4631 */
4632 int
4633win_alloc_lines(wp)
4634 win_T *wp;
4635{
4636 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004637 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 if (wp->w_lines == NULL)
4639 return FAIL;
4640 return OK;
4641}
4642
4643/*
4644 * free lsize arrays for a window
4645 */
4646 void
4647win_free_lsize(wp)
4648 win_T *wp;
4649{
4650 vim_free(wp->w_lines);
4651 wp->w_lines = NULL;
4652}
4653
4654/*
4655 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004656 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 */
4658 void
4659shell_new_rows()
4660{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004661 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662
4663 if (firstwin == NULL) /* not initialized yet */
4664 return;
4665#ifdef FEAT_WINDOWS
4666 if (h < frame_minheight(topframe, NULL))
4667 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004668
4669 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 * that doesn't result in the right height, forget about that option. */
4671 frame_new_height(topframe, h, FALSE, TRUE);
4672 if (topframe->fr_height != h)
4673 frame_new_height(topframe, h, FALSE, FALSE);
4674
4675 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4676#else
4677 if (h < 1)
4678 h = 1;
4679 win_new_height(firstwin, h);
4680#endif
4681 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004682#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004683 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004684#endif
4685
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686#if 0
4687 /* Disabled: don't want making the screen smaller make a window larger. */
4688 if (p_ea)
4689 win_equal(curwin, FALSE, 'v');
4690#endif
4691}
4692
4693#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4694/*
4695 * Called from win_new_shellsize() after Columns changed.
4696 */
4697 void
4698shell_new_columns()
4699{
4700 if (firstwin == NULL) /* not initialized yet */
4701 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004702
4703 /* First try setting the widths of windows with 'winfixwidth'. If that
4704 * doesn't result in the right width, forget about that option. */
4705 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4706 if (topframe->fr_width != Columns)
4707 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4708
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4710#if 0
4711 /* Disabled: don't want making the screen smaller make a window larger. */
4712 if (p_ea)
4713 win_equal(curwin, FALSE, 'h');
4714#endif
4715}
4716#endif
4717
4718#if defined(FEAT_CMDWIN) || defined(PROTO)
4719/*
4720 * Save the size of all windows in "gap".
4721 */
4722 void
4723win_size_save(gap)
4724 garray_T *gap;
4725
4726{
4727 win_T *wp;
4728
4729 ga_init2(gap, (int)sizeof(int), 1);
4730 if (ga_grow(gap, win_count() * 2) == OK)
4731 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4732 {
4733 ((int *)gap->ga_data)[gap->ga_len++] =
4734 wp->w_width + wp->w_vsep_width;
4735 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4736 }
4737}
4738
4739/*
4740 * Restore window sizes, but only if the number of windows is still the same.
4741 * Does not free the growarray.
4742 */
4743 void
4744win_size_restore(gap)
4745 garray_T *gap;
4746{
4747 win_T *wp;
4748 int i;
4749
4750 if (win_count() * 2 == gap->ga_len)
4751 {
4752 i = 0;
4753 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4754 {
4755 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4756 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4757 }
4758 /* recompute the window positions */
4759 (void)win_comp_pos();
4760 }
4761}
4762#endif /* FEAT_CMDWIN */
4763
4764#if defined(FEAT_WINDOWS) || defined(PROTO)
4765/*
4766 * Update the position for all windows, using the width and height of the
4767 * frames.
4768 * Returns the row just after the last window.
4769 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004770 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771win_comp_pos()
4772{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004773 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 int col = 0;
4775
4776 frame_comp_pos(topframe, &row, &col);
4777 return row;
4778}
4779
4780/*
4781 * Update the position of the windows in frame "topfrp", using the width and
4782 * height of the frames.
4783 * "*row" and "*col" are the top-left position of the frame. They are updated
4784 * to the bottom-right position plus one.
4785 */
4786 static void
4787frame_comp_pos(topfrp, row, col)
4788 frame_T *topfrp;
4789 int *row;
4790 int *col;
4791{
4792 win_T *wp;
4793 frame_T *frp;
4794#ifdef FEAT_VERTSPLIT
4795 int startcol;
4796 int startrow;
4797#endif
4798
4799 wp = topfrp->fr_win;
4800 if (wp != NULL)
4801 {
4802 if (wp->w_winrow != *row
4803#ifdef FEAT_VERTSPLIT
4804 || wp->w_wincol != *col
4805#endif
4806 )
4807 {
4808 /* position changed, redraw */
4809 wp->w_winrow = *row;
4810#ifdef FEAT_VERTSPLIT
4811 wp->w_wincol = *col;
4812#endif
4813 redraw_win_later(wp, NOT_VALID);
4814 wp->w_redr_status = TRUE;
4815 }
4816 *row += wp->w_height + wp->w_status_height;
4817#ifdef FEAT_VERTSPLIT
4818 *col += wp->w_width + wp->w_vsep_width;
4819#endif
4820 }
4821 else
4822 {
4823#ifdef FEAT_VERTSPLIT
4824 startrow = *row;
4825 startcol = *col;
4826#endif
4827 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4828 {
4829#ifdef FEAT_VERTSPLIT
4830 if (topfrp->fr_layout == FR_ROW)
4831 *row = startrow; /* all frames are at the same row */
4832 else
4833 *col = startcol; /* all frames are at the same col */
4834#endif
4835 frame_comp_pos(frp, row, col);
4836 }
4837 }
4838}
4839
4840#endif /* FEAT_WINDOWS */
4841
4842/*
4843 * Set current window height and take care of repositioning other windows to
4844 * fit around it.
4845 */
4846 void
4847win_setheight(height)
4848 int height;
4849{
4850 win_setheight_win(height, curwin);
4851}
4852
4853/*
4854 * Set the window height of window "win" and take care of repositioning other
4855 * windows to fit around it.
4856 */
4857 void
4858win_setheight_win(height, win)
4859 int height;
4860 win_T *win;
4861{
4862 int row;
4863
4864 if (win == curwin)
4865 {
4866 /* Always keep current window at least one line high, even when
4867 * 'winminheight' is zero. */
4868#ifdef FEAT_WINDOWS
4869 if (height < p_wmh)
4870 height = p_wmh;
4871#endif
4872 if (height == 0)
4873 height = 1;
4874 }
4875
4876#ifdef FEAT_WINDOWS
4877 frame_setheight(win->w_frame, height + win->w_status_height);
4878
4879 /* recompute the window positions */
4880 row = win_comp_pos();
4881#else
4882 if (height > topframe->fr_height)
4883 height = topframe->fr_height;
4884 win->w_height = height;
4885 row = height;
4886#endif
4887
4888 /*
4889 * If there is extra space created between the last window and the command
4890 * line, clear it.
4891 */
4892 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4893 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4894 cmdline_row = row;
4895 msg_row = row;
4896 msg_col = 0;
4897
4898 redraw_all_later(NOT_VALID);
4899}
4900
4901#if defined(FEAT_WINDOWS) || defined(PROTO)
4902
4903/*
4904 * Set the height of a frame to "height" and take care that all frames and
4905 * windows inside it are resized. Also resize frames on the left and right if
4906 * the are in the same FR_ROW frame.
4907 *
4908 * Strategy:
4909 * If the frame is part of a FR_COL frame, try fitting the frame in that
4910 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4911 * go to containing frames to resize them and make room.
4912 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4913 * Check for the minimal height of the FR_ROW frame.
4914 * At the top level we can also use change the command line height.
4915 */
4916 static void
4917frame_setheight(curfrp, height)
4918 frame_T *curfrp;
4919 int height;
4920{
4921 int room; /* total number of lines available */
4922 int take; /* number of lines taken from other windows */
4923 int room_cmdline; /* lines available from cmdline */
4924 int run;
4925 frame_T *frp;
4926 int h;
4927 int room_reserved;
4928
4929 /* If the height already is the desired value, nothing to do. */
4930 if (curfrp->fr_height == height)
4931 return;
4932
4933 if (curfrp->fr_parent == NULL)
4934 {
4935 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004936 if (height > ROWS_AVAIL)
4937 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 if (height > 0)
4939 frame_new_height(curfrp, height, FALSE, FALSE);
4940 }
4941 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4942 {
4943 /* Row of frames: Also need to resize frames left and right of this
4944 * one. First check for the minimal height of these. */
4945 h = frame_minheight(curfrp->fr_parent, NULL);
4946 if (height < h)
4947 height = h;
4948 frame_setheight(curfrp->fr_parent, height);
4949 }
4950 else
4951 {
4952 /*
4953 * Column of frames: try to change only frames in this column.
4954 */
4955#ifdef FEAT_VERTSPLIT
4956 /*
4957 * Do this twice:
4958 * 1: compute room available, if it's not enough try resizing the
4959 * containing frame.
4960 * 2: compute the room available and adjust the height to it.
4961 * Try not to reduce the height of a window with 'winfixheight' set.
4962 */
4963 for (run = 1; run <= 2; ++run)
4964#else
4965 for (;;)
4966#endif
4967 {
4968 room = 0;
4969 room_reserved = 0;
4970 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4971 frp = frp->fr_next)
4972 {
4973 if (frp != curfrp
4974 && frp->fr_win != NULL
4975 && frp->fr_win->w_p_wfh)
4976 room_reserved += frp->fr_height;
4977 room += frp->fr_height;
4978 if (frp != curfrp)
4979 room -= frame_minheight(frp, NULL);
4980 }
4981#ifdef FEAT_VERTSPLIT
4982 if (curfrp->fr_width != Columns)
4983 room_cmdline = 0;
4984 else
4985#endif
4986 {
4987 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4988 + lastwin->w_height + lastwin->w_status_height);
4989 if (room_cmdline < 0)
4990 room_cmdline = 0;
4991 }
4992
4993 if (height <= room + room_cmdline)
4994 break;
4995#ifdef FEAT_VERTSPLIT
4996 if (run == 2 || curfrp->fr_width == Columns)
4997#endif
4998 {
4999 if (height > room + room_cmdline)
5000 height = room + room_cmdline;
5001 break;
5002 }
5003#ifdef FEAT_VERTSPLIT
5004 frame_setheight(curfrp->fr_parent, height
5005 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5006#endif
5007 /*NOTREACHED*/
5008 }
5009
5010 /*
5011 * Compute the number of lines we will take from others frames (can be
5012 * negative!).
5013 */
5014 take = height - curfrp->fr_height;
5015
5016 /* If there is not enough room, also reduce the height of a window
5017 * with 'winfixheight' set. */
5018 if (height > room + room_cmdline - room_reserved)
5019 room_reserved = room + room_cmdline - height;
5020 /* If there is only a 'winfixheight' window and making the
5021 * window smaller, need to make the other window taller. */
5022 if (take < 0 && room - curfrp->fr_height < room_reserved)
5023 room_reserved = 0;
5024
5025 if (take > 0 && room_cmdline > 0)
5026 {
5027 /* use lines from cmdline first */
5028 if (take < room_cmdline)
5029 room_cmdline = take;
5030 take -= room_cmdline;
5031 topframe->fr_height += room_cmdline;
5032 }
5033
5034 /*
5035 * set the current frame to the new height
5036 */
5037 frame_new_height(curfrp, height, FALSE, FALSE);
5038
5039 /*
5040 * First take lines from the frames after the current frame. If
5041 * that is not enough, takes lines from frames above the current
5042 * frame.
5043 */
5044 for (run = 0; run < 2; ++run)
5045 {
5046 if (run == 0)
5047 frp = curfrp->fr_next; /* 1st run: start with next window */
5048 else
5049 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5050 while (frp != NULL && take != 0)
5051 {
5052 h = frame_minheight(frp, NULL);
5053 if (room_reserved > 0
5054 && frp->fr_win != NULL
5055 && frp->fr_win->w_p_wfh)
5056 {
5057 if (room_reserved >= frp->fr_height)
5058 room_reserved -= frp->fr_height;
5059 else
5060 {
5061 if (frp->fr_height - room_reserved > take)
5062 room_reserved = frp->fr_height - take;
5063 take -= frp->fr_height - room_reserved;
5064 frame_new_height(frp, room_reserved, FALSE, FALSE);
5065 room_reserved = 0;
5066 }
5067 }
5068 else
5069 {
5070 if (frp->fr_height - take < h)
5071 {
5072 take -= frp->fr_height - h;
5073 frame_new_height(frp, h, FALSE, FALSE);
5074 }
5075 else
5076 {
5077 frame_new_height(frp, frp->fr_height - take,
5078 FALSE, FALSE);
5079 take = 0;
5080 }
5081 }
5082 if (run == 0)
5083 frp = frp->fr_next;
5084 else
5085 frp = frp->fr_prev;
5086 }
5087 }
5088 }
5089}
5090
5091#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5092/*
5093 * Set current window width and take care of repositioning other windows to
5094 * fit around it.
5095 */
5096 void
5097win_setwidth(width)
5098 int width;
5099{
5100 win_setwidth_win(width, curwin);
5101}
5102
5103 void
5104win_setwidth_win(width, wp)
5105 int width;
5106 win_T *wp;
5107{
5108 /* Always keep current window at least one column wide, even when
5109 * 'winminwidth' is zero. */
5110 if (wp == curwin)
5111 {
5112 if (width < p_wmw)
5113 width = p_wmw;
5114 if (width == 0)
5115 width = 1;
5116 }
5117
5118 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5119
5120 /* recompute the window positions */
5121 (void)win_comp_pos();
5122
5123 redraw_all_later(NOT_VALID);
5124}
5125
5126/*
5127 * Set the width of a frame to "width" and take care that all frames and
5128 * windows inside it are resized. Also resize frames above and below if the
5129 * are in the same FR_ROW frame.
5130 *
5131 * Strategy is similar to frame_setheight().
5132 */
5133 static void
5134frame_setwidth(curfrp, width)
5135 frame_T *curfrp;
5136 int width;
5137{
5138 int room; /* total number of lines available */
5139 int take; /* number of lines taken from other windows */
5140 int run;
5141 frame_T *frp;
5142 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005143 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
5145 /* If the width already is the desired value, nothing to do. */
5146 if (curfrp->fr_width == width)
5147 return;
5148
5149 if (curfrp->fr_parent == NULL)
5150 /* topframe: can't change width */
5151 return;
5152
5153 if (curfrp->fr_parent->fr_layout == FR_COL)
5154 {
5155 /* Column of frames: Also need to resize frames above and below of
5156 * this one. First check for the minimal width of these. */
5157 w = frame_minwidth(curfrp->fr_parent, NULL);
5158 if (width < w)
5159 width = w;
5160 frame_setwidth(curfrp->fr_parent, width);
5161 }
5162 else
5163 {
5164 /*
5165 * Row of frames: try to change only frames in this row.
5166 *
5167 * Do this twice:
5168 * 1: compute room available, if it's not enough try resizing the
5169 * containing frame.
5170 * 2: compute the room available and adjust the width to it.
5171 */
5172 for (run = 1; run <= 2; ++run)
5173 {
5174 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005175 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5177 frp = frp->fr_next)
5178 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005179 if (frp != curfrp
5180 && frp->fr_win != NULL
5181 && frp->fr_win->w_p_wfw)
5182 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 room += frp->fr_width;
5184 if (frp != curfrp)
5185 room -= frame_minwidth(frp, NULL);
5186 }
5187
5188 if (width <= room)
5189 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005190 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 {
5192 if (width > room)
5193 width = room;
5194 break;
5195 }
5196 frame_setwidth(curfrp->fr_parent, width
5197 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5198 }
5199
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 /*
5201 * Compute the number of lines we will take from others frames (can be
5202 * negative!).
5203 */
5204 take = width - curfrp->fr_width;
5205
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005206 /* If there is not enough room, also reduce the width of a window
5207 * with 'winfixwidth' set. */
5208 if (width > room - room_reserved)
5209 room_reserved = room - width;
5210 /* If there is only a 'winfixwidth' window and making the
5211 * window smaller, need to make the other window narrower. */
5212 if (take < 0 && room - curfrp->fr_width < room_reserved)
5213 room_reserved = 0;
5214
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 /*
5216 * set the current frame to the new width
5217 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005218 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219
5220 /*
5221 * First take lines from the frames right of the current frame. If
5222 * that is not enough, takes lines from frames left of the current
5223 * frame.
5224 */
5225 for (run = 0; run < 2; ++run)
5226 {
5227 if (run == 0)
5228 frp = curfrp->fr_next; /* 1st run: start with next window */
5229 else
5230 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5231 while (frp != NULL && take != 0)
5232 {
5233 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005234 if (room_reserved > 0
5235 && frp->fr_win != NULL
5236 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005238 if (room_reserved >= frp->fr_width)
5239 room_reserved -= frp->fr_width;
5240 else
5241 {
5242 if (frp->fr_width - room_reserved > take)
5243 room_reserved = frp->fr_width - take;
5244 take -= frp->fr_width - room_reserved;
5245 frame_new_width(frp, room_reserved, FALSE, FALSE);
5246 room_reserved = 0;
5247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 }
5249 else
5250 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005251 if (frp->fr_width - take < w)
5252 {
5253 take -= frp->fr_width - w;
5254 frame_new_width(frp, w, FALSE, FALSE);
5255 }
5256 else
5257 {
5258 frame_new_width(frp, frp->fr_width - take,
5259 FALSE, FALSE);
5260 take = 0;
5261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 }
5263 if (run == 0)
5264 frp = frp->fr_next;
5265 else
5266 frp = frp->fr_prev;
5267 }
5268 }
5269 }
5270}
5271#endif /* FEAT_VERTSPLIT */
5272
5273/*
5274 * Check 'winminheight' for a valid value.
5275 */
5276 void
5277win_setminheight()
5278{
5279 int room;
5280 int first = TRUE;
5281 win_T *wp;
5282
5283 /* loop until there is a 'winminheight' that is possible */
5284 while (p_wmh > 0)
5285 {
5286 /* TODO: handle vertical splits */
5287 room = -p_wh;
5288 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5289 room += wp->w_height - p_wmh;
5290 if (room >= 0)
5291 break;
5292 --p_wmh;
5293 if (first)
5294 {
5295 EMSG(_(e_noroom));
5296 first = FALSE;
5297 }
5298 }
5299}
5300
5301#ifdef FEAT_MOUSE
5302
5303/*
5304 * Status line of dragwin is dragged "offset" lines down (negative is up).
5305 */
5306 void
5307win_drag_status_line(dragwin, offset)
5308 win_T *dragwin;
5309 int offset;
5310{
5311 frame_T *curfr;
5312 frame_T *fr;
5313 int room;
5314 int row;
5315 int up; /* if TRUE, drag status line up, otherwise down */
5316 int n;
5317
5318 fr = dragwin->w_frame;
5319 curfr = fr;
5320 if (fr != topframe) /* more than one window */
5321 {
5322 fr = fr->fr_parent;
5323 /* When the parent frame is not a column of frames, its parent should
5324 * be. */
5325 if (fr->fr_layout != FR_COL)
5326 {
5327 curfr = fr;
5328 if (fr != topframe) /* only a row of windows, may drag statusline */
5329 fr = fr->fr_parent;
5330 }
5331 }
5332
5333 /* If this is the last frame in a column, may want to resize the parent
5334 * frame instead (go two up to skip a row of frames). */
5335 while (curfr != topframe && curfr->fr_next == NULL)
5336 {
5337 if (fr != topframe)
5338 fr = fr->fr_parent;
5339 curfr = fr;
5340 if (fr != topframe)
5341 fr = fr->fr_parent;
5342 }
5343
5344 if (offset < 0) /* drag up */
5345 {
5346 up = TRUE;
5347 offset = -offset;
5348 /* sum up the room of the current frame and above it */
5349 if (fr == curfr)
5350 {
5351 /* only one window */
5352 room = fr->fr_height - frame_minheight(fr, NULL);
5353 }
5354 else
5355 {
5356 room = 0;
5357 for (fr = fr->fr_child; ; fr = fr->fr_next)
5358 {
5359 room += fr->fr_height - frame_minheight(fr, NULL);
5360 if (fr == curfr)
5361 break;
5362 }
5363 }
5364 fr = curfr->fr_next; /* put fr at frame that grows */
5365 }
5366 else /* drag down */
5367 {
5368 up = FALSE;
5369 /*
5370 * Only dragging the last status line can reduce p_ch.
5371 */
5372 room = Rows - cmdline_row;
5373 if (curfr->fr_next == NULL)
5374 room -= 1;
5375 else
5376 room -= p_ch;
5377 if (room < 0)
5378 room = 0;
5379 /* sum up the room of frames below of the current one */
5380 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5381 room += fr->fr_height - frame_minheight(fr, NULL);
5382 fr = curfr; /* put fr at window that grows */
5383 }
5384
5385 if (room < offset) /* Not enough room */
5386 offset = room; /* Move as far as we can */
5387 if (offset <= 0)
5388 return;
5389
5390 /*
5391 * Grow frame fr by "offset" lines.
5392 * Doesn't happen when dragging the last status line up.
5393 */
5394 if (fr != NULL)
5395 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5396
5397 if (up)
5398 fr = curfr; /* current frame gets smaller */
5399 else
5400 fr = curfr->fr_next; /* next frame gets smaller */
5401
5402 /*
5403 * Now make the other frames smaller.
5404 */
5405 while (fr != NULL && offset > 0)
5406 {
5407 n = frame_minheight(fr, NULL);
5408 if (fr->fr_height - offset <= n)
5409 {
5410 offset -= fr->fr_height - n;
5411 frame_new_height(fr, n, !up, FALSE);
5412 }
5413 else
5414 {
5415 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5416 break;
5417 }
5418 if (up)
5419 fr = fr->fr_prev;
5420 else
5421 fr = fr->fr_next;
5422 }
5423 row = win_comp_pos();
5424 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5425 cmdline_row = row;
5426 p_ch = Rows - cmdline_row;
5427 if (p_ch < 1)
5428 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005429 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005430 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 showmode();
5432}
5433
5434#ifdef FEAT_VERTSPLIT
5435/*
5436 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5437 */
5438 void
5439win_drag_vsep_line(dragwin, offset)
5440 win_T *dragwin;
5441 int offset;
5442{
5443 frame_T *curfr;
5444 frame_T *fr;
5445 int room;
5446 int left; /* if TRUE, drag separator line left, otherwise right */
5447 int n;
5448
5449 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005450 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 return;
5452 curfr = fr;
5453 fr = fr->fr_parent;
5454 /* When the parent frame is not a row of frames, its parent should be. */
5455 if (fr->fr_layout != FR_ROW)
5456 {
5457 if (fr == topframe) /* only a column of windows (cannot happen?) */
5458 return;
5459 curfr = fr;
5460 fr = fr->fr_parent;
5461 }
5462
5463 /* If this is the last frame in a row, may want to resize a parent
5464 * frame instead. */
5465 while (curfr->fr_next == NULL)
5466 {
5467 if (fr == topframe)
5468 break;
5469 curfr = fr;
5470 fr = fr->fr_parent;
5471 if (fr != topframe)
5472 {
5473 curfr = fr;
5474 fr = fr->fr_parent;
5475 }
5476 }
5477
5478 if (offset < 0) /* drag left */
5479 {
5480 left = TRUE;
5481 offset = -offset;
5482 /* sum up the room of the current frame and left of it */
5483 room = 0;
5484 for (fr = fr->fr_child; ; fr = fr->fr_next)
5485 {
5486 room += fr->fr_width - frame_minwidth(fr, NULL);
5487 if (fr == curfr)
5488 break;
5489 }
5490 fr = curfr->fr_next; /* put fr at frame that grows */
5491 }
5492 else /* drag right */
5493 {
5494 left = FALSE;
5495 /* sum up the room of frames right of the current one */
5496 room = 0;
5497 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5498 room += fr->fr_width - frame_minwidth(fr, NULL);
5499 fr = curfr; /* put fr at window that grows */
5500 }
5501
5502 if (room < offset) /* Not enough room */
5503 offset = room; /* Move as far as we can */
5504 if (offset <= 0) /* No room at all, quit. */
5505 return;
5506
5507 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005508 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509
5510 /* shrink other frames: current and at the left or at the right */
5511 if (left)
5512 fr = curfr; /* current frame gets smaller */
5513 else
5514 fr = curfr->fr_next; /* next frame gets smaller */
5515
5516 while (fr != NULL && offset > 0)
5517 {
5518 n = frame_minwidth(fr, NULL);
5519 if (fr->fr_width - offset <= n)
5520 {
5521 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005522 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 }
5524 else
5525 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005526 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 break;
5528 }
5529 if (left)
5530 fr = fr->fr_prev;
5531 else
5532 fr = fr->fr_next;
5533 }
5534 (void)win_comp_pos();
5535 redraw_all_later(NOT_VALID);
5536}
5537#endif /* FEAT_VERTSPLIT */
5538#endif /* FEAT_MOUSE */
5539
5540#endif /* FEAT_WINDOWS */
5541
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005542#define FRACTION_MULT 16384L
5543
5544/*
5545 * Set wp->w_fraction for the current w_wrow and w_height.
5546 */
5547 static void
5548set_fraction(wp)
5549 win_T *wp;
5550{
5551 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5552 + FRACTION_MULT / 2) / (long)wp->w_height;
5553}
5554
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555/*
5556 * Set the height of a window.
5557 * This takes care of the things inside the window, not what happens to the
5558 * window position, the frame or to other windows.
5559 */
5560 static void
5561win_new_height(wp, height)
5562 win_T *wp;
5563 int height;
5564{
5565 linenr_T lnum;
5566 int sline, line_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567
5568 /* Don't want a negative height. Happens when splitting a tiny window.
5569 * Will equalize heights soon to fix it. */
5570 if (height < 0)
5571 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005572 if (wp->w_height == height)
5573 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
5575 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005576 set_fraction(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577
5578 wp->w_height = height;
5579 wp->w_skipcol = 0;
5580
5581 /* Don't change w_topline when height is zero. Don't set w_topline when
5582 * 'scrollbind' is set and this isn't the current window. */
5583 if (height > 0
5584#ifdef FEAT_SCROLLBIND
5585 && (!wp->w_p_scb || wp == curwin)
5586#endif
5587 )
5588 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005589 /*
5590 * Find a value for w_topline that shows the cursor at the same
5591 * relative position in the window as before (more or less).
5592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 lnum = wp->w_cursor.lnum;
5594 if (lnum < 1) /* can happen when starting up */
5595 lnum = 1;
5596 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5597 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5598 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005599
5600 if (sline >= 0)
5601 {
5602 /* Make sure the whole cursor line is visible, if possible. */
5603 int rows = plines_win(wp, lnum, FALSE);
5604
5605 if (sline > wp->w_height - rows)
5606 {
5607 sline = wp->w_height - rows;
5608 wp->w_wrow -= rows - line_size;
5609 }
5610 }
5611
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 if (sline < 0)
5613 {
5614 /*
5615 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005616 * Make cursor line the first line in the window. If not enough
5617 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 */
5619 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005620 if (wp->w_wrow >= wp->w_height
5621 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5622 {
5623 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5624 --wp->w_wrow;
5625 while (wp->w_wrow >= wp->w_height)
5626 {
5627 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5628 + win_col_off2(wp);
5629 --wp->w_wrow;
5630 }
5631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 }
5633 else
5634 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005635 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 {
5637#ifdef FEAT_FOLDING
5638 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5639 if (lnum == 1)
5640 {
5641 /* first line in buffer is folded */
5642 line_size = 1;
5643 --sline;
5644 break;
5645 }
5646#endif
5647 --lnum;
5648#ifdef FEAT_DIFF
5649 if (lnum == wp->w_topline)
5650 line_size = plines_win_nofill(wp, lnum, TRUE)
5651 + wp->w_topfill;
5652 else
5653#endif
5654 line_size = plines_win(wp, lnum, TRUE);
5655 sline -= line_size;
5656 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005657
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 if (sline < 0)
5659 {
5660 /*
5661 * Line we want at top would go off top of screen. Use next
5662 * line instead.
5663 */
5664#ifdef FEAT_FOLDING
5665 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5666#endif
5667 lnum++;
5668 wp->w_wrow -= line_size + sline;
5669 }
5670 else if (sline > 0)
5671 {
5672 /* First line of file reached, use that as topline. */
5673 lnum = 1;
5674 wp->w_wrow -= sline;
5675 }
5676 }
5677 set_topline(wp, lnum);
5678 }
5679
5680 if (wp == curwin)
5681 {
5682 if (p_so)
5683 update_topline();
5684 curs_columns(FALSE); /* validate w_wrow */
5685 }
5686 wp->w_prev_fraction_row = wp->w_wrow;
5687
5688 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005689 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690#ifdef FEAT_WINDOWS
5691 wp->w_redr_status = TRUE;
5692#endif
5693 invalidate_botline_win(wp);
5694}
5695
5696#ifdef FEAT_VERTSPLIT
5697/*
5698 * Set the width of a window.
5699 */
5700 static void
5701win_new_width(wp, width)
5702 win_T *wp;
5703 int width;
5704{
5705 wp->w_width = width;
5706 wp->w_lines_valid = 0;
5707 changed_line_abv_curs_win(wp);
5708 invalidate_botline_win(wp);
5709 if (wp == curwin)
5710 {
5711 update_topline();
5712 curs_columns(TRUE); /* validate w_wrow */
5713 }
5714 redraw_win_later(wp, NOT_VALID);
5715 wp->w_redr_status = TRUE;
5716}
5717#endif
5718
5719 void
5720win_comp_scroll(wp)
5721 win_T *wp;
5722{
5723 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5724 if (wp->w_p_scr == 0)
5725 wp->w_p_scr = 1;
5726}
5727
5728/*
5729 * command_height: called whenever p_ch has been changed
5730 */
5731 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005732command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733{
5734#ifdef FEAT_WINDOWS
5735 int h;
5736 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005737 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005739 /* Use the value of p_ch that we remembered. This is needed for when the
5740 * GUI starts up, we can't be sure in what order things happen. And when
5741 * p_ch was changed in another tab page. */
5742 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005743
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 /* Find bottom frame with width of screen. */
5745 frp = lastwin->w_frame;
5746# ifdef FEAT_VERTSPLIT
5747 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5748 frp = frp->fr_parent;
5749# endif
5750
5751 /* Avoid changing the height of a window with 'winfixheight' set. */
5752 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5753 && frp->fr_win->w_p_wfh)
5754 frp = frp->fr_prev;
5755
5756 if (starting != NO_SCREEN)
5757 {
5758 cmdline_row = Rows - p_ch;
5759
5760 if (p_ch > old_p_ch) /* p_ch got bigger */
5761 {
5762 while (p_ch > old_p_ch)
5763 {
5764 if (frp == NULL)
5765 {
5766 EMSG(_(e_noroom));
5767 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005768 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 cmdline_row = Rows - p_ch;
5770 break;
5771 }
5772 h = frp->fr_height - frame_minheight(frp, NULL);
5773 if (h > p_ch - old_p_ch)
5774 h = p_ch - old_p_ch;
5775 old_p_ch += h;
5776 frame_add_height(frp, -h);
5777 frp = frp->fr_prev;
5778 }
5779
5780 /* Recompute window positions. */
5781 (void)win_comp_pos();
5782
5783 /* clear the lines added to cmdline */
5784 if (full_screen)
5785 screen_fill((int)(cmdline_row), (int)Rows, 0,
5786 (int)Columns, ' ', ' ', 0);
5787 msg_row = cmdline_row;
5788 redraw_cmdline = TRUE;
5789 return;
5790 }
5791
5792 if (msg_row < cmdline_row)
5793 msg_row = cmdline_row;
5794 redraw_cmdline = TRUE;
5795 }
5796 frame_add_height(frp, (int)(old_p_ch - p_ch));
5797
5798 /* Recompute window positions. */
5799 if (frp != lastwin->w_frame)
5800 (void)win_comp_pos();
5801#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005803 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804#endif
5805}
5806
5807#if defined(FEAT_WINDOWS) || defined(PROTO)
5808/*
5809 * Resize frame "frp" to be "n" lines higher (negative for less high).
5810 * Also resize the frames it is contained in.
5811 */
5812 static void
5813frame_add_height(frp, n)
5814 frame_T *frp;
5815 int n;
5816{
5817 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5818 for (;;)
5819 {
5820 frp = frp->fr_parent;
5821 if (frp == NULL)
5822 break;
5823 frp->fr_height += n;
5824 }
5825}
5826
5827/*
5828 * Add or remove a status line for the bottom window(s), according to the
5829 * value of 'laststatus'.
5830 */
5831 void
5832last_status(morewin)
5833 int morewin; /* pretend there are two or more windows */
5834{
5835 /* Don't make a difference between horizontal or vertical split. */
5836 last_status_rec(topframe, (p_ls == 2
5837 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5838}
5839
5840 static void
5841last_status_rec(fr, statusline)
5842 frame_T *fr;
5843 int statusline;
5844{
5845 frame_T *fp;
5846 win_T *wp;
5847
5848 if (fr->fr_layout == FR_LEAF)
5849 {
5850 wp = fr->fr_win;
5851 if (wp->w_status_height != 0 && !statusline)
5852 {
5853 /* remove status line */
5854 win_new_height(wp, wp->w_height + 1);
5855 wp->w_status_height = 0;
5856 comp_col();
5857 }
5858 else if (wp->w_status_height == 0 && statusline)
5859 {
5860 /* Find a frame to take a line from. */
5861 fp = fr;
5862 while (fp->fr_height <= frame_minheight(fp, NULL))
5863 {
5864 if (fp == topframe)
5865 {
5866 EMSG(_(e_noroom));
5867 return;
5868 }
5869 /* In a column of frames: go to frame above. If already at
5870 * the top or in a row of frames: go to parent. */
5871 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5872 fp = fp->fr_prev;
5873 else
5874 fp = fp->fr_parent;
5875 }
5876 wp->w_status_height = 1;
5877 if (fp != fr)
5878 {
5879 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5880 frame_fix_height(wp);
5881 (void)win_comp_pos();
5882 }
5883 else
5884 win_new_height(wp, wp->w_height - 1);
5885 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005886 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 }
5888 }
5889#ifdef FEAT_VERTSPLIT
5890 else if (fr->fr_layout == FR_ROW)
5891 {
5892 /* vertically split windows, set status line for each one */
5893 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5894 last_status_rec(fp, statusline);
5895 }
5896#endif
5897 else
5898 {
5899 /* horizontally split window, set status line for last one */
5900 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5901 ;
5902 last_status_rec(fp, statusline);
5903 }
5904}
5905
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005906/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005907 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005908 */
5909 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005910tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005911{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005912#ifdef FEAT_GUI_TABLINE
5913 /* When the GUI has the tabline then this always returns zero. */
5914 if (gui_use_tabline())
5915 return 0;
5916#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005917 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005918 {
5919 case 0: return 0;
5920 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5921 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005922 return 1;
5923}
5924
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925#endif /* FEAT_WINDOWS */
5926
5927#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5928/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005929 * Get the file name at the cursor.
5930 * If Visual mode is active, use the selected text if it's in one line.
5931 * Returns the name in allocated memory, NULL for failure.
5932 */
5933 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005934grab_file_name(count, file_lnum)
5935 long count;
5936 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005937{
5938# ifdef FEAT_VISUAL
5939 if (VIsual_active)
5940 {
5941 int len;
5942 char_u *ptr;
5943
5944 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5945 return NULL;
5946 return find_file_name_in_path(ptr, len,
5947 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5948 }
5949# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005950 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5951 file_lnum);
5952
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005953}
5954
5955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 * Return the file name under or after the cursor.
5957 *
5958 * The 'path' option is searched if the file name is not absolute.
5959 * The string returned has been alloc'ed and should be freed by the caller.
5960 * NULL is returned if the file name or file is not found.
5961 *
5962 * options:
5963 * FNAME_MESS give error messages
5964 * FNAME_EXP expand to path
5965 * FNAME_HYP check for hypertext link
5966 * FNAME_INCL apply "includeexpr"
5967 */
5968 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005969file_name_at_cursor(options, count, file_lnum)
5970 int options;
5971 long count;
5972 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973{
5974 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005975 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5976 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977}
5978
5979/*
5980 * Return the name of the file under or after ptr[col].
5981 * Otherwise like file_name_at_cursor().
5982 */
5983 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005984file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 char_u *line;
5986 int col;
5987 int options;
5988 long count;
5989 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005990 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991{
5992 char_u *ptr;
5993 int len;
5994
5995 /*
5996 * search forward for what could be the start of a file name
5997 */
5998 ptr = line + col;
5999 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006000 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 if (*ptr == NUL) /* nothing found */
6002 {
6003 if (options & FNAME_MESS)
6004 EMSG(_("E446: No file name under cursor"));
6005 return NULL;
6006 }
6007
6008 /*
6009 * Search backward for first char of the file name.
6010 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6011 */
6012 while (ptr > line)
6013 {
6014#ifdef FEAT_MBYTE
6015 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6016 ptr -= len + 1;
6017 else
6018#endif
6019 if (vim_isfilec(ptr[-1])
6020 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6021 --ptr;
6022 else
6023 break;
6024 }
6025
6026 /*
6027 * Search forward for the last char of the file name.
6028 * Also allow "://" when ':' is not in 'isfname'.
6029 */
6030 len = 0;
6031 while (vim_isfilec(ptr[len])
6032 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6033#ifdef FEAT_MBYTE
6034 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006035 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 else
6037#endif
6038 ++len;
6039
6040 /*
6041 * If there is trailing punctuation, remove it.
6042 * But don't remove "..", could be a directory name.
6043 */
6044 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6045 && ptr[len - 2] != '.')
6046 --len;
6047
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006048 if (file_lnum != NULL)
6049 {
6050 char_u *p;
6051
6052 /* Get the number after the file name and a separator character */
6053 p = ptr + len;
6054 p = skipwhite(p);
6055 if (*p != NUL)
6056 {
6057 if (!isdigit(*p))
6058 ++p; /* skip the separator */
6059 p = skipwhite(p);
6060 if (isdigit(*p))
6061 *file_lnum = (int)getdigits(&p);
6062 }
6063 }
6064
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6066}
6067
6068# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6069static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6070
6071 static char_u *
6072eval_includeexpr(ptr, len)
6073 char_u *ptr;
6074 int len;
6075{
6076 char_u *res;
6077
6078 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006079 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006080 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 set_vim_var_string(VV_FNAME, NULL, 0);
6082 return res;
6083}
6084#endif
6085
6086/*
6087 * Return the name of the file ptr[len] in 'path'.
6088 * Otherwise like file_name_at_cursor().
6089 */
6090 char_u *
6091find_file_name_in_path(ptr, len, options, count, rel_fname)
6092 char_u *ptr;
6093 int len;
6094 int options;
6095 long count;
6096 char_u *rel_fname; /* file we are searching relative to */
6097{
6098 char_u *file_name;
6099 int c;
6100# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6101 char_u *tofree = NULL;
6102
6103 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6104 {
6105 tofree = eval_includeexpr(ptr, len);
6106 if (tofree != NULL)
6107 {
6108 ptr = tofree;
6109 len = (int)STRLEN(ptr);
6110 }
6111 }
6112# endif
6113
6114 if (options & FNAME_EXP)
6115 {
6116 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6117 TRUE, rel_fname);
6118
6119# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6120 /*
6121 * If the file could not be found in a normal way, try applying
6122 * 'includeexpr' (unless done already).
6123 */
6124 if (file_name == NULL
6125 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6126 {
6127 tofree = eval_includeexpr(ptr, len);
6128 if (tofree != NULL)
6129 {
6130 ptr = tofree;
6131 len = (int)STRLEN(ptr);
6132 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6133 TRUE, rel_fname);
6134 }
6135 }
6136# endif
6137 if (file_name == NULL && (options & FNAME_MESS))
6138 {
6139 c = ptr[len];
6140 ptr[len] = NUL;
6141 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6142 ptr[len] = c;
6143 }
6144
6145 /* Repeat finding the file "count" times. This matters when it
6146 * appears several times in the path. */
6147 while (file_name != NULL && --count > 0)
6148 {
6149 vim_free(file_name);
6150 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6151 }
6152 }
6153 else
6154 file_name = vim_strnsave(ptr, len);
6155
6156# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6157 vim_free(tofree);
6158# endif
6159
6160 return file_name;
6161}
6162#endif /* FEAT_SEARCHPATH */
6163
6164/*
6165 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6166 * Also check for ":\\", which MS Internet Explorer accepts, return
6167 * URL_BACKSLASH.
6168 */
6169 static int
6170path_is_url(p)
6171 char_u *p;
6172{
6173 if (STRNCMP(p, "://", (size_t)3) == 0)
6174 return URL_SLASH;
6175 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6176 return URL_BACKSLASH;
6177 return 0;
6178}
6179
6180/*
6181 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6182 * Return URL_BACKSLASH for "name:\\".
6183 * Return zero otherwise.
6184 */
6185 int
6186path_with_url(fname)
6187 char_u *fname;
6188{
6189 char_u *p;
6190
6191 for (p = fname; isalpha(*p); ++p)
6192 ;
6193 return path_is_url(p);
6194}
6195
6196/*
6197 * Return TRUE if "name" is a full (absolute) path name or URL.
6198 */
6199 int
6200vim_isAbsName(name)
6201 char_u *name;
6202{
6203 return (path_with_url(name) != 0 || mch_isFullName(name));
6204}
6205
6206/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006207 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 *
6209 * return FAIL for failure, OK otherwise
6210 */
6211 int
6212vim_FullName(fname, buf, len, force)
6213 char_u *fname, *buf;
6214 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006215 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216{
6217 int retval = OK;
6218 int url;
6219
6220 *buf = NUL;
6221 if (fname == NULL)
6222 return FAIL;
6223
6224 url = path_with_url(fname);
6225 if (!url)
6226 retval = mch_FullName(fname, buf, len, force);
6227 if (url || retval == FAIL)
6228 {
6229 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006230 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 }
6232#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6233 slash_adjust(buf);
6234#endif
6235 return retval;
6236}
6237
6238/*
6239 * Return the minimal number of rows that is needed on the screen to display
6240 * the current number of windows.
6241 */
6242 int
6243min_rows()
6244{
6245 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006246#ifdef FEAT_WINDOWS
6247 tabpage_T *tp;
6248 int n;
6249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250
6251 if (firstwin == NULL) /* not initialized yet */
6252 return MIN_LINES;
6253
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006255 total = 0;
6256 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6257 {
6258 n = frame_minheight(tp->tp_topframe, NULL);
6259 if (total < n)
6260 total = n;
6261 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006262 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006264 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006266 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 return total;
6268}
6269
6270/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006271 * Return TRUE if there is only one window (in the current tab page), not
6272 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006273 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 */
6275 int
6276only_one_window()
6277{
6278#ifdef FEAT_WINDOWS
6279 int count = 0;
6280 win_T *wp;
6281
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006282 /* If there is another tab page there always is another window. */
6283 if (first_tabpage->tp_next != NULL)
6284 return FALSE;
6285
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006287 if ((!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288# ifdef FEAT_QUICKFIX
6289 || wp->w_p_pvw
6290# endif
6291 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006292# ifdef FEAT_AUTOCMD
6293 && wp != aucmd_win
6294# endif
6295 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 ++count;
6297 return (count <= 1);
6298#else
6299 return TRUE;
6300#endif
6301}
6302
6303#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6304/*
6305 * Correct the cursor line number in other windows. Used after changing the
6306 * current buffer, and before applying autocommands.
6307 * When "do_curwin" is TRUE, also check current window.
6308 */
6309 void
6310check_lnums(do_curwin)
6311 int do_curwin;
6312{
6313 win_T *wp;
6314
6315#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006316 tabpage_T *tp;
6317
6318 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6320#else
6321 wp = curwin;
6322 if (do_curwin)
6323#endif
6324 {
6325 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6326 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6327 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6328 wp->w_topline = curbuf->b_ml.ml_line_count;
6329 }
6330}
6331#endif
6332
6333#if defined(FEAT_WINDOWS) || defined(PROTO)
6334
6335/*
6336 * A snapshot of the window sizes, to restore them after closing the help
6337 * window.
6338 * Only these fields are used:
6339 * fr_layout
6340 * fr_width
6341 * fr_height
6342 * fr_next
6343 * fr_child
6344 * fr_win (only valid for the old curwin, NULL otherwise)
6345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346
6347/*
6348 * Create a snapshot of the current frame sizes.
6349 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006350 void
6351make_snapshot(idx)
6352 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006354 clear_snapshot(curtab, idx);
6355 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356}
6357
6358 static void
6359make_snapshot_rec(fr, frp)
6360 frame_T *fr;
6361 frame_T **frp;
6362{
6363 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6364 if (*frp == NULL)
6365 return;
6366 (*frp)->fr_layout = fr->fr_layout;
6367# ifdef FEAT_VERTSPLIT
6368 (*frp)->fr_width = fr->fr_width;
6369# endif
6370 (*frp)->fr_height = fr->fr_height;
6371 if (fr->fr_next != NULL)
6372 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6373 if (fr->fr_child != NULL)
6374 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6375 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6376 (*frp)->fr_win = curwin;
6377}
6378
6379/*
6380 * Remove any existing snapshot.
6381 */
6382 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006383clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006384 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006385 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006387 clear_snapshot_rec(tp->tp_snapshot[idx]);
6388 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389}
6390
6391 static void
6392clear_snapshot_rec(fr)
6393 frame_T *fr;
6394{
6395 if (fr != NULL)
6396 {
6397 clear_snapshot_rec(fr->fr_next);
6398 clear_snapshot_rec(fr->fr_child);
6399 vim_free(fr);
6400 }
6401}
6402
6403/*
6404 * Restore a previously created snapshot, if there is any.
6405 * This is only done if the screen size didn't change and the window layout is
6406 * still the same.
6407 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006408 void
6409restore_snapshot(idx, close_curwin)
6410 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 int close_curwin; /* closing current window */
6412{
6413 win_T *wp;
6414
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006415 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006417 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006419 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6420 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006422 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 win_comp_pos();
6424 if (wp != NULL && close_curwin)
6425 win_goto(wp);
6426 redraw_all_later(CLEAR);
6427 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006428 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429}
6430
6431/*
6432 * Check if frames "sn" and "fr" have the same layout, same following frames
6433 * and same children.
6434 */
6435 static int
6436check_snapshot_rec(sn, fr)
6437 frame_T *sn;
6438 frame_T *fr;
6439{
6440 if (sn->fr_layout != fr->fr_layout
6441 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6442 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6443 || (sn->fr_next != NULL
6444 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6445 || (sn->fr_child != NULL
6446 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6447 return FAIL;
6448 return OK;
6449}
6450
6451/*
6452 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6453 * following frames and children.
6454 * Returns a pointer to the old current window, or NULL.
6455 */
6456 static win_T *
6457restore_snapshot_rec(sn, fr)
6458 frame_T *sn;
6459 frame_T *fr;
6460{
6461 win_T *wp = NULL;
6462 win_T *wp2;
6463
6464 fr->fr_height = sn->fr_height;
6465# ifdef FEAT_VERTSPLIT
6466 fr->fr_width = sn->fr_width;
6467# endif
6468 if (fr->fr_layout == FR_LEAF)
6469 {
6470 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6471# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006472 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473# endif
6474 wp = sn->fr_win;
6475 }
6476 if (sn->fr_next != NULL)
6477 {
6478 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6479 if (wp2 != NULL)
6480 wp = wp2;
6481 }
6482 if (sn->fr_child != NULL)
6483 {
6484 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6485 if (wp2 != NULL)
6486 wp = wp2;
6487 }
6488 return wp;
6489}
6490
6491#endif
6492
6493#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6494/*
6495 * Return TRUE if there is any vertically split window.
6496 */
6497 int
6498win_hasvertsplit()
6499{
6500 frame_T *fr;
6501
6502 if (topframe->fr_layout == FR_ROW)
6503 return TRUE;
6504
6505 if (topframe->fr_layout == FR_COL)
6506 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6507 if (fr->fr_layout == FR_ROW)
6508 return TRUE;
6509
6510 return FALSE;
6511}
6512#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006513
6514#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6515/*
6516 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006517 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006518 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6519 * If no particular ID is desired, -1 must be specified for 'id'.
6520 * Return ID of added match, -1 on failure.
6521 */
6522 int
6523match_add(wp, grp, pat, prio, id)
6524 win_T *wp;
6525 char_u *grp;
6526 char_u *pat;
6527 int prio;
6528 int id;
6529{
6530 matchitem_T *cur;
6531 matchitem_T *prev;
6532 matchitem_T *m;
6533 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006534 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006535
6536 if (*grp == NUL || *pat == NUL)
6537 return -1;
6538 if (id < -1 || id == 0)
6539 {
6540 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6541 return -1;
6542 }
6543 if (id != -1)
6544 {
6545 cur = wp->w_match_head;
6546 while (cur != NULL)
6547 {
6548 if (cur->id == id)
6549 {
6550 EMSGN("E801: ID already taken: %ld", id);
6551 return -1;
6552 }
6553 cur = cur->next;
6554 }
6555 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006556 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006557 {
6558 EMSG2(_(e_nogroup), grp);
6559 return -1;
6560 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006561 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006562 {
6563 EMSG2(_(e_invarg2), pat);
6564 return -1;
6565 }
6566
6567 /* Find available match ID. */
6568 while (id == -1)
6569 {
6570 cur = wp->w_match_head;
6571 while (cur != NULL && cur->id != wp->w_next_match_id)
6572 cur = cur->next;
6573 if (cur == NULL)
6574 id = wp->w_next_match_id;
6575 wp->w_next_match_id++;
6576 }
6577
6578 /* Build new match. */
6579 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6580 m->id = id;
6581 m->priority = prio;
6582 m->pattern = vim_strsave(pat);
6583 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006584 m->match.regprog = regprog;
6585 m->match.rmm_ic = FALSE;
6586 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006587
6588 /* Insert new match. The match list is in ascending order with regard to
6589 * the match priorities. */
6590 cur = wp->w_match_head;
6591 prev = cur;
6592 while (cur != NULL && prio >= cur->priority)
6593 {
6594 prev = cur;
6595 cur = cur->next;
6596 }
6597 if (cur == prev)
6598 wp->w_match_head = m;
6599 else
6600 prev->next = m;
6601 m->next = cur;
6602
6603 redraw_later(SOME_VALID);
6604 return id;
6605}
6606
6607/*
6608 * Delete match with ID 'id' in the match list of window 'wp'.
6609 * Print error messages if 'perr' is TRUE.
6610 */
6611 int
6612match_delete(wp, id, perr)
6613 win_T *wp;
6614 int id;
6615 int perr;
6616{
6617 matchitem_T *cur = wp->w_match_head;
6618 matchitem_T *prev = cur;
6619
6620 if (id < 1)
6621 {
6622 if (perr == TRUE)
6623 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6624 id);
6625 return -1;
6626 }
6627 while (cur != NULL && cur->id != id)
6628 {
6629 prev = cur;
6630 cur = cur->next;
6631 }
6632 if (cur == NULL)
6633 {
6634 if (perr == TRUE)
6635 EMSGN("E803: ID not found: %ld", id);
6636 return -1;
6637 }
6638 if (cur == prev)
6639 wp->w_match_head = cur->next;
6640 else
6641 prev->next = cur->next;
6642 vim_free(cur->match.regprog);
6643 vim_free(cur->pattern);
6644 vim_free(cur);
6645 redraw_later(SOME_VALID);
6646 return 0;
6647}
6648
6649/*
6650 * Delete all matches in the match list of window 'wp'.
6651 */
6652 void
6653clear_matches(wp)
6654 win_T *wp;
6655{
6656 matchitem_T *m;
6657
6658 while (wp->w_match_head != NULL)
6659 {
6660 m = wp->w_match_head->next;
6661 vim_free(wp->w_match_head->match.regprog);
6662 vim_free(wp->w_match_head->pattern);
6663 vim_free(wp->w_match_head);
6664 wp->w_match_head = m;
6665 }
6666 redraw_later(SOME_VALID);
6667}
6668
6669/*
6670 * Get match from ID 'id' in window 'wp'.
6671 * Return NULL if match not found.
6672 */
6673 matchitem_T *
6674get_match(wp, id)
6675 win_T *wp;
6676 int id;
6677{
6678 matchitem_T *cur = wp->w_match_head;
6679
6680 while (cur != NULL && cur->id != id)
6681 cur = cur->next;
6682 return cur;
6683}
6684#endif