blob: 9f7131e90d202db6b152cf929e9bfb051d57a02b [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;
2310
2311 /* Close the link to the buffer. */
2312 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2313
2314 /* Careful: Autocommands may have closed the tab page or made it the
2315 * current tab page. */
2316 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2317 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002318 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002319 return;
2320
2321 /* Autocommands may have closed the window already. */
2322 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2323 ;
2324 if (wp == NULL)
2325 return;
2326
2327 /* Free the memory used for the window. */
2328 wp = win_free_mem(win, &dir, tp);
2329
2330 /* When closing the last window in a tab page remove the tab page. */
2331 if (wp == NULL)
2332 {
2333 if (tp == first_tabpage)
2334 first_tabpage = tp->tp_next;
2335 else
2336 {
2337 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2338 ptp = ptp->tp_next)
2339 ;
2340 if (ptp == NULL)
2341 {
2342 EMSG2(_(e_intern2), "win_close_othertab()");
2343 return;
2344 }
2345 ptp->tp_next = tp->tp_next;
2346 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002347 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002348 }
2349}
2350
2351/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002352 * Free the memory used for a window.
2353 * Returns a pointer to the window that got the freed up space.
2354 */
2355 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002356win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002357 win_T *win;
2358 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002359 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002360{
2361 frame_T *frp;
2362 win_T *wp;
2363
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002364 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002365 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002366 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002367 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002368 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002369
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002370 /* When deleting the current window of another tab page select a new
2371 * current window. */
2372 if (tp != NULL && win == tp->tp_curwin)
2373 tp->tp_curwin = wp;
2374
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002375 return wp;
2376}
2377
2378#if defined(EXITFREE) || defined(PROTO)
2379 void
2380win_free_all()
2381{
2382 int dummy;
2383
Bram Moolenaarf740b292006-02-16 22:11:02 +00002384# ifdef FEAT_WINDOWS
2385 while (first_tabpage->tp_next != NULL)
2386 tabpage_close(TRUE);
2387# endif
2388
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002389# ifdef FEAT_AUTOCMD
2390 if (aucmd_win != NULL)
2391 {
2392 (void)win_free_mem(aucmd_win, &dummy, NULL);
2393 aucmd_win = NULL;
2394 }
2395# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002396
2397 while (firstwin != NULL)
2398 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002399}
2400#endif
2401
2402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 * Remove a window and its frame from the tree of frames.
2404 * Returns a pointer to the window that got the freed up space.
2405 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002406 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002407winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002409 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002410 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
2412 frame_T *frp, *frp2, *frp3;
2413 frame_T *frp_close = win->w_frame;
2414 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
2416 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002417 * If there is only one window there is nothing to remove.
2418 */
2419 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2420 return NULL;
2421
2422 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 * Remove the window from its frame.
2424 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002425 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 wp = frame2win(frp2);
2427
2428 /* Remove this frame from the list of frames. */
2429 frame_remove(frp_close);
2430
2431#ifdef FEAT_VERTSPLIT
2432 if (frp_close->fr_parent->fr_layout == FR_COL)
2433 {
2434#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002435 /* When 'winfixheight' is set, try to find another frame in the column
2436 * (as close to the closed frame as possible) to distribute the height
2437 * to. */
2438 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2439 {
2440 frp = frp_close->fr_prev;
2441 frp3 = frp_close->fr_next;
2442 while (frp != NULL || frp3 != NULL)
2443 {
2444 if (frp != NULL)
2445 {
2446 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2447 {
2448 frp2 = frp;
2449 wp = frp->fr_win;
2450 break;
2451 }
2452 frp = frp->fr_prev;
2453 }
2454 if (frp3 != NULL)
2455 {
2456 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2457 {
2458 frp2 = frp3;
2459 wp = frp3->fr_win;
2460 break;
2461 }
2462 frp3 = frp3->fr_next;
2463 }
2464 }
2465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2467 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468#ifdef FEAT_VERTSPLIT
2469 *dirp = 'v';
2470 }
2471 else
2472 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002473 /* When 'winfixwidth' is set, try to find another frame in the column
2474 * (as close to the closed frame as possible) to distribute the width
2475 * to. */
2476 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2477 {
2478 frp = frp_close->fr_prev;
2479 frp3 = frp_close->fr_next;
2480 while (frp != NULL || frp3 != NULL)
2481 {
2482 if (frp != NULL)
2483 {
2484 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2485 {
2486 frp2 = frp;
2487 wp = frp->fr_win;
2488 break;
2489 }
2490 frp = frp->fr_prev;
2491 }
2492 if (frp3 != NULL)
2493 {
2494 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2495 {
2496 frp2 = frp3;
2497 wp = frp3->fr_win;
2498 break;
2499 }
2500 frp3 = frp3->fr_next;
2501 }
2502 }
2503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002505 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 *dirp = 'h';
2507 }
2508#endif
2509
2510 /* If rows/columns go to a window below/right its positions need to be
2511 * updated. Can only be done after the sizes have been updated. */
2512 if (frp2 == frp_close->fr_next)
2513 {
2514 int row = win->w_winrow;
2515 int col = W_WINCOL(win);
2516
2517 frame_comp_pos(frp2, &row, &col);
2518 }
2519
2520 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2521 {
2522 /* There is no other frame in this list, move its info to the parent
2523 * and remove it. */
2524 frp2->fr_parent->fr_layout = frp2->fr_layout;
2525 frp2->fr_parent->fr_child = frp2->fr_child;
2526 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2527 frp->fr_parent = frp2->fr_parent;
2528 frp2->fr_parent->fr_win = frp2->fr_win;
2529 if (frp2->fr_win != NULL)
2530 frp2->fr_win->w_frame = frp2->fr_parent;
2531 frp = frp2->fr_parent;
2532 vim_free(frp2);
2533
2534 frp2 = frp->fr_parent;
2535 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2536 {
2537 /* The frame above the parent has the same layout, have to merge
2538 * the frames into this list. */
2539 if (frp2->fr_child == frp)
2540 frp2->fr_child = frp->fr_child;
2541 frp->fr_child->fr_prev = frp->fr_prev;
2542 if (frp->fr_prev != NULL)
2543 frp->fr_prev->fr_next = frp->fr_child;
2544 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2545 {
2546 frp3->fr_parent = frp2;
2547 if (frp3->fr_next == NULL)
2548 {
2549 frp3->fr_next = frp->fr_next;
2550 if (frp->fr_next != NULL)
2551 frp->fr_next->fr_prev = frp3;
2552 break;
2553 }
2554 }
2555 vim_free(frp);
2556 }
2557 }
2558
2559 return wp;
2560}
2561
2562/*
2563 * Find out which frame is going to get the freed up space when "win" is
2564 * closed.
2565 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2566 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2567 * This makes opening a window and closing it immediately keep the same window
2568 * layout.
2569 */
2570 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002571win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002573 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574{
2575 frame_T *frp;
2576 int b;
2577
Bram Moolenaarf740b292006-02-16 22:11:02 +00002578 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002579 /* Last window in this tab page, will go to next tab page. */
2580 return alt_tabpage()->tp_curwin->w_frame;
2581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 frp = win->w_frame;
2583#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002584 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 b = p_spr;
2586 else
2587#endif
2588 b = p_sb;
2589 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2590 return frp->fr_next;
2591 return frp->fr_prev;
2592}
2593
2594/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002595 * Return the tabpage that will be used if the current one is closed.
2596 */
2597 static tabpage_T *
2598alt_tabpage()
2599{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002600 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002601
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002602 /* Use the next tab page if possible. */
2603 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002604 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002605
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002606 /* Find the last but one tab page. */
2607 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2608 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002609 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002610}
2611
2612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 * Find the left-upper window in frame "frp".
2614 */
2615 static win_T *
2616frame2win(frp)
2617 frame_T *frp;
2618{
2619 while (frp->fr_win == NULL)
2620 frp = frp->fr_child;
2621 return frp->fr_win;
2622}
2623
2624/*
2625 * Return TRUE if frame "frp" contains window "wp".
2626 */
2627 static int
2628frame_has_win(frp, wp)
2629 frame_T *frp;
2630 win_T *wp;
2631{
2632 frame_T *p;
2633
2634 if (frp->fr_layout == FR_LEAF)
2635 return frp->fr_win == wp;
2636
2637 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2638 if (frame_has_win(p, wp))
2639 return TRUE;
2640 return FALSE;
2641}
2642
2643/*
2644 * Set a new height for a frame. Recursively sets the height for contained
2645 * frames and windows. Caller must take care of positions.
2646 */
2647 static void
2648frame_new_height(topfrp, height, topfirst, wfh)
2649 frame_T *topfrp;
2650 int height;
2651 int topfirst; /* resize topmost contained frame first */
2652 int wfh; /* obey 'winfixheight' when there is a choice;
2653 may cause the height not to be set */
2654{
2655 frame_T *frp;
2656 int extra_lines;
2657 int h;
2658
2659 if (topfrp->fr_win != NULL)
2660 {
2661 /* Simple case: just one window. */
2662 win_new_height(topfrp->fr_win,
2663 height - topfrp->fr_win->w_status_height);
2664 }
2665#ifdef FEAT_VERTSPLIT
2666 else if (topfrp->fr_layout == FR_ROW)
2667 {
2668 do
2669 {
2670 /* All frames in this row get the same new height. */
2671 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2672 {
2673 frame_new_height(frp, height, topfirst, wfh);
2674 if (frp->fr_height > height)
2675 {
2676 /* Could not fit the windows, make the whole row higher. */
2677 height = frp->fr_height;
2678 break;
2679 }
2680 }
2681 }
2682 while (frp != NULL);
2683 }
2684#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002685 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 {
2687 /* Complicated case: Resize a column of frames. Resize the bottom
2688 * frame first, frames above that when needed. */
2689
2690 frp = topfrp->fr_child;
2691 if (wfh)
2692 /* Advance past frames with one window with 'wfh' set. */
2693 while (frame_fixed_height(frp))
2694 {
2695 frp = frp->fr_next;
2696 if (frp == NULL)
2697 return; /* no frame without 'wfh', give up */
2698 }
2699 if (!topfirst)
2700 {
2701 /* Find the bottom frame of this column */
2702 while (frp->fr_next != NULL)
2703 frp = frp->fr_next;
2704 if (wfh)
2705 /* Advance back for frames with one window with 'wfh' set. */
2706 while (frame_fixed_height(frp))
2707 frp = frp->fr_prev;
2708 }
2709
2710 extra_lines = height - topfrp->fr_height;
2711 if (extra_lines < 0)
2712 {
2713 /* reduce height of contained frames, bottom or top frame first */
2714 while (frp != NULL)
2715 {
2716 h = frame_minheight(frp, NULL);
2717 if (frp->fr_height + extra_lines < h)
2718 {
2719 extra_lines += frp->fr_height - h;
2720 frame_new_height(frp, h, topfirst, wfh);
2721 }
2722 else
2723 {
2724 frame_new_height(frp, frp->fr_height + extra_lines,
2725 topfirst, wfh);
2726 break;
2727 }
2728 if (topfirst)
2729 {
2730 do
2731 frp = frp->fr_next;
2732 while (wfh && frp != NULL && frame_fixed_height(frp));
2733 }
2734 else
2735 {
2736 do
2737 frp = frp->fr_prev;
2738 while (wfh && frp != NULL && frame_fixed_height(frp));
2739 }
2740 /* Increase "height" if we could not reduce enough frames. */
2741 if (frp == NULL)
2742 height -= extra_lines;
2743 }
2744 }
2745 else if (extra_lines > 0)
2746 {
2747 /* increase height of bottom or top frame */
2748 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2749 }
2750 }
2751 topfrp->fr_height = height;
2752}
2753
2754/*
2755 * Return TRUE if height of frame "frp" should not be changed because of
2756 * the 'winfixheight' option.
2757 */
2758 static int
2759frame_fixed_height(frp)
2760 frame_T *frp;
2761{
2762 /* frame with one window: fixed height if 'winfixheight' set. */
2763 if (frp->fr_win != NULL)
2764 return frp->fr_win->w_p_wfh;
2765
2766 if (frp->fr_layout == FR_ROW)
2767 {
2768 /* The frame is fixed height if one of the frames in the row is fixed
2769 * height. */
2770 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2771 if (frame_fixed_height(frp))
2772 return TRUE;
2773 return FALSE;
2774 }
2775
2776 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2777 * frames in the row are fixed height. */
2778 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2779 if (!frame_fixed_height(frp))
2780 return FALSE;
2781 return TRUE;
2782}
2783
2784#ifdef FEAT_VERTSPLIT
2785/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002786 * Return TRUE if width of frame "frp" should not be changed because of
2787 * the 'winfixwidth' option.
2788 */
2789 static int
2790frame_fixed_width(frp)
2791 frame_T *frp;
2792{
2793 /* frame with one window: fixed width if 'winfixwidth' set. */
2794 if (frp->fr_win != NULL)
2795 return frp->fr_win->w_p_wfw;
2796
2797 if (frp->fr_layout == FR_COL)
2798 {
2799 /* The frame is fixed width if one of the frames in the row is fixed
2800 * width. */
2801 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2802 if (frame_fixed_width(frp))
2803 return TRUE;
2804 return FALSE;
2805 }
2806
2807 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2808 * frames in the row are fixed width. */
2809 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2810 if (!frame_fixed_width(frp))
2811 return FALSE;
2812 return TRUE;
2813}
2814
2815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 * Add a status line to windows at the bottom of "frp".
2817 * Note: Does not check if there is room!
2818 */
2819 static void
2820frame_add_statusline(frp)
2821 frame_T *frp;
2822{
2823 win_T *wp;
2824
2825 if (frp->fr_layout == FR_LEAF)
2826 {
2827 wp = frp->fr_win;
2828 if (wp->w_status_height == 0)
2829 {
2830 if (wp->w_height > 0) /* don't make it negative */
2831 --wp->w_height;
2832 wp->w_status_height = STATUS_HEIGHT;
2833 }
2834 }
2835 else if (frp->fr_layout == FR_ROW)
2836 {
2837 /* Handle all the frames in the row. */
2838 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2839 frame_add_statusline(frp);
2840 }
2841 else /* frp->fr_layout == FR_COL */
2842 {
2843 /* Only need to handle the last frame in the column. */
2844 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2845 ;
2846 frame_add_statusline(frp);
2847 }
2848}
2849
2850/*
2851 * Set width of a frame. Handles recursively going through contained frames.
2852 * May remove separator line for windows at the right side (for win_close()).
2853 */
2854 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002855frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 frame_T *topfrp;
2857 int width;
2858 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002859 int wfw; /* obey 'winfixwidth' when there is a choice;
2860 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861{
2862 frame_T *frp;
2863 int extra_cols;
2864 int w;
2865 win_T *wp;
2866
2867 if (topfrp->fr_layout == FR_LEAF)
2868 {
2869 /* Simple case: just one window. */
2870 wp = topfrp->fr_win;
2871 /* Find out if there are any windows right of this one. */
2872 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2873 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2874 break;
2875 if (frp->fr_parent == NULL)
2876 wp->w_vsep_width = 0;
2877 win_new_width(wp, width - wp->w_vsep_width);
2878 }
2879 else if (topfrp->fr_layout == FR_COL)
2880 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002881 do
2882 {
2883 /* All frames in this column get the same new width. */
2884 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2885 {
2886 frame_new_width(frp, width, leftfirst, wfw);
2887 if (frp->fr_width > width)
2888 {
2889 /* Could not fit the windows, make whole column wider. */
2890 width = frp->fr_width;
2891 break;
2892 }
2893 }
2894 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 }
2896 else /* fr_layout == FR_ROW */
2897 {
2898 /* Complicated case: Resize a row of frames. Resize the rightmost
2899 * frame first, frames left of it when needed. */
2900
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002902 if (wfw)
2903 /* Advance past frames with one window with 'wfw' set. */
2904 while (frame_fixed_width(frp))
2905 {
2906 frp = frp->fr_next;
2907 if (frp == NULL)
2908 return; /* no frame without 'wfw', give up */
2909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002911 {
2912 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 while (frp->fr_next != NULL)
2914 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002915 if (wfw)
2916 /* Advance back for frames with one window with 'wfw' set. */
2917 while (frame_fixed_width(frp))
2918 frp = frp->fr_prev;
2919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920
2921 extra_cols = width - topfrp->fr_width;
2922 if (extra_cols < 0)
2923 {
2924 /* reduce frame width, rightmost frame first */
2925 while (frp != NULL)
2926 {
2927 w = frame_minwidth(frp, NULL);
2928 if (frp->fr_width + extra_cols < w)
2929 {
2930 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002931 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 }
2933 else
2934 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002935 frame_new_width(frp, frp->fr_width + extra_cols,
2936 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 break;
2938 }
2939 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002940 {
2941 do
2942 frp = frp->fr_next;
2943 while (wfw && frp != NULL && frame_fixed_width(frp));
2944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002946 {
2947 do
2948 frp = frp->fr_prev;
2949 while (wfw && frp != NULL && frame_fixed_width(frp));
2950 }
2951 /* Increase "width" if we could not reduce enough frames. */
2952 if (frp == NULL)
2953 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 }
2955 }
2956 else if (extra_cols > 0)
2957 {
2958 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002959 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 }
2961 }
2962 topfrp->fr_width = width;
2963}
2964
2965/*
2966 * Add the vertical separator to windows at the right side of "frp".
2967 * Note: Does not check if there is room!
2968 */
2969 static void
2970frame_add_vsep(frp)
2971 frame_T *frp;
2972{
2973 win_T *wp;
2974
2975 if (frp->fr_layout == FR_LEAF)
2976 {
2977 wp = frp->fr_win;
2978 if (wp->w_vsep_width == 0)
2979 {
2980 if (wp->w_width > 0) /* don't make it negative */
2981 --wp->w_width;
2982 wp->w_vsep_width = 1;
2983 }
2984 }
2985 else if (frp->fr_layout == FR_COL)
2986 {
2987 /* Handle all the frames in the column. */
2988 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2989 frame_add_vsep(frp);
2990 }
2991 else /* frp->fr_layout == FR_ROW */
2992 {
2993 /* Only need to handle the last frame in the row. */
2994 frp = frp->fr_child;
2995 while (frp->fr_next != NULL)
2996 frp = frp->fr_next;
2997 frame_add_vsep(frp);
2998 }
2999}
3000
3001/*
3002 * Set frame width from the window it contains.
3003 */
3004 static void
3005frame_fix_width(wp)
3006 win_T *wp;
3007{
3008 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3009}
3010#endif
3011
3012/*
3013 * Set frame height from the window it contains.
3014 */
3015 static void
3016frame_fix_height(wp)
3017 win_T *wp;
3018{
3019 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3020}
3021
3022/*
3023 * Compute the minimal height for frame "topfrp".
3024 * Uses the 'winminheight' option.
3025 * When "next_curwin" isn't NULL, use p_wh for this window.
3026 * When "next_curwin" is NOWIN, don't use at least one line for the current
3027 * window.
3028 */
3029 static int
3030frame_minheight(topfrp, next_curwin)
3031 frame_T *topfrp;
3032 win_T *next_curwin;
3033{
3034 frame_T *frp;
3035 int m;
3036#ifdef FEAT_VERTSPLIT
3037 int n;
3038#endif
3039
3040 if (topfrp->fr_win != NULL)
3041 {
3042 if (topfrp->fr_win == next_curwin)
3043 m = p_wh + topfrp->fr_win->w_status_height;
3044 else
3045 {
3046 /* window: minimal height of the window plus status line */
3047 m = p_wmh + topfrp->fr_win->w_status_height;
3048 /* Current window is minimal one line high */
3049 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3050 ++m;
3051 }
3052 }
3053#ifdef FEAT_VERTSPLIT
3054 else if (topfrp->fr_layout == FR_ROW)
3055 {
3056 /* get the minimal height from each frame in this row */
3057 m = 0;
3058 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3059 {
3060 n = frame_minheight(frp, next_curwin);
3061 if (n > m)
3062 m = n;
3063 }
3064 }
3065#endif
3066 else
3067 {
3068 /* Add up the minimal heights for all frames in this column. */
3069 m = 0;
3070 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3071 m += frame_minheight(frp, next_curwin);
3072 }
3073
3074 return m;
3075}
3076
3077#ifdef FEAT_VERTSPLIT
3078/*
3079 * Compute the minimal width for frame "topfrp".
3080 * When "next_curwin" isn't NULL, use p_wiw for this window.
3081 * When "next_curwin" is NOWIN, don't use at least one column for the current
3082 * window.
3083 */
3084 static int
3085frame_minwidth(topfrp, next_curwin)
3086 frame_T *topfrp;
3087 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3088{
3089 frame_T *frp;
3090 int m, n;
3091
3092 if (topfrp->fr_win != NULL)
3093 {
3094 if (topfrp->fr_win == next_curwin)
3095 m = p_wiw + topfrp->fr_win->w_vsep_width;
3096 else
3097 {
3098 /* window: minimal width of the window plus separator column */
3099 m = p_wmw + topfrp->fr_win->w_vsep_width;
3100 /* Current window is minimal one column wide */
3101 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3102 ++m;
3103 }
3104 }
3105 else if (topfrp->fr_layout == FR_COL)
3106 {
3107 /* get the minimal width from each frame in this column */
3108 m = 0;
3109 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3110 {
3111 n = frame_minwidth(frp, next_curwin);
3112 if (n > m)
3113 m = n;
3114 }
3115 }
3116 else
3117 {
3118 /* Add up the minimal widths for all frames in this row. */
3119 m = 0;
3120 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3121 m += frame_minwidth(frp, next_curwin);
3122 }
3123
3124 return m;
3125}
3126#endif
3127
3128
3129/*
3130 * Try to close all windows except current one.
3131 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3132 * used and the buffer was modified.
3133 *
3134 * Used by ":bdel" and ":only".
3135 */
3136 void
3137close_others(message, forceit)
3138 int message;
3139 int forceit; /* always hide all other windows */
3140{
3141 win_T *wp;
3142 win_T *nextwp;
3143 int r;
3144
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003145 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 {
3147 if (message
3148#ifdef FEAT_AUTOCMD
3149 && !autocmd_busy
3150#endif
3151 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003152 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 return;
3154 }
3155
3156 /* Be very careful here: autocommands may change the window layout. */
3157 for (wp = firstwin; win_valid(wp); wp = nextwp)
3158 {
3159 nextwp = wp->w_next;
3160 if (wp != curwin) /* don't close current window */
3161 {
3162
3163 /* Check if it's allowed to abandon this window */
3164 r = can_abandon(wp->w_buffer, forceit);
3165#ifdef FEAT_AUTOCMD
3166 if (!win_valid(wp)) /* autocommands messed wp up */
3167 {
3168 nextwp = firstwin;
3169 continue;
3170 }
3171#endif
3172 if (!r)
3173 {
3174#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3175 if (message && (p_confirm || cmdmod.confirm) && p_write)
3176 {
3177 dialog_changed(wp->w_buffer, FALSE);
3178# ifdef FEAT_AUTOCMD
3179 if (!win_valid(wp)) /* autocommands messed wp up */
3180 {
3181 nextwp = firstwin;
3182 continue;
3183 }
3184# endif
3185 }
3186 if (bufIsChanged(wp->w_buffer))
3187#endif
3188 continue;
3189 }
3190 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3191 }
3192 }
3193
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003194 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 EMSG(_("E445: Other window contains changes"));
3196}
3197
3198#endif /* FEAT_WINDOWS */
3199
3200/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003201 * Init the current window "curwin".
3202 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 */
3204 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003205curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003207 win_init_empty(curwin);
3208}
3209
3210 void
3211win_init_empty(wp)
3212 win_T *wp;
3213{
3214 redraw_win_later(wp, NOT_VALID);
3215 wp->w_lines_valid = 0;
3216 wp->w_cursor.lnum = 1;
3217 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003219 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003221 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3222 wp->w_pcmark.col = 0;
3223 wp->w_prev_pcmark.lnum = 0;
3224 wp->w_prev_pcmark.col = 0;
3225 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003227 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003229 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003231 if (wp->w_p_rl)
3232 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003234 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235#endif
3236}
3237
3238/*
3239 * Allocate the first window and put an empty buffer in it.
3240 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003241 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003243 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244win_alloc_first()
3245{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003246 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003247 return FAIL;
3248
3249#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003250 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003251 if (first_tabpage == NULL)
3252 return FAIL;
3253 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003254 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003255#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003256
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003257 return OK;
3258}
3259
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003260#if defined(FEAT_AUTOCMD) || defined(PROTO)
3261/*
3262 * Init "aucmd_win". This can only be done after the first
3263 * window is fully initialized, thus it can't be in win_alloc_first().
3264 */
3265 void
3266win_alloc_aucmd_win()
3267{
3268 aucmd_win = win_alloc(NULL, TRUE);
3269 if (aucmd_win != NULL)
3270 {
3271 win_init_some(aucmd_win, curwin);
3272# ifdef FEAT_SCROLLBIND
3273 aucmd_win->w_p_scb = FALSE;
3274# endif
3275 new_frame(aucmd_win);
3276 }
3277}
3278#endif
3279
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003280/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003281 * Allocate the first window or the first window in a new tab page.
3282 * When "oldwin" is NULL create an empty buffer for it.
3283 * When "oldwin" is not NULL copy info from it to the new window (only with
3284 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003285 * Return FAIL when something goes wrong (out of memory).
3286 */
3287 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003288win_alloc_firstwin(oldwin)
3289 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003290{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003291 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003292 if (oldwin == NULL)
3293 {
3294 /* Very first window, need to create an empty buffer for it and
3295 * initialize from scratch. */
3296 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3297 if (curwin == NULL || curbuf == NULL)
3298 return FAIL;
3299 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003300#ifdef FEAT_SYN_HL
3301 curwin->w_s = &(curbuf->b_s);
3302#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003303 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003305 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003307 curwin_init(); /* init current window */
3308 }
3309#ifdef FEAT_WINDOWS
3310 else
3311 {
3312 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003313 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003314
3315# ifdef FEAT_SCROLLBIND
3316 /* We don't want scroll-binding in the first window. */
3317 curwin->w_p_scb = FALSE;
3318# endif
3319 }
3320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003322 new_frame(curwin);
3323 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003324 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003325 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326#ifdef FEAT_VERTSPLIT
3327 topframe->fr_width = Columns;
3328#endif
3329 topframe->fr_height = Rows - p_ch;
3330 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003331
3332 return OK;
3333}
3334
3335/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003336 * Create a frame for window "wp".
3337 */
3338 static void
3339new_frame(win_T *wp)
3340{
3341 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3342
3343 wp->w_frame = frp;
3344 if (frp != NULL)
3345 {
3346 frp->fr_layout = FR_LEAF;
3347 frp->fr_win = wp;
3348 }
3349}
3350
3351/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003352 * Initialize the window and frame size to the maximum.
3353 */
3354 void
3355win_init_size()
3356{
3357 firstwin->w_height = ROWS_AVAIL;
3358 topframe->fr_height = ROWS_AVAIL;
3359#ifdef FEAT_VERTSPLIT
3360 firstwin->w_width = Columns;
3361 topframe->fr_width = Columns;
3362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363}
3364
3365#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003366
3367/*
3368 * Allocate a new tabpage_T and init the values.
3369 * Returns NULL when out of memory.
3370 */
3371 static tabpage_T *
3372alloc_tabpage()
3373{
3374 tabpage_T *tp;
3375
3376 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3377 if (tp != NULL)
3378 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003379# ifdef FEAT_GUI
3380 int i;
3381
3382 for (i = 0; i < 3; i++)
3383 tp->tp_prev_which_scrollbars[i] = -1;
3384# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003385# ifdef FEAT_DIFF
3386 tp->tp_diff_invalid = TRUE;
3387# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003388#ifdef FEAT_EVAL
3389 /* init t: variables */
3390 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3391#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003392 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003393 }
3394 return tp;
3395}
3396
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003397 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003398free_tabpage(tp)
3399 tabpage_T *tp;
3400{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003401 int idx;
3402
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003403# ifdef FEAT_DIFF
3404 diff_clear(tp);
3405# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003406 for (idx = 0; idx < SNAP_COUNT; ++idx)
3407 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003408#ifdef FEAT_EVAL
3409 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3410#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003411 vim_free(tp);
3412}
3413
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003414/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003415 * Create a new Tab page with one window.
3416 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003417 * When "after" is 0 put it just after the current Tab page.
3418 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003419 * Return FAIL or OK.
3420 */
3421 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003422win_new_tabpage(after)
3423 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003424{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003425 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003426 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003427 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003428
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003429 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003430 if (newtp == NULL)
3431 return FAIL;
3432
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003433 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003434 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003435 {
3436 vim_free(newtp);
3437 return FAIL;
3438 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003439 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003440
3441 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003442 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003443 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003444 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003445 if (after == 1)
3446 {
3447 /* New tab page becomes the first one. */
3448 newtp->tp_next = first_tabpage;
3449 first_tabpage = newtp;
3450 }
3451 else
3452 {
3453 if (after > 0)
3454 {
3455 /* Put new tab page before tab page "after". */
3456 n = 2;
3457 for (tp = first_tabpage; tp->tp_next != NULL
3458 && n < after; tp = tp->tp_next)
3459 ++n;
3460 }
3461 newtp->tp_next = tp->tp_next;
3462 tp->tp_next = newtp;
3463 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003464 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003465 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003466 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003467
3468 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003469 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003470
3471#if defined(FEAT_GUI)
3472 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3473 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003474 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003475#endif
3476
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003477 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003478#ifdef FEAT_AUTOCMD
3479 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3480 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3481#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003482 return OK;
3483 }
3484
3485 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003486 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003487 return FAIL;
3488}
3489
3490/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003491 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3492 * like with ":split".
3493 * Returns OK if a new tab page was created, FAIL otherwise.
3494 */
3495 int
3496may_open_tabpage()
3497{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003498 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003499
Bram Moolenaard326ce82007-03-11 14:48:29 +00003500 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003501 {
3502 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003503 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003504 return win_new_tabpage(n);
3505 }
3506 return FAIL;
3507}
3508
3509/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003510 * Create up to "maxcount" tabpages with empty windows.
3511 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003512 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003513 int
3514make_tabpages(maxcount)
3515 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003516{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003517 int count = maxcount;
3518 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003519
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003520 /* Limit to 'tabpagemax' tabs. */
3521 if (count > p_tpm)
3522 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003523
3524#ifdef FEAT_AUTOCMD
3525 /*
3526 * Don't execute autocommands while creating the tab pages. Must do that
3527 * when putting the buffers in the windows.
3528 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003529 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003530#endif
3531
3532 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003533 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003534 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003535
3536#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003537 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003538#endif
3539
3540 /* return actual number of tab pages */
3541 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003542}
3543
3544/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003545 * Return TRUE when "tpc" points to a valid tab page.
3546 */
3547 int
3548valid_tabpage(tpc)
3549 tabpage_T *tpc;
3550{
3551 tabpage_T *tp;
3552
3553 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3554 if (tp == tpc)
3555 return TRUE;
3556 return FALSE;
3557}
3558
3559/*
3560 * Find tab page "n" (first one is 1). Returns NULL when not found.
3561 */
3562 tabpage_T *
3563find_tabpage(n)
3564 int n;
3565{
3566 tabpage_T *tp;
3567 int i = 1;
3568
3569 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3570 ++i;
3571 return tp;
3572}
3573
3574/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003575 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003576 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003577 */
3578 int
3579tabpage_index(ftp)
3580 tabpage_T *ftp;
3581{
3582 int i = 1;
3583 tabpage_T *tp;
3584
3585 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3586 ++i;
3587 return i;
3588}
3589
3590/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003591 * Prepare for leaving the current tab page.
3592 * When autocomands change "curtab" we don't leave the tab page and return
3593 * FAIL.
3594 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003595 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003596 static int
3597leave_tabpage(new_curbuf)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003598 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003599 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003600{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003601 tabpage_T *tp = curtab;
3602
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003603#ifdef FEAT_VISUAL
3604 reset_VIsual_and_resel(); /* stop Visual mode */
3605#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003606#ifdef FEAT_AUTOCMD
3607 if (new_curbuf != curbuf)
3608 {
3609 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3610 if (curtab != tp)
3611 return FAIL;
3612 }
3613 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3614 if (curtab != tp)
3615 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003616 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003617 if (curtab != tp)
3618 return FAIL;
3619#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003620#if defined(FEAT_GUI)
3621 /* Remove the scrollbars. They may be added back later. */
3622 if (gui.in_use)
3623 gui_remove_scrollbars();
3624#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003625 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003626 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003627 tp->tp_firstwin = firstwin;
3628 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003629 tp->tp_old_Rows = Rows;
3630 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003631 firstwin = NULL;
3632 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003633 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003634}
3635
3636/*
3637 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003638 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003639 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003640 static void
3641enter_tabpage(tp, old_curbuf)
3642 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003643 buf_T *old_curbuf UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003644{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003645 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003646 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003647
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003648 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003649 firstwin = tp->tp_firstwin;
3650 lastwin = tp->tp_lastwin;
3651 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003652
3653 /* We would like doing the TabEnter event first, but we don't have a
3654 * valid current window yet, which may break some commands.
3655 * This triggers autocommands, thus may make "tp" invalid. */
3656 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3657 prevwin = next_prevwin;
3658
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003659#ifdef FEAT_AUTOCMD
3660 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003661
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003662 if (old_curbuf != curbuf)
3663 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3664#endif
3665
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003666 last_status(FALSE); /* status line may appear or disappear */
3667 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003668 must_redraw = CLEAR; /* need to redraw everything */
3669#ifdef FEAT_DIFF
3670 diff_need_scrollbind = TRUE;
3671#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003672
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003673 /* The tabpage line may have appeared or disappeared, may need to resize
3674 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003675 * frame sizes too. Use the stored value of p_ch, so that it can be
3676 * different for each tab page. */
3677 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003678 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3679#ifdef FEAT_GUI_TABLINE
3680 && !gui_use_tabline()
3681#endif
3682 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003683 shell_new_rows();
3684#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003685 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003686 shell_new_columns(); /* update window widths */
3687#endif
3688
3689#if defined(FEAT_GUI)
3690 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3691 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003692 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003693#endif
3694
3695 redraw_all_later(CLEAR);
3696}
3697
3698/*
3699 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003700 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003701 */
3702 void
3703goto_tabpage(n)
3704 int n;
3705{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003706 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003707 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003708 int i;
3709
Bram Moolenaard68071d2006-05-02 22:08:30 +00003710 if (text_locked())
3711 {
3712 /* Not allowed when editing the command line. */
3713#ifdef FEAT_CMDWIN
3714 if (cmdwin_type != 0)
3715 EMSG(_(e_cmdwin));
3716 else
3717#endif
3718 EMSG(_(e_secure));
3719 return;
3720 }
3721
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003722 /* If there is only one it can't work. */
3723 if (first_tabpage->tp_next == NULL)
3724 {
3725 if (n > 1)
3726 beep_flush();
3727 return;
3728 }
3729
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003730 if (n == 0)
3731 {
3732 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003733 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003734 tp = first_tabpage;
3735 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003736 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003737 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003738 else if (n < 0)
3739 {
3740 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3741 * this N times. */
3742 ttp = curtab;
3743 for (i = n; i < 0; ++i)
3744 {
3745 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3746 tp = tp->tp_next)
3747 ;
3748 ttp = tp;
3749 }
3750 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003751 else if (n == 9999)
3752 {
3753 /* Go to last tab page. */
3754 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3755 ;
3756 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003757 else
3758 {
3759 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003760 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003761 if (tp == NULL)
3762 {
3763 beep_flush();
3764 return;
3765 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003766 }
3767
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003768 goto_tabpage_tp(tp);
3769
3770#ifdef FEAT_GUI_TABLINE
3771 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003772 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003773#endif
3774}
3775
3776/*
3777 * Go to tabpage "tp".
3778 * Note: doesn't update the GUI tab.
3779 */
3780 void
3781goto_tabpage_tp(tp)
3782 tabpage_T *tp;
3783{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003784 /* Don't repeat a message in another tab page. */
3785 set_keep_msg(NULL, 0);
3786
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003787 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003788 {
3789 if (valid_tabpage(tp))
3790 enter_tabpage(tp, curbuf);
3791 else
3792 enter_tabpage(curtab, curbuf);
3793 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003794}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
3796/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003797 * Enter window "wp" in tab page "tp".
3798 * Also updates the GUI tab.
3799 */
3800 void
3801goto_tabpage_win(tp, wp)
3802 tabpage_T *tp;
3803 win_T *wp;
3804{
3805 goto_tabpage_tp(tp);
3806 if (curtab == tp && win_valid(wp))
3807 {
3808 win_enter(wp, TRUE);
3809# ifdef FEAT_GUI_TABLINE
3810 if (gui_use_tabline())
3811 gui_mch_set_curtab(tabpage_index(curtab));
3812# endif
3813 }
3814}
3815
3816/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003817 * Move the current tab page to before tab page "nr".
3818 */
3819 void
3820tabpage_move(nr)
3821 int nr;
3822{
3823 int n = nr;
3824 tabpage_T *tp;
3825
3826 if (first_tabpage->tp_next == NULL)
3827 return;
3828
3829 /* Remove the current tab page from the list of tab pages. */
3830 if (curtab == first_tabpage)
3831 first_tabpage = curtab->tp_next;
3832 else
3833 {
3834 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3835 if (tp->tp_next == curtab)
3836 break;
3837 if (tp == NULL) /* "cannot happen" */
3838 return;
3839 tp->tp_next = curtab->tp_next;
3840 }
3841
3842 /* Re-insert it at the specified position. */
3843 if (n == 0)
3844 {
3845 curtab->tp_next = first_tabpage;
3846 first_tabpage = curtab;
3847 }
3848 else
3849 {
3850 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3851 --n;
3852 curtab->tp_next = tp->tp_next;
3853 tp->tp_next = curtab;
3854 }
3855
3856 /* Need to redraw the tabline. Tab page contents doesn't change. */
3857 redraw_tabline = TRUE;
3858}
3859
3860
3861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 * Go to another window.
3863 * When jumping to another buffer, stop Visual mode. Do this before
3864 * changing windows so we can yank the selection into the '*' register.
3865 * When jumping to another window on the same buffer, adjust its cursor
3866 * position to keep the same Visual area.
3867 */
3868 void
3869win_goto(wp)
3870 win_T *wp;
3871{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003872 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
3874 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003875 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 return;
3877 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003878#ifdef FEAT_AUTOCMD
3879 if (curbuf_locked())
3880 return;
3881#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003882
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883#ifdef FEAT_VISUAL
3884 if (wp->w_buffer != curbuf)
3885 reset_VIsual_and_resel();
3886 else if (VIsual_active)
3887 wp->w_cursor = curwin->w_cursor;
3888#endif
3889
3890#ifdef FEAT_GUI
3891 need_mouse_correct = TRUE;
3892#endif
3893 win_enter(wp, TRUE);
3894}
3895
3896#if defined(FEAT_PERL) || defined(PROTO)
3897/*
3898 * Find window number "winnr" (counting top to bottom).
3899 */
3900 win_T *
3901win_find_nr(winnr)
3902 int winnr;
3903{
3904 win_T *wp;
3905
3906# ifdef FEAT_WINDOWS
3907 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3908 if (--winnr == 0)
3909 break;
3910 return wp;
3911# else
3912 return curwin;
3913# endif
3914}
3915#endif
3916
3917#ifdef FEAT_VERTSPLIT
3918/*
3919 * Move to window above or below "count" times.
3920 */
3921 static void
3922win_goto_ver(up, count)
3923 int up; /* TRUE to go to win above */
3924 long count;
3925{
3926 frame_T *fr;
3927 frame_T *nfr;
3928 frame_T *foundfr;
3929
3930 foundfr = curwin->w_frame;
3931 while (count--)
3932 {
3933 /*
3934 * First go upwards in the tree of frames until we find a upwards or
3935 * downwards neighbor.
3936 */
3937 fr = foundfr;
3938 for (;;)
3939 {
3940 if (fr == topframe)
3941 goto end;
3942 if (up)
3943 nfr = fr->fr_prev;
3944 else
3945 nfr = fr->fr_next;
3946 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3947 break;
3948 fr = fr->fr_parent;
3949 }
3950
3951 /*
3952 * Now go downwards to find the bottom or top frame in it.
3953 */
3954 for (;;)
3955 {
3956 if (nfr->fr_layout == FR_LEAF)
3957 {
3958 foundfr = nfr;
3959 break;
3960 }
3961 fr = nfr->fr_child;
3962 if (nfr->fr_layout == FR_ROW)
3963 {
3964 /* Find the frame at the cursor row. */
3965 while (fr->fr_next != NULL
3966 && frame2win(fr)->w_wincol + fr->fr_width
3967 <= curwin->w_wincol + curwin->w_wcol)
3968 fr = fr->fr_next;
3969 }
3970 if (nfr->fr_layout == FR_COL && up)
3971 while (fr->fr_next != NULL)
3972 fr = fr->fr_next;
3973 nfr = fr;
3974 }
3975 }
3976end:
3977 if (foundfr != NULL)
3978 win_goto(foundfr->fr_win);
3979}
3980
3981/*
3982 * Move to left or right window.
3983 */
3984 static void
3985win_goto_hor(left, count)
3986 int left; /* TRUE to go to left win */
3987 long count;
3988{
3989 frame_T *fr;
3990 frame_T *nfr;
3991 frame_T *foundfr;
3992
3993 foundfr = curwin->w_frame;
3994 while (count--)
3995 {
3996 /*
3997 * First go upwards in the tree of frames until we find a left or
3998 * right neighbor.
3999 */
4000 fr = foundfr;
4001 for (;;)
4002 {
4003 if (fr == topframe)
4004 goto end;
4005 if (left)
4006 nfr = fr->fr_prev;
4007 else
4008 nfr = fr->fr_next;
4009 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4010 break;
4011 fr = fr->fr_parent;
4012 }
4013
4014 /*
4015 * Now go downwards to find the leftmost or rightmost frame in it.
4016 */
4017 for (;;)
4018 {
4019 if (nfr->fr_layout == FR_LEAF)
4020 {
4021 foundfr = nfr;
4022 break;
4023 }
4024 fr = nfr->fr_child;
4025 if (nfr->fr_layout == FR_COL)
4026 {
4027 /* Find the frame at the cursor row. */
4028 while (fr->fr_next != NULL
4029 && frame2win(fr)->w_winrow + fr->fr_height
4030 <= curwin->w_winrow + curwin->w_wrow)
4031 fr = fr->fr_next;
4032 }
4033 if (nfr->fr_layout == FR_ROW && left)
4034 while (fr->fr_next != NULL)
4035 fr = fr->fr_next;
4036 nfr = fr;
4037 }
4038 }
4039end:
4040 if (foundfr != NULL)
4041 win_goto(foundfr->fr_win);
4042}
4043#endif
4044
4045/*
4046 * Make window "wp" the current window.
4047 */
4048 void
4049win_enter(wp, undo_sync)
4050 win_T *wp;
4051 int undo_sync;
4052{
4053 win_enter_ext(wp, undo_sync, FALSE);
4054}
4055
4056/*
4057 * Make window wp the current window.
4058 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4059 * been closed and isn't valid.
4060 */
4061 static void
4062win_enter_ext(wp, undo_sync, curwin_invalid)
4063 win_T *wp;
4064 int undo_sync;
4065 int curwin_invalid;
4066{
4067#ifdef FEAT_AUTOCMD
4068 int other_buffer = FALSE;
4069#endif
4070
4071 if (wp == curwin && !curwin_invalid) /* nothing to do */
4072 return;
4073
4074#ifdef FEAT_AUTOCMD
4075 if (!curwin_invalid)
4076 {
4077 /*
4078 * Be careful: If autocommands delete the window, return now.
4079 */
4080 if (wp->w_buffer != curbuf)
4081 {
4082 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4083 other_buffer = TRUE;
4084 if (!win_valid(wp))
4085 return;
4086 }
4087 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4088 if (!win_valid(wp))
4089 return;
4090# ifdef FEAT_EVAL
4091 /* autocmds may abort script processing */
4092 if (aborting())
4093 return;
4094# endif
4095 }
4096#endif
4097
4098 /* sync undo before leaving the current buffer */
4099 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004100 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 /* may have to copy the buffer options when 'cpo' contains 'S' */
4102 if (wp->w_buffer != curbuf)
4103 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4104 if (!curwin_invalid)
4105 {
4106 prevwin = curwin; /* remember for CTRL-W p */
4107 curwin->w_redr_status = TRUE;
4108 }
4109 curwin = wp;
4110 curbuf = wp->w_buffer;
4111 check_cursor();
4112#ifdef FEAT_VIRTUALEDIT
4113 if (!virtual_active())
4114 curwin->w_cursor.coladd = 0;
4115#endif
4116 changed_line_abv_curs(); /* assume cursor position needs updating */
4117
4118 if (curwin->w_localdir != NULL)
4119 {
4120 /* Window has a local directory: Save current directory as global
4121 * directory (unless that was done already) and change to the local
4122 * directory. */
4123 if (globaldir == NULL)
4124 {
4125 char_u cwd[MAXPATHL];
4126
4127 if (mch_dirname(cwd, MAXPATHL) == OK)
4128 globaldir = vim_strsave(cwd);
4129 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004130 if (mch_chdir((char *)curwin->w_localdir) == 0)
4131 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
4133 else if (globaldir != NULL)
4134 {
4135 /* Window doesn't have a local directory and we are not in the global
4136 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004137 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 vim_free(globaldir);
4139 globaldir = NULL;
4140 shorten_fnames(TRUE);
4141 }
4142
4143#ifdef FEAT_AUTOCMD
4144 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4145 if (other_buffer)
4146 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4147#endif
4148
4149#ifdef FEAT_TITLE
4150 maketitle();
4151#endif
4152 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004153 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 if (restart_edit)
4155 redraw_later(VALID); /* causes status line redraw */
4156
4157 /* set window height to desired minimal value */
4158 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4159 win_setheight((int)p_wh);
4160 else if (curwin->w_height == 0)
4161 win_setheight(1);
4162
4163#ifdef FEAT_VERTSPLIT
4164 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004165 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 win_setwidth((int)p_wiw);
4167#endif
4168
4169#ifdef FEAT_MOUSE
4170 setmouse(); /* in case jumped to/from help buffer */
4171#endif
4172
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004173 /* Change directories when the 'acd' option is set. */
4174 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175}
4176
4177#endif /* FEAT_WINDOWS */
4178
4179#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4180/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004181 * Jump to the first open window that contains buffer "buf", if one exists.
4182 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 */
4184 win_T *
4185buf_jump_open_win(buf)
4186 buf_T *buf;
4187{
4188# ifdef FEAT_WINDOWS
4189 win_T *wp;
4190
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004191 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 if (wp->w_buffer == buf)
4193 break;
4194 if (wp != NULL)
4195 win_enter(wp, FALSE);
4196 return wp;
4197# else
4198 if (curwin->w_buffer == buf)
4199 return curwin;
4200 return NULL;
4201# endif
4202}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004203
4204/*
4205 * Jump to the first open window in any tab page that contains buffer "buf",
4206 * if one exists.
4207 * Returns a pointer to the window found, otherwise NULL.
4208 */
4209 win_T *
4210buf_jump_open_tab(buf)
4211 buf_T *buf;
4212{
4213# ifdef FEAT_WINDOWS
4214 win_T *wp;
4215 tabpage_T *tp;
4216
4217 /* First try the current tab page. */
4218 wp = buf_jump_open_win(buf);
4219 if (wp != NULL)
4220 return wp;
4221
4222 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4223 if (tp != curtab)
4224 {
4225 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4226 if (wp->w_buffer == buf)
4227 break;
4228 if (wp != NULL)
4229 {
4230 goto_tabpage_win(tp, wp);
4231 if (curwin != wp)
4232 wp = NULL; /* something went wrong */
4233 break;
4234 }
4235 }
4236
4237 return wp;
4238# else
4239 if (curwin->w_buffer == buf)
4240 return curwin;
4241 return NULL;
4242# endif
4243}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244#endif
4245
4246/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004247 * Allocate a window structure and link it in the window list when "hidden" is
4248 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004251win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004252 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004253 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254{
4255 win_T *newwin;
4256
4257 /*
4258 * allocate window structure and linesizes arrays
4259 */
4260 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4261 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4262 {
4263 vim_free(newwin);
4264 newwin = NULL;
4265 }
4266
4267 if (newwin != NULL)
4268 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004269#ifdef FEAT_AUTOCMD
4270 /* Don't execute autocommands while the window is not properly
4271 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4272 * event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004273 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 /*
4276 * link the window in the window list
4277 */
4278#ifdef FEAT_WINDOWS
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004279 if (!hidden)
4280 win_append(after, newwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281#endif
4282#ifdef FEAT_VERTSPLIT
4283 newwin->w_wincol = 0;
4284 newwin->w_width = Columns;
4285#endif
4286
4287 /* position the display and the cursor at the top of the file. */
4288 newwin->w_topline = 1;
4289#ifdef FEAT_DIFF
4290 newwin->w_topfill = 0;
4291#endif
4292 newwin->w_botline = 2;
4293 newwin->w_cursor.lnum = 1;
4294#ifdef FEAT_SCROLLBIND
4295 newwin->w_scbind_pos = 1;
4296#endif
4297
4298 /* We won't calculate w_fraction until resizing the window */
4299 newwin->w_fraction = 0;
4300 newwin->w_prev_fraction_row = -1;
4301
4302#ifdef FEAT_GUI
4303 if (gui.in_use)
4304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4306 SBAR_LEFT, newwin);
4307 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4308 SBAR_RIGHT, newwin);
4309 }
4310#endif
4311#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004312 /* init w: variables */
4313 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314#endif
4315#ifdef FEAT_FOLDING
4316 foldInitWin(newwin);
4317#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004318#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004319 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004320#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004321#ifdef FEAT_SEARCH_EXTRA
4322 newwin->w_match_head = NULL;
4323 newwin->w_next_match_id = 4;
4324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
4326 return newwin;
4327}
4328
4329#if defined(FEAT_WINDOWS) || defined(PROTO)
4330
4331/*
4332 * remove window 'wp' from the window list and free the structure
4333 */
4334 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004335win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004337 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338{
4339 int i;
4340
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004341#ifdef FEAT_FOLDING
4342 clearFolding(wp);
4343#endif
4344
4345 /* reduce the reference count to the argument list. */
4346 alist_unlink(wp->w_alist);
4347
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004348#ifdef FEAT_AUTOCMD
4349 /* Don't execute autocommands while the window is halfway being deleted.
4350 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004351 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004352#endif
4353
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004354#ifdef FEAT_MZSCHEME
4355 mzscheme_window_free(wp);
4356#endif
4357
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358#ifdef FEAT_PERL
4359 perl_win_free(wp);
4360#endif
4361
4362#ifdef FEAT_PYTHON
4363 python_window_free(wp);
4364#endif
4365
4366#ifdef FEAT_TCL
4367 tcl_window_free(wp);
4368#endif
4369
4370#ifdef FEAT_RUBY
4371 ruby_window_free(wp);
4372#endif
4373
4374 clear_winopt(&wp->w_onebuf_opt);
4375 clear_winopt(&wp->w_allbuf_opt);
4376
4377#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004378 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379#endif
4380
4381 if (prevwin == wp)
4382 prevwin = NULL;
4383 win_free_lsize(wp);
4384
4385 for (i = 0; i < wp->w_tagstacklen; ++i)
4386 vim_free(wp->w_tagstack[i].tagname);
4387
4388 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004389
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004391 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004393
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394#ifdef FEAT_JUMPLIST
4395 free_jumplist(wp);
4396#endif
4397
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004398#ifdef FEAT_QUICKFIX
4399 qf_free_all(wp);
4400#endif
4401
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402#ifdef FEAT_GUI
4403 if (gui.in_use)
4404 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4406 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4407 }
4408#endif /* FEAT_GUI */
4409
Bram Moolenaar860cae12010-06-05 23:22:07 +02004410#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004411 reset_synblock(wp); /* free independent synblock */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004412#endif
4413
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004414#ifdef FEAT_AUTOCMD
4415 if (wp != aucmd_win)
4416#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004417 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004419
4420#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004421 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423}
4424
4425/*
4426 * Append window "wp" in the window list after window "after".
4427 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004428 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429win_append(after, wp)
4430 win_T *after, *wp;
4431{
4432 win_T *before;
4433
4434 if (after == NULL) /* after NULL is in front of the first */
4435 before = firstwin;
4436 else
4437 before = after->w_next;
4438
4439 wp->w_next = before;
4440 wp->w_prev = after;
4441 if (after == NULL)
4442 firstwin = wp;
4443 else
4444 after->w_next = wp;
4445 if (before == NULL)
4446 lastwin = wp;
4447 else
4448 before->w_prev = wp;
4449}
4450
4451/*
4452 * Remove a window from the window list.
4453 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004454 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004455win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004457 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458{
4459 if (wp->w_prev != NULL)
4460 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004461 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004463 else
4464 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 if (wp->w_next != NULL)
4466 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004467 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004469 else
4470 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471}
4472
4473/*
4474 * Append frame "frp" in a frame list after frame "after".
4475 */
4476 static void
4477frame_append(after, frp)
4478 frame_T *after, *frp;
4479{
4480 frp->fr_next = after->fr_next;
4481 after->fr_next = frp;
4482 if (frp->fr_next != NULL)
4483 frp->fr_next->fr_prev = frp;
4484 frp->fr_prev = after;
4485}
4486
4487/*
4488 * Insert frame "frp" in a frame list before frame "before".
4489 */
4490 static void
4491frame_insert(before, frp)
4492 frame_T *before, *frp;
4493{
4494 frp->fr_next = before;
4495 frp->fr_prev = before->fr_prev;
4496 before->fr_prev = frp;
4497 if (frp->fr_prev != NULL)
4498 frp->fr_prev->fr_next = frp;
4499 else
4500 frp->fr_parent->fr_child = frp;
4501}
4502
4503/*
4504 * Remove a frame from a frame list.
4505 */
4506 static void
4507frame_remove(frp)
4508 frame_T *frp;
4509{
4510 if (frp->fr_prev != NULL)
4511 frp->fr_prev->fr_next = frp->fr_next;
4512 else
4513 frp->fr_parent->fr_child = frp->fr_next;
4514 if (frp->fr_next != NULL)
4515 frp->fr_next->fr_prev = frp->fr_prev;
4516}
4517
4518#endif /* FEAT_WINDOWS */
4519
4520/*
4521 * Allocate w_lines[] for window "wp".
4522 * Return FAIL for failure, OK for success.
4523 */
4524 int
4525win_alloc_lines(wp)
4526 win_T *wp;
4527{
4528 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004529 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 if (wp->w_lines == NULL)
4531 return FAIL;
4532 return OK;
4533}
4534
4535/*
4536 * free lsize arrays for a window
4537 */
4538 void
4539win_free_lsize(wp)
4540 win_T *wp;
4541{
4542 vim_free(wp->w_lines);
4543 wp->w_lines = NULL;
4544}
4545
4546/*
4547 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004548 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 */
4550 void
4551shell_new_rows()
4552{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004553 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
4555 if (firstwin == NULL) /* not initialized yet */
4556 return;
4557#ifdef FEAT_WINDOWS
4558 if (h < frame_minheight(topframe, NULL))
4559 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004560
4561 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 * that doesn't result in the right height, forget about that option. */
4563 frame_new_height(topframe, h, FALSE, TRUE);
4564 if (topframe->fr_height != h)
4565 frame_new_height(topframe, h, FALSE, FALSE);
4566
4567 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4568#else
4569 if (h < 1)
4570 h = 1;
4571 win_new_height(firstwin, h);
4572#endif
4573 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004574#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004575 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004576#endif
4577
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578#if 0
4579 /* Disabled: don't want making the screen smaller make a window larger. */
4580 if (p_ea)
4581 win_equal(curwin, FALSE, 'v');
4582#endif
4583}
4584
4585#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4586/*
4587 * Called from win_new_shellsize() after Columns changed.
4588 */
4589 void
4590shell_new_columns()
4591{
4592 if (firstwin == NULL) /* not initialized yet */
4593 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004594
4595 /* First try setting the widths of windows with 'winfixwidth'. If that
4596 * doesn't result in the right width, forget about that option. */
4597 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4598 if (topframe->fr_width != Columns)
4599 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4600
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4602#if 0
4603 /* Disabled: don't want making the screen smaller make a window larger. */
4604 if (p_ea)
4605 win_equal(curwin, FALSE, 'h');
4606#endif
4607}
4608#endif
4609
4610#if defined(FEAT_CMDWIN) || defined(PROTO)
4611/*
4612 * Save the size of all windows in "gap".
4613 */
4614 void
4615win_size_save(gap)
4616 garray_T *gap;
4617
4618{
4619 win_T *wp;
4620
4621 ga_init2(gap, (int)sizeof(int), 1);
4622 if (ga_grow(gap, win_count() * 2) == OK)
4623 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4624 {
4625 ((int *)gap->ga_data)[gap->ga_len++] =
4626 wp->w_width + wp->w_vsep_width;
4627 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4628 }
4629}
4630
4631/*
4632 * Restore window sizes, but only if the number of windows is still the same.
4633 * Does not free the growarray.
4634 */
4635 void
4636win_size_restore(gap)
4637 garray_T *gap;
4638{
4639 win_T *wp;
4640 int i;
4641
4642 if (win_count() * 2 == gap->ga_len)
4643 {
4644 i = 0;
4645 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4646 {
4647 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4648 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4649 }
4650 /* recompute the window positions */
4651 (void)win_comp_pos();
4652 }
4653}
4654#endif /* FEAT_CMDWIN */
4655
4656#if defined(FEAT_WINDOWS) || defined(PROTO)
4657/*
4658 * Update the position for all windows, using the width and height of the
4659 * frames.
4660 * Returns the row just after the last window.
4661 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004662 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663win_comp_pos()
4664{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004665 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 int col = 0;
4667
4668 frame_comp_pos(topframe, &row, &col);
4669 return row;
4670}
4671
4672/*
4673 * Update the position of the windows in frame "topfrp", using the width and
4674 * height of the frames.
4675 * "*row" and "*col" are the top-left position of the frame. They are updated
4676 * to the bottom-right position plus one.
4677 */
4678 static void
4679frame_comp_pos(topfrp, row, col)
4680 frame_T *topfrp;
4681 int *row;
4682 int *col;
4683{
4684 win_T *wp;
4685 frame_T *frp;
4686#ifdef FEAT_VERTSPLIT
4687 int startcol;
4688 int startrow;
4689#endif
4690
4691 wp = topfrp->fr_win;
4692 if (wp != NULL)
4693 {
4694 if (wp->w_winrow != *row
4695#ifdef FEAT_VERTSPLIT
4696 || wp->w_wincol != *col
4697#endif
4698 )
4699 {
4700 /* position changed, redraw */
4701 wp->w_winrow = *row;
4702#ifdef FEAT_VERTSPLIT
4703 wp->w_wincol = *col;
4704#endif
4705 redraw_win_later(wp, NOT_VALID);
4706 wp->w_redr_status = TRUE;
4707 }
4708 *row += wp->w_height + wp->w_status_height;
4709#ifdef FEAT_VERTSPLIT
4710 *col += wp->w_width + wp->w_vsep_width;
4711#endif
4712 }
4713 else
4714 {
4715#ifdef FEAT_VERTSPLIT
4716 startrow = *row;
4717 startcol = *col;
4718#endif
4719 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4720 {
4721#ifdef FEAT_VERTSPLIT
4722 if (topfrp->fr_layout == FR_ROW)
4723 *row = startrow; /* all frames are at the same row */
4724 else
4725 *col = startcol; /* all frames are at the same col */
4726#endif
4727 frame_comp_pos(frp, row, col);
4728 }
4729 }
4730}
4731
4732#endif /* FEAT_WINDOWS */
4733
4734/*
4735 * Set current window height and take care of repositioning other windows to
4736 * fit around it.
4737 */
4738 void
4739win_setheight(height)
4740 int height;
4741{
4742 win_setheight_win(height, curwin);
4743}
4744
4745/*
4746 * Set the window height of window "win" and take care of repositioning other
4747 * windows to fit around it.
4748 */
4749 void
4750win_setheight_win(height, win)
4751 int height;
4752 win_T *win;
4753{
4754 int row;
4755
4756 if (win == curwin)
4757 {
4758 /* Always keep current window at least one line high, even when
4759 * 'winminheight' is zero. */
4760#ifdef FEAT_WINDOWS
4761 if (height < p_wmh)
4762 height = p_wmh;
4763#endif
4764 if (height == 0)
4765 height = 1;
4766 }
4767
4768#ifdef FEAT_WINDOWS
4769 frame_setheight(win->w_frame, height + win->w_status_height);
4770
4771 /* recompute the window positions */
4772 row = win_comp_pos();
4773#else
4774 if (height > topframe->fr_height)
4775 height = topframe->fr_height;
4776 win->w_height = height;
4777 row = height;
4778#endif
4779
4780 /*
4781 * If there is extra space created between the last window and the command
4782 * line, clear it.
4783 */
4784 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4785 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4786 cmdline_row = row;
4787 msg_row = row;
4788 msg_col = 0;
4789
4790 redraw_all_later(NOT_VALID);
4791}
4792
4793#if defined(FEAT_WINDOWS) || defined(PROTO)
4794
4795/*
4796 * Set the height of a frame to "height" and take care that all frames and
4797 * windows inside it are resized. Also resize frames on the left and right if
4798 * the are in the same FR_ROW frame.
4799 *
4800 * Strategy:
4801 * If the frame is part of a FR_COL frame, try fitting the frame in that
4802 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4803 * go to containing frames to resize them and make room.
4804 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4805 * Check for the minimal height of the FR_ROW frame.
4806 * At the top level we can also use change the command line height.
4807 */
4808 static void
4809frame_setheight(curfrp, height)
4810 frame_T *curfrp;
4811 int height;
4812{
4813 int room; /* total number of lines available */
4814 int take; /* number of lines taken from other windows */
4815 int room_cmdline; /* lines available from cmdline */
4816 int run;
4817 frame_T *frp;
4818 int h;
4819 int room_reserved;
4820
4821 /* If the height already is the desired value, nothing to do. */
4822 if (curfrp->fr_height == height)
4823 return;
4824
4825 if (curfrp->fr_parent == NULL)
4826 {
4827 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004828 if (height > ROWS_AVAIL)
4829 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 if (height > 0)
4831 frame_new_height(curfrp, height, FALSE, FALSE);
4832 }
4833 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4834 {
4835 /* Row of frames: Also need to resize frames left and right of this
4836 * one. First check for the minimal height of these. */
4837 h = frame_minheight(curfrp->fr_parent, NULL);
4838 if (height < h)
4839 height = h;
4840 frame_setheight(curfrp->fr_parent, height);
4841 }
4842 else
4843 {
4844 /*
4845 * Column of frames: try to change only frames in this column.
4846 */
4847#ifdef FEAT_VERTSPLIT
4848 /*
4849 * Do this twice:
4850 * 1: compute room available, if it's not enough try resizing the
4851 * containing frame.
4852 * 2: compute the room available and adjust the height to it.
4853 * Try not to reduce the height of a window with 'winfixheight' set.
4854 */
4855 for (run = 1; run <= 2; ++run)
4856#else
4857 for (;;)
4858#endif
4859 {
4860 room = 0;
4861 room_reserved = 0;
4862 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4863 frp = frp->fr_next)
4864 {
4865 if (frp != curfrp
4866 && frp->fr_win != NULL
4867 && frp->fr_win->w_p_wfh)
4868 room_reserved += frp->fr_height;
4869 room += frp->fr_height;
4870 if (frp != curfrp)
4871 room -= frame_minheight(frp, NULL);
4872 }
4873#ifdef FEAT_VERTSPLIT
4874 if (curfrp->fr_width != Columns)
4875 room_cmdline = 0;
4876 else
4877#endif
4878 {
4879 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4880 + lastwin->w_height + lastwin->w_status_height);
4881 if (room_cmdline < 0)
4882 room_cmdline = 0;
4883 }
4884
4885 if (height <= room + room_cmdline)
4886 break;
4887#ifdef FEAT_VERTSPLIT
4888 if (run == 2 || curfrp->fr_width == Columns)
4889#endif
4890 {
4891 if (height > room + room_cmdline)
4892 height = room + room_cmdline;
4893 break;
4894 }
4895#ifdef FEAT_VERTSPLIT
4896 frame_setheight(curfrp->fr_parent, height
4897 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4898#endif
4899 /*NOTREACHED*/
4900 }
4901
4902 /*
4903 * Compute the number of lines we will take from others frames (can be
4904 * negative!).
4905 */
4906 take = height - curfrp->fr_height;
4907
4908 /* If there is not enough room, also reduce the height of a window
4909 * with 'winfixheight' set. */
4910 if (height > room + room_cmdline - room_reserved)
4911 room_reserved = room + room_cmdline - height;
4912 /* If there is only a 'winfixheight' window and making the
4913 * window smaller, need to make the other window taller. */
4914 if (take < 0 && room - curfrp->fr_height < room_reserved)
4915 room_reserved = 0;
4916
4917 if (take > 0 && room_cmdline > 0)
4918 {
4919 /* use lines from cmdline first */
4920 if (take < room_cmdline)
4921 room_cmdline = take;
4922 take -= room_cmdline;
4923 topframe->fr_height += room_cmdline;
4924 }
4925
4926 /*
4927 * set the current frame to the new height
4928 */
4929 frame_new_height(curfrp, height, FALSE, FALSE);
4930
4931 /*
4932 * First take lines from the frames after the current frame. If
4933 * that is not enough, takes lines from frames above the current
4934 * frame.
4935 */
4936 for (run = 0; run < 2; ++run)
4937 {
4938 if (run == 0)
4939 frp = curfrp->fr_next; /* 1st run: start with next window */
4940 else
4941 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4942 while (frp != NULL && take != 0)
4943 {
4944 h = frame_minheight(frp, NULL);
4945 if (room_reserved > 0
4946 && frp->fr_win != NULL
4947 && frp->fr_win->w_p_wfh)
4948 {
4949 if (room_reserved >= frp->fr_height)
4950 room_reserved -= frp->fr_height;
4951 else
4952 {
4953 if (frp->fr_height - room_reserved > take)
4954 room_reserved = frp->fr_height - take;
4955 take -= frp->fr_height - room_reserved;
4956 frame_new_height(frp, room_reserved, FALSE, FALSE);
4957 room_reserved = 0;
4958 }
4959 }
4960 else
4961 {
4962 if (frp->fr_height - take < h)
4963 {
4964 take -= frp->fr_height - h;
4965 frame_new_height(frp, h, FALSE, FALSE);
4966 }
4967 else
4968 {
4969 frame_new_height(frp, frp->fr_height - take,
4970 FALSE, FALSE);
4971 take = 0;
4972 }
4973 }
4974 if (run == 0)
4975 frp = frp->fr_next;
4976 else
4977 frp = frp->fr_prev;
4978 }
4979 }
4980 }
4981}
4982
4983#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4984/*
4985 * Set current window width and take care of repositioning other windows to
4986 * fit around it.
4987 */
4988 void
4989win_setwidth(width)
4990 int width;
4991{
4992 win_setwidth_win(width, curwin);
4993}
4994
4995 void
4996win_setwidth_win(width, wp)
4997 int width;
4998 win_T *wp;
4999{
5000 /* Always keep current window at least one column wide, even when
5001 * 'winminwidth' is zero. */
5002 if (wp == curwin)
5003 {
5004 if (width < p_wmw)
5005 width = p_wmw;
5006 if (width == 0)
5007 width = 1;
5008 }
5009
5010 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5011
5012 /* recompute the window positions */
5013 (void)win_comp_pos();
5014
5015 redraw_all_later(NOT_VALID);
5016}
5017
5018/*
5019 * Set the width of a frame to "width" and take care that all frames and
5020 * windows inside it are resized. Also resize frames above and below if the
5021 * are in the same FR_ROW frame.
5022 *
5023 * Strategy is similar to frame_setheight().
5024 */
5025 static void
5026frame_setwidth(curfrp, width)
5027 frame_T *curfrp;
5028 int width;
5029{
5030 int room; /* total number of lines available */
5031 int take; /* number of lines taken from other windows */
5032 int run;
5033 frame_T *frp;
5034 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005035 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036
5037 /* If the width already is the desired value, nothing to do. */
5038 if (curfrp->fr_width == width)
5039 return;
5040
5041 if (curfrp->fr_parent == NULL)
5042 /* topframe: can't change width */
5043 return;
5044
5045 if (curfrp->fr_parent->fr_layout == FR_COL)
5046 {
5047 /* Column of frames: Also need to resize frames above and below of
5048 * this one. First check for the minimal width of these. */
5049 w = frame_minwidth(curfrp->fr_parent, NULL);
5050 if (width < w)
5051 width = w;
5052 frame_setwidth(curfrp->fr_parent, width);
5053 }
5054 else
5055 {
5056 /*
5057 * Row of frames: try to change only frames in this row.
5058 *
5059 * Do this twice:
5060 * 1: compute room available, if it's not enough try resizing the
5061 * containing frame.
5062 * 2: compute the room available and adjust the width to it.
5063 */
5064 for (run = 1; run <= 2; ++run)
5065 {
5066 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005067 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5069 frp = frp->fr_next)
5070 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005071 if (frp != curfrp
5072 && frp->fr_win != NULL
5073 && frp->fr_win->w_p_wfw)
5074 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 room += frp->fr_width;
5076 if (frp != curfrp)
5077 room -= frame_minwidth(frp, NULL);
5078 }
5079
5080 if (width <= room)
5081 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005082 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
5084 if (width > room)
5085 width = room;
5086 break;
5087 }
5088 frame_setwidth(curfrp->fr_parent, width
5089 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5090 }
5091
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 /*
5093 * Compute the number of lines we will take from others frames (can be
5094 * negative!).
5095 */
5096 take = width - curfrp->fr_width;
5097
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005098 /* If there is not enough room, also reduce the width of a window
5099 * with 'winfixwidth' set. */
5100 if (width > room - room_reserved)
5101 room_reserved = room - width;
5102 /* If there is only a 'winfixwidth' window and making the
5103 * window smaller, need to make the other window narrower. */
5104 if (take < 0 && room - curfrp->fr_width < room_reserved)
5105 room_reserved = 0;
5106
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 /*
5108 * set the current frame to the new width
5109 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005110 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111
5112 /*
5113 * First take lines from the frames right of the current frame. If
5114 * that is not enough, takes lines from frames left of the current
5115 * frame.
5116 */
5117 for (run = 0; run < 2; ++run)
5118 {
5119 if (run == 0)
5120 frp = curfrp->fr_next; /* 1st run: start with next window */
5121 else
5122 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5123 while (frp != NULL && take != 0)
5124 {
5125 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005126 if (room_reserved > 0
5127 && frp->fr_win != NULL
5128 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005130 if (room_reserved >= frp->fr_width)
5131 room_reserved -= frp->fr_width;
5132 else
5133 {
5134 if (frp->fr_width - room_reserved > take)
5135 room_reserved = frp->fr_width - take;
5136 take -= frp->fr_width - room_reserved;
5137 frame_new_width(frp, room_reserved, FALSE, FALSE);
5138 room_reserved = 0;
5139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
5141 else
5142 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005143 if (frp->fr_width - take < w)
5144 {
5145 take -= frp->fr_width - w;
5146 frame_new_width(frp, w, FALSE, FALSE);
5147 }
5148 else
5149 {
5150 frame_new_width(frp, frp->fr_width - take,
5151 FALSE, FALSE);
5152 take = 0;
5153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 }
5155 if (run == 0)
5156 frp = frp->fr_next;
5157 else
5158 frp = frp->fr_prev;
5159 }
5160 }
5161 }
5162}
5163#endif /* FEAT_VERTSPLIT */
5164
5165/*
5166 * Check 'winminheight' for a valid value.
5167 */
5168 void
5169win_setminheight()
5170{
5171 int room;
5172 int first = TRUE;
5173 win_T *wp;
5174
5175 /* loop until there is a 'winminheight' that is possible */
5176 while (p_wmh > 0)
5177 {
5178 /* TODO: handle vertical splits */
5179 room = -p_wh;
5180 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5181 room += wp->w_height - p_wmh;
5182 if (room >= 0)
5183 break;
5184 --p_wmh;
5185 if (first)
5186 {
5187 EMSG(_(e_noroom));
5188 first = FALSE;
5189 }
5190 }
5191}
5192
5193#ifdef FEAT_MOUSE
5194
5195/*
5196 * Status line of dragwin is dragged "offset" lines down (negative is up).
5197 */
5198 void
5199win_drag_status_line(dragwin, offset)
5200 win_T *dragwin;
5201 int offset;
5202{
5203 frame_T *curfr;
5204 frame_T *fr;
5205 int room;
5206 int row;
5207 int up; /* if TRUE, drag status line up, otherwise down */
5208 int n;
5209
5210 fr = dragwin->w_frame;
5211 curfr = fr;
5212 if (fr != topframe) /* more than one window */
5213 {
5214 fr = fr->fr_parent;
5215 /* When the parent frame is not a column of frames, its parent should
5216 * be. */
5217 if (fr->fr_layout != FR_COL)
5218 {
5219 curfr = fr;
5220 if (fr != topframe) /* only a row of windows, may drag statusline */
5221 fr = fr->fr_parent;
5222 }
5223 }
5224
5225 /* If this is the last frame in a column, may want to resize the parent
5226 * frame instead (go two up to skip a row of frames). */
5227 while (curfr != topframe && curfr->fr_next == NULL)
5228 {
5229 if (fr != topframe)
5230 fr = fr->fr_parent;
5231 curfr = fr;
5232 if (fr != topframe)
5233 fr = fr->fr_parent;
5234 }
5235
5236 if (offset < 0) /* drag up */
5237 {
5238 up = TRUE;
5239 offset = -offset;
5240 /* sum up the room of the current frame and above it */
5241 if (fr == curfr)
5242 {
5243 /* only one window */
5244 room = fr->fr_height - frame_minheight(fr, NULL);
5245 }
5246 else
5247 {
5248 room = 0;
5249 for (fr = fr->fr_child; ; fr = fr->fr_next)
5250 {
5251 room += fr->fr_height - frame_minheight(fr, NULL);
5252 if (fr == curfr)
5253 break;
5254 }
5255 }
5256 fr = curfr->fr_next; /* put fr at frame that grows */
5257 }
5258 else /* drag down */
5259 {
5260 up = FALSE;
5261 /*
5262 * Only dragging the last status line can reduce p_ch.
5263 */
5264 room = Rows - cmdline_row;
5265 if (curfr->fr_next == NULL)
5266 room -= 1;
5267 else
5268 room -= p_ch;
5269 if (room < 0)
5270 room = 0;
5271 /* sum up the room of frames below of the current one */
5272 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5273 room += fr->fr_height - frame_minheight(fr, NULL);
5274 fr = curfr; /* put fr at window that grows */
5275 }
5276
5277 if (room < offset) /* Not enough room */
5278 offset = room; /* Move as far as we can */
5279 if (offset <= 0)
5280 return;
5281
5282 /*
5283 * Grow frame fr by "offset" lines.
5284 * Doesn't happen when dragging the last status line up.
5285 */
5286 if (fr != NULL)
5287 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5288
5289 if (up)
5290 fr = curfr; /* current frame gets smaller */
5291 else
5292 fr = curfr->fr_next; /* next frame gets smaller */
5293
5294 /*
5295 * Now make the other frames smaller.
5296 */
5297 while (fr != NULL && offset > 0)
5298 {
5299 n = frame_minheight(fr, NULL);
5300 if (fr->fr_height - offset <= n)
5301 {
5302 offset -= fr->fr_height - n;
5303 frame_new_height(fr, n, !up, FALSE);
5304 }
5305 else
5306 {
5307 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5308 break;
5309 }
5310 if (up)
5311 fr = fr->fr_prev;
5312 else
5313 fr = fr->fr_next;
5314 }
5315 row = win_comp_pos();
5316 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5317 cmdline_row = row;
5318 p_ch = Rows - cmdline_row;
5319 if (p_ch < 1)
5320 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005321 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005322 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 showmode();
5324}
5325
5326#ifdef FEAT_VERTSPLIT
5327/*
5328 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5329 */
5330 void
5331win_drag_vsep_line(dragwin, offset)
5332 win_T *dragwin;
5333 int offset;
5334{
5335 frame_T *curfr;
5336 frame_T *fr;
5337 int room;
5338 int left; /* if TRUE, drag separator line left, otherwise right */
5339 int n;
5340
5341 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005342 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 return;
5344 curfr = fr;
5345 fr = fr->fr_parent;
5346 /* When the parent frame is not a row of frames, its parent should be. */
5347 if (fr->fr_layout != FR_ROW)
5348 {
5349 if (fr == topframe) /* only a column of windows (cannot happen?) */
5350 return;
5351 curfr = fr;
5352 fr = fr->fr_parent;
5353 }
5354
5355 /* If this is the last frame in a row, may want to resize a parent
5356 * frame instead. */
5357 while (curfr->fr_next == NULL)
5358 {
5359 if (fr == topframe)
5360 break;
5361 curfr = fr;
5362 fr = fr->fr_parent;
5363 if (fr != topframe)
5364 {
5365 curfr = fr;
5366 fr = fr->fr_parent;
5367 }
5368 }
5369
5370 if (offset < 0) /* drag left */
5371 {
5372 left = TRUE;
5373 offset = -offset;
5374 /* sum up the room of the current frame and left of it */
5375 room = 0;
5376 for (fr = fr->fr_child; ; fr = fr->fr_next)
5377 {
5378 room += fr->fr_width - frame_minwidth(fr, NULL);
5379 if (fr == curfr)
5380 break;
5381 }
5382 fr = curfr->fr_next; /* put fr at frame that grows */
5383 }
5384 else /* drag right */
5385 {
5386 left = FALSE;
5387 /* sum up the room of frames right of the current one */
5388 room = 0;
5389 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5390 room += fr->fr_width - frame_minwidth(fr, NULL);
5391 fr = curfr; /* put fr at window that grows */
5392 }
5393
5394 if (room < offset) /* Not enough room */
5395 offset = room; /* Move as far as we can */
5396 if (offset <= 0) /* No room at all, quit. */
5397 return;
5398
5399 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005400 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401
5402 /* shrink other frames: current and at the left or at the right */
5403 if (left)
5404 fr = curfr; /* current frame gets smaller */
5405 else
5406 fr = curfr->fr_next; /* next frame gets smaller */
5407
5408 while (fr != NULL && offset > 0)
5409 {
5410 n = frame_minwidth(fr, NULL);
5411 if (fr->fr_width - offset <= n)
5412 {
5413 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005414 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 }
5416 else
5417 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005418 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 break;
5420 }
5421 if (left)
5422 fr = fr->fr_prev;
5423 else
5424 fr = fr->fr_next;
5425 }
5426 (void)win_comp_pos();
5427 redraw_all_later(NOT_VALID);
5428}
5429#endif /* FEAT_VERTSPLIT */
5430#endif /* FEAT_MOUSE */
5431
5432#endif /* FEAT_WINDOWS */
5433
5434/*
5435 * Set the height of a window.
5436 * This takes care of the things inside the window, not what happens to the
5437 * window position, the frame or to other windows.
5438 */
5439 static void
5440win_new_height(wp, height)
5441 win_T *wp;
5442 int height;
5443{
5444 linenr_T lnum;
5445 int sline, line_size;
5446#define FRACTION_MULT 16384L
5447
5448 /* Don't want a negative height. Happens when splitting a tiny window.
5449 * Will equalize heights soon to fix it. */
5450 if (height < 0)
5451 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005452 if (wp->w_height == height)
5453 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454
5455 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5456 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5457 + FRACTION_MULT / 2) / (long)wp->w_height;
5458
5459 wp->w_height = height;
5460 wp->w_skipcol = 0;
5461
5462 /* Don't change w_topline when height is zero. Don't set w_topline when
5463 * 'scrollbind' is set and this isn't the current window. */
5464 if (height > 0
5465#ifdef FEAT_SCROLLBIND
5466 && (!wp->w_p_scb || wp == curwin)
5467#endif
5468 )
5469 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005470 /*
5471 * Find a value for w_topline that shows the cursor at the same
5472 * relative position in the window as before (more or less).
5473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 lnum = wp->w_cursor.lnum;
5475 if (lnum < 1) /* can happen when starting up */
5476 lnum = 1;
5477 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5478 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5479 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005480
5481 if (sline >= 0)
5482 {
5483 /* Make sure the whole cursor line is visible, if possible. */
5484 int rows = plines_win(wp, lnum, FALSE);
5485
5486 if (sline > wp->w_height - rows)
5487 {
5488 sline = wp->w_height - rows;
5489 wp->w_wrow -= rows - line_size;
5490 }
5491 }
5492
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 if (sline < 0)
5494 {
5495 /*
5496 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005497 * Make cursor line the first line in the window. If not enough
5498 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 */
5500 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005501 if (wp->w_wrow >= wp->w_height
5502 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5503 {
5504 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5505 --wp->w_wrow;
5506 while (wp->w_wrow >= wp->w_height)
5507 {
5508 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5509 + win_col_off2(wp);
5510 --wp->w_wrow;
5511 }
5512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 }
5514 else
5515 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005516 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 {
5518#ifdef FEAT_FOLDING
5519 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5520 if (lnum == 1)
5521 {
5522 /* first line in buffer is folded */
5523 line_size = 1;
5524 --sline;
5525 break;
5526 }
5527#endif
5528 --lnum;
5529#ifdef FEAT_DIFF
5530 if (lnum == wp->w_topline)
5531 line_size = plines_win_nofill(wp, lnum, TRUE)
5532 + wp->w_topfill;
5533 else
5534#endif
5535 line_size = plines_win(wp, lnum, TRUE);
5536 sline -= line_size;
5537 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005538
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 if (sline < 0)
5540 {
5541 /*
5542 * Line we want at top would go off top of screen. Use next
5543 * line instead.
5544 */
5545#ifdef FEAT_FOLDING
5546 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5547#endif
5548 lnum++;
5549 wp->w_wrow -= line_size + sline;
5550 }
5551 else if (sline > 0)
5552 {
5553 /* First line of file reached, use that as topline. */
5554 lnum = 1;
5555 wp->w_wrow -= sline;
5556 }
5557 }
5558 set_topline(wp, lnum);
5559 }
5560
5561 if (wp == curwin)
5562 {
5563 if (p_so)
5564 update_topline();
5565 curs_columns(FALSE); /* validate w_wrow */
5566 }
5567 wp->w_prev_fraction_row = wp->w_wrow;
5568
5569 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005570 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571#ifdef FEAT_WINDOWS
5572 wp->w_redr_status = TRUE;
5573#endif
5574 invalidate_botline_win(wp);
5575}
5576
5577#ifdef FEAT_VERTSPLIT
5578/*
5579 * Set the width of a window.
5580 */
5581 static void
5582win_new_width(wp, width)
5583 win_T *wp;
5584 int width;
5585{
5586 wp->w_width = width;
5587 wp->w_lines_valid = 0;
5588 changed_line_abv_curs_win(wp);
5589 invalidate_botline_win(wp);
5590 if (wp == curwin)
5591 {
5592 update_topline();
5593 curs_columns(TRUE); /* validate w_wrow */
5594 }
5595 redraw_win_later(wp, NOT_VALID);
5596 wp->w_redr_status = TRUE;
5597}
5598#endif
5599
5600 void
5601win_comp_scroll(wp)
5602 win_T *wp;
5603{
5604 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5605 if (wp->w_p_scr == 0)
5606 wp->w_p_scr = 1;
5607}
5608
5609/*
5610 * command_height: called whenever p_ch has been changed
5611 */
5612 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005613command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614{
5615#ifdef FEAT_WINDOWS
5616 int h;
5617 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005618 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005620 /* Use the value of p_ch that we remembered. This is needed for when the
5621 * GUI starts up, we can't be sure in what order things happen. And when
5622 * p_ch was changed in another tab page. */
5623 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005624
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 /* Find bottom frame with width of screen. */
5626 frp = lastwin->w_frame;
5627# ifdef FEAT_VERTSPLIT
5628 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5629 frp = frp->fr_parent;
5630# endif
5631
5632 /* Avoid changing the height of a window with 'winfixheight' set. */
5633 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5634 && frp->fr_win->w_p_wfh)
5635 frp = frp->fr_prev;
5636
5637 if (starting != NO_SCREEN)
5638 {
5639 cmdline_row = Rows - p_ch;
5640
5641 if (p_ch > old_p_ch) /* p_ch got bigger */
5642 {
5643 while (p_ch > old_p_ch)
5644 {
5645 if (frp == NULL)
5646 {
5647 EMSG(_(e_noroom));
5648 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005649 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 cmdline_row = Rows - p_ch;
5651 break;
5652 }
5653 h = frp->fr_height - frame_minheight(frp, NULL);
5654 if (h > p_ch - old_p_ch)
5655 h = p_ch - old_p_ch;
5656 old_p_ch += h;
5657 frame_add_height(frp, -h);
5658 frp = frp->fr_prev;
5659 }
5660
5661 /* Recompute window positions. */
5662 (void)win_comp_pos();
5663
5664 /* clear the lines added to cmdline */
5665 if (full_screen)
5666 screen_fill((int)(cmdline_row), (int)Rows, 0,
5667 (int)Columns, ' ', ' ', 0);
5668 msg_row = cmdline_row;
5669 redraw_cmdline = TRUE;
5670 return;
5671 }
5672
5673 if (msg_row < cmdline_row)
5674 msg_row = cmdline_row;
5675 redraw_cmdline = TRUE;
5676 }
5677 frame_add_height(frp, (int)(old_p_ch - p_ch));
5678
5679 /* Recompute window positions. */
5680 if (frp != lastwin->w_frame)
5681 (void)win_comp_pos();
5682#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005684 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685#endif
5686}
5687
5688#if defined(FEAT_WINDOWS) || defined(PROTO)
5689/*
5690 * Resize frame "frp" to be "n" lines higher (negative for less high).
5691 * Also resize the frames it is contained in.
5692 */
5693 static void
5694frame_add_height(frp, n)
5695 frame_T *frp;
5696 int n;
5697{
5698 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5699 for (;;)
5700 {
5701 frp = frp->fr_parent;
5702 if (frp == NULL)
5703 break;
5704 frp->fr_height += n;
5705 }
5706}
5707
5708/*
5709 * Add or remove a status line for the bottom window(s), according to the
5710 * value of 'laststatus'.
5711 */
5712 void
5713last_status(morewin)
5714 int morewin; /* pretend there are two or more windows */
5715{
5716 /* Don't make a difference between horizontal or vertical split. */
5717 last_status_rec(topframe, (p_ls == 2
5718 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5719}
5720
5721 static void
5722last_status_rec(fr, statusline)
5723 frame_T *fr;
5724 int statusline;
5725{
5726 frame_T *fp;
5727 win_T *wp;
5728
5729 if (fr->fr_layout == FR_LEAF)
5730 {
5731 wp = fr->fr_win;
5732 if (wp->w_status_height != 0 && !statusline)
5733 {
5734 /* remove status line */
5735 win_new_height(wp, wp->w_height + 1);
5736 wp->w_status_height = 0;
5737 comp_col();
5738 }
5739 else if (wp->w_status_height == 0 && statusline)
5740 {
5741 /* Find a frame to take a line from. */
5742 fp = fr;
5743 while (fp->fr_height <= frame_minheight(fp, NULL))
5744 {
5745 if (fp == topframe)
5746 {
5747 EMSG(_(e_noroom));
5748 return;
5749 }
5750 /* In a column of frames: go to frame above. If already at
5751 * the top or in a row of frames: go to parent. */
5752 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5753 fp = fp->fr_prev;
5754 else
5755 fp = fp->fr_parent;
5756 }
5757 wp->w_status_height = 1;
5758 if (fp != fr)
5759 {
5760 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5761 frame_fix_height(wp);
5762 (void)win_comp_pos();
5763 }
5764 else
5765 win_new_height(wp, wp->w_height - 1);
5766 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005767 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 }
5769 }
5770#ifdef FEAT_VERTSPLIT
5771 else if (fr->fr_layout == FR_ROW)
5772 {
5773 /* vertically split windows, set status line for each one */
5774 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5775 last_status_rec(fp, statusline);
5776 }
5777#endif
5778 else
5779 {
5780 /* horizontally split window, set status line for last one */
5781 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5782 ;
5783 last_status_rec(fp, statusline);
5784 }
5785}
5786
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005787/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005788 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005789 */
5790 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005791tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005792{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005793#ifdef FEAT_GUI_TABLINE
5794 /* When the GUI has the tabline then this always returns zero. */
5795 if (gui_use_tabline())
5796 return 0;
5797#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005798 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005799 {
5800 case 0: return 0;
5801 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5802 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005803 return 1;
5804}
5805
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806#endif /* FEAT_WINDOWS */
5807
5808#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5809/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005810 * Get the file name at the cursor.
5811 * If Visual mode is active, use the selected text if it's in one line.
5812 * Returns the name in allocated memory, NULL for failure.
5813 */
5814 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005815grab_file_name(count, file_lnum)
5816 long count;
5817 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005818{
5819# ifdef FEAT_VISUAL
5820 if (VIsual_active)
5821 {
5822 int len;
5823 char_u *ptr;
5824
5825 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5826 return NULL;
5827 return find_file_name_in_path(ptr, len,
5828 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5829 }
5830# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005831 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5832 file_lnum);
5833
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005834}
5835
5836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 * Return the file name under or after the cursor.
5838 *
5839 * The 'path' option is searched if the file name is not absolute.
5840 * The string returned has been alloc'ed and should be freed by the caller.
5841 * NULL is returned if the file name or file is not found.
5842 *
5843 * options:
5844 * FNAME_MESS give error messages
5845 * FNAME_EXP expand to path
5846 * FNAME_HYP check for hypertext link
5847 * FNAME_INCL apply "includeexpr"
5848 */
5849 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005850file_name_at_cursor(options, count, file_lnum)
5851 int options;
5852 long count;
5853 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854{
5855 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005856 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5857 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858}
5859
5860/*
5861 * Return the name of the file under or after ptr[col].
5862 * Otherwise like file_name_at_cursor().
5863 */
5864 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005865file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 char_u *line;
5867 int col;
5868 int options;
5869 long count;
5870 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005871 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872{
5873 char_u *ptr;
5874 int len;
5875
5876 /*
5877 * search forward for what could be the start of a file name
5878 */
5879 ptr = line + col;
5880 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005881 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 if (*ptr == NUL) /* nothing found */
5883 {
5884 if (options & FNAME_MESS)
5885 EMSG(_("E446: No file name under cursor"));
5886 return NULL;
5887 }
5888
5889 /*
5890 * Search backward for first char of the file name.
5891 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5892 */
5893 while (ptr > line)
5894 {
5895#ifdef FEAT_MBYTE
5896 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5897 ptr -= len + 1;
5898 else
5899#endif
5900 if (vim_isfilec(ptr[-1])
5901 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5902 --ptr;
5903 else
5904 break;
5905 }
5906
5907 /*
5908 * Search forward for the last char of the file name.
5909 * Also allow "://" when ':' is not in 'isfname'.
5910 */
5911 len = 0;
5912 while (vim_isfilec(ptr[len])
5913 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5914#ifdef FEAT_MBYTE
5915 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005916 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 else
5918#endif
5919 ++len;
5920
5921 /*
5922 * If there is trailing punctuation, remove it.
5923 * But don't remove "..", could be a directory name.
5924 */
5925 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5926 && ptr[len - 2] != '.')
5927 --len;
5928
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005929 if (file_lnum != NULL)
5930 {
5931 char_u *p;
5932
5933 /* Get the number after the file name and a separator character */
5934 p = ptr + len;
5935 p = skipwhite(p);
5936 if (*p != NUL)
5937 {
5938 if (!isdigit(*p))
5939 ++p; /* skip the separator */
5940 p = skipwhite(p);
5941 if (isdigit(*p))
5942 *file_lnum = (int)getdigits(&p);
5943 }
5944 }
5945
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5947}
5948
5949# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5950static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5951
5952 static char_u *
5953eval_includeexpr(ptr, len)
5954 char_u *ptr;
5955 int len;
5956{
5957 char_u *res;
5958
5959 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005960 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005961 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 set_vim_var_string(VV_FNAME, NULL, 0);
5963 return res;
5964}
5965#endif
5966
5967/*
5968 * Return the name of the file ptr[len] in 'path'.
5969 * Otherwise like file_name_at_cursor().
5970 */
5971 char_u *
5972find_file_name_in_path(ptr, len, options, count, rel_fname)
5973 char_u *ptr;
5974 int len;
5975 int options;
5976 long count;
5977 char_u *rel_fname; /* file we are searching relative to */
5978{
5979 char_u *file_name;
5980 int c;
5981# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5982 char_u *tofree = NULL;
5983
5984 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5985 {
5986 tofree = eval_includeexpr(ptr, len);
5987 if (tofree != NULL)
5988 {
5989 ptr = tofree;
5990 len = (int)STRLEN(ptr);
5991 }
5992 }
5993# endif
5994
5995 if (options & FNAME_EXP)
5996 {
5997 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5998 TRUE, rel_fname);
5999
6000# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6001 /*
6002 * If the file could not be found in a normal way, try applying
6003 * 'includeexpr' (unless done already).
6004 */
6005 if (file_name == NULL
6006 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6007 {
6008 tofree = eval_includeexpr(ptr, len);
6009 if (tofree != NULL)
6010 {
6011 ptr = tofree;
6012 len = (int)STRLEN(ptr);
6013 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6014 TRUE, rel_fname);
6015 }
6016 }
6017# endif
6018 if (file_name == NULL && (options & FNAME_MESS))
6019 {
6020 c = ptr[len];
6021 ptr[len] = NUL;
6022 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6023 ptr[len] = c;
6024 }
6025
6026 /* Repeat finding the file "count" times. This matters when it
6027 * appears several times in the path. */
6028 while (file_name != NULL && --count > 0)
6029 {
6030 vim_free(file_name);
6031 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6032 }
6033 }
6034 else
6035 file_name = vim_strnsave(ptr, len);
6036
6037# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6038 vim_free(tofree);
6039# endif
6040
6041 return file_name;
6042}
6043#endif /* FEAT_SEARCHPATH */
6044
6045/*
6046 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6047 * Also check for ":\\", which MS Internet Explorer accepts, return
6048 * URL_BACKSLASH.
6049 */
6050 static int
6051path_is_url(p)
6052 char_u *p;
6053{
6054 if (STRNCMP(p, "://", (size_t)3) == 0)
6055 return URL_SLASH;
6056 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6057 return URL_BACKSLASH;
6058 return 0;
6059}
6060
6061/*
6062 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6063 * Return URL_BACKSLASH for "name:\\".
6064 * Return zero otherwise.
6065 */
6066 int
6067path_with_url(fname)
6068 char_u *fname;
6069{
6070 char_u *p;
6071
6072 for (p = fname; isalpha(*p); ++p)
6073 ;
6074 return path_is_url(p);
6075}
6076
6077/*
6078 * Return TRUE if "name" is a full (absolute) path name or URL.
6079 */
6080 int
6081vim_isAbsName(name)
6082 char_u *name;
6083{
6084 return (path_with_url(name) != 0 || mch_isFullName(name));
6085}
6086
6087/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006088 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 *
6090 * return FAIL for failure, OK otherwise
6091 */
6092 int
6093vim_FullName(fname, buf, len, force)
6094 char_u *fname, *buf;
6095 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006096 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097{
6098 int retval = OK;
6099 int url;
6100
6101 *buf = NUL;
6102 if (fname == NULL)
6103 return FAIL;
6104
6105 url = path_with_url(fname);
6106 if (!url)
6107 retval = mch_FullName(fname, buf, len, force);
6108 if (url || retval == FAIL)
6109 {
6110 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006111 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 }
6113#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6114 slash_adjust(buf);
6115#endif
6116 return retval;
6117}
6118
6119/*
6120 * Return the minimal number of rows that is needed on the screen to display
6121 * the current number of windows.
6122 */
6123 int
6124min_rows()
6125{
6126 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006127#ifdef FEAT_WINDOWS
6128 tabpage_T *tp;
6129 int n;
6130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131
6132 if (firstwin == NULL) /* not initialized yet */
6133 return MIN_LINES;
6134
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006136 total = 0;
6137 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6138 {
6139 n = frame_minheight(tp->tp_topframe, NULL);
6140 if (total < n)
6141 total = n;
6142 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006143 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006145 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006147 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 return total;
6149}
6150
6151/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006152 * Return TRUE if there is only one window (in the current tab page), not
6153 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006154 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 */
6156 int
6157only_one_window()
6158{
6159#ifdef FEAT_WINDOWS
6160 int count = 0;
6161 win_T *wp;
6162
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006163 /* If there is another tab page there always is another window. */
6164 if (first_tabpage->tp_next != NULL)
6165 return FALSE;
6166
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006168 if ((!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169# ifdef FEAT_QUICKFIX
6170 || wp->w_p_pvw
6171# endif
6172 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006173# ifdef FEAT_AUTOCMD
6174 && wp != aucmd_win
6175# endif
6176 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 ++count;
6178 return (count <= 1);
6179#else
6180 return TRUE;
6181#endif
6182}
6183
6184#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6185/*
6186 * Correct the cursor line number in other windows. Used after changing the
6187 * current buffer, and before applying autocommands.
6188 * When "do_curwin" is TRUE, also check current window.
6189 */
6190 void
6191check_lnums(do_curwin)
6192 int do_curwin;
6193{
6194 win_T *wp;
6195
6196#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006197 tabpage_T *tp;
6198
6199 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6201#else
6202 wp = curwin;
6203 if (do_curwin)
6204#endif
6205 {
6206 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6207 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6208 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6209 wp->w_topline = curbuf->b_ml.ml_line_count;
6210 }
6211}
6212#endif
6213
6214#if defined(FEAT_WINDOWS) || defined(PROTO)
6215
6216/*
6217 * A snapshot of the window sizes, to restore them after closing the help
6218 * window.
6219 * Only these fields are used:
6220 * fr_layout
6221 * fr_width
6222 * fr_height
6223 * fr_next
6224 * fr_child
6225 * fr_win (only valid for the old curwin, NULL otherwise)
6226 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227
6228/*
6229 * Create a snapshot of the current frame sizes.
6230 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006231 void
6232make_snapshot(idx)
6233 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006235 clear_snapshot(curtab, idx);
6236 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237}
6238
6239 static void
6240make_snapshot_rec(fr, frp)
6241 frame_T *fr;
6242 frame_T **frp;
6243{
6244 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6245 if (*frp == NULL)
6246 return;
6247 (*frp)->fr_layout = fr->fr_layout;
6248# ifdef FEAT_VERTSPLIT
6249 (*frp)->fr_width = fr->fr_width;
6250# endif
6251 (*frp)->fr_height = fr->fr_height;
6252 if (fr->fr_next != NULL)
6253 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6254 if (fr->fr_child != NULL)
6255 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6256 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6257 (*frp)->fr_win = curwin;
6258}
6259
6260/*
6261 * Remove any existing snapshot.
6262 */
6263 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006264clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006265 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006266 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006268 clear_snapshot_rec(tp->tp_snapshot[idx]);
6269 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270}
6271
6272 static void
6273clear_snapshot_rec(fr)
6274 frame_T *fr;
6275{
6276 if (fr != NULL)
6277 {
6278 clear_snapshot_rec(fr->fr_next);
6279 clear_snapshot_rec(fr->fr_child);
6280 vim_free(fr);
6281 }
6282}
6283
6284/*
6285 * Restore a previously created snapshot, if there is any.
6286 * This is only done if the screen size didn't change and the window layout is
6287 * still the same.
6288 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006289 void
6290restore_snapshot(idx, close_curwin)
6291 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 int close_curwin; /* closing current window */
6293{
6294 win_T *wp;
6295
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006296 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006298 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006300 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6301 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006303 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 win_comp_pos();
6305 if (wp != NULL && close_curwin)
6306 win_goto(wp);
6307 redraw_all_later(CLEAR);
6308 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006309 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310}
6311
6312/*
6313 * Check if frames "sn" and "fr" have the same layout, same following frames
6314 * and same children.
6315 */
6316 static int
6317check_snapshot_rec(sn, fr)
6318 frame_T *sn;
6319 frame_T *fr;
6320{
6321 if (sn->fr_layout != fr->fr_layout
6322 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6323 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6324 || (sn->fr_next != NULL
6325 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6326 || (sn->fr_child != NULL
6327 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6328 return FAIL;
6329 return OK;
6330}
6331
6332/*
6333 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6334 * following frames and children.
6335 * Returns a pointer to the old current window, or NULL.
6336 */
6337 static win_T *
6338restore_snapshot_rec(sn, fr)
6339 frame_T *sn;
6340 frame_T *fr;
6341{
6342 win_T *wp = NULL;
6343 win_T *wp2;
6344
6345 fr->fr_height = sn->fr_height;
6346# ifdef FEAT_VERTSPLIT
6347 fr->fr_width = sn->fr_width;
6348# endif
6349 if (fr->fr_layout == FR_LEAF)
6350 {
6351 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6352# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006353 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354# endif
6355 wp = sn->fr_win;
6356 }
6357 if (sn->fr_next != NULL)
6358 {
6359 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6360 if (wp2 != NULL)
6361 wp = wp2;
6362 }
6363 if (sn->fr_child != NULL)
6364 {
6365 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6366 if (wp2 != NULL)
6367 wp = wp2;
6368 }
6369 return wp;
6370}
6371
6372#endif
6373
6374#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6375/*
6376 * Return TRUE if there is any vertically split window.
6377 */
6378 int
6379win_hasvertsplit()
6380{
6381 frame_T *fr;
6382
6383 if (topframe->fr_layout == FR_ROW)
6384 return TRUE;
6385
6386 if (topframe->fr_layout == FR_COL)
6387 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6388 if (fr->fr_layout == FR_ROW)
6389 return TRUE;
6390
6391 return FALSE;
6392}
6393#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006394
6395#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6396/*
6397 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006398 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006399 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6400 * If no particular ID is desired, -1 must be specified for 'id'.
6401 * Return ID of added match, -1 on failure.
6402 */
6403 int
6404match_add(wp, grp, pat, prio, id)
6405 win_T *wp;
6406 char_u *grp;
6407 char_u *pat;
6408 int prio;
6409 int id;
6410{
6411 matchitem_T *cur;
6412 matchitem_T *prev;
6413 matchitem_T *m;
6414 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006415 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006416
6417 if (*grp == NUL || *pat == NUL)
6418 return -1;
6419 if (id < -1 || id == 0)
6420 {
6421 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6422 return -1;
6423 }
6424 if (id != -1)
6425 {
6426 cur = wp->w_match_head;
6427 while (cur != NULL)
6428 {
6429 if (cur->id == id)
6430 {
6431 EMSGN("E801: ID already taken: %ld", id);
6432 return -1;
6433 }
6434 cur = cur->next;
6435 }
6436 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006437 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006438 {
6439 EMSG2(_(e_nogroup), grp);
6440 return -1;
6441 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006442 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006443 {
6444 EMSG2(_(e_invarg2), pat);
6445 return -1;
6446 }
6447
6448 /* Find available match ID. */
6449 while (id == -1)
6450 {
6451 cur = wp->w_match_head;
6452 while (cur != NULL && cur->id != wp->w_next_match_id)
6453 cur = cur->next;
6454 if (cur == NULL)
6455 id = wp->w_next_match_id;
6456 wp->w_next_match_id++;
6457 }
6458
6459 /* Build new match. */
6460 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6461 m->id = id;
6462 m->priority = prio;
6463 m->pattern = vim_strsave(pat);
6464 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006465 m->match.regprog = regprog;
6466 m->match.rmm_ic = FALSE;
6467 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006468
6469 /* Insert new match. The match list is in ascending order with regard to
6470 * the match priorities. */
6471 cur = wp->w_match_head;
6472 prev = cur;
6473 while (cur != NULL && prio >= cur->priority)
6474 {
6475 prev = cur;
6476 cur = cur->next;
6477 }
6478 if (cur == prev)
6479 wp->w_match_head = m;
6480 else
6481 prev->next = m;
6482 m->next = cur;
6483
6484 redraw_later(SOME_VALID);
6485 return id;
6486}
6487
6488/*
6489 * Delete match with ID 'id' in the match list of window 'wp'.
6490 * Print error messages if 'perr' is TRUE.
6491 */
6492 int
6493match_delete(wp, id, perr)
6494 win_T *wp;
6495 int id;
6496 int perr;
6497{
6498 matchitem_T *cur = wp->w_match_head;
6499 matchitem_T *prev = cur;
6500
6501 if (id < 1)
6502 {
6503 if (perr == TRUE)
6504 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6505 id);
6506 return -1;
6507 }
6508 while (cur != NULL && cur->id != id)
6509 {
6510 prev = cur;
6511 cur = cur->next;
6512 }
6513 if (cur == NULL)
6514 {
6515 if (perr == TRUE)
6516 EMSGN("E803: ID not found: %ld", id);
6517 return -1;
6518 }
6519 if (cur == prev)
6520 wp->w_match_head = cur->next;
6521 else
6522 prev->next = cur->next;
6523 vim_free(cur->match.regprog);
6524 vim_free(cur->pattern);
6525 vim_free(cur);
6526 redraw_later(SOME_VALID);
6527 return 0;
6528}
6529
6530/*
6531 * Delete all matches in the match list of window 'wp'.
6532 */
6533 void
6534clear_matches(wp)
6535 win_T *wp;
6536{
6537 matchitem_T *m;
6538
6539 while (wp->w_match_head != NULL)
6540 {
6541 m = wp->w_match_head->next;
6542 vim_free(wp->w_match_head->match.regprog);
6543 vim_free(wp->w_match_head->pattern);
6544 vim_free(wp->w_match_head);
6545 wp->w_match_head = m;
6546 }
6547 redraw_later(SOME_VALID);
6548}
6549
6550/*
6551 * Get match from ID 'id' in window 'wp'.
6552 * Return NULL if match not found.
6553 */
6554 matchitem_T *
6555get_match(wp, id)
6556 win_T *wp;
6557 int id;
6558{
6559 matchitem_T *cur = wp->w_match_head;
6560
6561 while (cur != NULL && cur->id != id)
6562 cur = cur->next;
6563 return cur;
6564}
6565#endif