blob: 2c780dfb5cdbac46a1c78a433c79eae569674d19 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#include "vim.h"
11
Bram Moolenaar071d4272004-06-13 20:20:40 +000012static int path_is_url __ARGS((char_u *p));
13#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar884ae642009-02-22 01:37:59 +000014static void win_init __ARGS((win_T *newp, win_T *oldp, int flags));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000015static void win_init_some __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
17static void frame_setheight __ARGS((frame_T *curfrp, int height));
18#ifdef FEAT_VERTSPLIT
19static void frame_setwidth __ARGS((frame_T *curfrp, int width));
20#endif
21static void win_exchange __ARGS((long));
22static void win_rotate __ARGS((int, int));
23static void win_totop __ARGS((int size, int flags));
24static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000025static int last_window __ARGS((void));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000026static int one_window __ARGS((void));
Bram Moolenaarf740b292006-02-16 22:11:02 +000027static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
Bram Moolenaarf740b292006-02-16 22:11:02 +000028static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000029static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000030static win_T *frame2win __ARGS((frame_T *frp));
31static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
32static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
33static int frame_fixed_height __ARGS((frame_T *frp));
34#ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000035static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000036static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000037static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000038static void frame_add_vsep __ARGS((frame_T *frp));
39static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
40static void frame_fix_width __ARGS((win_T *wp));
41#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000042#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000043static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000044static void new_frame __ARGS((win_T *wp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000045#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000046static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000047static int leave_tabpage __ARGS((buf_T *new_curbuf));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000048static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000049static void frame_fix_height __ARGS((win_T *wp));
50static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
51static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
Bram Moolenaarf740b292006-02-16 22:11:02 +000052static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000053static void frame_append __ARGS((frame_T *after, frame_T *frp));
54static void frame_insert __ARGS((frame_T *before, frame_T *frp));
55static void frame_remove __ARGS((frame_T *frp));
56#ifdef FEAT_VERTSPLIT
57static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static void win_goto_ver __ARGS((int up, long count));
59static void win_goto_hor __ARGS((int left, long count));
60#endif
61static void frame_add_height __ARGS((frame_T *frp, int n));
62static void last_status_rec __ARGS((frame_T *fr, int statusline));
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000065static void clear_snapshot __ARGS((tabpage_T *tp, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static void clear_snapshot_rec __ARGS((frame_T *fr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000067static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
68static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
69
70#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000071
Bram Moolenaar746ebd32009-06-16 14:01:43 +000072static win_T *win_alloc __ARGS((win_T *after, int hidden));
Bram Moolenaar071d4272004-06-13 20:20:40 +000073static void win_new_height __ARGS((win_T *, int));
74
75#define URL_SLASH 1 /* path_is_url() has found "://" */
76#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
77
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000078#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Bram Moolenaar05159a02005-02-26 23:04:13 +000080#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000081# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000082#else
83# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000084#endif
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000087
88static char *m_onlyone = N_("Already only one window");
89
Bram Moolenaar071d4272004-06-13 20:20:40 +000090/*
91 * all CTRL-W window commands are handled here, called from normal_cmd().
92 */
93 void
94do_window(nchar, Prenum, xchar)
95 int nchar;
96 long Prenum;
97 int xchar; /* extra char from ":wincmd gx" or NUL */
98{
99 long Prenum1;
100 win_T *wp;
101#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
102 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000103 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105#ifdef FEAT_FIND_ID
106 int type = FIND_DEFINE;
107 int len;
108#endif
109 char_u cbuf[40];
110
111 if (Prenum == 0)
112 Prenum1 = 1;
113 else
114 Prenum1 = Prenum;
115
116#ifdef FEAT_CMDWIN
117# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
118#else
119# define CHECK_CMDWIN
120#endif
121
122 switch (nchar)
123 {
124/* split current window in two parts, horizontally */
125 case 'S':
126 case Ctrl_S:
127 case 's':
128 CHECK_CMDWIN
129#ifdef FEAT_VISUAL
130 reset_VIsual_and_resel(); /* stop Visual mode */
131#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000132#ifdef FEAT_QUICKFIX
133 /* When splitting the quickfix window open a new buffer in it,
134 * don't replicate the quickfix buffer. */
135 if (bt_quickfix(curbuf))
136 goto newwindow;
137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#ifdef FEAT_GUI
139 need_mouse_correct = TRUE;
140#endif
141 win_split((int)Prenum, 0);
142 break;
143
144#ifdef FEAT_VERTSPLIT
145/* split current window in two parts, vertically */
146 case Ctrl_V:
147 case 'v':
148 CHECK_CMDWIN
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000149# ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000151# endif
152# ifdef FEAT_QUICKFIX
153 /* When splitting the quickfix window open a new buffer in it,
154 * don't replicate the quickfix buffer. */
155 if (bt_quickfix(curbuf))
156 goto newwindow;
157# endif
158# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 win_split((int)Prenum, WSP_VERT);
162 break;
163#endif
164
165/* split current window and edit alternate file */
166 case Ctrl_HAT:
167 case '^':
168 CHECK_CMDWIN
169#ifdef FEAT_VISUAL
170 reset_VIsual_and_resel(); /* stop Visual mode */
171#endif
172 STRCPY(cbuf, "split #");
173 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000174 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
175 "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 do_cmdline_cmd(cbuf);
177 break;
178
179/* open new window */
180 case Ctrl_N:
181 case 'n':
182 CHECK_CMDWIN
183#ifdef FEAT_VISUAL
184 reset_VIsual_and_resel(); /* stop Visual mode */
185#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000186#ifdef FEAT_QUICKFIX
187newwindow:
188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000190 /* window height */
191 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 else
193 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000194#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
195 if (nchar == 'v' || nchar == Ctrl_V)
196 STRCAT(cbuf, "v");
197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 STRCAT(cbuf, "new");
199 do_cmdline_cmd(cbuf);
200 break;
201
202/* quit current window */
203 case Ctrl_Q:
204 case 'q':
205#ifdef FEAT_VISUAL
206 reset_VIsual_and_resel(); /* stop Visual mode */
207#endif
208 do_cmdline_cmd((char_u *)"quit");
209 break;
210
211/* close current window */
212 case Ctrl_C:
213 case 'c':
214#ifdef FEAT_VISUAL
215 reset_VIsual_and_resel(); /* stop Visual mode */
216#endif
217 do_cmdline_cmd((char_u *)"close");
218 break;
219
220#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
221/* close preview window */
222 case Ctrl_Z:
223 case 'z':
224 CHECK_CMDWIN
225#ifdef FEAT_VISUAL
226 reset_VIsual_and_resel(); /* stop Visual mode */
227#endif
228 do_cmdline_cmd((char_u *)"pclose");
229 break;
230
231/* cursor to preview window */
232 case 'P':
233 for (wp = firstwin; wp != NULL; wp = wp->w_next)
234 if (wp->w_p_pvw)
235 break;
236 if (wp == NULL)
237 EMSG(_("E441: There is no preview window"));
238 else
239 win_goto(wp);
240 break;
241#endif
242
243/* close all but current window */
244 case Ctrl_O:
245 case 'o':
246 CHECK_CMDWIN
247#ifdef FEAT_VISUAL
248 reset_VIsual_and_resel(); /* stop Visual mode */
249#endif
250 do_cmdline_cmd((char_u *)"only");
251 break;
252
253/* cursor to next window with wrap around */
254 case Ctrl_W:
255 case 'w':
256/* cursor to previous window with wrap around */
257 case 'W':
258 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000259 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 beep_flush();
261 else
262 {
263 if (Prenum) /* go to specified window */
264 {
265 for (wp = firstwin; --Prenum > 0; )
266 {
267 if (wp->w_next == NULL)
268 break;
269 else
270 wp = wp->w_next;
271 }
272 }
273 else
274 {
275 if (nchar == 'W') /* go to previous window */
276 {
277 wp = curwin->w_prev;
278 if (wp == NULL)
279 wp = lastwin; /* wrap around */
280 }
281 else /* go to next window */
282 {
283 wp = curwin->w_next;
284 if (wp == NULL)
285 wp = firstwin; /* wrap around */
286 }
287 }
288 win_goto(wp);
289 }
290 break;
291
292/* cursor to window below */
293 case 'j':
294 case K_DOWN:
295 case Ctrl_J:
296 CHECK_CMDWIN
297#ifdef FEAT_VERTSPLIT
298 win_goto_ver(FALSE, Prenum1);
299#else
300 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
301 wp = wp->w_next)
302 ;
303 win_goto(wp);
304#endif
305 break;
306
307/* cursor to window above */
308 case 'k':
309 case K_UP:
310 case Ctrl_K:
311 CHECK_CMDWIN
312#ifdef FEAT_VERTSPLIT
313 win_goto_ver(TRUE, Prenum1);
314#else
315 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
316 wp = wp->w_prev)
317 ;
318 win_goto(wp);
319#endif
320 break;
321
322#ifdef FEAT_VERTSPLIT
323/* cursor to left window */
324 case 'h':
325 case K_LEFT:
326 case Ctrl_H:
327 case K_BS:
328 CHECK_CMDWIN
329 win_goto_hor(TRUE, Prenum1);
330 break;
331
332/* cursor to right window */
333 case 'l':
334 case K_RIGHT:
335 case Ctrl_L:
336 CHECK_CMDWIN
337 win_goto_hor(FALSE, Prenum1);
338 break;
339#endif
340
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000341/* move window to new tab page */
342 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000343 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000344 MSG(_(m_onlyone));
345 else
346 {
347 tabpage_T *oldtab = curtab;
348 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000349
350 /* First create a new tab with the window, then go back to
351 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000352 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000353 if (win_new_tabpage((int)Prenum) == OK
354 && valid_tabpage(oldtab))
355 {
356 newtab = curtab;
357 goto_tabpage_tp(oldtab);
358 if (curwin == wp)
359 win_close(curwin, FALSE);
360 if (valid_tabpage(newtab))
361 goto_tabpage_tp(newtab);
362 }
363 }
364 break;
365
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366/* cursor to top-left window */
367 case 't':
368 case Ctrl_T:
369 win_goto(firstwin);
370 break;
371
372/* cursor to bottom-right window */
373 case 'b':
374 case Ctrl_B:
375 win_goto(lastwin);
376 break;
377
378/* cursor to last accessed (previous) window */
379 case 'p':
380 case Ctrl_P:
381 if (prevwin == NULL)
382 beep_flush();
383 else
384 win_goto(prevwin);
385 break;
386
387/* exchange current and next window */
388 case 'x':
389 case Ctrl_X:
390 CHECK_CMDWIN
391 win_exchange(Prenum);
392 break;
393
394/* rotate windows downwards */
395 case Ctrl_R:
396 case 'r':
397 CHECK_CMDWIN
398#ifdef FEAT_VISUAL
399 reset_VIsual_and_resel(); /* stop Visual mode */
400#endif
401 win_rotate(FALSE, (int)Prenum1); /* downwards */
402 break;
403
404/* rotate windows upwards */
405 case 'R':
406 CHECK_CMDWIN
407#ifdef FEAT_VISUAL
408 reset_VIsual_and_resel(); /* stop Visual mode */
409#endif
410 win_rotate(TRUE, (int)Prenum1); /* upwards */
411 break;
412
413/* move window to the very top/bottom/left/right */
414 case 'K':
415 case 'J':
416#ifdef FEAT_VERTSPLIT
417 case 'H':
418 case 'L':
419#endif
420 CHECK_CMDWIN
421 win_totop((int)Prenum,
422 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
423 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
424 break;
425
426/* make all windows the same height */
427 case '=':
428#ifdef FEAT_GUI
429 need_mouse_correct = TRUE;
430#endif
431 win_equal(NULL, FALSE, 'b');
432 break;
433
434/* increase current window height */
435 case '+':
436#ifdef FEAT_GUI
437 need_mouse_correct = TRUE;
438#endif
439 win_setheight(curwin->w_height + (int)Prenum1);
440 break;
441
442/* decrease current window height */
443 case '-':
444#ifdef FEAT_GUI
445 need_mouse_correct = TRUE;
446#endif
447 win_setheight(curwin->w_height - (int)Prenum1);
448 break;
449
450/* set current window height */
451 case Ctrl__:
452 case '_':
453#ifdef FEAT_GUI
454 need_mouse_correct = TRUE;
455#endif
456 win_setheight(Prenum ? (int)Prenum : 9999);
457 break;
458
459#ifdef FEAT_VERTSPLIT
460/* increase current window width */
461 case '>':
462#ifdef FEAT_GUI
463 need_mouse_correct = TRUE;
464#endif
465 win_setwidth(curwin->w_width + (int)Prenum1);
466 break;
467
468/* decrease current window width */
469 case '<':
470#ifdef FEAT_GUI
471 need_mouse_correct = TRUE;
472#endif
473 win_setwidth(curwin->w_width - (int)Prenum1);
474 break;
475
476/* set current window width */
477 case '|':
478#ifdef FEAT_GUI
479 need_mouse_correct = TRUE;
480#endif
481 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
482 break;
483#endif
484
485/* jump to tag and split window if tag exists (in preview window) */
486#if defined(FEAT_QUICKFIX)
487 case '}':
488 CHECK_CMDWIN
489 if (Prenum)
490 g_do_tagpreview = Prenum;
491 else
492 g_do_tagpreview = p_pvh;
493 /*FALLTHROUGH*/
494#endif
495 case ']':
496 case Ctrl_RSB:
497 CHECK_CMDWIN
498#ifdef FEAT_VISUAL
499 reset_VIsual_and_resel(); /* stop Visual mode */
500#endif
501 if (Prenum)
502 postponed_split = Prenum;
503 else
504 postponed_split = -1;
505
506 /* Execute the command right here, required when
507 * "wincmd ]" was used in a function. */
508 do_nv_ident(Ctrl_RSB, NUL);
509 break;
510
511#ifdef FEAT_SEARCHPATH
512/* edit file name under cursor in a new window */
513 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000514 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000516wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000518
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000519 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 if (ptr != NULL)
521 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000522# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000524# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 setpcmark();
526 if (win_split(0, 0) == OK)
527 {
528# ifdef FEAT_SCROLLBIND
529 curwin->w_p_scb = FALSE;
530# endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000531 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
532 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000533 if (nchar == 'F' && lnum >= 0)
534 {
535 curwin->w_cursor.lnum = lnum;
536 check_cursor_lnum();
537 beginline(BL_SOL | BL_FIX);
538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 }
540 vim_free(ptr);
541 }
542 break;
543#endif
544
545#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000546/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 * new window -- webb
548 */
549 case 'i': /* Go to any match */
550 case Ctrl_I:
551 type = FIND_ANY;
552 /* FALLTHROUGH */
553 case 'd': /* Go to definition, using 'define' */
554 case Ctrl_D:
555 CHECK_CMDWIN
556 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
557 break;
558 find_pattern_in_path(ptr, 0, len, TRUE,
559 Prenum == 0 ? TRUE : FALSE, type,
560 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
561 curwin->w_set_curswant = TRUE;
562 break;
563#endif
564
Bram Moolenaar05159a02005-02-26 23:04:13 +0000565 case K_KENTER:
566 case CAR:
567#if defined(FEAT_QUICKFIX)
568 /*
569 * In a quickfix window a <CR> jumps to the error under the
570 * cursor in a new window.
571 */
572 if (bt_quickfix(curbuf))
573 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000574 sprintf((char *)cbuf, "split +%ld%s",
575 (long)curwin->w_cursor.lnum,
576 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000577 do_cmdline_cmd(cbuf);
578 }
579#endif
580 break;
581
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583/* CTRL-W g extended commands */
584 case 'g':
585 case Ctrl_G:
586 CHECK_CMDWIN
587#ifdef USE_ON_FLY_SCROLL
588 dont_scroll = TRUE; /* disallow scrolling here */
589#endif
590 ++no_mapping;
591 ++allow_keys; /* no mapping for xchar, but allow key codes */
592 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000593 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 --no_mapping;
596 --allow_keys;
597#ifdef FEAT_CMDL_INFO
598 (void)add_to_showcmd(xchar);
599#endif
600 switch (xchar)
601 {
602#if defined(FEAT_QUICKFIX)
603 case '}':
604 xchar = Ctrl_RSB;
605 if (Prenum)
606 g_do_tagpreview = Prenum;
607 else
608 g_do_tagpreview = p_pvh;
609 /*FALLTHROUGH*/
610#endif
611 case ']':
612 case Ctrl_RSB:
613#ifdef FEAT_VISUAL
614 reset_VIsual_and_resel(); /* stop Visual mode */
615#endif
616 if (Prenum)
617 postponed_split = Prenum;
618 else
619 postponed_split = -1;
620
621 /* Execute the command right here, required when
622 * "wincmd g}" was used in a function. */
623 do_nv_ident('g', xchar);
624 break;
625
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000626#ifdef FEAT_SEARCHPATH
627 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000628 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100629 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000630 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000631 goto wingotofile;
632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 default:
634 beep_flush();
635 break;
636 }
637 break;
638
639 default: beep_flush();
640 break;
641 }
642}
643
644/*
645 * split the current window, implements CTRL-W s and :split
646 *
647 * "size" is the height or width for the new window, 0 to use half of current
648 * height or width.
649 *
650 * "flags":
651 * WSP_ROOM: require enough room for new window
652 * WSP_VERT: vertical split.
653 * WSP_TOP: open window at the top-left of the shell (help window).
654 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
655 * WSP_HELP: creating the help window, keep layout snapshot
656 *
657 * return FAIL for failure, OK otherwise
658 */
659 int
660win_split(size, flags)
661 int size;
662 int flags;
663{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000664 /* When the ":tab" modifier was used open a new tab page instead. */
665 if (may_open_tabpage() == OK)
666 return OK;
667
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 /* Add flags from ":vertical", ":topleft" and ":botright". */
669 flags |= cmdmod.split;
670 if ((flags & WSP_TOP) && (flags & WSP_BOT))
671 {
672 EMSG(_("E442: Can't split topleft and botright at the same time"));
673 return FAIL;
674 }
675
676 /* When creating the help window make a snapshot of the window layout.
677 * Otherwise clear the snapshot, it's now invalid. */
678 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000679 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000681 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682
683 return win_split_ins(size, flags, NULL, 0);
684}
685
686/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000687 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 * When "newwin" is not NULL: insert this window at the far
689 * top/left/right/bottom.
690 * return FAIL for failure, OK otherwise
691 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000692 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693win_split_ins(size, flags, newwin, dir)
694 int size;
695 int flags;
696 win_T *newwin;
697 int dir;
698{
699 win_T *wp = newwin;
700 win_T *oldwin;
701 int new_size = size;
702 int i;
703 int need_status = 0;
704 int do_equal = FALSE;
705 int needed;
706 int available;
707 int oldwin_height = 0;
708 int layout;
709 frame_T *frp, *curfrp;
710 int before;
711
712 if (flags & WSP_TOP)
713 oldwin = firstwin;
714 else if (flags & WSP_BOT)
715 oldwin = lastwin;
716 else
717 oldwin = curwin;
718
719 /* add a status line when p_ls == 1 and splitting the first window */
720 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
721 {
722 if (oldwin->w_height <= p_wmh && newwin == NULL)
723 {
724 EMSG(_(e_noroom));
725 return FAIL;
726 }
727 need_status = STATUS_HEIGHT;
728 }
729
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000730#ifdef FEAT_GUI
731 /* May be needed for the scrollbars that are going to change. */
732 if (gui.in_use)
733 out_flush();
734#endif
735
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#ifdef FEAT_VERTSPLIT
737 if (flags & WSP_VERT)
738 {
739 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
741 /*
742 * Check if we are able to split the current window and compute its
743 * width.
744 */
745 needed = p_wmw + 1;
746 if (flags & WSP_ROOM)
747 needed += p_wiw - p_wmw;
748 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
749 {
750 available = topframe->fr_width;
751 needed += frame_minwidth(topframe, NULL);
752 }
753 else
754 available = oldwin->w_width;
755 if (available < needed && newwin == NULL)
756 {
757 EMSG(_(e_noroom));
758 return FAIL;
759 }
760 if (new_size == 0)
761 new_size = oldwin->w_width / 2;
762 if (new_size > oldwin->w_width - p_wmw - 1)
763 new_size = oldwin->w_width - p_wmw - 1;
764 if (new_size < p_wmw)
765 new_size = p_wmw;
766
767 /* if it doesn't fit in the current window, need win_equal() */
768 if (oldwin->w_width - new_size - 1 < p_wmw)
769 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000770
771 /* We don't like to take lines for the new window from a
772 * 'winfixwidth' window. Take them from a window to the left or right
773 * instead, if possible. */
774 if (oldwin->w_p_wfw)
775 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000776
777 /* Only make all windows the same width if one of them (except oldwin)
778 * is wider than one of the split windows. */
779 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
780 && oldwin->w_frame->fr_parent != NULL)
781 {
782 frp = oldwin->w_frame->fr_parent->fr_child;
783 while (frp != NULL)
784 {
785 if (frp->fr_win != oldwin && frp->fr_win != NULL
786 && (frp->fr_win->w_width > new_size
787 || frp->fr_win->w_width > oldwin->w_width
788 - new_size - STATUS_HEIGHT))
789 {
790 do_equal = TRUE;
791 break;
792 }
793 frp = frp->fr_next;
794 }
795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 }
797 else
798#endif
799 {
800 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
802 /*
803 * Check if we are able to split the current window and compute its
804 * height.
805 */
806 needed = p_wmh + STATUS_HEIGHT + need_status;
807 if (flags & WSP_ROOM)
808 needed += p_wh - p_wmh;
809 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
810 {
811 available = topframe->fr_height;
812 needed += frame_minheight(topframe, NULL);
813 }
814 else
815 {
816 available = oldwin->w_height;
817 needed += p_wmh;
818 }
819 if (available < needed && newwin == NULL)
820 {
821 EMSG(_(e_noroom));
822 return FAIL;
823 }
824 oldwin_height = oldwin->w_height;
825 if (need_status)
826 {
827 oldwin->w_status_height = STATUS_HEIGHT;
828 oldwin_height -= STATUS_HEIGHT;
829 }
830 if (new_size == 0)
831 new_size = oldwin_height / 2;
832
833 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
834 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
835 if (new_size < p_wmh)
836 new_size = p_wmh;
837
838 /* if it doesn't fit in the current window, need win_equal() */
839 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
840 do_equal = TRUE;
841
842 /* We don't like to take lines for the new window from a
843 * 'winfixheight' window. Take them from a window above or below
844 * instead, if possible. */
845 if (oldwin->w_p_wfh)
846 {
847 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
848 oldwin);
849 oldwin_height = oldwin->w_height;
850 if (need_status)
851 oldwin_height -= STATUS_HEIGHT;
852 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000853
854 /* Only make all windows the same height if one of them (except oldwin)
855 * is higher than one of the split windows. */
856 if (!do_equal && p_ea && size == 0
857#ifdef FEAT_VERTSPLIT
858 && *p_ead != 'h'
859#endif
860 && oldwin->w_frame->fr_parent != NULL)
861 {
862 frp = oldwin->w_frame->fr_parent->fr_child;
863 while (frp != NULL)
864 {
865 if (frp->fr_win != oldwin && frp->fr_win != NULL
866 && (frp->fr_win->w_height > new_size
867 || frp->fr_win->w_height > oldwin_height - new_size
868 - STATUS_HEIGHT))
869 {
870 do_equal = TRUE;
871 break;
872 }
873 frp = frp->fr_next;
874 }
875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 }
877
878 /*
879 * allocate new window structure and link it in the window list
880 */
881 if ((flags & WSP_TOP) == 0
882 && ((flags & WSP_BOT)
883 || (flags & WSP_BELOW)
884 || (!(flags & WSP_ABOVE)
885 && (
886#ifdef FEAT_VERTSPLIT
887 (flags & WSP_VERT) ? p_spr :
888#endif
889 p_sb))))
890 {
891 /* new window below/right of current one */
892 if (newwin == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000893 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 else
895 win_append(oldwin, wp);
896 }
897 else
898 {
899 if (newwin == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000900 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 else
902 win_append(oldwin->w_prev, wp);
903 }
904
905 if (newwin == NULL)
906 {
907 if (wp == NULL)
908 return FAIL;
909
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000910 new_frame(wp);
911 if (wp->w_frame == NULL)
912 {
913 win_free(wp, NULL);
914 return FAIL;
915 }
916
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000917 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000918 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
920
921 /*
922 * Reorganise the tree of frames to insert the new window.
923 */
924 if (flags & (WSP_TOP | WSP_BOT))
925 {
926#ifdef FEAT_VERTSPLIT
927 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
928 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
929#else
930 if (topframe->fr_layout == FR_COL)
931#endif
932 {
933 curfrp = topframe->fr_child;
934 if (flags & WSP_BOT)
935 while (curfrp->fr_next != NULL)
936 curfrp = curfrp->fr_next;
937 }
938 else
939 curfrp = topframe;
940 before = (flags & WSP_TOP);
941 }
942 else
943 {
944 curfrp = oldwin->w_frame;
945 if (flags & WSP_BELOW)
946 before = FALSE;
947 else if (flags & WSP_ABOVE)
948 before = TRUE;
949 else
950#ifdef FEAT_VERTSPLIT
951 if (flags & WSP_VERT)
952 before = !p_spr;
953 else
954#endif
955 before = !p_sb;
956 }
957 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
958 {
959 /* Need to create a new frame in the tree to make a branch. */
960 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
961 *frp = *curfrp;
962 curfrp->fr_layout = layout;
963 frp->fr_parent = curfrp;
964 frp->fr_next = NULL;
965 frp->fr_prev = NULL;
966 curfrp->fr_child = frp;
967 curfrp->fr_win = NULL;
968 curfrp = frp;
969 if (frp->fr_win != NULL)
970 oldwin->w_frame = frp;
971 else
972 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
973 frp->fr_parent = curfrp;
974 }
975
976 if (newwin == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000977 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 else
979 frp = newwin->w_frame;
980 frp->fr_parent = curfrp->fr_parent;
981
982 /* Insert the new frame at the right place in the frame list. */
983 if (before)
984 frame_insert(curfrp, frp);
985 else
986 frame_append(curfrp, frp);
987
988#ifdef FEAT_VERTSPLIT
989 if (flags & WSP_VERT)
990 {
991 wp->w_p_scr = curwin->w_p_scr;
992 if (need_status)
993 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +0100994 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 oldwin->w_status_height = need_status;
996 }
997 if (flags & (WSP_TOP | WSP_BOT))
998 {
999 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001000 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001001 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 wp->w_status_height = (p_ls > 0);
1003 }
1004 else
1005 {
1006 /* height and row of new window is same as current window */
1007 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001008 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 wp->w_status_height = oldwin->w_status_height;
1010 }
1011 frp->fr_height = curfrp->fr_height;
1012
1013 /* "new_size" of the current window goes to the new window, use
1014 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001015 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (before)
1017 wp->w_vsep_width = 1;
1018 else
1019 {
1020 wp->w_vsep_width = oldwin->w_vsep_width;
1021 oldwin->w_vsep_width = 1;
1022 }
1023 if (flags & (WSP_TOP | WSP_BOT))
1024 {
1025 if (flags & WSP_BOT)
1026 frame_add_vsep(curfrp);
1027 /* Set width of neighbor frame */
1028 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001029 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1030 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
1032 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001033 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (before) /* new window left of current one */
1035 {
1036 wp->w_wincol = oldwin->w_wincol;
1037 oldwin->w_wincol += new_size + 1;
1038 }
1039 else /* new window right of current one */
1040 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1041 frame_fix_width(oldwin);
1042 frame_fix_width(wp);
1043 }
1044 else
1045#endif
1046 {
1047 /* width and column of new window is same as current window */
1048#ifdef FEAT_VERTSPLIT
1049 if (flags & (WSP_TOP | WSP_BOT))
1050 {
1051 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001052 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 wp->w_vsep_width = 0;
1054 }
1055 else
1056 {
1057 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001058 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 wp->w_vsep_width = oldwin->w_vsep_width;
1060 }
1061 frp->fr_width = curfrp->fr_width;
1062#endif
1063
1064 /* "new_size" of the current window goes to the new window, use
1065 * one row for the status line */
1066 win_new_height(wp, new_size);
1067 if (flags & (WSP_TOP | WSP_BOT))
1068 frame_new_height(curfrp, curfrp->fr_height
1069 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1070 else
1071 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1072 if (before) /* new window above current one */
1073 {
1074 wp->w_winrow = oldwin->w_winrow;
1075 wp->w_status_height = STATUS_HEIGHT;
1076 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1077 }
1078 else /* new window below current one */
1079 {
1080 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1081 wp->w_status_height = oldwin->w_status_height;
1082 oldwin->w_status_height = STATUS_HEIGHT;
1083 }
1084#ifdef FEAT_VERTSPLIT
1085 if (flags & WSP_BOT)
1086 frame_add_statusline(curfrp);
1087#endif
1088 frame_fix_height(wp);
1089 frame_fix_height(oldwin);
1090 }
1091
1092 if (flags & (WSP_TOP | WSP_BOT))
1093 (void)win_comp_pos();
1094
1095 /*
1096 * Both windows need redrawing
1097 */
1098 redraw_win_later(wp, NOT_VALID);
1099 wp->w_redr_status = TRUE;
1100 redraw_win_later(oldwin, NOT_VALID);
1101 oldwin->w_redr_status = TRUE;
1102
1103 if (need_status)
1104 {
1105 msg_row = Rows - 1;
1106 msg_col = sc_col;
1107 msg_clr_eos_force(); /* Old command/ruler may still be there */
1108 comp_col();
1109 msg_row = Rows - 1;
1110 msg_col = 0; /* put position back at start of line */
1111 }
1112
1113 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001114 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 */
1116 if (do_equal || dir != 0)
1117 win_equal(wp, TRUE,
1118#ifdef FEAT_VERTSPLIT
1119 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1120 : dir == 'h' ? 'b' :
1121#endif
1122 'v');
1123
1124 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1125 * size was given. */
1126#ifdef FEAT_VERTSPLIT
1127 if (flags & WSP_VERT)
1128 {
1129 i = p_wiw;
1130 if (size != 0)
1131 p_wiw = size;
1132
1133# ifdef FEAT_GUI
1134 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1135 if (gui.in_use)
1136 gui_init_which_components(NULL);
1137# endif
1138 }
1139 else
1140#endif
1141 {
1142 i = p_wh;
1143 if (size != 0)
1144 p_wh = size;
1145 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001146
1147 /*
1148 * make the new window the current window
1149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 win_enter(wp, FALSE);
1151#ifdef FEAT_VERTSPLIT
1152 if (flags & WSP_VERT)
1153 p_wiw = i;
1154 else
1155#endif
1156 p_wh = i;
1157
1158 return OK;
1159}
1160
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001161
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001162/*
1163 * Initialize window "newp" from window "oldp".
1164 * Used when splitting a window and when creating a new tab page.
1165 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001166 * WSP_NEWLOC may be specified in flags to prevent the location list from
1167 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001168 */
1169 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001170win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001171 win_T *newp;
1172 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001173 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001174{
1175 int i;
1176
1177 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001178#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001179 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001180#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001181 oldp->w_buffer->b_nwindows++;
1182 newp->w_cursor = oldp->w_cursor;
1183 newp->w_valid = 0;
1184 newp->w_curswant = oldp->w_curswant;
1185 newp->w_set_curswant = oldp->w_set_curswant;
1186 newp->w_topline = oldp->w_topline;
1187#ifdef FEAT_DIFF
1188 newp->w_topfill = oldp->w_topfill;
1189#endif
1190 newp->w_leftcol = oldp->w_leftcol;
1191 newp->w_pcmark = oldp->w_pcmark;
1192 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1193 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001194 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001195 newp->w_fraction = oldp->w_fraction;
1196 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1197#ifdef FEAT_JUMPLIST
1198 copy_jumplist(oldp, newp);
1199#endif
1200#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001201 if (flags & WSP_NEWLOC)
1202 {
1203 /* Don't copy the location list. */
1204 newp->w_llist = NULL;
1205 newp->w_llist_ref = NULL;
1206 }
1207 else
1208 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001209#endif
1210 if (oldp->w_localdir != NULL)
1211 newp->w_localdir = vim_strsave(oldp->w_localdir);
1212
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001213 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001214 for (i = 0; i < oldp->w_tagstacklen; i++)
1215 {
1216 newp->w_tagstack[i] = oldp->w_tagstack[i];
1217 if (newp->w_tagstack[i].tagname != NULL)
1218 newp->w_tagstack[i].tagname =
1219 vim_strsave(newp->w_tagstack[i].tagname);
1220 }
1221 newp->w_tagstackidx = oldp->w_tagstackidx;
1222 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001223# ifdef FEAT_FOLDING
1224 copyFoldingState(oldp, newp);
1225# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001226
1227 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001228
1229# ifdef FEAT_SYN_HL
1230 check_colorcolumn(newp);
1231# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001232}
1233
1234/*
1235 * Initialize window "newp" from window"old".
1236 * Only the essential things are copied.
1237 */
1238 static void
1239win_init_some(newp, oldp)
1240 win_T *newp;
1241 win_T *oldp;
1242{
1243 /* Use the same argument list. */
1244 newp->w_alist = oldp->w_alist;
1245 ++newp->w_alist->al_refcount;
1246 newp->w_arg_idx = oldp->w_arg_idx;
1247
1248 /* copy options from existing window */
1249 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001250}
1251
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252#endif /* FEAT_WINDOWS */
1253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254#if defined(FEAT_WINDOWS) || defined(PROTO)
1255/*
1256 * Check if "win" is a pointer to an existing window.
1257 */
1258 int
1259win_valid(win)
1260 win_T *win;
1261{
1262 win_T *wp;
1263
1264 if (win == NULL)
1265 return FALSE;
1266 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1267 if (wp == win)
1268 return TRUE;
1269 return FALSE;
1270}
1271
1272/*
1273 * Return the number of windows.
1274 */
1275 int
1276win_count()
1277{
1278 win_T *wp;
1279 int count = 0;
1280
1281 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1282 ++count;
1283 return count;
1284}
1285
1286/*
1287 * Make "count" windows on the screen.
1288 * Return actual number of windows on the screen.
1289 * Must be called when there is just one window, filling the whole screen
1290 * (excluding the command line).
1291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 int
1293make_windows(count, vertical)
1294 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001295 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 int maxcount;
1298 int todo;
1299
1300#ifdef FEAT_VERTSPLIT
1301 if (vertical)
1302 {
1303 /* Each windows needs at least 'winminwidth' lines and a separator
1304 * column. */
1305 maxcount = (curwin->w_width + curwin->w_vsep_width
1306 - (p_wiw - p_wmw)) / (p_wmw + 1);
1307 }
1308 else
1309#endif
1310 {
1311 /* Each window needs at least 'winminheight' lines and a status line. */
1312 maxcount = (curwin->w_height + curwin->w_status_height
1313 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1314 }
1315
1316 if (maxcount < 2)
1317 maxcount = 2;
1318 if (count > maxcount)
1319 count = maxcount;
1320
1321 /*
1322 * add status line now, otherwise first window will be too big
1323 */
1324 if (count > 1)
1325 last_status(TRUE);
1326
1327#ifdef FEAT_AUTOCMD
1328 /*
1329 * Don't execute autocommands while creating the windows. Must do that
1330 * when putting the buffers in the windows.
1331 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001332 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333#endif
1334
1335 /* todo is number of windows left to create */
1336 for (todo = count - 1; todo > 0; --todo)
1337#ifdef FEAT_VERTSPLIT
1338 if (vertical)
1339 {
1340 if (win_split(curwin->w_width - (curwin->w_width - todo)
1341 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1342 break;
1343 }
1344 else
1345#endif
1346 {
1347 if (win_split(curwin->w_height - (curwin->w_height - todo
1348 * STATUS_HEIGHT) / (todo + 1)
1349 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1350 break;
1351 }
1352
1353#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001354 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355#endif
1356
1357 /* return actual number of windows */
1358 return (count - todo);
1359}
1360
1361/*
1362 * Exchange current and next window
1363 */
1364 static void
1365win_exchange(Prenum)
1366 long Prenum;
1367{
1368 frame_T *frp;
1369 frame_T *frp2;
1370 win_T *wp;
1371 win_T *wp2;
1372 int temp;
1373
1374 if (lastwin == firstwin) /* just one window */
1375 {
1376 beep_flush();
1377 return;
1378 }
1379
1380#ifdef FEAT_GUI
1381 need_mouse_correct = TRUE;
1382#endif
1383
1384 /*
1385 * find window to exchange with
1386 */
1387 if (Prenum)
1388 {
1389 frp = curwin->w_frame->fr_parent->fr_child;
1390 while (frp != NULL && --Prenum > 0)
1391 frp = frp->fr_next;
1392 }
1393 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1394 frp = curwin->w_frame->fr_next;
1395 else /* Swap last window in row/col with previous */
1396 frp = curwin->w_frame->fr_prev;
1397
1398 /* We can only exchange a window with another window, not with a frame
1399 * containing windows. */
1400 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1401 return;
1402 wp = frp->fr_win;
1403
1404/*
1405 * 1. remove curwin from the list. Remember after which window it was in wp2
1406 * 2. insert curwin before wp in the list
1407 * if wp != wp2
1408 * 3. remove wp from the list
1409 * 4. insert wp after wp2
1410 * 5. exchange the status line height and vsep width.
1411 */
1412 wp2 = curwin->w_prev;
1413 frp2 = curwin->w_frame->fr_prev;
1414 if (wp->w_prev != curwin)
1415 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001416 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 frame_remove(curwin->w_frame);
1418 win_append(wp->w_prev, curwin);
1419 frame_insert(frp, curwin->w_frame);
1420 }
1421 if (wp != wp2)
1422 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001423 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 frame_remove(wp->w_frame);
1425 win_append(wp2, wp);
1426 if (frp2 == NULL)
1427 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1428 else
1429 frame_append(frp2, wp->w_frame);
1430 }
1431 temp = curwin->w_status_height;
1432 curwin->w_status_height = wp->w_status_height;
1433 wp->w_status_height = temp;
1434#ifdef FEAT_VERTSPLIT
1435 temp = curwin->w_vsep_width;
1436 curwin->w_vsep_width = wp->w_vsep_width;
1437 wp->w_vsep_width = temp;
1438
1439 /* If the windows are not in the same frame, exchange the sizes to avoid
1440 * messing up the window layout. Otherwise fix the frame sizes. */
1441 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1442 {
1443 temp = curwin->w_height;
1444 curwin->w_height = wp->w_height;
1445 wp->w_height = temp;
1446 temp = curwin->w_width;
1447 curwin->w_width = wp->w_width;
1448 wp->w_width = temp;
1449 }
1450 else
1451 {
1452 frame_fix_height(curwin);
1453 frame_fix_height(wp);
1454 frame_fix_width(curwin);
1455 frame_fix_width(wp);
1456 }
1457#endif
1458
1459 (void)win_comp_pos(); /* recompute window positions */
1460
1461 win_enter(wp, TRUE);
1462 redraw_later(CLEAR);
1463}
1464
1465/*
1466 * rotate windows: if upwards TRUE the second window becomes the first one
1467 * if upwards FALSE the first window becomes the second one
1468 */
1469 static void
1470win_rotate(upwards, count)
1471 int upwards;
1472 int count;
1473{
1474 win_T *wp1;
1475 win_T *wp2;
1476 frame_T *frp;
1477 int n;
1478
1479 if (firstwin == lastwin) /* nothing to do */
1480 {
1481 beep_flush();
1482 return;
1483 }
1484
1485#ifdef FEAT_GUI
1486 need_mouse_correct = TRUE;
1487#endif
1488
1489#ifdef FEAT_VERTSPLIT
1490 /* Check if all frames in this row/col have one window. */
1491 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1492 frp = frp->fr_next)
1493 if (frp->fr_win == NULL)
1494 {
1495 EMSG(_("E443: Cannot rotate when another window is split"));
1496 return;
1497 }
1498#endif
1499
1500 while (count--)
1501 {
1502 if (upwards) /* first window becomes last window */
1503 {
1504 /* remove first window/frame from the list */
1505 frp = curwin->w_frame->fr_parent->fr_child;
1506 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001507 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 frame_remove(frp);
1509
1510 /* find last frame and append removed window/frame after it */
1511 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1512 ;
1513 win_append(frp->fr_win, wp1);
1514 frame_append(frp, wp1->w_frame);
1515
1516 wp2 = frp->fr_win; /* previously last window */
1517 }
1518 else /* last window becomes first window */
1519 {
1520 /* find last window/frame in the list and remove it */
1521 for (frp = curwin->w_frame; frp->fr_next != NULL;
1522 frp = frp->fr_next)
1523 ;
1524 wp1 = frp->fr_win;
1525 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001526 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 frame_remove(frp);
1528
1529 /* append the removed window/frame before the first in the list */
1530 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1531 frame_insert(frp->fr_parent->fr_child, frp);
1532 }
1533
1534 /* exchange status height and vsep width of old and new last window */
1535 n = wp2->w_status_height;
1536 wp2->w_status_height = wp1->w_status_height;
1537 wp1->w_status_height = n;
1538 frame_fix_height(wp1);
1539 frame_fix_height(wp2);
1540#ifdef FEAT_VERTSPLIT
1541 n = wp2->w_vsep_width;
1542 wp2->w_vsep_width = wp1->w_vsep_width;
1543 wp1->w_vsep_width = n;
1544 frame_fix_width(wp1);
1545 frame_fix_width(wp2);
1546#endif
1547
1548 /* recompute w_winrow and w_wincol for all windows */
1549 (void)win_comp_pos();
1550 }
1551
1552 redraw_later(CLEAR);
1553}
1554
1555/*
1556 * Move the current window to the very top/bottom/left/right of the screen.
1557 */
1558 static void
1559win_totop(size, flags)
1560 int size;
1561 int flags;
1562{
1563 int dir;
1564 int height = curwin->w_height;
1565
1566 if (lastwin == firstwin)
1567 {
1568 beep_flush();
1569 return;
1570 }
1571
1572 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001573 (void)winframe_remove(curwin, &dir, NULL);
1574 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 last_status(FALSE); /* may need to remove last status line */
1576 (void)win_comp_pos(); /* recompute window positions */
1577
1578 /* Split a window on the desired side and put the window there. */
1579 (void)win_split_ins(size, flags, curwin, dir);
1580 if (!(flags & WSP_VERT))
1581 {
1582 win_setheight(height);
1583 if (p_ea)
1584 win_equal(curwin, TRUE, 'v');
1585 }
1586
1587#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1588 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1589 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001590 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592}
1593
1594/*
1595 * Move window "win1" to below/right of "win2" and make "win1" the current
1596 * window. Only works within the same frame!
1597 */
1598 void
1599win_move_after(win1, win2)
1600 win_T *win1, *win2;
1601{
1602 int height;
1603
1604 /* check if the arguments are reasonable */
1605 if (win1 == win2)
1606 return;
1607
1608 /* check if there is something to do */
1609 if (win2->w_next != win1)
1610 {
1611 /* may need move the status line/vertical separator of the last window
1612 * */
1613 if (win1 == lastwin)
1614 {
1615 height = win1->w_prev->w_status_height;
1616 win1->w_prev->w_status_height = win1->w_status_height;
1617 win1->w_status_height = height;
1618#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001619 if (win1->w_prev->w_vsep_width == 1)
1620 {
1621 /* Remove the vertical separator from the last-but-one window,
1622 * add it to the last window. Adjust the frame widths. */
1623 win1->w_prev->w_vsep_width = 0;
1624 win1->w_prev->w_frame->fr_width -= 1;
1625 win1->w_vsep_width = 1;
1626 win1->w_frame->fr_width += 1;
1627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628#endif
1629 }
1630 else if (win2 == lastwin)
1631 {
1632 height = win1->w_status_height;
1633 win1->w_status_height = win2->w_status_height;
1634 win2->w_status_height = height;
1635#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001636 if (win1->w_vsep_width == 1)
1637 {
1638 /* Remove the vertical separator from win1, add it to the last
1639 * window, win2. Adjust the frame widths. */
1640 win2->w_vsep_width = 1;
1641 win2->w_frame->fr_width += 1;
1642 win1->w_vsep_width = 0;
1643 win1->w_frame->fr_width -= 1;
1644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645#endif
1646 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001647 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 frame_remove(win1->w_frame);
1649 win_append(win2, win1);
1650 frame_append(win2->w_frame, win1->w_frame);
1651
1652 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1653 redraw_later(NOT_VALID);
1654 }
1655 win_enter(win1, FALSE);
1656}
1657
1658/*
1659 * Make all windows the same height.
1660 * 'next_curwin' will soon be the current window, make sure it has enough
1661 * rows.
1662 */
1663 void
1664win_equal(next_curwin, current, dir)
1665 win_T *next_curwin; /* pointer to current window to be or NULL */
1666 int current; /* do only frame with current window */
1667 int dir; /* 'v' for vertically, 'h' for horizontally,
1668 'b' for both, 0 for using p_ead */
1669{
1670 if (dir == 0)
1671#ifdef FEAT_VERTSPLIT
1672 dir = *p_ead;
1673#else
1674 dir = 'b';
1675#endif
1676 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001677 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001678 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679}
1680
1681/*
1682 * Set a frame to a new position and height, spreading the available room
1683 * equally over contained frames.
1684 * The window "next_curwin" (if not NULL) should at least get the size from
1685 * 'winheight' and 'winwidth' if possible.
1686 */
1687 static void
1688win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1689 win_T *next_curwin; /* pointer to current window to be or NULL */
1690 int current; /* do only frame with current window */
1691 frame_T *topfr; /* frame to set size off */
1692 int dir; /* 'v', 'h' or 'b', see win_equal() */
1693 int col; /* horizontal position for frame */
1694 int row; /* vertical position for frame */
1695 int width; /* new width of frame */
1696 int height; /* new height of frame */
1697{
1698 int n, m;
1699 int extra_sep = 0;
1700 int wincount, totwincount = 0;
1701 frame_T *fr;
1702 int next_curwin_size = 0;
1703 int room = 0;
1704 int new_size;
1705 int has_next_curwin = 0;
1706 int hnc;
1707
1708 if (topfr->fr_layout == FR_LEAF)
1709 {
1710 /* Set the width/height of this frame.
1711 * Redraw when size or position changes */
1712 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1713#ifdef FEAT_VERTSPLIT
1714 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1715#endif
1716 )
1717 {
1718 topfr->fr_win->w_winrow = row;
1719 frame_new_height(topfr, height, FALSE, FALSE);
1720#ifdef FEAT_VERTSPLIT
1721 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001722 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
1724 redraw_all_later(CLEAR);
1725 }
1726 }
1727#ifdef FEAT_VERTSPLIT
1728 else if (topfr->fr_layout == FR_ROW)
1729 {
1730 topfr->fr_width = width;
1731 topfr->fr_height = height;
1732
1733 if (dir != 'v') /* equalize frame widths */
1734 {
1735 /* Compute the maximum number of windows horizontally in this
1736 * frame. */
1737 n = frame_minwidth(topfr, NOWIN);
1738 /* add one for the rightmost window, it doesn't have a separator */
1739 if (col + width == Columns)
1740 extra_sep = 1;
1741 else
1742 extra_sep = 0;
1743 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001744 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001746 /*
1747 * Compute width for "next_curwin" window and room available for
1748 * other windows.
1749 * "m" is the minimal width when counting p_wiw for "next_curwin".
1750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 m = frame_minwidth(topfr, next_curwin);
1752 room = width - m;
1753 if (room < 0)
1754 {
1755 next_curwin_size = p_wiw + room;
1756 room = 0;
1757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 else
1759 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001760 next_curwin_size = -1;
1761 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1762 {
1763 /* If 'winfixwidth' set keep the window width if
1764 * possible.
1765 * Watch out for this window being the next_curwin. */
1766 if (frame_fixed_width(fr))
1767 {
1768 n = frame_minwidth(fr, NOWIN);
1769 new_size = fr->fr_width;
1770 if (frame_has_win(fr, next_curwin))
1771 {
1772 room += p_wiw - p_wmw;
1773 next_curwin_size = 0;
1774 if (new_size < p_wiw)
1775 new_size = p_wiw;
1776 }
1777 else
1778 /* These windows don't use up room. */
1779 totwincount -= (n + (fr->fr_next == NULL
1780 ? extra_sep : 0)) / (p_wmw + 1);
1781 room -= new_size - n;
1782 if (room < 0)
1783 {
1784 new_size += room;
1785 room = 0;
1786 }
1787 fr->fr_newwidth = new_size;
1788 }
1789 }
1790 if (next_curwin_size == -1)
1791 {
1792 if (!has_next_curwin)
1793 next_curwin_size = 0;
1794 else if (totwincount > 1
1795 && (room + (totwincount - 2))
1796 / (totwincount - 1) > p_wiw)
1797 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001798 /* Can make all windows wider than 'winwidth', spread
1799 * the room equally. */
1800 next_curwin_size = (room + p_wiw
1801 + (totwincount - 1) * p_wmw
1802 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001803 room -= next_curwin_size - p_wiw;
1804 }
1805 else
1806 next_curwin_size = p_wiw;
1807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001809
1810 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 --totwincount; /* don't count curwin */
1812 }
1813
1814 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1815 {
1816 n = m = 0;
1817 wincount = 1;
1818 if (fr->fr_next == NULL)
1819 /* last frame gets all that remains (avoid roundoff error) */
1820 new_size = width;
1821 else if (dir == 'v')
1822 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001823 else if (frame_fixed_width(fr))
1824 {
1825 new_size = fr->fr_newwidth;
1826 wincount = 0; /* doesn't count as a sizeable window */
1827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 else
1829 {
1830 /* Compute the maximum number of windows horiz. in "fr". */
1831 n = frame_minwidth(fr, NOWIN);
1832 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1833 / (p_wmw + 1);
1834 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001835 if (has_next_curwin)
1836 hnc = frame_has_win(fr, next_curwin);
1837 else
1838 hnc = FALSE;
1839 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001841 if (totwincount == 0)
1842 new_size = room;
1843 else
1844 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001846 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
1848 next_curwin_size -= p_wiw - (m - n);
1849 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001850 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001852 else
1853 room -= new_size;
1854 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 }
1856
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001857 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 * window, unless equalizing all frames. */
1859 if (!current || dir != 'v' || topfr->fr_parent != NULL
1860 || (new_size != fr->fr_width)
1861 || frame_has_win(fr, next_curwin))
1862 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001863 new_size, height);
1864 col += new_size;
1865 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 totwincount -= wincount;
1867 }
1868 }
1869#endif
1870 else /* topfr->fr_layout == FR_COL */
1871 {
1872#ifdef FEAT_VERTSPLIT
1873 topfr->fr_width = width;
1874#endif
1875 topfr->fr_height = height;
1876
1877 if (dir != 'h') /* equalize frame heights */
1878 {
1879 /* Compute maximum number of windows vertically in this frame. */
1880 n = frame_minheight(topfr, NOWIN);
1881 /* add one for the bottom window if it doesn't have a statusline */
1882 if (row + height == cmdline_row && p_ls == 0)
1883 extra_sep = 1;
1884 else
1885 extra_sep = 0;
1886 totwincount = (n + extra_sep) / (p_wmh + 1);
1887 has_next_curwin = frame_has_win(topfr, next_curwin);
1888
1889 /*
1890 * Compute height for "next_curwin" window and room available for
1891 * other windows.
1892 * "m" is the minimal height when counting p_wh for "next_curwin".
1893 */
1894 m = frame_minheight(topfr, next_curwin);
1895 room = height - m;
1896 if (room < 0)
1897 {
1898 /* The room is less then 'winheight', use all space for the
1899 * current window. */
1900 next_curwin_size = p_wh + room;
1901 room = 0;
1902 }
1903 else
1904 {
1905 next_curwin_size = -1;
1906 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1907 {
1908 /* If 'winfixheight' set keep the window height if
1909 * possible.
1910 * Watch out for this window being the next_curwin. */
1911 if (frame_fixed_height(fr))
1912 {
1913 n = frame_minheight(fr, NOWIN);
1914 new_size = fr->fr_height;
1915 if (frame_has_win(fr, next_curwin))
1916 {
1917 room += p_wh - p_wmh;
1918 next_curwin_size = 0;
1919 if (new_size < p_wh)
1920 new_size = p_wh;
1921 }
1922 else
1923 /* These windows don't use up room. */
1924 totwincount -= (n + (fr->fr_next == NULL
1925 ? extra_sep : 0)) / (p_wmh + 1);
1926 room -= new_size - n;
1927 if (room < 0)
1928 {
1929 new_size += room;
1930 room = 0;
1931 }
1932 fr->fr_newheight = new_size;
1933 }
1934 }
1935 if (next_curwin_size == -1)
1936 {
1937 if (!has_next_curwin)
1938 next_curwin_size = 0;
1939 else if (totwincount > 1
1940 && (room + (totwincount - 2))
1941 / (totwincount - 1) > p_wh)
1942 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001943 /* can make all windows higher than 'winheight',
1944 * spread the room equally. */
1945 next_curwin_size = (room + p_wh
1946 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 + (totwincount - 1)) / totwincount;
1948 room -= next_curwin_size - p_wh;
1949 }
1950 else
1951 next_curwin_size = p_wh;
1952 }
1953 }
1954
1955 if (has_next_curwin)
1956 --totwincount; /* don't count curwin */
1957 }
1958
1959 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1960 {
1961 n = m = 0;
1962 wincount = 1;
1963 if (fr->fr_next == NULL)
1964 /* last frame gets all that remains (avoid roundoff error) */
1965 new_size = height;
1966 else if (dir == 'h')
1967 new_size = fr->fr_height;
1968 else if (frame_fixed_height(fr))
1969 {
1970 new_size = fr->fr_newheight;
1971 wincount = 0; /* doesn't count as a sizeable window */
1972 }
1973 else
1974 {
1975 /* Compute the maximum number of windows vert. in "fr". */
1976 n = frame_minheight(fr, NOWIN);
1977 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1978 / (p_wmh + 1);
1979 m = frame_minheight(fr, next_curwin);
1980 if (has_next_curwin)
1981 hnc = frame_has_win(fr, next_curwin);
1982 else
1983 hnc = FALSE;
1984 if (hnc) /* don't count next_curwin */
1985 --wincount;
1986 if (totwincount == 0)
1987 new_size = room;
1988 else
1989 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1990 / totwincount;
1991 if (hnc) /* add next_curwin size */
1992 {
1993 next_curwin_size -= p_wh - (m - n);
1994 new_size += next_curwin_size;
1995 room -= new_size - next_curwin_size;
1996 }
1997 else
1998 room -= new_size;
1999 new_size += n;
2000 }
2001 /* Skip frame that is full width when splitting or closing a
2002 * window, unless equalizing all frames. */
2003 if (!current || dir != 'h' || topfr->fr_parent != NULL
2004 || (new_size != fr->fr_height)
2005 || frame_has_win(fr, next_curwin))
2006 win_equal_rec(next_curwin, current, fr, dir, col, row,
2007 width, new_size);
2008 row += new_size;
2009 height -= new_size;
2010 totwincount -= wincount;
2011 }
2012 }
2013}
2014
2015/*
2016 * close all windows for buffer 'buf'
2017 */
2018 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002019close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002021 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002023 win_T *wp;
2024 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002025 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
2027 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002028
2029 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002031 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002033 win_close(wp, FALSE);
2034
2035 /* Start all over, autocommands may change the window layout. */
2036 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
2038 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002039 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002041
2042 /* Also check windows in other tab pages. */
2043 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2044 {
2045 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002046 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002047 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2048 if (wp->w_buffer == buf)
2049 {
2050 win_close_othertab(wp, FALSE, tp);
2051
2052 /* Start all over, the tab page may be closed and
2053 * autocommands may change the window layout. */
2054 nexttp = first_tabpage;
2055 break;
2056 }
2057 }
2058
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002060
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002061 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002062 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063}
2064
2065/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002066 * Return TRUE if the current window is the only window that exists (ignoring
2067 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002068 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002069 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002070 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002071last_window()
2072{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002073 return (one_window() && first_tabpage->tp_next == NULL);
2074}
2075
2076/*
2077 * Return TRUE if there is only one window other than "aucmd_win" in the
2078 * current tab page.
2079 */
2080 static int
2081one_window()
2082{
2083#ifdef FEAT_AUTOCMD
2084 win_T *wp;
2085 int seen_one = FALSE;
2086
2087 FOR_ALL_WINDOWS(wp)
2088 {
2089 if (wp != aucmd_win)
2090 {
2091 if (seen_one)
2092 return FALSE;
2093 seen_one = TRUE;
2094 }
2095 }
2096 return TRUE;
2097#else
2098 return firstwin == lastwin;
2099#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002100}
2101
2102/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002103 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 * If "free_buf" is TRUE related buffer may be unloaded.
2105 *
2106 * called by :quit, :close, :xit, :wq and findtag()
2107 */
2108 void
2109win_close(win, free_buf)
2110 win_T *win;
2111 int free_buf;
2112{
2113 win_T *wp;
2114#ifdef FEAT_AUTOCMD
2115 int other_buffer = FALSE;
2116#endif
2117 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 int dir;
2119 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002120 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002122 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 {
2124 EMSG(_("E444: Cannot close last window"));
2125 return;
2126 }
2127
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002128#ifdef FEAT_AUTOCMD
2129 if (win == aucmd_win)
2130 {
2131 EMSG(_("E813: Cannot close autocmd window"));
2132 return;
2133 }
2134 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2135 {
2136 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2137 return;
2138 }
2139#endif
2140
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002141 /*
2142 * When closing the last window in a tab page first go to another tab
2143 * page and then close the window and the tab page. This avoids that
2144 * curwin and curtab are not invalid while we are freeing memory, they may
2145 * be used in GUI events.
2146 */
2147 if (firstwin == lastwin)
2148 {
2149 goto_tabpage_tp(alt_tabpage());
2150 redraw_tabline = TRUE;
2151
2152 /* Safety check: Autocommands may have closed the window when jumping
2153 * to the other tab page. */
2154 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2155 {
2156 int h = tabline_height();
2157
2158 win_close_othertab(win, free_buf, prev_curtab);
2159 if (h != tabline_height())
2160 shell_new_rows();
2161 }
2162 return;
2163 }
2164
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 /* When closing the help window, try restoring a snapshot after closing
2166 * the window. Otherwise clear the snapshot, it's now invalid. */
2167 if (win->w_buffer->b_help)
2168 help_window = TRUE;
2169 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002170 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171
2172#ifdef FEAT_AUTOCMD
2173 if (win == curwin)
2174 {
2175 /*
2176 * Guess which window is going to be the new current window.
2177 * This may change because of the autocommands (sigh).
2178 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002179 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180
2181 /*
2182 * Be careful: If autocommands delete the window, return now.
2183 */
2184 if (wp->w_buffer != curbuf)
2185 {
2186 other_buffer = TRUE;
2187 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002188 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 return;
2190 }
2191 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002192 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 return;
2194# ifdef FEAT_EVAL
2195 /* autocmds may abort script processing */
2196 if (aborting())
2197 return;
2198# endif
2199 }
2200#endif
2201
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002202#ifdef FEAT_GUI
2203 /* Avoid trouble with scrollbars that are going to be deleted in
2204 * win_free(). */
2205 if (gui.in_use)
2206 out_flush();
2207#endif
2208
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 /*
2210 * Close the link to the buffer.
2211 */
2212 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002213
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002215 * other window or moved to another tab page. */
2216 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 return;
2218
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002219 /* Free the memory used for the window. */
2220 wp = win_free_mem(win, &dir, NULL);
2221
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 /* Make sure curwin isn't invalid. It can cause severe trouble when
2223 * printing an error message. For win_equal() curbuf needs to be valid
2224 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002225 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
2227 curwin = wp;
2228#ifdef FEAT_QUICKFIX
2229 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2230 {
2231 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002232 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 * finding another window to go to.
2234 */
2235 for (;;)
2236 {
2237 if (wp->w_next == NULL)
2238 wp = firstwin;
2239 else
2240 wp = wp->w_next;
2241 if (wp == curwin)
2242 break;
2243 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2244 {
2245 curwin = wp;
2246 break;
2247 }
2248 }
2249 }
2250#endif
2251 curbuf = curwin->w_buffer;
2252 close_curwin = TRUE;
2253 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002254 if (p_ea
2255#ifdef FEAT_VERTSPLIT
2256 && (*p_ead == 'b' || *p_ead == dir)
2257#endif
2258 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 win_equal(curwin, TRUE,
2260#ifdef FEAT_VERTSPLIT
2261 dir
2262#else
2263 0
2264#endif
2265 );
2266 else
2267 win_comp_pos();
2268 if (close_curwin)
2269 {
2270 win_enter_ext(wp, FALSE, TRUE);
2271#ifdef FEAT_AUTOCMD
2272 if (other_buffer)
2273 /* careful: after this wp and win may be invalid! */
2274 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2275#endif
2276 }
2277
2278 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002279 * If last window has a status line now and we don't want one,
2280 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 */
2282 last_status(FALSE);
2283
2284 /* After closing the help window, try restoring the window layout from
2285 * before it was opened. */
2286 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002287 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
2289#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2290 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2291 if (gui.in_use && !win_hasvertsplit())
2292 gui_init_which_components(NULL);
2293#endif
2294
2295 redraw_all_later(NOT_VALID);
2296}
2297
2298/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002299 * Close window "win" in tab page "tp", which is not the current tab page.
2300 * This may be the last window ih that tab page and result in closing the tab,
2301 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002302 * Caller must check if buffer is hidden and whether the tabline needs to be
2303 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002304 */
2305 void
2306win_close_othertab(win, free_buf, tp)
2307 win_T *win;
2308 int free_buf;
2309 tabpage_T *tp;
2310{
2311 win_T *wp;
2312 int dir;
2313 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002314 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002315
2316 /* Close the link to the buffer. */
2317 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2318
2319 /* Careful: Autocommands may have closed the tab page or made it the
2320 * current tab page. */
2321 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2322 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002323 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002324 return;
2325
2326 /* Autocommands may have closed the window already. */
2327 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2328 ;
2329 if (wp == NULL)
2330 return;
2331
Bram Moolenaarf740b292006-02-16 22:11:02 +00002332 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002333 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002334 {
2335 if (tp == first_tabpage)
2336 first_tabpage = tp->tp_next;
2337 else
2338 {
2339 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2340 ptp = ptp->tp_next)
2341 ;
2342 if (ptp == NULL)
2343 {
2344 EMSG2(_(e_intern2), "win_close_othertab()");
2345 return;
2346 }
2347 ptp->tp_next = tp->tp_next;
2348 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002349 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002350 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002351
2352 /* Free the memory used for the window. */
2353 win_free_mem(win, &dir, tp);
2354
2355 if (free_tp)
2356 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002357}
2358
2359/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002360 * Free the memory used for a window.
2361 * Returns a pointer to the window that got the freed up space.
2362 */
2363 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002364win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002365 win_T *win;
2366 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002367 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002368{
2369 frame_T *frp;
2370 win_T *wp;
2371
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002372 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002373 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002374 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002375 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002376 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002377
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002378 /* When deleting the current window of another tab page select a new
2379 * current window. */
2380 if (tp != NULL && win == tp->tp_curwin)
2381 tp->tp_curwin = wp;
2382
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002383 return wp;
2384}
2385
2386#if defined(EXITFREE) || defined(PROTO)
2387 void
2388win_free_all()
2389{
2390 int dummy;
2391
Bram Moolenaarf740b292006-02-16 22:11:02 +00002392# ifdef FEAT_WINDOWS
2393 while (first_tabpage->tp_next != NULL)
2394 tabpage_close(TRUE);
2395# endif
2396
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002397# ifdef FEAT_AUTOCMD
2398 if (aucmd_win != NULL)
2399 {
2400 (void)win_free_mem(aucmd_win, &dummy, NULL);
2401 aucmd_win = NULL;
2402 }
2403# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002404
2405 while (firstwin != NULL)
2406 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002407}
2408#endif
2409
2410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 * Remove a window and its frame from the tree of frames.
2412 * Returns a pointer to the window that got the freed up space.
2413 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002414 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002415winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002417 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002418 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419{
2420 frame_T *frp, *frp2, *frp3;
2421 frame_T *frp_close = win->w_frame;
2422 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
2424 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002425 * If there is only one window there is nothing to remove.
2426 */
2427 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2428 return NULL;
2429
2430 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 * Remove the window from its frame.
2432 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002433 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 wp = frame2win(frp2);
2435
2436 /* Remove this frame from the list of frames. */
2437 frame_remove(frp_close);
2438
2439#ifdef FEAT_VERTSPLIT
2440 if (frp_close->fr_parent->fr_layout == FR_COL)
2441 {
2442#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002443 /* When 'winfixheight' is set, try to find another frame in the column
2444 * (as close to the closed frame as possible) to distribute the height
2445 * to. */
2446 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2447 {
2448 frp = frp_close->fr_prev;
2449 frp3 = frp_close->fr_next;
2450 while (frp != NULL || frp3 != NULL)
2451 {
2452 if (frp != NULL)
2453 {
2454 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2455 {
2456 frp2 = frp;
2457 wp = frp->fr_win;
2458 break;
2459 }
2460 frp = frp->fr_prev;
2461 }
2462 if (frp3 != NULL)
2463 {
2464 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2465 {
2466 frp2 = frp3;
2467 wp = frp3->fr_win;
2468 break;
2469 }
2470 frp3 = frp3->fr_next;
2471 }
2472 }
2473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2475 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476#ifdef FEAT_VERTSPLIT
2477 *dirp = 'v';
2478 }
2479 else
2480 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002481 /* When 'winfixwidth' is set, try to find another frame in the column
2482 * (as close to the closed frame as possible) to distribute the width
2483 * to. */
2484 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2485 {
2486 frp = frp_close->fr_prev;
2487 frp3 = frp_close->fr_next;
2488 while (frp != NULL || frp3 != NULL)
2489 {
2490 if (frp != NULL)
2491 {
2492 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2493 {
2494 frp2 = frp;
2495 wp = frp->fr_win;
2496 break;
2497 }
2498 frp = frp->fr_prev;
2499 }
2500 if (frp3 != NULL)
2501 {
2502 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2503 {
2504 frp2 = frp3;
2505 wp = frp3->fr_win;
2506 break;
2507 }
2508 frp3 = frp3->fr_next;
2509 }
2510 }
2511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002513 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 *dirp = 'h';
2515 }
2516#endif
2517
2518 /* If rows/columns go to a window below/right its positions need to be
2519 * updated. Can only be done after the sizes have been updated. */
2520 if (frp2 == frp_close->fr_next)
2521 {
2522 int row = win->w_winrow;
2523 int col = W_WINCOL(win);
2524
2525 frame_comp_pos(frp2, &row, &col);
2526 }
2527
2528 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2529 {
2530 /* There is no other frame in this list, move its info to the parent
2531 * and remove it. */
2532 frp2->fr_parent->fr_layout = frp2->fr_layout;
2533 frp2->fr_parent->fr_child = frp2->fr_child;
2534 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2535 frp->fr_parent = frp2->fr_parent;
2536 frp2->fr_parent->fr_win = frp2->fr_win;
2537 if (frp2->fr_win != NULL)
2538 frp2->fr_win->w_frame = frp2->fr_parent;
2539 frp = frp2->fr_parent;
2540 vim_free(frp2);
2541
2542 frp2 = frp->fr_parent;
2543 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2544 {
2545 /* The frame above the parent has the same layout, have to merge
2546 * the frames into this list. */
2547 if (frp2->fr_child == frp)
2548 frp2->fr_child = frp->fr_child;
2549 frp->fr_child->fr_prev = frp->fr_prev;
2550 if (frp->fr_prev != NULL)
2551 frp->fr_prev->fr_next = frp->fr_child;
2552 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2553 {
2554 frp3->fr_parent = frp2;
2555 if (frp3->fr_next == NULL)
2556 {
2557 frp3->fr_next = frp->fr_next;
2558 if (frp->fr_next != NULL)
2559 frp->fr_next->fr_prev = frp3;
2560 break;
2561 }
2562 }
2563 vim_free(frp);
2564 }
2565 }
2566
2567 return wp;
2568}
2569
2570/*
2571 * Find out which frame is going to get the freed up space when "win" is
2572 * closed.
2573 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2574 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2575 * This makes opening a window and closing it immediately keep the same window
2576 * layout.
2577 */
2578 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002579win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002581 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
2583 frame_T *frp;
2584 int b;
2585
Bram Moolenaarf740b292006-02-16 22:11:02 +00002586 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002587 /* Last window in this tab page, will go to next tab page. */
2588 return alt_tabpage()->tp_curwin->w_frame;
2589
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 frp = win->w_frame;
2591#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002592 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 b = p_spr;
2594 else
2595#endif
2596 b = p_sb;
2597 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2598 return frp->fr_next;
2599 return frp->fr_prev;
2600}
2601
2602/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002603 * Return the tabpage that will be used if the current one is closed.
2604 */
2605 static tabpage_T *
2606alt_tabpage()
2607{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002608 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002609
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002610 /* Use the next tab page if possible. */
2611 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002612 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002613
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002614 /* Find the last but one tab page. */
2615 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2616 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002617 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002618}
2619
2620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 * Find the left-upper window in frame "frp".
2622 */
2623 static win_T *
2624frame2win(frp)
2625 frame_T *frp;
2626{
2627 while (frp->fr_win == NULL)
2628 frp = frp->fr_child;
2629 return frp->fr_win;
2630}
2631
2632/*
2633 * Return TRUE if frame "frp" contains window "wp".
2634 */
2635 static int
2636frame_has_win(frp, wp)
2637 frame_T *frp;
2638 win_T *wp;
2639{
2640 frame_T *p;
2641
2642 if (frp->fr_layout == FR_LEAF)
2643 return frp->fr_win == wp;
2644
2645 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2646 if (frame_has_win(p, wp))
2647 return TRUE;
2648 return FALSE;
2649}
2650
2651/*
2652 * Set a new height for a frame. Recursively sets the height for contained
2653 * frames and windows. Caller must take care of positions.
2654 */
2655 static void
2656frame_new_height(topfrp, height, topfirst, wfh)
2657 frame_T *topfrp;
2658 int height;
2659 int topfirst; /* resize topmost contained frame first */
2660 int wfh; /* obey 'winfixheight' when there is a choice;
2661 may cause the height not to be set */
2662{
2663 frame_T *frp;
2664 int extra_lines;
2665 int h;
2666
2667 if (topfrp->fr_win != NULL)
2668 {
2669 /* Simple case: just one window. */
2670 win_new_height(topfrp->fr_win,
2671 height - topfrp->fr_win->w_status_height);
2672 }
2673#ifdef FEAT_VERTSPLIT
2674 else if (topfrp->fr_layout == FR_ROW)
2675 {
2676 do
2677 {
2678 /* All frames in this row get the same new height. */
2679 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2680 {
2681 frame_new_height(frp, height, topfirst, wfh);
2682 if (frp->fr_height > height)
2683 {
2684 /* Could not fit the windows, make the whole row higher. */
2685 height = frp->fr_height;
2686 break;
2687 }
2688 }
2689 }
2690 while (frp != NULL);
2691 }
2692#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002693 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 {
2695 /* Complicated case: Resize a column of frames. Resize the bottom
2696 * frame first, frames above that when needed. */
2697
2698 frp = topfrp->fr_child;
2699 if (wfh)
2700 /* Advance past frames with one window with 'wfh' set. */
2701 while (frame_fixed_height(frp))
2702 {
2703 frp = frp->fr_next;
2704 if (frp == NULL)
2705 return; /* no frame without 'wfh', give up */
2706 }
2707 if (!topfirst)
2708 {
2709 /* Find the bottom frame of this column */
2710 while (frp->fr_next != NULL)
2711 frp = frp->fr_next;
2712 if (wfh)
2713 /* Advance back for frames with one window with 'wfh' set. */
2714 while (frame_fixed_height(frp))
2715 frp = frp->fr_prev;
2716 }
2717
2718 extra_lines = height - topfrp->fr_height;
2719 if (extra_lines < 0)
2720 {
2721 /* reduce height of contained frames, bottom or top frame first */
2722 while (frp != NULL)
2723 {
2724 h = frame_minheight(frp, NULL);
2725 if (frp->fr_height + extra_lines < h)
2726 {
2727 extra_lines += frp->fr_height - h;
2728 frame_new_height(frp, h, topfirst, wfh);
2729 }
2730 else
2731 {
2732 frame_new_height(frp, frp->fr_height + extra_lines,
2733 topfirst, wfh);
2734 break;
2735 }
2736 if (topfirst)
2737 {
2738 do
2739 frp = frp->fr_next;
2740 while (wfh && frp != NULL && frame_fixed_height(frp));
2741 }
2742 else
2743 {
2744 do
2745 frp = frp->fr_prev;
2746 while (wfh && frp != NULL && frame_fixed_height(frp));
2747 }
2748 /* Increase "height" if we could not reduce enough frames. */
2749 if (frp == NULL)
2750 height -= extra_lines;
2751 }
2752 }
2753 else if (extra_lines > 0)
2754 {
2755 /* increase height of bottom or top frame */
2756 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2757 }
2758 }
2759 topfrp->fr_height = height;
2760}
2761
2762/*
2763 * Return TRUE if height of frame "frp" should not be changed because of
2764 * the 'winfixheight' option.
2765 */
2766 static int
2767frame_fixed_height(frp)
2768 frame_T *frp;
2769{
2770 /* frame with one window: fixed height if 'winfixheight' set. */
2771 if (frp->fr_win != NULL)
2772 return frp->fr_win->w_p_wfh;
2773
2774 if (frp->fr_layout == FR_ROW)
2775 {
2776 /* The frame is fixed height if one of the frames in the row is fixed
2777 * height. */
2778 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2779 if (frame_fixed_height(frp))
2780 return TRUE;
2781 return FALSE;
2782 }
2783
2784 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2785 * frames in the row are fixed height. */
2786 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2787 if (!frame_fixed_height(frp))
2788 return FALSE;
2789 return TRUE;
2790}
2791
2792#ifdef FEAT_VERTSPLIT
2793/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002794 * Return TRUE if width of frame "frp" should not be changed because of
2795 * the 'winfixwidth' option.
2796 */
2797 static int
2798frame_fixed_width(frp)
2799 frame_T *frp;
2800{
2801 /* frame with one window: fixed width if 'winfixwidth' set. */
2802 if (frp->fr_win != NULL)
2803 return frp->fr_win->w_p_wfw;
2804
2805 if (frp->fr_layout == FR_COL)
2806 {
2807 /* The frame is fixed width if one of the frames in the row is fixed
2808 * width. */
2809 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2810 if (frame_fixed_width(frp))
2811 return TRUE;
2812 return FALSE;
2813 }
2814
2815 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2816 * frames in the row are fixed width. */
2817 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2818 if (!frame_fixed_width(frp))
2819 return FALSE;
2820 return TRUE;
2821}
2822
2823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 * Add a status line to windows at the bottom of "frp".
2825 * Note: Does not check if there is room!
2826 */
2827 static void
2828frame_add_statusline(frp)
2829 frame_T *frp;
2830{
2831 win_T *wp;
2832
2833 if (frp->fr_layout == FR_LEAF)
2834 {
2835 wp = frp->fr_win;
2836 if (wp->w_status_height == 0)
2837 {
2838 if (wp->w_height > 0) /* don't make it negative */
2839 --wp->w_height;
2840 wp->w_status_height = STATUS_HEIGHT;
2841 }
2842 }
2843 else if (frp->fr_layout == FR_ROW)
2844 {
2845 /* Handle all the frames in the row. */
2846 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2847 frame_add_statusline(frp);
2848 }
2849 else /* frp->fr_layout == FR_COL */
2850 {
2851 /* Only need to handle the last frame in the column. */
2852 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2853 ;
2854 frame_add_statusline(frp);
2855 }
2856}
2857
2858/*
2859 * Set width of a frame. Handles recursively going through contained frames.
2860 * May remove separator line for windows at the right side (for win_close()).
2861 */
2862 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002863frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 frame_T *topfrp;
2865 int width;
2866 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002867 int wfw; /* obey 'winfixwidth' when there is a choice;
2868 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869{
2870 frame_T *frp;
2871 int extra_cols;
2872 int w;
2873 win_T *wp;
2874
2875 if (topfrp->fr_layout == FR_LEAF)
2876 {
2877 /* Simple case: just one window. */
2878 wp = topfrp->fr_win;
2879 /* Find out if there are any windows right of this one. */
2880 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2881 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2882 break;
2883 if (frp->fr_parent == NULL)
2884 wp->w_vsep_width = 0;
2885 win_new_width(wp, width - wp->w_vsep_width);
2886 }
2887 else if (topfrp->fr_layout == FR_COL)
2888 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002889 do
2890 {
2891 /* All frames in this column get the same new width. */
2892 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2893 {
2894 frame_new_width(frp, width, leftfirst, wfw);
2895 if (frp->fr_width > width)
2896 {
2897 /* Could not fit the windows, make whole column wider. */
2898 width = frp->fr_width;
2899 break;
2900 }
2901 }
2902 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 }
2904 else /* fr_layout == FR_ROW */
2905 {
2906 /* Complicated case: Resize a row of frames. Resize the rightmost
2907 * frame first, frames left of it when needed. */
2908
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002910 if (wfw)
2911 /* Advance past frames with one window with 'wfw' set. */
2912 while (frame_fixed_width(frp))
2913 {
2914 frp = frp->fr_next;
2915 if (frp == NULL)
2916 return; /* no frame without 'wfw', give up */
2917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002919 {
2920 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 while (frp->fr_next != NULL)
2922 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002923 if (wfw)
2924 /* Advance back for frames with one window with 'wfw' set. */
2925 while (frame_fixed_width(frp))
2926 frp = frp->fr_prev;
2927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
2929 extra_cols = width - topfrp->fr_width;
2930 if (extra_cols < 0)
2931 {
2932 /* reduce frame width, rightmost frame first */
2933 while (frp != NULL)
2934 {
2935 w = frame_minwidth(frp, NULL);
2936 if (frp->fr_width + extra_cols < w)
2937 {
2938 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002939 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 }
2941 else
2942 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002943 frame_new_width(frp, frp->fr_width + extra_cols,
2944 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 break;
2946 }
2947 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002948 {
2949 do
2950 frp = frp->fr_next;
2951 while (wfw && frp != NULL && frame_fixed_width(frp));
2952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002954 {
2955 do
2956 frp = frp->fr_prev;
2957 while (wfw && frp != NULL && frame_fixed_width(frp));
2958 }
2959 /* Increase "width" if we could not reduce enough frames. */
2960 if (frp == NULL)
2961 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 }
2963 }
2964 else if (extra_cols > 0)
2965 {
2966 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002967 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
2969 }
2970 topfrp->fr_width = width;
2971}
2972
2973/*
2974 * Add the vertical separator to windows at the right side of "frp".
2975 * Note: Does not check if there is room!
2976 */
2977 static void
2978frame_add_vsep(frp)
2979 frame_T *frp;
2980{
2981 win_T *wp;
2982
2983 if (frp->fr_layout == FR_LEAF)
2984 {
2985 wp = frp->fr_win;
2986 if (wp->w_vsep_width == 0)
2987 {
2988 if (wp->w_width > 0) /* don't make it negative */
2989 --wp->w_width;
2990 wp->w_vsep_width = 1;
2991 }
2992 }
2993 else if (frp->fr_layout == FR_COL)
2994 {
2995 /* Handle all the frames in the column. */
2996 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2997 frame_add_vsep(frp);
2998 }
2999 else /* frp->fr_layout == FR_ROW */
3000 {
3001 /* Only need to handle the last frame in the row. */
3002 frp = frp->fr_child;
3003 while (frp->fr_next != NULL)
3004 frp = frp->fr_next;
3005 frame_add_vsep(frp);
3006 }
3007}
3008
3009/*
3010 * Set frame width from the window it contains.
3011 */
3012 static void
3013frame_fix_width(wp)
3014 win_T *wp;
3015{
3016 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3017}
3018#endif
3019
3020/*
3021 * Set frame height from the window it contains.
3022 */
3023 static void
3024frame_fix_height(wp)
3025 win_T *wp;
3026{
3027 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3028}
3029
3030/*
3031 * Compute the minimal height for frame "topfrp".
3032 * Uses the 'winminheight' option.
3033 * When "next_curwin" isn't NULL, use p_wh for this window.
3034 * When "next_curwin" is NOWIN, don't use at least one line for the current
3035 * window.
3036 */
3037 static int
3038frame_minheight(topfrp, next_curwin)
3039 frame_T *topfrp;
3040 win_T *next_curwin;
3041{
3042 frame_T *frp;
3043 int m;
3044#ifdef FEAT_VERTSPLIT
3045 int n;
3046#endif
3047
3048 if (topfrp->fr_win != NULL)
3049 {
3050 if (topfrp->fr_win == next_curwin)
3051 m = p_wh + topfrp->fr_win->w_status_height;
3052 else
3053 {
3054 /* window: minimal height of the window plus status line */
3055 m = p_wmh + topfrp->fr_win->w_status_height;
3056 /* Current window is minimal one line high */
3057 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3058 ++m;
3059 }
3060 }
3061#ifdef FEAT_VERTSPLIT
3062 else if (topfrp->fr_layout == FR_ROW)
3063 {
3064 /* get the minimal height from each frame in this row */
3065 m = 0;
3066 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3067 {
3068 n = frame_minheight(frp, next_curwin);
3069 if (n > m)
3070 m = n;
3071 }
3072 }
3073#endif
3074 else
3075 {
3076 /* Add up the minimal heights for all frames in this column. */
3077 m = 0;
3078 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3079 m += frame_minheight(frp, next_curwin);
3080 }
3081
3082 return m;
3083}
3084
3085#ifdef FEAT_VERTSPLIT
3086/*
3087 * Compute the minimal width for frame "topfrp".
3088 * When "next_curwin" isn't NULL, use p_wiw for this window.
3089 * When "next_curwin" is NOWIN, don't use at least one column for the current
3090 * window.
3091 */
3092 static int
3093frame_minwidth(topfrp, next_curwin)
3094 frame_T *topfrp;
3095 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3096{
3097 frame_T *frp;
3098 int m, n;
3099
3100 if (topfrp->fr_win != NULL)
3101 {
3102 if (topfrp->fr_win == next_curwin)
3103 m = p_wiw + topfrp->fr_win->w_vsep_width;
3104 else
3105 {
3106 /* window: minimal width of the window plus separator column */
3107 m = p_wmw + topfrp->fr_win->w_vsep_width;
3108 /* Current window is minimal one column wide */
3109 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3110 ++m;
3111 }
3112 }
3113 else if (topfrp->fr_layout == FR_COL)
3114 {
3115 /* get the minimal width from each frame in this column */
3116 m = 0;
3117 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3118 {
3119 n = frame_minwidth(frp, next_curwin);
3120 if (n > m)
3121 m = n;
3122 }
3123 }
3124 else
3125 {
3126 /* Add up the minimal widths for all frames in this row. */
3127 m = 0;
3128 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3129 m += frame_minwidth(frp, next_curwin);
3130 }
3131
3132 return m;
3133}
3134#endif
3135
3136
3137/*
3138 * Try to close all windows except current one.
3139 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3140 * used and the buffer was modified.
3141 *
3142 * Used by ":bdel" and ":only".
3143 */
3144 void
3145close_others(message, forceit)
3146 int message;
3147 int forceit; /* always hide all other windows */
3148{
3149 win_T *wp;
3150 win_T *nextwp;
3151 int r;
3152
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003153 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 {
3155 if (message
3156#ifdef FEAT_AUTOCMD
3157 && !autocmd_busy
3158#endif
3159 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003160 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 return;
3162 }
3163
3164 /* Be very careful here: autocommands may change the window layout. */
3165 for (wp = firstwin; win_valid(wp); wp = nextwp)
3166 {
3167 nextwp = wp->w_next;
3168 if (wp != curwin) /* don't close current window */
3169 {
3170
3171 /* Check if it's allowed to abandon this window */
3172 r = can_abandon(wp->w_buffer, forceit);
3173#ifdef FEAT_AUTOCMD
3174 if (!win_valid(wp)) /* autocommands messed wp up */
3175 {
3176 nextwp = firstwin;
3177 continue;
3178 }
3179#endif
3180 if (!r)
3181 {
3182#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3183 if (message && (p_confirm || cmdmod.confirm) && p_write)
3184 {
3185 dialog_changed(wp->w_buffer, FALSE);
3186# ifdef FEAT_AUTOCMD
3187 if (!win_valid(wp)) /* autocommands messed wp up */
3188 {
3189 nextwp = firstwin;
3190 continue;
3191 }
3192# endif
3193 }
3194 if (bufIsChanged(wp->w_buffer))
3195#endif
3196 continue;
3197 }
3198 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3199 }
3200 }
3201
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003202 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 EMSG(_("E445: Other window contains changes"));
3204}
3205
3206#endif /* FEAT_WINDOWS */
3207
3208/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003209 * Init the current window "curwin".
3210 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 */
3212 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003213curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003215 win_init_empty(curwin);
3216}
3217
3218 void
3219win_init_empty(wp)
3220 win_T *wp;
3221{
3222 redraw_win_later(wp, NOT_VALID);
3223 wp->w_lines_valid = 0;
3224 wp->w_cursor.lnum = 1;
3225 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003227 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003229 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3230 wp->w_pcmark.col = 0;
3231 wp->w_prev_pcmark.lnum = 0;
3232 wp->w_prev_pcmark.col = 0;
3233 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003235 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003237 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003239 if (wp->w_p_rl)
3240 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003242 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243#endif
3244}
3245
3246/*
3247 * Allocate the first window and put an empty buffer in it.
3248 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003249 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003251 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252win_alloc_first()
3253{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003254 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003255 return FAIL;
3256
3257#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003258 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003259 if (first_tabpage == NULL)
3260 return FAIL;
3261 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003262 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003263#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003264
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003265 return OK;
3266}
3267
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003268#if defined(FEAT_AUTOCMD) || defined(PROTO)
3269/*
3270 * Init "aucmd_win". This can only be done after the first
3271 * window is fully initialized, thus it can't be in win_alloc_first().
3272 */
3273 void
3274win_alloc_aucmd_win()
3275{
3276 aucmd_win = win_alloc(NULL, TRUE);
3277 if (aucmd_win != NULL)
3278 {
3279 win_init_some(aucmd_win, curwin);
3280# ifdef FEAT_SCROLLBIND
3281 aucmd_win->w_p_scb = FALSE;
3282# endif
3283 new_frame(aucmd_win);
3284 }
3285}
3286#endif
3287
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003288/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003289 * Allocate the first window or the first window in a new tab page.
3290 * When "oldwin" is NULL create an empty buffer for it.
3291 * When "oldwin" is not NULL copy info from it to the new window (only with
3292 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003293 * Return FAIL when something goes wrong (out of memory).
3294 */
3295 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003296win_alloc_firstwin(oldwin)
3297 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003298{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003299 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003300 if (oldwin == NULL)
3301 {
3302 /* Very first window, need to create an empty buffer for it and
3303 * initialize from scratch. */
3304 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3305 if (curwin == NULL || curbuf == NULL)
3306 return FAIL;
3307 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003308#ifdef FEAT_SYN_HL
3309 curwin->w_s = &(curbuf->b_s);
3310#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003311 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003313 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003315 curwin_init(); /* init current window */
3316 }
3317#ifdef FEAT_WINDOWS
3318 else
3319 {
3320 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003321 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003322
3323# ifdef FEAT_SCROLLBIND
3324 /* We don't want scroll-binding in the first window. */
3325 curwin->w_p_scb = FALSE;
3326# endif
3327 }
3328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003330 new_frame(curwin);
3331 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003332 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003333 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334#ifdef FEAT_VERTSPLIT
3335 topframe->fr_width = Columns;
3336#endif
3337 topframe->fr_height = Rows - p_ch;
3338 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003339
3340 return OK;
3341}
3342
3343/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003344 * Create a frame for window "wp".
3345 */
3346 static void
3347new_frame(win_T *wp)
3348{
3349 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3350
3351 wp->w_frame = frp;
3352 if (frp != NULL)
3353 {
3354 frp->fr_layout = FR_LEAF;
3355 frp->fr_win = wp;
3356 }
3357}
3358
3359/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003360 * Initialize the window and frame size to the maximum.
3361 */
3362 void
3363win_init_size()
3364{
3365 firstwin->w_height = ROWS_AVAIL;
3366 topframe->fr_height = ROWS_AVAIL;
3367#ifdef FEAT_VERTSPLIT
3368 firstwin->w_width = Columns;
3369 topframe->fr_width = Columns;
3370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371}
3372
3373#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003374
3375/*
3376 * Allocate a new tabpage_T and init the values.
3377 * Returns NULL when out of memory.
3378 */
3379 static tabpage_T *
3380alloc_tabpage()
3381{
3382 tabpage_T *tp;
3383
3384 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3385 if (tp != NULL)
3386 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003387# ifdef FEAT_GUI
3388 int i;
3389
3390 for (i = 0; i < 3; i++)
3391 tp->tp_prev_which_scrollbars[i] = -1;
3392# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003393# ifdef FEAT_DIFF
3394 tp->tp_diff_invalid = TRUE;
3395# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003396#ifdef FEAT_EVAL
3397 /* init t: variables */
3398 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3399#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003400 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003401 }
3402 return tp;
3403}
3404
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003405 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003406free_tabpage(tp)
3407 tabpage_T *tp;
3408{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003409 int idx;
3410
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003411# ifdef FEAT_DIFF
3412 diff_clear(tp);
3413# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003414 for (idx = 0; idx < SNAP_COUNT; ++idx)
3415 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003416#ifdef FEAT_EVAL
3417 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3418#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003419 vim_free(tp);
3420}
3421
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003422/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003423 * Create a new Tab page with one window.
3424 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003425 * When "after" is 0 put it just after the current Tab page.
3426 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003427 * Return FAIL or OK.
3428 */
3429 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003430win_new_tabpage(after)
3431 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003432{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003433 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003434 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003435 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003436
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003437 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003438 if (newtp == NULL)
3439 return FAIL;
3440
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003441 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003442 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003443 {
3444 vim_free(newtp);
3445 return FAIL;
3446 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003447 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003448
3449 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003450 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003451 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003452 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003453 if (after == 1)
3454 {
3455 /* New tab page becomes the first one. */
3456 newtp->tp_next = first_tabpage;
3457 first_tabpage = newtp;
3458 }
3459 else
3460 {
3461 if (after > 0)
3462 {
3463 /* Put new tab page before tab page "after". */
3464 n = 2;
3465 for (tp = first_tabpage; tp->tp_next != NULL
3466 && n < after; tp = tp->tp_next)
3467 ++n;
3468 }
3469 newtp->tp_next = tp->tp_next;
3470 tp->tp_next = newtp;
3471 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003472 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003473 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003474 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003475
3476 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003477 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003478
3479#if defined(FEAT_GUI)
3480 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3481 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003482 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003483#endif
3484
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003485 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003486#ifdef FEAT_AUTOCMD
3487 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3488 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3489#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003490 return OK;
3491 }
3492
3493 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003494 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003495 return FAIL;
3496}
3497
3498/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003499 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3500 * like with ":split".
3501 * Returns OK if a new tab page was created, FAIL otherwise.
3502 */
3503 int
3504may_open_tabpage()
3505{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003506 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003507
Bram Moolenaard326ce82007-03-11 14:48:29 +00003508 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003509 {
3510 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003511 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003512 return win_new_tabpage(n);
3513 }
3514 return FAIL;
3515}
3516
3517/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003518 * Create up to "maxcount" tabpages with empty windows.
3519 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003520 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003521 int
3522make_tabpages(maxcount)
3523 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003524{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003525 int count = maxcount;
3526 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003527
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003528 /* Limit to 'tabpagemax' tabs. */
3529 if (count > p_tpm)
3530 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003531
3532#ifdef FEAT_AUTOCMD
3533 /*
3534 * Don't execute autocommands while creating the tab pages. Must do that
3535 * when putting the buffers in the windows.
3536 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003537 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003538#endif
3539
3540 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003541 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003542 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003543
3544#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003545 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003546#endif
3547
3548 /* return actual number of tab pages */
3549 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003550}
3551
3552/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003553 * Return TRUE when "tpc" points to a valid tab page.
3554 */
3555 int
3556valid_tabpage(tpc)
3557 tabpage_T *tpc;
3558{
3559 tabpage_T *tp;
3560
3561 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3562 if (tp == tpc)
3563 return TRUE;
3564 return FALSE;
3565}
3566
3567/*
3568 * Find tab page "n" (first one is 1). Returns NULL when not found.
3569 */
3570 tabpage_T *
3571find_tabpage(n)
3572 int n;
3573{
3574 tabpage_T *tp;
3575 int i = 1;
3576
3577 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3578 ++i;
3579 return tp;
3580}
3581
3582/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003583 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003584 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003585 */
3586 int
3587tabpage_index(ftp)
3588 tabpage_T *ftp;
3589{
3590 int i = 1;
3591 tabpage_T *tp;
3592
3593 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3594 ++i;
3595 return i;
3596}
3597
3598/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003599 * Prepare for leaving the current tab page.
3600 * When autocomands change "curtab" we don't leave the tab page and return
3601 * FAIL.
3602 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003603 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003604 static int
3605leave_tabpage(new_curbuf)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003606 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003607 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003608{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003609 tabpage_T *tp = curtab;
3610
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003611#ifdef FEAT_VISUAL
3612 reset_VIsual_and_resel(); /* stop Visual mode */
3613#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003614#ifdef FEAT_AUTOCMD
3615 if (new_curbuf != curbuf)
3616 {
3617 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3618 if (curtab != tp)
3619 return FAIL;
3620 }
3621 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3622 if (curtab != tp)
3623 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003624 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003625 if (curtab != tp)
3626 return FAIL;
3627#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003628#if defined(FEAT_GUI)
3629 /* Remove the scrollbars. They may be added back later. */
3630 if (gui.in_use)
3631 gui_remove_scrollbars();
3632#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003633 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003634 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003635 tp->tp_firstwin = firstwin;
3636 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003637 tp->tp_old_Rows = Rows;
3638 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003639 firstwin = NULL;
3640 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003641 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003642}
3643
3644/*
3645 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003646 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003647 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003648 static void
3649enter_tabpage(tp, old_curbuf)
3650 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003651 buf_T *old_curbuf UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003652{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003653 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003654 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003655
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003656 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003657 firstwin = tp->tp_firstwin;
3658 lastwin = tp->tp_lastwin;
3659 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003660
3661 /* We would like doing the TabEnter event first, but we don't have a
3662 * valid current window yet, which may break some commands.
3663 * This triggers autocommands, thus may make "tp" invalid. */
3664 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3665 prevwin = next_prevwin;
3666
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003667#ifdef FEAT_AUTOCMD
3668 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003669
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003670 if (old_curbuf != curbuf)
3671 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3672#endif
3673
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003674 last_status(FALSE); /* status line may appear or disappear */
3675 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003676 must_redraw = CLEAR; /* need to redraw everything */
3677#ifdef FEAT_DIFF
3678 diff_need_scrollbind = TRUE;
3679#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003680
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003681 /* The tabpage line may have appeared or disappeared, may need to resize
3682 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003683 * frame sizes too. Use the stored value of p_ch, so that it can be
3684 * different for each tab page. */
3685 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003686 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3687#ifdef FEAT_GUI_TABLINE
3688 && !gui_use_tabline()
3689#endif
3690 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003691 shell_new_rows();
3692#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003693 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003694 shell_new_columns(); /* update window widths */
3695#endif
3696
3697#if defined(FEAT_GUI)
3698 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3699 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003700 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003701#endif
3702
3703 redraw_all_later(CLEAR);
3704}
3705
3706/*
3707 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003708 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003709 */
3710 void
3711goto_tabpage(n)
3712 int n;
3713{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003714 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003715 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003716 int i;
3717
Bram Moolenaard68071d2006-05-02 22:08:30 +00003718 if (text_locked())
3719 {
3720 /* Not allowed when editing the command line. */
3721#ifdef FEAT_CMDWIN
3722 if (cmdwin_type != 0)
3723 EMSG(_(e_cmdwin));
3724 else
3725#endif
3726 EMSG(_(e_secure));
3727 return;
3728 }
3729
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003730 /* If there is only one it can't work. */
3731 if (first_tabpage->tp_next == NULL)
3732 {
3733 if (n > 1)
3734 beep_flush();
3735 return;
3736 }
3737
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003738 if (n == 0)
3739 {
3740 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003741 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003742 tp = first_tabpage;
3743 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003744 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003745 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003746 else if (n < 0)
3747 {
3748 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3749 * this N times. */
3750 ttp = curtab;
3751 for (i = n; i < 0; ++i)
3752 {
3753 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3754 tp = tp->tp_next)
3755 ;
3756 ttp = tp;
3757 }
3758 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003759 else if (n == 9999)
3760 {
3761 /* Go to last tab page. */
3762 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3763 ;
3764 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003765 else
3766 {
3767 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003768 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003769 if (tp == NULL)
3770 {
3771 beep_flush();
3772 return;
3773 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003774 }
3775
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003776 goto_tabpage_tp(tp);
3777
3778#ifdef FEAT_GUI_TABLINE
3779 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003780 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003781#endif
3782}
3783
3784/*
3785 * Go to tabpage "tp".
3786 * Note: doesn't update the GUI tab.
3787 */
3788 void
3789goto_tabpage_tp(tp)
3790 tabpage_T *tp;
3791{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003792 /* Don't repeat a message in another tab page. */
3793 set_keep_msg(NULL, 0);
3794
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003795 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003796 {
3797 if (valid_tabpage(tp))
3798 enter_tabpage(tp, curbuf);
3799 else
3800 enter_tabpage(curtab, curbuf);
3801 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003802}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
3804/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003805 * Enter window "wp" in tab page "tp".
3806 * Also updates the GUI tab.
3807 */
3808 void
3809goto_tabpage_win(tp, wp)
3810 tabpage_T *tp;
3811 win_T *wp;
3812{
3813 goto_tabpage_tp(tp);
3814 if (curtab == tp && win_valid(wp))
3815 {
3816 win_enter(wp, TRUE);
3817# ifdef FEAT_GUI_TABLINE
3818 if (gui_use_tabline())
3819 gui_mch_set_curtab(tabpage_index(curtab));
3820# endif
3821 }
3822}
3823
3824/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003825 * Move the current tab page to before tab page "nr".
3826 */
3827 void
3828tabpage_move(nr)
3829 int nr;
3830{
3831 int n = nr;
3832 tabpage_T *tp;
3833
3834 if (first_tabpage->tp_next == NULL)
3835 return;
3836
3837 /* Remove the current tab page from the list of tab pages. */
3838 if (curtab == first_tabpage)
3839 first_tabpage = curtab->tp_next;
3840 else
3841 {
3842 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3843 if (tp->tp_next == curtab)
3844 break;
3845 if (tp == NULL) /* "cannot happen" */
3846 return;
3847 tp->tp_next = curtab->tp_next;
3848 }
3849
3850 /* Re-insert it at the specified position. */
3851 if (n == 0)
3852 {
3853 curtab->tp_next = first_tabpage;
3854 first_tabpage = curtab;
3855 }
3856 else
3857 {
3858 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3859 --n;
3860 curtab->tp_next = tp->tp_next;
3861 tp->tp_next = curtab;
3862 }
3863
3864 /* Need to redraw the tabline. Tab page contents doesn't change. */
3865 redraw_tabline = TRUE;
3866}
3867
3868
3869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 * Go to another window.
3871 * When jumping to another buffer, stop Visual mode. Do this before
3872 * changing windows so we can yank the selection into the '*' register.
3873 * When jumping to another window on the same buffer, adjust its cursor
3874 * position to keep the same Visual area.
3875 */
3876 void
3877win_goto(wp)
3878 win_T *wp;
3879{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003880 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 {
3882 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003883 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 return;
3885 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003886#ifdef FEAT_AUTOCMD
3887 if (curbuf_locked())
3888 return;
3889#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003890
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891#ifdef FEAT_VISUAL
3892 if (wp->w_buffer != curbuf)
3893 reset_VIsual_and_resel();
3894 else if (VIsual_active)
3895 wp->w_cursor = curwin->w_cursor;
3896#endif
3897
3898#ifdef FEAT_GUI
3899 need_mouse_correct = TRUE;
3900#endif
3901 win_enter(wp, TRUE);
3902}
3903
3904#if defined(FEAT_PERL) || defined(PROTO)
3905/*
3906 * Find window number "winnr" (counting top to bottom).
3907 */
3908 win_T *
3909win_find_nr(winnr)
3910 int winnr;
3911{
3912 win_T *wp;
3913
3914# ifdef FEAT_WINDOWS
3915 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3916 if (--winnr == 0)
3917 break;
3918 return wp;
3919# else
3920 return curwin;
3921# endif
3922}
3923#endif
3924
3925#ifdef FEAT_VERTSPLIT
3926/*
3927 * Move to window above or below "count" times.
3928 */
3929 static void
3930win_goto_ver(up, count)
3931 int up; /* TRUE to go to win above */
3932 long count;
3933{
3934 frame_T *fr;
3935 frame_T *nfr;
3936 frame_T *foundfr;
3937
3938 foundfr = curwin->w_frame;
3939 while (count--)
3940 {
3941 /*
3942 * First go upwards in the tree of frames until we find a upwards or
3943 * downwards neighbor.
3944 */
3945 fr = foundfr;
3946 for (;;)
3947 {
3948 if (fr == topframe)
3949 goto end;
3950 if (up)
3951 nfr = fr->fr_prev;
3952 else
3953 nfr = fr->fr_next;
3954 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3955 break;
3956 fr = fr->fr_parent;
3957 }
3958
3959 /*
3960 * Now go downwards to find the bottom or top frame in it.
3961 */
3962 for (;;)
3963 {
3964 if (nfr->fr_layout == FR_LEAF)
3965 {
3966 foundfr = nfr;
3967 break;
3968 }
3969 fr = nfr->fr_child;
3970 if (nfr->fr_layout == FR_ROW)
3971 {
3972 /* Find the frame at the cursor row. */
3973 while (fr->fr_next != NULL
3974 && frame2win(fr)->w_wincol + fr->fr_width
3975 <= curwin->w_wincol + curwin->w_wcol)
3976 fr = fr->fr_next;
3977 }
3978 if (nfr->fr_layout == FR_COL && up)
3979 while (fr->fr_next != NULL)
3980 fr = fr->fr_next;
3981 nfr = fr;
3982 }
3983 }
3984end:
3985 if (foundfr != NULL)
3986 win_goto(foundfr->fr_win);
3987}
3988
3989/*
3990 * Move to left or right window.
3991 */
3992 static void
3993win_goto_hor(left, count)
3994 int left; /* TRUE to go to left win */
3995 long count;
3996{
3997 frame_T *fr;
3998 frame_T *nfr;
3999 frame_T *foundfr;
4000
4001 foundfr = curwin->w_frame;
4002 while (count--)
4003 {
4004 /*
4005 * First go upwards in the tree of frames until we find a left or
4006 * right neighbor.
4007 */
4008 fr = foundfr;
4009 for (;;)
4010 {
4011 if (fr == topframe)
4012 goto end;
4013 if (left)
4014 nfr = fr->fr_prev;
4015 else
4016 nfr = fr->fr_next;
4017 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4018 break;
4019 fr = fr->fr_parent;
4020 }
4021
4022 /*
4023 * Now go downwards to find the leftmost or rightmost frame in it.
4024 */
4025 for (;;)
4026 {
4027 if (nfr->fr_layout == FR_LEAF)
4028 {
4029 foundfr = nfr;
4030 break;
4031 }
4032 fr = nfr->fr_child;
4033 if (nfr->fr_layout == FR_COL)
4034 {
4035 /* Find the frame at the cursor row. */
4036 while (fr->fr_next != NULL
4037 && frame2win(fr)->w_winrow + fr->fr_height
4038 <= curwin->w_winrow + curwin->w_wrow)
4039 fr = fr->fr_next;
4040 }
4041 if (nfr->fr_layout == FR_ROW && left)
4042 while (fr->fr_next != NULL)
4043 fr = fr->fr_next;
4044 nfr = fr;
4045 }
4046 }
4047end:
4048 if (foundfr != NULL)
4049 win_goto(foundfr->fr_win);
4050}
4051#endif
4052
4053/*
4054 * Make window "wp" the current window.
4055 */
4056 void
4057win_enter(wp, undo_sync)
4058 win_T *wp;
4059 int undo_sync;
4060{
4061 win_enter_ext(wp, undo_sync, FALSE);
4062}
4063
4064/*
4065 * Make window wp the current window.
4066 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4067 * been closed and isn't valid.
4068 */
4069 static void
4070win_enter_ext(wp, undo_sync, curwin_invalid)
4071 win_T *wp;
4072 int undo_sync;
4073 int curwin_invalid;
4074{
4075#ifdef FEAT_AUTOCMD
4076 int other_buffer = FALSE;
4077#endif
4078
4079 if (wp == curwin && !curwin_invalid) /* nothing to do */
4080 return;
4081
4082#ifdef FEAT_AUTOCMD
4083 if (!curwin_invalid)
4084 {
4085 /*
4086 * Be careful: If autocommands delete the window, return now.
4087 */
4088 if (wp->w_buffer != curbuf)
4089 {
4090 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4091 other_buffer = TRUE;
4092 if (!win_valid(wp))
4093 return;
4094 }
4095 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4096 if (!win_valid(wp))
4097 return;
4098# ifdef FEAT_EVAL
4099 /* autocmds may abort script processing */
4100 if (aborting())
4101 return;
4102# endif
4103 }
4104#endif
4105
4106 /* sync undo before leaving the current buffer */
4107 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004108 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 /* may have to copy the buffer options when 'cpo' contains 'S' */
4110 if (wp->w_buffer != curbuf)
4111 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4112 if (!curwin_invalid)
4113 {
4114 prevwin = curwin; /* remember for CTRL-W p */
4115 curwin->w_redr_status = TRUE;
4116 }
4117 curwin = wp;
4118 curbuf = wp->w_buffer;
4119 check_cursor();
4120#ifdef FEAT_VIRTUALEDIT
4121 if (!virtual_active())
4122 curwin->w_cursor.coladd = 0;
4123#endif
4124 changed_line_abv_curs(); /* assume cursor position needs updating */
4125
4126 if (curwin->w_localdir != NULL)
4127 {
4128 /* Window has a local directory: Save current directory as global
4129 * directory (unless that was done already) and change to the local
4130 * directory. */
4131 if (globaldir == NULL)
4132 {
4133 char_u cwd[MAXPATHL];
4134
4135 if (mch_dirname(cwd, MAXPATHL) == OK)
4136 globaldir = vim_strsave(cwd);
4137 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004138 if (mch_chdir((char *)curwin->w_localdir) == 0)
4139 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
4141 else if (globaldir != NULL)
4142 {
4143 /* Window doesn't have a local directory and we are not in the global
4144 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004145 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 vim_free(globaldir);
4147 globaldir = NULL;
4148 shorten_fnames(TRUE);
4149 }
4150
4151#ifdef FEAT_AUTOCMD
4152 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4153 if (other_buffer)
4154 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4155#endif
4156
4157#ifdef FEAT_TITLE
4158 maketitle();
4159#endif
4160 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004161 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 if (restart_edit)
4163 redraw_later(VALID); /* causes status line redraw */
4164
4165 /* set window height to desired minimal value */
4166 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4167 win_setheight((int)p_wh);
4168 else if (curwin->w_height == 0)
4169 win_setheight(1);
4170
4171#ifdef FEAT_VERTSPLIT
4172 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004173 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 win_setwidth((int)p_wiw);
4175#endif
4176
4177#ifdef FEAT_MOUSE
4178 setmouse(); /* in case jumped to/from help buffer */
4179#endif
4180
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004181 /* Change directories when the 'acd' option is set. */
4182 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183}
4184
4185#endif /* FEAT_WINDOWS */
4186
4187#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4188/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004189 * Jump to the first open window that contains buffer "buf", if one exists.
4190 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 */
4192 win_T *
4193buf_jump_open_win(buf)
4194 buf_T *buf;
4195{
4196# ifdef FEAT_WINDOWS
4197 win_T *wp;
4198
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004199 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 if (wp->w_buffer == buf)
4201 break;
4202 if (wp != NULL)
4203 win_enter(wp, FALSE);
4204 return wp;
4205# else
4206 if (curwin->w_buffer == buf)
4207 return curwin;
4208 return NULL;
4209# endif
4210}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004211
4212/*
4213 * Jump to the first open window in any tab page that contains buffer "buf",
4214 * if one exists.
4215 * Returns a pointer to the window found, otherwise NULL.
4216 */
4217 win_T *
4218buf_jump_open_tab(buf)
4219 buf_T *buf;
4220{
4221# ifdef FEAT_WINDOWS
4222 win_T *wp;
4223 tabpage_T *tp;
4224
4225 /* First try the current tab page. */
4226 wp = buf_jump_open_win(buf);
4227 if (wp != NULL)
4228 return wp;
4229
4230 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4231 if (tp != curtab)
4232 {
4233 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4234 if (wp->w_buffer == buf)
4235 break;
4236 if (wp != NULL)
4237 {
4238 goto_tabpage_win(tp, wp);
4239 if (curwin != wp)
4240 wp = NULL; /* something went wrong */
4241 break;
4242 }
4243 }
4244
4245 return wp;
4246# else
4247 if (curwin->w_buffer == buf)
4248 return curwin;
4249 return NULL;
4250# endif
4251}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252#endif
4253
4254/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004255 * Allocate a window structure and link it in the window list when "hidden" is
4256 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004259win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004260 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004261 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262{
4263 win_T *newwin;
4264
4265 /*
4266 * allocate window structure and linesizes arrays
4267 */
4268 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4269 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4270 {
4271 vim_free(newwin);
4272 newwin = NULL;
4273 }
4274
4275 if (newwin != NULL)
4276 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004277#ifdef FEAT_AUTOCMD
4278 /* Don't execute autocommands while the window is not properly
4279 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4280 * event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004281 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 /*
4284 * link the window in the window list
4285 */
4286#ifdef FEAT_WINDOWS
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004287 if (!hidden)
4288 win_append(after, newwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289#endif
4290#ifdef FEAT_VERTSPLIT
4291 newwin->w_wincol = 0;
4292 newwin->w_width = Columns;
4293#endif
4294
4295 /* position the display and the cursor at the top of the file. */
4296 newwin->w_topline = 1;
4297#ifdef FEAT_DIFF
4298 newwin->w_topfill = 0;
4299#endif
4300 newwin->w_botline = 2;
4301 newwin->w_cursor.lnum = 1;
4302#ifdef FEAT_SCROLLBIND
4303 newwin->w_scbind_pos = 1;
4304#endif
4305
4306 /* We won't calculate w_fraction until resizing the window */
4307 newwin->w_fraction = 0;
4308 newwin->w_prev_fraction_row = -1;
4309
4310#ifdef FEAT_GUI
4311 if (gui.in_use)
4312 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4314 SBAR_LEFT, newwin);
4315 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4316 SBAR_RIGHT, newwin);
4317 }
4318#endif
4319#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004320 /* init w: variables */
4321 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322#endif
4323#ifdef FEAT_FOLDING
4324 foldInitWin(newwin);
4325#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004326#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004327 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004328#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004329#ifdef FEAT_SEARCH_EXTRA
4330 newwin->w_match_head = NULL;
4331 newwin->w_next_match_id = 4;
4332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 }
4334 return newwin;
4335}
4336
4337#if defined(FEAT_WINDOWS) || defined(PROTO)
4338
4339/*
4340 * remove window 'wp' from the window list and free the structure
4341 */
4342 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004343win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004345 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346{
4347 int i;
4348
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004349#ifdef FEAT_FOLDING
4350 clearFolding(wp);
4351#endif
4352
4353 /* reduce the reference count to the argument list. */
4354 alist_unlink(wp->w_alist);
4355
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004356#ifdef FEAT_AUTOCMD
4357 /* Don't execute autocommands while the window is halfway being deleted.
4358 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004359 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004360#endif
4361
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004362#ifdef FEAT_MZSCHEME
4363 mzscheme_window_free(wp);
4364#endif
4365
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366#ifdef FEAT_PERL
4367 perl_win_free(wp);
4368#endif
4369
4370#ifdef FEAT_PYTHON
4371 python_window_free(wp);
4372#endif
4373
4374#ifdef FEAT_TCL
4375 tcl_window_free(wp);
4376#endif
4377
4378#ifdef FEAT_RUBY
4379 ruby_window_free(wp);
4380#endif
4381
4382 clear_winopt(&wp->w_onebuf_opt);
4383 clear_winopt(&wp->w_allbuf_opt);
4384
4385#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004386 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387#endif
4388
4389 if (prevwin == wp)
4390 prevwin = NULL;
4391 win_free_lsize(wp);
4392
4393 for (i = 0; i < wp->w_tagstacklen; ++i)
4394 vim_free(wp->w_tagstack[i].tagname);
4395
4396 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004397
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004399 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004401
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402#ifdef FEAT_JUMPLIST
4403 free_jumplist(wp);
4404#endif
4405
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004406#ifdef FEAT_QUICKFIX
4407 qf_free_all(wp);
4408#endif
4409
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410#ifdef FEAT_GUI
4411 if (gui.in_use)
4412 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4414 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4415 }
4416#endif /* FEAT_GUI */
4417
Bram Moolenaar860cae12010-06-05 23:22:07 +02004418#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004419 reset_synblock(wp); /* free independent synblock */
Bram Moolenaar1a384422010-07-14 19:53:30 +02004420 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004421#endif
4422
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004423#ifdef FEAT_AUTOCMD
4424 if (wp != aucmd_win)
4425#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004426 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004428
4429#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004430 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432}
4433
4434/*
4435 * Append window "wp" in the window list after window "after".
4436 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004437 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438win_append(after, wp)
4439 win_T *after, *wp;
4440{
4441 win_T *before;
4442
4443 if (after == NULL) /* after NULL is in front of the first */
4444 before = firstwin;
4445 else
4446 before = after->w_next;
4447
4448 wp->w_next = before;
4449 wp->w_prev = after;
4450 if (after == NULL)
4451 firstwin = wp;
4452 else
4453 after->w_next = wp;
4454 if (before == NULL)
4455 lastwin = wp;
4456 else
4457 before->w_prev = wp;
4458}
4459
4460/*
4461 * Remove a window from the window list.
4462 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004463 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004464win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004466 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467{
4468 if (wp->w_prev != NULL)
4469 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004470 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004472 else
4473 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 if (wp->w_next != NULL)
4475 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004476 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004478 else
4479 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480}
4481
4482/*
4483 * Append frame "frp" in a frame list after frame "after".
4484 */
4485 static void
4486frame_append(after, frp)
4487 frame_T *after, *frp;
4488{
4489 frp->fr_next = after->fr_next;
4490 after->fr_next = frp;
4491 if (frp->fr_next != NULL)
4492 frp->fr_next->fr_prev = frp;
4493 frp->fr_prev = after;
4494}
4495
4496/*
4497 * Insert frame "frp" in a frame list before frame "before".
4498 */
4499 static void
4500frame_insert(before, frp)
4501 frame_T *before, *frp;
4502{
4503 frp->fr_next = before;
4504 frp->fr_prev = before->fr_prev;
4505 before->fr_prev = frp;
4506 if (frp->fr_prev != NULL)
4507 frp->fr_prev->fr_next = frp;
4508 else
4509 frp->fr_parent->fr_child = frp;
4510}
4511
4512/*
4513 * Remove a frame from a frame list.
4514 */
4515 static void
4516frame_remove(frp)
4517 frame_T *frp;
4518{
4519 if (frp->fr_prev != NULL)
4520 frp->fr_prev->fr_next = frp->fr_next;
4521 else
4522 frp->fr_parent->fr_child = frp->fr_next;
4523 if (frp->fr_next != NULL)
4524 frp->fr_next->fr_prev = frp->fr_prev;
4525}
4526
4527#endif /* FEAT_WINDOWS */
4528
4529/*
4530 * Allocate w_lines[] for window "wp".
4531 * Return FAIL for failure, OK for success.
4532 */
4533 int
4534win_alloc_lines(wp)
4535 win_T *wp;
4536{
4537 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004538 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 if (wp->w_lines == NULL)
4540 return FAIL;
4541 return OK;
4542}
4543
4544/*
4545 * free lsize arrays for a window
4546 */
4547 void
4548win_free_lsize(wp)
4549 win_T *wp;
4550{
4551 vim_free(wp->w_lines);
4552 wp->w_lines = NULL;
4553}
4554
4555/*
4556 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004557 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 */
4559 void
4560shell_new_rows()
4561{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004562 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563
4564 if (firstwin == NULL) /* not initialized yet */
4565 return;
4566#ifdef FEAT_WINDOWS
4567 if (h < frame_minheight(topframe, NULL))
4568 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004569
4570 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 * that doesn't result in the right height, forget about that option. */
4572 frame_new_height(topframe, h, FALSE, TRUE);
4573 if (topframe->fr_height != h)
4574 frame_new_height(topframe, h, FALSE, FALSE);
4575
4576 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4577#else
4578 if (h < 1)
4579 h = 1;
4580 win_new_height(firstwin, h);
4581#endif
4582 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004583#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004584 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004585#endif
4586
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587#if 0
4588 /* Disabled: don't want making the screen smaller make a window larger. */
4589 if (p_ea)
4590 win_equal(curwin, FALSE, 'v');
4591#endif
4592}
4593
4594#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4595/*
4596 * Called from win_new_shellsize() after Columns changed.
4597 */
4598 void
4599shell_new_columns()
4600{
4601 if (firstwin == NULL) /* not initialized yet */
4602 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004603
4604 /* First try setting the widths of windows with 'winfixwidth'. If that
4605 * doesn't result in the right width, forget about that option. */
4606 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4607 if (topframe->fr_width != Columns)
4608 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4609
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4611#if 0
4612 /* Disabled: don't want making the screen smaller make a window larger. */
4613 if (p_ea)
4614 win_equal(curwin, FALSE, 'h');
4615#endif
4616}
4617#endif
4618
4619#if defined(FEAT_CMDWIN) || defined(PROTO)
4620/*
4621 * Save the size of all windows in "gap".
4622 */
4623 void
4624win_size_save(gap)
4625 garray_T *gap;
4626
4627{
4628 win_T *wp;
4629
4630 ga_init2(gap, (int)sizeof(int), 1);
4631 if (ga_grow(gap, win_count() * 2) == OK)
4632 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4633 {
4634 ((int *)gap->ga_data)[gap->ga_len++] =
4635 wp->w_width + wp->w_vsep_width;
4636 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4637 }
4638}
4639
4640/*
4641 * Restore window sizes, but only if the number of windows is still the same.
4642 * Does not free the growarray.
4643 */
4644 void
4645win_size_restore(gap)
4646 garray_T *gap;
4647{
4648 win_T *wp;
4649 int i;
4650
4651 if (win_count() * 2 == gap->ga_len)
4652 {
4653 i = 0;
4654 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4655 {
4656 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4657 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4658 }
4659 /* recompute the window positions */
4660 (void)win_comp_pos();
4661 }
4662}
4663#endif /* FEAT_CMDWIN */
4664
4665#if defined(FEAT_WINDOWS) || defined(PROTO)
4666/*
4667 * Update the position for all windows, using the width and height of the
4668 * frames.
4669 * Returns the row just after the last window.
4670 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004671 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672win_comp_pos()
4673{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004674 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 int col = 0;
4676
4677 frame_comp_pos(topframe, &row, &col);
4678 return row;
4679}
4680
4681/*
4682 * Update the position of the windows in frame "topfrp", using the width and
4683 * height of the frames.
4684 * "*row" and "*col" are the top-left position of the frame. They are updated
4685 * to the bottom-right position plus one.
4686 */
4687 static void
4688frame_comp_pos(topfrp, row, col)
4689 frame_T *topfrp;
4690 int *row;
4691 int *col;
4692{
4693 win_T *wp;
4694 frame_T *frp;
4695#ifdef FEAT_VERTSPLIT
4696 int startcol;
4697 int startrow;
4698#endif
4699
4700 wp = topfrp->fr_win;
4701 if (wp != NULL)
4702 {
4703 if (wp->w_winrow != *row
4704#ifdef FEAT_VERTSPLIT
4705 || wp->w_wincol != *col
4706#endif
4707 )
4708 {
4709 /* position changed, redraw */
4710 wp->w_winrow = *row;
4711#ifdef FEAT_VERTSPLIT
4712 wp->w_wincol = *col;
4713#endif
4714 redraw_win_later(wp, NOT_VALID);
4715 wp->w_redr_status = TRUE;
4716 }
4717 *row += wp->w_height + wp->w_status_height;
4718#ifdef FEAT_VERTSPLIT
4719 *col += wp->w_width + wp->w_vsep_width;
4720#endif
4721 }
4722 else
4723 {
4724#ifdef FEAT_VERTSPLIT
4725 startrow = *row;
4726 startcol = *col;
4727#endif
4728 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4729 {
4730#ifdef FEAT_VERTSPLIT
4731 if (topfrp->fr_layout == FR_ROW)
4732 *row = startrow; /* all frames are at the same row */
4733 else
4734 *col = startcol; /* all frames are at the same col */
4735#endif
4736 frame_comp_pos(frp, row, col);
4737 }
4738 }
4739}
4740
4741#endif /* FEAT_WINDOWS */
4742
4743/*
4744 * Set current window height and take care of repositioning other windows to
4745 * fit around it.
4746 */
4747 void
4748win_setheight(height)
4749 int height;
4750{
4751 win_setheight_win(height, curwin);
4752}
4753
4754/*
4755 * Set the window height of window "win" and take care of repositioning other
4756 * windows to fit around it.
4757 */
4758 void
4759win_setheight_win(height, win)
4760 int height;
4761 win_T *win;
4762{
4763 int row;
4764
4765 if (win == curwin)
4766 {
4767 /* Always keep current window at least one line high, even when
4768 * 'winminheight' is zero. */
4769#ifdef FEAT_WINDOWS
4770 if (height < p_wmh)
4771 height = p_wmh;
4772#endif
4773 if (height == 0)
4774 height = 1;
4775 }
4776
4777#ifdef FEAT_WINDOWS
4778 frame_setheight(win->w_frame, height + win->w_status_height);
4779
4780 /* recompute the window positions */
4781 row = win_comp_pos();
4782#else
4783 if (height > topframe->fr_height)
4784 height = topframe->fr_height;
4785 win->w_height = height;
4786 row = height;
4787#endif
4788
4789 /*
4790 * If there is extra space created between the last window and the command
4791 * line, clear it.
4792 */
4793 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4794 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4795 cmdline_row = row;
4796 msg_row = row;
4797 msg_col = 0;
4798
4799 redraw_all_later(NOT_VALID);
4800}
4801
4802#if defined(FEAT_WINDOWS) || defined(PROTO)
4803
4804/*
4805 * Set the height of a frame to "height" and take care that all frames and
4806 * windows inside it are resized. Also resize frames on the left and right if
4807 * the are in the same FR_ROW frame.
4808 *
4809 * Strategy:
4810 * If the frame is part of a FR_COL frame, try fitting the frame in that
4811 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4812 * go to containing frames to resize them and make room.
4813 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4814 * Check for the minimal height of the FR_ROW frame.
4815 * At the top level we can also use change the command line height.
4816 */
4817 static void
4818frame_setheight(curfrp, height)
4819 frame_T *curfrp;
4820 int height;
4821{
4822 int room; /* total number of lines available */
4823 int take; /* number of lines taken from other windows */
4824 int room_cmdline; /* lines available from cmdline */
4825 int run;
4826 frame_T *frp;
4827 int h;
4828 int room_reserved;
4829
4830 /* If the height already is the desired value, nothing to do. */
4831 if (curfrp->fr_height == height)
4832 return;
4833
4834 if (curfrp->fr_parent == NULL)
4835 {
4836 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004837 if (height > ROWS_AVAIL)
4838 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 if (height > 0)
4840 frame_new_height(curfrp, height, FALSE, FALSE);
4841 }
4842 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4843 {
4844 /* Row of frames: Also need to resize frames left and right of this
4845 * one. First check for the minimal height of these. */
4846 h = frame_minheight(curfrp->fr_parent, NULL);
4847 if (height < h)
4848 height = h;
4849 frame_setheight(curfrp->fr_parent, height);
4850 }
4851 else
4852 {
4853 /*
4854 * Column of frames: try to change only frames in this column.
4855 */
4856#ifdef FEAT_VERTSPLIT
4857 /*
4858 * Do this twice:
4859 * 1: compute room available, if it's not enough try resizing the
4860 * containing frame.
4861 * 2: compute the room available and adjust the height to it.
4862 * Try not to reduce the height of a window with 'winfixheight' set.
4863 */
4864 for (run = 1; run <= 2; ++run)
4865#else
4866 for (;;)
4867#endif
4868 {
4869 room = 0;
4870 room_reserved = 0;
4871 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4872 frp = frp->fr_next)
4873 {
4874 if (frp != curfrp
4875 && frp->fr_win != NULL
4876 && frp->fr_win->w_p_wfh)
4877 room_reserved += frp->fr_height;
4878 room += frp->fr_height;
4879 if (frp != curfrp)
4880 room -= frame_minheight(frp, NULL);
4881 }
4882#ifdef FEAT_VERTSPLIT
4883 if (curfrp->fr_width != Columns)
4884 room_cmdline = 0;
4885 else
4886#endif
4887 {
4888 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4889 + lastwin->w_height + lastwin->w_status_height);
4890 if (room_cmdline < 0)
4891 room_cmdline = 0;
4892 }
4893
4894 if (height <= room + room_cmdline)
4895 break;
4896#ifdef FEAT_VERTSPLIT
4897 if (run == 2 || curfrp->fr_width == Columns)
4898#endif
4899 {
4900 if (height > room + room_cmdline)
4901 height = room + room_cmdline;
4902 break;
4903 }
4904#ifdef FEAT_VERTSPLIT
4905 frame_setheight(curfrp->fr_parent, height
4906 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4907#endif
4908 /*NOTREACHED*/
4909 }
4910
4911 /*
4912 * Compute the number of lines we will take from others frames (can be
4913 * negative!).
4914 */
4915 take = height - curfrp->fr_height;
4916
4917 /* If there is not enough room, also reduce the height of a window
4918 * with 'winfixheight' set. */
4919 if (height > room + room_cmdline - room_reserved)
4920 room_reserved = room + room_cmdline - height;
4921 /* If there is only a 'winfixheight' window and making the
4922 * window smaller, need to make the other window taller. */
4923 if (take < 0 && room - curfrp->fr_height < room_reserved)
4924 room_reserved = 0;
4925
4926 if (take > 0 && room_cmdline > 0)
4927 {
4928 /* use lines from cmdline first */
4929 if (take < room_cmdline)
4930 room_cmdline = take;
4931 take -= room_cmdline;
4932 topframe->fr_height += room_cmdline;
4933 }
4934
4935 /*
4936 * set the current frame to the new height
4937 */
4938 frame_new_height(curfrp, height, FALSE, FALSE);
4939
4940 /*
4941 * First take lines from the frames after the current frame. If
4942 * that is not enough, takes lines from frames above the current
4943 * frame.
4944 */
4945 for (run = 0; run < 2; ++run)
4946 {
4947 if (run == 0)
4948 frp = curfrp->fr_next; /* 1st run: start with next window */
4949 else
4950 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4951 while (frp != NULL && take != 0)
4952 {
4953 h = frame_minheight(frp, NULL);
4954 if (room_reserved > 0
4955 && frp->fr_win != NULL
4956 && frp->fr_win->w_p_wfh)
4957 {
4958 if (room_reserved >= frp->fr_height)
4959 room_reserved -= frp->fr_height;
4960 else
4961 {
4962 if (frp->fr_height - room_reserved > take)
4963 room_reserved = frp->fr_height - take;
4964 take -= frp->fr_height - room_reserved;
4965 frame_new_height(frp, room_reserved, FALSE, FALSE);
4966 room_reserved = 0;
4967 }
4968 }
4969 else
4970 {
4971 if (frp->fr_height - take < h)
4972 {
4973 take -= frp->fr_height - h;
4974 frame_new_height(frp, h, FALSE, FALSE);
4975 }
4976 else
4977 {
4978 frame_new_height(frp, frp->fr_height - take,
4979 FALSE, FALSE);
4980 take = 0;
4981 }
4982 }
4983 if (run == 0)
4984 frp = frp->fr_next;
4985 else
4986 frp = frp->fr_prev;
4987 }
4988 }
4989 }
4990}
4991
4992#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4993/*
4994 * Set current window width and take care of repositioning other windows to
4995 * fit around it.
4996 */
4997 void
4998win_setwidth(width)
4999 int width;
5000{
5001 win_setwidth_win(width, curwin);
5002}
5003
5004 void
5005win_setwidth_win(width, wp)
5006 int width;
5007 win_T *wp;
5008{
5009 /* Always keep current window at least one column wide, even when
5010 * 'winminwidth' is zero. */
5011 if (wp == curwin)
5012 {
5013 if (width < p_wmw)
5014 width = p_wmw;
5015 if (width == 0)
5016 width = 1;
5017 }
5018
5019 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5020
5021 /* recompute the window positions */
5022 (void)win_comp_pos();
5023
5024 redraw_all_later(NOT_VALID);
5025}
5026
5027/*
5028 * Set the width of a frame to "width" and take care that all frames and
5029 * windows inside it are resized. Also resize frames above and below if the
5030 * are in the same FR_ROW frame.
5031 *
5032 * Strategy is similar to frame_setheight().
5033 */
5034 static void
5035frame_setwidth(curfrp, width)
5036 frame_T *curfrp;
5037 int width;
5038{
5039 int room; /* total number of lines available */
5040 int take; /* number of lines taken from other windows */
5041 int run;
5042 frame_T *frp;
5043 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005044 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045
5046 /* If the width already is the desired value, nothing to do. */
5047 if (curfrp->fr_width == width)
5048 return;
5049
5050 if (curfrp->fr_parent == NULL)
5051 /* topframe: can't change width */
5052 return;
5053
5054 if (curfrp->fr_parent->fr_layout == FR_COL)
5055 {
5056 /* Column of frames: Also need to resize frames above and below of
5057 * this one. First check for the minimal width of these. */
5058 w = frame_minwidth(curfrp->fr_parent, NULL);
5059 if (width < w)
5060 width = w;
5061 frame_setwidth(curfrp->fr_parent, width);
5062 }
5063 else
5064 {
5065 /*
5066 * Row of frames: try to change only frames in this row.
5067 *
5068 * Do this twice:
5069 * 1: compute room available, if it's not enough try resizing the
5070 * containing frame.
5071 * 2: compute the room available and adjust the width to it.
5072 */
5073 for (run = 1; run <= 2; ++run)
5074 {
5075 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005076 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5078 frp = frp->fr_next)
5079 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005080 if (frp != curfrp
5081 && frp->fr_win != NULL
5082 && frp->fr_win->w_p_wfw)
5083 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 room += frp->fr_width;
5085 if (frp != curfrp)
5086 room -= frame_minwidth(frp, NULL);
5087 }
5088
5089 if (width <= room)
5090 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005091 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
5093 if (width > room)
5094 width = room;
5095 break;
5096 }
5097 frame_setwidth(curfrp->fr_parent, width
5098 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5099 }
5100
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 /*
5102 * Compute the number of lines we will take from others frames (can be
5103 * negative!).
5104 */
5105 take = width - curfrp->fr_width;
5106
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005107 /* If there is not enough room, also reduce the width of a window
5108 * with 'winfixwidth' set. */
5109 if (width > room - room_reserved)
5110 room_reserved = room - width;
5111 /* If there is only a 'winfixwidth' window and making the
5112 * window smaller, need to make the other window narrower. */
5113 if (take < 0 && room - curfrp->fr_width < room_reserved)
5114 room_reserved = 0;
5115
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 /*
5117 * set the current frame to the new width
5118 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005119 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120
5121 /*
5122 * First take lines from the frames right of the current frame. If
5123 * that is not enough, takes lines from frames left of the current
5124 * frame.
5125 */
5126 for (run = 0; run < 2; ++run)
5127 {
5128 if (run == 0)
5129 frp = curfrp->fr_next; /* 1st run: start with next window */
5130 else
5131 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5132 while (frp != NULL && take != 0)
5133 {
5134 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005135 if (room_reserved > 0
5136 && frp->fr_win != NULL
5137 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005139 if (room_reserved >= frp->fr_width)
5140 room_reserved -= frp->fr_width;
5141 else
5142 {
5143 if (frp->fr_width - room_reserved > take)
5144 room_reserved = frp->fr_width - take;
5145 take -= frp->fr_width - room_reserved;
5146 frame_new_width(frp, room_reserved, FALSE, FALSE);
5147 room_reserved = 0;
5148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150 else
5151 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005152 if (frp->fr_width - take < w)
5153 {
5154 take -= frp->fr_width - w;
5155 frame_new_width(frp, w, FALSE, FALSE);
5156 }
5157 else
5158 {
5159 frame_new_width(frp, frp->fr_width - take,
5160 FALSE, FALSE);
5161 take = 0;
5162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
5164 if (run == 0)
5165 frp = frp->fr_next;
5166 else
5167 frp = frp->fr_prev;
5168 }
5169 }
5170 }
5171}
5172#endif /* FEAT_VERTSPLIT */
5173
5174/*
5175 * Check 'winminheight' for a valid value.
5176 */
5177 void
5178win_setminheight()
5179{
5180 int room;
5181 int first = TRUE;
5182 win_T *wp;
5183
5184 /* loop until there is a 'winminheight' that is possible */
5185 while (p_wmh > 0)
5186 {
5187 /* TODO: handle vertical splits */
5188 room = -p_wh;
5189 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5190 room += wp->w_height - p_wmh;
5191 if (room >= 0)
5192 break;
5193 --p_wmh;
5194 if (first)
5195 {
5196 EMSG(_(e_noroom));
5197 first = FALSE;
5198 }
5199 }
5200}
5201
5202#ifdef FEAT_MOUSE
5203
5204/*
5205 * Status line of dragwin is dragged "offset" lines down (negative is up).
5206 */
5207 void
5208win_drag_status_line(dragwin, offset)
5209 win_T *dragwin;
5210 int offset;
5211{
5212 frame_T *curfr;
5213 frame_T *fr;
5214 int room;
5215 int row;
5216 int up; /* if TRUE, drag status line up, otherwise down */
5217 int n;
5218
5219 fr = dragwin->w_frame;
5220 curfr = fr;
5221 if (fr != topframe) /* more than one window */
5222 {
5223 fr = fr->fr_parent;
5224 /* When the parent frame is not a column of frames, its parent should
5225 * be. */
5226 if (fr->fr_layout != FR_COL)
5227 {
5228 curfr = fr;
5229 if (fr != topframe) /* only a row of windows, may drag statusline */
5230 fr = fr->fr_parent;
5231 }
5232 }
5233
5234 /* If this is the last frame in a column, may want to resize the parent
5235 * frame instead (go two up to skip a row of frames). */
5236 while (curfr != topframe && curfr->fr_next == NULL)
5237 {
5238 if (fr != topframe)
5239 fr = fr->fr_parent;
5240 curfr = fr;
5241 if (fr != topframe)
5242 fr = fr->fr_parent;
5243 }
5244
5245 if (offset < 0) /* drag up */
5246 {
5247 up = TRUE;
5248 offset = -offset;
5249 /* sum up the room of the current frame and above it */
5250 if (fr == curfr)
5251 {
5252 /* only one window */
5253 room = fr->fr_height - frame_minheight(fr, NULL);
5254 }
5255 else
5256 {
5257 room = 0;
5258 for (fr = fr->fr_child; ; fr = fr->fr_next)
5259 {
5260 room += fr->fr_height - frame_minheight(fr, NULL);
5261 if (fr == curfr)
5262 break;
5263 }
5264 }
5265 fr = curfr->fr_next; /* put fr at frame that grows */
5266 }
5267 else /* drag down */
5268 {
5269 up = FALSE;
5270 /*
5271 * Only dragging the last status line can reduce p_ch.
5272 */
5273 room = Rows - cmdline_row;
5274 if (curfr->fr_next == NULL)
5275 room -= 1;
5276 else
5277 room -= p_ch;
5278 if (room < 0)
5279 room = 0;
5280 /* sum up the room of frames below of the current one */
5281 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5282 room += fr->fr_height - frame_minheight(fr, NULL);
5283 fr = curfr; /* put fr at window that grows */
5284 }
5285
5286 if (room < offset) /* Not enough room */
5287 offset = room; /* Move as far as we can */
5288 if (offset <= 0)
5289 return;
5290
5291 /*
5292 * Grow frame fr by "offset" lines.
5293 * Doesn't happen when dragging the last status line up.
5294 */
5295 if (fr != NULL)
5296 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5297
5298 if (up)
5299 fr = curfr; /* current frame gets smaller */
5300 else
5301 fr = curfr->fr_next; /* next frame gets smaller */
5302
5303 /*
5304 * Now make the other frames smaller.
5305 */
5306 while (fr != NULL && offset > 0)
5307 {
5308 n = frame_minheight(fr, NULL);
5309 if (fr->fr_height - offset <= n)
5310 {
5311 offset -= fr->fr_height - n;
5312 frame_new_height(fr, n, !up, FALSE);
5313 }
5314 else
5315 {
5316 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5317 break;
5318 }
5319 if (up)
5320 fr = fr->fr_prev;
5321 else
5322 fr = fr->fr_next;
5323 }
5324 row = win_comp_pos();
5325 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5326 cmdline_row = row;
5327 p_ch = Rows - cmdline_row;
5328 if (p_ch < 1)
5329 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005330 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005331 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 showmode();
5333}
5334
5335#ifdef FEAT_VERTSPLIT
5336/*
5337 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5338 */
5339 void
5340win_drag_vsep_line(dragwin, offset)
5341 win_T *dragwin;
5342 int offset;
5343{
5344 frame_T *curfr;
5345 frame_T *fr;
5346 int room;
5347 int left; /* if TRUE, drag separator line left, otherwise right */
5348 int n;
5349
5350 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005351 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 return;
5353 curfr = fr;
5354 fr = fr->fr_parent;
5355 /* When the parent frame is not a row of frames, its parent should be. */
5356 if (fr->fr_layout != FR_ROW)
5357 {
5358 if (fr == topframe) /* only a column of windows (cannot happen?) */
5359 return;
5360 curfr = fr;
5361 fr = fr->fr_parent;
5362 }
5363
5364 /* If this is the last frame in a row, may want to resize a parent
5365 * frame instead. */
5366 while (curfr->fr_next == NULL)
5367 {
5368 if (fr == topframe)
5369 break;
5370 curfr = fr;
5371 fr = fr->fr_parent;
5372 if (fr != topframe)
5373 {
5374 curfr = fr;
5375 fr = fr->fr_parent;
5376 }
5377 }
5378
5379 if (offset < 0) /* drag left */
5380 {
5381 left = TRUE;
5382 offset = -offset;
5383 /* sum up the room of the current frame and left of it */
5384 room = 0;
5385 for (fr = fr->fr_child; ; fr = fr->fr_next)
5386 {
5387 room += fr->fr_width - frame_minwidth(fr, NULL);
5388 if (fr == curfr)
5389 break;
5390 }
5391 fr = curfr->fr_next; /* put fr at frame that grows */
5392 }
5393 else /* drag right */
5394 {
5395 left = FALSE;
5396 /* sum up the room of frames right of the current one */
5397 room = 0;
5398 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5399 room += fr->fr_width - frame_minwidth(fr, NULL);
5400 fr = curfr; /* put fr at window that grows */
5401 }
5402
5403 if (room < offset) /* Not enough room */
5404 offset = room; /* Move as far as we can */
5405 if (offset <= 0) /* No room at all, quit. */
5406 return;
5407
5408 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005409 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
5411 /* shrink other frames: current and at the left or at the right */
5412 if (left)
5413 fr = curfr; /* current frame gets smaller */
5414 else
5415 fr = curfr->fr_next; /* next frame gets smaller */
5416
5417 while (fr != NULL && offset > 0)
5418 {
5419 n = frame_minwidth(fr, NULL);
5420 if (fr->fr_width - offset <= n)
5421 {
5422 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005423 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 }
5425 else
5426 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005427 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 break;
5429 }
5430 if (left)
5431 fr = fr->fr_prev;
5432 else
5433 fr = fr->fr_next;
5434 }
5435 (void)win_comp_pos();
5436 redraw_all_later(NOT_VALID);
5437}
5438#endif /* FEAT_VERTSPLIT */
5439#endif /* FEAT_MOUSE */
5440
5441#endif /* FEAT_WINDOWS */
5442
5443/*
5444 * Set the height of a window.
5445 * This takes care of the things inside the window, not what happens to the
5446 * window position, the frame or to other windows.
5447 */
5448 static void
5449win_new_height(wp, height)
5450 win_T *wp;
5451 int height;
5452{
5453 linenr_T lnum;
5454 int sline, line_size;
5455#define FRACTION_MULT 16384L
5456
5457 /* Don't want a negative height. Happens when splitting a tiny window.
5458 * Will equalize heights soon to fix it. */
5459 if (height < 0)
5460 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005461 if (wp->w_height == height)
5462 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463
5464 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5465 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5466 + FRACTION_MULT / 2) / (long)wp->w_height;
5467
5468 wp->w_height = height;
5469 wp->w_skipcol = 0;
5470
5471 /* Don't change w_topline when height is zero. Don't set w_topline when
5472 * 'scrollbind' is set and this isn't the current window. */
5473 if (height > 0
5474#ifdef FEAT_SCROLLBIND
5475 && (!wp->w_p_scb || wp == curwin)
5476#endif
5477 )
5478 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005479 /*
5480 * Find a value for w_topline that shows the cursor at the same
5481 * relative position in the window as before (more or less).
5482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 lnum = wp->w_cursor.lnum;
5484 if (lnum < 1) /* can happen when starting up */
5485 lnum = 1;
5486 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5487 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5488 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005489
5490 if (sline >= 0)
5491 {
5492 /* Make sure the whole cursor line is visible, if possible. */
5493 int rows = plines_win(wp, lnum, FALSE);
5494
5495 if (sline > wp->w_height - rows)
5496 {
5497 sline = wp->w_height - rows;
5498 wp->w_wrow -= rows - line_size;
5499 }
5500 }
5501
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 if (sline < 0)
5503 {
5504 /*
5505 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005506 * Make cursor line the first line in the window. If not enough
5507 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 */
5509 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005510 if (wp->w_wrow >= wp->w_height
5511 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5512 {
5513 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5514 --wp->w_wrow;
5515 while (wp->w_wrow >= wp->w_height)
5516 {
5517 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5518 + win_col_off2(wp);
5519 --wp->w_wrow;
5520 }
5521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 }
5523 else
5524 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005525 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 {
5527#ifdef FEAT_FOLDING
5528 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5529 if (lnum == 1)
5530 {
5531 /* first line in buffer is folded */
5532 line_size = 1;
5533 --sline;
5534 break;
5535 }
5536#endif
5537 --lnum;
5538#ifdef FEAT_DIFF
5539 if (lnum == wp->w_topline)
5540 line_size = plines_win_nofill(wp, lnum, TRUE)
5541 + wp->w_topfill;
5542 else
5543#endif
5544 line_size = plines_win(wp, lnum, TRUE);
5545 sline -= line_size;
5546 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005547
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 if (sline < 0)
5549 {
5550 /*
5551 * Line we want at top would go off top of screen. Use next
5552 * line instead.
5553 */
5554#ifdef FEAT_FOLDING
5555 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5556#endif
5557 lnum++;
5558 wp->w_wrow -= line_size + sline;
5559 }
5560 else if (sline > 0)
5561 {
5562 /* First line of file reached, use that as topline. */
5563 lnum = 1;
5564 wp->w_wrow -= sline;
5565 }
5566 }
5567 set_topline(wp, lnum);
5568 }
5569
5570 if (wp == curwin)
5571 {
5572 if (p_so)
5573 update_topline();
5574 curs_columns(FALSE); /* validate w_wrow */
5575 }
5576 wp->w_prev_fraction_row = wp->w_wrow;
5577
5578 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005579 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580#ifdef FEAT_WINDOWS
5581 wp->w_redr_status = TRUE;
5582#endif
5583 invalidate_botline_win(wp);
5584}
5585
5586#ifdef FEAT_VERTSPLIT
5587/*
5588 * Set the width of a window.
5589 */
5590 static void
5591win_new_width(wp, width)
5592 win_T *wp;
5593 int width;
5594{
5595 wp->w_width = width;
5596 wp->w_lines_valid = 0;
5597 changed_line_abv_curs_win(wp);
5598 invalidate_botline_win(wp);
5599 if (wp == curwin)
5600 {
5601 update_topline();
5602 curs_columns(TRUE); /* validate w_wrow */
5603 }
5604 redraw_win_later(wp, NOT_VALID);
5605 wp->w_redr_status = TRUE;
5606}
5607#endif
5608
5609 void
5610win_comp_scroll(wp)
5611 win_T *wp;
5612{
5613 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5614 if (wp->w_p_scr == 0)
5615 wp->w_p_scr = 1;
5616}
5617
5618/*
5619 * command_height: called whenever p_ch has been changed
5620 */
5621 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005622command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623{
5624#ifdef FEAT_WINDOWS
5625 int h;
5626 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005627 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005629 /* Use the value of p_ch that we remembered. This is needed for when the
5630 * GUI starts up, we can't be sure in what order things happen. And when
5631 * p_ch was changed in another tab page. */
5632 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005633
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 /* Find bottom frame with width of screen. */
5635 frp = lastwin->w_frame;
5636# ifdef FEAT_VERTSPLIT
5637 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5638 frp = frp->fr_parent;
5639# endif
5640
5641 /* Avoid changing the height of a window with 'winfixheight' set. */
5642 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5643 && frp->fr_win->w_p_wfh)
5644 frp = frp->fr_prev;
5645
5646 if (starting != NO_SCREEN)
5647 {
5648 cmdline_row = Rows - p_ch;
5649
5650 if (p_ch > old_p_ch) /* p_ch got bigger */
5651 {
5652 while (p_ch > old_p_ch)
5653 {
5654 if (frp == NULL)
5655 {
5656 EMSG(_(e_noroom));
5657 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005658 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 cmdline_row = Rows - p_ch;
5660 break;
5661 }
5662 h = frp->fr_height - frame_minheight(frp, NULL);
5663 if (h > p_ch - old_p_ch)
5664 h = p_ch - old_p_ch;
5665 old_p_ch += h;
5666 frame_add_height(frp, -h);
5667 frp = frp->fr_prev;
5668 }
5669
5670 /* Recompute window positions. */
5671 (void)win_comp_pos();
5672
5673 /* clear the lines added to cmdline */
5674 if (full_screen)
5675 screen_fill((int)(cmdline_row), (int)Rows, 0,
5676 (int)Columns, ' ', ' ', 0);
5677 msg_row = cmdline_row;
5678 redraw_cmdline = TRUE;
5679 return;
5680 }
5681
5682 if (msg_row < cmdline_row)
5683 msg_row = cmdline_row;
5684 redraw_cmdline = TRUE;
5685 }
5686 frame_add_height(frp, (int)(old_p_ch - p_ch));
5687
5688 /* Recompute window positions. */
5689 if (frp != lastwin->w_frame)
5690 (void)win_comp_pos();
5691#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005693 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694#endif
5695}
5696
5697#if defined(FEAT_WINDOWS) || defined(PROTO)
5698/*
5699 * Resize frame "frp" to be "n" lines higher (negative for less high).
5700 * Also resize the frames it is contained in.
5701 */
5702 static void
5703frame_add_height(frp, n)
5704 frame_T *frp;
5705 int n;
5706{
5707 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5708 for (;;)
5709 {
5710 frp = frp->fr_parent;
5711 if (frp == NULL)
5712 break;
5713 frp->fr_height += n;
5714 }
5715}
5716
5717/*
5718 * Add or remove a status line for the bottom window(s), according to the
5719 * value of 'laststatus'.
5720 */
5721 void
5722last_status(morewin)
5723 int morewin; /* pretend there are two or more windows */
5724{
5725 /* Don't make a difference between horizontal or vertical split. */
5726 last_status_rec(topframe, (p_ls == 2
5727 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5728}
5729
5730 static void
5731last_status_rec(fr, statusline)
5732 frame_T *fr;
5733 int statusline;
5734{
5735 frame_T *fp;
5736 win_T *wp;
5737
5738 if (fr->fr_layout == FR_LEAF)
5739 {
5740 wp = fr->fr_win;
5741 if (wp->w_status_height != 0 && !statusline)
5742 {
5743 /* remove status line */
5744 win_new_height(wp, wp->w_height + 1);
5745 wp->w_status_height = 0;
5746 comp_col();
5747 }
5748 else if (wp->w_status_height == 0 && statusline)
5749 {
5750 /* Find a frame to take a line from. */
5751 fp = fr;
5752 while (fp->fr_height <= frame_minheight(fp, NULL))
5753 {
5754 if (fp == topframe)
5755 {
5756 EMSG(_(e_noroom));
5757 return;
5758 }
5759 /* In a column of frames: go to frame above. If already at
5760 * the top or in a row of frames: go to parent. */
5761 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5762 fp = fp->fr_prev;
5763 else
5764 fp = fp->fr_parent;
5765 }
5766 wp->w_status_height = 1;
5767 if (fp != fr)
5768 {
5769 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5770 frame_fix_height(wp);
5771 (void)win_comp_pos();
5772 }
5773 else
5774 win_new_height(wp, wp->w_height - 1);
5775 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005776 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 }
5778 }
5779#ifdef FEAT_VERTSPLIT
5780 else if (fr->fr_layout == FR_ROW)
5781 {
5782 /* vertically split windows, set status line for each one */
5783 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5784 last_status_rec(fp, statusline);
5785 }
5786#endif
5787 else
5788 {
5789 /* horizontally split window, set status line for last one */
5790 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5791 ;
5792 last_status_rec(fp, statusline);
5793 }
5794}
5795
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005796/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005797 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005798 */
5799 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005800tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005801{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005802#ifdef FEAT_GUI_TABLINE
5803 /* When the GUI has the tabline then this always returns zero. */
5804 if (gui_use_tabline())
5805 return 0;
5806#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005807 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005808 {
5809 case 0: return 0;
5810 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5811 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005812 return 1;
5813}
5814
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815#endif /* FEAT_WINDOWS */
5816
5817#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5818/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005819 * Get the file name at the cursor.
5820 * If Visual mode is active, use the selected text if it's in one line.
5821 * Returns the name in allocated memory, NULL for failure.
5822 */
5823 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005824grab_file_name(count, file_lnum)
5825 long count;
5826 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005827{
5828# ifdef FEAT_VISUAL
5829 if (VIsual_active)
5830 {
5831 int len;
5832 char_u *ptr;
5833
5834 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5835 return NULL;
5836 return find_file_name_in_path(ptr, len,
5837 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5838 }
5839# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005840 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5841 file_lnum);
5842
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005843}
5844
5845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 * Return the file name under or after the cursor.
5847 *
5848 * The 'path' option is searched if the file name is not absolute.
5849 * The string returned has been alloc'ed and should be freed by the caller.
5850 * NULL is returned if the file name or file is not found.
5851 *
5852 * options:
5853 * FNAME_MESS give error messages
5854 * FNAME_EXP expand to path
5855 * FNAME_HYP check for hypertext link
5856 * FNAME_INCL apply "includeexpr"
5857 */
5858 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005859file_name_at_cursor(options, count, file_lnum)
5860 int options;
5861 long count;
5862 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863{
5864 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005865 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5866 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867}
5868
5869/*
5870 * Return the name of the file under or after ptr[col].
5871 * Otherwise like file_name_at_cursor().
5872 */
5873 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005874file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 char_u *line;
5876 int col;
5877 int options;
5878 long count;
5879 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005880 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881{
5882 char_u *ptr;
5883 int len;
5884
5885 /*
5886 * search forward for what could be the start of a file name
5887 */
5888 ptr = line + col;
5889 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005890 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 if (*ptr == NUL) /* nothing found */
5892 {
5893 if (options & FNAME_MESS)
5894 EMSG(_("E446: No file name under cursor"));
5895 return NULL;
5896 }
5897
5898 /*
5899 * Search backward for first char of the file name.
5900 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5901 */
5902 while (ptr > line)
5903 {
5904#ifdef FEAT_MBYTE
5905 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5906 ptr -= len + 1;
5907 else
5908#endif
5909 if (vim_isfilec(ptr[-1])
5910 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5911 --ptr;
5912 else
5913 break;
5914 }
5915
5916 /*
5917 * Search forward for the last char of the file name.
5918 * Also allow "://" when ':' is not in 'isfname'.
5919 */
5920 len = 0;
5921 while (vim_isfilec(ptr[len])
5922 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5923#ifdef FEAT_MBYTE
5924 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005925 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 else
5927#endif
5928 ++len;
5929
5930 /*
5931 * If there is trailing punctuation, remove it.
5932 * But don't remove "..", could be a directory name.
5933 */
5934 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5935 && ptr[len - 2] != '.')
5936 --len;
5937
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005938 if (file_lnum != NULL)
5939 {
5940 char_u *p;
5941
5942 /* Get the number after the file name and a separator character */
5943 p = ptr + len;
5944 p = skipwhite(p);
5945 if (*p != NUL)
5946 {
5947 if (!isdigit(*p))
5948 ++p; /* skip the separator */
5949 p = skipwhite(p);
5950 if (isdigit(*p))
5951 *file_lnum = (int)getdigits(&p);
5952 }
5953 }
5954
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5956}
5957
5958# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5959static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5960
5961 static char_u *
5962eval_includeexpr(ptr, len)
5963 char_u *ptr;
5964 int len;
5965{
5966 char_u *res;
5967
5968 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005969 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005970 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 set_vim_var_string(VV_FNAME, NULL, 0);
5972 return res;
5973}
5974#endif
5975
5976/*
5977 * Return the name of the file ptr[len] in 'path'.
5978 * Otherwise like file_name_at_cursor().
5979 */
5980 char_u *
5981find_file_name_in_path(ptr, len, options, count, rel_fname)
5982 char_u *ptr;
5983 int len;
5984 int options;
5985 long count;
5986 char_u *rel_fname; /* file we are searching relative to */
5987{
5988 char_u *file_name;
5989 int c;
5990# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5991 char_u *tofree = NULL;
5992
5993 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5994 {
5995 tofree = eval_includeexpr(ptr, len);
5996 if (tofree != NULL)
5997 {
5998 ptr = tofree;
5999 len = (int)STRLEN(ptr);
6000 }
6001 }
6002# endif
6003
6004 if (options & FNAME_EXP)
6005 {
6006 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6007 TRUE, rel_fname);
6008
6009# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6010 /*
6011 * If the file could not be found in a normal way, try applying
6012 * 'includeexpr' (unless done already).
6013 */
6014 if (file_name == NULL
6015 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6016 {
6017 tofree = eval_includeexpr(ptr, len);
6018 if (tofree != NULL)
6019 {
6020 ptr = tofree;
6021 len = (int)STRLEN(ptr);
6022 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6023 TRUE, rel_fname);
6024 }
6025 }
6026# endif
6027 if (file_name == NULL && (options & FNAME_MESS))
6028 {
6029 c = ptr[len];
6030 ptr[len] = NUL;
6031 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6032 ptr[len] = c;
6033 }
6034
6035 /* Repeat finding the file "count" times. This matters when it
6036 * appears several times in the path. */
6037 while (file_name != NULL && --count > 0)
6038 {
6039 vim_free(file_name);
6040 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6041 }
6042 }
6043 else
6044 file_name = vim_strnsave(ptr, len);
6045
6046# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6047 vim_free(tofree);
6048# endif
6049
6050 return file_name;
6051}
6052#endif /* FEAT_SEARCHPATH */
6053
6054/*
6055 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6056 * Also check for ":\\", which MS Internet Explorer accepts, return
6057 * URL_BACKSLASH.
6058 */
6059 static int
6060path_is_url(p)
6061 char_u *p;
6062{
6063 if (STRNCMP(p, "://", (size_t)3) == 0)
6064 return URL_SLASH;
6065 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6066 return URL_BACKSLASH;
6067 return 0;
6068}
6069
6070/*
6071 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6072 * Return URL_BACKSLASH for "name:\\".
6073 * Return zero otherwise.
6074 */
6075 int
6076path_with_url(fname)
6077 char_u *fname;
6078{
6079 char_u *p;
6080
6081 for (p = fname; isalpha(*p); ++p)
6082 ;
6083 return path_is_url(p);
6084}
6085
6086/*
6087 * Return TRUE if "name" is a full (absolute) path name or URL.
6088 */
6089 int
6090vim_isAbsName(name)
6091 char_u *name;
6092{
6093 return (path_with_url(name) != 0 || mch_isFullName(name));
6094}
6095
6096/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006097 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 *
6099 * return FAIL for failure, OK otherwise
6100 */
6101 int
6102vim_FullName(fname, buf, len, force)
6103 char_u *fname, *buf;
6104 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006105 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106{
6107 int retval = OK;
6108 int url;
6109
6110 *buf = NUL;
6111 if (fname == NULL)
6112 return FAIL;
6113
6114 url = path_with_url(fname);
6115 if (!url)
6116 retval = mch_FullName(fname, buf, len, force);
6117 if (url || retval == FAIL)
6118 {
6119 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006120 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 }
6122#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6123 slash_adjust(buf);
6124#endif
6125 return retval;
6126}
6127
6128/*
6129 * Return the minimal number of rows that is needed on the screen to display
6130 * the current number of windows.
6131 */
6132 int
6133min_rows()
6134{
6135 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006136#ifdef FEAT_WINDOWS
6137 tabpage_T *tp;
6138 int n;
6139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140
6141 if (firstwin == NULL) /* not initialized yet */
6142 return MIN_LINES;
6143
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006145 total = 0;
6146 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6147 {
6148 n = frame_minheight(tp->tp_topframe, NULL);
6149 if (total < n)
6150 total = n;
6151 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006152 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006154 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006156 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 return total;
6158}
6159
6160/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006161 * Return TRUE if there is only one window (in the current tab page), not
6162 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006163 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 */
6165 int
6166only_one_window()
6167{
6168#ifdef FEAT_WINDOWS
6169 int count = 0;
6170 win_T *wp;
6171
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006172 /* If there is another tab page there always is another window. */
6173 if (first_tabpage->tp_next != NULL)
6174 return FALSE;
6175
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006177 if ((!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178# ifdef FEAT_QUICKFIX
6179 || wp->w_p_pvw
6180# endif
6181 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006182# ifdef FEAT_AUTOCMD
6183 && wp != aucmd_win
6184# endif
6185 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 ++count;
6187 return (count <= 1);
6188#else
6189 return TRUE;
6190#endif
6191}
6192
6193#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6194/*
6195 * Correct the cursor line number in other windows. Used after changing the
6196 * current buffer, and before applying autocommands.
6197 * When "do_curwin" is TRUE, also check current window.
6198 */
6199 void
6200check_lnums(do_curwin)
6201 int do_curwin;
6202{
6203 win_T *wp;
6204
6205#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006206 tabpage_T *tp;
6207
6208 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6210#else
6211 wp = curwin;
6212 if (do_curwin)
6213#endif
6214 {
6215 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6216 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6217 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6218 wp->w_topline = curbuf->b_ml.ml_line_count;
6219 }
6220}
6221#endif
6222
6223#if defined(FEAT_WINDOWS) || defined(PROTO)
6224
6225/*
6226 * A snapshot of the window sizes, to restore them after closing the help
6227 * window.
6228 * Only these fields are used:
6229 * fr_layout
6230 * fr_width
6231 * fr_height
6232 * fr_next
6233 * fr_child
6234 * fr_win (only valid for the old curwin, NULL otherwise)
6235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236
6237/*
6238 * Create a snapshot of the current frame sizes.
6239 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006240 void
6241make_snapshot(idx)
6242 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006244 clear_snapshot(curtab, idx);
6245 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246}
6247
6248 static void
6249make_snapshot_rec(fr, frp)
6250 frame_T *fr;
6251 frame_T **frp;
6252{
6253 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6254 if (*frp == NULL)
6255 return;
6256 (*frp)->fr_layout = fr->fr_layout;
6257# ifdef FEAT_VERTSPLIT
6258 (*frp)->fr_width = fr->fr_width;
6259# endif
6260 (*frp)->fr_height = fr->fr_height;
6261 if (fr->fr_next != NULL)
6262 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6263 if (fr->fr_child != NULL)
6264 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6265 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6266 (*frp)->fr_win = curwin;
6267}
6268
6269/*
6270 * Remove any existing snapshot.
6271 */
6272 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006273clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006274 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006275 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006277 clear_snapshot_rec(tp->tp_snapshot[idx]);
6278 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279}
6280
6281 static void
6282clear_snapshot_rec(fr)
6283 frame_T *fr;
6284{
6285 if (fr != NULL)
6286 {
6287 clear_snapshot_rec(fr->fr_next);
6288 clear_snapshot_rec(fr->fr_child);
6289 vim_free(fr);
6290 }
6291}
6292
6293/*
6294 * Restore a previously created snapshot, if there is any.
6295 * This is only done if the screen size didn't change and the window layout is
6296 * still the same.
6297 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006298 void
6299restore_snapshot(idx, close_curwin)
6300 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 int close_curwin; /* closing current window */
6302{
6303 win_T *wp;
6304
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006305 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006307 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006309 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6310 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006312 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 win_comp_pos();
6314 if (wp != NULL && close_curwin)
6315 win_goto(wp);
6316 redraw_all_later(CLEAR);
6317 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006318 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319}
6320
6321/*
6322 * Check if frames "sn" and "fr" have the same layout, same following frames
6323 * and same children.
6324 */
6325 static int
6326check_snapshot_rec(sn, fr)
6327 frame_T *sn;
6328 frame_T *fr;
6329{
6330 if (sn->fr_layout != fr->fr_layout
6331 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6332 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6333 || (sn->fr_next != NULL
6334 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6335 || (sn->fr_child != NULL
6336 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6337 return FAIL;
6338 return OK;
6339}
6340
6341/*
6342 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6343 * following frames and children.
6344 * Returns a pointer to the old current window, or NULL.
6345 */
6346 static win_T *
6347restore_snapshot_rec(sn, fr)
6348 frame_T *sn;
6349 frame_T *fr;
6350{
6351 win_T *wp = NULL;
6352 win_T *wp2;
6353
6354 fr->fr_height = sn->fr_height;
6355# ifdef FEAT_VERTSPLIT
6356 fr->fr_width = sn->fr_width;
6357# endif
6358 if (fr->fr_layout == FR_LEAF)
6359 {
6360 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6361# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006362 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363# endif
6364 wp = sn->fr_win;
6365 }
6366 if (sn->fr_next != NULL)
6367 {
6368 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6369 if (wp2 != NULL)
6370 wp = wp2;
6371 }
6372 if (sn->fr_child != NULL)
6373 {
6374 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6375 if (wp2 != NULL)
6376 wp = wp2;
6377 }
6378 return wp;
6379}
6380
6381#endif
6382
6383#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6384/*
6385 * Return TRUE if there is any vertically split window.
6386 */
6387 int
6388win_hasvertsplit()
6389{
6390 frame_T *fr;
6391
6392 if (topframe->fr_layout == FR_ROW)
6393 return TRUE;
6394
6395 if (topframe->fr_layout == FR_COL)
6396 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6397 if (fr->fr_layout == FR_ROW)
6398 return TRUE;
6399
6400 return FALSE;
6401}
6402#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006403
6404#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6405/*
6406 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006407 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006408 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6409 * If no particular ID is desired, -1 must be specified for 'id'.
6410 * Return ID of added match, -1 on failure.
6411 */
6412 int
6413match_add(wp, grp, pat, prio, id)
6414 win_T *wp;
6415 char_u *grp;
6416 char_u *pat;
6417 int prio;
6418 int id;
6419{
6420 matchitem_T *cur;
6421 matchitem_T *prev;
6422 matchitem_T *m;
6423 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006424 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006425
6426 if (*grp == NUL || *pat == NUL)
6427 return -1;
6428 if (id < -1 || id == 0)
6429 {
6430 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6431 return -1;
6432 }
6433 if (id != -1)
6434 {
6435 cur = wp->w_match_head;
6436 while (cur != NULL)
6437 {
6438 if (cur->id == id)
6439 {
6440 EMSGN("E801: ID already taken: %ld", id);
6441 return -1;
6442 }
6443 cur = cur->next;
6444 }
6445 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006446 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006447 {
6448 EMSG2(_(e_nogroup), grp);
6449 return -1;
6450 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006451 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006452 {
6453 EMSG2(_(e_invarg2), pat);
6454 return -1;
6455 }
6456
6457 /* Find available match ID. */
6458 while (id == -1)
6459 {
6460 cur = wp->w_match_head;
6461 while (cur != NULL && cur->id != wp->w_next_match_id)
6462 cur = cur->next;
6463 if (cur == NULL)
6464 id = wp->w_next_match_id;
6465 wp->w_next_match_id++;
6466 }
6467
6468 /* Build new match. */
6469 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6470 m->id = id;
6471 m->priority = prio;
6472 m->pattern = vim_strsave(pat);
6473 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006474 m->match.regprog = regprog;
6475 m->match.rmm_ic = FALSE;
6476 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006477
6478 /* Insert new match. The match list is in ascending order with regard to
6479 * the match priorities. */
6480 cur = wp->w_match_head;
6481 prev = cur;
6482 while (cur != NULL && prio >= cur->priority)
6483 {
6484 prev = cur;
6485 cur = cur->next;
6486 }
6487 if (cur == prev)
6488 wp->w_match_head = m;
6489 else
6490 prev->next = m;
6491 m->next = cur;
6492
6493 redraw_later(SOME_VALID);
6494 return id;
6495}
6496
6497/*
6498 * Delete match with ID 'id' in the match list of window 'wp'.
6499 * Print error messages if 'perr' is TRUE.
6500 */
6501 int
6502match_delete(wp, id, perr)
6503 win_T *wp;
6504 int id;
6505 int perr;
6506{
6507 matchitem_T *cur = wp->w_match_head;
6508 matchitem_T *prev = cur;
6509
6510 if (id < 1)
6511 {
6512 if (perr == TRUE)
6513 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6514 id);
6515 return -1;
6516 }
6517 while (cur != NULL && cur->id != id)
6518 {
6519 prev = cur;
6520 cur = cur->next;
6521 }
6522 if (cur == NULL)
6523 {
6524 if (perr == TRUE)
6525 EMSGN("E803: ID not found: %ld", id);
6526 return -1;
6527 }
6528 if (cur == prev)
6529 wp->w_match_head = cur->next;
6530 else
6531 prev->next = cur->next;
6532 vim_free(cur->match.regprog);
6533 vim_free(cur->pattern);
6534 vim_free(cur);
6535 redraw_later(SOME_VALID);
6536 return 0;
6537}
6538
6539/*
6540 * Delete all matches in the match list of window 'wp'.
6541 */
6542 void
6543clear_matches(wp)
6544 win_T *wp;
6545{
6546 matchitem_T *m;
6547
6548 while (wp->w_match_head != NULL)
6549 {
6550 m = wp->w_match_head->next;
6551 vim_free(wp->w_match_head->match.regprog);
6552 vim_free(wp->w_match_head->pattern);
6553 vim_free(wp->w_match_head);
6554 wp->w_match_head = m;
6555 }
6556 redraw_later(SOME_VALID);
6557}
6558
6559/*
6560 * Get match from ID 'id' in window 'wp'.
6561 * Return NULL if match not found.
6562 */
6563 matchitem_T *
6564get_match(wp, id)
6565 win_T *wp;
6566 int id;
6567{
6568 matchitem_T *cur = wp->w_match_head;
6569
6570 while (cur != NULL && cur->id != id)
6571 cur = cur->next;
6572 return cur;
6573}
6574#endif