blob: c2590e5f91344014f6b77804fa46777241fd8664 [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 Moolenaar746ebd32009-06-16 14:01:43 +000026static int one_window __ARGS((void));
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 Moolenaar1d2ba7f2006-02-14 22:29:30 +000048static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
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 Moolenaar071d4272004-06-13 20:20:40 +000073static void win_new_height __ARGS((win_T *, int));
74
75#define URL_SLASH 1 /* path_is_url() has found "://" */
76#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
77
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000078#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Bram Moolenaar05159a02005-02-26 23:04:13 +000080#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000081# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000082#else
83# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000084#endif
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000087
88static char *m_onlyone = N_("Already only one window");
89
Bram Moolenaar071d4272004-06-13 20:20:40 +000090/*
91 * all CTRL-W window commands are handled here, called from normal_cmd().
92 */
93 void
94do_window(nchar, Prenum, xchar)
95 int nchar;
96 long Prenum;
97 int xchar; /* extra char from ":wincmd gx" or NUL */
98{
99 long Prenum1;
100 win_T *wp;
101#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
102 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000103 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105#ifdef FEAT_FIND_ID
106 int type = FIND_DEFINE;
107 int len;
108#endif
109 char_u cbuf[40];
110
111 if (Prenum == 0)
112 Prenum1 = 1;
113 else
114 Prenum1 = Prenum;
115
116#ifdef FEAT_CMDWIN
117# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
118#else
119# define CHECK_CMDWIN
120#endif
121
122 switch (nchar)
123 {
124/* split current window in two parts, horizontally */
125 case 'S':
126 case Ctrl_S:
127 case 's':
128 CHECK_CMDWIN
129#ifdef FEAT_VISUAL
130 reset_VIsual_and_resel(); /* stop Visual mode */
131#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000132#ifdef FEAT_QUICKFIX
133 /* When splitting the quickfix window open a new buffer in it,
134 * don't replicate the quickfix buffer. */
135 if (bt_quickfix(curbuf))
136 goto newwindow;
137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#ifdef FEAT_GUI
139 need_mouse_correct = TRUE;
140#endif
141 win_split((int)Prenum, 0);
142 break;
143
144#ifdef FEAT_VERTSPLIT
145/* split current window in two parts, vertically */
146 case Ctrl_V:
147 case 'v':
148 CHECK_CMDWIN
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000149# ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000151# endif
152# ifdef FEAT_QUICKFIX
153 /* When splitting the quickfix window open a new buffer in it,
154 * don't replicate the quickfix buffer. */
155 if (bt_quickfix(curbuf))
156 goto newwindow;
157# endif
158# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 win_split((int)Prenum, WSP_VERT);
162 break;
163#endif
164
165/* split current window and edit alternate file */
166 case Ctrl_HAT:
167 case '^':
168 CHECK_CMDWIN
169#ifdef FEAT_VISUAL
170 reset_VIsual_and_resel(); /* stop Visual mode */
171#endif
172 STRCPY(cbuf, "split #");
173 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000174 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
175 "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 do_cmdline_cmd(cbuf);
177 break;
178
179/* open new window */
180 case Ctrl_N:
181 case 'n':
182 CHECK_CMDWIN
183#ifdef FEAT_VISUAL
184 reset_VIsual_and_resel(); /* stop Visual mode */
185#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000186#ifdef FEAT_QUICKFIX
187newwindow:
188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000190 /* window height */
191 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 else
193 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000194#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
195 if (nchar == 'v' || nchar == Ctrl_V)
196 STRCAT(cbuf, "v");
197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 STRCAT(cbuf, "new");
199 do_cmdline_cmd(cbuf);
200 break;
201
202/* quit current window */
203 case Ctrl_Q:
204 case 'q':
205#ifdef FEAT_VISUAL
206 reset_VIsual_and_resel(); /* stop Visual mode */
207#endif
208 do_cmdline_cmd((char_u *)"quit");
209 break;
210
211/* close current window */
212 case Ctrl_C:
213 case 'c':
214#ifdef FEAT_VISUAL
215 reset_VIsual_and_resel(); /* stop Visual mode */
216#endif
217 do_cmdline_cmd((char_u *)"close");
218 break;
219
220#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
221/* close preview window */
222 case Ctrl_Z:
223 case 'z':
224 CHECK_CMDWIN
225#ifdef FEAT_VISUAL
226 reset_VIsual_and_resel(); /* stop Visual mode */
227#endif
228 do_cmdline_cmd((char_u *)"pclose");
229 break;
230
231/* cursor to preview window */
232 case 'P':
233 for (wp = firstwin; wp != NULL; wp = wp->w_next)
234 if (wp->w_p_pvw)
235 break;
236 if (wp == NULL)
237 EMSG(_("E441: There is no preview window"));
238 else
239 win_goto(wp);
240 break;
241#endif
242
243/* close all but current window */
244 case Ctrl_O:
245 case 'o':
246 CHECK_CMDWIN
247#ifdef FEAT_VISUAL
248 reset_VIsual_and_resel(); /* stop Visual mode */
249#endif
250 do_cmdline_cmd((char_u *)"only");
251 break;
252
253/* cursor to next window with wrap around */
254 case Ctrl_W:
255 case 'w':
256/* cursor to previous window with wrap around */
257 case 'W':
258 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000259 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 beep_flush();
261 else
262 {
263 if (Prenum) /* go to specified window */
264 {
265 for (wp = firstwin; --Prenum > 0; )
266 {
267 if (wp->w_next == NULL)
268 break;
269 else
270 wp = wp->w_next;
271 }
272 }
273 else
274 {
275 if (nchar == 'W') /* go to previous window */
276 {
277 wp = curwin->w_prev;
278 if (wp == NULL)
279 wp = lastwin; /* wrap around */
280 }
281 else /* go to next window */
282 {
283 wp = curwin->w_next;
284 if (wp == NULL)
285 wp = firstwin; /* wrap around */
286 }
287 }
288 win_goto(wp);
289 }
290 break;
291
292/* cursor to window below */
293 case 'j':
294 case K_DOWN:
295 case Ctrl_J:
296 CHECK_CMDWIN
297#ifdef FEAT_VERTSPLIT
298 win_goto_ver(FALSE, Prenum1);
299#else
300 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
301 wp = wp->w_next)
302 ;
303 win_goto(wp);
304#endif
305 break;
306
307/* cursor to window above */
308 case 'k':
309 case K_UP:
310 case Ctrl_K:
311 CHECK_CMDWIN
312#ifdef FEAT_VERTSPLIT
313 win_goto_ver(TRUE, Prenum1);
314#else
315 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
316 wp = wp->w_prev)
317 ;
318 win_goto(wp);
319#endif
320 break;
321
322#ifdef FEAT_VERTSPLIT
323/* cursor to left window */
324 case 'h':
325 case K_LEFT:
326 case Ctrl_H:
327 case K_BS:
328 CHECK_CMDWIN
329 win_goto_hor(TRUE, Prenum1);
330 break;
331
332/* cursor to right window */
333 case 'l':
334 case K_RIGHT:
335 case Ctrl_L:
336 CHECK_CMDWIN
337 win_goto_hor(FALSE, Prenum1);
338 break;
339#endif
340
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000341/* move window to new tab page */
342 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000343 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000344 MSG(_(m_onlyone));
345 else
346 {
347 tabpage_T *oldtab = curtab;
348 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000349
350 /* First create a new tab with the window, then go back to
351 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000352 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000353 if (win_new_tabpage((int)Prenum) == OK
354 && valid_tabpage(oldtab))
355 {
356 newtab = curtab;
357 goto_tabpage_tp(oldtab);
358 if (curwin == wp)
359 win_close(curwin, FALSE);
360 if (valid_tabpage(newtab))
361 goto_tabpage_tp(newtab);
362 }
363 }
364 break;
365
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366/* cursor to top-left window */
367 case 't':
368 case Ctrl_T:
369 win_goto(firstwin);
370 break;
371
372/* cursor to bottom-right window */
373 case 'b':
374 case Ctrl_B:
375 win_goto(lastwin);
376 break;
377
378/* cursor to last accessed (previous) window */
379 case 'p':
380 case Ctrl_P:
381 if (prevwin == NULL)
382 beep_flush();
383 else
384 win_goto(prevwin);
385 break;
386
387/* exchange current and next window */
388 case 'x':
389 case Ctrl_X:
390 CHECK_CMDWIN
391 win_exchange(Prenum);
392 break;
393
394/* rotate windows downwards */
395 case Ctrl_R:
396 case 'r':
397 CHECK_CMDWIN
398#ifdef FEAT_VISUAL
399 reset_VIsual_and_resel(); /* stop Visual mode */
400#endif
401 win_rotate(FALSE, (int)Prenum1); /* downwards */
402 break;
403
404/* rotate windows upwards */
405 case 'R':
406 CHECK_CMDWIN
407#ifdef FEAT_VISUAL
408 reset_VIsual_and_resel(); /* stop Visual mode */
409#endif
410 win_rotate(TRUE, (int)Prenum1); /* upwards */
411 break;
412
413/* move window to the very top/bottom/left/right */
414 case 'K':
415 case 'J':
416#ifdef FEAT_VERTSPLIT
417 case 'H':
418 case 'L':
419#endif
420 CHECK_CMDWIN
421 win_totop((int)Prenum,
422 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
423 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
424 break;
425
426/* make all windows the same height */
427 case '=':
428#ifdef FEAT_GUI
429 need_mouse_correct = TRUE;
430#endif
431 win_equal(NULL, FALSE, 'b');
432 break;
433
434/* increase current window height */
435 case '+':
436#ifdef FEAT_GUI
437 need_mouse_correct = TRUE;
438#endif
439 win_setheight(curwin->w_height + (int)Prenum1);
440 break;
441
442/* decrease current window height */
443 case '-':
444#ifdef FEAT_GUI
445 need_mouse_correct = TRUE;
446#endif
447 win_setheight(curwin->w_height - (int)Prenum1);
448 break;
449
450/* set current window height */
451 case Ctrl__:
452 case '_':
453#ifdef FEAT_GUI
454 need_mouse_correct = TRUE;
455#endif
456 win_setheight(Prenum ? (int)Prenum : 9999);
457 break;
458
459#ifdef FEAT_VERTSPLIT
460/* increase current window width */
461 case '>':
462#ifdef FEAT_GUI
463 need_mouse_correct = TRUE;
464#endif
465 win_setwidth(curwin->w_width + (int)Prenum1);
466 break;
467
468/* decrease current window width */
469 case '<':
470#ifdef FEAT_GUI
471 need_mouse_correct = TRUE;
472#endif
473 win_setwidth(curwin->w_width - (int)Prenum1);
474 break;
475
476/* set current window width */
477 case '|':
478#ifdef FEAT_GUI
479 need_mouse_correct = TRUE;
480#endif
481 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
482 break;
483#endif
484
485/* jump to tag and split window if tag exists (in preview window) */
486#if defined(FEAT_QUICKFIX)
487 case '}':
488 CHECK_CMDWIN
489 if (Prenum)
490 g_do_tagpreview = Prenum;
491 else
492 g_do_tagpreview = p_pvh;
493 /*FALLTHROUGH*/
494#endif
495 case ']':
496 case Ctrl_RSB:
497 CHECK_CMDWIN
498#ifdef FEAT_VISUAL
499 reset_VIsual_and_resel(); /* stop Visual mode */
500#endif
501 if (Prenum)
502 postponed_split = Prenum;
503 else
504 postponed_split = -1;
505
506 /* Execute the command right here, required when
507 * "wincmd ]" was used in a function. */
508 do_nv_ident(Ctrl_RSB, NUL);
509 break;
510
511#ifdef FEAT_SEARCHPATH
512/* edit file name under cursor in a new window */
513 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000514 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000516wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000518
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000519 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 if (ptr != NULL)
521 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000522# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000524# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 setpcmark();
526 if (win_split(0, 0) == OK)
527 {
528# ifdef FEAT_SCROLLBIND
529 curwin->w_p_scb = FALSE;
530# endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000531 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
532 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000533 if (nchar == 'F' && lnum >= 0)
534 {
535 curwin->w_cursor.lnum = lnum;
536 check_cursor_lnum();
537 beginline(BL_SOL | BL_FIX);
538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 }
540 vim_free(ptr);
541 }
542 break;
543#endif
544
545#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000546/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 * new window -- webb
548 */
549 case 'i': /* Go to any match */
550 case Ctrl_I:
551 type = FIND_ANY;
552 /* FALLTHROUGH */
553 case 'd': /* Go to definition, using 'define' */
554 case Ctrl_D:
555 CHECK_CMDWIN
556 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
557 break;
558 find_pattern_in_path(ptr, 0, len, TRUE,
559 Prenum == 0 ? TRUE : FALSE, type,
560 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
561 curwin->w_set_curswant = TRUE;
562 break;
563#endif
564
Bram Moolenaar05159a02005-02-26 23:04:13 +0000565 case K_KENTER:
566 case CAR:
567#if defined(FEAT_QUICKFIX)
568 /*
569 * In a quickfix window a <CR> jumps to the error under the
570 * cursor in a new window.
571 */
572 if (bt_quickfix(curbuf))
573 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000574 sprintf((char *)cbuf, "split +%ld%s",
575 (long)curwin->w_cursor.lnum,
576 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000577 do_cmdline_cmd(cbuf);
578 }
579#endif
580 break;
581
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583/* CTRL-W g extended commands */
584 case 'g':
585 case Ctrl_G:
586 CHECK_CMDWIN
587#ifdef USE_ON_FLY_SCROLL
588 dont_scroll = TRUE; /* disallow scrolling here */
589#endif
590 ++no_mapping;
591 ++allow_keys; /* no mapping for xchar, but allow key codes */
592 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000593 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 --no_mapping;
596 --allow_keys;
597#ifdef FEAT_CMDL_INFO
598 (void)add_to_showcmd(xchar);
599#endif
600 switch (xchar)
601 {
602#if defined(FEAT_QUICKFIX)
603 case '}':
604 xchar = Ctrl_RSB;
605 if (Prenum)
606 g_do_tagpreview = Prenum;
607 else
608 g_do_tagpreview = p_pvh;
609 /*FALLTHROUGH*/
610#endif
611 case ']':
612 case Ctrl_RSB:
613#ifdef FEAT_VISUAL
614 reset_VIsual_and_resel(); /* stop Visual mode */
615#endif
616 if (Prenum)
617 postponed_split = Prenum;
618 else
619 postponed_split = -1;
620
621 /* Execute the command right here, required when
622 * "wincmd g}" was used in a function. */
623 do_nv_ident('g', xchar);
624 break;
625
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000626#ifdef FEAT_SEARCHPATH
627 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000628 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100629 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000630 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000631 goto wingotofile;
632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 default:
634 beep_flush();
635 break;
636 }
637 break;
638
639 default: beep_flush();
640 break;
641 }
642}
643
644/*
645 * split the current window, implements CTRL-W s and :split
646 *
647 * "size" is the height or width for the new window, 0 to use half of current
648 * height or width.
649 *
650 * "flags":
651 * WSP_ROOM: require enough room for new window
652 * WSP_VERT: vertical split.
653 * WSP_TOP: open window at the top-left of the shell (help window).
654 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
655 * WSP_HELP: creating the help window, keep layout snapshot
656 *
657 * return FAIL for failure, OK otherwise
658 */
659 int
660win_split(size, flags)
661 int size;
662 int flags;
663{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000664 /* When the ":tab" modifier was used open a new tab page instead. */
665 if (may_open_tabpage() == OK)
666 return OK;
667
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 /* Add flags from ":vertical", ":topleft" and ":botright". */
669 flags |= cmdmod.split;
670 if ((flags & WSP_TOP) && (flags & WSP_BOT))
671 {
672 EMSG(_("E442: Can't split topleft and botright at the same time"));
673 return FAIL;
674 }
675
676 /* When creating the help window make a snapshot of the window layout.
677 * Otherwise clear the snapshot, it's now invalid. */
678 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000679 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000681 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682
683 return win_split_ins(size, flags, NULL, 0);
684}
685
686/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000687 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 * When "newwin" is not NULL: insert this window at the far
689 * top/left/right/bottom.
690 * return FAIL for failure, OK otherwise
691 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000692 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693win_split_ins(size, flags, newwin, dir)
694 int size;
695 int flags;
696 win_T *newwin;
697 int dir;
698{
699 win_T *wp = newwin;
700 win_T *oldwin;
701 int new_size = size;
702 int i;
703 int need_status = 0;
704 int do_equal = FALSE;
705 int needed;
706 int available;
707 int oldwin_height = 0;
708 int layout;
709 frame_T *frp, *curfrp;
710 int before;
711
712 if (flags & WSP_TOP)
713 oldwin = firstwin;
714 else if (flags & WSP_BOT)
715 oldwin = lastwin;
716 else
717 oldwin = curwin;
718
719 /* add a status line when p_ls == 1 and splitting the first window */
720 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
721 {
722 if (oldwin->w_height <= p_wmh && newwin == NULL)
723 {
724 EMSG(_(e_noroom));
725 return FAIL;
726 }
727 need_status = STATUS_HEIGHT;
728 }
729
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000730#ifdef FEAT_GUI
731 /* May be needed for the scrollbars that are going to change. */
732 if (gui.in_use)
733 out_flush();
734#endif
735
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#ifdef FEAT_VERTSPLIT
737 if (flags & WSP_VERT)
738 {
739 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
741 /*
742 * Check if we are able to split the current window and compute its
743 * width.
744 */
745 needed = p_wmw + 1;
746 if (flags & WSP_ROOM)
747 needed += p_wiw - p_wmw;
748 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
749 {
750 available = topframe->fr_width;
751 needed += frame_minwidth(topframe, NULL);
752 }
753 else
754 available = oldwin->w_width;
755 if (available < needed && newwin == NULL)
756 {
757 EMSG(_(e_noroom));
758 return FAIL;
759 }
760 if (new_size == 0)
761 new_size = oldwin->w_width / 2;
762 if (new_size > oldwin->w_width - p_wmw - 1)
763 new_size = oldwin->w_width - p_wmw - 1;
764 if (new_size < p_wmw)
765 new_size = p_wmw;
766
767 /* if it doesn't fit in the current window, need win_equal() */
768 if (oldwin->w_width - new_size - 1 < p_wmw)
769 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000770
771 /* We don't like to take lines for the new window from a
772 * 'winfixwidth' window. Take them from a window to the left or right
773 * instead, if possible. */
774 if (oldwin->w_p_wfw)
775 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000776
777 /* Only make all windows the same width if one of them (except oldwin)
778 * is wider than one of the split windows. */
779 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
780 && oldwin->w_frame->fr_parent != NULL)
781 {
782 frp = oldwin->w_frame->fr_parent->fr_child;
783 while (frp != NULL)
784 {
785 if (frp->fr_win != oldwin && frp->fr_win != NULL
786 && (frp->fr_win->w_width > new_size
787 || frp->fr_win->w_width > oldwin->w_width
788 - new_size - STATUS_HEIGHT))
789 {
790 do_equal = TRUE;
791 break;
792 }
793 frp = frp->fr_next;
794 }
795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 }
797 else
798#endif
799 {
800 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
802 /*
803 * Check if we are able to split the current window and compute its
804 * height.
805 */
806 needed = p_wmh + STATUS_HEIGHT + need_status;
807 if (flags & WSP_ROOM)
808 needed += p_wh - p_wmh;
809 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
810 {
811 available = topframe->fr_height;
812 needed += frame_minheight(topframe, NULL);
813 }
814 else
815 {
816 available = oldwin->w_height;
817 needed += p_wmh;
818 }
819 if (available < needed && newwin == NULL)
820 {
821 EMSG(_(e_noroom));
822 return FAIL;
823 }
824 oldwin_height = oldwin->w_height;
825 if (need_status)
826 {
827 oldwin->w_status_height = STATUS_HEIGHT;
828 oldwin_height -= STATUS_HEIGHT;
829 }
830 if (new_size == 0)
831 new_size = oldwin_height / 2;
832
833 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
834 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
835 if (new_size < p_wmh)
836 new_size = p_wmh;
837
838 /* if it doesn't fit in the current window, need win_equal() */
839 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
840 do_equal = TRUE;
841
842 /* We don't like to take lines for the new window from a
843 * 'winfixheight' window. Take them from a window above or below
844 * instead, if possible. */
845 if (oldwin->w_p_wfh)
846 {
847 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
848 oldwin);
849 oldwin_height = oldwin->w_height;
850 if (need_status)
851 oldwin_height -= STATUS_HEIGHT;
852 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000853
854 /* Only make all windows the same height if one of them (except oldwin)
855 * is higher than one of the split windows. */
856 if (!do_equal && p_ea && size == 0
857#ifdef FEAT_VERTSPLIT
858 && *p_ead != 'h'
859#endif
860 && oldwin->w_frame->fr_parent != NULL)
861 {
862 frp = oldwin->w_frame->fr_parent->fr_child;
863 while (frp != NULL)
864 {
865 if (frp->fr_win != oldwin && frp->fr_win != NULL
866 && (frp->fr_win->w_height > new_size
867 || frp->fr_win->w_height > oldwin_height - new_size
868 - STATUS_HEIGHT))
869 {
870 do_equal = TRUE;
871 break;
872 }
873 frp = frp->fr_next;
874 }
875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 }
877
878 /*
879 * allocate new window structure and link it in the window list
880 */
881 if ((flags & WSP_TOP) == 0
882 && ((flags & WSP_BOT)
883 || (flags & WSP_BELOW)
884 || (!(flags & WSP_ABOVE)
885 && (
886#ifdef FEAT_VERTSPLIT
887 (flags & WSP_VERT) ? p_spr :
888#endif
889 p_sb))))
890 {
891 /* new window below/right of current one */
892 if (newwin == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000893 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 else
895 win_append(oldwin, wp);
896 }
897 else
898 {
899 if (newwin == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000900 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 else
902 win_append(oldwin->w_prev, wp);
903 }
904
905 if (newwin == NULL)
906 {
907 if (wp == NULL)
908 return FAIL;
909
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000910 new_frame(wp);
911 if (wp->w_frame == NULL)
912 {
913 win_free(wp, NULL);
914 return FAIL;
915 }
916
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000917 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000918 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
920
921 /*
922 * Reorganise the tree of frames to insert the new window.
923 */
924 if (flags & (WSP_TOP | WSP_BOT))
925 {
926#ifdef FEAT_VERTSPLIT
927 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
928 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
929#else
930 if (topframe->fr_layout == FR_COL)
931#endif
932 {
933 curfrp = topframe->fr_child;
934 if (flags & WSP_BOT)
935 while (curfrp->fr_next != NULL)
936 curfrp = curfrp->fr_next;
937 }
938 else
939 curfrp = topframe;
940 before = (flags & WSP_TOP);
941 }
942 else
943 {
944 curfrp = oldwin->w_frame;
945 if (flags & WSP_BELOW)
946 before = FALSE;
947 else if (flags & WSP_ABOVE)
948 before = TRUE;
949 else
950#ifdef FEAT_VERTSPLIT
951 if (flags & WSP_VERT)
952 before = !p_spr;
953 else
954#endif
955 before = !p_sb;
956 }
957 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
958 {
959 /* Need to create a new frame in the tree to make a branch. */
960 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
961 *frp = *curfrp;
962 curfrp->fr_layout = layout;
963 frp->fr_parent = curfrp;
964 frp->fr_next = NULL;
965 frp->fr_prev = NULL;
966 curfrp->fr_child = frp;
967 curfrp->fr_win = NULL;
968 curfrp = frp;
969 if (frp->fr_win != NULL)
970 oldwin->w_frame = frp;
971 else
972 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
973 frp->fr_parent = curfrp;
974 }
975
976 if (newwin == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000977 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 else
979 frp = newwin->w_frame;
980 frp->fr_parent = curfrp->fr_parent;
981
982 /* Insert the new frame at the right place in the frame list. */
983 if (before)
984 frame_insert(curfrp, frp);
985 else
986 frame_append(curfrp, frp);
987
988#ifdef FEAT_VERTSPLIT
989 if (flags & WSP_VERT)
990 {
991 wp->w_p_scr = curwin->w_p_scr;
992 if (need_status)
993 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +0100994 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 oldwin->w_status_height = need_status;
996 }
997 if (flags & (WSP_TOP | WSP_BOT))
998 {
999 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001000 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001001 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 wp->w_status_height = (p_ls > 0);
1003 }
1004 else
1005 {
1006 /* height and row of new window is same as current window */
1007 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001008 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 wp->w_status_height = oldwin->w_status_height;
1010 }
1011 frp->fr_height = curfrp->fr_height;
1012
1013 /* "new_size" of the current window goes to the new window, use
1014 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001015 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (before)
1017 wp->w_vsep_width = 1;
1018 else
1019 {
1020 wp->w_vsep_width = oldwin->w_vsep_width;
1021 oldwin->w_vsep_width = 1;
1022 }
1023 if (flags & (WSP_TOP | WSP_BOT))
1024 {
1025 if (flags & WSP_BOT)
1026 frame_add_vsep(curfrp);
1027 /* Set width of neighbor frame */
1028 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001029 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1030 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
1032 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001033 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (before) /* new window left of current one */
1035 {
1036 wp->w_wincol = oldwin->w_wincol;
1037 oldwin->w_wincol += new_size + 1;
1038 }
1039 else /* new window right of current one */
1040 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1041 frame_fix_width(oldwin);
1042 frame_fix_width(wp);
1043 }
1044 else
1045#endif
1046 {
1047 /* width and column of new window is same as current window */
1048#ifdef FEAT_VERTSPLIT
1049 if (flags & (WSP_TOP | WSP_BOT))
1050 {
1051 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001052 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 wp->w_vsep_width = 0;
1054 }
1055 else
1056 {
1057 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001058 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 wp->w_vsep_width = oldwin->w_vsep_width;
1060 }
1061 frp->fr_width = curfrp->fr_width;
1062#endif
1063
1064 /* "new_size" of the current window goes to the new window, use
1065 * one row for the status line */
1066 win_new_height(wp, new_size);
1067 if (flags & (WSP_TOP | WSP_BOT))
1068 frame_new_height(curfrp, curfrp->fr_height
1069 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1070 else
1071 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1072 if (before) /* new window above current one */
1073 {
1074 wp->w_winrow = oldwin->w_winrow;
1075 wp->w_status_height = STATUS_HEIGHT;
1076 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1077 }
1078 else /* new window below current one */
1079 {
1080 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1081 wp->w_status_height = oldwin->w_status_height;
1082 oldwin->w_status_height = STATUS_HEIGHT;
1083 }
1084#ifdef FEAT_VERTSPLIT
1085 if (flags & WSP_BOT)
1086 frame_add_statusline(curfrp);
1087#endif
1088 frame_fix_height(wp);
1089 frame_fix_height(oldwin);
1090 }
1091
1092 if (flags & (WSP_TOP | WSP_BOT))
1093 (void)win_comp_pos();
1094
1095 /*
1096 * Both windows need redrawing
1097 */
1098 redraw_win_later(wp, NOT_VALID);
1099 wp->w_redr_status = TRUE;
1100 redraw_win_later(oldwin, NOT_VALID);
1101 oldwin->w_redr_status = TRUE;
1102
1103 if (need_status)
1104 {
1105 msg_row = Rows - 1;
1106 msg_col = sc_col;
1107 msg_clr_eos_force(); /* Old command/ruler may still be there */
1108 comp_col();
1109 msg_row = Rows - 1;
1110 msg_col = 0; /* put position back at start of line */
1111 }
1112
1113 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001114 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 */
1116 if (do_equal || dir != 0)
1117 win_equal(wp, TRUE,
1118#ifdef FEAT_VERTSPLIT
1119 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1120 : dir == 'h' ? 'b' :
1121#endif
1122 'v');
1123
1124 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1125 * size was given. */
1126#ifdef FEAT_VERTSPLIT
1127 if (flags & WSP_VERT)
1128 {
1129 i = p_wiw;
1130 if (size != 0)
1131 p_wiw = size;
1132
1133# ifdef FEAT_GUI
1134 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1135 if (gui.in_use)
1136 gui_init_which_components(NULL);
1137# endif
1138 }
1139 else
1140#endif
1141 {
1142 i = p_wh;
1143 if (size != 0)
1144 p_wh = size;
1145 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001146
1147 /*
1148 * make the new window the current window
1149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 win_enter(wp, FALSE);
1151#ifdef FEAT_VERTSPLIT
1152 if (flags & WSP_VERT)
1153 p_wiw = i;
1154 else
1155#endif
1156 p_wh = i;
1157
1158 return OK;
1159}
1160
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001161
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001162/*
1163 * Initialize window "newp" from window "oldp".
1164 * Used when splitting a window and when creating a new tab page.
1165 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001166 * WSP_NEWLOC may be specified in flags to prevent the location list from
1167 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001168 */
1169 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001170win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001171 win_T *newp;
1172 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001173 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001174{
1175 int i;
1176
1177 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001178#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001179 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001180#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001181 oldp->w_buffer->b_nwindows++;
1182 newp->w_cursor = oldp->w_cursor;
1183 newp->w_valid = 0;
1184 newp->w_curswant = oldp->w_curswant;
1185 newp->w_set_curswant = oldp->w_set_curswant;
1186 newp->w_topline = oldp->w_topline;
1187#ifdef FEAT_DIFF
1188 newp->w_topfill = oldp->w_topfill;
1189#endif
1190 newp->w_leftcol = oldp->w_leftcol;
1191 newp->w_pcmark = oldp->w_pcmark;
1192 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1193 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001194 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001195 newp->w_fraction = oldp->w_fraction;
1196 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1197#ifdef FEAT_JUMPLIST
1198 copy_jumplist(oldp, newp);
1199#endif
1200#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001201 if (flags & WSP_NEWLOC)
1202 {
1203 /* Don't copy the location list. */
1204 newp->w_llist = NULL;
1205 newp->w_llist_ref = NULL;
1206 }
1207 else
1208 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001209#endif
1210 if (oldp->w_localdir != NULL)
1211 newp->w_localdir = vim_strsave(oldp->w_localdir);
1212
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001213 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001214 for (i = 0; i < oldp->w_tagstacklen; i++)
1215 {
1216 newp->w_tagstack[i] = oldp->w_tagstack[i];
1217 if (newp->w_tagstack[i].tagname != NULL)
1218 newp->w_tagstack[i].tagname =
1219 vim_strsave(newp->w_tagstack[i].tagname);
1220 }
1221 newp->w_tagstackidx = oldp->w_tagstackidx;
1222 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001223# ifdef FEAT_FOLDING
1224 copyFoldingState(oldp, newp);
1225# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001226
1227 win_init_some(newp, oldp);
1228}
1229
1230/*
1231 * Initialize window "newp" from window"old".
1232 * Only the essential things are copied.
1233 */
1234 static void
1235win_init_some(newp, oldp)
1236 win_T *newp;
1237 win_T *oldp;
1238{
1239 /* Use the same argument list. */
1240 newp->w_alist = oldp->w_alist;
1241 ++newp->w_alist->al_refcount;
1242 newp->w_arg_idx = oldp->w_arg_idx;
1243
1244 /* copy options from existing window */
1245 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001246}
1247
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248#endif /* FEAT_WINDOWS */
1249
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250#if defined(FEAT_WINDOWS) || defined(PROTO)
1251/*
1252 * Check if "win" is a pointer to an existing window.
1253 */
1254 int
1255win_valid(win)
1256 win_T *win;
1257{
1258 win_T *wp;
1259
1260 if (win == NULL)
1261 return FALSE;
1262 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1263 if (wp == win)
1264 return TRUE;
1265 return FALSE;
1266}
1267
1268/*
1269 * Return the number of windows.
1270 */
1271 int
1272win_count()
1273{
1274 win_T *wp;
1275 int count = 0;
1276
1277 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1278 ++count;
1279 return count;
1280}
1281
1282/*
1283 * Make "count" windows on the screen.
1284 * Return actual number of windows on the screen.
1285 * Must be called when there is just one window, filling the whole screen
1286 * (excluding the command line).
1287 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 int
1289make_windows(count, vertical)
1290 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001291 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292{
1293 int maxcount;
1294 int todo;
1295
1296#ifdef FEAT_VERTSPLIT
1297 if (vertical)
1298 {
1299 /* Each windows needs at least 'winminwidth' lines and a separator
1300 * column. */
1301 maxcount = (curwin->w_width + curwin->w_vsep_width
1302 - (p_wiw - p_wmw)) / (p_wmw + 1);
1303 }
1304 else
1305#endif
1306 {
1307 /* Each window needs at least 'winminheight' lines and a status line. */
1308 maxcount = (curwin->w_height + curwin->w_status_height
1309 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1310 }
1311
1312 if (maxcount < 2)
1313 maxcount = 2;
1314 if (count > maxcount)
1315 count = maxcount;
1316
1317 /*
1318 * add status line now, otherwise first window will be too big
1319 */
1320 if (count > 1)
1321 last_status(TRUE);
1322
1323#ifdef FEAT_AUTOCMD
1324 /*
1325 * Don't execute autocommands while creating the windows. Must do that
1326 * when putting the buffers in the windows.
1327 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001328 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329#endif
1330
1331 /* todo is number of windows left to create */
1332 for (todo = count - 1; todo > 0; --todo)
1333#ifdef FEAT_VERTSPLIT
1334 if (vertical)
1335 {
1336 if (win_split(curwin->w_width - (curwin->w_width - todo)
1337 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1338 break;
1339 }
1340 else
1341#endif
1342 {
1343 if (win_split(curwin->w_height - (curwin->w_height - todo
1344 * STATUS_HEIGHT) / (todo + 1)
1345 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1346 break;
1347 }
1348
1349#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001350 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351#endif
1352
1353 /* return actual number of windows */
1354 return (count - todo);
1355}
1356
1357/*
1358 * Exchange current and next window
1359 */
1360 static void
1361win_exchange(Prenum)
1362 long Prenum;
1363{
1364 frame_T *frp;
1365 frame_T *frp2;
1366 win_T *wp;
1367 win_T *wp2;
1368 int temp;
1369
1370 if (lastwin == firstwin) /* just one window */
1371 {
1372 beep_flush();
1373 return;
1374 }
1375
1376#ifdef FEAT_GUI
1377 need_mouse_correct = TRUE;
1378#endif
1379
1380 /*
1381 * find window to exchange with
1382 */
1383 if (Prenum)
1384 {
1385 frp = curwin->w_frame->fr_parent->fr_child;
1386 while (frp != NULL && --Prenum > 0)
1387 frp = frp->fr_next;
1388 }
1389 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1390 frp = curwin->w_frame->fr_next;
1391 else /* Swap last window in row/col with previous */
1392 frp = curwin->w_frame->fr_prev;
1393
1394 /* We can only exchange a window with another window, not with a frame
1395 * containing windows. */
1396 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1397 return;
1398 wp = frp->fr_win;
1399
1400/*
1401 * 1. remove curwin from the list. Remember after which window it was in wp2
1402 * 2. insert curwin before wp in the list
1403 * if wp != wp2
1404 * 3. remove wp from the list
1405 * 4. insert wp after wp2
1406 * 5. exchange the status line height and vsep width.
1407 */
1408 wp2 = curwin->w_prev;
1409 frp2 = curwin->w_frame->fr_prev;
1410 if (wp->w_prev != curwin)
1411 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001412 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 frame_remove(curwin->w_frame);
1414 win_append(wp->w_prev, curwin);
1415 frame_insert(frp, curwin->w_frame);
1416 }
1417 if (wp != wp2)
1418 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001419 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 frame_remove(wp->w_frame);
1421 win_append(wp2, wp);
1422 if (frp2 == NULL)
1423 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1424 else
1425 frame_append(frp2, wp->w_frame);
1426 }
1427 temp = curwin->w_status_height;
1428 curwin->w_status_height = wp->w_status_height;
1429 wp->w_status_height = temp;
1430#ifdef FEAT_VERTSPLIT
1431 temp = curwin->w_vsep_width;
1432 curwin->w_vsep_width = wp->w_vsep_width;
1433 wp->w_vsep_width = temp;
1434
1435 /* If the windows are not in the same frame, exchange the sizes to avoid
1436 * messing up the window layout. Otherwise fix the frame sizes. */
1437 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1438 {
1439 temp = curwin->w_height;
1440 curwin->w_height = wp->w_height;
1441 wp->w_height = temp;
1442 temp = curwin->w_width;
1443 curwin->w_width = wp->w_width;
1444 wp->w_width = temp;
1445 }
1446 else
1447 {
1448 frame_fix_height(curwin);
1449 frame_fix_height(wp);
1450 frame_fix_width(curwin);
1451 frame_fix_width(wp);
1452 }
1453#endif
1454
1455 (void)win_comp_pos(); /* recompute window positions */
1456
1457 win_enter(wp, TRUE);
1458 redraw_later(CLEAR);
1459}
1460
1461/*
1462 * rotate windows: if upwards TRUE the second window becomes the first one
1463 * if upwards FALSE the first window becomes the second one
1464 */
1465 static void
1466win_rotate(upwards, count)
1467 int upwards;
1468 int count;
1469{
1470 win_T *wp1;
1471 win_T *wp2;
1472 frame_T *frp;
1473 int n;
1474
1475 if (firstwin == lastwin) /* nothing to do */
1476 {
1477 beep_flush();
1478 return;
1479 }
1480
1481#ifdef FEAT_GUI
1482 need_mouse_correct = TRUE;
1483#endif
1484
1485#ifdef FEAT_VERTSPLIT
1486 /* Check if all frames in this row/col have one window. */
1487 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1488 frp = frp->fr_next)
1489 if (frp->fr_win == NULL)
1490 {
1491 EMSG(_("E443: Cannot rotate when another window is split"));
1492 return;
1493 }
1494#endif
1495
1496 while (count--)
1497 {
1498 if (upwards) /* first window becomes last window */
1499 {
1500 /* remove first window/frame from the list */
1501 frp = curwin->w_frame->fr_parent->fr_child;
1502 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001503 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 frame_remove(frp);
1505
1506 /* find last frame and append removed window/frame after it */
1507 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1508 ;
1509 win_append(frp->fr_win, wp1);
1510 frame_append(frp, wp1->w_frame);
1511
1512 wp2 = frp->fr_win; /* previously last window */
1513 }
1514 else /* last window becomes first window */
1515 {
1516 /* find last window/frame in the list and remove it */
1517 for (frp = curwin->w_frame; frp->fr_next != NULL;
1518 frp = frp->fr_next)
1519 ;
1520 wp1 = frp->fr_win;
1521 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001522 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 frame_remove(frp);
1524
1525 /* append the removed window/frame before the first in the list */
1526 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1527 frame_insert(frp->fr_parent->fr_child, frp);
1528 }
1529
1530 /* exchange status height and vsep width of old and new last window */
1531 n = wp2->w_status_height;
1532 wp2->w_status_height = wp1->w_status_height;
1533 wp1->w_status_height = n;
1534 frame_fix_height(wp1);
1535 frame_fix_height(wp2);
1536#ifdef FEAT_VERTSPLIT
1537 n = wp2->w_vsep_width;
1538 wp2->w_vsep_width = wp1->w_vsep_width;
1539 wp1->w_vsep_width = n;
1540 frame_fix_width(wp1);
1541 frame_fix_width(wp2);
1542#endif
1543
1544 /* recompute w_winrow and w_wincol for all windows */
1545 (void)win_comp_pos();
1546 }
1547
1548 redraw_later(CLEAR);
1549}
1550
1551/*
1552 * Move the current window to the very top/bottom/left/right of the screen.
1553 */
1554 static void
1555win_totop(size, flags)
1556 int size;
1557 int flags;
1558{
1559 int dir;
1560 int height = curwin->w_height;
1561
1562 if (lastwin == firstwin)
1563 {
1564 beep_flush();
1565 return;
1566 }
1567
1568 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001569 (void)winframe_remove(curwin, &dir, NULL);
1570 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 last_status(FALSE); /* may need to remove last status line */
1572 (void)win_comp_pos(); /* recompute window positions */
1573
1574 /* Split a window on the desired side and put the window there. */
1575 (void)win_split_ins(size, flags, curwin, dir);
1576 if (!(flags & WSP_VERT))
1577 {
1578 win_setheight(height);
1579 if (p_ea)
1580 win_equal(curwin, TRUE, 'v');
1581 }
1582
1583#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1584 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1585 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001586 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588}
1589
1590/*
1591 * Move window "win1" to below/right of "win2" and make "win1" the current
1592 * window. Only works within the same frame!
1593 */
1594 void
1595win_move_after(win1, win2)
1596 win_T *win1, *win2;
1597{
1598 int height;
1599
1600 /* check if the arguments are reasonable */
1601 if (win1 == win2)
1602 return;
1603
1604 /* check if there is something to do */
1605 if (win2->w_next != win1)
1606 {
1607 /* may need move the status line/vertical separator of the last window
1608 * */
1609 if (win1 == lastwin)
1610 {
1611 height = win1->w_prev->w_status_height;
1612 win1->w_prev->w_status_height = win1->w_status_height;
1613 win1->w_status_height = height;
1614#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001615 if (win1->w_prev->w_vsep_width == 1)
1616 {
1617 /* Remove the vertical separator from the last-but-one window,
1618 * add it to the last window. Adjust the frame widths. */
1619 win1->w_prev->w_vsep_width = 0;
1620 win1->w_prev->w_frame->fr_width -= 1;
1621 win1->w_vsep_width = 1;
1622 win1->w_frame->fr_width += 1;
1623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624#endif
1625 }
1626 else if (win2 == lastwin)
1627 {
1628 height = win1->w_status_height;
1629 win1->w_status_height = win2->w_status_height;
1630 win2->w_status_height = height;
1631#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001632 if (win1->w_vsep_width == 1)
1633 {
1634 /* Remove the vertical separator from win1, add it to the last
1635 * window, win2. Adjust the frame widths. */
1636 win2->w_vsep_width = 1;
1637 win2->w_frame->fr_width += 1;
1638 win1->w_vsep_width = 0;
1639 win1->w_frame->fr_width -= 1;
1640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641#endif
1642 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001643 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 frame_remove(win1->w_frame);
1645 win_append(win2, win1);
1646 frame_append(win2->w_frame, win1->w_frame);
1647
1648 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1649 redraw_later(NOT_VALID);
1650 }
1651 win_enter(win1, FALSE);
1652}
1653
1654/*
1655 * Make all windows the same height.
1656 * 'next_curwin' will soon be the current window, make sure it has enough
1657 * rows.
1658 */
1659 void
1660win_equal(next_curwin, current, dir)
1661 win_T *next_curwin; /* pointer to current window to be or NULL */
1662 int current; /* do only frame with current window */
1663 int dir; /* 'v' for vertically, 'h' for horizontally,
1664 'b' for both, 0 for using p_ead */
1665{
1666 if (dir == 0)
1667#ifdef FEAT_VERTSPLIT
1668 dir = *p_ead;
1669#else
1670 dir = 'b';
1671#endif
1672 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001673 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001674 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675}
1676
1677/*
1678 * Set a frame to a new position and height, spreading the available room
1679 * equally over contained frames.
1680 * The window "next_curwin" (if not NULL) should at least get the size from
1681 * 'winheight' and 'winwidth' if possible.
1682 */
1683 static void
1684win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1685 win_T *next_curwin; /* pointer to current window to be or NULL */
1686 int current; /* do only frame with current window */
1687 frame_T *topfr; /* frame to set size off */
1688 int dir; /* 'v', 'h' or 'b', see win_equal() */
1689 int col; /* horizontal position for frame */
1690 int row; /* vertical position for frame */
1691 int width; /* new width of frame */
1692 int height; /* new height of frame */
1693{
1694 int n, m;
1695 int extra_sep = 0;
1696 int wincount, totwincount = 0;
1697 frame_T *fr;
1698 int next_curwin_size = 0;
1699 int room = 0;
1700 int new_size;
1701 int has_next_curwin = 0;
1702 int hnc;
1703
1704 if (topfr->fr_layout == FR_LEAF)
1705 {
1706 /* Set the width/height of this frame.
1707 * Redraw when size or position changes */
1708 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1709#ifdef FEAT_VERTSPLIT
1710 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1711#endif
1712 )
1713 {
1714 topfr->fr_win->w_winrow = row;
1715 frame_new_height(topfr, height, FALSE, FALSE);
1716#ifdef FEAT_VERTSPLIT
1717 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001718 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719#endif
1720 redraw_all_later(CLEAR);
1721 }
1722 }
1723#ifdef FEAT_VERTSPLIT
1724 else if (topfr->fr_layout == FR_ROW)
1725 {
1726 topfr->fr_width = width;
1727 topfr->fr_height = height;
1728
1729 if (dir != 'v') /* equalize frame widths */
1730 {
1731 /* Compute the maximum number of windows horizontally in this
1732 * frame. */
1733 n = frame_minwidth(topfr, NOWIN);
1734 /* add one for the rightmost window, it doesn't have a separator */
1735 if (col + width == Columns)
1736 extra_sep = 1;
1737 else
1738 extra_sep = 0;
1739 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001740 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001742 /*
1743 * Compute width for "next_curwin" window and room available for
1744 * other windows.
1745 * "m" is the minimal width when counting p_wiw for "next_curwin".
1746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 m = frame_minwidth(topfr, next_curwin);
1748 room = width - m;
1749 if (room < 0)
1750 {
1751 next_curwin_size = p_wiw + room;
1752 room = 0;
1753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 else
1755 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001756 next_curwin_size = -1;
1757 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1758 {
1759 /* If 'winfixwidth' set keep the window width if
1760 * possible.
1761 * Watch out for this window being the next_curwin. */
1762 if (frame_fixed_width(fr))
1763 {
1764 n = frame_minwidth(fr, NOWIN);
1765 new_size = fr->fr_width;
1766 if (frame_has_win(fr, next_curwin))
1767 {
1768 room += p_wiw - p_wmw;
1769 next_curwin_size = 0;
1770 if (new_size < p_wiw)
1771 new_size = p_wiw;
1772 }
1773 else
1774 /* These windows don't use up room. */
1775 totwincount -= (n + (fr->fr_next == NULL
1776 ? extra_sep : 0)) / (p_wmw + 1);
1777 room -= new_size - n;
1778 if (room < 0)
1779 {
1780 new_size += room;
1781 room = 0;
1782 }
1783 fr->fr_newwidth = new_size;
1784 }
1785 }
1786 if (next_curwin_size == -1)
1787 {
1788 if (!has_next_curwin)
1789 next_curwin_size = 0;
1790 else if (totwincount > 1
1791 && (room + (totwincount - 2))
1792 / (totwincount - 1) > p_wiw)
1793 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001794 /* Can make all windows wider than 'winwidth', spread
1795 * the room equally. */
1796 next_curwin_size = (room + p_wiw
1797 + (totwincount - 1) * p_wmw
1798 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001799 room -= next_curwin_size - p_wiw;
1800 }
1801 else
1802 next_curwin_size = p_wiw;
1803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001805
1806 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 --totwincount; /* don't count curwin */
1808 }
1809
1810 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1811 {
1812 n = m = 0;
1813 wincount = 1;
1814 if (fr->fr_next == NULL)
1815 /* last frame gets all that remains (avoid roundoff error) */
1816 new_size = width;
1817 else if (dir == 'v')
1818 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001819 else if (frame_fixed_width(fr))
1820 {
1821 new_size = fr->fr_newwidth;
1822 wincount = 0; /* doesn't count as a sizeable window */
1823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 else
1825 {
1826 /* Compute the maximum number of windows horiz. in "fr". */
1827 n = frame_minwidth(fr, NOWIN);
1828 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1829 / (p_wmw + 1);
1830 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001831 if (has_next_curwin)
1832 hnc = frame_has_win(fr, next_curwin);
1833 else
1834 hnc = FALSE;
1835 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001837 if (totwincount == 0)
1838 new_size = room;
1839 else
1840 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001842 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 {
1844 next_curwin_size -= p_wiw - (m - n);
1845 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001846 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001848 else
1849 room -= new_size;
1850 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 }
1852
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001853 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 * window, unless equalizing all frames. */
1855 if (!current || dir != 'v' || topfr->fr_parent != NULL
1856 || (new_size != fr->fr_width)
1857 || frame_has_win(fr, next_curwin))
1858 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001859 new_size, height);
1860 col += new_size;
1861 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 totwincount -= wincount;
1863 }
1864 }
1865#endif
1866 else /* topfr->fr_layout == FR_COL */
1867 {
1868#ifdef FEAT_VERTSPLIT
1869 topfr->fr_width = width;
1870#endif
1871 topfr->fr_height = height;
1872
1873 if (dir != 'h') /* equalize frame heights */
1874 {
1875 /* Compute maximum number of windows vertically in this frame. */
1876 n = frame_minheight(topfr, NOWIN);
1877 /* add one for the bottom window if it doesn't have a statusline */
1878 if (row + height == cmdline_row && p_ls == 0)
1879 extra_sep = 1;
1880 else
1881 extra_sep = 0;
1882 totwincount = (n + extra_sep) / (p_wmh + 1);
1883 has_next_curwin = frame_has_win(topfr, next_curwin);
1884
1885 /*
1886 * Compute height for "next_curwin" window and room available for
1887 * other windows.
1888 * "m" is the minimal height when counting p_wh for "next_curwin".
1889 */
1890 m = frame_minheight(topfr, next_curwin);
1891 room = height - m;
1892 if (room < 0)
1893 {
1894 /* The room is less then 'winheight', use all space for the
1895 * current window. */
1896 next_curwin_size = p_wh + room;
1897 room = 0;
1898 }
1899 else
1900 {
1901 next_curwin_size = -1;
1902 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1903 {
1904 /* If 'winfixheight' set keep the window height if
1905 * possible.
1906 * Watch out for this window being the next_curwin. */
1907 if (frame_fixed_height(fr))
1908 {
1909 n = frame_minheight(fr, NOWIN);
1910 new_size = fr->fr_height;
1911 if (frame_has_win(fr, next_curwin))
1912 {
1913 room += p_wh - p_wmh;
1914 next_curwin_size = 0;
1915 if (new_size < p_wh)
1916 new_size = p_wh;
1917 }
1918 else
1919 /* These windows don't use up room. */
1920 totwincount -= (n + (fr->fr_next == NULL
1921 ? extra_sep : 0)) / (p_wmh + 1);
1922 room -= new_size - n;
1923 if (room < 0)
1924 {
1925 new_size += room;
1926 room = 0;
1927 }
1928 fr->fr_newheight = new_size;
1929 }
1930 }
1931 if (next_curwin_size == -1)
1932 {
1933 if (!has_next_curwin)
1934 next_curwin_size = 0;
1935 else if (totwincount > 1
1936 && (room + (totwincount - 2))
1937 / (totwincount - 1) > p_wh)
1938 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001939 /* can make all windows higher than 'winheight',
1940 * spread the room equally. */
1941 next_curwin_size = (room + p_wh
1942 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 + (totwincount - 1)) / totwincount;
1944 room -= next_curwin_size - p_wh;
1945 }
1946 else
1947 next_curwin_size = p_wh;
1948 }
1949 }
1950
1951 if (has_next_curwin)
1952 --totwincount; /* don't count curwin */
1953 }
1954
1955 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1956 {
1957 n = m = 0;
1958 wincount = 1;
1959 if (fr->fr_next == NULL)
1960 /* last frame gets all that remains (avoid roundoff error) */
1961 new_size = height;
1962 else if (dir == 'h')
1963 new_size = fr->fr_height;
1964 else if (frame_fixed_height(fr))
1965 {
1966 new_size = fr->fr_newheight;
1967 wincount = 0; /* doesn't count as a sizeable window */
1968 }
1969 else
1970 {
1971 /* Compute the maximum number of windows vert. in "fr". */
1972 n = frame_minheight(fr, NOWIN);
1973 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1974 / (p_wmh + 1);
1975 m = frame_minheight(fr, next_curwin);
1976 if (has_next_curwin)
1977 hnc = frame_has_win(fr, next_curwin);
1978 else
1979 hnc = FALSE;
1980 if (hnc) /* don't count next_curwin */
1981 --wincount;
1982 if (totwincount == 0)
1983 new_size = room;
1984 else
1985 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1986 / totwincount;
1987 if (hnc) /* add next_curwin size */
1988 {
1989 next_curwin_size -= p_wh - (m - n);
1990 new_size += next_curwin_size;
1991 room -= new_size - next_curwin_size;
1992 }
1993 else
1994 room -= new_size;
1995 new_size += n;
1996 }
1997 /* Skip frame that is full width when splitting or closing a
1998 * window, unless equalizing all frames. */
1999 if (!current || dir != 'h' || topfr->fr_parent != NULL
2000 || (new_size != fr->fr_height)
2001 || frame_has_win(fr, next_curwin))
2002 win_equal_rec(next_curwin, current, fr, dir, col, row,
2003 width, new_size);
2004 row += new_size;
2005 height -= new_size;
2006 totwincount -= wincount;
2007 }
2008 }
2009}
2010
2011/*
2012 * close all windows for buffer 'buf'
2013 */
2014 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002015close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002017 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002019 win_T *wp;
2020 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002021 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022
2023 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002024
2025 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002027 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002029 win_close(wp, FALSE);
2030
2031 /* Start all over, autocommands may change the window layout. */
2032 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 }
2034 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002035 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002037
2038 /* Also check windows in other tab pages. */
2039 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2040 {
2041 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002042 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002043 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2044 if (wp->w_buffer == buf)
2045 {
2046 win_close_othertab(wp, FALSE, tp);
2047
2048 /* Start all over, the tab page may be closed and
2049 * autocommands may change the window layout. */
2050 nexttp = first_tabpage;
2051 break;
2052 }
2053 }
2054
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002056
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002057 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002058 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059}
2060
2061/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002062 * Return TRUE if the current window is the only window that exists (ignoring
2063 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002064 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002065 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002066 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002067last_window()
2068{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002069 return (one_window() && first_tabpage->tp_next == NULL);
2070}
2071
2072/*
2073 * Return TRUE if there is only one window other than "aucmd_win" in the
2074 * current tab page.
2075 */
2076 static int
2077one_window()
2078{
2079#ifdef FEAT_AUTOCMD
2080 win_T *wp;
2081 int seen_one = FALSE;
2082
2083 FOR_ALL_WINDOWS(wp)
2084 {
2085 if (wp != aucmd_win)
2086 {
2087 if (seen_one)
2088 return FALSE;
2089 seen_one = TRUE;
2090 }
2091 }
2092 return TRUE;
2093#else
2094 return firstwin == lastwin;
2095#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002096}
2097
2098/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002099 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 * If "free_buf" is TRUE related buffer may be unloaded.
2101 *
2102 * called by :quit, :close, :xit, :wq and findtag()
2103 */
2104 void
2105win_close(win, free_buf)
2106 win_T *win;
2107 int free_buf;
2108{
2109 win_T *wp;
2110#ifdef FEAT_AUTOCMD
2111 int other_buffer = FALSE;
2112#endif
2113 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 int dir;
2115 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002116 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002118 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
2120 EMSG(_("E444: Cannot close last window"));
2121 return;
2122 }
2123
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002124#ifdef FEAT_AUTOCMD
2125 if (win == aucmd_win)
2126 {
2127 EMSG(_("E813: Cannot close autocmd window"));
2128 return;
2129 }
2130 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2131 {
2132 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2133 return;
2134 }
2135#endif
2136
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002137 /*
2138 * When closing the last window in a tab page first go to another tab
2139 * page and then close the window and the tab page. This avoids that
2140 * curwin and curtab are not invalid while we are freeing memory, they may
2141 * be used in GUI events.
2142 */
2143 if (firstwin == lastwin)
2144 {
2145 goto_tabpage_tp(alt_tabpage());
2146 redraw_tabline = TRUE;
2147
2148 /* Safety check: Autocommands may have closed the window when jumping
2149 * to the other tab page. */
2150 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2151 {
2152 int h = tabline_height();
2153
2154 win_close_othertab(win, free_buf, prev_curtab);
2155 if (h != tabline_height())
2156 shell_new_rows();
2157 }
2158 return;
2159 }
2160
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 /* When closing the help window, try restoring a snapshot after closing
2162 * the window. Otherwise clear the snapshot, it's now invalid. */
2163 if (win->w_buffer->b_help)
2164 help_window = TRUE;
2165 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002166 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
2168#ifdef FEAT_AUTOCMD
2169 if (win == curwin)
2170 {
2171 /*
2172 * Guess which window is going to be the new current window.
2173 * This may change because of the autocommands (sigh).
2174 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002175 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
2177 /*
2178 * Be careful: If autocommands delete the window, return now.
2179 */
2180 if (wp->w_buffer != curbuf)
2181 {
2182 other_buffer = TRUE;
2183 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002184 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 return;
2186 }
2187 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002188 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 return;
2190# ifdef FEAT_EVAL
2191 /* autocmds may abort script processing */
2192 if (aborting())
2193 return;
2194# endif
2195 }
2196#endif
2197
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002198#ifdef FEAT_GUI
2199 /* Avoid trouble with scrollbars that are going to be deleted in
2200 * win_free(). */
2201 if (gui.in_use)
2202 out_flush();
2203#endif
2204
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 /*
2206 * Close the link to the buffer.
2207 */
2208 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002209
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002211 * other window or moved to another tab page. */
2212 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 return;
2214
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002215 /* Free the memory used for the window. */
2216 wp = win_free_mem(win, &dir, NULL);
2217
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 /* Make sure curwin isn't invalid. It can cause severe trouble when
2219 * printing an error message. For win_equal() curbuf needs to be valid
2220 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002221 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
2223 curwin = wp;
2224#ifdef FEAT_QUICKFIX
2225 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2226 {
2227 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002228 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 * finding another window to go to.
2230 */
2231 for (;;)
2232 {
2233 if (wp->w_next == NULL)
2234 wp = firstwin;
2235 else
2236 wp = wp->w_next;
2237 if (wp == curwin)
2238 break;
2239 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2240 {
2241 curwin = wp;
2242 break;
2243 }
2244 }
2245 }
2246#endif
2247 curbuf = curwin->w_buffer;
2248 close_curwin = TRUE;
2249 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002250 if (p_ea
2251#ifdef FEAT_VERTSPLIT
2252 && (*p_ead == 'b' || *p_ead == dir)
2253#endif
2254 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 win_equal(curwin, TRUE,
2256#ifdef FEAT_VERTSPLIT
2257 dir
2258#else
2259 0
2260#endif
2261 );
2262 else
2263 win_comp_pos();
2264 if (close_curwin)
2265 {
2266 win_enter_ext(wp, FALSE, TRUE);
2267#ifdef FEAT_AUTOCMD
2268 if (other_buffer)
2269 /* careful: after this wp and win may be invalid! */
2270 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2271#endif
2272 }
2273
2274 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002275 * If last window has a status line now and we don't want one,
2276 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 */
2278 last_status(FALSE);
2279
2280 /* After closing the help window, try restoring the window layout from
2281 * before it was opened. */
2282 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002283 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284
2285#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2286 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2287 if (gui.in_use && !win_hasvertsplit())
2288 gui_init_which_components(NULL);
2289#endif
2290
2291 redraw_all_later(NOT_VALID);
2292}
2293
2294/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002295 * Close window "win" in tab page "tp", which is not the current tab page.
2296 * This may be the last window ih that tab page and result in closing the tab,
2297 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002298 * Caller must check if buffer is hidden and whether the tabline needs to be
2299 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002300 */
2301 void
2302win_close_othertab(win, free_buf, tp)
2303 win_T *win;
2304 int free_buf;
2305 tabpage_T *tp;
2306{
2307 win_T *wp;
2308 int dir;
2309 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002310 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002311
2312 /* Close the link to the buffer. */
2313 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2314
2315 /* Careful: Autocommands may have closed the tab page or made it the
2316 * current tab page. */
2317 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2318 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002319 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002320 return;
2321
2322 /* Autocommands may have closed the window already. */
2323 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2324 ;
2325 if (wp == NULL)
2326 return;
2327
Bram Moolenaarf740b292006-02-16 22:11:02 +00002328 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002329 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002330 {
2331 if (tp == first_tabpage)
2332 first_tabpage = tp->tp_next;
2333 else
2334 {
2335 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2336 ptp = ptp->tp_next)
2337 ;
2338 if (ptp == NULL)
2339 {
2340 EMSG2(_(e_intern2), "win_close_othertab()");
2341 return;
2342 }
2343 ptp->tp_next = tp->tp_next;
2344 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002345 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002346 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002347
2348 /* Free the memory used for the window. */
2349 win_free_mem(win, &dir, tp);
2350
2351 if (free_tp)
2352 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002353}
2354
2355/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002356 * Free the memory used for a window.
2357 * Returns a pointer to the window that got the freed up space.
2358 */
2359 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002360win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002361 win_T *win;
2362 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002363 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002364{
2365 frame_T *frp;
2366 win_T *wp;
2367
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002368 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002369 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002370 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002371 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002372 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002373
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002374 /* When deleting the current window of another tab page select a new
2375 * current window. */
2376 if (tp != NULL && win == tp->tp_curwin)
2377 tp->tp_curwin = wp;
2378
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002379 return wp;
2380}
2381
2382#if defined(EXITFREE) || defined(PROTO)
2383 void
2384win_free_all()
2385{
2386 int dummy;
2387
Bram Moolenaarf740b292006-02-16 22:11:02 +00002388# ifdef FEAT_WINDOWS
2389 while (first_tabpage->tp_next != NULL)
2390 tabpage_close(TRUE);
2391# endif
2392
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002393# ifdef FEAT_AUTOCMD
2394 if (aucmd_win != NULL)
2395 {
2396 (void)win_free_mem(aucmd_win, &dummy, NULL);
2397 aucmd_win = NULL;
2398 }
2399# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002400
2401 while (firstwin != NULL)
2402 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002403}
2404#endif
2405
2406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 * Remove a window and its frame from the tree of frames.
2408 * Returns a pointer to the window that got the freed up space.
2409 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002410 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002411winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002413 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002414 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
2416 frame_T *frp, *frp2, *frp3;
2417 frame_T *frp_close = win->w_frame;
2418 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419
2420 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002421 * If there is only one window there is nothing to remove.
2422 */
2423 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2424 return NULL;
2425
2426 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 * Remove the window from its frame.
2428 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002429 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 wp = frame2win(frp2);
2431
2432 /* Remove this frame from the list of frames. */
2433 frame_remove(frp_close);
2434
2435#ifdef FEAT_VERTSPLIT
2436 if (frp_close->fr_parent->fr_layout == FR_COL)
2437 {
2438#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002439 /* When 'winfixheight' is set, try to find another frame in the column
2440 * (as close to the closed frame as possible) to distribute the height
2441 * to. */
2442 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2443 {
2444 frp = frp_close->fr_prev;
2445 frp3 = frp_close->fr_next;
2446 while (frp != NULL || frp3 != NULL)
2447 {
2448 if (frp != NULL)
2449 {
2450 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2451 {
2452 frp2 = frp;
2453 wp = frp->fr_win;
2454 break;
2455 }
2456 frp = frp->fr_prev;
2457 }
2458 if (frp3 != NULL)
2459 {
2460 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2461 {
2462 frp2 = frp3;
2463 wp = frp3->fr_win;
2464 break;
2465 }
2466 frp3 = frp3->fr_next;
2467 }
2468 }
2469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2471 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472#ifdef FEAT_VERTSPLIT
2473 *dirp = 'v';
2474 }
2475 else
2476 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002477 /* When 'winfixwidth' is set, try to find another frame in the column
2478 * (as close to the closed frame as possible) to distribute the width
2479 * to. */
2480 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2481 {
2482 frp = frp_close->fr_prev;
2483 frp3 = frp_close->fr_next;
2484 while (frp != NULL || frp3 != NULL)
2485 {
2486 if (frp != NULL)
2487 {
2488 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2489 {
2490 frp2 = frp;
2491 wp = frp->fr_win;
2492 break;
2493 }
2494 frp = frp->fr_prev;
2495 }
2496 if (frp3 != NULL)
2497 {
2498 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2499 {
2500 frp2 = frp3;
2501 wp = frp3->fr_win;
2502 break;
2503 }
2504 frp3 = frp3->fr_next;
2505 }
2506 }
2507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002509 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 *dirp = 'h';
2511 }
2512#endif
2513
2514 /* If rows/columns go to a window below/right its positions need to be
2515 * updated. Can only be done after the sizes have been updated. */
2516 if (frp2 == frp_close->fr_next)
2517 {
2518 int row = win->w_winrow;
2519 int col = W_WINCOL(win);
2520
2521 frame_comp_pos(frp2, &row, &col);
2522 }
2523
2524 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2525 {
2526 /* There is no other frame in this list, move its info to the parent
2527 * and remove it. */
2528 frp2->fr_parent->fr_layout = frp2->fr_layout;
2529 frp2->fr_parent->fr_child = frp2->fr_child;
2530 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2531 frp->fr_parent = frp2->fr_parent;
2532 frp2->fr_parent->fr_win = frp2->fr_win;
2533 if (frp2->fr_win != NULL)
2534 frp2->fr_win->w_frame = frp2->fr_parent;
2535 frp = frp2->fr_parent;
2536 vim_free(frp2);
2537
2538 frp2 = frp->fr_parent;
2539 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2540 {
2541 /* The frame above the parent has the same layout, have to merge
2542 * the frames into this list. */
2543 if (frp2->fr_child == frp)
2544 frp2->fr_child = frp->fr_child;
2545 frp->fr_child->fr_prev = frp->fr_prev;
2546 if (frp->fr_prev != NULL)
2547 frp->fr_prev->fr_next = frp->fr_child;
2548 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2549 {
2550 frp3->fr_parent = frp2;
2551 if (frp3->fr_next == NULL)
2552 {
2553 frp3->fr_next = frp->fr_next;
2554 if (frp->fr_next != NULL)
2555 frp->fr_next->fr_prev = frp3;
2556 break;
2557 }
2558 }
2559 vim_free(frp);
2560 }
2561 }
2562
2563 return wp;
2564}
2565
2566/*
2567 * Find out which frame is going to get the freed up space when "win" is
2568 * closed.
2569 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2570 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2571 * This makes opening a window and closing it immediately keep the same window
2572 * layout.
2573 */
2574 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002575win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002577 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578{
2579 frame_T *frp;
2580 int b;
2581
Bram Moolenaarf740b292006-02-16 22:11:02 +00002582 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002583 /* Last window in this tab page, will go to next tab page. */
2584 return alt_tabpage()->tp_curwin->w_frame;
2585
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 frp = win->w_frame;
2587#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002588 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 b = p_spr;
2590 else
2591#endif
2592 b = p_sb;
2593 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2594 return frp->fr_next;
2595 return frp->fr_prev;
2596}
2597
2598/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002599 * Return the tabpage that will be used if the current one is closed.
2600 */
2601 static tabpage_T *
2602alt_tabpage()
2603{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002604 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002605
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002606 /* Use the next tab page if possible. */
2607 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002608 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002609
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002610 /* Find the last but one tab page. */
2611 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2612 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002613 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002614}
2615
2616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 * Find the left-upper window in frame "frp".
2618 */
2619 static win_T *
2620frame2win(frp)
2621 frame_T *frp;
2622{
2623 while (frp->fr_win == NULL)
2624 frp = frp->fr_child;
2625 return frp->fr_win;
2626}
2627
2628/*
2629 * Return TRUE if frame "frp" contains window "wp".
2630 */
2631 static int
2632frame_has_win(frp, wp)
2633 frame_T *frp;
2634 win_T *wp;
2635{
2636 frame_T *p;
2637
2638 if (frp->fr_layout == FR_LEAF)
2639 return frp->fr_win == wp;
2640
2641 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2642 if (frame_has_win(p, wp))
2643 return TRUE;
2644 return FALSE;
2645}
2646
2647/*
2648 * Set a new height for a frame. Recursively sets the height for contained
2649 * frames and windows. Caller must take care of positions.
2650 */
2651 static void
2652frame_new_height(topfrp, height, topfirst, wfh)
2653 frame_T *topfrp;
2654 int height;
2655 int topfirst; /* resize topmost contained frame first */
2656 int wfh; /* obey 'winfixheight' when there is a choice;
2657 may cause the height not to be set */
2658{
2659 frame_T *frp;
2660 int extra_lines;
2661 int h;
2662
2663 if (topfrp->fr_win != NULL)
2664 {
2665 /* Simple case: just one window. */
2666 win_new_height(topfrp->fr_win,
2667 height - topfrp->fr_win->w_status_height);
2668 }
2669#ifdef FEAT_VERTSPLIT
2670 else if (topfrp->fr_layout == FR_ROW)
2671 {
2672 do
2673 {
2674 /* All frames in this row get the same new height. */
2675 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2676 {
2677 frame_new_height(frp, height, topfirst, wfh);
2678 if (frp->fr_height > height)
2679 {
2680 /* Could not fit the windows, make the whole row higher. */
2681 height = frp->fr_height;
2682 break;
2683 }
2684 }
2685 }
2686 while (frp != NULL);
2687 }
2688#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002689 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {
2691 /* Complicated case: Resize a column of frames. Resize the bottom
2692 * frame first, frames above that when needed. */
2693
2694 frp = topfrp->fr_child;
2695 if (wfh)
2696 /* Advance past frames with one window with 'wfh' set. */
2697 while (frame_fixed_height(frp))
2698 {
2699 frp = frp->fr_next;
2700 if (frp == NULL)
2701 return; /* no frame without 'wfh', give up */
2702 }
2703 if (!topfirst)
2704 {
2705 /* Find the bottom frame of this column */
2706 while (frp->fr_next != NULL)
2707 frp = frp->fr_next;
2708 if (wfh)
2709 /* Advance back for frames with one window with 'wfh' set. */
2710 while (frame_fixed_height(frp))
2711 frp = frp->fr_prev;
2712 }
2713
2714 extra_lines = height - topfrp->fr_height;
2715 if (extra_lines < 0)
2716 {
2717 /* reduce height of contained frames, bottom or top frame first */
2718 while (frp != NULL)
2719 {
2720 h = frame_minheight(frp, NULL);
2721 if (frp->fr_height + extra_lines < h)
2722 {
2723 extra_lines += frp->fr_height - h;
2724 frame_new_height(frp, h, topfirst, wfh);
2725 }
2726 else
2727 {
2728 frame_new_height(frp, frp->fr_height + extra_lines,
2729 topfirst, wfh);
2730 break;
2731 }
2732 if (topfirst)
2733 {
2734 do
2735 frp = frp->fr_next;
2736 while (wfh && frp != NULL && frame_fixed_height(frp));
2737 }
2738 else
2739 {
2740 do
2741 frp = frp->fr_prev;
2742 while (wfh && frp != NULL && frame_fixed_height(frp));
2743 }
2744 /* Increase "height" if we could not reduce enough frames. */
2745 if (frp == NULL)
2746 height -= extra_lines;
2747 }
2748 }
2749 else if (extra_lines > 0)
2750 {
2751 /* increase height of bottom or top frame */
2752 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2753 }
2754 }
2755 topfrp->fr_height = height;
2756}
2757
2758/*
2759 * Return TRUE if height of frame "frp" should not be changed because of
2760 * the 'winfixheight' option.
2761 */
2762 static int
2763frame_fixed_height(frp)
2764 frame_T *frp;
2765{
2766 /* frame with one window: fixed height if 'winfixheight' set. */
2767 if (frp->fr_win != NULL)
2768 return frp->fr_win->w_p_wfh;
2769
2770 if (frp->fr_layout == FR_ROW)
2771 {
2772 /* The frame is fixed height if one of the frames in the row is fixed
2773 * height. */
2774 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2775 if (frame_fixed_height(frp))
2776 return TRUE;
2777 return FALSE;
2778 }
2779
2780 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2781 * frames in the row are fixed height. */
2782 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2783 if (!frame_fixed_height(frp))
2784 return FALSE;
2785 return TRUE;
2786}
2787
2788#ifdef FEAT_VERTSPLIT
2789/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002790 * Return TRUE if width of frame "frp" should not be changed because of
2791 * the 'winfixwidth' option.
2792 */
2793 static int
2794frame_fixed_width(frp)
2795 frame_T *frp;
2796{
2797 /* frame with one window: fixed width if 'winfixwidth' set. */
2798 if (frp->fr_win != NULL)
2799 return frp->fr_win->w_p_wfw;
2800
2801 if (frp->fr_layout == FR_COL)
2802 {
2803 /* The frame is fixed width if one of the frames in the row is fixed
2804 * width. */
2805 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2806 if (frame_fixed_width(frp))
2807 return TRUE;
2808 return FALSE;
2809 }
2810
2811 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2812 * frames in the row are fixed width. */
2813 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2814 if (!frame_fixed_width(frp))
2815 return FALSE;
2816 return TRUE;
2817}
2818
2819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 * Add a status line to windows at the bottom of "frp".
2821 * Note: Does not check if there is room!
2822 */
2823 static void
2824frame_add_statusline(frp)
2825 frame_T *frp;
2826{
2827 win_T *wp;
2828
2829 if (frp->fr_layout == FR_LEAF)
2830 {
2831 wp = frp->fr_win;
2832 if (wp->w_status_height == 0)
2833 {
2834 if (wp->w_height > 0) /* don't make it negative */
2835 --wp->w_height;
2836 wp->w_status_height = STATUS_HEIGHT;
2837 }
2838 }
2839 else if (frp->fr_layout == FR_ROW)
2840 {
2841 /* Handle all the frames in the row. */
2842 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2843 frame_add_statusline(frp);
2844 }
2845 else /* frp->fr_layout == FR_COL */
2846 {
2847 /* Only need to handle the last frame in the column. */
2848 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2849 ;
2850 frame_add_statusline(frp);
2851 }
2852}
2853
2854/*
2855 * Set width of a frame. Handles recursively going through contained frames.
2856 * May remove separator line for windows at the right side (for win_close()).
2857 */
2858 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002859frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 frame_T *topfrp;
2861 int width;
2862 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002863 int wfw; /* obey 'winfixwidth' when there is a choice;
2864 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865{
2866 frame_T *frp;
2867 int extra_cols;
2868 int w;
2869 win_T *wp;
2870
2871 if (topfrp->fr_layout == FR_LEAF)
2872 {
2873 /* Simple case: just one window. */
2874 wp = topfrp->fr_win;
2875 /* Find out if there are any windows right of this one. */
2876 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2877 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2878 break;
2879 if (frp->fr_parent == NULL)
2880 wp->w_vsep_width = 0;
2881 win_new_width(wp, width - wp->w_vsep_width);
2882 }
2883 else if (topfrp->fr_layout == FR_COL)
2884 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002885 do
2886 {
2887 /* All frames in this column get the same new width. */
2888 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2889 {
2890 frame_new_width(frp, width, leftfirst, wfw);
2891 if (frp->fr_width > width)
2892 {
2893 /* Could not fit the windows, make whole column wider. */
2894 width = frp->fr_width;
2895 break;
2896 }
2897 }
2898 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
2900 else /* fr_layout == FR_ROW */
2901 {
2902 /* Complicated case: Resize a row of frames. Resize the rightmost
2903 * frame first, frames left of it when needed. */
2904
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002906 if (wfw)
2907 /* Advance past frames with one window with 'wfw' set. */
2908 while (frame_fixed_width(frp))
2909 {
2910 frp = frp->fr_next;
2911 if (frp == NULL)
2912 return; /* no frame without 'wfw', give up */
2913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002915 {
2916 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 while (frp->fr_next != NULL)
2918 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002919 if (wfw)
2920 /* Advance back for frames with one window with 'wfw' set. */
2921 while (frame_fixed_width(frp))
2922 frp = frp->fr_prev;
2923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
2925 extra_cols = width - topfrp->fr_width;
2926 if (extra_cols < 0)
2927 {
2928 /* reduce frame width, rightmost frame first */
2929 while (frp != NULL)
2930 {
2931 w = frame_minwidth(frp, NULL);
2932 if (frp->fr_width + extra_cols < w)
2933 {
2934 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002935 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 }
2937 else
2938 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002939 frame_new_width(frp, frp->fr_width + extra_cols,
2940 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 break;
2942 }
2943 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002944 {
2945 do
2946 frp = frp->fr_next;
2947 while (wfw && frp != NULL && frame_fixed_width(frp));
2948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002950 {
2951 do
2952 frp = frp->fr_prev;
2953 while (wfw && frp != NULL && frame_fixed_width(frp));
2954 }
2955 /* Increase "width" if we could not reduce enough frames. */
2956 if (frp == NULL)
2957 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
2959 }
2960 else if (extra_cols > 0)
2961 {
2962 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002963 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 }
2965 }
2966 topfrp->fr_width = width;
2967}
2968
2969/*
2970 * Add the vertical separator to windows at the right side of "frp".
2971 * Note: Does not check if there is room!
2972 */
2973 static void
2974frame_add_vsep(frp)
2975 frame_T *frp;
2976{
2977 win_T *wp;
2978
2979 if (frp->fr_layout == FR_LEAF)
2980 {
2981 wp = frp->fr_win;
2982 if (wp->w_vsep_width == 0)
2983 {
2984 if (wp->w_width > 0) /* don't make it negative */
2985 --wp->w_width;
2986 wp->w_vsep_width = 1;
2987 }
2988 }
2989 else if (frp->fr_layout == FR_COL)
2990 {
2991 /* Handle all the frames in the column. */
2992 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2993 frame_add_vsep(frp);
2994 }
2995 else /* frp->fr_layout == FR_ROW */
2996 {
2997 /* Only need to handle the last frame in the row. */
2998 frp = frp->fr_child;
2999 while (frp->fr_next != NULL)
3000 frp = frp->fr_next;
3001 frame_add_vsep(frp);
3002 }
3003}
3004
3005/*
3006 * Set frame width from the window it contains.
3007 */
3008 static void
3009frame_fix_width(wp)
3010 win_T *wp;
3011{
3012 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3013}
3014#endif
3015
3016/*
3017 * Set frame height from the window it contains.
3018 */
3019 static void
3020frame_fix_height(wp)
3021 win_T *wp;
3022{
3023 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3024}
3025
3026/*
3027 * Compute the minimal height for frame "topfrp".
3028 * Uses the 'winminheight' option.
3029 * When "next_curwin" isn't NULL, use p_wh for this window.
3030 * When "next_curwin" is NOWIN, don't use at least one line for the current
3031 * window.
3032 */
3033 static int
3034frame_minheight(topfrp, next_curwin)
3035 frame_T *topfrp;
3036 win_T *next_curwin;
3037{
3038 frame_T *frp;
3039 int m;
3040#ifdef FEAT_VERTSPLIT
3041 int n;
3042#endif
3043
3044 if (topfrp->fr_win != NULL)
3045 {
3046 if (topfrp->fr_win == next_curwin)
3047 m = p_wh + topfrp->fr_win->w_status_height;
3048 else
3049 {
3050 /* window: minimal height of the window plus status line */
3051 m = p_wmh + topfrp->fr_win->w_status_height;
3052 /* Current window is minimal one line high */
3053 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3054 ++m;
3055 }
3056 }
3057#ifdef FEAT_VERTSPLIT
3058 else if (topfrp->fr_layout == FR_ROW)
3059 {
3060 /* get the minimal height from each frame in this row */
3061 m = 0;
3062 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3063 {
3064 n = frame_minheight(frp, next_curwin);
3065 if (n > m)
3066 m = n;
3067 }
3068 }
3069#endif
3070 else
3071 {
3072 /* Add up the minimal heights for all frames in this column. */
3073 m = 0;
3074 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3075 m += frame_minheight(frp, next_curwin);
3076 }
3077
3078 return m;
3079}
3080
3081#ifdef FEAT_VERTSPLIT
3082/*
3083 * Compute the minimal width for frame "topfrp".
3084 * When "next_curwin" isn't NULL, use p_wiw for this window.
3085 * When "next_curwin" is NOWIN, don't use at least one column for the current
3086 * window.
3087 */
3088 static int
3089frame_minwidth(topfrp, next_curwin)
3090 frame_T *topfrp;
3091 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3092{
3093 frame_T *frp;
3094 int m, n;
3095
3096 if (topfrp->fr_win != NULL)
3097 {
3098 if (topfrp->fr_win == next_curwin)
3099 m = p_wiw + topfrp->fr_win->w_vsep_width;
3100 else
3101 {
3102 /* window: minimal width of the window plus separator column */
3103 m = p_wmw + topfrp->fr_win->w_vsep_width;
3104 /* Current window is minimal one column wide */
3105 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3106 ++m;
3107 }
3108 }
3109 else if (topfrp->fr_layout == FR_COL)
3110 {
3111 /* get the minimal width from each frame in this column */
3112 m = 0;
3113 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3114 {
3115 n = frame_minwidth(frp, next_curwin);
3116 if (n > m)
3117 m = n;
3118 }
3119 }
3120 else
3121 {
3122 /* Add up the minimal widths for all frames in this row. */
3123 m = 0;
3124 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3125 m += frame_minwidth(frp, next_curwin);
3126 }
3127
3128 return m;
3129}
3130#endif
3131
3132
3133/*
3134 * Try to close all windows except current one.
3135 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3136 * used and the buffer was modified.
3137 *
3138 * Used by ":bdel" and ":only".
3139 */
3140 void
3141close_others(message, forceit)
3142 int message;
3143 int forceit; /* always hide all other windows */
3144{
3145 win_T *wp;
3146 win_T *nextwp;
3147 int r;
3148
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003149 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 {
3151 if (message
3152#ifdef FEAT_AUTOCMD
3153 && !autocmd_busy
3154#endif
3155 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003156 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 return;
3158 }
3159
3160 /* Be very careful here: autocommands may change the window layout. */
3161 for (wp = firstwin; win_valid(wp); wp = nextwp)
3162 {
3163 nextwp = wp->w_next;
3164 if (wp != curwin) /* don't close current window */
3165 {
3166
3167 /* Check if it's allowed to abandon this window */
3168 r = can_abandon(wp->w_buffer, forceit);
3169#ifdef FEAT_AUTOCMD
3170 if (!win_valid(wp)) /* autocommands messed wp up */
3171 {
3172 nextwp = firstwin;
3173 continue;
3174 }
3175#endif
3176 if (!r)
3177 {
3178#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3179 if (message && (p_confirm || cmdmod.confirm) && p_write)
3180 {
3181 dialog_changed(wp->w_buffer, FALSE);
3182# ifdef FEAT_AUTOCMD
3183 if (!win_valid(wp)) /* autocommands messed wp up */
3184 {
3185 nextwp = firstwin;
3186 continue;
3187 }
3188# endif
3189 }
3190 if (bufIsChanged(wp->w_buffer))
3191#endif
3192 continue;
3193 }
3194 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3195 }
3196 }
3197
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003198 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 EMSG(_("E445: Other window contains changes"));
3200}
3201
3202#endif /* FEAT_WINDOWS */
3203
3204/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003205 * Init the current window "curwin".
3206 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 */
3208 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003209curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003211 win_init_empty(curwin);
3212}
3213
3214 void
3215win_init_empty(wp)
3216 win_T *wp;
3217{
3218 redraw_win_later(wp, NOT_VALID);
3219 wp->w_lines_valid = 0;
3220 wp->w_cursor.lnum = 1;
3221 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003223 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003225 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3226 wp->w_pcmark.col = 0;
3227 wp->w_prev_pcmark.lnum = 0;
3228 wp->w_prev_pcmark.col = 0;
3229 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003231 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003233 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003235 if (wp->w_p_rl)
3236 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003238 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239#endif
3240}
3241
3242/*
3243 * Allocate the first window and put an empty buffer in it.
3244 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003245 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003247 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248win_alloc_first()
3249{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003250 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003251 return FAIL;
3252
3253#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003254 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003255 if (first_tabpage == NULL)
3256 return FAIL;
3257 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003258 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003259#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003260
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003261 return OK;
3262}
3263
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003264#if defined(FEAT_AUTOCMD) || defined(PROTO)
3265/*
3266 * Init "aucmd_win". This can only be done after the first
3267 * window is fully initialized, thus it can't be in win_alloc_first().
3268 */
3269 void
3270win_alloc_aucmd_win()
3271{
3272 aucmd_win = win_alloc(NULL, TRUE);
3273 if (aucmd_win != NULL)
3274 {
3275 win_init_some(aucmd_win, curwin);
3276# ifdef FEAT_SCROLLBIND
3277 aucmd_win->w_p_scb = FALSE;
3278# endif
3279 new_frame(aucmd_win);
3280 }
3281}
3282#endif
3283
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003284/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003285 * Allocate the first window or the first window in a new tab page.
3286 * When "oldwin" is NULL create an empty buffer for it.
3287 * When "oldwin" is not NULL copy info from it to the new window (only with
3288 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003289 * Return FAIL when something goes wrong (out of memory).
3290 */
3291 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003292win_alloc_firstwin(oldwin)
3293 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003294{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003295 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003296 if (oldwin == NULL)
3297 {
3298 /* Very first window, need to create an empty buffer for it and
3299 * initialize from scratch. */
3300 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3301 if (curwin == NULL || curbuf == NULL)
3302 return FAIL;
3303 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003304#ifdef FEAT_SYN_HL
3305 curwin->w_s = &(curbuf->b_s);
3306#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003307 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003309 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003311 curwin_init(); /* init current window */
3312 }
3313#ifdef FEAT_WINDOWS
3314 else
3315 {
3316 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003317 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003318
3319# ifdef FEAT_SCROLLBIND
3320 /* We don't want scroll-binding in the first window. */
3321 curwin->w_p_scb = FALSE;
3322# endif
3323 }
3324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003326 new_frame(curwin);
3327 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003328 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003329 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330#ifdef FEAT_VERTSPLIT
3331 topframe->fr_width = Columns;
3332#endif
3333 topframe->fr_height = Rows - p_ch;
3334 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003335
3336 return OK;
3337}
3338
3339/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003340 * Create a frame for window "wp".
3341 */
3342 static void
3343new_frame(win_T *wp)
3344{
3345 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3346
3347 wp->w_frame = frp;
3348 if (frp != NULL)
3349 {
3350 frp->fr_layout = FR_LEAF;
3351 frp->fr_win = wp;
3352 }
3353}
3354
3355/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003356 * Initialize the window and frame size to the maximum.
3357 */
3358 void
3359win_init_size()
3360{
3361 firstwin->w_height = ROWS_AVAIL;
3362 topframe->fr_height = ROWS_AVAIL;
3363#ifdef FEAT_VERTSPLIT
3364 firstwin->w_width = Columns;
3365 topframe->fr_width = Columns;
3366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367}
3368
3369#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003370
3371/*
3372 * Allocate a new tabpage_T and init the values.
3373 * Returns NULL when out of memory.
3374 */
3375 static tabpage_T *
3376alloc_tabpage()
3377{
3378 tabpage_T *tp;
3379
3380 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3381 if (tp != NULL)
3382 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003383# ifdef FEAT_GUI
3384 int i;
3385
3386 for (i = 0; i < 3; i++)
3387 tp->tp_prev_which_scrollbars[i] = -1;
3388# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003389# ifdef FEAT_DIFF
3390 tp->tp_diff_invalid = TRUE;
3391# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003392#ifdef FEAT_EVAL
3393 /* init t: variables */
3394 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3395#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003396 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003397 }
3398 return tp;
3399}
3400
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003401 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003402free_tabpage(tp)
3403 tabpage_T *tp;
3404{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003405 int idx;
3406
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003407# ifdef FEAT_DIFF
3408 diff_clear(tp);
3409# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003410 for (idx = 0; idx < SNAP_COUNT; ++idx)
3411 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003412#ifdef FEAT_EVAL
3413 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3414#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003415 vim_free(tp);
3416}
3417
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003418/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003419 * Create a new Tab page with one window.
3420 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003421 * When "after" is 0 put it just after the current Tab page.
3422 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003423 * Return FAIL or OK.
3424 */
3425 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003426win_new_tabpage(after)
3427 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003428{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003429 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003430 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003431 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003432
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003433 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003434 if (newtp == NULL)
3435 return FAIL;
3436
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003437 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003438 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003439 {
3440 vim_free(newtp);
3441 return FAIL;
3442 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003443 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003444
3445 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003446 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003447 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003448 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003449 if (after == 1)
3450 {
3451 /* New tab page becomes the first one. */
3452 newtp->tp_next = first_tabpage;
3453 first_tabpage = newtp;
3454 }
3455 else
3456 {
3457 if (after > 0)
3458 {
3459 /* Put new tab page before tab page "after". */
3460 n = 2;
3461 for (tp = first_tabpage; tp->tp_next != NULL
3462 && n < after; tp = tp->tp_next)
3463 ++n;
3464 }
3465 newtp->tp_next = tp->tp_next;
3466 tp->tp_next = newtp;
3467 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003468 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003469 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003470 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003471
3472 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003473 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003474
3475#if defined(FEAT_GUI)
3476 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3477 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003478 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003479#endif
3480
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003481 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003482#ifdef FEAT_AUTOCMD
3483 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3484 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3485#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003486 return OK;
3487 }
3488
3489 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003490 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003491 return FAIL;
3492}
3493
3494/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003495 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3496 * like with ":split".
3497 * Returns OK if a new tab page was created, FAIL otherwise.
3498 */
3499 int
3500may_open_tabpage()
3501{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003502 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003503
Bram Moolenaard326ce82007-03-11 14:48:29 +00003504 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003505 {
3506 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003507 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003508 return win_new_tabpage(n);
3509 }
3510 return FAIL;
3511}
3512
3513/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003514 * Create up to "maxcount" tabpages with empty windows.
3515 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003516 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003517 int
3518make_tabpages(maxcount)
3519 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003520{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003521 int count = maxcount;
3522 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003523
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003524 /* Limit to 'tabpagemax' tabs. */
3525 if (count > p_tpm)
3526 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003527
3528#ifdef FEAT_AUTOCMD
3529 /*
3530 * Don't execute autocommands while creating the tab pages. Must do that
3531 * when putting the buffers in the windows.
3532 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003533 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003534#endif
3535
3536 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003537 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003538 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003539
3540#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003541 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003542#endif
3543
3544 /* return actual number of tab pages */
3545 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003546}
3547
3548/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003549 * Return TRUE when "tpc" points to a valid tab page.
3550 */
3551 int
3552valid_tabpage(tpc)
3553 tabpage_T *tpc;
3554{
3555 tabpage_T *tp;
3556
3557 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3558 if (tp == tpc)
3559 return TRUE;
3560 return FALSE;
3561}
3562
3563/*
3564 * Find tab page "n" (first one is 1). Returns NULL when not found.
3565 */
3566 tabpage_T *
3567find_tabpage(n)
3568 int n;
3569{
3570 tabpage_T *tp;
3571 int i = 1;
3572
3573 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3574 ++i;
3575 return tp;
3576}
3577
3578/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003579 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003580 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003581 */
3582 int
3583tabpage_index(ftp)
3584 tabpage_T *ftp;
3585{
3586 int i = 1;
3587 tabpage_T *tp;
3588
3589 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3590 ++i;
3591 return i;
3592}
3593
3594/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003595 * Prepare for leaving the current tab page.
3596 * When autocomands change "curtab" we don't leave the tab page and return
3597 * FAIL.
3598 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003599 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003600 static int
3601leave_tabpage(new_curbuf)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003602 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003603 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003604{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003605 tabpage_T *tp = curtab;
3606
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003607#ifdef FEAT_VISUAL
3608 reset_VIsual_and_resel(); /* stop Visual mode */
3609#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003610#ifdef FEAT_AUTOCMD
3611 if (new_curbuf != curbuf)
3612 {
3613 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3614 if (curtab != tp)
3615 return FAIL;
3616 }
3617 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3618 if (curtab != tp)
3619 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003620 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003621 if (curtab != tp)
3622 return FAIL;
3623#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003624#if defined(FEAT_GUI)
3625 /* Remove the scrollbars. They may be added back later. */
3626 if (gui.in_use)
3627 gui_remove_scrollbars();
3628#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003629 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003630 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003631 tp->tp_firstwin = firstwin;
3632 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003633 tp->tp_old_Rows = Rows;
3634 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003635 firstwin = NULL;
3636 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003637 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003638}
3639
3640/*
3641 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003642 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003643 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003644 static void
3645enter_tabpage(tp, old_curbuf)
3646 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003647 buf_T *old_curbuf UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003648{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003649 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003650 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003651
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003652 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003653 firstwin = tp->tp_firstwin;
3654 lastwin = tp->tp_lastwin;
3655 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003656
3657 /* We would like doing the TabEnter event first, but we don't have a
3658 * valid current window yet, which may break some commands.
3659 * This triggers autocommands, thus may make "tp" invalid. */
3660 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3661 prevwin = next_prevwin;
3662
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003663#ifdef FEAT_AUTOCMD
3664 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003665
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003666 if (old_curbuf != curbuf)
3667 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3668#endif
3669
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003670 last_status(FALSE); /* status line may appear or disappear */
3671 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003672 must_redraw = CLEAR; /* need to redraw everything */
3673#ifdef FEAT_DIFF
3674 diff_need_scrollbind = TRUE;
3675#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003676
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003677 /* The tabpage line may have appeared or disappeared, may need to resize
3678 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003679 * frame sizes too. Use the stored value of p_ch, so that it can be
3680 * different for each tab page. */
3681 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003682 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3683#ifdef FEAT_GUI_TABLINE
3684 && !gui_use_tabline()
3685#endif
3686 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003687 shell_new_rows();
3688#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003689 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003690 shell_new_columns(); /* update window widths */
3691#endif
3692
3693#if defined(FEAT_GUI)
3694 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3695 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003696 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003697#endif
3698
3699 redraw_all_later(CLEAR);
3700}
3701
3702/*
3703 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003704 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003705 */
3706 void
3707goto_tabpage(n)
3708 int n;
3709{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003710 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003711 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003712 int i;
3713
Bram Moolenaard68071d2006-05-02 22:08:30 +00003714 if (text_locked())
3715 {
3716 /* Not allowed when editing the command line. */
3717#ifdef FEAT_CMDWIN
3718 if (cmdwin_type != 0)
3719 EMSG(_(e_cmdwin));
3720 else
3721#endif
3722 EMSG(_(e_secure));
3723 return;
3724 }
3725
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003726 /* If there is only one it can't work. */
3727 if (first_tabpage->tp_next == NULL)
3728 {
3729 if (n > 1)
3730 beep_flush();
3731 return;
3732 }
3733
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003734 if (n == 0)
3735 {
3736 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003737 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003738 tp = first_tabpage;
3739 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003740 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003741 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003742 else if (n < 0)
3743 {
3744 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3745 * this N times. */
3746 ttp = curtab;
3747 for (i = n; i < 0; ++i)
3748 {
3749 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3750 tp = tp->tp_next)
3751 ;
3752 ttp = tp;
3753 }
3754 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003755 else if (n == 9999)
3756 {
3757 /* Go to last tab page. */
3758 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3759 ;
3760 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003761 else
3762 {
3763 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003764 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003765 if (tp == NULL)
3766 {
3767 beep_flush();
3768 return;
3769 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003770 }
3771
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003772 goto_tabpage_tp(tp);
3773
3774#ifdef FEAT_GUI_TABLINE
3775 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003776 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003777#endif
3778}
3779
3780/*
3781 * Go to tabpage "tp".
3782 * Note: doesn't update the GUI tab.
3783 */
3784 void
3785goto_tabpage_tp(tp)
3786 tabpage_T *tp;
3787{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003788 /* Don't repeat a message in another tab page. */
3789 set_keep_msg(NULL, 0);
3790
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003791 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003792 {
3793 if (valid_tabpage(tp))
3794 enter_tabpage(tp, curbuf);
3795 else
3796 enter_tabpage(curtab, curbuf);
3797 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003798}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799
3800/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003801 * Enter window "wp" in tab page "tp".
3802 * Also updates the GUI tab.
3803 */
3804 void
3805goto_tabpage_win(tp, wp)
3806 tabpage_T *tp;
3807 win_T *wp;
3808{
3809 goto_tabpage_tp(tp);
3810 if (curtab == tp && win_valid(wp))
3811 {
3812 win_enter(wp, TRUE);
3813# ifdef FEAT_GUI_TABLINE
3814 if (gui_use_tabline())
3815 gui_mch_set_curtab(tabpage_index(curtab));
3816# endif
3817 }
3818}
3819
3820/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003821 * Move the current tab page to before tab page "nr".
3822 */
3823 void
3824tabpage_move(nr)
3825 int nr;
3826{
3827 int n = nr;
3828 tabpage_T *tp;
3829
3830 if (first_tabpage->tp_next == NULL)
3831 return;
3832
3833 /* Remove the current tab page from the list of tab pages. */
3834 if (curtab == first_tabpage)
3835 first_tabpage = curtab->tp_next;
3836 else
3837 {
3838 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3839 if (tp->tp_next == curtab)
3840 break;
3841 if (tp == NULL) /* "cannot happen" */
3842 return;
3843 tp->tp_next = curtab->tp_next;
3844 }
3845
3846 /* Re-insert it at the specified position. */
3847 if (n == 0)
3848 {
3849 curtab->tp_next = first_tabpage;
3850 first_tabpage = curtab;
3851 }
3852 else
3853 {
3854 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3855 --n;
3856 curtab->tp_next = tp->tp_next;
3857 tp->tp_next = curtab;
3858 }
3859
3860 /* Need to redraw the tabline. Tab page contents doesn't change. */
3861 redraw_tabline = TRUE;
3862}
3863
3864
3865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 * Go to another window.
3867 * When jumping to another buffer, stop Visual mode. Do this before
3868 * changing windows so we can yank the selection into the '*' register.
3869 * When jumping to another window on the same buffer, adjust its cursor
3870 * position to keep the same Visual area.
3871 */
3872 void
3873win_goto(wp)
3874 win_T *wp;
3875{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003876 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
3878 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003879 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 return;
3881 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003882#ifdef FEAT_AUTOCMD
3883 if (curbuf_locked())
3884 return;
3885#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003886
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887#ifdef FEAT_VISUAL
3888 if (wp->w_buffer != curbuf)
3889 reset_VIsual_and_resel();
3890 else if (VIsual_active)
3891 wp->w_cursor = curwin->w_cursor;
3892#endif
3893
3894#ifdef FEAT_GUI
3895 need_mouse_correct = TRUE;
3896#endif
3897 win_enter(wp, TRUE);
3898}
3899
3900#if defined(FEAT_PERL) || defined(PROTO)
3901/*
3902 * Find window number "winnr" (counting top to bottom).
3903 */
3904 win_T *
3905win_find_nr(winnr)
3906 int winnr;
3907{
3908 win_T *wp;
3909
3910# ifdef FEAT_WINDOWS
3911 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3912 if (--winnr == 0)
3913 break;
3914 return wp;
3915# else
3916 return curwin;
3917# endif
3918}
3919#endif
3920
3921#ifdef FEAT_VERTSPLIT
3922/*
3923 * Move to window above or below "count" times.
3924 */
3925 static void
3926win_goto_ver(up, count)
3927 int up; /* TRUE to go to win above */
3928 long count;
3929{
3930 frame_T *fr;
3931 frame_T *nfr;
3932 frame_T *foundfr;
3933
3934 foundfr = curwin->w_frame;
3935 while (count--)
3936 {
3937 /*
3938 * First go upwards in the tree of frames until we find a upwards or
3939 * downwards neighbor.
3940 */
3941 fr = foundfr;
3942 for (;;)
3943 {
3944 if (fr == topframe)
3945 goto end;
3946 if (up)
3947 nfr = fr->fr_prev;
3948 else
3949 nfr = fr->fr_next;
3950 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3951 break;
3952 fr = fr->fr_parent;
3953 }
3954
3955 /*
3956 * Now go downwards to find the bottom or top frame in it.
3957 */
3958 for (;;)
3959 {
3960 if (nfr->fr_layout == FR_LEAF)
3961 {
3962 foundfr = nfr;
3963 break;
3964 }
3965 fr = nfr->fr_child;
3966 if (nfr->fr_layout == FR_ROW)
3967 {
3968 /* Find the frame at the cursor row. */
3969 while (fr->fr_next != NULL
3970 && frame2win(fr)->w_wincol + fr->fr_width
3971 <= curwin->w_wincol + curwin->w_wcol)
3972 fr = fr->fr_next;
3973 }
3974 if (nfr->fr_layout == FR_COL && up)
3975 while (fr->fr_next != NULL)
3976 fr = fr->fr_next;
3977 nfr = fr;
3978 }
3979 }
3980end:
3981 if (foundfr != NULL)
3982 win_goto(foundfr->fr_win);
3983}
3984
3985/*
3986 * Move to left or right window.
3987 */
3988 static void
3989win_goto_hor(left, count)
3990 int left; /* TRUE to go to left win */
3991 long count;
3992{
3993 frame_T *fr;
3994 frame_T *nfr;
3995 frame_T *foundfr;
3996
3997 foundfr = curwin->w_frame;
3998 while (count--)
3999 {
4000 /*
4001 * First go upwards in the tree of frames until we find a left or
4002 * right neighbor.
4003 */
4004 fr = foundfr;
4005 for (;;)
4006 {
4007 if (fr == topframe)
4008 goto end;
4009 if (left)
4010 nfr = fr->fr_prev;
4011 else
4012 nfr = fr->fr_next;
4013 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4014 break;
4015 fr = fr->fr_parent;
4016 }
4017
4018 /*
4019 * Now go downwards to find the leftmost or rightmost frame in it.
4020 */
4021 for (;;)
4022 {
4023 if (nfr->fr_layout == FR_LEAF)
4024 {
4025 foundfr = nfr;
4026 break;
4027 }
4028 fr = nfr->fr_child;
4029 if (nfr->fr_layout == FR_COL)
4030 {
4031 /* Find the frame at the cursor row. */
4032 while (fr->fr_next != NULL
4033 && frame2win(fr)->w_winrow + fr->fr_height
4034 <= curwin->w_winrow + curwin->w_wrow)
4035 fr = fr->fr_next;
4036 }
4037 if (nfr->fr_layout == FR_ROW && left)
4038 while (fr->fr_next != NULL)
4039 fr = fr->fr_next;
4040 nfr = fr;
4041 }
4042 }
4043end:
4044 if (foundfr != NULL)
4045 win_goto(foundfr->fr_win);
4046}
4047#endif
4048
4049/*
4050 * Make window "wp" the current window.
4051 */
4052 void
4053win_enter(wp, undo_sync)
4054 win_T *wp;
4055 int undo_sync;
4056{
4057 win_enter_ext(wp, undo_sync, FALSE);
4058}
4059
4060/*
4061 * Make window wp the current window.
4062 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4063 * been closed and isn't valid.
4064 */
4065 static void
4066win_enter_ext(wp, undo_sync, curwin_invalid)
4067 win_T *wp;
4068 int undo_sync;
4069 int curwin_invalid;
4070{
4071#ifdef FEAT_AUTOCMD
4072 int other_buffer = FALSE;
4073#endif
4074
4075 if (wp == curwin && !curwin_invalid) /* nothing to do */
4076 return;
4077
4078#ifdef FEAT_AUTOCMD
4079 if (!curwin_invalid)
4080 {
4081 /*
4082 * Be careful: If autocommands delete the window, return now.
4083 */
4084 if (wp->w_buffer != curbuf)
4085 {
4086 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4087 other_buffer = TRUE;
4088 if (!win_valid(wp))
4089 return;
4090 }
4091 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4092 if (!win_valid(wp))
4093 return;
4094# ifdef FEAT_EVAL
4095 /* autocmds may abort script processing */
4096 if (aborting())
4097 return;
4098# endif
4099 }
4100#endif
4101
4102 /* sync undo before leaving the current buffer */
4103 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004104 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 /* may have to copy the buffer options when 'cpo' contains 'S' */
4106 if (wp->w_buffer != curbuf)
4107 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4108 if (!curwin_invalid)
4109 {
4110 prevwin = curwin; /* remember for CTRL-W p */
4111 curwin->w_redr_status = TRUE;
4112 }
4113 curwin = wp;
4114 curbuf = wp->w_buffer;
4115 check_cursor();
4116#ifdef FEAT_VIRTUALEDIT
4117 if (!virtual_active())
4118 curwin->w_cursor.coladd = 0;
4119#endif
4120 changed_line_abv_curs(); /* assume cursor position needs updating */
4121
4122 if (curwin->w_localdir != NULL)
4123 {
4124 /* Window has a local directory: Save current directory as global
4125 * directory (unless that was done already) and change to the local
4126 * directory. */
4127 if (globaldir == NULL)
4128 {
4129 char_u cwd[MAXPATHL];
4130
4131 if (mch_dirname(cwd, MAXPATHL) == OK)
4132 globaldir = vim_strsave(cwd);
4133 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004134 if (mch_chdir((char *)curwin->w_localdir) == 0)
4135 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
4137 else if (globaldir != NULL)
4138 {
4139 /* Window doesn't have a local directory and we are not in the global
4140 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004141 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 vim_free(globaldir);
4143 globaldir = NULL;
4144 shorten_fnames(TRUE);
4145 }
4146
4147#ifdef FEAT_AUTOCMD
4148 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4149 if (other_buffer)
4150 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4151#endif
4152
4153#ifdef FEAT_TITLE
4154 maketitle();
4155#endif
4156 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004157 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 if (restart_edit)
4159 redraw_later(VALID); /* causes status line redraw */
4160
4161 /* set window height to desired minimal value */
4162 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4163 win_setheight((int)p_wh);
4164 else if (curwin->w_height == 0)
4165 win_setheight(1);
4166
4167#ifdef FEAT_VERTSPLIT
4168 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004169 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 win_setwidth((int)p_wiw);
4171#endif
4172
4173#ifdef FEAT_MOUSE
4174 setmouse(); /* in case jumped to/from help buffer */
4175#endif
4176
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004177 /* Change directories when the 'acd' option is set. */
4178 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179}
4180
4181#endif /* FEAT_WINDOWS */
4182
4183#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4184/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004185 * Jump to the first open window that contains buffer "buf", if one exists.
4186 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 */
4188 win_T *
4189buf_jump_open_win(buf)
4190 buf_T *buf;
4191{
4192# ifdef FEAT_WINDOWS
4193 win_T *wp;
4194
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004195 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 if (wp->w_buffer == buf)
4197 break;
4198 if (wp != NULL)
4199 win_enter(wp, FALSE);
4200 return wp;
4201# else
4202 if (curwin->w_buffer == buf)
4203 return curwin;
4204 return NULL;
4205# endif
4206}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004207
4208/*
4209 * Jump to the first open window in any tab page that contains buffer "buf",
4210 * if one exists.
4211 * Returns a pointer to the window found, otherwise NULL.
4212 */
4213 win_T *
4214buf_jump_open_tab(buf)
4215 buf_T *buf;
4216{
4217# ifdef FEAT_WINDOWS
4218 win_T *wp;
4219 tabpage_T *tp;
4220
4221 /* First try the current tab page. */
4222 wp = buf_jump_open_win(buf);
4223 if (wp != NULL)
4224 return wp;
4225
4226 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4227 if (tp != curtab)
4228 {
4229 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4230 if (wp->w_buffer == buf)
4231 break;
4232 if (wp != NULL)
4233 {
4234 goto_tabpage_win(tp, wp);
4235 if (curwin != wp)
4236 wp = NULL; /* something went wrong */
4237 break;
4238 }
4239 }
4240
4241 return wp;
4242# else
4243 if (curwin->w_buffer == buf)
4244 return curwin;
4245 return NULL;
4246# endif
4247}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248#endif
4249
4250/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004251 * Allocate a window structure and link it in the window list when "hidden" is
4252 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004255win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004256 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004257 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258{
4259 win_T *newwin;
4260
4261 /*
4262 * allocate window structure and linesizes arrays
4263 */
4264 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4265 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4266 {
4267 vim_free(newwin);
4268 newwin = NULL;
4269 }
4270
4271 if (newwin != NULL)
4272 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004273#ifdef FEAT_AUTOCMD
4274 /* Don't execute autocommands while the window is not properly
4275 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4276 * event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004277 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 /*
4280 * link the window in the window list
4281 */
4282#ifdef FEAT_WINDOWS
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004283 if (!hidden)
4284 win_append(after, newwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285#endif
4286#ifdef FEAT_VERTSPLIT
4287 newwin->w_wincol = 0;
4288 newwin->w_width = Columns;
4289#endif
4290
4291 /* position the display and the cursor at the top of the file. */
4292 newwin->w_topline = 1;
4293#ifdef FEAT_DIFF
4294 newwin->w_topfill = 0;
4295#endif
4296 newwin->w_botline = 2;
4297 newwin->w_cursor.lnum = 1;
4298#ifdef FEAT_SCROLLBIND
4299 newwin->w_scbind_pos = 1;
4300#endif
4301
4302 /* We won't calculate w_fraction until resizing the window */
4303 newwin->w_fraction = 0;
4304 newwin->w_prev_fraction_row = -1;
4305
4306#ifdef FEAT_GUI
4307 if (gui.in_use)
4308 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4310 SBAR_LEFT, newwin);
4311 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4312 SBAR_RIGHT, newwin);
4313 }
4314#endif
4315#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004316 /* init w: variables */
4317 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318#endif
4319#ifdef FEAT_FOLDING
4320 foldInitWin(newwin);
4321#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004322#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004323 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004324#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004325#ifdef FEAT_SEARCH_EXTRA
4326 newwin->w_match_head = NULL;
4327 newwin->w_next_match_id = 4;
4328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 }
4330 return newwin;
4331}
4332
4333#if defined(FEAT_WINDOWS) || defined(PROTO)
4334
4335/*
4336 * remove window 'wp' from the window list and free the structure
4337 */
4338 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004339win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004341 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342{
4343 int i;
4344
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004345#ifdef FEAT_FOLDING
4346 clearFolding(wp);
4347#endif
4348
4349 /* reduce the reference count to the argument list. */
4350 alist_unlink(wp->w_alist);
4351
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004352#ifdef FEAT_AUTOCMD
4353 /* Don't execute autocommands while the window is halfway being deleted.
4354 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004355 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004356#endif
4357
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004358#ifdef FEAT_MZSCHEME
4359 mzscheme_window_free(wp);
4360#endif
4361
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362#ifdef FEAT_PERL
4363 perl_win_free(wp);
4364#endif
4365
4366#ifdef FEAT_PYTHON
4367 python_window_free(wp);
4368#endif
4369
4370#ifdef FEAT_TCL
4371 tcl_window_free(wp);
4372#endif
4373
4374#ifdef FEAT_RUBY
4375 ruby_window_free(wp);
4376#endif
4377
4378 clear_winopt(&wp->w_onebuf_opt);
4379 clear_winopt(&wp->w_allbuf_opt);
4380
4381#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004382 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383#endif
4384
4385 if (prevwin == wp)
4386 prevwin = NULL;
4387 win_free_lsize(wp);
4388
4389 for (i = 0; i < wp->w_tagstacklen; ++i)
4390 vim_free(wp->w_tagstack[i].tagname);
4391
4392 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004393
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004395 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004397
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398#ifdef FEAT_JUMPLIST
4399 free_jumplist(wp);
4400#endif
4401
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004402#ifdef FEAT_QUICKFIX
4403 qf_free_all(wp);
4404#endif
4405
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406#ifdef FEAT_GUI
4407 if (gui.in_use)
4408 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4410 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4411 }
4412#endif /* FEAT_GUI */
4413
Bram Moolenaar860cae12010-06-05 23:22:07 +02004414#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004415 reset_synblock(wp); /* free independent synblock */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004416#endif
4417
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004418#ifdef FEAT_AUTOCMD
4419 if (wp != aucmd_win)
4420#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004421 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004423
4424#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004425 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427}
4428
4429/*
4430 * Append window "wp" in the window list after window "after".
4431 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004432 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433win_append(after, wp)
4434 win_T *after, *wp;
4435{
4436 win_T *before;
4437
4438 if (after == NULL) /* after NULL is in front of the first */
4439 before = firstwin;
4440 else
4441 before = after->w_next;
4442
4443 wp->w_next = before;
4444 wp->w_prev = after;
4445 if (after == NULL)
4446 firstwin = wp;
4447 else
4448 after->w_next = wp;
4449 if (before == NULL)
4450 lastwin = wp;
4451 else
4452 before->w_prev = wp;
4453}
4454
4455/*
4456 * Remove a window from the window list.
4457 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004458 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004459win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004461 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462{
4463 if (wp->w_prev != NULL)
4464 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004465 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004467 else
4468 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 if (wp->w_next != NULL)
4470 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004471 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004473 else
4474 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475}
4476
4477/*
4478 * Append frame "frp" in a frame list after frame "after".
4479 */
4480 static void
4481frame_append(after, frp)
4482 frame_T *after, *frp;
4483{
4484 frp->fr_next = after->fr_next;
4485 after->fr_next = frp;
4486 if (frp->fr_next != NULL)
4487 frp->fr_next->fr_prev = frp;
4488 frp->fr_prev = after;
4489}
4490
4491/*
4492 * Insert frame "frp" in a frame list before frame "before".
4493 */
4494 static void
4495frame_insert(before, frp)
4496 frame_T *before, *frp;
4497{
4498 frp->fr_next = before;
4499 frp->fr_prev = before->fr_prev;
4500 before->fr_prev = frp;
4501 if (frp->fr_prev != NULL)
4502 frp->fr_prev->fr_next = frp;
4503 else
4504 frp->fr_parent->fr_child = frp;
4505}
4506
4507/*
4508 * Remove a frame from a frame list.
4509 */
4510 static void
4511frame_remove(frp)
4512 frame_T *frp;
4513{
4514 if (frp->fr_prev != NULL)
4515 frp->fr_prev->fr_next = frp->fr_next;
4516 else
4517 frp->fr_parent->fr_child = frp->fr_next;
4518 if (frp->fr_next != NULL)
4519 frp->fr_next->fr_prev = frp->fr_prev;
4520}
4521
4522#endif /* FEAT_WINDOWS */
4523
4524/*
4525 * Allocate w_lines[] for window "wp".
4526 * Return FAIL for failure, OK for success.
4527 */
4528 int
4529win_alloc_lines(wp)
4530 win_T *wp;
4531{
4532 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004533 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 if (wp->w_lines == NULL)
4535 return FAIL;
4536 return OK;
4537}
4538
4539/*
4540 * free lsize arrays for a window
4541 */
4542 void
4543win_free_lsize(wp)
4544 win_T *wp;
4545{
4546 vim_free(wp->w_lines);
4547 wp->w_lines = NULL;
4548}
4549
4550/*
4551 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004552 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 */
4554 void
4555shell_new_rows()
4556{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004557 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
4559 if (firstwin == NULL) /* not initialized yet */
4560 return;
4561#ifdef FEAT_WINDOWS
4562 if (h < frame_minheight(topframe, NULL))
4563 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004564
4565 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 * that doesn't result in the right height, forget about that option. */
4567 frame_new_height(topframe, h, FALSE, TRUE);
4568 if (topframe->fr_height != h)
4569 frame_new_height(topframe, h, FALSE, FALSE);
4570
4571 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4572#else
4573 if (h < 1)
4574 h = 1;
4575 win_new_height(firstwin, h);
4576#endif
4577 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004578#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004579 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004580#endif
4581
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582#if 0
4583 /* Disabled: don't want making the screen smaller make a window larger. */
4584 if (p_ea)
4585 win_equal(curwin, FALSE, 'v');
4586#endif
4587}
4588
4589#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4590/*
4591 * Called from win_new_shellsize() after Columns changed.
4592 */
4593 void
4594shell_new_columns()
4595{
4596 if (firstwin == NULL) /* not initialized yet */
4597 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004598
4599 /* First try setting the widths of windows with 'winfixwidth'. If that
4600 * doesn't result in the right width, forget about that option. */
4601 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4602 if (topframe->fr_width != Columns)
4603 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4604
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4606#if 0
4607 /* Disabled: don't want making the screen smaller make a window larger. */
4608 if (p_ea)
4609 win_equal(curwin, FALSE, 'h');
4610#endif
4611}
4612#endif
4613
4614#if defined(FEAT_CMDWIN) || defined(PROTO)
4615/*
4616 * Save the size of all windows in "gap".
4617 */
4618 void
4619win_size_save(gap)
4620 garray_T *gap;
4621
4622{
4623 win_T *wp;
4624
4625 ga_init2(gap, (int)sizeof(int), 1);
4626 if (ga_grow(gap, win_count() * 2) == OK)
4627 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4628 {
4629 ((int *)gap->ga_data)[gap->ga_len++] =
4630 wp->w_width + wp->w_vsep_width;
4631 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4632 }
4633}
4634
4635/*
4636 * Restore window sizes, but only if the number of windows is still the same.
4637 * Does not free the growarray.
4638 */
4639 void
4640win_size_restore(gap)
4641 garray_T *gap;
4642{
4643 win_T *wp;
4644 int i;
4645
4646 if (win_count() * 2 == gap->ga_len)
4647 {
4648 i = 0;
4649 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4650 {
4651 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4652 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4653 }
4654 /* recompute the window positions */
4655 (void)win_comp_pos();
4656 }
4657}
4658#endif /* FEAT_CMDWIN */
4659
4660#if defined(FEAT_WINDOWS) || defined(PROTO)
4661/*
4662 * Update the position for all windows, using the width and height of the
4663 * frames.
4664 * Returns the row just after the last window.
4665 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004666 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667win_comp_pos()
4668{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004669 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 int col = 0;
4671
4672 frame_comp_pos(topframe, &row, &col);
4673 return row;
4674}
4675
4676/*
4677 * Update the position of the windows in frame "topfrp", using the width and
4678 * height of the frames.
4679 * "*row" and "*col" are the top-left position of the frame. They are updated
4680 * to the bottom-right position plus one.
4681 */
4682 static void
4683frame_comp_pos(topfrp, row, col)
4684 frame_T *topfrp;
4685 int *row;
4686 int *col;
4687{
4688 win_T *wp;
4689 frame_T *frp;
4690#ifdef FEAT_VERTSPLIT
4691 int startcol;
4692 int startrow;
4693#endif
4694
4695 wp = topfrp->fr_win;
4696 if (wp != NULL)
4697 {
4698 if (wp->w_winrow != *row
4699#ifdef FEAT_VERTSPLIT
4700 || wp->w_wincol != *col
4701#endif
4702 )
4703 {
4704 /* position changed, redraw */
4705 wp->w_winrow = *row;
4706#ifdef FEAT_VERTSPLIT
4707 wp->w_wincol = *col;
4708#endif
4709 redraw_win_later(wp, NOT_VALID);
4710 wp->w_redr_status = TRUE;
4711 }
4712 *row += wp->w_height + wp->w_status_height;
4713#ifdef FEAT_VERTSPLIT
4714 *col += wp->w_width + wp->w_vsep_width;
4715#endif
4716 }
4717 else
4718 {
4719#ifdef FEAT_VERTSPLIT
4720 startrow = *row;
4721 startcol = *col;
4722#endif
4723 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4724 {
4725#ifdef FEAT_VERTSPLIT
4726 if (topfrp->fr_layout == FR_ROW)
4727 *row = startrow; /* all frames are at the same row */
4728 else
4729 *col = startcol; /* all frames are at the same col */
4730#endif
4731 frame_comp_pos(frp, row, col);
4732 }
4733 }
4734}
4735
4736#endif /* FEAT_WINDOWS */
4737
4738/*
4739 * Set current window height and take care of repositioning other windows to
4740 * fit around it.
4741 */
4742 void
4743win_setheight(height)
4744 int height;
4745{
4746 win_setheight_win(height, curwin);
4747}
4748
4749/*
4750 * Set the window height of window "win" and take care of repositioning other
4751 * windows to fit around it.
4752 */
4753 void
4754win_setheight_win(height, win)
4755 int height;
4756 win_T *win;
4757{
4758 int row;
4759
4760 if (win == curwin)
4761 {
4762 /* Always keep current window at least one line high, even when
4763 * 'winminheight' is zero. */
4764#ifdef FEAT_WINDOWS
4765 if (height < p_wmh)
4766 height = p_wmh;
4767#endif
4768 if (height == 0)
4769 height = 1;
4770 }
4771
4772#ifdef FEAT_WINDOWS
4773 frame_setheight(win->w_frame, height + win->w_status_height);
4774
4775 /* recompute the window positions */
4776 row = win_comp_pos();
4777#else
4778 if (height > topframe->fr_height)
4779 height = topframe->fr_height;
4780 win->w_height = height;
4781 row = height;
4782#endif
4783
4784 /*
4785 * If there is extra space created between the last window and the command
4786 * line, clear it.
4787 */
4788 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4789 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4790 cmdline_row = row;
4791 msg_row = row;
4792 msg_col = 0;
4793
4794 redraw_all_later(NOT_VALID);
4795}
4796
4797#if defined(FEAT_WINDOWS) || defined(PROTO)
4798
4799/*
4800 * Set the height of a frame to "height" and take care that all frames and
4801 * windows inside it are resized. Also resize frames on the left and right if
4802 * the are in the same FR_ROW frame.
4803 *
4804 * Strategy:
4805 * If the frame is part of a FR_COL frame, try fitting the frame in that
4806 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4807 * go to containing frames to resize them and make room.
4808 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4809 * Check for the minimal height of the FR_ROW frame.
4810 * At the top level we can also use change the command line height.
4811 */
4812 static void
4813frame_setheight(curfrp, height)
4814 frame_T *curfrp;
4815 int height;
4816{
4817 int room; /* total number of lines available */
4818 int take; /* number of lines taken from other windows */
4819 int room_cmdline; /* lines available from cmdline */
4820 int run;
4821 frame_T *frp;
4822 int h;
4823 int room_reserved;
4824
4825 /* If the height already is the desired value, nothing to do. */
4826 if (curfrp->fr_height == height)
4827 return;
4828
4829 if (curfrp->fr_parent == NULL)
4830 {
4831 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004832 if (height > ROWS_AVAIL)
4833 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 if (height > 0)
4835 frame_new_height(curfrp, height, FALSE, FALSE);
4836 }
4837 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4838 {
4839 /* Row of frames: Also need to resize frames left and right of this
4840 * one. First check for the minimal height of these. */
4841 h = frame_minheight(curfrp->fr_parent, NULL);
4842 if (height < h)
4843 height = h;
4844 frame_setheight(curfrp->fr_parent, height);
4845 }
4846 else
4847 {
4848 /*
4849 * Column of frames: try to change only frames in this column.
4850 */
4851#ifdef FEAT_VERTSPLIT
4852 /*
4853 * Do this twice:
4854 * 1: compute room available, if it's not enough try resizing the
4855 * containing frame.
4856 * 2: compute the room available and adjust the height to it.
4857 * Try not to reduce the height of a window with 'winfixheight' set.
4858 */
4859 for (run = 1; run <= 2; ++run)
4860#else
4861 for (;;)
4862#endif
4863 {
4864 room = 0;
4865 room_reserved = 0;
4866 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4867 frp = frp->fr_next)
4868 {
4869 if (frp != curfrp
4870 && frp->fr_win != NULL
4871 && frp->fr_win->w_p_wfh)
4872 room_reserved += frp->fr_height;
4873 room += frp->fr_height;
4874 if (frp != curfrp)
4875 room -= frame_minheight(frp, NULL);
4876 }
4877#ifdef FEAT_VERTSPLIT
4878 if (curfrp->fr_width != Columns)
4879 room_cmdline = 0;
4880 else
4881#endif
4882 {
4883 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4884 + lastwin->w_height + lastwin->w_status_height);
4885 if (room_cmdline < 0)
4886 room_cmdline = 0;
4887 }
4888
4889 if (height <= room + room_cmdline)
4890 break;
4891#ifdef FEAT_VERTSPLIT
4892 if (run == 2 || curfrp->fr_width == Columns)
4893#endif
4894 {
4895 if (height > room + room_cmdline)
4896 height = room + room_cmdline;
4897 break;
4898 }
4899#ifdef FEAT_VERTSPLIT
4900 frame_setheight(curfrp->fr_parent, height
4901 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4902#endif
4903 /*NOTREACHED*/
4904 }
4905
4906 /*
4907 * Compute the number of lines we will take from others frames (can be
4908 * negative!).
4909 */
4910 take = height - curfrp->fr_height;
4911
4912 /* If there is not enough room, also reduce the height of a window
4913 * with 'winfixheight' set. */
4914 if (height > room + room_cmdline - room_reserved)
4915 room_reserved = room + room_cmdline - height;
4916 /* If there is only a 'winfixheight' window and making the
4917 * window smaller, need to make the other window taller. */
4918 if (take < 0 && room - curfrp->fr_height < room_reserved)
4919 room_reserved = 0;
4920
4921 if (take > 0 && room_cmdline > 0)
4922 {
4923 /* use lines from cmdline first */
4924 if (take < room_cmdline)
4925 room_cmdline = take;
4926 take -= room_cmdline;
4927 topframe->fr_height += room_cmdline;
4928 }
4929
4930 /*
4931 * set the current frame to the new height
4932 */
4933 frame_new_height(curfrp, height, FALSE, FALSE);
4934
4935 /*
4936 * First take lines from the frames after the current frame. If
4937 * that is not enough, takes lines from frames above the current
4938 * frame.
4939 */
4940 for (run = 0; run < 2; ++run)
4941 {
4942 if (run == 0)
4943 frp = curfrp->fr_next; /* 1st run: start with next window */
4944 else
4945 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4946 while (frp != NULL && take != 0)
4947 {
4948 h = frame_minheight(frp, NULL);
4949 if (room_reserved > 0
4950 && frp->fr_win != NULL
4951 && frp->fr_win->w_p_wfh)
4952 {
4953 if (room_reserved >= frp->fr_height)
4954 room_reserved -= frp->fr_height;
4955 else
4956 {
4957 if (frp->fr_height - room_reserved > take)
4958 room_reserved = frp->fr_height - take;
4959 take -= frp->fr_height - room_reserved;
4960 frame_new_height(frp, room_reserved, FALSE, FALSE);
4961 room_reserved = 0;
4962 }
4963 }
4964 else
4965 {
4966 if (frp->fr_height - take < h)
4967 {
4968 take -= frp->fr_height - h;
4969 frame_new_height(frp, h, FALSE, FALSE);
4970 }
4971 else
4972 {
4973 frame_new_height(frp, frp->fr_height - take,
4974 FALSE, FALSE);
4975 take = 0;
4976 }
4977 }
4978 if (run == 0)
4979 frp = frp->fr_next;
4980 else
4981 frp = frp->fr_prev;
4982 }
4983 }
4984 }
4985}
4986
4987#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4988/*
4989 * Set current window width and take care of repositioning other windows to
4990 * fit around it.
4991 */
4992 void
4993win_setwidth(width)
4994 int width;
4995{
4996 win_setwidth_win(width, curwin);
4997}
4998
4999 void
5000win_setwidth_win(width, wp)
5001 int width;
5002 win_T *wp;
5003{
5004 /* Always keep current window at least one column wide, even when
5005 * 'winminwidth' is zero. */
5006 if (wp == curwin)
5007 {
5008 if (width < p_wmw)
5009 width = p_wmw;
5010 if (width == 0)
5011 width = 1;
5012 }
5013
5014 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5015
5016 /* recompute the window positions */
5017 (void)win_comp_pos();
5018
5019 redraw_all_later(NOT_VALID);
5020}
5021
5022/*
5023 * Set the width of a frame to "width" and take care that all frames and
5024 * windows inside it are resized. Also resize frames above and below if the
5025 * are in the same FR_ROW frame.
5026 *
5027 * Strategy is similar to frame_setheight().
5028 */
5029 static void
5030frame_setwidth(curfrp, width)
5031 frame_T *curfrp;
5032 int width;
5033{
5034 int room; /* total number of lines available */
5035 int take; /* number of lines taken from other windows */
5036 int run;
5037 frame_T *frp;
5038 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005039 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040
5041 /* If the width already is the desired value, nothing to do. */
5042 if (curfrp->fr_width == width)
5043 return;
5044
5045 if (curfrp->fr_parent == NULL)
5046 /* topframe: can't change width */
5047 return;
5048
5049 if (curfrp->fr_parent->fr_layout == FR_COL)
5050 {
5051 /* Column of frames: Also need to resize frames above and below of
5052 * this one. First check for the minimal width of these. */
5053 w = frame_minwidth(curfrp->fr_parent, NULL);
5054 if (width < w)
5055 width = w;
5056 frame_setwidth(curfrp->fr_parent, width);
5057 }
5058 else
5059 {
5060 /*
5061 * Row of frames: try to change only frames in this row.
5062 *
5063 * Do this twice:
5064 * 1: compute room available, if it's not enough try resizing the
5065 * containing frame.
5066 * 2: compute the room available and adjust the width to it.
5067 */
5068 for (run = 1; run <= 2; ++run)
5069 {
5070 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005071 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5073 frp = frp->fr_next)
5074 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005075 if (frp != curfrp
5076 && frp->fr_win != NULL
5077 && frp->fr_win->w_p_wfw)
5078 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 room += frp->fr_width;
5080 if (frp != curfrp)
5081 room -= frame_minwidth(frp, NULL);
5082 }
5083
5084 if (width <= room)
5085 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005086 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 {
5088 if (width > room)
5089 width = room;
5090 break;
5091 }
5092 frame_setwidth(curfrp->fr_parent, width
5093 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5094 }
5095
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 /*
5097 * Compute the number of lines we will take from others frames (can be
5098 * negative!).
5099 */
5100 take = width - curfrp->fr_width;
5101
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005102 /* If there is not enough room, also reduce the width of a window
5103 * with 'winfixwidth' set. */
5104 if (width > room - room_reserved)
5105 room_reserved = room - width;
5106 /* If there is only a 'winfixwidth' window and making the
5107 * window smaller, need to make the other window narrower. */
5108 if (take < 0 && room - curfrp->fr_width < room_reserved)
5109 room_reserved = 0;
5110
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 /*
5112 * set the current frame to the new width
5113 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005114 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115
5116 /*
5117 * First take lines from the frames right of the current frame. If
5118 * that is not enough, takes lines from frames left of the current
5119 * frame.
5120 */
5121 for (run = 0; run < 2; ++run)
5122 {
5123 if (run == 0)
5124 frp = curfrp->fr_next; /* 1st run: start with next window */
5125 else
5126 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5127 while (frp != NULL && take != 0)
5128 {
5129 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005130 if (room_reserved > 0
5131 && frp->fr_win != NULL
5132 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005134 if (room_reserved >= frp->fr_width)
5135 room_reserved -= frp->fr_width;
5136 else
5137 {
5138 if (frp->fr_width - room_reserved > take)
5139 room_reserved = frp->fr_width - take;
5140 take -= frp->fr_width - room_reserved;
5141 frame_new_width(frp, room_reserved, FALSE, FALSE);
5142 room_reserved = 0;
5143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
5145 else
5146 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005147 if (frp->fr_width - take < w)
5148 {
5149 take -= frp->fr_width - w;
5150 frame_new_width(frp, w, FALSE, FALSE);
5151 }
5152 else
5153 {
5154 frame_new_width(frp, frp->fr_width - take,
5155 FALSE, FALSE);
5156 take = 0;
5157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
5159 if (run == 0)
5160 frp = frp->fr_next;
5161 else
5162 frp = frp->fr_prev;
5163 }
5164 }
5165 }
5166}
5167#endif /* FEAT_VERTSPLIT */
5168
5169/*
5170 * Check 'winminheight' for a valid value.
5171 */
5172 void
5173win_setminheight()
5174{
5175 int room;
5176 int first = TRUE;
5177 win_T *wp;
5178
5179 /* loop until there is a 'winminheight' that is possible */
5180 while (p_wmh > 0)
5181 {
5182 /* TODO: handle vertical splits */
5183 room = -p_wh;
5184 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5185 room += wp->w_height - p_wmh;
5186 if (room >= 0)
5187 break;
5188 --p_wmh;
5189 if (first)
5190 {
5191 EMSG(_(e_noroom));
5192 first = FALSE;
5193 }
5194 }
5195}
5196
5197#ifdef FEAT_MOUSE
5198
5199/*
5200 * Status line of dragwin is dragged "offset" lines down (negative is up).
5201 */
5202 void
5203win_drag_status_line(dragwin, offset)
5204 win_T *dragwin;
5205 int offset;
5206{
5207 frame_T *curfr;
5208 frame_T *fr;
5209 int room;
5210 int row;
5211 int up; /* if TRUE, drag status line up, otherwise down */
5212 int n;
5213
5214 fr = dragwin->w_frame;
5215 curfr = fr;
5216 if (fr != topframe) /* more than one window */
5217 {
5218 fr = fr->fr_parent;
5219 /* When the parent frame is not a column of frames, its parent should
5220 * be. */
5221 if (fr->fr_layout != FR_COL)
5222 {
5223 curfr = fr;
5224 if (fr != topframe) /* only a row of windows, may drag statusline */
5225 fr = fr->fr_parent;
5226 }
5227 }
5228
5229 /* If this is the last frame in a column, may want to resize the parent
5230 * frame instead (go two up to skip a row of frames). */
5231 while (curfr != topframe && curfr->fr_next == NULL)
5232 {
5233 if (fr != topframe)
5234 fr = fr->fr_parent;
5235 curfr = fr;
5236 if (fr != topframe)
5237 fr = fr->fr_parent;
5238 }
5239
5240 if (offset < 0) /* drag up */
5241 {
5242 up = TRUE;
5243 offset = -offset;
5244 /* sum up the room of the current frame and above it */
5245 if (fr == curfr)
5246 {
5247 /* only one window */
5248 room = fr->fr_height - frame_minheight(fr, NULL);
5249 }
5250 else
5251 {
5252 room = 0;
5253 for (fr = fr->fr_child; ; fr = fr->fr_next)
5254 {
5255 room += fr->fr_height - frame_minheight(fr, NULL);
5256 if (fr == curfr)
5257 break;
5258 }
5259 }
5260 fr = curfr->fr_next; /* put fr at frame that grows */
5261 }
5262 else /* drag down */
5263 {
5264 up = FALSE;
5265 /*
5266 * Only dragging the last status line can reduce p_ch.
5267 */
5268 room = Rows - cmdline_row;
5269 if (curfr->fr_next == NULL)
5270 room -= 1;
5271 else
5272 room -= p_ch;
5273 if (room < 0)
5274 room = 0;
5275 /* sum up the room of frames below of the current one */
5276 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5277 room += fr->fr_height - frame_minheight(fr, NULL);
5278 fr = curfr; /* put fr at window that grows */
5279 }
5280
5281 if (room < offset) /* Not enough room */
5282 offset = room; /* Move as far as we can */
5283 if (offset <= 0)
5284 return;
5285
5286 /*
5287 * Grow frame fr by "offset" lines.
5288 * Doesn't happen when dragging the last status line up.
5289 */
5290 if (fr != NULL)
5291 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5292
5293 if (up)
5294 fr = curfr; /* current frame gets smaller */
5295 else
5296 fr = curfr->fr_next; /* next frame gets smaller */
5297
5298 /*
5299 * Now make the other frames smaller.
5300 */
5301 while (fr != NULL && offset > 0)
5302 {
5303 n = frame_minheight(fr, NULL);
5304 if (fr->fr_height - offset <= n)
5305 {
5306 offset -= fr->fr_height - n;
5307 frame_new_height(fr, n, !up, FALSE);
5308 }
5309 else
5310 {
5311 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5312 break;
5313 }
5314 if (up)
5315 fr = fr->fr_prev;
5316 else
5317 fr = fr->fr_next;
5318 }
5319 row = win_comp_pos();
5320 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5321 cmdline_row = row;
5322 p_ch = Rows - cmdline_row;
5323 if (p_ch < 1)
5324 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005325 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005326 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 showmode();
5328}
5329
5330#ifdef FEAT_VERTSPLIT
5331/*
5332 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5333 */
5334 void
5335win_drag_vsep_line(dragwin, offset)
5336 win_T *dragwin;
5337 int offset;
5338{
5339 frame_T *curfr;
5340 frame_T *fr;
5341 int room;
5342 int left; /* if TRUE, drag separator line left, otherwise right */
5343 int n;
5344
5345 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005346 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 return;
5348 curfr = fr;
5349 fr = fr->fr_parent;
5350 /* When the parent frame is not a row of frames, its parent should be. */
5351 if (fr->fr_layout != FR_ROW)
5352 {
5353 if (fr == topframe) /* only a column of windows (cannot happen?) */
5354 return;
5355 curfr = fr;
5356 fr = fr->fr_parent;
5357 }
5358
5359 /* If this is the last frame in a row, may want to resize a parent
5360 * frame instead. */
5361 while (curfr->fr_next == NULL)
5362 {
5363 if (fr == topframe)
5364 break;
5365 curfr = fr;
5366 fr = fr->fr_parent;
5367 if (fr != topframe)
5368 {
5369 curfr = fr;
5370 fr = fr->fr_parent;
5371 }
5372 }
5373
5374 if (offset < 0) /* drag left */
5375 {
5376 left = TRUE;
5377 offset = -offset;
5378 /* sum up the room of the current frame and left of it */
5379 room = 0;
5380 for (fr = fr->fr_child; ; fr = fr->fr_next)
5381 {
5382 room += fr->fr_width - frame_minwidth(fr, NULL);
5383 if (fr == curfr)
5384 break;
5385 }
5386 fr = curfr->fr_next; /* put fr at frame that grows */
5387 }
5388 else /* drag right */
5389 {
5390 left = FALSE;
5391 /* sum up the room of frames right of the current one */
5392 room = 0;
5393 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5394 room += fr->fr_width - frame_minwidth(fr, NULL);
5395 fr = curfr; /* put fr at window that grows */
5396 }
5397
5398 if (room < offset) /* Not enough room */
5399 offset = room; /* Move as far as we can */
5400 if (offset <= 0) /* No room at all, quit. */
5401 return;
5402
5403 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005404 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405
5406 /* shrink other frames: current and at the left or at the right */
5407 if (left)
5408 fr = curfr; /* current frame gets smaller */
5409 else
5410 fr = curfr->fr_next; /* next frame gets smaller */
5411
5412 while (fr != NULL && offset > 0)
5413 {
5414 n = frame_minwidth(fr, NULL);
5415 if (fr->fr_width - offset <= n)
5416 {
5417 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005418 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 }
5420 else
5421 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005422 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 break;
5424 }
5425 if (left)
5426 fr = fr->fr_prev;
5427 else
5428 fr = fr->fr_next;
5429 }
5430 (void)win_comp_pos();
5431 redraw_all_later(NOT_VALID);
5432}
5433#endif /* FEAT_VERTSPLIT */
5434#endif /* FEAT_MOUSE */
5435
5436#endif /* FEAT_WINDOWS */
5437
5438/*
5439 * Set the height of a window.
5440 * This takes care of the things inside the window, not what happens to the
5441 * window position, the frame or to other windows.
5442 */
5443 static void
5444win_new_height(wp, height)
5445 win_T *wp;
5446 int height;
5447{
5448 linenr_T lnum;
5449 int sline, line_size;
5450#define FRACTION_MULT 16384L
5451
5452 /* Don't want a negative height. Happens when splitting a tiny window.
5453 * Will equalize heights soon to fix it. */
5454 if (height < 0)
5455 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005456 if (wp->w_height == height)
5457 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458
5459 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5460 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5461 + FRACTION_MULT / 2) / (long)wp->w_height;
5462
5463 wp->w_height = height;
5464 wp->w_skipcol = 0;
5465
5466 /* Don't change w_topline when height is zero. Don't set w_topline when
5467 * 'scrollbind' is set and this isn't the current window. */
5468 if (height > 0
5469#ifdef FEAT_SCROLLBIND
5470 && (!wp->w_p_scb || wp == curwin)
5471#endif
5472 )
5473 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005474 /*
5475 * Find a value for w_topline that shows the cursor at the same
5476 * relative position in the window as before (more or less).
5477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 lnum = wp->w_cursor.lnum;
5479 if (lnum < 1) /* can happen when starting up */
5480 lnum = 1;
5481 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5482 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5483 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005484
5485 if (sline >= 0)
5486 {
5487 /* Make sure the whole cursor line is visible, if possible. */
5488 int rows = plines_win(wp, lnum, FALSE);
5489
5490 if (sline > wp->w_height - rows)
5491 {
5492 sline = wp->w_height - rows;
5493 wp->w_wrow -= rows - line_size;
5494 }
5495 }
5496
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 if (sline < 0)
5498 {
5499 /*
5500 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005501 * Make cursor line the first line in the window. If not enough
5502 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 */
5504 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005505 if (wp->w_wrow >= wp->w_height
5506 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5507 {
5508 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5509 --wp->w_wrow;
5510 while (wp->w_wrow >= wp->w_height)
5511 {
5512 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5513 + win_col_off2(wp);
5514 --wp->w_wrow;
5515 }
5516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 }
5518 else
5519 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005520 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 {
5522#ifdef FEAT_FOLDING
5523 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5524 if (lnum == 1)
5525 {
5526 /* first line in buffer is folded */
5527 line_size = 1;
5528 --sline;
5529 break;
5530 }
5531#endif
5532 --lnum;
5533#ifdef FEAT_DIFF
5534 if (lnum == wp->w_topline)
5535 line_size = plines_win_nofill(wp, lnum, TRUE)
5536 + wp->w_topfill;
5537 else
5538#endif
5539 line_size = plines_win(wp, lnum, TRUE);
5540 sline -= line_size;
5541 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005542
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 if (sline < 0)
5544 {
5545 /*
5546 * Line we want at top would go off top of screen. Use next
5547 * line instead.
5548 */
5549#ifdef FEAT_FOLDING
5550 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5551#endif
5552 lnum++;
5553 wp->w_wrow -= line_size + sline;
5554 }
5555 else if (sline > 0)
5556 {
5557 /* First line of file reached, use that as topline. */
5558 lnum = 1;
5559 wp->w_wrow -= sline;
5560 }
5561 }
5562 set_topline(wp, lnum);
5563 }
5564
5565 if (wp == curwin)
5566 {
5567 if (p_so)
5568 update_topline();
5569 curs_columns(FALSE); /* validate w_wrow */
5570 }
5571 wp->w_prev_fraction_row = wp->w_wrow;
5572
5573 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005574 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575#ifdef FEAT_WINDOWS
5576 wp->w_redr_status = TRUE;
5577#endif
5578 invalidate_botline_win(wp);
5579}
5580
5581#ifdef FEAT_VERTSPLIT
5582/*
5583 * Set the width of a window.
5584 */
5585 static void
5586win_new_width(wp, width)
5587 win_T *wp;
5588 int width;
5589{
5590 wp->w_width = width;
5591 wp->w_lines_valid = 0;
5592 changed_line_abv_curs_win(wp);
5593 invalidate_botline_win(wp);
5594 if (wp == curwin)
5595 {
5596 update_topline();
5597 curs_columns(TRUE); /* validate w_wrow */
5598 }
5599 redraw_win_later(wp, NOT_VALID);
5600 wp->w_redr_status = TRUE;
5601}
5602#endif
5603
5604 void
5605win_comp_scroll(wp)
5606 win_T *wp;
5607{
5608 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5609 if (wp->w_p_scr == 0)
5610 wp->w_p_scr = 1;
5611}
5612
5613/*
5614 * command_height: called whenever p_ch has been changed
5615 */
5616 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005617command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
5619#ifdef FEAT_WINDOWS
5620 int h;
5621 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005622 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005624 /* Use the value of p_ch that we remembered. This is needed for when the
5625 * GUI starts up, we can't be sure in what order things happen. And when
5626 * p_ch was changed in another tab page. */
5627 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005628
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 /* Find bottom frame with width of screen. */
5630 frp = lastwin->w_frame;
5631# ifdef FEAT_VERTSPLIT
5632 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5633 frp = frp->fr_parent;
5634# endif
5635
5636 /* Avoid changing the height of a window with 'winfixheight' set. */
5637 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5638 && frp->fr_win->w_p_wfh)
5639 frp = frp->fr_prev;
5640
5641 if (starting != NO_SCREEN)
5642 {
5643 cmdline_row = Rows - p_ch;
5644
5645 if (p_ch > old_p_ch) /* p_ch got bigger */
5646 {
5647 while (p_ch > old_p_ch)
5648 {
5649 if (frp == NULL)
5650 {
5651 EMSG(_(e_noroom));
5652 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005653 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 cmdline_row = Rows - p_ch;
5655 break;
5656 }
5657 h = frp->fr_height - frame_minheight(frp, NULL);
5658 if (h > p_ch - old_p_ch)
5659 h = p_ch - old_p_ch;
5660 old_p_ch += h;
5661 frame_add_height(frp, -h);
5662 frp = frp->fr_prev;
5663 }
5664
5665 /* Recompute window positions. */
5666 (void)win_comp_pos();
5667
5668 /* clear the lines added to cmdline */
5669 if (full_screen)
5670 screen_fill((int)(cmdline_row), (int)Rows, 0,
5671 (int)Columns, ' ', ' ', 0);
5672 msg_row = cmdline_row;
5673 redraw_cmdline = TRUE;
5674 return;
5675 }
5676
5677 if (msg_row < cmdline_row)
5678 msg_row = cmdline_row;
5679 redraw_cmdline = TRUE;
5680 }
5681 frame_add_height(frp, (int)(old_p_ch - p_ch));
5682
5683 /* Recompute window positions. */
5684 if (frp != lastwin->w_frame)
5685 (void)win_comp_pos();
5686#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005688 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689#endif
5690}
5691
5692#if defined(FEAT_WINDOWS) || defined(PROTO)
5693/*
5694 * Resize frame "frp" to be "n" lines higher (negative for less high).
5695 * Also resize the frames it is contained in.
5696 */
5697 static void
5698frame_add_height(frp, n)
5699 frame_T *frp;
5700 int n;
5701{
5702 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5703 for (;;)
5704 {
5705 frp = frp->fr_parent;
5706 if (frp == NULL)
5707 break;
5708 frp->fr_height += n;
5709 }
5710}
5711
5712/*
5713 * Add or remove a status line for the bottom window(s), according to the
5714 * value of 'laststatus'.
5715 */
5716 void
5717last_status(morewin)
5718 int morewin; /* pretend there are two or more windows */
5719{
5720 /* Don't make a difference between horizontal or vertical split. */
5721 last_status_rec(topframe, (p_ls == 2
5722 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5723}
5724
5725 static void
5726last_status_rec(fr, statusline)
5727 frame_T *fr;
5728 int statusline;
5729{
5730 frame_T *fp;
5731 win_T *wp;
5732
5733 if (fr->fr_layout == FR_LEAF)
5734 {
5735 wp = fr->fr_win;
5736 if (wp->w_status_height != 0 && !statusline)
5737 {
5738 /* remove status line */
5739 win_new_height(wp, wp->w_height + 1);
5740 wp->w_status_height = 0;
5741 comp_col();
5742 }
5743 else if (wp->w_status_height == 0 && statusline)
5744 {
5745 /* Find a frame to take a line from. */
5746 fp = fr;
5747 while (fp->fr_height <= frame_minheight(fp, NULL))
5748 {
5749 if (fp == topframe)
5750 {
5751 EMSG(_(e_noroom));
5752 return;
5753 }
5754 /* In a column of frames: go to frame above. If already at
5755 * the top or in a row of frames: go to parent. */
5756 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5757 fp = fp->fr_prev;
5758 else
5759 fp = fp->fr_parent;
5760 }
5761 wp->w_status_height = 1;
5762 if (fp != fr)
5763 {
5764 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5765 frame_fix_height(wp);
5766 (void)win_comp_pos();
5767 }
5768 else
5769 win_new_height(wp, wp->w_height - 1);
5770 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005771 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 }
5773 }
5774#ifdef FEAT_VERTSPLIT
5775 else if (fr->fr_layout == FR_ROW)
5776 {
5777 /* vertically split windows, set status line for each one */
5778 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5779 last_status_rec(fp, statusline);
5780 }
5781#endif
5782 else
5783 {
5784 /* horizontally split window, set status line for last one */
5785 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5786 ;
5787 last_status_rec(fp, statusline);
5788 }
5789}
5790
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005791/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005792 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005793 */
5794 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005795tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005796{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005797#ifdef FEAT_GUI_TABLINE
5798 /* When the GUI has the tabline then this always returns zero. */
5799 if (gui_use_tabline())
5800 return 0;
5801#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005802 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005803 {
5804 case 0: return 0;
5805 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5806 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005807 return 1;
5808}
5809
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810#endif /* FEAT_WINDOWS */
5811
5812#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5813/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005814 * Get the file name at the cursor.
5815 * If Visual mode is active, use the selected text if it's in one line.
5816 * Returns the name in allocated memory, NULL for failure.
5817 */
5818 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005819grab_file_name(count, file_lnum)
5820 long count;
5821 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005822{
5823# ifdef FEAT_VISUAL
5824 if (VIsual_active)
5825 {
5826 int len;
5827 char_u *ptr;
5828
5829 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5830 return NULL;
5831 return find_file_name_in_path(ptr, len,
5832 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5833 }
5834# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005835 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5836 file_lnum);
5837
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005838}
5839
5840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 * Return the file name under or after the cursor.
5842 *
5843 * The 'path' option is searched if the file name is not absolute.
5844 * The string returned has been alloc'ed and should be freed by the caller.
5845 * NULL is returned if the file name or file is not found.
5846 *
5847 * options:
5848 * FNAME_MESS give error messages
5849 * FNAME_EXP expand to path
5850 * FNAME_HYP check for hypertext link
5851 * FNAME_INCL apply "includeexpr"
5852 */
5853 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005854file_name_at_cursor(options, count, file_lnum)
5855 int options;
5856 long count;
5857 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858{
5859 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005860 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5861 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862}
5863
5864/*
5865 * Return the name of the file under or after ptr[col].
5866 * Otherwise like file_name_at_cursor().
5867 */
5868 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005869file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 char_u *line;
5871 int col;
5872 int options;
5873 long count;
5874 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005875 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876{
5877 char_u *ptr;
5878 int len;
5879
5880 /*
5881 * search forward for what could be the start of a file name
5882 */
5883 ptr = line + col;
5884 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005885 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 if (*ptr == NUL) /* nothing found */
5887 {
5888 if (options & FNAME_MESS)
5889 EMSG(_("E446: No file name under cursor"));
5890 return NULL;
5891 }
5892
5893 /*
5894 * Search backward for first char of the file name.
5895 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5896 */
5897 while (ptr > line)
5898 {
5899#ifdef FEAT_MBYTE
5900 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5901 ptr -= len + 1;
5902 else
5903#endif
5904 if (vim_isfilec(ptr[-1])
5905 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5906 --ptr;
5907 else
5908 break;
5909 }
5910
5911 /*
5912 * Search forward for the last char of the file name.
5913 * Also allow "://" when ':' is not in 'isfname'.
5914 */
5915 len = 0;
5916 while (vim_isfilec(ptr[len])
5917 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5918#ifdef FEAT_MBYTE
5919 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005920 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 else
5922#endif
5923 ++len;
5924
5925 /*
5926 * If there is trailing punctuation, remove it.
5927 * But don't remove "..", could be a directory name.
5928 */
5929 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5930 && ptr[len - 2] != '.')
5931 --len;
5932
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005933 if (file_lnum != NULL)
5934 {
5935 char_u *p;
5936
5937 /* Get the number after the file name and a separator character */
5938 p = ptr + len;
5939 p = skipwhite(p);
5940 if (*p != NUL)
5941 {
5942 if (!isdigit(*p))
5943 ++p; /* skip the separator */
5944 p = skipwhite(p);
5945 if (isdigit(*p))
5946 *file_lnum = (int)getdigits(&p);
5947 }
5948 }
5949
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5951}
5952
5953# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5954static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5955
5956 static char_u *
5957eval_includeexpr(ptr, len)
5958 char_u *ptr;
5959 int len;
5960{
5961 char_u *res;
5962
5963 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005964 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005965 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 set_vim_var_string(VV_FNAME, NULL, 0);
5967 return res;
5968}
5969#endif
5970
5971/*
5972 * Return the name of the file ptr[len] in 'path'.
5973 * Otherwise like file_name_at_cursor().
5974 */
5975 char_u *
5976find_file_name_in_path(ptr, len, options, count, rel_fname)
5977 char_u *ptr;
5978 int len;
5979 int options;
5980 long count;
5981 char_u *rel_fname; /* file we are searching relative to */
5982{
5983 char_u *file_name;
5984 int c;
5985# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5986 char_u *tofree = NULL;
5987
5988 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5989 {
5990 tofree = eval_includeexpr(ptr, len);
5991 if (tofree != NULL)
5992 {
5993 ptr = tofree;
5994 len = (int)STRLEN(ptr);
5995 }
5996 }
5997# endif
5998
5999 if (options & FNAME_EXP)
6000 {
6001 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6002 TRUE, rel_fname);
6003
6004# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6005 /*
6006 * If the file could not be found in a normal way, try applying
6007 * 'includeexpr' (unless done already).
6008 */
6009 if (file_name == NULL
6010 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6011 {
6012 tofree = eval_includeexpr(ptr, len);
6013 if (tofree != NULL)
6014 {
6015 ptr = tofree;
6016 len = (int)STRLEN(ptr);
6017 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6018 TRUE, rel_fname);
6019 }
6020 }
6021# endif
6022 if (file_name == NULL && (options & FNAME_MESS))
6023 {
6024 c = ptr[len];
6025 ptr[len] = NUL;
6026 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6027 ptr[len] = c;
6028 }
6029
6030 /* Repeat finding the file "count" times. This matters when it
6031 * appears several times in the path. */
6032 while (file_name != NULL && --count > 0)
6033 {
6034 vim_free(file_name);
6035 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6036 }
6037 }
6038 else
6039 file_name = vim_strnsave(ptr, len);
6040
6041# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6042 vim_free(tofree);
6043# endif
6044
6045 return file_name;
6046}
6047#endif /* FEAT_SEARCHPATH */
6048
6049/*
6050 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6051 * Also check for ":\\", which MS Internet Explorer accepts, return
6052 * URL_BACKSLASH.
6053 */
6054 static int
6055path_is_url(p)
6056 char_u *p;
6057{
6058 if (STRNCMP(p, "://", (size_t)3) == 0)
6059 return URL_SLASH;
6060 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6061 return URL_BACKSLASH;
6062 return 0;
6063}
6064
6065/*
6066 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6067 * Return URL_BACKSLASH for "name:\\".
6068 * Return zero otherwise.
6069 */
6070 int
6071path_with_url(fname)
6072 char_u *fname;
6073{
6074 char_u *p;
6075
6076 for (p = fname; isalpha(*p); ++p)
6077 ;
6078 return path_is_url(p);
6079}
6080
6081/*
6082 * Return TRUE if "name" is a full (absolute) path name or URL.
6083 */
6084 int
6085vim_isAbsName(name)
6086 char_u *name;
6087{
6088 return (path_with_url(name) != 0 || mch_isFullName(name));
6089}
6090
6091/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006092 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 *
6094 * return FAIL for failure, OK otherwise
6095 */
6096 int
6097vim_FullName(fname, buf, len, force)
6098 char_u *fname, *buf;
6099 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006100 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101{
6102 int retval = OK;
6103 int url;
6104
6105 *buf = NUL;
6106 if (fname == NULL)
6107 return FAIL;
6108
6109 url = path_with_url(fname);
6110 if (!url)
6111 retval = mch_FullName(fname, buf, len, force);
6112 if (url || retval == FAIL)
6113 {
6114 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006115 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 }
6117#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6118 slash_adjust(buf);
6119#endif
6120 return retval;
6121}
6122
6123/*
6124 * Return the minimal number of rows that is needed on the screen to display
6125 * the current number of windows.
6126 */
6127 int
6128min_rows()
6129{
6130 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006131#ifdef FEAT_WINDOWS
6132 tabpage_T *tp;
6133 int n;
6134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135
6136 if (firstwin == NULL) /* not initialized yet */
6137 return MIN_LINES;
6138
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006140 total = 0;
6141 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6142 {
6143 n = frame_minheight(tp->tp_topframe, NULL);
6144 if (total < n)
6145 total = n;
6146 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006147 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006149 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006151 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 return total;
6153}
6154
6155/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006156 * Return TRUE if there is only one window (in the current tab page), not
6157 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006158 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 */
6160 int
6161only_one_window()
6162{
6163#ifdef FEAT_WINDOWS
6164 int count = 0;
6165 win_T *wp;
6166
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006167 /* If there is another tab page there always is another window. */
6168 if (first_tabpage->tp_next != NULL)
6169 return FALSE;
6170
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006172 if ((!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173# ifdef FEAT_QUICKFIX
6174 || wp->w_p_pvw
6175# endif
6176 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006177# ifdef FEAT_AUTOCMD
6178 && wp != aucmd_win
6179# endif
6180 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 ++count;
6182 return (count <= 1);
6183#else
6184 return TRUE;
6185#endif
6186}
6187
6188#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6189/*
6190 * Correct the cursor line number in other windows. Used after changing the
6191 * current buffer, and before applying autocommands.
6192 * When "do_curwin" is TRUE, also check current window.
6193 */
6194 void
6195check_lnums(do_curwin)
6196 int do_curwin;
6197{
6198 win_T *wp;
6199
6200#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006201 tabpage_T *tp;
6202
6203 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6205#else
6206 wp = curwin;
6207 if (do_curwin)
6208#endif
6209 {
6210 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6211 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6212 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6213 wp->w_topline = curbuf->b_ml.ml_line_count;
6214 }
6215}
6216#endif
6217
6218#if defined(FEAT_WINDOWS) || defined(PROTO)
6219
6220/*
6221 * A snapshot of the window sizes, to restore them after closing the help
6222 * window.
6223 * Only these fields are used:
6224 * fr_layout
6225 * fr_width
6226 * fr_height
6227 * fr_next
6228 * fr_child
6229 * fr_win (only valid for the old curwin, NULL otherwise)
6230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231
6232/*
6233 * Create a snapshot of the current frame sizes.
6234 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006235 void
6236make_snapshot(idx)
6237 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006239 clear_snapshot(curtab, idx);
6240 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241}
6242
6243 static void
6244make_snapshot_rec(fr, frp)
6245 frame_T *fr;
6246 frame_T **frp;
6247{
6248 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6249 if (*frp == NULL)
6250 return;
6251 (*frp)->fr_layout = fr->fr_layout;
6252# ifdef FEAT_VERTSPLIT
6253 (*frp)->fr_width = fr->fr_width;
6254# endif
6255 (*frp)->fr_height = fr->fr_height;
6256 if (fr->fr_next != NULL)
6257 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6258 if (fr->fr_child != NULL)
6259 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6260 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6261 (*frp)->fr_win = curwin;
6262}
6263
6264/*
6265 * Remove any existing snapshot.
6266 */
6267 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006268clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006269 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006270 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006272 clear_snapshot_rec(tp->tp_snapshot[idx]);
6273 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274}
6275
6276 static void
6277clear_snapshot_rec(fr)
6278 frame_T *fr;
6279{
6280 if (fr != NULL)
6281 {
6282 clear_snapshot_rec(fr->fr_next);
6283 clear_snapshot_rec(fr->fr_child);
6284 vim_free(fr);
6285 }
6286}
6287
6288/*
6289 * Restore a previously created snapshot, if there is any.
6290 * This is only done if the screen size didn't change and the window layout is
6291 * still the same.
6292 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006293 void
6294restore_snapshot(idx, close_curwin)
6295 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 int close_curwin; /* closing current window */
6297{
6298 win_T *wp;
6299
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006300 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006302 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006304 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6305 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006307 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 win_comp_pos();
6309 if (wp != NULL && close_curwin)
6310 win_goto(wp);
6311 redraw_all_later(CLEAR);
6312 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006313 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314}
6315
6316/*
6317 * Check if frames "sn" and "fr" have the same layout, same following frames
6318 * and same children.
6319 */
6320 static int
6321check_snapshot_rec(sn, fr)
6322 frame_T *sn;
6323 frame_T *fr;
6324{
6325 if (sn->fr_layout != fr->fr_layout
6326 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6327 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6328 || (sn->fr_next != NULL
6329 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6330 || (sn->fr_child != NULL
6331 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6332 return FAIL;
6333 return OK;
6334}
6335
6336/*
6337 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6338 * following frames and children.
6339 * Returns a pointer to the old current window, or NULL.
6340 */
6341 static win_T *
6342restore_snapshot_rec(sn, fr)
6343 frame_T *sn;
6344 frame_T *fr;
6345{
6346 win_T *wp = NULL;
6347 win_T *wp2;
6348
6349 fr->fr_height = sn->fr_height;
6350# ifdef FEAT_VERTSPLIT
6351 fr->fr_width = sn->fr_width;
6352# endif
6353 if (fr->fr_layout == FR_LEAF)
6354 {
6355 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6356# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006357 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358# endif
6359 wp = sn->fr_win;
6360 }
6361 if (sn->fr_next != NULL)
6362 {
6363 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6364 if (wp2 != NULL)
6365 wp = wp2;
6366 }
6367 if (sn->fr_child != NULL)
6368 {
6369 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6370 if (wp2 != NULL)
6371 wp = wp2;
6372 }
6373 return wp;
6374}
6375
6376#endif
6377
6378#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6379/*
6380 * Return TRUE if there is any vertically split window.
6381 */
6382 int
6383win_hasvertsplit()
6384{
6385 frame_T *fr;
6386
6387 if (topframe->fr_layout == FR_ROW)
6388 return TRUE;
6389
6390 if (topframe->fr_layout == FR_COL)
6391 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6392 if (fr->fr_layout == FR_ROW)
6393 return TRUE;
6394
6395 return FALSE;
6396}
6397#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006398
6399#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6400/*
6401 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006402 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006403 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6404 * If no particular ID is desired, -1 must be specified for 'id'.
6405 * Return ID of added match, -1 on failure.
6406 */
6407 int
6408match_add(wp, grp, pat, prio, id)
6409 win_T *wp;
6410 char_u *grp;
6411 char_u *pat;
6412 int prio;
6413 int id;
6414{
6415 matchitem_T *cur;
6416 matchitem_T *prev;
6417 matchitem_T *m;
6418 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006419 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006420
6421 if (*grp == NUL || *pat == NUL)
6422 return -1;
6423 if (id < -1 || id == 0)
6424 {
6425 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6426 return -1;
6427 }
6428 if (id != -1)
6429 {
6430 cur = wp->w_match_head;
6431 while (cur != NULL)
6432 {
6433 if (cur->id == id)
6434 {
6435 EMSGN("E801: ID already taken: %ld", id);
6436 return -1;
6437 }
6438 cur = cur->next;
6439 }
6440 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006441 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006442 {
6443 EMSG2(_(e_nogroup), grp);
6444 return -1;
6445 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006446 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006447 {
6448 EMSG2(_(e_invarg2), pat);
6449 return -1;
6450 }
6451
6452 /* Find available match ID. */
6453 while (id == -1)
6454 {
6455 cur = wp->w_match_head;
6456 while (cur != NULL && cur->id != wp->w_next_match_id)
6457 cur = cur->next;
6458 if (cur == NULL)
6459 id = wp->w_next_match_id;
6460 wp->w_next_match_id++;
6461 }
6462
6463 /* Build new match. */
6464 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6465 m->id = id;
6466 m->priority = prio;
6467 m->pattern = vim_strsave(pat);
6468 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006469 m->match.regprog = regprog;
6470 m->match.rmm_ic = FALSE;
6471 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006472
6473 /* Insert new match. The match list is in ascending order with regard to
6474 * the match priorities. */
6475 cur = wp->w_match_head;
6476 prev = cur;
6477 while (cur != NULL && prio >= cur->priority)
6478 {
6479 prev = cur;
6480 cur = cur->next;
6481 }
6482 if (cur == prev)
6483 wp->w_match_head = m;
6484 else
6485 prev->next = m;
6486 m->next = cur;
6487
6488 redraw_later(SOME_VALID);
6489 return id;
6490}
6491
6492/*
6493 * Delete match with ID 'id' in the match list of window 'wp'.
6494 * Print error messages if 'perr' is TRUE.
6495 */
6496 int
6497match_delete(wp, id, perr)
6498 win_T *wp;
6499 int id;
6500 int perr;
6501{
6502 matchitem_T *cur = wp->w_match_head;
6503 matchitem_T *prev = cur;
6504
6505 if (id < 1)
6506 {
6507 if (perr == TRUE)
6508 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6509 id);
6510 return -1;
6511 }
6512 while (cur != NULL && cur->id != id)
6513 {
6514 prev = cur;
6515 cur = cur->next;
6516 }
6517 if (cur == NULL)
6518 {
6519 if (perr == TRUE)
6520 EMSGN("E803: ID not found: %ld", id);
6521 return -1;
6522 }
6523 if (cur == prev)
6524 wp->w_match_head = cur->next;
6525 else
6526 prev->next = cur->next;
6527 vim_free(cur->match.regprog);
6528 vim_free(cur->pattern);
6529 vim_free(cur);
6530 redraw_later(SOME_VALID);
6531 return 0;
6532}
6533
6534/*
6535 * Delete all matches in the match list of window 'wp'.
6536 */
6537 void
6538clear_matches(wp)
6539 win_T *wp;
6540{
6541 matchitem_T *m;
6542
6543 while (wp->w_match_head != NULL)
6544 {
6545 m = wp->w_match_head->next;
6546 vim_free(wp->w_match_head->match.regprog);
6547 vim_free(wp->w_match_head->pattern);
6548 vim_free(wp->w_match_head);
6549 wp->w_match_head = m;
6550 }
6551 redraw_later(SOME_VALID);
6552}
6553
6554/*
6555 * Get match from ID 'id' in window 'wp'.
6556 * Return NULL if match not found.
6557 */
6558 matchitem_T *
6559get_match(wp, id)
6560 win_T *wp;
6561 int id;
6562{
6563 matchitem_T *cur = wp->w_match_head;
6564
6565 while (cur != NULL && cur->id != id)
6566 cur = cur->next;
6567 return cur;
6568}
6569#endif