blob: 9071012955b40186d63fff16ac577106a53dd1ab [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;
1178 oldp->w_buffer->b_nwindows++;
1179 newp->w_cursor = oldp->w_cursor;
1180 newp->w_valid = 0;
1181 newp->w_curswant = oldp->w_curswant;
1182 newp->w_set_curswant = oldp->w_set_curswant;
1183 newp->w_topline = oldp->w_topline;
1184#ifdef FEAT_DIFF
1185 newp->w_topfill = oldp->w_topfill;
1186#endif
1187 newp->w_leftcol = oldp->w_leftcol;
1188 newp->w_pcmark = oldp->w_pcmark;
1189 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1190 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001191 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001192 newp->w_fraction = oldp->w_fraction;
1193 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1194#ifdef FEAT_JUMPLIST
1195 copy_jumplist(oldp, newp);
1196#endif
1197#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001198 if (flags & WSP_NEWLOC)
1199 {
1200 /* Don't copy the location list. */
1201 newp->w_llist = NULL;
1202 newp->w_llist_ref = NULL;
1203 }
1204 else
1205 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001206#endif
1207 if (oldp->w_localdir != NULL)
1208 newp->w_localdir = vim_strsave(oldp->w_localdir);
1209
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001210 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001211 for (i = 0; i < oldp->w_tagstacklen; i++)
1212 {
1213 newp->w_tagstack[i] = oldp->w_tagstack[i];
1214 if (newp->w_tagstack[i].tagname != NULL)
1215 newp->w_tagstack[i].tagname =
1216 vim_strsave(newp->w_tagstack[i].tagname);
1217 }
1218 newp->w_tagstackidx = oldp->w_tagstackidx;
1219 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001220# ifdef FEAT_FOLDING
1221 copyFoldingState(oldp, newp);
1222# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001223
1224 win_init_some(newp, oldp);
1225}
1226
1227/*
1228 * Initialize window "newp" from window"old".
1229 * Only the essential things are copied.
1230 */
1231 static void
1232win_init_some(newp, oldp)
1233 win_T *newp;
1234 win_T *oldp;
1235{
1236 /* Use the same argument list. */
1237 newp->w_alist = oldp->w_alist;
1238 ++newp->w_alist->al_refcount;
1239 newp->w_arg_idx = oldp->w_arg_idx;
1240
1241 /* copy options from existing window */
1242 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001243}
1244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245#endif /* FEAT_WINDOWS */
1246
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247#if defined(FEAT_WINDOWS) || defined(PROTO)
1248/*
1249 * Check if "win" is a pointer to an existing window.
1250 */
1251 int
1252win_valid(win)
1253 win_T *win;
1254{
1255 win_T *wp;
1256
1257 if (win == NULL)
1258 return FALSE;
1259 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1260 if (wp == win)
1261 return TRUE;
1262 return FALSE;
1263}
1264
1265/*
1266 * Return the number of windows.
1267 */
1268 int
1269win_count()
1270{
1271 win_T *wp;
1272 int count = 0;
1273
1274 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1275 ++count;
1276 return count;
1277}
1278
1279/*
1280 * Make "count" windows on the screen.
1281 * Return actual number of windows on the screen.
1282 * Must be called when there is just one window, filling the whole screen
1283 * (excluding the command line).
1284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 int
1286make_windows(count, vertical)
1287 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001288 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289{
1290 int maxcount;
1291 int todo;
1292
1293#ifdef FEAT_VERTSPLIT
1294 if (vertical)
1295 {
1296 /* Each windows needs at least 'winminwidth' lines and a separator
1297 * column. */
1298 maxcount = (curwin->w_width + curwin->w_vsep_width
1299 - (p_wiw - p_wmw)) / (p_wmw + 1);
1300 }
1301 else
1302#endif
1303 {
1304 /* Each window needs at least 'winminheight' lines and a status line. */
1305 maxcount = (curwin->w_height + curwin->w_status_height
1306 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1307 }
1308
1309 if (maxcount < 2)
1310 maxcount = 2;
1311 if (count > maxcount)
1312 count = maxcount;
1313
1314 /*
1315 * add status line now, otherwise first window will be too big
1316 */
1317 if (count > 1)
1318 last_status(TRUE);
1319
1320#ifdef FEAT_AUTOCMD
1321 /*
1322 * Don't execute autocommands while creating the windows. Must do that
1323 * when putting the buffers in the windows.
1324 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001325 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326#endif
1327
1328 /* todo is number of windows left to create */
1329 for (todo = count - 1; todo > 0; --todo)
1330#ifdef FEAT_VERTSPLIT
1331 if (vertical)
1332 {
1333 if (win_split(curwin->w_width - (curwin->w_width - todo)
1334 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1335 break;
1336 }
1337 else
1338#endif
1339 {
1340 if (win_split(curwin->w_height - (curwin->w_height - todo
1341 * STATUS_HEIGHT) / (todo + 1)
1342 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1343 break;
1344 }
1345
1346#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001347 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348#endif
1349
1350 /* return actual number of windows */
1351 return (count - todo);
1352}
1353
1354/*
1355 * Exchange current and next window
1356 */
1357 static void
1358win_exchange(Prenum)
1359 long Prenum;
1360{
1361 frame_T *frp;
1362 frame_T *frp2;
1363 win_T *wp;
1364 win_T *wp2;
1365 int temp;
1366
1367 if (lastwin == firstwin) /* just one window */
1368 {
1369 beep_flush();
1370 return;
1371 }
1372
1373#ifdef FEAT_GUI
1374 need_mouse_correct = TRUE;
1375#endif
1376
1377 /*
1378 * find window to exchange with
1379 */
1380 if (Prenum)
1381 {
1382 frp = curwin->w_frame->fr_parent->fr_child;
1383 while (frp != NULL && --Prenum > 0)
1384 frp = frp->fr_next;
1385 }
1386 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1387 frp = curwin->w_frame->fr_next;
1388 else /* Swap last window in row/col with previous */
1389 frp = curwin->w_frame->fr_prev;
1390
1391 /* We can only exchange a window with another window, not with a frame
1392 * containing windows. */
1393 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1394 return;
1395 wp = frp->fr_win;
1396
1397/*
1398 * 1. remove curwin from the list. Remember after which window it was in wp2
1399 * 2. insert curwin before wp in the list
1400 * if wp != wp2
1401 * 3. remove wp from the list
1402 * 4. insert wp after wp2
1403 * 5. exchange the status line height and vsep width.
1404 */
1405 wp2 = curwin->w_prev;
1406 frp2 = curwin->w_frame->fr_prev;
1407 if (wp->w_prev != curwin)
1408 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001409 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 frame_remove(curwin->w_frame);
1411 win_append(wp->w_prev, curwin);
1412 frame_insert(frp, curwin->w_frame);
1413 }
1414 if (wp != wp2)
1415 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001416 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 frame_remove(wp->w_frame);
1418 win_append(wp2, wp);
1419 if (frp2 == NULL)
1420 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1421 else
1422 frame_append(frp2, wp->w_frame);
1423 }
1424 temp = curwin->w_status_height;
1425 curwin->w_status_height = wp->w_status_height;
1426 wp->w_status_height = temp;
1427#ifdef FEAT_VERTSPLIT
1428 temp = curwin->w_vsep_width;
1429 curwin->w_vsep_width = wp->w_vsep_width;
1430 wp->w_vsep_width = temp;
1431
1432 /* If the windows are not in the same frame, exchange the sizes to avoid
1433 * messing up the window layout. Otherwise fix the frame sizes. */
1434 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1435 {
1436 temp = curwin->w_height;
1437 curwin->w_height = wp->w_height;
1438 wp->w_height = temp;
1439 temp = curwin->w_width;
1440 curwin->w_width = wp->w_width;
1441 wp->w_width = temp;
1442 }
1443 else
1444 {
1445 frame_fix_height(curwin);
1446 frame_fix_height(wp);
1447 frame_fix_width(curwin);
1448 frame_fix_width(wp);
1449 }
1450#endif
1451
1452 (void)win_comp_pos(); /* recompute window positions */
1453
1454 win_enter(wp, TRUE);
1455 redraw_later(CLEAR);
1456}
1457
1458/*
1459 * rotate windows: if upwards TRUE the second window becomes the first one
1460 * if upwards FALSE the first window becomes the second one
1461 */
1462 static void
1463win_rotate(upwards, count)
1464 int upwards;
1465 int count;
1466{
1467 win_T *wp1;
1468 win_T *wp2;
1469 frame_T *frp;
1470 int n;
1471
1472 if (firstwin == lastwin) /* nothing to do */
1473 {
1474 beep_flush();
1475 return;
1476 }
1477
1478#ifdef FEAT_GUI
1479 need_mouse_correct = TRUE;
1480#endif
1481
1482#ifdef FEAT_VERTSPLIT
1483 /* Check if all frames in this row/col have one window. */
1484 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1485 frp = frp->fr_next)
1486 if (frp->fr_win == NULL)
1487 {
1488 EMSG(_("E443: Cannot rotate when another window is split"));
1489 return;
1490 }
1491#endif
1492
1493 while (count--)
1494 {
1495 if (upwards) /* first window becomes last window */
1496 {
1497 /* remove first window/frame from the list */
1498 frp = curwin->w_frame->fr_parent->fr_child;
1499 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001500 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 frame_remove(frp);
1502
1503 /* find last frame and append removed window/frame after it */
1504 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1505 ;
1506 win_append(frp->fr_win, wp1);
1507 frame_append(frp, wp1->w_frame);
1508
1509 wp2 = frp->fr_win; /* previously last window */
1510 }
1511 else /* last window becomes first window */
1512 {
1513 /* find last window/frame in the list and remove it */
1514 for (frp = curwin->w_frame; frp->fr_next != NULL;
1515 frp = frp->fr_next)
1516 ;
1517 wp1 = frp->fr_win;
1518 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001519 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 frame_remove(frp);
1521
1522 /* append the removed window/frame before the first in the list */
1523 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1524 frame_insert(frp->fr_parent->fr_child, frp);
1525 }
1526
1527 /* exchange status height and vsep width of old and new last window */
1528 n = wp2->w_status_height;
1529 wp2->w_status_height = wp1->w_status_height;
1530 wp1->w_status_height = n;
1531 frame_fix_height(wp1);
1532 frame_fix_height(wp2);
1533#ifdef FEAT_VERTSPLIT
1534 n = wp2->w_vsep_width;
1535 wp2->w_vsep_width = wp1->w_vsep_width;
1536 wp1->w_vsep_width = n;
1537 frame_fix_width(wp1);
1538 frame_fix_width(wp2);
1539#endif
1540
1541 /* recompute w_winrow and w_wincol for all windows */
1542 (void)win_comp_pos();
1543 }
1544
1545 redraw_later(CLEAR);
1546}
1547
1548/*
1549 * Move the current window to the very top/bottom/left/right of the screen.
1550 */
1551 static void
1552win_totop(size, flags)
1553 int size;
1554 int flags;
1555{
1556 int dir;
1557 int height = curwin->w_height;
1558
1559 if (lastwin == firstwin)
1560 {
1561 beep_flush();
1562 return;
1563 }
1564
1565 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001566 (void)winframe_remove(curwin, &dir, NULL);
1567 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 last_status(FALSE); /* may need to remove last status line */
1569 (void)win_comp_pos(); /* recompute window positions */
1570
1571 /* Split a window on the desired side and put the window there. */
1572 (void)win_split_ins(size, flags, curwin, dir);
1573 if (!(flags & WSP_VERT))
1574 {
1575 win_setheight(height);
1576 if (p_ea)
1577 win_equal(curwin, TRUE, 'v');
1578 }
1579
1580#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1581 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1582 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001583 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585}
1586
1587/*
1588 * Move window "win1" to below/right of "win2" and make "win1" the current
1589 * window. Only works within the same frame!
1590 */
1591 void
1592win_move_after(win1, win2)
1593 win_T *win1, *win2;
1594{
1595 int height;
1596
1597 /* check if the arguments are reasonable */
1598 if (win1 == win2)
1599 return;
1600
1601 /* check if there is something to do */
1602 if (win2->w_next != win1)
1603 {
1604 /* may need move the status line/vertical separator of the last window
1605 * */
1606 if (win1 == lastwin)
1607 {
1608 height = win1->w_prev->w_status_height;
1609 win1->w_prev->w_status_height = win1->w_status_height;
1610 win1->w_status_height = height;
1611#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001612 if (win1->w_prev->w_vsep_width == 1)
1613 {
1614 /* Remove the vertical separator from the last-but-one window,
1615 * add it to the last window. Adjust the frame widths. */
1616 win1->w_prev->w_vsep_width = 0;
1617 win1->w_prev->w_frame->fr_width -= 1;
1618 win1->w_vsep_width = 1;
1619 win1->w_frame->fr_width += 1;
1620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621#endif
1622 }
1623 else if (win2 == lastwin)
1624 {
1625 height = win1->w_status_height;
1626 win1->w_status_height = win2->w_status_height;
1627 win2->w_status_height = height;
1628#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001629 if (win1->w_vsep_width == 1)
1630 {
1631 /* Remove the vertical separator from win1, add it to the last
1632 * window, win2. Adjust the frame widths. */
1633 win2->w_vsep_width = 1;
1634 win2->w_frame->fr_width += 1;
1635 win1->w_vsep_width = 0;
1636 win1->w_frame->fr_width -= 1;
1637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638#endif
1639 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001640 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 frame_remove(win1->w_frame);
1642 win_append(win2, win1);
1643 frame_append(win2->w_frame, win1->w_frame);
1644
1645 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1646 redraw_later(NOT_VALID);
1647 }
1648 win_enter(win1, FALSE);
1649}
1650
1651/*
1652 * Make all windows the same height.
1653 * 'next_curwin' will soon be the current window, make sure it has enough
1654 * rows.
1655 */
1656 void
1657win_equal(next_curwin, current, dir)
1658 win_T *next_curwin; /* pointer to current window to be or NULL */
1659 int current; /* do only frame with current window */
1660 int dir; /* 'v' for vertically, 'h' for horizontally,
1661 'b' for both, 0 for using p_ead */
1662{
1663 if (dir == 0)
1664#ifdef FEAT_VERTSPLIT
1665 dir = *p_ead;
1666#else
1667 dir = 'b';
1668#endif
1669 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001670 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001671 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672}
1673
1674/*
1675 * Set a frame to a new position and height, spreading the available room
1676 * equally over contained frames.
1677 * The window "next_curwin" (if not NULL) should at least get the size from
1678 * 'winheight' and 'winwidth' if possible.
1679 */
1680 static void
1681win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1682 win_T *next_curwin; /* pointer to current window to be or NULL */
1683 int current; /* do only frame with current window */
1684 frame_T *topfr; /* frame to set size off */
1685 int dir; /* 'v', 'h' or 'b', see win_equal() */
1686 int col; /* horizontal position for frame */
1687 int row; /* vertical position for frame */
1688 int width; /* new width of frame */
1689 int height; /* new height of frame */
1690{
1691 int n, m;
1692 int extra_sep = 0;
1693 int wincount, totwincount = 0;
1694 frame_T *fr;
1695 int next_curwin_size = 0;
1696 int room = 0;
1697 int new_size;
1698 int has_next_curwin = 0;
1699 int hnc;
1700
1701 if (topfr->fr_layout == FR_LEAF)
1702 {
1703 /* Set the width/height of this frame.
1704 * Redraw when size or position changes */
1705 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1706#ifdef FEAT_VERTSPLIT
1707 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1708#endif
1709 )
1710 {
1711 topfr->fr_win->w_winrow = row;
1712 frame_new_height(topfr, height, FALSE, FALSE);
1713#ifdef FEAT_VERTSPLIT
1714 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001715 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716#endif
1717 redraw_all_later(CLEAR);
1718 }
1719 }
1720#ifdef FEAT_VERTSPLIT
1721 else if (topfr->fr_layout == FR_ROW)
1722 {
1723 topfr->fr_width = width;
1724 topfr->fr_height = height;
1725
1726 if (dir != 'v') /* equalize frame widths */
1727 {
1728 /* Compute the maximum number of windows horizontally in this
1729 * frame. */
1730 n = frame_minwidth(topfr, NOWIN);
1731 /* add one for the rightmost window, it doesn't have a separator */
1732 if (col + width == Columns)
1733 extra_sep = 1;
1734 else
1735 extra_sep = 0;
1736 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001737 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001739 /*
1740 * Compute width for "next_curwin" window and room available for
1741 * other windows.
1742 * "m" is the minimal width when counting p_wiw for "next_curwin".
1743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 m = frame_minwidth(topfr, next_curwin);
1745 room = width - m;
1746 if (room < 0)
1747 {
1748 next_curwin_size = p_wiw + room;
1749 room = 0;
1750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 else
1752 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001753 next_curwin_size = -1;
1754 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1755 {
1756 /* If 'winfixwidth' set keep the window width if
1757 * possible.
1758 * Watch out for this window being the next_curwin. */
1759 if (frame_fixed_width(fr))
1760 {
1761 n = frame_minwidth(fr, NOWIN);
1762 new_size = fr->fr_width;
1763 if (frame_has_win(fr, next_curwin))
1764 {
1765 room += p_wiw - p_wmw;
1766 next_curwin_size = 0;
1767 if (new_size < p_wiw)
1768 new_size = p_wiw;
1769 }
1770 else
1771 /* These windows don't use up room. */
1772 totwincount -= (n + (fr->fr_next == NULL
1773 ? extra_sep : 0)) / (p_wmw + 1);
1774 room -= new_size - n;
1775 if (room < 0)
1776 {
1777 new_size += room;
1778 room = 0;
1779 }
1780 fr->fr_newwidth = new_size;
1781 }
1782 }
1783 if (next_curwin_size == -1)
1784 {
1785 if (!has_next_curwin)
1786 next_curwin_size = 0;
1787 else if (totwincount > 1
1788 && (room + (totwincount - 2))
1789 / (totwincount - 1) > p_wiw)
1790 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001791 /* Can make all windows wider than 'winwidth', spread
1792 * the room equally. */
1793 next_curwin_size = (room + p_wiw
1794 + (totwincount - 1) * p_wmw
1795 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001796 room -= next_curwin_size - p_wiw;
1797 }
1798 else
1799 next_curwin_size = p_wiw;
1800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001802
1803 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 --totwincount; /* don't count curwin */
1805 }
1806
1807 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1808 {
1809 n = m = 0;
1810 wincount = 1;
1811 if (fr->fr_next == NULL)
1812 /* last frame gets all that remains (avoid roundoff error) */
1813 new_size = width;
1814 else if (dir == 'v')
1815 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001816 else if (frame_fixed_width(fr))
1817 {
1818 new_size = fr->fr_newwidth;
1819 wincount = 0; /* doesn't count as a sizeable window */
1820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 else
1822 {
1823 /* Compute the maximum number of windows horiz. in "fr". */
1824 n = frame_minwidth(fr, NOWIN);
1825 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1826 / (p_wmw + 1);
1827 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001828 if (has_next_curwin)
1829 hnc = frame_has_win(fr, next_curwin);
1830 else
1831 hnc = FALSE;
1832 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001834 if (totwincount == 0)
1835 new_size = room;
1836 else
1837 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001839 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 {
1841 next_curwin_size -= p_wiw - (m - n);
1842 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001843 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001845 else
1846 room -= new_size;
1847 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
1849
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001850 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 * window, unless equalizing all frames. */
1852 if (!current || dir != 'v' || topfr->fr_parent != NULL
1853 || (new_size != fr->fr_width)
1854 || frame_has_win(fr, next_curwin))
1855 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001856 new_size, height);
1857 col += new_size;
1858 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 totwincount -= wincount;
1860 }
1861 }
1862#endif
1863 else /* topfr->fr_layout == FR_COL */
1864 {
1865#ifdef FEAT_VERTSPLIT
1866 topfr->fr_width = width;
1867#endif
1868 topfr->fr_height = height;
1869
1870 if (dir != 'h') /* equalize frame heights */
1871 {
1872 /* Compute maximum number of windows vertically in this frame. */
1873 n = frame_minheight(topfr, NOWIN);
1874 /* add one for the bottom window if it doesn't have a statusline */
1875 if (row + height == cmdline_row && p_ls == 0)
1876 extra_sep = 1;
1877 else
1878 extra_sep = 0;
1879 totwincount = (n + extra_sep) / (p_wmh + 1);
1880 has_next_curwin = frame_has_win(topfr, next_curwin);
1881
1882 /*
1883 * Compute height for "next_curwin" window and room available for
1884 * other windows.
1885 * "m" is the minimal height when counting p_wh for "next_curwin".
1886 */
1887 m = frame_minheight(topfr, next_curwin);
1888 room = height - m;
1889 if (room < 0)
1890 {
1891 /* The room is less then 'winheight', use all space for the
1892 * current window. */
1893 next_curwin_size = p_wh + room;
1894 room = 0;
1895 }
1896 else
1897 {
1898 next_curwin_size = -1;
1899 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1900 {
1901 /* If 'winfixheight' set keep the window height if
1902 * possible.
1903 * Watch out for this window being the next_curwin. */
1904 if (frame_fixed_height(fr))
1905 {
1906 n = frame_minheight(fr, NOWIN);
1907 new_size = fr->fr_height;
1908 if (frame_has_win(fr, next_curwin))
1909 {
1910 room += p_wh - p_wmh;
1911 next_curwin_size = 0;
1912 if (new_size < p_wh)
1913 new_size = p_wh;
1914 }
1915 else
1916 /* These windows don't use up room. */
1917 totwincount -= (n + (fr->fr_next == NULL
1918 ? extra_sep : 0)) / (p_wmh + 1);
1919 room -= new_size - n;
1920 if (room < 0)
1921 {
1922 new_size += room;
1923 room = 0;
1924 }
1925 fr->fr_newheight = new_size;
1926 }
1927 }
1928 if (next_curwin_size == -1)
1929 {
1930 if (!has_next_curwin)
1931 next_curwin_size = 0;
1932 else if (totwincount > 1
1933 && (room + (totwincount - 2))
1934 / (totwincount - 1) > p_wh)
1935 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001936 /* can make all windows higher than 'winheight',
1937 * spread the room equally. */
1938 next_curwin_size = (room + p_wh
1939 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 + (totwincount - 1)) / totwincount;
1941 room -= next_curwin_size - p_wh;
1942 }
1943 else
1944 next_curwin_size = p_wh;
1945 }
1946 }
1947
1948 if (has_next_curwin)
1949 --totwincount; /* don't count curwin */
1950 }
1951
1952 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1953 {
1954 n = m = 0;
1955 wincount = 1;
1956 if (fr->fr_next == NULL)
1957 /* last frame gets all that remains (avoid roundoff error) */
1958 new_size = height;
1959 else if (dir == 'h')
1960 new_size = fr->fr_height;
1961 else if (frame_fixed_height(fr))
1962 {
1963 new_size = fr->fr_newheight;
1964 wincount = 0; /* doesn't count as a sizeable window */
1965 }
1966 else
1967 {
1968 /* Compute the maximum number of windows vert. in "fr". */
1969 n = frame_minheight(fr, NOWIN);
1970 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1971 / (p_wmh + 1);
1972 m = frame_minheight(fr, next_curwin);
1973 if (has_next_curwin)
1974 hnc = frame_has_win(fr, next_curwin);
1975 else
1976 hnc = FALSE;
1977 if (hnc) /* don't count next_curwin */
1978 --wincount;
1979 if (totwincount == 0)
1980 new_size = room;
1981 else
1982 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1983 / totwincount;
1984 if (hnc) /* add next_curwin size */
1985 {
1986 next_curwin_size -= p_wh - (m - n);
1987 new_size += next_curwin_size;
1988 room -= new_size - next_curwin_size;
1989 }
1990 else
1991 room -= new_size;
1992 new_size += n;
1993 }
1994 /* Skip frame that is full width when splitting or closing a
1995 * window, unless equalizing all frames. */
1996 if (!current || dir != 'h' || topfr->fr_parent != NULL
1997 || (new_size != fr->fr_height)
1998 || frame_has_win(fr, next_curwin))
1999 win_equal_rec(next_curwin, current, fr, dir, col, row,
2000 width, new_size);
2001 row += new_size;
2002 height -= new_size;
2003 totwincount -= wincount;
2004 }
2005 }
2006}
2007
2008/*
2009 * close all windows for buffer 'buf'
2010 */
2011 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002012close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002014 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002016 win_T *wp;
2017 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002018 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019
2020 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002021
2022 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002024 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002026 win_close(wp, FALSE);
2027
2028 /* Start all over, autocommands may change the window layout. */
2029 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 }
2031 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002032 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002034
2035 /* Also check windows in other tab pages. */
2036 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2037 {
2038 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002039 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002040 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2041 if (wp->w_buffer == buf)
2042 {
2043 win_close_othertab(wp, FALSE, tp);
2044
2045 /* Start all over, the tab page may be closed and
2046 * autocommands may change the window layout. */
2047 nexttp = first_tabpage;
2048 break;
2049 }
2050 }
2051
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002053
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002054 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002055 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056}
2057
2058/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002059 * Return TRUE if the current window is the only window that exists (ignoring
2060 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002061 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002062 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002063 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002064last_window()
2065{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002066 return (one_window() && first_tabpage->tp_next == NULL);
2067}
2068
2069/*
2070 * Return TRUE if there is only one window other than "aucmd_win" in the
2071 * current tab page.
2072 */
2073 static int
2074one_window()
2075{
2076#ifdef FEAT_AUTOCMD
2077 win_T *wp;
2078 int seen_one = FALSE;
2079
2080 FOR_ALL_WINDOWS(wp)
2081 {
2082 if (wp != aucmd_win)
2083 {
2084 if (seen_one)
2085 return FALSE;
2086 seen_one = TRUE;
2087 }
2088 }
2089 return TRUE;
2090#else
2091 return firstwin == lastwin;
2092#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002093}
2094
2095/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002096 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 * If "free_buf" is TRUE related buffer may be unloaded.
2098 *
2099 * called by :quit, :close, :xit, :wq and findtag()
2100 */
2101 void
2102win_close(win, free_buf)
2103 win_T *win;
2104 int free_buf;
2105{
2106 win_T *wp;
2107#ifdef FEAT_AUTOCMD
2108 int other_buffer = FALSE;
2109#endif
2110 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 int dir;
2112 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002113 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002115 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 {
2117 EMSG(_("E444: Cannot close last window"));
2118 return;
2119 }
2120
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002121#ifdef FEAT_AUTOCMD
2122 if (win == aucmd_win)
2123 {
2124 EMSG(_("E813: Cannot close autocmd window"));
2125 return;
2126 }
2127 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2128 {
2129 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2130 return;
2131 }
2132#endif
2133
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002134 /*
2135 * When closing the last window in a tab page first go to another tab
2136 * page and then close the window and the tab page. This avoids that
2137 * curwin and curtab are not invalid while we are freeing memory, they may
2138 * be used in GUI events.
2139 */
2140 if (firstwin == lastwin)
2141 {
2142 goto_tabpage_tp(alt_tabpage());
2143 redraw_tabline = TRUE;
2144
2145 /* Safety check: Autocommands may have closed the window when jumping
2146 * to the other tab page. */
2147 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2148 {
2149 int h = tabline_height();
2150
2151 win_close_othertab(win, free_buf, prev_curtab);
2152 if (h != tabline_height())
2153 shell_new_rows();
2154 }
2155 return;
2156 }
2157
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 /* When closing the help window, try restoring a snapshot after closing
2159 * the window. Otherwise clear the snapshot, it's now invalid. */
2160 if (win->w_buffer->b_help)
2161 help_window = TRUE;
2162 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002163 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
2165#ifdef FEAT_AUTOCMD
2166 if (win == curwin)
2167 {
2168 /*
2169 * Guess which window is going to be the new current window.
2170 * This may change because of the autocommands (sigh).
2171 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002172 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173
2174 /*
2175 * Be careful: If autocommands delete the window, return now.
2176 */
2177 if (wp->w_buffer != curbuf)
2178 {
2179 other_buffer = TRUE;
2180 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002181 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 return;
2183 }
2184 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002185 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 return;
2187# ifdef FEAT_EVAL
2188 /* autocmds may abort script processing */
2189 if (aborting())
2190 return;
2191# endif
2192 }
2193#endif
2194
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002195#ifdef FEAT_GUI
2196 /* Avoid trouble with scrollbars that are going to be deleted in
2197 * win_free(). */
2198 if (gui.in_use)
2199 out_flush();
2200#endif
2201
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 /*
2203 * Close the link to the buffer.
2204 */
2205 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002206
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002208 * other window or moved to another tab page. */
2209 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 return;
2211
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002212 /* Free the memory used for the window. */
2213 wp = win_free_mem(win, &dir, NULL);
2214
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 /* Make sure curwin isn't invalid. It can cause severe trouble when
2216 * printing an error message. For win_equal() curbuf needs to be valid
2217 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002218 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
2220 curwin = wp;
2221#ifdef FEAT_QUICKFIX
2222 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2223 {
2224 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002225 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 * finding another window to go to.
2227 */
2228 for (;;)
2229 {
2230 if (wp->w_next == NULL)
2231 wp = firstwin;
2232 else
2233 wp = wp->w_next;
2234 if (wp == curwin)
2235 break;
2236 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2237 {
2238 curwin = wp;
2239 break;
2240 }
2241 }
2242 }
2243#endif
2244 curbuf = curwin->w_buffer;
2245 close_curwin = TRUE;
2246 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002247 if (p_ea
2248#ifdef FEAT_VERTSPLIT
2249 && (*p_ead == 'b' || *p_ead == dir)
2250#endif
2251 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 win_equal(curwin, TRUE,
2253#ifdef FEAT_VERTSPLIT
2254 dir
2255#else
2256 0
2257#endif
2258 );
2259 else
2260 win_comp_pos();
2261 if (close_curwin)
2262 {
2263 win_enter_ext(wp, FALSE, TRUE);
2264#ifdef FEAT_AUTOCMD
2265 if (other_buffer)
2266 /* careful: after this wp and win may be invalid! */
2267 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2268#endif
2269 }
2270
2271 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002272 * If last window has a status line now and we don't want one,
2273 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 */
2275 last_status(FALSE);
2276
2277 /* After closing the help window, try restoring the window layout from
2278 * before it was opened. */
2279 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002280 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281
2282#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2283 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2284 if (gui.in_use && !win_hasvertsplit())
2285 gui_init_which_components(NULL);
2286#endif
2287
2288 redraw_all_later(NOT_VALID);
2289}
2290
2291/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002292 * Close window "win" in tab page "tp", which is not the current tab page.
2293 * This may be the last window ih that tab page and result in closing the tab,
2294 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002295 * Caller must check if buffer is hidden and whether the tabline needs to be
2296 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002297 */
2298 void
2299win_close_othertab(win, free_buf, tp)
2300 win_T *win;
2301 int free_buf;
2302 tabpage_T *tp;
2303{
2304 win_T *wp;
2305 int dir;
2306 tabpage_T *ptp = NULL;
2307
2308 /* Close the link to the buffer. */
2309 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2310
2311 /* Careful: Autocommands may have closed the tab page or made it the
2312 * current tab page. */
2313 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2314 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002315 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002316 return;
2317
2318 /* Autocommands may have closed the window already. */
2319 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2320 ;
2321 if (wp == NULL)
2322 return;
2323
2324 /* Free the memory used for the window. */
2325 wp = win_free_mem(win, &dir, tp);
2326
2327 /* When closing the last window in a tab page remove the tab page. */
2328 if (wp == NULL)
2329 {
2330 if (tp == first_tabpage)
2331 first_tabpage = tp->tp_next;
2332 else
2333 {
2334 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2335 ptp = ptp->tp_next)
2336 ;
2337 if (ptp == NULL)
2338 {
2339 EMSG2(_(e_intern2), "win_close_othertab()");
2340 return;
2341 }
2342 ptp->tp_next = tp->tp_next;
2343 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002344 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002345 }
2346}
2347
2348/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002349 * Free the memory used for a window.
2350 * Returns a pointer to the window that got the freed up space.
2351 */
2352 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002353win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002354 win_T *win;
2355 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002356 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002357{
2358 frame_T *frp;
2359 win_T *wp;
2360
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002361 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002362 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002363 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002364 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002365 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002366
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002367 /* When deleting the current window of another tab page select a new
2368 * current window. */
2369 if (tp != NULL && win == tp->tp_curwin)
2370 tp->tp_curwin = wp;
2371
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002372 return wp;
2373}
2374
2375#if defined(EXITFREE) || defined(PROTO)
2376 void
2377win_free_all()
2378{
2379 int dummy;
2380
Bram Moolenaarf740b292006-02-16 22:11:02 +00002381# ifdef FEAT_WINDOWS
2382 while (first_tabpage->tp_next != NULL)
2383 tabpage_close(TRUE);
2384# endif
2385
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002386# ifdef FEAT_AUTOCMD
2387 if (aucmd_win != NULL)
2388 {
2389 (void)win_free_mem(aucmd_win, &dummy, NULL);
2390 aucmd_win = NULL;
2391 }
2392# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002393
2394 while (firstwin != NULL)
2395 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002396}
2397#endif
2398
2399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 * Remove a window and its frame from the tree of frames.
2401 * Returns a pointer to the window that got the freed up space.
2402 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002403 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002404winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002406 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002407 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408{
2409 frame_T *frp, *frp2, *frp3;
2410 frame_T *frp_close = win->w_frame;
2411 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
2413 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002414 * If there is only one window there is nothing to remove.
2415 */
2416 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2417 return NULL;
2418
2419 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 * Remove the window from its frame.
2421 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002422 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 wp = frame2win(frp2);
2424
2425 /* Remove this frame from the list of frames. */
2426 frame_remove(frp_close);
2427
2428#ifdef FEAT_VERTSPLIT
2429 if (frp_close->fr_parent->fr_layout == FR_COL)
2430 {
2431#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002432 /* When 'winfixheight' is set, try to find another frame in the column
2433 * (as close to the closed frame as possible) to distribute the height
2434 * to. */
2435 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2436 {
2437 frp = frp_close->fr_prev;
2438 frp3 = frp_close->fr_next;
2439 while (frp != NULL || frp3 != NULL)
2440 {
2441 if (frp != NULL)
2442 {
2443 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2444 {
2445 frp2 = frp;
2446 wp = frp->fr_win;
2447 break;
2448 }
2449 frp = frp->fr_prev;
2450 }
2451 if (frp3 != NULL)
2452 {
2453 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2454 {
2455 frp2 = frp3;
2456 wp = frp3->fr_win;
2457 break;
2458 }
2459 frp3 = frp3->fr_next;
2460 }
2461 }
2462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2464 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465#ifdef FEAT_VERTSPLIT
2466 *dirp = 'v';
2467 }
2468 else
2469 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002470 /* When 'winfixwidth' is set, try to find another frame in the column
2471 * (as close to the closed frame as possible) to distribute the width
2472 * to. */
2473 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2474 {
2475 frp = frp_close->fr_prev;
2476 frp3 = frp_close->fr_next;
2477 while (frp != NULL || frp3 != NULL)
2478 {
2479 if (frp != NULL)
2480 {
2481 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2482 {
2483 frp2 = frp;
2484 wp = frp->fr_win;
2485 break;
2486 }
2487 frp = frp->fr_prev;
2488 }
2489 if (frp3 != NULL)
2490 {
2491 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2492 {
2493 frp2 = frp3;
2494 wp = frp3->fr_win;
2495 break;
2496 }
2497 frp3 = frp3->fr_next;
2498 }
2499 }
2500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002502 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 *dirp = 'h';
2504 }
2505#endif
2506
2507 /* If rows/columns go to a window below/right its positions need to be
2508 * updated. Can only be done after the sizes have been updated. */
2509 if (frp2 == frp_close->fr_next)
2510 {
2511 int row = win->w_winrow;
2512 int col = W_WINCOL(win);
2513
2514 frame_comp_pos(frp2, &row, &col);
2515 }
2516
2517 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2518 {
2519 /* There is no other frame in this list, move its info to the parent
2520 * and remove it. */
2521 frp2->fr_parent->fr_layout = frp2->fr_layout;
2522 frp2->fr_parent->fr_child = frp2->fr_child;
2523 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2524 frp->fr_parent = frp2->fr_parent;
2525 frp2->fr_parent->fr_win = frp2->fr_win;
2526 if (frp2->fr_win != NULL)
2527 frp2->fr_win->w_frame = frp2->fr_parent;
2528 frp = frp2->fr_parent;
2529 vim_free(frp2);
2530
2531 frp2 = frp->fr_parent;
2532 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2533 {
2534 /* The frame above the parent has the same layout, have to merge
2535 * the frames into this list. */
2536 if (frp2->fr_child == frp)
2537 frp2->fr_child = frp->fr_child;
2538 frp->fr_child->fr_prev = frp->fr_prev;
2539 if (frp->fr_prev != NULL)
2540 frp->fr_prev->fr_next = frp->fr_child;
2541 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2542 {
2543 frp3->fr_parent = frp2;
2544 if (frp3->fr_next == NULL)
2545 {
2546 frp3->fr_next = frp->fr_next;
2547 if (frp->fr_next != NULL)
2548 frp->fr_next->fr_prev = frp3;
2549 break;
2550 }
2551 }
2552 vim_free(frp);
2553 }
2554 }
2555
2556 return wp;
2557}
2558
2559/*
2560 * Find out which frame is going to get the freed up space when "win" is
2561 * closed.
2562 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2563 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2564 * This makes opening a window and closing it immediately keep the same window
2565 * layout.
2566 */
2567 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002568win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002570 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
2572 frame_T *frp;
2573 int b;
2574
Bram Moolenaarf740b292006-02-16 22:11:02 +00002575 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002576 /* Last window in this tab page, will go to next tab page. */
2577 return alt_tabpage()->tp_curwin->w_frame;
2578
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 frp = win->w_frame;
2580#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002581 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 b = p_spr;
2583 else
2584#endif
2585 b = p_sb;
2586 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2587 return frp->fr_next;
2588 return frp->fr_prev;
2589}
2590
2591/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002592 * Return the tabpage that will be used if the current one is closed.
2593 */
2594 static tabpage_T *
2595alt_tabpage()
2596{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002597 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002598
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002599 /* Use the next tab page if possible. */
2600 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002601 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002602
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002603 /* Find the last but one tab page. */
2604 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2605 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002606 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002607}
2608
2609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 * Find the left-upper window in frame "frp".
2611 */
2612 static win_T *
2613frame2win(frp)
2614 frame_T *frp;
2615{
2616 while (frp->fr_win == NULL)
2617 frp = frp->fr_child;
2618 return frp->fr_win;
2619}
2620
2621/*
2622 * Return TRUE if frame "frp" contains window "wp".
2623 */
2624 static int
2625frame_has_win(frp, wp)
2626 frame_T *frp;
2627 win_T *wp;
2628{
2629 frame_T *p;
2630
2631 if (frp->fr_layout == FR_LEAF)
2632 return frp->fr_win == wp;
2633
2634 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2635 if (frame_has_win(p, wp))
2636 return TRUE;
2637 return FALSE;
2638}
2639
2640/*
2641 * Set a new height for a frame. Recursively sets the height for contained
2642 * frames and windows. Caller must take care of positions.
2643 */
2644 static void
2645frame_new_height(topfrp, height, topfirst, wfh)
2646 frame_T *topfrp;
2647 int height;
2648 int topfirst; /* resize topmost contained frame first */
2649 int wfh; /* obey 'winfixheight' when there is a choice;
2650 may cause the height not to be set */
2651{
2652 frame_T *frp;
2653 int extra_lines;
2654 int h;
2655
2656 if (topfrp->fr_win != NULL)
2657 {
2658 /* Simple case: just one window. */
2659 win_new_height(topfrp->fr_win,
2660 height - topfrp->fr_win->w_status_height);
2661 }
2662#ifdef FEAT_VERTSPLIT
2663 else if (topfrp->fr_layout == FR_ROW)
2664 {
2665 do
2666 {
2667 /* All frames in this row get the same new height. */
2668 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2669 {
2670 frame_new_height(frp, height, topfirst, wfh);
2671 if (frp->fr_height > height)
2672 {
2673 /* Could not fit the windows, make the whole row higher. */
2674 height = frp->fr_height;
2675 break;
2676 }
2677 }
2678 }
2679 while (frp != NULL);
2680 }
2681#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002682 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 {
2684 /* Complicated case: Resize a column of frames. Resize the bottom
2685 * frame first, frames above that when needed. */
2686
2687 frp = topfrp->fr_child;
2688 if (wfh)
2689 /* Advance past frames with one window with 'wfh' set. */
2690 while (frame_fixed_height(frp))
2691 {
2692 frp = frp->fr_next;
2693 if (frp == NULL)
2694 return; /* no frame without 'wfh', give up */
2695 }
2696 if (!topfirst)
2697 {
2698 /* Find the bottom frame of this column */
2699 while (frp->fr_next != NULL)
2700 frp = frp->fr_next;
2701 if (wfh)
2702 /* Advance back for frames with one window with 'wfh' set. */
2703 while (frame_fixed_height(frp))
2704 frp = frp->fr_prev;
2705 }
2706
2707 extra_lines = height - topfrp->fr_height;
2708 if (extra_lines < 0)
2709 {
2710 /* reduce height of contained frames, bottom or top frame first */
2711 while (frp != NULL)
2712 {
2713 h = frame_minheight(frp, NULL);
2714 if (frp->fr_height + extra_lines < h)
2715 {
2716 extra_lines += frp->fr_height - h;
2717 frame_new_height(frp, h, topfirst, wfh);
2718 }
2719 else
2720 {
2721 frame_new_height(frp, frp->fr_height + extra_lines,
2722 topfirst, wfh);
2723 break;
2724 }
2725 if (topfirst)
2726 {
2727 do
2728 frp = frp->fr_next;
2729 while (wfh && frp != NULL && frame_fixed_height(frp));
2730 }
2731 else
2732 {
2733 do
2734 frp = frp->fr_prev;
2735 while (wfh && frp != NULL && frame_fixed_height(frp));
2736 }
2737 /* Increase "height" if we could not reduce enough frames. */
2738 if (frp == NULL)
2739 height -= extra_lines;
2740 }
2741 }
2742 else if (extra_lines > 0)
2743 {
2744 /* increase height of bottom or top frame */
2745 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2746 }
2747 }
2748 topfrp->fr_height = height;
2749}
2750
2751/*
2752 * Return TRUE if height of frame "frp" should not be changed because of
2753 * the 'winfixheight' option.
2754 */
2755 static int
2756frame_fixed_height(frp)
2757 frame_T *frp;
2758{
2759 /* frame with one window: fixed height if 'winfixheight' set. */
2760 if (frp->fr_win != NULL)
2761 return frp->fr_win->w_p_wfh;
2762
2763 if (frp->fr_layout == FR_ROW)
2764 {
2765 /* The frame is fixed height if one of the frames in the row is fixed
2766 * height. */
2767 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2768 if (frame_fixed_height(frp))
2769 return TRUE;
2770 return FALSE;
2771 }
2772
2773 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2774 * frames in the row are fixed height. */
2775 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2776 if (!frame_fixed_height(frp))
2777 return FALSE;
2778 return TRUE;
2779}
2780
2781#ifdef FEAT_VERTSPLIT
2782/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002783 * Return TRUE if width of frame "frp" should not be changed because of
2784 * the 'winfixwidth' option.
2785 */
2786 static int
2787frame_fixed_width(frp)
2788 frame_T *frp;
2789{
2790 /* frame with one window: fixed width if 'winfixwidth' set. */
2791 if (frp->fr_win != NULL)
2792 return frp->fr_win->w_p_wfw;
2793
2794 if (frp->fr_layout == FR_COL)
2795 {
2796 /* The frame is fixed width if one of the frames in the row is fixed
2797 * width. */
2798 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2799 if (frame_fixed_width(frp))
2800 return TRUE;
2801 return FALSE;
2802 }
2803
2804 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2805 * frames in the row are fixed width. */
2806 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2807 if (!frame_fixed_width(frp))
2808 return FALSE;
2809 return TRUE;
2810}
2811
2812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 * Add a status line to windows at the bottom of "frp".
2814 * Note: Does not check if there is room!
2815 */
2816 static void
2817frame_add_statusline(frp)
2818 frame_T *frp;
2819{
2820 win_T *wp;
2821
2822 if (frp->fr_layout == FR_LEAF)
2823 {
2824 wp = frp->fr_win;
2825 if (wp->w_status_height == 0)
2826 {
2827 if (wp->w_height > 0) /* don't make it negative */
2828 --wp->w_height;
2829 wp->w_status_height = STATUS_HEIGHT;
2830 }
2831 }
2832 else if (frp->fr_layout == FR_ROW)
2833 {
2834 /* Handle all the frames in the row. */
2835 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2836 frame_add_statusline(frp);
2837 }
2838 else /* frp->fr_layout == FR_COL */
2839 {
2840 /* Only need to handle the last frame in the column. */
2841 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2842 ;
2843 frame_add_statusline(frp);
2844 }
2845}
2846
2847/*
2848 * Set width of a frame. Handles recursively going through contained frames.
2849 * May remove separator line for windows at the right side (for win_close()).
2850 */
2851 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002852frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 frame_T *topfrp;
2854 int width;
2855 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002856 int wfw; /* obey 'winfixwidth' when there is a choice;
2857 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858{
2859 frame_T *frp;
2860 int extra_cols;
2861 int w;
2862 win_T *wp;
2863
2864 if (topfrp->fr_layout == FR_LEAF)
2865 {
2866 /* Simple case: just one window. */
2867 wp = topfrp->fr_win;
2868 /* Find out if there are any windows right of this one. */
2869 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2870 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2871 break;
2872 if (frp->fr_parent == NULL)
2873 wp->w_vsep_width = 0;
2874 win_new_width(wp, width - wp->w_vsep_width);
2875 }
2876 else if (topfrp->fr_layout == FR_COL)
2877 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002878 do
2879 {
2880 /* All frames in this column get the same new width. */
2881 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2882 {
2883 frame_new_width(frp, width, leftfirst, wfw);
2884 if (frp->fr_width > width)
2885 {
2886 /* Could not fit the windows, make whole column wider. */
2887 width = frp->fr_width;
2888 break;
2889 }
2890 }
2891 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 }
2893 else /* fr_layout == FR_ROW */
2894 {
2895 /* Complicated case: Resize a row of frames. Resize the rightmost
2896 * frame first, frames left of it when needed. */
2897
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002899 if (wfw)
2900 /* Advance past frames with one window with 'wfw' set. */
2901 while (frame_fixed_width(frp))
2902 {
2903 frp = frp->fr_next;
2904 if (frp == NULL)
2905 return; /* no frame without 'wfw', give up */
2906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002908 {
2909 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 while (frp->fr_next != NULL)
2911 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002912 if (wfw)
2913 /* Advance back for frames with one window with 'wfw' set. */
2914 while (frame_fixed_width(frp))
2915 frp = frp->fr_prev;
2916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917
2918 extra_cols = width - topfrp->fr_width;
2919 if (extra_cols < 0)
2920 {
2921 /* reduce frame width, rightmost frame first */
2922 while (frp != NULL)
2923 {
2924 w = frame_minwidth(frp, NULL);
2925 if (frp->fr_width + extra_cols < w)
2926 {
2927 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002928 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 }
2930 else
2931 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002932 frame_new_width(frp, frp->fr_width + extra_cols,
2933 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 break;
2935 }
2936 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002937 {
2938 do
2939 frp = frp->fr_next;
2940 while (wfw && frp != NULL && frame_fixed_width(frp));
2941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002943 {
2944 do
2945 frp = frp->fr_prev;
2946 while (wfw && frp != NULL && frame_fixed_width(frp));
2947 }
2948 /* Increase "width" if we could not reduce enough frames. */
2949 if (frp == NULL)
2950 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 }
2952 }
2953 else if (extra_cols > 0)
2954 {
2955 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002956 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 }
2958 }
2959 topfrp->fr_width = width;
2960}
2961
2962/*
2963 * Add the vertical separator to windows at the right side of "frp".
2964 * Note: Does not check if there is room!
2965 */
2966 static void
2967frame_add_vsep(frp)
2968 frame_T *frp;
2969{
2970 win_T *wp;
2971
2972 if (frp->fr_layout == FR_LEAF)
2973 {
2974 wp = frp->fr_win;
2975 if (wp->w_vsep_width == 0)
2976 {
2977 if (wp->w_width > 0) /* don't make it negative */
2978 --wp->w_width;
2979 wp->w_vsep_width = 1;
2980 }
2981 }
2982 else if (frp->fr_layout == FR_COL)
2983 {
2984 /* Handle all the frames in the column. */
2985 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2986 frame_add_vsep(frp);
2987 }
2988 else /* frp->fr_layout == FR_ROW */
2989 {
2990 /* Only need to handle the last frame in the row. */
2991 frp = frp->fr_child;
2992 while (frp->fr_next != NULL)
2993 frp = frp->fr_next;
2994 frame_add_vsep(frp);
2995 }
2996}
2997
2998/*
2999 * Set frame width from the window it contains.
3000 */
3001 static void
3002frame_fix_width(wp)
3003 win_T *wp;
3004{
3005 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3006}
3007#endif
3008
3009/*
3010 * Set frame height from the window it contains.
3011 */
3012 static void
3013frame_fix_height(wp)
3014 win_T *wp;
3015{
3016 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3017}
3018
3019/*
3020 * Compute the minimal height for frame "topfrp".
3021 * Uses the 'winminheight' option.
3022 * When "next_curwin" isn't NULL, use p_wh for this window.
3023 * When "next_curwin" is NOWIN, don't use at least one line for the current
3024 * window.
3025 */
3026 static int
3027frame_minheight(topfrp, next_curwin)
3028 frame_T *topfrp;
3029 win_T *next_curwin;
3030{
3031 frame_T *frp;
3032 int m;
3033#ifdef FEAT_VERTSPLIT
3034 int n;
3035#endif
3036
3037 if (topfrp->fr_win != NULL)
3038 {
3039 if (topfrp->fr_win == next_curwin)
3040 m = p_wh + topfrp->fr_win->w_status_height;
3041 else
3042 {
3043 /* window: minimal height of the window plus status line */
3044 m = p_wmh + topfrp->fr_win->w_status_height;
3045 /* Current window is minimal one line high */
3046 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3047 ++m;
3048 }
3049 }
3050#ifdef FEAT_VERTSPLIT
3051 else if (topfrp->fr_layout == FR_ROW)
3052 {
3053 /* get the minimal height from each frame in this row */
3054 m = 0;
3055 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3056 {
3057 n = frame_minheight(frp, next_curwin);
3058 if (n > m)
3059 m = n;
3060 }
3061 }
3062#endif
3063 else
3064 {
3065 /* Add up the minimal heights for all frames in this column. */
3066 m = 0;
3067 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3068 m += frame_minheight(frp, next_curwin);
3069 }
3070
3071 return m;
3072}
3073
3074#ifdef FEAT_VERTSPLIT
3075/*
3076 * Compute the minimal width for frame "topfrp".
3077 * When "next_curwin" isn't NULL, use p_wiw for this window.
3078 * When "next_curwin" is NOWIN, don't use at least one column for the current
3079 * window.
3080 */
3081 static int
3082frame_minwidth(topfrp, next_curwin)
3083 frame_T *topfrp;
3084 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3085{
3086 frame_T *frp;
3087 int m, n;
3088
3089 if (topfrp->fr_win != NULL)
3090 {
3091 if (topfrp->fr_win == next_curwin)
3092 m = p_wiw + topfrp->fr_win->w_vsep_width;
3093 else
3094 {
3095 /* window: minimal width of the window plus separator column */
3096 m = p_wmw + topfrp->fr_win->w_vsep_width;
3097 /* Current window is minimal one column wide */
3098 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3099 ++m;
3100 }
3101 }
3102 else if (topfrp->fr_layout == FR_COL)
3103 {
3104 /* get the minimal width from each frame in this column */
3105 m = 0;
3106 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3107 {
3108 n = frame_minwidth(frp, next_curwin);
3109 if (n > m)
3110 m = n;
3111 }
3112 }
3113 else
3114 {
3115 /* Add up the minimal widths for all frames in this row. */
3116 m = 0;
3117 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3118 m += frame_minwidth(frp, next_curwin);
3119 }
3120
3121 return m;
3122}
3123#endif
3124
3125
3126/*
3127 * Try to close all windows except current one.
3128 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3129 * used and the buffer was modified.
3130 *
3131 * Used by ":bdel" and ":only".
3132 */
3133 void
3134close_others(message, forceit)
3135 int message;
3136 int forceit; /* always hide all other windows */
3137{
3138 win_T *wp;
3139 win_T *nextwp;
3140 int r;
3141
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003142 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 {
3144 if (message
3145#ifdef FEAT_AUTOCMD
3146 && !autocmd_busy
3147#endif
3148 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003149 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 return;
3151 }
3152
3153 /* Be very careful here: autocommands may change the window layout. */
3154 for (wp = firstwin; win_valid(wp); wp = nextwp)
3155 {
3156 nextwp = wp->w_next;
3157 if (wp != curwin) /* don't close current window */
3158 {
3159
3160 /* Check if it's allowed to abandon this window */
3161 r = can_abandon(wp->w_buffer, forceit);
3162#ifdef FEAT_AUTOCMD
3163 if (!win_valid(wp)) /* autocommands messed wp up */
3164 {
3165 nextwp = firstwin;
3166 continue;
3167 }
3168#endif
3169 if (!r)
3170 {
3171#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3172 if (message && (p_confirm || cmdmod.confirm) && p_write)
3173 {
3174 dialog_changed(wp->w_buffer, FALSE);
3175# ifdef FEAT_AUTOCMD
3176 if (!win_valid(wp)) /* autocommands messed wp up */
3177 {
3178 nextwp = firstwin;
3179 continue;
3180 }
3181# endif
3182 }
3183 if (bufIsChanged(wp->w_buffer))
3184#endif
3185 continue;
3186 }
3187 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3188 }
3189 }
3190
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003191 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 EMSG(_("E445: Other window contains changes"));
3193}
3194
3195#endif /* FEAT_WINDOWS */
3196
3197/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003198 * Init the current window "curwin".
3199 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 */
3201 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003202curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003204 win_init_empty(curwin);
3205}
3206
3207 void
3208win_init_empty(wp)
3209 win_T *wp;
3210{
3211 redraw_win_later(wp, NOT_VALID);
3212 wp->w_lines_valid = 0;
3213 wp->w_cursor.lnum = 1;
3214 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003216 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003218 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3219 wp->w_pcmark.col = 0;
3220 wp->w_prev_pcmark.lnum = 0;
3221 wp->w_prev_pcmark.col = 0;
3222 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003224 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003226 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003228 if (wp->w_p_rl)
3229 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003231 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232#endif
3233}
3234
3235/*
3236 * Allocate the first window and put an empty buffer in it.
3237 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003238 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003240 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241win_alloc_first()
3242{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003243 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003244 return FAIL;
3245
3246#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003247 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003248 if (first_tabpage == NULL)
3249 return FAIL;
3250 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003251 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003252#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003253
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003254 return OK;
3255}
3256
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003257#if defined(FEAT_AUTOCMD) || defined(PROTO)
3258/*
3259 * Init "aucmd_win". This can only be done after the first
3260 * window is fully initialized, thus it can't be in win_alloc_first().
3261 */
3262 void
3263win_alloc_aucmd_win()
3264{
3265 aucmd_win = win_alloc(NULL, TRUE);
3266 if (aucmd_win != NULL)
3267 {
3268 win_init_some(aucmd_win, curwin);
3269# ifdef FEAT_SCROLLBIND
3270 aucmd_win->w_p_scb = FALSE;
3271# endif
3272 new_frame(aucmd_win);
3273 }
3274}
3275#endif
3276
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003277/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003278 * Allocate the first window or the first window in a new tab page.
3279 * When "oldwin" is NULL create an empty buffer for it.
3280 * When "oldwin" is not NULL copy info from it to the new window (only with
3281 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003282 * Return FAIL when something goes wrong (out of memory).
3283 */
3284 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003285win_alloc_firstwin(oldwin)
3286 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003287{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003288 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003289 if (oldwin == NULL)
3290 {
3291 /* Very first window, need to create an empty buffer for it and
3292 * initialize from scratch. */
3293 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3294 if (curwin == NULL || curbuf == NULL)
3295 return FAIL;
3296 curwin->w_buffer = curbuf;
3297 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003299 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003301 curwin_init(); /* init current window */
3302 }
3303#ifdef FEAT_WINDOWS
3304 else
3305 {
3306 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003307 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003308
3309# ifdef FEAT_SCROLLBIND
3310 /* We don't want scroll-binding in the first window. */
3311 curwin->w_p_scb = FALSE;
3312# endif
3313 }
3314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003316 new_frame(curwin);
3317 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003318 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003319 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#ifdef FEAT_VERTSPLIT
3321 topframe->fr_width = Columns;
3322#endif
3323 topframe->fr_height = Rows - p_ch;
3324 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003325
3326 return OK;
3327}
3328
3329/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003330 * Create a frame for window "wp".
3331 */
3332 static void
3333new_frame(win_T *wp)
3334{
3335 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3336
3337 wp->w_frame = frp;
3338 if (frp != NULL)
3339 {
3340 frp->fr_layout = FR_LEAF;
3341 frp->fr_win = wp;
3342 }
3343}
3344
3345/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003346 * Initialize the window and frame size to the maximum.
3347 */
3348 void
3349win_init_size()
3350{
3351 firstwin->w_height = ROWS_AVAIL;
3352 topframe->fr_height = ROWS_AVAIL;
3353#ifdef FEAT_VERTSPLIT
3354 firstwin->w_width = Columns;
3355 topframe->fr_width = Columns;
3356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357}
3358
3359#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003360
3361/*
3362 * Allocate a new tabpage_T and init the values.
3363 * Returns NULL when out of memory.
3364 */
3365 static tabpage_T *
3366alloc_tabpage()
3367{
3368 tabpage_T *tp;
3369
3370 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3371 if (tp != NULL)
3372 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003373# ifdef FEAT_GUI
3374 int i;
3375
3376 for (i = 0; i < 3; i++)
3377 tp->tp_prev_which_scrollbars[i] = -1;
3378# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003379# ifdef FEAT_DIFF
3380 tp->tp_diff_invalid = TRUE;
3381# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003382#ifdef FEAT_EVAL
3383 /* init t: variables */
3384 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3385#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003386 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003387 }
3388 return tp;
3389}
3390
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003391 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003392free_tabpage(tp)
3393 tabpage_T *tp;
3394{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003395 int idx;
3396
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003397# ifdef FEAT_DIFF
3398 diff_clear(tp);
3399# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003400 for (idx = 0; idx < SNAP_COUNT; ++idx)
3401 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003402#ifdef FEAT_EVAL
3403 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3404#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003405 vim_free(tp);
3406}
3407
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003408/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003409 * Create a new Tab page with one window.
3410 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003411 * When "after" is 0 put it just after the current Tab page.
3412 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003413 * Return FAIL or OK.
3414 */
3415 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003416win_new_tabpage(after)
3417 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003418{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003419 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003420 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003421 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003422
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003423 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003424 if (newtp == NULL)
3425 return FAIL;
3426
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003427 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003428 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003429 {
3430 vim_free(newtp);
3431 return FAIL;
3432 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003433 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003434
3435 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003436 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003437 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003438 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003439 if (after == 1)
3440 {
3441 /* New tab page becomes the first one. */
3442 newtp->tp_next = first_tabpage;
3443 first_tabpage = newtp;
3444 }
3445 else
3446 {
3447 if (after > 0)
3448 {
3449 /* Put new tab page before tab page "after". */
3450 n = 2;
3451 for (tp = first_tabpage; tp->tp_next != NULL
3452 && n < after; tp = tp->tp_next)
3453 ++n;
3454 }
3455 newtp->tp_next = tp->tp_next;
3456 tp->tp_next = newtp;
3457 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003458 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003459 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003460 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003461
3462 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003463 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003464
3465#if defined(FEAT_GUI)
3466 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3467 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003468 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003469#endif
3470
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003471 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003472#ifdef FEAT_AUTOCMD
3473 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3474 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3475#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003476 return OK;
3477 }
3478
3479 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003480 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003481 return FAIL;
3482}
3483
3484/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003485 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3486 * like with ":split".
3487 * Returns OK if a new tab page was created, FAIL otherwise.
3488 */
3489 int
3490may_open_tabpage()
3491{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003492 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003493
Bram Moolenaard326ce82007-03-11 14:48:29 +00003494 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003495 {
3496 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003497 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003498 return win_new_tabpage(n);
3499 }
3500 return FAIL;
3501}
3502
3503/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003504 * Create up to "maxcount" tabpages with empty windows.
3505 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003506 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003507 int
3508make_tabpages(maxcount)
3509 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003510{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003511 int count = maxcount;
3512 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003513
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003514 /* Limit to 'tabpagemax' tabs. */
3515 if (count > p_tpm)
3516 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003517
3518#ifdef FEAT_AUTOCMD
3519 /*
3520 * Don't execute autocommands while creating the tab pages. Must do that
3521 * when putting the buffers in the windows.
3522 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003523 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003524#endif
3525
3526 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003527 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003528 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003529
3530#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003531 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003532#endif
3533
3534 /* return actual number of tab pages */
3535 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003536}
3537
3538/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003539 * Return TRUE when "tpc" points to a valid tab page.
3540 */
3541 int
3542valid_tabpage(tpc)
3543 tabpage_T *tpc;
3544{
3545 tabpage_T *tp;
3546
3547 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3548 if (tp == tpc)
3549 return TRUE;
3550 return FALSE;
3551}
3552
3553/*
3554 * Find tab page "n" (first one is 1). Returns NULL when not found.
3555 */
3556 tabpage_T *
3557find_tabpage(n)
3558 int n;
3559{
3560 tabpage_T *tp;
3561 int i = 1;
3562
3563 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3564 ++i;
3565 return tp;
3566}
3567
3568/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003569 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003570 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003571 */
3572 int
3573tabpage_index(ftp)
3574 tabpage_T *ftp;
3575{
3576 int i = 1;
3577 tabpage_T *tp;
3578
3579 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3580 ++i;
3581 return i;
3582}
3583
3584/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003585 * Prepare for leaving the current tab page.
3586 * When autocomands change "curtab" we don't leave the tab page and return
3587 * FAIL.
3588 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003589 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003590 static int
3591leave_tabpage(new_curbuf)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003592 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003593 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003594{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003595 tabpage_T *tp = curtab;
3596
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003597#ifdef FEAT_VISUAL
3598 reset_VIsual_and_resel(); /* stop Visual mode */
3599#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003600#ifdef FEAT_AUTOCMD
3601 if (new_curbuf != curbuf)
3602 {
3603 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3604 if (curtab != tp)
3605 return FAIL;
3606 }
3607 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3608 if (curtab != tp)
3609 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003610 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003611 if (curtab != tp)
3612 return FAIL;
3613#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003614#if defined(FEAT_GUI)
3615 /* Remove the scrollbars. They may be added back later. */
3616 if (gui.in_use)
3617 gui_remove_scrollbars();
3618#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003619 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003620 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003621 tp->tp_firstwin = firstwin;
3622 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003623 tp->tp_old_Rows = Rows;
3624 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003625 firstwin = NULL;
3626 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003627 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003628}
3629
3630/*
3631 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003632 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003633 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003634 static void
3635enter_tabpage(tp, old_curbuf)
3636 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003637 buf_T *old_curbuf UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003638{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003639 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003640 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003641
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003642 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003643 firstwin = tp->tp_firstwin;
3644 lastwin = tp->tp_lastwin;
3645 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003646
3647 /* We would like doing the TabEnter event first, but we don't have a
3648 * valid current window yet, which may break some commands.
3649 * This triggers autocommands, thus may make "tp" invalid. */
3650 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3651 prevwin = next_prevwin;
3652
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003653#ifdef FEAT_AUTOCMD
3654 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003655
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003656 if (old_curbuf != curbuf)
3657 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3658#endif
3659
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003660 last_status(FALSE); /* status line may appear or disappear */
3661 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003662 must_redraw = CLEAR; /* need to redraw everything */
3663#ifdef FEAT_DIFF
3664 diff_need_scrollbind = TRUE;
3665#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003666
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003667 /* The tabpage line may have appeared or disappeared, may need to resize
3668 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003669 * frame sizes too. Use the stored value of p_ch, so that it can be
3670 * different for each tab page. */
3671 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003672 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3673#ifdef FEAT_GUI_TABLINE
3674 && !gui_use_tabline()
3675#endif
3676 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003677 shell_new_rows();
3678#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003679 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003680 shell_new_columns(); /* update window widths */
3681#endif
3682
3683#if defined(FEAT_GUI)
3684 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3685 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003686 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003687#endif
3688
3689 redraw_all_later(CLEAR);
3690}
3691
3692/*
3693 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003694 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003695 */
3696 void
3697goto_tabpage(n)
3698 int n;
3699{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003700 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003701 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003702 int i;
3703
Bram Moolenaard68071d2006-05-02 22:08:30 +00003704 if (text_locked())
3705 {
3706 /* Not allowed when editing the command line. */
3707#ifdef FEAT_CMDWIN
3708 if (cmdwin_type != 0)
3709 EMSG(_(e_cmdwin));
3710 else
3711#endif
3712 EMSG(_(e_secure));
3713 return;
3714 }
3715
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003716 /* If there is only one it can't work. */
3717 if (first_tabpage->tp_next == NULL)
3718 {
3719 if (n > 1)
3720 beep_flush();
3721 return;
3722 }
3723
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003724 if (n == 0)
3725 {
3726 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003727 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003728 tp = first_tabpage;
3729 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003730 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003731 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003732 else if (n < 0)
3733 {
3734 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3735 * this N times. */
3736 ttp = curtab;
3737 for (i = n; i < 0; ++i)
3738 {
3739 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3740 tp = tp->tp_next)
3741 ;
3742 ttp = tp;
3743 }
3744 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003745 else if (n == 9999)
3746 {
3747 /* Go to last tab page. */
3748 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3749 ;
3750 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003751 else
3752 {
3753 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003754 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003755 if (tp == NULL)
3756 {
3757 beep_flush();
3758 return;
3759 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003760 }
3761
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003762 goto_tabpage_tp(tp);
3763
3764#ifdef FEAT_GUI_TABLINE
3765 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003766 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003767#endif
3768}
3769
3770/*
3771 * Go to tabpage "tp".
3772 * Note: doesn't update the GUI tab.
3773 */
3774 void
3775goto_tabpage_tp(tp)
3776 tabpage_T *tp;
3777{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003778 /* Don't repeat a message in another tab page. */
3779 set_keep_msg(NULL, 0);
3780
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003781 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003782 {
3783 if (valid_tabpage(tp))
3784 enter_tabpage(tp, curbuf);
3785 else
3786 enter_tabpage(curtab, curbuf);
3787 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003788}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789
3790/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003791 * Enter window "wp" in tab page "tp".
3792 * Also updates the GUI tab.
3793 */
3794 void
3795goto_tabpage_win(tp, wp)
3796 tabpage_T *tp;
3797 win_T *wp;
3798{
3799 goto_tabpage_tp(tp);
3800 if (curtab == tp && win_valid(wp))
3801 {
3802 win_enter(wp, TRUE);
3803# ifdef FEAT_GUI_TABLINE
3804 if (gui_use_tabline())
3805 gui_mch_set_curtab(tabpage_index(curtab));
3806# endif
3807 }
3808}
3809
3810/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003811 * Move the current tab page to before tab page "nr".
3812 */
3813 void
3814tabpage_move(nr)
3815 int nr;
3816{
3817 int n = nr;
3818 tabpage_T *tp;
3819
3820 if (first_tabpage->tp_next == NULL)
3821 return;
3822
3823 /* Remove the current tab page from the list of tab pages. */
3824 if (curtab == first_tabpage)
3825 first_tabpage = curtab->tp_next;
3826 else
3827 {
3828 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3829 if (tp->tp_next == curtab)
3830 break;
3831 if (tp == NULL) /* "cannot happen" */
3832 return;
3833 tp->tp_next = curtab->tp_next;
3834 }
3835
3836 /* Re-insert it at the specified position. */
3837 if (n == 0)
3838 {
3839 curtab->tp_next = first_tabpage;
3840 first_tabpage = curtab;
3841 }
3842 else
3843 {
3844 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3845 --n;
3846 curtab->tp_next = tp->tp_next;
3847 tp->tp_next = curtab;
3848 }
3849
3850 /* Need to redraw the tabline. Tab page contents doesn't change. */
3851 redraw_tabline = TRUE;
3852}
3853
3854
3855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 * Go to another window.
3857 * When jumping to another buffer, stop Visual mode. Do this before
3858 * changing windows so we can yank the selection into the '*' register.
3859 * When jumping to another window on the same buffer, adjust its cursor
3860 * position to keep the same Visual area.
3861 */
3862 void
3863win_goto(wp)
3864 win_T *wp;
3865{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003866 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 {
3868 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003869 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 return;
3871 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003872#ifdef FEAT_AUTOCMD
3873 if (curbuf_locked())
3874 return;
3875#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003876
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877#ifdef FEAT_VISUAL
3878 if (wp->w_buffer != curbuf)
3879 reset_VIsual_and_resel();
3880 else if (VIsual_active)
3881 wp->w_cursor = curwin->w_cursor;
3882#endif
3883
3884#ifdef FEAT_GUI
3885 need_mouse_correct = TRUE;
3886#endif
3887 win_enter(wp, TRUE);
3888}
3889
3890#if defined(FEAT_PERL) || defined(PROTO)
3891/*
3892 * Find window number "winnr" (counting top to bottom).
3893 */
3894 win_T *
3895win_find_nr(winnr)
3896 int winnr;
3897{
3898 win_T *wp;
3899
3900# ifdef FEAT_WINDOWS
3901 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3902 if (--winnr == 0)
3903 break;
3904 return wp;
3905# else
3906 return curwin;
3907# endif
3908}
3909#endif
3910
3911#ifdef FEAT_VERTSPLIT
3912/*
3913 * Move to window above or below "count" times.
3914 */
3915 static void
3916win_goto_ver(up, count)
3917 int up; /* TRUE to go to win above */
3918 long count;
3919{
3920 frame_T *fr;
3921 frame_T *nfr;
3922 frame_T *foundfr;
3923
3924 foundfr = curwin->w_frame;
3925 while (count--)
3926 {
3927 /*
3928 * First go upwards in the tree of frames until we find a upwards or
3929 * downwards neighbor.
3930 */
3931 fr = foundfr;
3932 for (;;)
3933 {
3934 if (fr == topframe)
3935 goto end;
3936 if (up)
3937 nfr = fr->fr_prev;
3938 else
3939 nfr = fr->fr_next;
3940 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3941 break;
3942 fr = fr->fr_parent;
3943 }
3944
3945 /*
3946 * Now go downwards to find the bottom or top frame in it.
3947 */
3948 for (;;)
3949 {
3950 if (nfr->fr_layout == FR_LEAF)
3951 {
3952 foundfr = nfr;
3953 break;
3954 }
3955 fr = nfr->fr_child;
3956 if (nfr->fr_layout == FR_ROW)
3957 {
3958 /* Find the frame at the cursor row. */
3959 while (fr->fr_next != NULL
3960 && frame2win(fr)->w_wincol + fr->fr_width
3961 <= curwin->w_wincol + curwin->w_wcol)
3962 fr = fr->fr_next;
3963 }
3964 if (nfr->fr_layout == FR_COL && up)
3965 while (fr->fr_next != NULL)
3966 fr = fr->fr_next;
3967 nfr = fr;
3968 }
3969 }
3970end:
3971 if (foundfr != NULL)
3972 win_goto(foundfr->fr_win);
3973}
3974
3975/*
3976 * Move to left or right window.
3977 */
3978 static void
3979win_goto_hor(left, count)
3980 int left; /* TRUE to go to left win */
3981 long count;
3982{
3983 frame_T *fr;
3984 frame_T *nfr;
3985 frame_T *foundfr;
3986
3987 foundfr = curwin->w_frame;
3988 while (count--)
3989 {
3990 /*
3991 * First go upwards in the tree of frames until we find a left or
3992 * right neighbor.
3993 */
3994 fr = foundfr;
3995 for (;;)
3996 {
3997 if (fr == topframe)
3998 goto end;
3999 if (left)
4000 nfr = fr->fr_prev;
4001 else
4002 nfr = fr->fr_next;
4003 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4004 break;
4005 fr = fr->fr_parent;
4006 }
4007
4008 /*
4009 * Now go downwards to find the leftmost or rightmost frame in it.
4010 */
4011 for (;;)
4012 {
4013 if (nfr->fr_layout == FR_LEAF)
4014 {
4015 foundfr = nfr;
4016 break;
4017 }
4018 fr = nfr->fr_child;
4019 if (nfr->fr_layout == FR_COL)
4020 {
4021 /* Find the frame at the cursor row. */
4022 while (fr->fr_next != NULL
4023 && frame2win(fr)->w_winrow + fr->fr_height
4024 <= curwin->w_winrow + curwin->w_wrow)
4025 fr = fr->fr_next;
4026 }
4027 if (nfr->fr_layout == FR_ROW && left)
4028 while (fr->fr_next != NULL)
4029 fr = fr->fr_next;
4030 nfr = fr;
4031 }
4032 }
4033end:
4034 if (foundfr != NULL)
4035 win_goto(foundfr->fr_win);
4036}
4037#endif
4038
4039/*
4040 * Make window "wp" the current window.
4041 */
4042 void
4043win_enter(wp, undo_sync)
4044 win_T *wp;
4045 int undo_sync;
4046{
4047 win_enter_ext(wp, undo_sync, FALSE);
4048}
4049
4050/*
4051 * Make window wp the current window.
4052 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4053 * been closed and isn't valid.
4054 */
4055 static void
4056win_enter_ext(wp, undo_sync, curwin_invalid)
4057 win_T *wp;
4058 int undo_sync;
4059 int curwin_invalid;
4060{
4061#ifdef FEAT_AUTOCMD
4062 int other_buffer = FALSE;
4063#endif
4064
4065 if (wp == curwin && !curwin_invalid) /* nothing to do */
4066 return;
4067
4068#ifdef FEAT_AUTOCMD
4069 if (!curwin_invalid)
4070 {
4071 /*
4072 * Be careful: If autocommands delete the window, return now.
4073 */
4074 if (wp->w_buffer != curbuf)
4075 {
4076 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4077 other_buffer = TRUE;
4078 if (!win_valid(wp))
4079 return;
4080 }
4081 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4082 if (!win_valid(wp))
4083 return;
4084# ifdef FEAT_EVAL
4085 /* autocmds may abort script processing */
4086 if (aborting())
4087 return;
4088# endif
4089 }
4090#endif
4091
4092 /* sync undo before leaving the current buffer */
4093 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004094 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 /* may have to copy the buffer options when 'cpo' contains 'S' */
4096 if (wp->w_buffer != curbuf)
4097 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4098 if (!curwin_invalid)
4099 {
4100 prevwin = curwin; /* remember for CTRL-W p */
4101 curwin->w_redr_status = TRUE;
4102 }
4103 curwin = wp;
4104 curbuf = wp->w_buffer;
4105 check_cursor();
4106#ifdef FEAT_VIRTUALEDIT
4107 if (!virtual_active())
4108 curwin->w_cursor.coladd = 0;
4109#endif
4110 changed_line_abv_curs(); /* assume cursor position needs updating */
4111
4112 if (curwin->w_localdir != NULL)
4113 {
4114 /* Window has a local directory: Save current directory as global
4115 * directory (unless that was done already) and change to the local
4116 * directory. */
4117 if (globaldir == NULL)
4118 {
4119 char_u cwd[MAXPATHL];
4120
4121 if (mch_dirname(cwd, MAXPATHL) == OK)
4122 globaldir = vim_strsave(cwd);
4123 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004124 if (mch_chdir((char *)curwin->w_localdir) == 0)
4125 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
4127 else if (globaldir != NULL)
4128 {
4129 /* Window doesn't have a local directory and we are not in the global
4130 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004131 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 vim_free(globaldir);
4133 globaldir = NULL;
4134 shorten_fnames(TRUE);
4135 }
4136
4137#ifdef FEAT_AUTOCMD
4138 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4139 if (other_buffer)
4140 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4141#endif
4142
4143#ifdef FEAT_TITLE
4144 maketitle();
4145#endif
4146 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004147 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 if (restart_edit)
4149 redraw_later(VALID); /* causes status line redraw */
4150
4151 /* set window height to desired minimal value */
4152 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4153 win_setheight((int)p_wh);
4154 else if (curwin->w_height == 0)
4155 win_setheight(1);
4156
4157#ifdef FEAT_VERTSPLIT
4158 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004159 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 win_setwidth((int)p_wiw);
4161#endif
4162
4163#ifdef FEAT_MOUSE
4164 setmouse(); /* in case jumped to/from help buffer */
4165#endif
4166
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004167 /* Change directories when the 'acd' option is set. */
4168 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169}
4170
4171#endif /* FEAT_WINDOWS */
4172
4173#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4174/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004175 * Jump to the first open window that contains buffer "buf", if one exists.
4176 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 */
4178 win_T *
4179buf_jump_open_win(buf)
4180 buf_T *buf;
4181{
4182# ifdef FEAT_WINDOWS
4183 win_T *wp;
4184
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004185 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 if (wp->w_buffer == buf)
4187 break;
4188 if (wp != NULL)
4189 win_enter(wp, FALSE);
4190 return wp;
4191# else
4192 if (curwin->w_buffer == buf)
4193 return curwin;
4194 return NULL;
4195# endif
4196}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004197
4198/*
4199 * Jump to the first open window in any tab page that contains buffer "buf",
4200 * if one exists.
4201 * Returns a pointer to the window found, otherwise NULL.
4202 */
4203 win_T *
4204buf_jump_open_tab(buf)
4205 buf_T *buf;
4206{
4207# ifdef FEAT_WINDOWS
4208 win_T *wp;
4209 tabpage_T *tp;
4210
4211 /* First try the current tab page. */
4212 wp = buf_jump_open_win(buf);
4213 if (wp != NULL)
4214 return wp;
4215
4216 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4217 if (tp != curtab)
4218 {
4219 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4220 if (wp->w_buffer == buf)
4221 break;
4222 if (wp != NULL)
4223 {
4224 goto_tabpage_win(tp, wp);
4225 if (curwin != wp)
4226 wp = NULL; /* something went wrong */
4227 break;
4228 }
4229 }
4230
4231 return wp;
4232# else
4233 if (curwin->w_buffer == buf)
4234 return curwin;
4235 return NULL;
4236# endif
4237}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238#endif
4239
4240/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004241 * Allocate a window structure and link it in the window list when "hidden" is
4242 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004245win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004246 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004247 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248{
4249 win_T *newwin;
4250
4251 /*
4252 * allocate window structure and linesizes arrays
4253 */
4254 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4255 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4256 {
4257 vim_free(newwin);
4258 newwin = NULL;
4259 }
4260
4261 if (newwin != NULL)
4262 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004263#ifdef FEAT_AUTOCMD
4264 /* Don't execute autocommands while the window is not properly
4265 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4266 * event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004267 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 /*
4270 * link the window in the window list
4271 */
4272#ifdef FEAT_WINDOWS
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004273 if (!hidden)
4274 win_append(after, newwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275#endif
4276#ifdef FEAT_VERTSPLIT
4277 newwin->w_wincol = 0;
4278 newwin->w_width = Columns;
4279#endif
4280
4281 /* position the display and the cursor at the top of the file. */
4282 newwin->w_topline = 1;
4283#ifdef FEAT_DIFF
4284 newwin->w_topfill = 0;
4285#endif
4286 newwin->w_botline = 2;
4287 newwin->w_cursor.lnum = 1;
4288#ifdef FEAT_SCROLLBIND
4289 newwin->w_scbind_pos = 1;
4290#endif
4291
4292 /* We won't calculate w_fraction until resizing the window */
4293 newwin->w_fraction = 0;
4294 newwin->w_prev_fraction_row = -1;
4295
4296#ifdef FEAT_GUI
4297 if (gui.in_use)
4298 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4300 SBAR_LEFT, newwin);
4301 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4302 SBAR_RIGHT, newwin);
4303 }
4304#endif
4305#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004306 /* init w: variables */
4307 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308#endif
4309#ifdef FEAT_FOLDING
4310 foldInitWin(newwin);
4311#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004312#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004313 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004314#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004315#ifdef FEAT_SEARCH_EXTRA
4316 newwin->w_match_head = NULL;
4317 newwin->w_next_match_id = 4;
4318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 }
4320 return newwin;
4321}
4322
4323#if defined(FEAT_WINDOWS) || defined(PROTO)
4324
4325/*
4326 * remove window 'wp' from the window list and free the structure
4327 */
4328 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004329win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004331 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332{
4333 int i;
4334
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004335#ifdef FEAT_FOLDING
4336 clearFolding(wp);
4337#endif
4338
4339 /* reduce the reference count to the argument list. */
4340 alist_unlink(wp->w_alist);
4341
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004342#ifdef FEAT_AUTOCMD
4343 /* Don't execute autocommands while the window is halfway being deleted.
4344 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004345 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004346#endif
4347
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004348#ifdef FEAT_MZSCHEME
4349 mzscheme_window_free(wp);
4350#endif
4351
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352#ifdef FEAT_PERL
4353 perl_win_free(wp);
4354#endif
4355
4356#ifdef FEAT_PYTHON
4357 python_window_free(wp);
4358#endif
4359
4360#ifdef FEAT_TCL
4361 tcl_window_free(wp);
4362#endif
4363
4364#ifdef FEAT_RUBY
4365 ruby_window_free(wp);
4366#endif
4367
4368 clear_winopt(&wp->w_onebuf_opt);
4369 clear_winopt(&wp->w_allbuf_opt);
4370
4371#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004372 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373#endif
4374
4375 if (prevwin == wp)
4376 prevwin = NULL;
4377 win_free_lsize(wp);
4378
4379 for (i = 0; i < wp->w_tagstacklen; ++i)
4380 vim_free(wp->w_tagstack[i].tagname);
4381
4382 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004383
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004385 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004387
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388#ifdef FEAT_JUMPLIST
4389 free_jumplist(wp);
4390#endif
4391
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004392#ifdef FEAT_QUICKFIX
4393 qf_free_all(wp);
4394#endif
4395
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396#ifdef FEAT_GUI
4397 if (gui.in_use)
4398 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4400 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4401 }
4402#endif /* FEAT_GUI */
4403
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004404#ifdef FEAT_AUTOCMD
4405 if (wp != aucmd_win)
4406#endif
4407 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004409
4410#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004411 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413}
4414
4415/*
4416 * Append window "wp" in the window list after window "after".
4417 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004418 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419win_append(after, wp)
4420 win_T *after, *wp;
4421{
4422 win_T *before;
4423
4424 if (after == NULL) /* after NULL is in front of the first */
4425 before = firstwin;
4426 else
4427 before = after->w_next;
4428
4429 wp->w_next = before;
4430 wp->w_prev = after;
4431 if (after == NULL)
4432 firstwin = wp;
4433 else
4434 after->w_next = wp;
4435 if (before == NULL)
4436 lastwin = wp;
4437 else
4438 before->w_prev = wp;
4439}
4440
4441/*
4442 * Remove a window from the window list.
4443 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004444 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004445win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004447 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448{
4449 if (wp->w_prev != NULL)
4450 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004451 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004453 else
4454 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 if (wp->w_next != NULL)
4456 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004457 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004459 else
4460 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461}
4462
4463/*
4464 * Append frame "frp" in a frame list after frame "after".
4465 */
4466 static void
4467frame_append(after, frp)
4468 frame_T *after, *frp;
4469{
4470 frp->fr_next = after->fr_next;
4471 after->fr_next = frp;
4472 if (frp->fr_next != NULL)
4473 frp->fr_next->fr_prev = frp;
4474 frp->fr_prev = after;
4475}
4476
4477/*
4478 * Insert frame "frp" in a frame list before frame "before".
4479 */
4480 static void
4481frame_insert(before, frp)
4482 frame_T *before, *frp;
4483{
4484 frp->fr_next = before;
4485 frp->fr_prev = before->fr_prev;
4486 before->fr_prev = frp;
4487 if (frp->fr_prev != NULL)
4488 frp->fr_prev->fr_next = frp;
4489 else
4490 frp->fr_parent->fr_child = frp;
4491}
4492
4493/*
4494 * Remove a frame from a frame list.
4495 */
4496 static void
4497frame_remove(frp)
4498 frame_T *frp;
4499{
4500 if (frp->fr_prev != NULL)
4501 frp->fr_prev->fr_next = frp->fr_next;
4502 else
4503 frp->fr_parent->fr_child = frp->fr_next;
4504 if (frp->fr_next != NULL)
4505 frp->fr_next->fr_prev = frp->fr_prev;
4506}
4507
4508#endif /* FEAT_WINDOWS */
4509
4510/*
4511 * Allocate w_lines[] for window "wp".
4512 * Return FAIL for failure, OK for success.
4513 */
4514 int
4515win_alloc_lines(wp)
4516 win_T *wp;
4517{
4518 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004519 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 if (wp->w_lines == NULL)
4521 return FAIL;
4522 return OK;
4523}
4524
4525/*
4526 * free lsize arrays for a window
4527 */
4528 void
4529win_free_lsize(wp)
4530 win_T *wp;
4531{
4532 vim_free(wp->w_lines);
4533 wp->w_lines = NULL;
4534}
4535
4536/*
4537 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004538 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 */
4540 void
4541shell_new_rows()
4542{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004543 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
4545 if (firstwin == NULL) /* not initialized yet */
4546 return;
4547#ifdef FEAT_WINDOWS
4548 if (h < frame_minheight(topframe, NULL))
4549 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004550
4551 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 * that doesn't result in the right height, forget about that option. */
4553 frame_new_height(topframe, h, FALSE, TRUE);
4554 if (topframe->fr_height != h)
4555 frame_new_height(topframe, h, FALSE, FALSE);
4556
4557 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4558#else
4559 if (h < 1)
4560 h = 1;
4561 win_new_height(firstwin, h);
4562#endif
4563 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004564#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004565 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004566#endif
4567
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568#if 0
4569 /* Disabled: don't want making the screen smaller make a window larger. */
4570 if (p_ea)
4571 win_equal(curwin, FALSE, 'v');
4572#endif
4573}
4574
4575#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4576/*
4577 * Called from win_new_shellsize() after Columns changed.
4578 */
4579 void
4580shell_new_columns()
4581{
4582 if (firstwin == NULL) /* not initialized yet */
4583 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004584
4585 /* First try setting the widths of windows with 'winfixwidth'. If that
4586 * doesn't result in the right width, forget about that option. */
4587 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4588 if (topframe->fr_width != Columns)
4589 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4590
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4592#if 0
4593 /* Disabled: don't want making the screen smaller make a window larger. */
4594 if (p_ea)
4595 win_equal(curwin, FALSE, 'h');
4596#endif
4597}
4598#endif
4599
4600#if defined(FEAT_CMDWIN) || defined(PROTO)
4601/*
4602 * Save the size of all windows in "gap".
4603 */
4604 void
4605win_size_save(gap)
4606 garray_T *gap;
4607
4608{
4609 win_T *wp;
4610
4611 ga_init2(gap, (int)sizeof(int), 1);
4612 if (ga_grow(gap, win_count() * 2) == OK)
4613 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4614 {
4615 ((int *)gap->ga_data)[gap->ga_len++] =
4616 wp->w_width + wp->w_vsep_width;
4617 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4618 }
4619}
4620
4621/*
4622 * Restore window sizes, but only if the number of windows is still the same.
4623 * Does not free the growarray.
4624 */
4625 void
4626win_size_restore(gap)
4627 garray_T *gap;
4628{
4629 win_T *wp;
4630 int i;
4631
4632 if (win_count() * 2 == gap->ga_len)
4633 {
4634 i = 0;
4635 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4636 {
4637 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4638 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4639 }
4640 /* recompute the window positions */
4641 (void)win_comp_pos();
4642 }
4643}
4644#endif /* FEAT_CMDWIN */
4645
4646#if defined(FEAT_WINDOWS) || defined(PROTO)
4647/*
4648 * Update the position for all windows, using the width and height of the
4649 * frames.
4650 * Returns the row just after the last window.
4651 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004652 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653win_comp_pos()
4654{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004655 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 int col = 0;
4657
4658 frame_comp_pos(topframe, &row, &col);
4659 return row;
4660}
4661
4662/*
4663 * Update the position of the windows in frame "topfrp", using the width and
4664 * height of the frames.
4665 * "*row" and "*col" are the top-left position of the frame. They are updated
4666 * to the bottom-right position plus one.
4667 */
4668 static void
4669frame_comp_pos(topfrp, row, col)
4670 frame_T *topfrp;
4671 int *row;
4672 int *col;
4673{
4674 win_T *wp;
4675 frame_T *frp;
4676#ifdef FEAT_VERTSPLIT
4677 int startcol;
4678 int startrow;
4679#endif
4680
4681 wp = topfrp->fr_win;
4682 if (wp != NULL)
4683 {
4684 if (wp->w_winrow != *row
4685#ifdef FEAT_VERTSPLIT
4686 || wp->w_wincol != *col
4687#endif
4688 )
4689 {
4690 /* position changed, redraw */
4691 wp->w_winrow = *row;
4692#ifdef FEAT_VERTSPLIT
4693 wp->w_wincol = *col;
4694#endif
4695 redraw_win_later(wp, NOT_VALID);
4696 wp->w_redr_status = TRUE;
4697 }
4698 *row += wp->w_height + wp->w_status_height;
4699#ifdef FEAT_VERTSPLIT
4700 *col += wp->w_width + wp->w_vsep_width;
4701#endif
4702 }
4703 else
4704 {
4705#ifdef FEAT_VERTSPLIT
4706 startrow = *row;
4707 startcol = *col;
4708#endif
4709 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4710 {
4711#ifdef FEAT_VERTSPLIT
4712 if (topfrp->fr_layout == FR_ROW)
4713 *row = startrow; /* all frames are at the same row */
4714 else
4715 *col = startcol; /* all frames are at the same col */
4716#endif
4717 frame_comp_pos(frp, row, col);
4718 }
4719 }
4720}
4721
4722#endif /* FEAT_WINDOWS */
4723
4724/*
4725 * Set current window height and take care of repositioning other windows to
4726 * fit around it.
4727 */
4728 void
4729win_setheight(height)
4730 int height;
4731{
4732 win_setheight_win(height, curwin);
4733}
4734
4735/*
4736 * Set the window height of window "win" and take care of repositioning other
4737 * windows to fit around it.
4738 */
4739 void
4740win_setheight_win(height, win)
4741 int height;
4742 win_T *win;
4743{
4744 int row;
4745
4746 if (win == curwin)
4747 {
4748 /* Always keep current window at least one line high, even when
4749 * 'winminheight' is zero. */
4750#ifdef FEAT_WINDOWS
4751 if (height < p_wmh)
4752 height = p_wmh;
4753#endif
4754 if (height == 0)
4755 height = 1;
4756 }
4757
4758#ifdef FEAT_WINDOWS
4759 frame_setheight(win->w_frame, height + win->w_status_height);
4760
4761 /* recompute the window positions */
4762 row = win_comp_pos();
4763#else
4764 if (height > topframe->fr_height)
4765 height = topframe->fr_height;
4766 win->w_height = height;
4767 row = height;
4768#endif
4769
4770 /*
4771 * If there is extra space created between the last window and the command
4772 * line, clear it.
4773 */
4774 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4775 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4776 cmdline_row = row;
4777 msg_row = row;
4778 msg_col = 0;
4779
4780 redraw_all_later(NOT_VALID);
4781}
4782
4783#if defined(FEAT_WINDOWS) || defined(PROTO)
4784
4785/*
4786 * Set the height of a frame to "height" and take care that all frames and
4787 * windows inside it are resized. Also resize frames on the left and right if
4788 * the are in the same FR_ROW frame.
4789 *
4790 * Strategy:
4791 * If the frame is part of a FR_COL frame, try fitting the frame in that
4792 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4793 * go to containing frames to resize them and make room.
4794 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4795 * Check for the minimal height of the FR_ROW frame.
4796 * At the top level we can also use change the command line height.
4797 */
4798 static void
4799frame_setheight(curfrp, height)
4800 frame_T *curfrp;
4801 int height;
4802{
4803 int room; /* total number of lines available */
4804 int take; /* number of lines taken from other windows */
4805 int room_cmdline; /* lines available from cmdline */
4806 int run;
4807 frame_T *frp;
4808 int h;
4809 int room_reserved;
4810
4811 /* If the height already is the desired value, nothing to do. */
4812 if (curfrp->fr_height == height)
4813 return;
4814
4815 if (curfrp->fr_parent == NULL)
4816 {
4817 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004818 if (height > ROWS_AVAIL)
4819 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 if (height > 0)
4821 frame_new_height(curfrp, height, FALSE, FALSE);
4822 }
4823 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4824 {
4825 /* Row of frames: Also need to resize frames left and right of this
4826 * one. First check for the minimal height of these. */
4827 h = frame_minheight(curfrp->fr_parent, NULL);
4828 if (height < h)
4829 height = h;
4830 frame_setheight(curfrp->fr_parent, height);
4831 }
4832 else
4833 {
4834 /*
4835 * Column of frames: try to change only frames in this column.
4836 */
4837#ifdef FEAT_VERTSPLIT
4838 /*
4839 * Do this twice:
4840 * 1: compute room available, if it's not enough try resizing the
4841 * containing frame.
4842 * 2: compute the room available and adjust the height to it.
4843 * Try not to reduce the height of a window with 'winfixheight' set.
4844 */
4845 for (run = 1; run <= 2; ++run)
4846#else
4847 for (;;)
4848#endif
4849 {
4850 room = 0;
4851 room_reserved = 0;
4852 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4853 frp = frp->fr_next)
4854 {
4855 if (frp != curfrp
4856 && frp->fr_win != NULL
4857 && frp->fr_win->w_p_wfh)
4858 room_reserved += frp->fr_height;
4859 room += frp->fr_height;
4860 if (frp != curfrp)
4861 room -= frame_minheight(frp, NULL);
4862 }
4863#ifdef FEAT_VERTSPLIT
4864 if (curfrp->fr_width != Columns)
4865 room_cmdline = 0;
4866 else
4867#endif
4868 {
4869 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4870 + lastwin->w_height + lastwin->w_status_height);
4871 if (room_cmdline < 0)
4872 room_cmdline = 0;
4873 }
4874
4875 if (height <= room + room_cmdline)
4876 break;
4877#ifdef FEAT_VERTSPLIT
4878 if (run == 2 || curfrp->fr_width == Columns)
4879#endif
4880 {
4881 if (height > room + room_cmdline)
4882 height = room + room_cmdline;
4883 break;
4884 }
4885#ifdef FEAT_VERTSPLIT
4886 frame_setheight(curfrp->fr_parent, height
4887 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4888#endif
4889 /*NOTREACHED*/
4890 }
4891
4892 /*
4893 * Compute the number of lines we will take from others frames (can be
4894 * negative!).
4895 */
4896 take = height - curfrp->fr_height;
4897
4898 /* If there is not enough room, also reduce the height of a window
4899 * with 'winfixheight' set. */
4900 if (height > room + room_cmdline - room_reserved)
4901 room_reserved = room + room_cmdline - height;
4902 /* If there is only a 'winfixheight' window and making the
4903 * window smaller, need to make the other window taller. */
4904 if (take < 0 && room - curfrp->fr_height < room_reserved)
4905 room_reserved = 0;
4906
4907 if (take > 0 && room_cmdline > 0)
4908 {
4909 /* use lines from cmdline first */
4910 if (take < room_cmdline)
4911 room_cmdline = take;
4912 take -= room_cmdline;
4913 topframe->fr_height += room_cmdline;
4914 }
4915
4916 /*
4917 * set the current frame to the new height
4918 */
4919 frame_new_height(curfrp, height, FALSE, FALSE);
4920
4921 /*
4922 * First take lines from the frames after the current frame. If
4923 * that is not enough, takes lines from frames above the current
4924 * frame.
4925 */
4926 for (run = 0; run < 2; ++run)
4927 {
4928 if (run == 0)
4929 frp = curfrp->fr_next; /* 1st run: start with next window */
4930 else
4931 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4932 while (frp != NULL && take != 0)
4933 {
4934 h = frame_minheight(frp, NULL);
4935 if (room_reserved > 0
4936 && frp->fr_win != NULL
4937 && frp->fr_win->w_p_wfh)
4938 {
4939 if (room_reserved >= frp->fr_height)
4940 room_reserved -= frp->fr_height;
4941 else
4942 {
4943 if (frp->fr_height - room_reserved > take)
4944 room_reserved = frp->fr_height - take;
4945 take -= frp->fr_height - room_reserved;
4946 frame_new_height(frp, room_reserved, FALSE, FALSE);
4947 room_reserved = 0;
4948 }
4949 }
4950 else
4951 {
4952 if (frp->fr_height - take < h)
4953 {
4954 take -= frp->fr_height - h;
4955 frame_new_height(frp, h, FALSE, FALSE);
4956 }
4957 else
4958 {
4959 frame_new_height(frp, frp->fr_height - take,
4960 FALSE, FALSE);
4961 take = 0;
4962 }
4963 }
4964 if (run == 0)
4965 frp = frp->fr_next;
4966 else
4967 frp = frp->fr_prev;
4968 }
4969 }
4970 }
4971}
4972
4973#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4974/*
4975 * Set current window width and take care of repositioning other windows to
4976 * fit around it.
4977 */
4978 void
4979win_setwidth(width)
4980 int width;
4981{
4982 win_setwidth_win(width, curwin);
4983}
4984
4985 void
4986win_setwidth_win(width, wp)
4987 int width;
4988 win_T *wp;
4989{
4990 /* Always keep current window at least one column wide, even when
4991 * 'winminwidth' is zero. */
4992 if (wp == curwin)
4993 {
4994 if (width < p_wmw)
4995 width = p_wmw;
4996 if (width == 0)
4997 width = 1;
4998 }
4999
5000 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5001
5002 /* recompute the window positions */
5003 (void)win_comp_pos();
5004
5005 redraw_all_later(NOT_VALID);
5006}
5007
5008/*
5009 * Set the width of a frame to "width" and take care that all frames and
5010 * windows inside it are resized. Also resize frames above and below if the
5011 * are in the same FR_ROW frame.
5012 *
5013 * Strategy is similar to frame_setheight().
5014 */
5015 static void
5016frame_setwidth(curfrp, width)
5017 frame_T *curfrp;
5018 int width;
5019{
5020 int room; /* total number of lines available */
5021 int take; /* number of lines taken from other windows */
5022 int run;
5023 frame_T *frp;
5024 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005025 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026
5027 /* If the width already is the desired value, nothing to do. */
5028 if (curfrp->fr_width == width)
5029 return;
5030
5031 if (curfrp->fr_parent == NULL)
5032 /* topframe: can't change width */
5033 return;
5034
5035 if (curfrp->fr_parent->fr_layout == FR_COL)
5036 {
5037 /* Column of frames: Also need to resize frames above and below of
5038 * this one. First check for the minimal width of these. */
5039 w = frame_minwidth(curfrp->fr_parent, NULL);
5040 if (width < w)
5041 width = w;
5042 frame_setwidth(curfrp->fr_parent, width);
5043 }
5044 else
5045 {
5046 /*
5047 * Row of frames: try to change only frames in this row.
5048 *
5049 * Do this twice:
5050 * 1: compute room available, if it's not enough try resizing the
5051 * containing frame.
5052 * 2: compute the room available and adjust the width to it.
5053 */
5054 for (run = 1; run <= 2; ++run)
5055 {
5056 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005057 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5059 frp = frp->fr_next)
5060 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005061 if (frp != curfrp
5062 && frp->fr_win != NULL
5063 && frp->fr_win->w_p_wfw)
5064 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 room += frp->fr_width;
5066 if (frp != curfrp)
5067 room -= frame_minwidth(frp, NULL);
5068 }
5069
5070 if (width <= room)
5071 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005072 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 {
5074 if (width > room)
5075 width = room;
5076 break;
5077 }
5078 frame_setwidth(curfrp->fr_parent, width
5079 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5080 }
5081
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 /*
5083 * Compute the number of lines we will take from others frames (can be
5084 * negative!).
5085 */
5086 take = width - curfrp->fr_width;
5087
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005088 /* If there is not enough room, also reduce the width of a window
5089 * with 'winfixwidth' set. */
5090 if (width > room - room_reserved)
5091 room_reserved = room - width;
5092 /* If there is only a 'winfixwidth' window and making the
5093 * window smaller, need to make the other window narrower. */
5094 if (take < 0 && room - curfrp->fr_width < room_reserved)
5095 room_reserved = 0;
5096
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 /*
5098 * set the current frame to the new width
5099 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005100 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101
5102 /*
5103 * First take lines from the frames right of the current frame. If
5104 * that is not enough, takes lines from frames left of the current
5105 * frame.
5106 */
5107 for (run = 0; run < 2; ++run)
5108 {
5109 if (run == 0)
5110 frp = curfrp->fr_next; /* 1st run: start with next window */
5111 else
5112 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5113 while (frp != NULL && take != 0)
5114 {
5115 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005116 if (room_reserved > 0
5117 && frp->fr_win != NULL
5118 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005120 if (room_reserved >= frp->fr_width)
5121 room_reserved -= frp->fr_width;
5122 else
5123 {
5124 if (frp->fr_width - room_reserved > take)
5125 room_reserved = frp->fr_width - take;
5126 take -= frp->fr_width - room_reserved;
5127 frame_new_width(frp, room_reserved, FALSE, FALSE);
5128 room_reserved = 0;
5129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
5131 else
5132 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005133 if (frp->fr_width - take < w)
5134 {
5135 take -= frp->fr_width - w;
5136 frame_new_width(frp, w, FALSE, FALSE);
5137 }
5138 else
5139 {
5140 frame_new_width(frp, frp->fr_width - take,
5141 FALSE, FALSE);
5142 take = 0;
5143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
5145 if (run == 0)
5146 frp = frp->fr_next;
5147 else
5148 frp = frp->fr_prev;
5149 }
5150 }
5151 }
5152}
5153#endif /* FEAT_VERTSPLIT */
5154
5155/*
5156 * Check 'winminheight' for a valid value.
5157 */
5158 void
5159win_setminheight()
5160{
5161 int room;
5162 int first = TRUE;
5163 win_T *wp;
5164
5165 /* loop until there is a 'winminheight' that is possible */
5166 while (p_wmh > 0)
5167 {
5168 /* TODO: handle vertical splits */
5169 room = -p_wh;
5170 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5171 room += wp->w_height - p_wmh;
5172 if (room >= 0)
5173 break;
5174 --p_wmh;
5175 if (first)
5176 {
5177 EMSG(_(e_noroom));
5178 first = FALSE;
5179 }
5180 }
5181}
5182
5183#ifdef FEAT_MOUSE
5184
5185/*
5186 * Status line of dragwin is dragged "offset" lines down (negative is up).
5187 */
5188 void
5189win_drag_status_line(dragwin, offset)
5190 win_T *dragwin;
5191 int offset;
5192{
5193 frame_T *curfr;
5194 frame_T *fr;
5195 int room;
5196 int row;
5197 int up; /* if TRUE, drag status line up, otherwise down */
5198 int n;
5199
5200 fr = dragwin->w_frame;
5201 curfr = fr;
5202 if (fr != topframe) /* more than one window */
5203 {
5204 fr = fr->fr_parent;
5205 /* When the parent frame is not a column of frames, its parent should
5206 * be. */
5207 if (fr->fr_layout != FR_COL)
5208 {
5209 curfr = fr;
5210 if (fr != topframe) /* only a row of windows, may drag statusline */
5211 fr = fr->fr_parent;
5212 }
5213 }
5214
5215 /* If this is the last frame in a column, may want to resize the parent
5216 * frame instead (go two up to skip a row of frames). */
5217 while (curfr != topframe && curfr->fr_next == NULL)
5218 {
5219 if (fr != topframe)
5220 fr = fr->fr_parent;
5221 curfr = fr;
5222 if (fr != topframe)
5223 fr = fr->fr_parent;
5224 }
5225
5226 if (offset < 0) /* drag up */
5227 {
5228 up = TRUE;
5229 offset = -offset;
5230 /* sum up the room of the current frame and above it */
5231 if (fr == curfr)
5232 {
5233 /* only one window */
5234 room = fr->fr_height - frame_minheight(fr, NULL);
5235 }
5236 else
5237 {
5238 room = 0;
5239 for (fr = fr->fr_child; ; fr = fr->fr_next)
5240 {
5241 room += fr->fr_height - frame_minheight(fr, NULL);
5242 if (fr == curfr)
5243 break;
5244 }
5245 }
5246 fr = curfr->fr_next; /* put fr at frame that grows */
5247 }
5248 else /* drag down */
5249 {
5250 up = FALSE;
5251 /*
5252 * Only dragging the last status line can reduce p_ch.
5253 */
5254 room = Rows - cmdline_row;
5255 if (curfr->fr_next == NULL)
5256 room -= 1;
5257 else
5258 room -= p_ch;
5259 if (room < 0)
5260 room = 0;
5261 /* sum up the room of frames below of the current one */
5262 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5263 room += fr->fr_height - frame_minheight(fr, NULL);
5264 fr = curfr; /* put fr at window that grows */
5265 }
5266
5267 if (room < offset) /* Not enough room */
5268 offset = room; /* Move as far as we can */
5269 if (offset <= 0)
5270 return;
5271
5272 /*
5273 * Grow frame fr by "offset" lines.
5274 * Doesn't happen when dragging the last status line up.
5275 */
5276 if (fr != NULL)
5277 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5278
5279 if (up)
5280 fr = curfr; /* current frame gets smaller */
5281 else
5282 fr = curfr->fr_next; /* next frame gets smaller */
5283
5284 /*
5285 * Now make the other frames smaller.
5286 */
5287 while (fr != NULL && offset > 0)
5288 {
5289 n = frame_minheight(fr, NULL);
5290 if (fr->fr_height - offset <= n)
5291 {
5292 offset -= fr->fr_height - n;
5293 frame_new_height(fr, n, !up, FALSE);
5294 }
5295 else
5296 {
5297 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5298 break;
5299 }
5300 if (up)
5301 fr = fr->fr_prev;
5302 else
5303 fr = fr->fr_next;
5304 }
5305 row = win_comp_pos();
5306 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5307 cmdline_row = row;
5308 p_ch = Rows - cmdline_row;
5309 if (p_ch < 1)
5310 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005311 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005312 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 showmode();
5314}
5315
5316#ifdef FEAT_VERTSPLIT
5317/*
5318 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5319 */
5320 void
5321win_drag_vsep_line(dragwin, offset)
5322 win_T *dragwin;
5323 int offset;
5324{
5325 frame_T *curfr;
5326 frame_T *fr;
5327 int room;
5328 int left; /* if TRUE, drag separator line left, otherwise right */
5329 int n;
5330
5331 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005332 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 return;
5334 curfr = fr;
5335 fr = fr->fr_parent;
5336 /* When the parent frame is not a row of frames, its parent should be. */
5337 if (fr->fr_layout != FR_ROW)
5338 {
5339 if (fr == topframe) /* only a column of windows (cannot happen?) */
5340 return;
5341 curfr = fr;
5342 fr = fr->fr_parent;
5343 }
5344
5345 /* If this is the last frame in a row, may want to resize a parent
5346 * frame instead. */
5347 while (curfr->fr_next == NULL)
5348 {
5349 if (fr == topframe)
5350 break;
5351 curfr = fr;
5352 fr = fr->fr_parent;
5353 if (fr != topframe)
5354 {
5355 curfr = fr;
5356 fr = fr->fr_parent;
5357 }
5358 }
5359
5360 if (offset < 0) /* drag left */
5361 {
5362 left = TRUE;
5363 offset = -offset;
5364 /* sum up the room of the current frame and left of it */
5365 room = 0;
5366 for (fr = fr->fr_child; ; fr = fr->fr_next)
5367 {
5368 room += fr->fr_width - frame_minwidth(fr, NULL);
5369 if (fr == curfr)
5370 break;
5371 }
5372 fr = curfr->fr_next; /* put fr at frame that grows */
5373 }
5374 else /* drag right */
5375 {
5376 left = FALSE;
5377 /* sum up the room of frames right of the current one */
5378 room = 0;
5379 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5380 room += fr->fr_width - frame_minwidth(fr, NULL);
5381 fr = curfr; /* put fr at window that grows */
5382 }
5383
5384 if (room < offset) /* Not enough room */
5385 offset = room; /* Move as far as we can */
5386 if (offset <= 0) /* No room at all, quit. */
5387 return;
5388
5389 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005390 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391
5392 /* shrink other frames: current and at the left or at the right */
5393 if (left)
5394 fr = curfr; /* current frame gets smaller */
5395 else
5396 fr = curfr->fr_next; /* next frame gets smaller */
5397
5398 while (fr != NULL && offset > 0)
5399 {
5400 n = frame_minwidth(fr, NULL);
5401 if (fr->fr_width - offset <= n)
5402 {
5403 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005404 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 }
5406 else
5407 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005408 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 break;
5410 }
5411 if (left)
5412 fr = fr->fr_prev;
5413 else
5414 fr = fr->fr_next;
5415 }
5416 (void)win_comp_pos();
5417 redraw_all_later(NOT_VALID);
5418}
5419#endif /* FEAT_VERTSPLIT */
5420#endif /* FEAT_MOUSE */
5421
5422#endif /* FEAT_WINDOWS */
5423
5424/*
5425 * Set the height of a window.
5426 * This takes care of the things inside the window, not what happens to the
5427 * window position, the frame or to other windows.
5428 */
5429 static void
5430win_new_height(wp, height)
5431 win_T *wp;
5432 int height;
5433{
5434 linenr_T lnum;
5435 int sline, line_size;
5436#define FRACTION_MULT 16384L
5437
5438 /* Don't want a negative height. Happens when splitting a tiny window.
5439 * Will equalize heights soon to fix it. */
5440 if (height < 0)
5441 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005442 if (wp->w_height == height)
5443 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
5445 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5446 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5447 + FRACTION_MULT / 2) / (long)wp->w_height;
5448
5449 wp->w_height = height;
5450 wp->w_skipcol = 0;
5451
5452 /* Don't change w_topline when height is zero. Don't set w_topline when
5453 * 'scrollbind' is set and this isn't the current window. */
5454 if (height > 0
5455#ifdef FEAT_SCROLLBIND
5456 && (!wp->w_p_scb || wp == curwin)
5457#endif
5458 )
5459 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005460 /*
5461 * Find a value for w_topline that shows the cursor at the same
5462 * relative position in the window as before (more or less).
5463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 lnum = wp->w_cursor.lnum;
5465 if (lnum < 1) /* can happen when starting up */
5466 lnum = 1;
5467 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5468 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5469 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005470
5471 if (sline >= 0)
5472 {
5473 /* Make sure the whole cursor line is visible, if possible. */
5474 int rows = plines_win(wp, lnum, FALSE);
5475
5476 if (sline > wp->w_height - rows)
5477 {
5478 sline = wp->w_height - rows;
5479 wp->w_wrow -= rows - line_size;
5480 }
5481 }
5482
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 if (sline < 0)
5484 {
5485 /*
5486 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005487 * Make cursor line the first line in the window. If not enough
5488 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 */
5490 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005491 if (wp->w_wrow >= wp->w_height
5492 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5493 {
5494 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5495 --wp->w_wrow;
5496 while (wp->w_wrow >= wp->w_height)
5497 {
5498 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5499 + win_col_off2(wp);
5500 --wp->w_wrow;
5501 }
5502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 }
5504 else
5505 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005506 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 {
5508#ifdef FEAT_FOLDING
5509 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5510 if (lnum == 1)
5511 {
5512 /* first line in buffer is folded */
5513 line_size = 1;
5514 --sline;
5515 break;
5516 }
5517#endif
5518 --lnum;
5519#ifdef FEAT_DIFF
5520 if (lnum == wp->w_topline)
5521 line_size = plines_win_nofill(wp, lnum, TRUE)
5522 + wp->w_topfill;
5523 else
5524#endif
5525 line_size = plines_win(wp, lnum, TRUE);
5526 sline -= line_size;
5527 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005528
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 if (sline < 0)
5530 {
5531 /*
5532 * Line we want at top would go off top of screen. Use next
5533 * line instead.
5534 */
5535#ifdef FEAT_FOLDING
5536 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5537#endif
5538 lnum++;
5539 wp->w_wrow -= line_size + sline;
5540 }
5541 else if (sline > 0)
5542 {
5543 /* First line of file reached, use that as topline. */
5544 lnum = 1;
5545 wp->w_wrow -= sline;
5546 }
5547 }
5548 set_topline(wp, lnum);
5549 }
5550
5551 if (wp == curwin)
5552 {
5553 if (p_so)
5554 update_topline();
5555 curs_columns(FALSE); /* validate w_wrow */
5556 }
5557 wp->w_prev_fraction_row = wp->w_wrow;
5558
5559 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005560 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561#ifdef FEAT_WINDOWS
5562 wp->w_redr_status = TRUE;
5563#endif
5564 invalidate_botline_win(wp);
5565}
5566
5567#ifdef FEAT_VERTSPLIT
5568/*
5569 * Set the width of a window.
5570 */
5571 static void
5572win_new_width(wp, width)
5573 win_T *wp;
5574 int width;
5575{
5576 wp->w_width = width;
5577 wp->w_lines_valid = 0;
5578 changed_line_abv_curs_win(wp);
5579 invalidate_botline_win(wp);
5580 if (wp == curwin)
5581 {
5582 update_topline();
5583 curs_columns(TRUE); /* validate w_wrow */
5584 }
5585 redraw_win_later(wp, NOT_VALID);
5586 wp->w_redr_status = TRUE;
5587}
5588#endif
5589
5590 void
5591win_comp_scroll(wp)
5592 win_T *wp;
5593{
5594 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5595 if (wp->w_p_scr == 0)
5596 wp->w_p_scr = 1;
5597}
5598
5599/*
5600 * command_height: called whenever p_ch has been changed
5601 */
5602 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005603command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604{
5605#ifdef FEAT_WINDOWS
5606 int h;
5607 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005608 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005610 /* Use the value of p_ch that we remembered. This is needed for when the
5611 * GUI starts up, we can't be sure in what order things happen. And when
5612 * p_ch was changed in another tab page. */
5613 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005614
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 /* Find bottom frame with width of screen. */
5616 frp = lastwin->w_frame;
5617# ifdef FEAT_VERTSPLIT
5618 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5619 frp = frp->fr_parent;
5620# endif
5621
5622 /* Avoid changing the height of a window with 'winfixheight' set. */
5623 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5624 && frp->fr_win->w_p_wfh)
5625 frp = frp->fr_prev;
5626
5627 if (starting != NO_SCREEN)
5628 {
5629 cmdline_row = Rows - p_ch;
5630
5631 if (p_ch > old_p_ch) /* p_ch got bigger */
5632 {
5633 while (p_ch > old_p_ch)
5634 {
5635 if (frp == NULL)
5636 {
5637 EMSG(_(e_noroom));
5638 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005639 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 cmdline_row = Rows - p_ch;
5641 break;
5642 }
5643 h = frp->fr_height - frame_minheight(frp, NULL);
5644 if (h > p_ch - old_p_ch)
5645 h = p_ch - old_p_ch;
5646 old_p_ch += h;
5647 frame_add_height(frp, -h);
5648 frp = frp->fr_prev;
5649 }
5650
5651 /* Recompute window positions. */
5652 (void)win_comp_pos();
5653
5654 /* clear the lines added to cmdline */
5655 if (full_screen)
5656 screen_fill((int)(cmdline_row), (int)Rows, 0,
5657 (int)Columns, ' ', ' ', 0);
5658 msg_row = cmdline_row;
5659 redraw_cmdline = TRUE;
5660 return;
5661 }
5662
5663 if (msg_row < cmdline_row)
5664 msg_row = cmdline_row;
5665 redraw_cmdline = TRUE;
5666 }
5667 frame_add_height(frp, (int)(old_p_ch - p_ch));
5668
5669 /* Recompute window positions. */
5670 if (frp != lastwin->w_frame)
5671 (void)win_comp_pos();
5672#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005674 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675#endif
5676}
5677
5678#if defined(FEAT_WINDOWS) || defined(PROTO)
5679/*
5680 * Resize frame "frp" to be "n" lines higher (negative for less high).
5681 * Also resize the frames it is contained in.
5682 */
5683 static void
5684frame_add_height(frp, n)
5685 frame_T *frp;
5686 int n;
5687{
5688 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5689 for (;;)
5690 {
5691 frp = frp->fr_parent;
5692 if (frp == NULL)
5693 break;
5694 frp->fr_height += n;
5695 }
5696}
5697
5698/*
5699 * Add or remove a status line for the bottom window(s), according to the
5700 * value of 'laststatus'.
5701 */
5702 void
5703last_status(morewin)
5704 int morewin; /* pretend there are two or more windows */
5705{
5706 /* Don't make a difference between horizontal or vertical split. */
5707 last_status_rec(topframe, (p_ls == 2
5708 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5709}
5710
5711 static void
5712last_status_rec(fr, statusline)
5713 frame_T *fr;
5714 int statusline;
5715{
5716 frame_T *fp;
5717 win_T *wp;
5718
5719 if (fr->fr_layout == FR_LEAF)
5720 {
5721 wp = fr->fr_win;
5722 if (wp->w_status_height != 0 && !statusline)
5723 {
5724 /* remove status line */
5725 win_new_height(wp, wp->w_height + 1);
5726 wp->w_status_height = 0;
5727 comp_col();
5728 }
5729 else if (wp->w_status_height == 0 && statusline)
5730 {
5731 /* Find a frame to take a line from. */
5732 fp = fr;
5733 while (fp->fr_height <= frame_minheight(fp, NULL))
5734 {
5735 if (fp == topframe)
5736 {
5737 EMSG(_(e_noroom));
5738 return;
5739 }
5740 /* In a column of frames: go to frame above. If already at
5741 * the top or in a row of frames: go to parent. */
5742 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5743 fp = fp->fr_prev;
5744 else
5745 fp = fp->fr_parent;
5746 }
5747 wp->w_status_height = 1;
5748 if (fp != fr)
5749 {
5750 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5751 frame_fix_height(wp);
5752 (void)win_comp_pos();
5753 }
5754 else
5755 win_new_height(wp, wp->w_height - 1);
5756 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005757 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 }
5759 }
5760#ifdef FEAT_VERTSPLIT
5761 else if (fr->fr_layout == FR_ROW)
5762 {
5763 /* vertically split windows, set status line for each one */
5764 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5765 last_status_rec(fp, statusline);
5766 }
5767#endif
5768 else
5769 {
5770 /* horizontally split window, set status line for last one */
5771 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5772 ;
5773 last_status_rec(fp, statusline);
5774 }
5775}
5776
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005777/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005778 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005779 */
5780 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005781tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005782{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005783#ifdef FEAT_GUI_TABLINE
5784 /* When the GUI has the tabline then this always returns zero. */
5785 if (gui_use_tabline())
5786 return 0;
5787#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005788 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005789 {
5790 case 0: return 0;
5791 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5792 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005793 return 1;
5794}
5795
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796#endif /* FEAT_WINDOWS */
5797
5798#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5799/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005800 * Get the file name at the cursor.
5801 * If Visual mode is active, use the selected text if it's in one line.
5802 * Returns the name in allocated memory, NULL for failure.
5803 */
5804 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005805grab_file_name(count, file_lnum)
5806 long count;
5807 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005808{
5809# ifdef FEAT_VISUAL
5810 if (VIsual_active)
5811 {
5812 int len;
5813 char_u *ptr;
5814
5815 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5816 return NULL;
5817 return find_file_name_in_path(ptr, len,
5818 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5819 }
5820# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005821 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5822 file_lnum);
5823
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005824}
5825
5826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 * Return the file name under or after the cursor.
5828 *
5829 * The 'path' option is searched if the file name is not absolute.
5830 * The string returned has been alloc'ed and should be freed by the caller.
5831 * NULL is returned if the file name or file is not found.
5832 *
5833 * options:
5834 * FNAME_MESS give error messages
5835 * FNAME_EXP expand to path
5836 * FNAME_HYP check for hypertext link
5837 * FNAME_INCL apply "includeexpr"
5838 */
5839 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005840file_name_at_cursor(options, count, file_lnum)
5841 int options;
5842 long count;
5843 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844{
5845 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005846 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5847 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848}
5849
5850/*
5851 * Return the name of the file under or after ptr[col].
5852 * Otherwise like file_name_at_cursor().
5853 */
5854 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005855file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 char_u *line;
5857 int col;
5858 int options;
5859 long count;
5860 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005861 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
5863 char_u *ptr;
5864 int len;
5865
5866 /*
5867 * search forward for what could be the start of a file name
5868 */
5869 ptr = line + col;
5870 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005871 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 if (*ptr == NUL) /* nothing found */
5873 {
5874 if (options & FNAME_MESS)
5875 EMSG(_("E446: No file name under cursor"));
5876 return NULL;
5877 }
5878
5879 /*
5880 * Search backward for first char of the file name.
5881 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5882 */
5883 while (ptr > line)
5884 {
5885#ifdef FEAT_MBYTE
5886 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5887 ptr -= len + 1;
5888 else
5889#endif
5890 if (vim_isfilec(ptr[-1])
5891 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5892 --ptr;
5893 else
5894 break;
5895 }
5896
5897 /*
5898 * Search forward for the last char of the file name.
5899 * Also allow "://" when ':' is not in 'isfname'.
5900 */
5901 len = 0;
5902 while (vim_isfilec(ptr[len])
5903 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5904#ifdef FEAT_MBYTE
5905 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005906 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 else
5908#endif
5909 ++len;
5910
5911 /*
5912 * If there is trailing punctuation, remove it.
5913 * But don't remove "..", could be a directory name.
5914 */
5915 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5916 && ptr[len - 2] != '.')
5917 --len;
5918
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005919 if (file_lnum != NULL)
5920 {
5921 char_u *p;
5922
5923 /* Get the number after the file name and a separator character */
5924 p = ptr + len;
5925 p = skipwhite(p);
5926 if (*p != NUL)
5927 {
5928 if (!isdigit(*p))
5929 ++p; /* skip the separator */
5930 p = skipwhite(p);
5931 if (isdigit(*p))
5932 *file_lnum = (int)getdigits(&p);
5933 }
5934 }
5935
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5937}
5938
5939# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5940static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5941
5942 static char_u *
5943eval_includeexpr(ptr, len)
5944 char_u *ptr;
5945 int len;
5946{
5947 char_u *res;
5948
5949 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005950 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005951 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 set_vim_var_string(VV_FNAME, NULL, 0);
5953 return res;
5954}
5955#endif
5956
5957/*
5958 * Return the name of the file ptr[len] in 'path'.
5959 * Otherwise like file_name_at_cursor().
5960 */
5961 char_u *
5962find_file_name_in_path(ptr, len, options, count, rel_fname)
5963 char_u *ptr;
5964 int len;
5965 int options;
5966 long count;
5967 char_u *rel_fname; /* file we are searching relative to */
5968{
5969 char_u *file_name;
5970 int c;
5971# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5972 char_u *tofree = NULL;
5973
5974 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5975 {
5976 tofree = eval_includeexpr(ptr, len);
5977 if (tofree != NULL)
5978 {
5979 ptr = tofree;
5980 len = (int)STRLEN(ptr);
5981 }
5982 }
5983# endif
5984
5985 if (options & FNAME_EXP)
5986 {
5987 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5988 TRUE, rel_fname);
5989
5990# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5991 /*
5992 * If the file could not be found in a normal way, try applying
5993 * 'includeexpr' (unless done already).
5994 */
5995 if (file_name == NULL
5996 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5997 {
5998 tofree = eval_includeexpr(ptr, len);
5999 if (tofree != NULL)
6000 {
6001 ptr = tofree;
6002 len = (int)STRLEN(ptr);
6003 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6004 TRUE, rel_fname);
6005 }
6006 }
6007# endif
6008 if (file_name == NULL && (options & FNAME_MESS))
6009 {
6010 c = ptr[len];
6011 ptr[len] = NUL;
6012 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6013 ptr[len] = c;
6014 }
6015
6016 /* Repeat finding the file "count" times. This matters when it
6017 * appears several times in the path. */
6018 while (file_name != NULL && --count > 0)
6019 {
6020 vim_free(file_name);
6021 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6022 }
6023 }
6024 else
6025 file_name = vim_strnsave(ptr, len);
6026
6027# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6028 vim_free(tofree);
6029# endif
6030
6031 return file_name;
6032}
6033#endif /* FEAT_SEARCHPATH */
6034
6035/*
6036 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6037 * Also check for ":\\", which MS Internet Explorer accepts, return
6038 * URL_BACKSLASH.
6039 */
6040 static int
6041path_is_url(p)
6042 char_u *p;
6043{
6044 if (STRNCMP(p, "://", (size_t)3) == 0)
6045 return URL_SLASH;
6046 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6047 return URL_BACKSLASH;
6048 return 0;
6049}
6050
6051/*
6052 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6053 * Return URL_BACKSLASH for "name:\\".
6054 * Return zero otherwise.
6055 */
6056 int
6057path_with_url(fname)
6058 char_u *fname;
6059{
6060 char_u *p;
6061
6062 for (p = fname; isalpha(*p); ++p)
6063 ;
6064 return path_is_url(p);
6065}
6066
6067/*
6068 * Return TRUE if "name" is a full (absolute) path name or URL.
6069 */
6070 int
6071vim_isAbsName(name)
6072 char_u *name;
6073{
6074 return (path_with_url(name) != 0 || mch_isFullName(name));
6075}
6076
6077/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006078 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 *
6080 * return FAIL for failure, OK otherwise
6081 */
6082 int
6083vim_FullName(fname, buf, len, force)
6084 char_u *fname, *buf;
6085 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006086 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087{
6088 int retval = OK;
6089 int url;
6090
6091 *buf = NUL;
6092 if (fname == NULL)
6093 return FAIL;
6094
6095 url = path_with_url(fname);
6096 if (!url)
6097 retval = mch_FullName(fname, buf, len, force);
6098 if (url || retval == FAIL)
6099 {
6100 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006101 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 }
6103#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6104 slash_adjust(buf);
6105#endif
6106 return retval;
6107}
6108
6109/*
6110 * Return the minimal number of rows that is needed on the screen to display
6111 * the current number of windows.
6112 */
6113 int
6114min_rows()
6115{
6116 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006117#ifdef FEAT_WINDOWS
6118 tabpage_T *tp;
6119 int n;
6120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
6122 if (firstwin == NULL) /* not initialized yet */
6123 return MIN_LINES;
6124
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006126 total = 0;
6127 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6128 {
6129 n = frame_minheight(tp->tp_topframe, NULL);
6130 if (total < n)
6131 total = n;
6132 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006133 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006135 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006137 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 return total;
6139}
6140
6141/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006142 * Return TRUE if there is only one window (in the current tab page), not
6143 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006144 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 */
6146 int
6147only_one_window()
6148{
6149#ifdef FEAT_WINDOWS
6150 int count = 0;
6151 win_T *wp;
6152
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006153 /* If there is another tab page there always is another window. */
6154 if (first_tabpage->tp_next != NULL)
6155 return FALSE;
6156
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006158 if ((!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159# ifdef FEAT_QUICKFIX
6160 || wp->w_p_pvw
6161# endif
6162 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006163# ifdef FEAT_AUTOCMD
6164 && wp != aucmd_win
6165# endif
6166 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 ++count;
6168 return (count <= 1);
6169#else
6170 return TRUE;
6171#endif
6172}
6173
6174#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6175/*
6176 * Correct the cursor line number in other windows. Used after changing the
6177 * current buffer, and before applying autocommands.
6178 * When "do_curwin" is TRUE, also check current window.
6179 */
6180 void
6181check_lnums(do_curwin)
6182 int do_curwin;
6183{
6184 win_T *wp;
6185
6186#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006187 tabpage_T *tp;
6188
6189 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6191#else
6192 wp = curwin;
6193 if (do_curwin)
6194#endif
6195 {
6196 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6197 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6198 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6199 wp->w_topline = curbuf->b_ml.ml_line_count;
6200 }
6201}
6202#endif
6203
6204#if defined(FEAT_WINDOWS) || defined(PROTO)
6205
6206/*
6207 * A snapshot of the window sizes, to restore them after closing the help
6208 * window.
6209 * Only these fields are used:
6210 * fr_layout
6211 * fr_width
6212 * fr_height
6213 * fr_next
6214 * fr_child
6215 * fr_win (only valid for the old curwin, NULL otherwise)
6216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
6218/*
6219 * Create a snapshot of the current frame sizes.
6220 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006221 void
6222make_snapshot(idx)
6223 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006225 clear_snapshot(curtab, idx);
6226 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227}
6228
6229 static void
6230make_snapshot_rec(fr, frp)
6231 frame_T *fr;
6232 frame_T **frp;
6233{
6234 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6235 if (*frp == NULL)
6236 return;
6237 (*frp)->fr_layout = fr->fr_layout;
6238# ifdef FEAT_VERTSPLIT
6239 (*frp)->fr_width = fr->fr_width;
6240# endif
6241 (*frp)->fr_height = fr->fr_height;
6242 if (fr->fr_next != NULL)
6243 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6244 if (fr->fr_child != NULL)
6245 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6246 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6247 (*frp)->fr_win = curwin;
6248}
6249
6250/*
6251 * Remove any existing snapshot.
6252 */
6253 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006254clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006255 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006256 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006258 clear_snapshot_rec(tp->tp_snapshot[idx]);
6259 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260}
6261
6262 static void
6263clear_snapshot_rec(fr)
6264 frame_T *fr;
6265{
6266 if (fr != NULL)
6267 {
6268 clear_snapshot_rec(fr->fr_next);
6269 clear_snapshot_rec(fr->fr_child);
6270 vim_free(fr);
6271 }
6272}
6273
6274/*
6275 * Restore a previously created snapshot, if there is any.
6276 * This is only done if the screen size didn't change and the window layout is
6277 * still the same.
6278 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006279 void
6280restore_snapshot(idx, close_curwin)
6281 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 int close_curwin; /* closing current window */
6283{
6284 win_T *wp;
6285
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006286 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006288 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006290 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6291 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006293 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 win_comp_pos();
6295 if (wp != NULL && close_curwin)
6296 win_goto(wp);
6297 redraw_all_later(CLEAR);
6298 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006299 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300}
6301
6302/*
6303 * Check if frames "sn" and "fr" have the same layout, same following frames
6304 * and same children.
6305 */
6306 static int
6307check_snapshot_rec(sn, fr)
6308 frame_T *sn;
6309 frame_T *fr;
6310{
6311 if (sn->fr_layout != fr->fr_layout
6312 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6313 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6314 || (sn->fr_next != NULL
6315 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6316 || (sn->fr_child != NULL
6317 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6318 return FAIL;
6319 return OK;
6320}
6321
6322/*
6323 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6324 * following frames and children.
6325 * Returns a pointer to the old current window, or NULL.
6326 */
6327 static win_T *
6328restore_snapshot_rec(sn, fr)
6329 frame_T *sn;
6330 frame_T *fr;
6331{
6332 win_T *wp = NULL;
6333 win_T *wp2;
6334
6335 fr->fr_height = sn->fr_height;
6336# ifdef FEAT_VERTSPLIT
6337 fr->fr_width = sn->fr_width;
6338# endif
6339 if (fr->fr_layout == FR_LEAF)
6340 {
6341 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6342# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006343 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344# endif
6345 wp = sn->fr_win;
6346 }
6347 if (sn->fr_next != NULL)
6348 {
6349 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6350 if (wp2 != NULL)
6351 wp = wp2;
6352 }
6353 if (sn->fr_child != NULL)
6354 {
6355 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6356 if (wp2 != NULL)
6357 wp = wp2;
6358 }
6359 return wp;
6360}
6361
6362#endif
6363
6364#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6365/*
6366 * Return TRUE if there is any vertically split window.
6367 */
6368 int
6369win_hasvertsplit()
6370{
6371 frame_T *fr;
6372
6373 if (topframe->fr_layout == FR_ROW)
6374 return TRUE;
6375
6376 if (topframe->fr_layout == FR_COL)
6377 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6378 if (fr->fr_layout == FR_ROW)
6379 return TRUE;
6380
6381 return FALSE;
6382}
6383#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006384
6385#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6386/*
6387 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006388 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006389 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6390 * If no particular ID is desired, -1 must be specified for 'id'.
6391 * Return ID of added match, -1 on failure.
6392 */
6393 int
6394match_add(wp, grp, pat, prio, id)
6395 win_T *wp;
6396 char_u *grp;
6397 char_u *pat;
6398 int prio;
6399 int id;
6400{
6401 matchitem_T *cur;
6402 matchitem_T *prev;
6403 matchitem_T *m;
6404 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006405 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006406
6407 if (*grp == NUL || *pat == NUL)
6408 return -1;
6409 if (id < -1 || id == 0)
6410 {
6411 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6412 return -1;
6413 }
6414 if (id != -1)
6415 {
6416 cur = wp->w_match_head;
6417 while (cur != NULL)
6418 {
6419 if (cur->id == id)
6420 {
6421 EMSGN("E801: ID already taken: %ld", id);
6422 return -1;
6423 }
6424 cur = cur->next;
6425 }
6426 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006427 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006428 {
6429 EMSG2(_(e_nogroup), grp);
6430 return -1;
6431 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006432 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006433 {
6434 EMSG2(_(e_invarg2), pat);
6435 return -1;
6436 }
6437
6438 /* Find available match ID. */
6439 while (id == -1)
6440 {
6441 cur = wp->w_match_head;
6442 while (cur != NULL && cur->id != wp->w_next_match_id)
6443 cur = cur->next;
6444 if (cur == NULL)
6445 id = wp->w_next_match_id;
6446 wp->w_next_match_id++;
6447 }
6448
6449 /* Build new match. */
6450 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6451 m->id = id;
6452 m->priority = prio;
6453 m->pattern = vim_strsave(pat);
6454 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006455 m->match.regprog = regprog;
6456 m->match.rmm_ic = FALSE;
6457 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006458
6459 /* Insert new match. The match list is in ascending order with regard to
6460 * the match priorities. */
6461 cur = wp->w_match_head;
6462 prev = cur;
6463 while (cur != NULL && prio >= cur->priority)
6464 {
6465 prev = cur;
6466 cur = cur->next;
6467 }
6468 if (cur == prev)
6469 wp->w_match_head = m;
6470 else
6471 prev->next = m;
6472 m->next = cur;
6473
6474 redraw_later(SOME_VALID);
6475 return id;
6476}
6477
6478/*
6479 * Delete match with ID 'id' in the match list of window 'wp'.
6480 * Print error messages if 'perr' is TRUE.
6481 */
6482 int
6483match_delete(wp, id, perr)
6484 win_T *wp;
6485 int id;
6486 int perr;
6487{
6488 matchitem_T *cur = wp->w_match_head;
6489 matchitem_T *prev = cur;
6490
6491 if (id < 1)
6492 {
6493 if (perr == TRUE)
6494 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6495 id);
6496 return -1;
6497 }
6498 while (cur != NULL && cur->id != id)
6499 {
6500 prev = cur;
6501 cur = cur->next;
6502 }
6503 if (cur == NULL)
6504 {
6505 if (perr == TRUE)
6506 EMSGN("E803: ID not found: %ld", id);
6507 return -1;
6508 }
6509 if (cur == prev)
6510 wp->w_match_head = cur->next;
6511 else
6512 prev->next = cur->next;
6513 vim_free(cur->match.regprog);
6514 vim_free(cur->pattern);
6515 vim_free(cur);
6516 redraw_later(SOME_VALID);
6517 return 0;
6518}
6519
6520/*
6521 * Delete all matches in the match list of window 'wp'.
6522 */
6523 void
6524clear_matches(wp)
6525 win_T *wp;
6526{
6527 matchitem_T *m;
6528
6529 while (wp->w_match_head != NULL)
6530 {
6531 m = wp->w_match_head->next;
6532 vim_free(wp->w_match_head->match.regprog);
6533 vim_free(wp->w_match_head->pattern);
6534 vim_free(wp->w_match_head);
6535 wp->w_match_head = m;
6536 }
6537 redraw_later(SOME_VALID);
6538}
6539
6540/*
6541 * Get match from ID 'id' in window 'wp'.
6542 * Return NULL if match not found.
6543 */
6544 matchitem_T *
6545get_match(wp, id)
6546 win_T *wp;
6547 int id;
6548{
6549 matchitem_T *cur = wp->w_match_head;
6550
6551 while (cur != NULL && cur->id != id)
6552 cur = cur->next;
6553 return cur;
6554}
6555#endif