blob: 2d8f801a6fd8e1088b10c4553a1f36c4a507803d [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 Moolenaar23c347c2010-07-14 20:57:00 +02003880#ifdef FEAT_CONCEAL
3881 win_T *owp = curwin;
3882#endif
3883
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003884 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 {
3886 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003887 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 return;
3889 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003890#ifdef FEAT_AUTOCMD
3891 if (curbuf_locked())
3892 return;
3893#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003894
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895#ifdef FEAT_VISUAL
3896 if (wp->w_buffer != curbuf)
3897 reset_VIsual_and_resel();
3898 else if (VIsual_active)
3899 wp->w_cursor = curwin->w_cursor;
3900#endif
3901
3902#ifdef FEAT_GUI
3903 need_mouse_correct = TRUE;
3904#endif
3905 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02003906
3907#ifdef FEAT_CONCEAL
3908 /* Conceal cursor line in previous window, unconceal in current window. */
3909 if (win_valid(owp))
3910 update_single_line(owp, owp->w_cursor.lnum);
3911 update_single_line(curwin, curwin->w_cursor.lnum);
3912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913}
3914
3915#if defined(FEAT_PERL) || defined(PROTO)
3916/*
3917 * Find window number "winnr" (counting top to bottom).
3918 */
3919 win_T *
3920win_find_nr(winnr)
3921 int winnr;
3922{
3923 win_T *wp;
3924
3925# ifdef FEAT_WINDOWS
3926 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3927 if (--winnr == 0)
3928 break;
3929 return wp;
3930# else
3931 return curwin;
3932# endif
3933}
3934#endif
3935
3936#ifdef FEAT_VERTSPLIT
3937/*
3938 * Move to window above or below "count" times.
3939 */
3940 static void
3941win_goto_ver(up, count)
3942 int up; /* TRUE to go to win above */
3943 long count;
3944{
3945 frame_T *fr;
3946 frame_T *nfr;
3947 frame_T *foundfr;
3948
3949 foundfr = curwin->w_frame;
3950 while (count--)
3951 {
3952 /*
3953 * First go upwards in the tree of frames until we find a upwards or
3954 * downwards neighbor.
3955 */
3956 fr = foundfr;
3957 for (;;)
3958 {
3959 if (fr == topframe)
3960 goto end;
3961 if (up)
3962 nfr = fr->fr_prev;
3963 else
3964 nfr = fr->fr_next;
3965 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3966 break;
3967 fr = fr->fr_parent;
3968 }
3969
3970 /*
3971 * Now go downwards to find the bottom or top frame in it.
3972 */
3973 for (;;)
3974 {
3975 if (nfr->fr_layout == FR_LEAF)
3976 {
3977 foundfr = nfr;
3978 break;
3979 }
3980 fr = nfr->fr_child;
3981 if (nfr->fr_layout == FR_ROW)
3982 {
3983 /* Find the frame at the cursor row. */
3984 while (fr->fr_next != NULL
3985 && frame2win(fr)->w_wincol + fr->fr_width
3986 <= curwin->w_wincol + curwin->w_wcol)
3987 fr = fr->fr_next;
3988 }
3989 if (nfr->fr_layout == FR_COL && up)
3990 while (fr->fr_next != NULL)
3991 fr = fr->fr_next;
3992 nfr = fr;
3993 }
3994 }
3995end:
3996 if (foundfr != NULL)
3997 win_goto(foundfr->fr_win);
3998}
3999
4000/*
4001 * Move to left or right window.
4002 */
4003 static void
4004win_goto_hor(left, count)
4005 int left; /* TRUE to go to left win */
4006 long count;
4007{
4008 frame_T *fr;
4009 frame_T *nfr;
4010 frame_T *foundfr;
4011
4012 foundfr = curwin->w_frame;
4013 while (count--)
4014 {
4015 /*
4016 * First go upwards in the tree of frames until we find a left or
4017 * right neighbor.
4018 */
4019 fr = foundfr;
4020 for (;;)
4021 {
4022 if (fr == topframe)
4023 goto end;
4024 if (left)
4025 nfr = fr->fr_prev;
4026 else
4027 nfr = fr->fr_next;
4028 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4029 break;
4030 fr = fr->fr_parent;
4031 }
4032
4033 /*
4034 * Now go downwards to find the leftmost or rightmost frame in it.
4035 */
4036 for (;;)
4037 {
4038 if (nfr->fr_layout == FR_LEAF)
4039 {
4040 foundfr = nfr;
4041 break;
4042 }
4043 fr = nfr->fr_child;
4044 if (nfr->fr_layout == FR_COL)
4045 {
4046 /* Find the frame at the cursor row. */
4047 while (fr->fr_next != NULL
4048 && frame2win(fr)->w_winrow + fr->fr_height
4049 <= curwin->w_winrow + curwin->w_wrow)
4050 fr = fr->fr_next;
4051 }
4052 if (nfr->fr_layout == FR_ROW && left)
4053 while (fr->fr_next != NULL)
4054 fr = fr->fr_next;
4055 nfr = fr;
4056 }
4057 }
4058end:
4059 if (foundfr != NULL)
4060 win_goto(foundfr->fr_win);
4061}
4062#endif
4063
4064/*
4065 * Make window "wp" the current window.
4066 */
4067 void
4068win_enter(wp, undo_sync)
4069 win_T *wp;
4070 int undo_sync;
4071{
4072 win_enter_ext(wp, undo_sync, FALSE);
4073}
4074
4075/*
4076 * Make window wp the current window.
4077 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4078 * been closed and isn't valid.
4079 */
4080 static void
4081win_enter_ext(wp, undo_sync, curwin_invalid)
4082 win_T *wp;
4083 int undo_sync;
4084 int curwin_invalid;
4085{
4086#ifdef FEAT_AUTOCMD
4087 int other_buffer = FALSE;
4088#endif
4089
4090 if (wp == curwin && !curwin_invalid) /* nothing to do */
4091 return;
4092
4093#ifdef FEAT_AUTOCMD
4094 if (!curwin_invalid)
4095 {
4096 /*
4097 * Be careful: If autocommands delete the window, return now.
4098 */
4099 if (wp->w_buffer != curbuf)
4100 {
4101 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4102 other_buffer = TRUE;
4103 if (!win_valid(wp))
4104 return;
4105 }
4106 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4107 if (!win_valid(wp))
4108 return;
4109# ifdef FEAT_EVAL
4110 /* autocmds may abort script processing */
4111 if (aborting())
4112 return;
4113# endif
4114 }
4115#endif
4116
4117 /* sync undo before leaving the current buffer */
4118 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004119 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 /* may have to copy the buffer options when 'cpo' contains 'S' */
4121 if (wp->w_buffer != curbuf)
4122 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4123 if (!curwin_invalid)
4124 {
4125 prevwin = curwin; /* remember for CTRL-W p */
4126 curwin->w_redr_status = TRUE;
4127 }
4128 curwin = wp;
4129 curbuf = wp->w_buffer;
4130 check_cursor();
4131#ifdef FEAT_VIRTUALEDIT
4132 if (!virtual_active())
4133 curwin->w_cursor.coladd = 0;
4134#endif
4135 changed_line_abv_curs(); /* assume cursor position needs updating */
4136
4137 if (curwin->w_localdir != NULL)
4138 {
4139 /* Window has a local directory: Save current directory as global
4140 * directory (unless that was done already) and change to the local
4141 * directory. */
4142 if (globaldir == NULL)
4143 {
4144 char_u cwd[MAXPATHL];
4145
4146 if (mch_dirname(cwd, MAXPATHL) == OK)
4147 globaldir = vim_strsave(cwd);
4148 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004149 if (mch_chdir((char *)curwin->w_localdir) == 0)
4150 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152 else if (globaldir != NULL)
4153 {
4154 /* Window doesn't have a local directory and we are not in the global
4155 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004156 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 vim_free(globaldir);
4158 globaldir = NULL;
4159 shorten_fnames(TRUE);
4160 }
4161
4162#ifdef FEAT_AUTOCMD
4163 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4164 if (other_buffer)
4165 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4166#endif
4167
4168#ifdef FEAT_TITLE
4169 maketitle();
4170#endif
4171 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004172 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 if (restart_edit)
4174 redraw_later(VALID); /* causes status line redraw */
4175
4176 /* set window height to desired minimal value */
4177 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4178 win_setheight((int)p_wh);
4179 else if (curwin->w_height == 0)
4180 win_setheight(1);
4181
4182#ifdef FEAT_VERTSPLIT
4183 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004184 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 win_setwidth((int)p_wiw);
4186#endif
4187
4188#ifdef FEAT_MOUSE
4189 setmouse(); /* in case jumped to/from help buffer */
4190#endif
4191
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004192 /* Change directories when the 'acd' option is set. */
4193 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194}
4195
4196#endif /* FEAT_WINDOWS */
4197
4198#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4199/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004200 * Jump to the first open window that contains buffer "buf", if one exists.
4201 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 */
4203 win_T *
4204buf_jump_open_win(buf)
4205 buf_T *buf;
4206{
4207# ifdef FEAT_WINDOWS
4208 win_T *wp;
4209
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004210 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 if (wp->w_buffer == buf)
4212 break;
4213 if (wp != NULL)
4214 win_enter(wp, FALSE);
4215 return wp;
4216# else
4217 if (curwin->w_buffer == buf)
4218 return curwin;
4219 return NULL;
4220# endif
4221}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004222
4223/*
4224 * Jump to the first open window in any tab page that contains buffer "buf",
4225 * if one exists.
4226 * Returns a pointer to the window found, otherwise NULL.
4227 */
4228 win_T *
4229buf_jump_open_tab(buf)
4230 buf_T *buf;
4231{
4232# ifdef FEAT_WINDOWS
4233 win_T *wp;
4234 tabpage_T *tp;
4235
4236 /* First try the current tab page. */
4237 wp = buf_jump_open_win(buf);
4238 if (wp != NULL)
4239 return wp;
4240
4241 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4242 if (tp != curtab)
4243 {
4244 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4245 if (wp->w_buffer == buf)
4246 break;
4247 if (wp != NULL)
4248 {
4249 goto_tabpage_win(tp, wp);
4250 if (curwin != wp)
4251 wp = NULL; /* something went wrong */
4252 break;
4253 }
4254 }
4255
4256 return wp;
4257# else
4258 if (curwin->w_buffer == buf)
4259 return curwin;
4260 return NULL;
4261# endif
4262}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263#endif
4264
4265/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004266 * Allocate a window structure and link it in the window list when "hidden" is
4267 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004270win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004271 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004272 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273{
4274 win_T *newwin;
4275
4276 /*
4277 * allocate window structure and linesizes arrays
4278 */
4279 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4280 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4281 {
4282 vim_free(newwin);
4283 newwin = NULL;
4284 }
4285
4286 if (newwin != NULL)
4287 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004288#ifdef FEAT_AUTOCMD
4289 /* Don't execute autocommands while the window is not properly
4290 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4291 * event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004292 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 /*
4295 * link the window in the window list
4296 */
4297#ifdef FEAT_WINDOWS
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004298 if (!hidden)
4299 win_append(after, newwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300#endif
4301#ifdef FEAT_VERTSPLIT
4302 newwin->w_wincol = 0;
4303 newwin->w_width = Columns;
4304#endif
4305
4306 /* position the display and the cursor at the top of the file. */
4307 newwin->w_topline = 1;
4308#ifdef FEAT_DIFF
4309 newwin->w_topfill = 0;
4310#endif
4311 newwin->w_botline = 2;
4312 newwin->w_cursor.lnum = 1;
4313#ifdef FEAT_SCROLLBIND
4314 newwin->w_scbind_pos = 1;
4315#endif
4316
4317 /* We won't calculate w_fraction until resizing the window */
4318 newwin->w_fraction = 0;
4319 newwin->w_prev_fraction_row = -1;
4320
4321#ifdef FEAT_GUI
4322 if (gui.in_use)
4323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4325 SBAR_LEFT, newwin);
4326 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4327 SBAR_RIGHT, newwin);
4328 }
4329#endif
4330#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004331 /* init w: variables */
4332 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333#endif
4334#ifdef FEAT_FOLDING
4335 foldInitWin(newwin);
4336#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004337#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004338 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004339#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004340#ifdef FEAT_SEARCH_EXTRA
4341 newwin->w_match_head = NULL;
4342 newwin->w_next_match_id = 4;
4343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345 return newwin;
4346}
4347
4348#if defined(FEAT_WINDOWS) || defined(PROTO)
4349
4350/*
4351 * remove window 'wp' from the window list and free the structure
4352 */
4353 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004354win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004356 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357{
4358 int i;
4359
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004360#ifdef FEAT_FOLDING
4361 clearFolding(wp);
4362#endif
4363
4364 /* reduce the reference count to the argument list. */
4365 alist_unlink(wp->w_alist);
4366
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004367#ifdef FEAT_AUTOCMD
4368 /* Don't execute autocommands while the window is halfway being deleted.
4369 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004370 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004371#endif
4372
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004373#ifdef FEAT_LUA
4374 lua_window_free(wp);
4375#endif
4376
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004377#ifdef FEAT_MZSCHEME
4378 mzscheme_window_free(wp);
4379#endif
4380
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381#ifdef FEAT_PERL
4382 perl_win_free(wp);
4383#endif
4384
4385#ifdef FEAT_PYTHON
4386 python_window_free(wp);
4387#endif
4388
4389#ifdef FEAT_TCL
4390 tcl_window_free(wp);
4391#endif
4392
4393#ifdef FEAT_RUBY
4394 ruby_window_free(wp);
4395#endif
4396
4397 clear_winopt(&wp->w_onebuf_opt);
4398 clear_winopt(&wp->w_allbuf_opt);
4399
4400#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004401 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402#endif
4403
4404 if (prevwin == wp)
4405 prevwin = NULL;
4406 win_free_lsize(wp);
4407
4408 for (i = 0; i < wp->w_tagstacklen; ++i)
4409 vim_free(wp->w_tagstack[i].tagname);
4410
4411 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004412
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004414 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004416
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417#ifdef FEAT_JUMPLIST
4418 free_jumplist(wp);
4419#endif
4420
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004421#ifdef FEAT_QUICKFIX
4422 qf_free_all(wp);
4423#endif
4424
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425#ifdef FEAT_GUI
4426 if (gui.in_use)
4427 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4429 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4430 }
4431#endif /* FEAT_GUI */
4432
Bram Moolenaar860cae12010-06-05 23:22:07 +02004433#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004434 reset_synblock(wp); /* free independent synblock */
Bram Moolenaar1a384422010-07-14 19:53:30 +02004435 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004436#endif
4437
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004438#ifdef FEAT_AUTOCMD
4439 if (wp != aucmd_win)
4440#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004441 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004443
4444#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004445 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447}
4448
4449/*
4450 * Append window "wp" in the window list after window "after".
4451 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004452 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453win_append(after, wp)
4454 win_T *after, *wp;
4455{
4456 win_T *before;
4457
4458 if (after == NULL) /* after NULL is in front of the first */
4459 before = firstwin;
4460 else
4461 before = after->w_next;
4462
4463 wp->w_next = before;
4464 wp->w_prev = after;
4465 if (after == NULL)
4466 firstwin = wp;
4467 else
4468 after->w_next = wp;
4469 if (before == NULL)
4470 lastwin = wp;
4471 else
4472 before->w_prev = wp;
4473}
4474
4475/*
4476 * Remove a window from the window list.
4477 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004478 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004479win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004481 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482{
4483 if (wp->w_prev != NULL)
4484 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004485 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004487 else
4488 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 if (wp->w_next != NULL)
4490 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004491 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004493 else
4494 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495}
4496
4497/*
4498 * Append frame "frp" in a frame list after frame "after".
4499 */
4500 static void
4501frame_append(after, frp)
4502 frame_T *after, *frp;
4503{
4504 frp->fr_next = after->fr_next;
4505 after->fr_next = frp;
4506 if (frp->fr_next != NULL)
4507 frp->fr_next->fr_prev = frp;
4508 frp->fr_prev = after;
4509}
4510
4511/*
4512 * Insert frame "frp" in a frame list before frame "before".
4513 */
4514 static void
4515frame_insert(before, frp)
4516 frame_T *before, *frp;
4517{
4518 frp->fr_next = before;
4519 frp->fr_prev = before->fr_prev;
4520 before->fr_prev = frp;
4521 if (frp->fr_prev != NULL)
4522 frp->fr_prev->fr_next = frp;
4523 else
4524 frp->fr_parent->fr_child = frp;
4525}
4526
4527/*
4528 * Remove a frame from a frame list.
4529 */
4530 static void
4531frame_remove(frp)
4532 frame_T *frp;
4533{
4534 if (frp->fr_prev != NULL)
4535 frp->fr_prev->fr_next = frp->fr_next;
4536 else
4537 frp->fr_parent->fr_child = frp->fr_next;
4538 if (frp->fr_next != NULL)
4539 frp->fr_next->fr_prev = frp->fr_prev;
4540}
4541
4542#endif /* FEAT_WINDOWS */
4543
4544/*
4545 * Allocate w_lines[] for window "wp".
4546 * Return FAIL for failure, OK for success.
4547 */
4548 int
4549win_alloc_lines(wp)
4550 win_T *wp;
4551{
4552 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004553 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 if (wp->w_lines == NULL)
4555 return FAIL;
4556 return OK;
4557}
4558
4559/*
4560 * free lsize arrays for a window
4561 */
4562 void
4563win_free_lsize(wp)
4564 win_T *wp;
4565{
4566 vim_free(wp->w_lines);
4567 wp->w_lines = NULL;
4568}
4569
4570/*
4571 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004572 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 */
4574 void
4575shell_new_rows()
4576{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004577 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
4579 if (firstwin == NULL) /* not initialized yet */
4580 return;
4581#ifdef FEAT_WINDOWS
4582 if (h < frame_minheight(topframe, NULL))
4583 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004584
4585 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 * that doesn't result in the right height, forget about that option. */
4587 frame_new_height(topframe, h, FALSE, TRUE);
4588 if (topframe->fr_height != h)
4589 frame_new_height(topframe, h, FALSE, FALSE);
4590
4591 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4592#else
4593 if (h < 1)
4594 h = 1;
4595 win_new_height(firstwin, h);
4596#endif
4597 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004598#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004599 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004600#endif
4601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602#if 0
4603 /* Disabled: don't want making the screen smaller make a window larger. */
4604 if (p_ea)
4605 win_equal(curwin, FALSE, 'v');
4606#endif
4607}
4608
4609#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4610/*
4611 * Called from win_new_shellsize() after Columns changed.
4612 */
4613 void
4614shell_new_columns()
4615{
4616 if (firstwin == NULL) /* not initialized yet */
4617 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004618
4619 /* First try setting the widths of windows with 'winfixwidth'. If that
4620 * doesn't result in the right width, forget about that option. */
4621 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4622 if (topframe->fr_width != Columns)
4623 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4624
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4626#if 0
4627 /* Disabled: don't want making the screen smaller make a window larger. */
4628 if (p_ea)
4629 win_equal(curwin, FALSE, 'h');
4630#endif
4631}
4632#endif
4633
4634#if defined(FEAT_CMDWIN) || defined(PROTO)
4635/*
4636 * Save the size of all windows in "gap".
4637 */
4638 void
4639win_size_save(gap)
4640 garray_T *gap;
4641
4642{
4643 win_T *wp;
4644
4645 ga_init2(gap, (int)sizeof(int), 1);
4646 if (ga_grow(gap, win_count() * 2) == OK)
4647 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4648 {
4649 ((int *)gap->ga_data)[gap->ga_len++] =
4650 wp->w_width + wp->w_vsep_width;
4651 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4652 }
4653}
4654
4655/*
4656 * Restore window sizes, but only if the number of windows is still the same.
4657 * Does not free the growarray.
4658 */
4659 void
4660win_size_restore(gap)
4661 garray_T *gap;
4662{
4663 win_T *wp;
4664 int i;
4665
4666 if (win_count() * 2 == gap->ga_len)
4667 {
4668 i = 0;
4669 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4670 {
4671 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4672 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4673 }
4674 /* recompute the window positions */
4675 (void)win_comp_pos();
4676 }
4677}
4678#endif /* FEAT_CMDWIN */
4679
4680#if defined(FEAT_WINDOWS) || defined(PROTO)
4681/*
4682 * Update the position for all windows, using the width and height of the
4683 * frames.
4684 * Returns the row just after the last window.
4685 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004686 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687win_comp_pos()
4688{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004689 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 int col = 0;
4691
4692 frame_comp_pos(topframe, &row, &col);
4693 return row;
4694}
4695
4696/*
4697 * Update the position of the windows in frame "topfrp", using the width and
4698 * height of the frames.
4699 * "*row" and "*col" are the top-left position of the frame. They are updated
4700 * to the bottom-right position plus one.
4701 */
4702 static void
4703frame_comp_pos(topfrp, row, col)
4704 frame_T *topfrp;
4705 int *row;
4706 int *col;
4707{
4708 win_T *wp;
4709 frame_T *frp;
4710#ifdef FEAT_VERTSPLIT
4711 int startcol;
4712 int startrow;
4713#endif
4714
4715 wp = topfrp->fr_win;
4716 if (wp != NULL)
4717 {
4718 if (wp->w_winrow != *row
4719#ifdef FEAT_VERTSPLIT
4720 || wp->w_wincol != *col
4721#endif
4722 )
4723 {
4724 /* position changed, redraw */
4725 wp->w_winrow = *row;
4726#ifdef FEAT_VERTSPLIT
4727 wp->w_wincol = *col;
4728#endif
4729 redraw_win_later(wp, NOT_VALID);
4730 wp->w_redr_status = TRUE;
4731 }
4732 *row += wp->w_height + wp->w_status_height;
4733#ifdef FEAT_VERTSPLIT
4734 *col += wp->w_width + wp->w_vsep_width;
4735#endif
4736 }
4737 else
4738 {
4739#ifdef FEAT_VERTSPLIT
4740 startrow = *row;
4741 startcol = *col;
4742#endif
4743 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4744 {
4745#ifdef FEAT_VERTSPLIT
4746 if (topfrp->fr_layout == FR_ROW)
4747 *row = startrow; /* all frames are at the same row */
4748 else
4749 *col = startcol; /* all frames are at the same col */
4750#endif
4751 frame_comp_pos(frp, row, col);
4752 }
4753 }
4754}
4755
4756#endif /* FEAT_WINDOWS */
4757
4758/*
4759 * Set current window height and take care of repositioning other windows to
4760 * fit around it.
4761 */
4762 void
4763win_setheight(height)
4764 int height;
4765{
4766 win_setheight_win(height, curwin);
4767}
4768
4769/*
4770 * Set the window height of window "win" and take care of repositioning other
4771 * windows to fit around it.
4772 */
4773 void
4774win_setheight_win(height, win)
4775 int height;
4776 win_T *win;
4777{
4778 int row;
4779
4780 if (win == curwin)
4781 {
4782 /* Always keep current window at least one line high, even when
4783 * 'winminheight' is zero. */
4784#ifdef FEAT_WINDOWS
4785 if (height < p_wmh)
4786 height = p_wmh;
4787#endif
4788 if (height == 0)
4789 height = 1;
4790 }
4791
4792#ifdef FEAT_WINDOWS
4793 frame_setheight(win->w_frame, height + win->w_status_height);
4794
4795 /* recompute the window positions */
4796 row = win_comp_pos();
4797#else
4798 if (height > topframe->fr_height)
4799 height = topframe->fr_height;
4800 win->w_height = height;
4801 row = height;
4802#endif
4803
4804 /*
4805 * If there is extra space created between the last window and the command
4806 * line, clear it.
4807 */
4808 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4809 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4810 cmdline_row = row;
4811 msg_row = row;
4812 msg_col = 0;
4813
4814 redraw_all_later(NOT_VALID);
4815}
4816
4817#if defined(FEAT_WINDOWS) || defined(PROTO)
4818
4819/*
4820 * Set the height of a frame to "height" and take care that all frames and
4821 * windows inside it are resized. Also resize frames on the left and right if
4822 * the are in the same FR_ROW frame.
4823 *
4824 * Strategy:
4825 * If the frame is part of a FR_COL frame, try fitting the frame in that
4826 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4827 * go to containing frames to resize them and make room.
4828 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4829 * Check for the minimal height of the FR_ROW frame.
4830 * At the top level we can also use change the command line height.
4831 */
4832 static void
4833frame_setheight(curfrp, height)
4834 frame_T *curfrp;
4835 int height;
4836{
4837 int room; /* total number of lines available */
4838 int take; /* number of lines taken from other windows */
4839 int room_cmdline; /* lines available from cmdline */
4840 int run;
4841 frame_T *frp;
4842 int h;
4843 int room_reserved;
4844
4845 /* If the height already is the desired value, nothing to do. */
4846 if (curfrp->fr_height == height)
4847 return;
4848
4849 if (curfrp->fr_parent == NULL)
4850 {
4851 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004852 if (height > ROWS_AVAIL)
4853 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 if (height > 0)
4855 frame_new_height(curfrp, height, FALSE, FALSE);
4856 }
4857 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4858 {
4859 /* Row of frames: Also need to resize frames left and right of this
4860 * one. First check for the minimal height of these. */
4861 h = frame_minheight(curfrp->fr_parent, NULL);
4862 if (height < h)
4863 height = h;
4864 frame_setheight(curfrp->fr_parent, height);
4865 }
4866 else
4867 {
4868 /*
4869 * Column of frames: try to change only frames in this column.
4870 */
4871#ifdef FEAT_VERTSPLIT
4872 /*
4873 * Do this twice:
4874 * 1: compute room available, if it's not enough try resizing the
4875 * containing frame.
4876 * 2: compute the room available and adjust the height to it.
4877 * Try not to reduce the height of a window with 'winfixheight' set.
4878 */
4879 for (run = 1; run <= 2; ++run)
4880#else
4881 for (;;)
4882#endif
4883 {
4884 room = 0;
4885 room_reserved = 0;
4886 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4887 frp = frp->fr_next)
4888 {
4889 if (frp != curfrp
4890 && frp->fr_win != NULL
4891 && frp->fr_win->w_p_wfh)
4892 room_reserved += frp->fr_height;
4893 room += frp->fr_height;
4894 if (frp != curfrp)
4895 room -= frame_minheight(frp, NULL);
4896 }
4897#ifdef FEAT_VERTSPLIT
4898 if (curfrp->fr_width != Columns)
4899 room_cmdline = 0;
4900 else
4901#endif
4902 {
4903 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4904 + lastwin->w_height + lastwin->w_status_height);
4905 if (room_cmdline < 0)
4906 room_cmdline = 0;
4907 }
4908
4909 if (height <= room + room_cmdline)
4910 break;
4911#ifdef FEAT_VERTSPLIT
4912 if (run == 2 || curfrp->fr_width == Columns)
4913#endif
4914 {
4915 if (height > room + room_cmdline)
4916 height = room + room_cmdline;
4917 break;
4918 }
4919#ifdef FEAT_VERTSPLIT
4920 frame_setheight(curfrp->fr_parent, height
4921 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4922#endif
4923 /*NOTREACHED*/
4924 }
4925
4926 /*
4927 * Compute the number of lines we will take from others frames (can be
4928 * negative!).
4929 */
4930 take = height - curfrp->fr_height;
4931
4932 /* If there is not enough room, also reduce the height of a window
4933 * with 'winfixheight' set. */
4934 if (height > room + room_cmdline - room_reserved)
4935 room_reserved = room + room_cmdline - height;
4936 /* If there is only a 'winfixheight' window and making the
4937 * window smaller, need to make the other window taller. */
4938 if (take < 0 && room - curfrp->fr_height < room_reserved)
4939 room_reserved = 0;
4940
4941 if (take > 0 && room_cmdline > 0)
4942 {
4943 /* use lines from cmdline first */
4944 if (take < room_cmdline)
4945 room_cmdline = take;
4946 take -= room_cmdline;
4947 topframe->fr_height += room_cmdline;
4948 }
4949
4950 /*
4951 * set the current frame to the new height
4952 */
4953 frame_new_height(curfrp, height, FALSE, FALSE);
4954
4955 /*
4956 * First take lines from the frames after the current frame. If
4957 * that is not enough, takes lines from frames above the current
4958 * frame.
4959 */
4960 for (run = 0; run < 2; ++run)
4961 {
4962 if (run == 0)
4963 frp = curfrp->fr_next; /* 1st run: start with next window */
4964 else
4965 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4966 while (frp != NULL && take != 0)
4967 {
4968 h = frame_minheight(frp, NULL);
4969 if (room_reserved > 0
4970 && frp->fr_win != NULL
4971 && frp->fr_win->w_p_wfh)
4972 {
4973 if (room_reserved >= frp->fr_height)
4974 room_reserved -= frp->fr_height;
4975 else
4976 {
4977 if (frp->fr_height - room_reserved > take)
4978 room_reserved = frp->fr_height - take;
4979 take -= frp->fr_height - room_reserved;
4980 frame_new_height(frp, room_reserved, FALSE, FALSE);
4981 room_reserved = 0;
4982 }
4983 }
4984 else
4985 {
4986 if (frp->fr_height - take < h)
4987 {
4988 take -= frp->fr_height - h;
4989 frame_new_height(frp, h, FALSE, FALSE);
4990 }
4991 else
4992 {
4993 frame_new_height(frp, frp->fr_height - take,
4994 FALSE, FALSE);
4995 take = 0;
4996 }
4997 }
4998 if (run == 0)
4999 frp = frp->fr_next;
5000 else
5001 frp = frp->fr_prev;
5002 }
5003 }
5004 }
5005}
5006
5007#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5008/*
5009 * Set current window width and take care of repositioning other windows to
5010 * fit around it.
5011 */
5012 void
5013win_setwidth(width)
5014 int width;
5015{
5016 win_setwidth_win(width, curwin);
5017}
5018
5019 void
5020win_setwidth_win(width, wp)
5021 int width;
5022 win_T *wp;
5023{
5024 /* Always keep current window at least one column wide, even when
5025 * 'winminwidth' is zero. */
5026 if (wp == curwin)
5027 {
5028 if (width < p_wmw)
5029 width = p_wmw;
5030 if (width == 0)
5031 width = 1;
5032 }
5033
5034 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5035
5036 /* recompute the window positions */
5037 (void)win_comp_pos();
5038
5039 redraw_all_later(NOT_VALID);
5040}
5041
5042/*
5043 * Set the width of a frame to "width" and take care that all frames and
5044 * windows inside it are resized. Also resize frames above and below if the
5045 * are in the same FR_ROW frame.
5046 *
5047 * Strategy is similar to frame_setheight().
5048 */
5049 static void
5050frame_setwidth(curfrp, width)
5051 frame_T *curfrp;
5052 int width;
5053{
5054 int room; /* total number of lines available */
5055 int take; /* number of lines taken from other windows */
5056 int run;
5057 frame_T *frp;
5058 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005059 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060
5061 /* If the width already is the desired value, nothing to do. */
5062 if (curfrp->fr_width == width)
5063 return;
5064
5065 if (curfrp->fr_parent == NULL)
5066 /* topframe: can't change width */
5067 return;
5068
5069 if (curfrp->fr_parent->fr_layout == FR_COL)
5070 {
5071 /* Column of frames: Also need to resize frames above and below of
5072 * this one. First check for the minimal width of these. */
5073 w = frame_minwidth(curfrp->fr_parent, NULL);
5074 if (width < w)
5075 width = w;
5076 frame_setwidth(curfrp->fr_parent, width);
5077 }
5078 else
5079 {
5080 /*
5081 * Row of frames: try to change only frames in this row.
5082 *
5083 * Do this twice:
5084 * 1: compute room available, if it's not enough try resizing the
5085 * containing frame.
5086 * 2: compute the room available and adjust the width to it.
5087 */
5088 for (run = 1; run <= 2; ++run)
5089 {
5090 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005091 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5093 frp = frp->fr_next)
5094 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005095 if (frp != curfrp
5096 && frp->fr_win != NULL
5097 && frp->fr_win->w_p_wfw)
5098 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 room += frp->fr_width;
5100 if (frp != curfrp)
5101 room -= frame_minwidth(frp, NULL);
5102 }
5103
5104 if (width <= room)
5105 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005106 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
5108 if (width > room)
5109 width = room;
5110 break;
5111 }
5112 frame_setwidth(curfrp->fr_parent, width
5113 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5114 }
5115
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 /*
5117 * Compute the number of lines we will take from others frames (can be
5118 * negative!).
5119 */
5120 take = width - curfrp->fr_width;
5121
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005122 /* If there is not enough room, also reduce the width of a window
5123 * with 'winfixwidth' set. */
5124 if (width > room - room_reserved)
5125 room_reserved = room - width;
5126 /* If there is only a 'winfixwidth' window and making the
5127 * window smaller, need to make the other window narrower. */
5128 if (take < 0 && room - curfrp->fr_width < room_reserved)
5129 room_reserved = 0;
5130
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 /*
5132 * set the current frame to the new width
5133 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005134 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135
5136 /*
5137 * First take lines from the frames right of the current frame. If
5138 * that is not enough, takes lines from frames left of the current
5139 * frame.
5140 */
5141 for (run = 0; run < 2; ++run)
5142 {
5143 if (run == 0)
5144 frp = curfrp->fr_next; /* 1st run: start with next window */
5145 else
5146 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5147 while (frp != NULL && take != 0)
5148 {
5149 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005150 if (room_reserved > 0
5151 && frp->fr_win != NULL
5152 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005154 if (room_reserved >= frp->fr_width)
5155 room_reserved -= frp->fr_width;
5156 else
5157 {
5158 if (frp->fr_width - room_reserved > take)
5159 room_reserved = frp->fr_width - take;
5160 take -= frp->fr_width - room_reserved;
5161 frame_new_width(frp, room_reserved, FALSE, FALSE);
5162 room_reserved = 0;
5163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165 else
5166 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005167 if (frp->fr_width - take < w)
5168 {
5169 take -= frp->fr_width - w;
5170 frame_new_width(frp, w, FALSE, FALSE);
5171 }
5172 else
5173 {
5174 frame_new_width(frp, frp->fr_width - take,
5175 FALSE, FALSE);
5176 take = 0;
5177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
5179 if (run == 0)
5180 frp = frp->fr_next;
5181 else
5182 frp = frp->fr_prev;
5183 }
5184 }
5185 }
5186}
5187#endif /* FEAT_VERTSPLIT */
5188
5189/*
5190 * Check 'winminheight' for a valid value.
5191 */
5192 void
5193win_setminheight()
5194{
5195 int room;
5196 int first = TRUE;
5197 win_T *wp;
5198
5199 /* loop until there is a 'winminheight' that is possible */
5200 while (p_wmh > 0)
5201 {
5202 /* TODO: handle vertical splits */
5203 room = -p_wh;
5204 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5205 room += wp->w_height - p_wmh;
5206 if (room >= 0)
5207 break;
5208 --p_wmh;
5209 if (first)
5210 {
5211 EMSG(_(e_noroom));
5212 first = FALSE;
5213 }
5214 }
5215}
5216
5217#ifdef FEAT_MOUSE
5218
5219/*
5220 * Status line of dragwin is dragged "offset" lines down (negative is up).
5221 */
5222 void
5223win_drag_status_line(dragwin, offset)
5224 win_T *dragwin;
5225 int offset;
5226{
5227 frame_T *curfr;
5228 frame_T *fr;
5229 int room;
5230 int row;
5231 int up; /* if TRUE, drag status line up, otherwise down */
5232 int n;
5233
5234 fr = dragwin->w_frame;
5235 curfr = fr;
5236 if (fr != topframe) /* more than one window */
5237 {
5238 fr = fr->fr_parent;
5239 /* When the parent frame is not a column of frames, its parent should
5240 * be. */
5241 if (fr->fr_layout != FR_COL)
5242 {
5243 curfr = fr;
5244 if (fr != topframe) /* only a row of windows, may drag statusline */
5245 fr = fr->fr_parent;
5246 }
5247 }
5248
5249 /* If this is the last frame in a column, may want to resize the parent
5250 * frame instead (go two up to skip a row of frames). */
5251 while (curfr != topframe && curfr->fr_next == NULL)
5252 {
5253 if (fr != topframe)
5254 fr = fr->fr_parent;
5255 curfr = fr;
5256 if (fr != topframe)
5257 fr = fr->fr_parent;
5258 }
5259
5260 if (offset < 0) /* drag up */
5261 {
5262 up = TRUE;
5263 offset = -offset;
5264 /* sum up the room of the current frame and above it */
5265 if (fr == curfr)
5266 {
5267 /* only one window */
5268 room = fr->fr_height - frame_minheight(fr, NULL);
5269 }
5270 else
5271 {
5272 room = 0;
5273 for (fr = fr->fr_child; ; fr = fr->fr_next)
5274 {
5275 room += fr->fr_height - frame_minheight(fr, NULL);
5276 if (fr == curfr)
5277 break;
5278 }
5279 }
5280 fr = curfr->fr_next; /* put fr at frame that grows */
5281 }
5282 else /* drag down */
5283 {
5284 up = FALSE;
5285 /*
5286 * Only dragging the last status line can reduce p_ch.
5287 */
5288 room = Rows - cmdline_row;
5289 if (curfr->fr_next == NULL)
5290 room -= 1;
5291 else
5292 room -= p_ch;
5293 if (room < 0)
5294 room = 0;
5295 /* sum up the room of frames below of the current one */
5296 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5297 room += fr->fr_height - frame_minheight(fr, NULL);
5298 fr = curfr; /* put fr at window that grows */
5299 }
5300
5301 if (room < offset) /* Not enough room */
5302 offset = room; /* Move as far as we can */
5303 if (offset <= 0)
5304 return;
5305
5306 /*
5307 * Grow frame fr by "offset" lines.
5308 * Doesn't happen when dragging the last status line up.
5309 */
5310 if (fr != NULL)
5311 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5312
5313 if (up)
5314 fr = curfr; /* current frame gets smaller */
5315 else
5316 fr = curfr->fr_next; /* next frame gets smaller */
5317
5318 /*
5319 * Now make the other frames smaller.
5320 */
5321 while (fr != NULL && offset > 0)
5322 {
5323 n = frame_minheight(fr, NULL);
5324 if (fr->fr_height - offset <= n)
5325 {
5326 offset -= fr->fr_height - n;
5327 frame_new_height(fr, n, !up, FALSE);
5328 }
5329 else
5330 {
5331 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5332 break;
5333 }
5334 if (up)
5335 fr = fr->fr_prev;
5336 else
5337 fr = fr->fr_next;
5338 }
5339 row = win_comp_pos();
5340 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5341 cmdline_row = row;
5342 p_ch = Rows - cmdline_row;
5343 if (p_ch < 1)
5344 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005345 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005346 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 showmode();
5348}
5349
5350#ifdef FEAT_VERTSPLIT
5351/*
5352 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5353 */
5354 void
5355win_drag_vsep_line(dragwin, offset)
5356 win_T *dragwin;
5357 int offset;
5358{
5359 frame_T *curfr;
5360 frame_T *fr;
5361 int room;
5362 int left; /* if TRUE, drag separator line left, otherwise right */
5363 int n;
5364
5365 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005366 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 return;
5368 curfr = fr;
5369 fr = fr->fr_parent;
5370 /* When the parent frame is not a row of frames, its parent should be. */
5371 if (fr->fr_layout != FR_ROW)
5372 {
5373 if (fr == topframe) /* only a column of windows (cannot happen?) */
5374 return;
5375 curfr = fr;
5376 fr = fr->fr_parent;
5377 }
5378
5379 /* If this is the last frame in a row, may want to resize a parent
5380 * frame instead. */
5381 while (curfr->fr_next == NULL)
5382 {
5383 if (fr == topframe)
5384 break;
5385 curfr = fr;
5386 fr = fr->fr_parent;
5387 if (fr != topframe)
5388 {
5389 curfr = fr;
5390 fr = fr->fr_parent;
5391 }
5392 }
5393
5394 if (offset < 0) /* drag left */
5395 {
5396 left = TRUE;
5397 offset = -offset;
5398 /* sum up the room of the current frame and left of it */
5399 room = 0;
5400 for (fr = fr->fr_child; ; fr = fr->fr_next)
5401 {
5402 room += fr->fr_width - frame_minwidth(fr, NULL);
5403 if (fr == curfr)
5404 break;
5405 }
5406 fr = curfr->fr_next; /* put fr at frame that grows */
5407 }
5408 else /* drag right */
5409 {
5410 left = FALSE;
5411 /* sum up the room of frames right of the current one */
5412 room = 0;
5413 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5414 room += fr->fr_width - frame_minwidth(fr, NULL);
5415 fr = curfr; /* put fr at window that grows */
5416 }
5417
5418 if (room < offset) /* Not enough room */
5419 offset = room; /* Move as far as we can */
5420 if (offset <= 0) /* No room at all, quit. */
5421 return;
5422
5423 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005424 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425
5426 /* shrink other frames: current and at the left or at the right */
5427 if (left)
5428 fr = curfr; /* current frame gets smaller */
5429 else
5430 fr = curfr->fr_next; /* next frame gets smaller */
5431
5432 while (fr != NULL && offset > 0)
5433 {
5434 n = frame_minwidth(fr, NULL);
5435 if (fr->fr_width - offset <= n)
5436 {
5437 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005438 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 }
5440 else
5441 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005442 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 break;
5444 }
5445 if (left)
5446 fr = fr->fr_prev;
5447 else
5448 fr = fr->fr_next;
5449 }
5450 (void)win_comp_pos();
5451 redraw_all_later(NOT_VALID);
5452}
5453#endif /* FEAT_VERTSPLIT */
5454#endif /* FEAT_MOUSE */
5455
5456#endif /* FEAT_WINDOWS */
5457
5458/*
5459 * Set the height of a window.
5460 * This takes care of the things inside the window, not what happens to the
5461 * window position, the frame or to other windows.
5462 */
5463 static void
5464win_new_height(wp, height)
5465 win_T *wp;
5466 int height;
5467{
5468 linenr_T lnum;
5469 int sline, line_size;
5470#define FRACTION_MULT 16384L
5471
5472 /* Don't want a negative height. Happens when splitting a tiny window.
5473 * Will equalize heights soon to fix it. */
5474 if (height < 0)
5475 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005476 if (wp->w_height == height)
5477 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478
5479 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5480 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5481 + FRACTION_MULT / 2) / (long)wp->w_height;
5482
5483 wp->w_height = height;
5484 wp->w_skipcol = 0;
5485
5486 /* Don't change w_topline when height is zero. Don't set w_topline when
5487 * 'scrollbind' is set and this isn't the current window. */
5488 if (height > 0
5489#ifdef FEAT_SCROLLBIND
5490 && (!wp->w_p_scb || wp == curwin)
5491#endif
5492 )
5493 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005494 /*
5495 * Find a value for w_topline that shows the cursor at the same
5496 * relative position in the window as before (more or less).
5497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 lnum = wp->w_cursor.lnum;
5499 if (lnum < 1) /* can happen when starting up */
5500 lnum = 1;
5501 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5502 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5503 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005504
5505 if (sline >= 0)
5506 {
5507 /* Make sure the whole cursor line is visible, if possible. */
5508 int rows = plines_win(wp, lnum, FALSE);
5509
5510 if (sline > wp->w_height - rows)
5511 {
5512 sline = wp->w_height - rows;
5513 wp->w_wrow -= rows - line_size;
5514 }
5515 }
5516
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 if (sline < 0)
5518 {
5519 /*
5520 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005521 * Make cursor line the first line in the window. If not enough
5522 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 */
5524 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005525 if (wp->w_wrow >= wp->w_height
5526 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5527 {
5528 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5529 --wp->w_wrow;
5530 while (wp->w_wrow >= wp->w_height)
5531 {
5532 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5533 + win_col_off2(wp);
5534 --wp->w_wrow;
5535 }
5536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 }
5538 else
5539 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005540 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 {
5542#ifdef FEAT_FOLDING
5543 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5544 if (lnum == 1)
5545 {
5546 /* first line in buffer is folded */
5547 line_size = 1;
5548 --sline;
5549 break;
5550 }
5551#endif
5552 --lnum;
5553#ifdef FEAT_DIFF
5554 if (lnum == wp->w_topline)
5555 line_size = plines_win_nofill(wp, lnum, TRUE)
5556 + wp->w_topfill;
5557 else
5558#endif
5559 line_size = plines_win(wp, lnum, TRUE);
5560 sline -= line_size;
5561 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005562
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 if (sline < 0)
5564 {
5565 /*
5566 * Line we want at top would go off top of screen. Use next
5567 * line instead.
5568 */
5569#ifdef FEAT_FOLDING
5570 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5571#endif
5572 lnum++;
5573 wp->w_wrow -= line_size + sline;
5574 }
5575 else if (sline > 0)
5576 {
5577 /* First line of file reached, use that as topline. */
5578 lnum = 1;
5579 wp->w_wrow -= sline;
5580 }
5581 }
5582 set_topline(wp, lnum);
5583 }
5584
5585 if (wp == curwin)
5586 {
5587 if (p_so)
5588 update_topline();
5589 curs_columns(FALSE); /* validate w_wrow */
5590 }
5591 wp->w_prev_fraction_row = wp->w_wrow;
5592
5593 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005594 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595#ifdef FEAT_WINDOWS
5596 wp->w_redr_status = TRUE;
5597#endif
5598 invalidate_botline_win(wp);
5599}
5600
5601#ifdef FEAT_VERTSPLIT
5602/*
5603 * Set the width of a window.
5604 */
5605 static void
5606win_new_width(wp, width)
5607 win_T *wp;
5608 int width;
5609{
5610 wp->w_width = width;
5611 wp->w_lines_valid = 0;
5612 changed_line_abv_curs_win(wp);
5613 invalidate_botline_win(wp);
5614 if (wp == curwin)
5615 {
5616 update_topline();
5617 curs_columns(TRUE); /* validate w_wrow */
5618 }
5619 redraw_win_later(wp, NOT_VALID);
5620 wp->w_redr_status = TRUE;
5621}
5622#endif
5623
5624 void
5625win_comp_scroll(wp)
5626 win_T *wp;
5627{
5628 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5629 if (wp->w_p_scr == 0)
5630 wp->w_p_scr = 1;
5631}
5632
5633/*
5634 * command_height: called whenever p_ch has been changed
5635 */
5636 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005637command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638{
5639#ifdef FEAT_WINDOWS
5640 int h;
5641 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005642 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005644 /* Use the value of p_ch that we remembered. This is needed for when the
5645 * GUI starts up, we can't be sure in what order things happen. And when
5646 * p_ch was changed in another tab page. */
5647 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005648
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 /* Find bottom frame with width of screen. */
5650 frp = lastwin->w_frame;
5651# ifdef FEAT_VERTSPLIT
5652 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5653 frp = frp->fr_parent;
5654# endif
5655
5656 /* Avoid changing the height of a window with 'winfixheight' set. */
5657 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5658 && frp->fr_win->w_p_wfh)
5659 frp = frp->fr_prev;
5660
5661 if (starting != NO_SCREEN)
5662 {
5663 cmdline_row = Rows - p_ch;
5664
5665 if (p_ch > old_p_ch) /* p_ch got bigger */
5666 {
5667 while (p_ch > old_p_ch)
5668 {
5669 if (frp == NULL)
5670 {
5671 EMSG(_(e_noroom));
5672 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005673 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 cmdline_row = Rows - p_ch;
5675 break;
5676 }
5677 h = frp->fr_height - frame_minheight(frp, NULL);
5678 if (h > p_ch - old_p_ch)
5679 h = p_ch - old_p_ch;
5680 old_p_ch += h;
5681 frame_add_height(frp, -h);
5682 frp = frp->fr_prev;
5683 }
5684
5685 /* Recompute window positions. */
5686 (void)win_comp_pos();
5687
5688 /* clear the lines added to cmdline */
5689 if (full_screen)
5690 screen_fill((int)(cmdline_row), (int)Rows, 0,
5691 (int)Columns, ' ', ' ', 0);
5692 msg_row = cmdline_row;
5693 redraw_cmdline = TRUE;
5694 return;
5695 }
5696
5697 if (msg_row < cmdline_row)
5698 msg_row = cmdline_row;
5699 redraw_cmdline = TRUE;
5700 }
5701 frame_add_height(frp, (int)(old_p_ch - p_ch));
5702
5703 /* Recompute window positions. */
5704 if (frp != lastwin->w_frame)
5705 (void)win_comp_pos();
5706#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005708 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709#endif
5710}
5711
5712#if defined(FEAT_WINDOWS) || defined(PROTO)
5713/*
5714 * Resize frame "frp" to be "n" lines higher (negative for less high).
5715 * Also resize the frames it is contained in.
5716 */
5717 static void
5718frame_add_height(frp, n)
5719 frame_T *frp;
5720 int n;
5721{
5722 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5723 for (;;)
5724 {
5725 frp = frp->fr_parent;
5726 if (frp == NULL)
5727 break;
5728 frp->fr_height += n;
5729 }
5730}
5731
5732/*
5733 * Add or remove a status line for the bottom window(s), according to the
5734 * value of 'laststatus'.
5735 */
5736 void
5737last_status(morewin)
5738 int morewin; /* pretend there are two or more windows */
5739{
5740 /* Don't make a difference between horizontal or vertical split. */
5741 last_status_rec(topframe, (p_ls == 2
5742 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5743}
5744
5745 static void
5746last_status_rec(fr, statusline)
5747 frame_T *fr;
5748 int statusline;
5749{
5750 frame_T *fp;
5751 win_T *wp;
5752
5753 if (fr->fr_layout == FR_LEAF)
5754 {
5755 wp = fr->fr_win;
5756 if (wp->w_status_height != 0 && !statusline)
5757 {
5758 /* remove status line */
5759 win_new_height(wp, wp->w_height + 1);
5760 wp->w_status_height = 0;
5761 comp_col();
5762 }
5763 else if (wp->w_status_height == 0 && statusline)
5764 {
5765 /* Find a frame to take a line from. */
5766 fp = fr;
5767 while (fp->fr_height <= frame_minheight(fp, NULL))
5768 {
5769 if (fp == topframe)
5770 {
5771 EMSG(_(e_noroom));
5772 return;
5773 }
5774 /* In a column of frames: go to frame above. If already at
5775 * the top or in a row of frames: go to parent. */
5776 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5777 fp = fp->fr_prev;
5778 else
5779 fp = fp->fr_parent;
5780 }
5781 wp->w_status_height = 1;
5782 if (fp != fr)
5783 {
5784 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5785 frame_fix_height(wp);
5786 (void)win_comp_pos();
5787 }
5788 else
5789 win_new_height(wp, wp->w_height - 1);
5790 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005791 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 }
5793 }
5794#ifdef FEAT_VERTSPLIT
5795 else if (fr->fr_layout == FR_ROW)
5796 {
5797 /* vertically split windows, set status line for each one */
5798 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5799 last_status_rec(fp, statusline);
5800 }
5801#endif
5802 else
5803 {
5804 /* horizontally split window, set status line for last one */
5805 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5806 ;
5807 last_status_rec(fp, statusline);
5808 }
5809}
5810
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005811/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005812 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005813 */
5814 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005815tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005816{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005817#ifdef FEAT_GUI_TABLINE
5818 /* When the GUI has the tabline then this always returns zero. */
5819 if (gui_use_tabline())
5820 return 0;
5821#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005822 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005823 {
5824 case 0: return 0;
5825 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5826 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005827 return 1;
5828}
5829
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830#endif /* FEAT_WINDOWS */
5831
5832#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5833/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005834 * Get the file name at the cursor.
5835 * If Visual mode is active, use the selected text if it's in one line.
5836 * Returns the name in allocated memory, NULL for failure.
5837 */
5838 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005839grab_file_name(count, file_lnum)
5840 long count;
5841 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005842{
5843# ifdef FEAT_VISUAL
5844 if (VIsual_active)
5845 {
5846 int len;
5847 char_u *ptr;
5848
5849 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5850 return NULL;
5851 return find_file_name_in_path(ptr, len,
5852 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5853 }
5854# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005855 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5856 file_lnum);
5857
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005858}
5859
5860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 * Return the file name under or after the cursor.
5862 *
5863 * The 'path' option is searched if the file name is not absolute.
5864 * The string returned has been alloc'ed and should be freed by the caller.
5865 * NULL is returned if the file name or file is not found.
5866 *
5867 * options:
5868 * FNAME_MESS give error messages
5869 * FNAME_EXP expand to path
5870 * FNAME_HYP check for hypertext link
5871 * FNAME_INCL apply "includeexpr"
5872 */
5873 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005874file_name_at_cursor(options, count, file_lnum)
5875 int options;
5876 long count;
5877 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878{
5879 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005880 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5881 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882}
5883
5884/*
5885 * Return the name of the file under or after ptr[col].
5886 * Otherwise like file_name_at_cursor().
5887 */
5888 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005889file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 char_u *line;
5891 int col;
5892 int options;
5893 long count;
5894 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005895 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896{
5897 char_u *ptr;
5898 int len;
5899
5900 /*
5901 * search forward for what could be the start of a file name
5902 */
5903 ptr = line + col;
5904 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005905 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 if (*ptr == NUL) /* nothing found */
5907 {
5908 if (options & FNAME_MESS)
5909 EMSG(_("E446: No file name under cursor"));
5910 return NULL;
5911 }
5912
5913 /*
5914 * Search backward for first char of the file name.
5915 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5916 */
5917 while (ptr > line)
5918 {
5919#ifdef FEAT_MBYTE
5920 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5921 ptr -= len + 1;
5922 else
5923#endif
5924 if (vim_isfilec(ptr[-1])
5925 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5926 --ptr;
5927 else
5928 break;
5929 }
5930
5931 /*
5932 * Search forward for the last char of the file name.
5933 * Also allow "://" when ':' is not in 'isfname'.
5934 */
5935 len = 0;
5936 while (vim_isfilec(ptr[len])
5937 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5938#ifdef FEAT_MBYTE
5939 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005940 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 else
5942#endif
5943 ++len;
5944
5945 /*
5946 * If there is trailing punctuation, remove it.
5947 * But don't remove "..", could be a directory name.
5948 */
5949 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5950 && ptr[len - 2] != '.')
5951 --len;
5952
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005953 if (file_lnum != NULL)
5954 {
5955 char_u *p;
5956
5957 /* Get the number after the file name and a separator character */
5958 p = ptr + len;
5959 p = skipwhite(p);
5960 if (*p != NUL)
5961 {
5962 if (!isdigit(*p))
5963 ++p; /* skip the separator */
5964 p = skipwhite(p);
5965 if (isdigit(*p))
5966 *file_lnum = (int)getdigits(&p);
5967 }
5968 }
5969
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5971}
5972
5973# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5974static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5975
5976 static char_u *
5977eval_includeexpr(ptr, len)
5978 char_u *ptr;
5979 int len;
5980{
5981 char_u *res;
5982
5983 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005984 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005985 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 set_vim_var_string(VV_FNAME, NULL, 0);
5987 return res;
5988}
5989#endif
5990
5991/*
5992 * Return the name of the file ptr[len] in 'path'.
5993 * Otherwise like file_name_at_cursor().
5994 */
5995 char_u *
5996find_file_name_in_path(ptr, len, options, count, rel_fname)
5997 char_u *ptr;
5998 int len;
5999 int options;
6000 long count;
6001 char_u *rel_fname; /* file we are searching relative to */
6002{
6003 char_u *file_name;
6004 int c;
6005# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6006 char_u *tofree = NULL;
6007
6008 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6009 {
6010 tofree = eval_includeexpr(ptr, len);
6011 if (tofree != NULL)
6012 {
6013 ptr = tofree;
6014 len = (int)STRLEN(ptr);
6015 }
6016 }
6017# endif
6018
6019 if (options & FNAME_EXP)
6020 {
6021 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6022 TRUE, rel_fname);
6023
6024# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6025 /*
6026 * If the file could not be found in a normal way, try applying
6027 * 'includeexpr' (unless done already).
6028 */
6029 if (file_name == NULL
6030 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6031 {
6032 tofree = eval_includeexpr(ptr, len);
6033 if (tofree != NULL)
6034 {
6035 ptr = tofree;
6036 len = (int)STRLEN(ptr);
6037 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6038 TRUE, rel_fname);
6039 }
6040 }
6041# endif
6042 if (file_name == NULL && (options & FNAME_MESS))
6043 {
6044 c = ptr[len];
6045 ptr[len] = NUL;
6046 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6047 ptr[len] = c;
6048 }
6049
6050 /* Repeat finding the file "count" times. This matters when it
6051 * appears several times in the path. */
6052 while (file_name != NULL && --count > 0)
6053 {
6054 vim_free(file_name);
6055 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6056 }
6057 }
6058 else
6059 file_name = vim_strnsave(ptr, len);
6060
6061# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6062 vim_free(tofree);
6063# endif
6064
6065 return file_name;
6066}
6067#endif /* FEAT_SEARCHPATH */
6068
6069/*
6070 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6071 * Also check for ":\\", which MS Internet Explorer accepts, return
6072 * URL_BACKSLASH.
6073 */
6074 static int
6075path_is_url(p)
6076 char_u *p;
6077{
6078 if (STRNCMP(p, "://", (size_t)3) == 0)
6079 return URL_SLASH;
6080 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6081 return URL_BACKSLASH;
6082 return 0;
6083}
6084
6085/*
6086 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6087 * Return URL_BACKSLASH for "name:\\".
6088 * Return zero otherwise.
6089 */
6090 int
6091path_with_url(fname)
6092 char_u *fname;
6093{
6094 char_u *p;
6095
6096 for (p = fname; isalpha(*p); ++p)
6097 ;
6098 return path_is_url(p);
6099}
6100
6101/*
6102 * Return TRUE if "name" is a full (absolute) path name or URL.
6103 */
6104 int
6105vim_isAbsName(name)
6106 char_u *name;
6107{
6108 return (path_with_url(name) != 0 || mch_isFullName(name));
6109}
6110
6111/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006112 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 *
6114 * return FAIL for failure, OK otherwise
6115 */
6116 int
6117vim_FullName(fname, buf, len, force)
6118 char_u *fname, *buf;
6119 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006120 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
6122 int retval = OK;
6123 int url;
6124
6125 *buf = NUL;
6126 if (fname == NULL)
6127 return FAIL;
6128
6129 url = path_with_url(fname);
6130 if (!url)
6131 retval = mch_FullName(fname, buf, len, force);
6132 if (url || retval == FAIL)
6133 {
6134 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006135 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 }
6137#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6138 slash_adjust(buf);
6139#endif
6140 return retval;
6141}
6142
6143/*
6144 * Return the minimal number of rows that is needed on the screen to display
6145 * the current number of windows.
6146 */
6147 int
6148min_rows()
6149{
6150 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006151#ifdef FEAT_WINDOWS
6152 tabpage_T *tp;
6153 int n;
6154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155
6156 if (firstwin == NULL) /* not initialized yet */
6157 return MIN_LINES;
6158
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006160 total = 0;
6161 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6162 {
6163 n = frame_minheight(tp->tp_topframe, NULL);
6164 if (total < n)
6165 total = n;
6166 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006167 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006169 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006171 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 return total;
6173}
6174
6175/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006176 * Return TRUE if there is only one window (in the current tab page), not
6177 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006178 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 */
6180 int
6181only_one_window()
6182{
6183#ifdef FEAT_WINDOWS
6184 int count = 0;
6185 win_T *wp;
6186
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006187 /* If there is another tab page there always is another window. */
6188 if (first_tabpage->tp_next != NULL)
6189 return FALSE;
6190
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006192 if ((!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193# ifdef FEAT_QUICKFIX
6194 || wp->w_p_pvw
6195# endif
6196 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006197# ifdef FEAT_AUTOCMD
6198 && wp != aucmd_win
6199# endif
6200 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 ++count;
6202 return (count <= 1);
6203#else
6204 return TRUE;
6205#endif
6206}
6207
6208#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6209/*
6210 * Correct the cursor line number in other windows. Used after changing the
6211 * current buffer, and before applying autocommands.
6212 * When "do_curwin" is TRUE, also check current window.
6213 */
6214 void
6215check_lnums(do_curwin)
6216 int do_curwin;
6217{
6218 win_T *wp;
6219
6220#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006221 tabpage_T *tp;
6222
6223 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6225#else
6226 wp = curwin;
6227 if (do_curwin)
6228#endif
6229 {
6230 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6231 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6232 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6233 wp->w_topline = curbuf->b_ml.ml_line_count;
6234 }
6235}
6236#endif
6237
6238#if defined(FEAT_WINDOWS) || defined(PROTO)
6239
6240/*
6241 * A snapshot of the window sizes, to restore them after closing the help
6242 * window.
6243 * Only these fields are used:
6244 * fr_layout
6245 * fr_width
6246 * fr_height
6247 * fr_next
6248 * fr_child
6249 * fr_win (only valid for the old curwin, NULL otherwise)
6250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251
6252/*
6253 * Create a snapshot of the current frame sizes.
6254 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006255 void
6256make_snapshot(idx)
6257 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006259 clear_snapshot(curtab, idx);
6260 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261}
6262
6263 static void
6264make_snapshot_rec(fr, frp)
6265 frame_T *fr;
6266 frame_T **frp;
6267{
6268 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6269 if (*frp == NULL)
6270 return;
6271 (*frp)->fr_layout = fr->fr_layout;
6272# ifdef FEAT_VERTSPLIT
6273 (*frp)->fr_width = fr->fr_width;
6274# endif
6275 (*frp)->fr_height = fr->fr_height;
6276 if (fr->fr_next != NULL)
6277 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6278 if (fr->fr_child != NULL)
6279 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6280 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6281 (*frp)->fr_win = curwin;
6282}
6283
6284/*
6285 * Remove any existing snapshot.
6286 */
6287 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006288clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006289 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006290 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006292 clear_snapshot_rec(tp->tp_snapshot[idx]);
6293 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294}
6295
6296 static void
6297clear_snapshot_rec(fr)
6298 frame_T *fr;
6299{
6300 if (fr != NULL)
6301 {
6302 clear_snapshot_rec(fr->fr_next);
6303 clear_snapshot_rec(fr->fr_child);
6304 vim_free(fr);
6305 }
6306}
6307
6308/*
6309 * Restore a previously created snapshot, if there is any.
6310 * This is only done if the screen size didn't change and the window layout is
6311 * still the same.
6312 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006313 void
6314restore_snapshot(idx, close_curwin)
6315 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 int close_curwin; /* closing current window */
6317{
6318 win_T *wp;
6319
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006320 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006322 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006324 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6325 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006327 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 win_comp_pos();
6329 if (wp != NULL && close_curwin)
6330 win_goto(wp);
6331 redraw_all_later(CLEAR);
6332 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006333 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334}
6335
6336/*
6337 * Check if frames "sn" and "fr" have the same layout, same following frames
6338 * and same children.
6339 */
6340 static int
6341check_snapshot_rec(sn, fr)
6342 frame_T *sn;
6343 frame_T *fr;
6344{
6345 if (sn->fr_layout != fr->fr_layout
6346 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6347 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6348 || (sn->fr_next != NULL
6349 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6350 || (sn->fr_child != NULL
6351 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6352 return FAIL;
6353 return OK;
6354}
6355
6356/*
6357 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6358 * following frames and children.
6359 * Returns a pointer to the old current window, or NULL.
6360 */
6361 static win_T *
6362restore_snapshot_rec(sn, fr)
6363 frame_T *sn;
6364 frame_T *fr;
6365{
6366 win_T *wp = NULL;
6367 win_T *wp2;
6368
6369 fr->fr_height = sn->fr_height;
6370# ifdef FEAT_VERTSPLIT
6371 fr->fr_width = sn->fr_width;
6372# endif
6373 if (fr->fr_layout == FR_LEAF)
6374 {
6375 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6376# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006377 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378# endif
6379 wp = sn->fr_win;
6380 }
6381 if (sn->fr_next != NULL)
6382 {
6383 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6384 if (wp2 != NULL)
6385 wp = wp2;
6386 }
6387 if (sn->fr_child != NULL)
6388 {
6389 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6390 if (wp2 != NULL)
6391 wp = wp2;
6392 }
6393 return wp;
6394}
6395
6396#endif
6397
6398#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6399/*
6400 * Return TRUE if there is any vertically split window.
6401 */
6402 int
6403win_hasvertsplit()
6404{
6405 frame_T *fr;
6406
6407 if (topframe->fr_layout == FR_ROW)
6408 return TRUE;
6409
6410 if (topframe->fr_layout == FR_COL)
6411 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6412 if (fr->fr_layout == FR_ROW)
6413 return TRUE;
6414
6415 return FALSE;
6416}
6417#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006418
6419#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6420/*
6421 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006422 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006423 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6424 * If no particular ID is desired, -1 must be specified for 'id'.
6425 * Return ID of added match, -1 on failure.
6426 */
6427 int
6428match_add(wp, grp, pat, prio, id)
6429 win_T *wp;
6430 char_u *grp;
6431 char_u *pat;
6432 int prio;
6433 int id;
6434{
6435 matchitem_T *cur;
6436 matchitem_T *prev;
6437 matchitem_T *m;
6438 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006439 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006440
6441 if (*grp == NUL || *pat == NUL)
6442 return -1;
6443 if (id < -1 || id == 0)
6444 {
6445 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6446 return -1;
6447 }
6448 if (id != -1)
6449 {
6450 cur = wp->w_match_head;
6451 while (cur != NULL)
6452 {
6453 if (cur->id == id)
6454 {
6455 EMSGN("E801: ID already taken: %ld", id);
6456 return -1;
6457 }
6458 cur = cur->next;
6459 }
6460 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006461 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006462 {
6463 EMSG2(_(e_nogroup), grp);
6464 return -1;
6465 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006466 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006467 {
6468 EMSG2(_(e_invarg2), pat);
6469 return -1;
6470 }
6471
6472 /* Find available match ID. */
6473 while (id == -1)
6474 {
6475 cur = wp->w_match_head;
6476 while (cur != NULL && cur->id != wp->w_next_match_id)
6477 cur = cur->next;
6478 if (cur == NULL)
6479 id = wp->w_next_match_id;
6480 wp->w_next_match_id++;
6481 }
6482
6483 /* Build new match. */
6484 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6485 m->id = id;
6486 m->priority = prio;
6487 m->pattern = vim_strsave(pat);
6488 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006489 m->match.regprog = regprog;
6490 m->match.rmm_ic = FALSE;
6491 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006492
6493 /* Insert new match. The match list is in ascending order with regard to
6494 * the match priorities. */
6495 cur = wp->w_match_head;
6496 prev = cur;
6497 while (cur != NULL && prio >= cur->priority)
6498 {
6499 prev = cur;
6500 cur = cur->next;
6501 }
6502 if (cur == prev)
6503 wp->w_match_head = m;
6504 else
6505 prev->next = m;
6506 m->next = cur;
6507
6508 redraw_later(SOME_VALID);
6509 return id;
6510}
6511
6512/*
6513 * Delete match with ID 'id' in the match list of window 'wp'.
6514 * Print error messages if 'perr' is TRUE.
6515 */
6516 int
6517match_delete(wp, id, perr)
6518 win_T *wp;
6519 int id;
6520 int perr;
6521{
6522 matchitem_T *cur = wp->w_match_head;
6523 matchitem_T *prev = cur;
6524
6525 if (id < 1)
6526 {
6527 if (perr == TRUE)
6528 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6529 id);
6530 return -1;
6531 }
6532 while (cur != NULL && cur->id != id)
6533 {
6534 prev = cur;
6535 cur = cur->next;
6536 }
6537 if (cur == NULL)
6538 {
6539 if (perr == TRUE)
6540 EMSGN("E803: ID not found: %ld", id);
6541 return -1;
6542 }
6543 if (cur == prev)
6544 wp->w_match_head = cur->next;
6545 else
6546 prev->next = cur->next;
6547 vim_free(cur->match.regprog);
6548 vim_free(cur->pattern);
6549 vim_free(cur);
6550 redraw_later(SOME_VALID);
6551 return 0;
6552}
6553
6554/*
6555 * Delete all matches in the match list of window 'wp'.
6556 */
6557 void
6558clear_matches(wp)
6559 win_T *wp;
6560{
6561 matchitem_T *m;
6562
6563 while (wp->w_match_head != NULL)
6564 {
6565 m = wp->w_match_head->next;
6566 vim_free(wp->w_match_head->match.regprog);
6567 vim_free(wp->w_match_head->pattern);
6568 vim_free(wp->w_match_head);
6569 wp->w_match_head = m;
6570 }
6571 redraw_later(SOME_VALID);
6572}
6573
6574/*
6575 * Get match from ID 'id' in window 'wp'.
6576 * Return NULL if match not found.
6577 */
6578 matchitem_T *
6579get_match(wp, id)
6580 win_T *wp;
6581 int id;
6582{
6583 matchitem_T *cur = wp->w_match_head;
6584
6585 while (cur != NULL && cur->id != id)
6586 cur = cur->next;
6587 return cur;
6588}
6589#endif