blob: 2feb9e25b5c539b933e4e0efeddd4fb1a71644ae [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)
14static int win_split_ins __ARGS((int size, int flags, win_T *newwin, int dir));
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000015static void win_init __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 Moolenaarf740b292006-02-16 22:11:02 +000026static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
27static win_T *winframe_remove __ARGS((win_T *win, int *dirp, tabpage_T *tp));
28static 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 Moolenaar1d2ba7f2006-02-14 22:29:30 +000044#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000045static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000046static int leave_tabpage __ARGS((buf_T *new_curbuf));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000047static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static void frame_fix_height __ARGS((win_T *wp));
49static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
50static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
Bram Moolenaarf740b292006-02-16 22:11:02 +000051static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000052static void win_append __ARGS((win_T *, win_T *));
Bram Moolenaarf740b292006-02-16 22:11:02 +000053static void win_remove __ARGS((win_T *, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000054static void frame_append __ARGS((frame_T *after, frame_T *frp));
55static void frame_insert __ARGS((frame_T *before, frame_T *frp));
56static void frame_remove __ARGS((frame_T *frp));
57#ifdef FEAT_VERTSPLIT
58static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000059static void win_goto_ver __ARGS((int up, long count));
60static void win_goto_hor __ARGS((int left, long count));
61#endif
62static void frame_add_height __ARGS((frame_T *frp, int n));
63static void last_status_rec __ARGS((frame_T *fr, int statusline));
64
65static void make_snapshot __ARGS((void));
66static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000067static void clear_snapshot __ARGS((tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000068static void clear_snapshot_rec __ARGS((frame_T *fr));
69static void restore_snapshot __ARGS((int close_curwin));
70static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
71static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
72
73#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000074
Bram Moolenaar071d4272004-06-13 20:20:40 +000075static win_T *win_alloc __ARGS((win_T *after));
76static void win_new_height __ARGS((win_T *, int));
77
78#define URL_SLASH 1 /* path_is_url() has found "://" */
79#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
80
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000081#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000082
Bram Moolenaar05159a02005-02-26 23:04:13 +000083#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000084# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000085#else
86# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000087#endif
88
Bram Moolenaar071d4272004-06-13 20:20:40 +000089#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000090
91static char *m_onlyone = N_("Already only one window");
92
Bram Moolenaar071d4272004-06-13 20:20:40 +000093/*
94 * all CTRL-W window commands are handled here, called from normal_cmd().
95 */
96 void
97do_window(nchar, Prenum, xchar)
98 int nchar;
99 long Prenum;
100 int xchar; /* extra char from ":wincmd gx" or NUL */
101{
102 long Prenum1;
103 win_T *wp;
104#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
105 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000106 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
108#ifdef FEAT_FIND_ID
109 int type = FIND_DEFINE;
110 int len;
111#endif
112 char_u cbuf[40];
113
114 if (Prenum == 0)
115 Prenum1 = 1;
116 else
117 Prenum1 = Prenum;
118
119#ifdef FEAT_CMDWIN
120# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
121#else
122# define CHECK_CMDWIN
123#endif
124
125 switch (nchar)
126 {
127/* split current window in two parts, horizontally */
128 case 'S':
129 case Ctrl_S:
130 case 's':
131 CHECK_CMDWIN
132#ifdef FEAT_VISUAL
133 reset_VIsual_and_resel(); /* stop Visual mode */
134#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000135#ifdef FEAT_QUICKFIX
136 /* When splitting the quickfix window open a new buffer in it,
137 * don't replicate the quickfix buffer. */
138 if (bt_quickfix(curbuf))
139 goto newwindow;
140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_GUI
142 need_mouse_correct = TRUE;
143#endif
144 win_split((int)Prenum, 0);
145 break;
146
147#ifdef FEAT_VERTSPLIT
148/* split current window in two parts, vertically */
149 case Ctrl_V:
150 case 'v':
151 CHECK_CMDWIN
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000152# ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000154# endif
155# ifdef FEAT_QUICKFIX
156 /* When splitting the quickfix window open a new buffer in it,
157 * don't replicate the quickfix buffer. */
158 if (bt_quickfix(curbuf))
159 goto newwindow;
160# endif
161# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000163# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 win_split((int)Prenum, WSP_VERT);
165 break;
166#endif
167
168/* split current window and edit alternate file */
169 case Ctrl_HAT:
170 case '^':
171 CHECK_CMDWIN
172#ifdef FEAT_VISUAL
173 reset_VIsual_and_resel(); /* stop Visual mode */
174#endif
175 STRCPY(cbuf, "split #");
176 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000177 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
178 "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 do_cmdline_cmd(cbuf);
180 break;
181
182/* open new window */
183 case Ctrl_N:
184 case 'n':
185 CHECK_CMDWIN
186#ifdef FEAT_VISUAL
187 reset_VIsual_and_resel(); /* stop Visual mode */
188#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000189#ifdef FEAT_QUICKFIX
190newwindow:
191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000193 /* window height */
194 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 else
196 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000197#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
198 if (nchar == 'v' || nchar == Ctrl_V)
199 STRCAT(cbuf, "v");
200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 STRCAT(cbuf, "new");
202 do_cmdline_cmd(cbuf);
203 break;
204
205/* quit current window */
206 case Ctrl_Q:
207 case 'q':
208#ifdef FEAT_VISUAL
209 reset_VIsual_and_resel(); /* stop Visual mode */
210#endif
211 do_cmdline_cmd((char_u *)"quit");
212 break;
213
214/* close current window */
215 case Ctrl_C:
216 case 'c':
217#ifdef FEAT_VISUAL
218 reset_VIsual_and_resel(); /* stop Visual mode */
219#endif
220 do_cmdline_cmd((char_u *)"close");
221 break;
222
223#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
224/* close preview window */
225 case Ctrl_Z:
226 case 'z':
227 CHECK_CMDWIN
228#ifdef FEAT_VISUAL
229 reset_VIsual_and_resel(); /* stop Visual mode */
230#endif
231 do_cmdline_cmd((char_u *)"pclose");
232 break;
233
234/* cursor to preview window */
235 case 'P':
236 for (wp = firstwin; wp != NULL; wp = wp->w_next)
237 if (wp->w_p_pvw)
238 break;
239 if (wp == NULL)
240 EMSG(_("E441: There is no preview window"));
241 else
242 win_goto(wp);
243 break;
244#endif
245
246/* close all but current window */
247 case Ctrl_O:
248 case 'o':
249 CHECK_CMDWIN
250#ifdef FEAT_VISUAL
251 reset_VIsual_and_resel(); /* stop Visual mode */
252#endif
253 do_cmdline_cmd((char_u *)"only");
254 break;
255
256/* cursor to next window with wrap around */
257 case Ctrl_W:
258 case 'w':
259/* cursor to previous window with wrap around */
260 case 'W':
261 CHECK_CMDWIN
262 if (lastwin == firstwin && Prenum != 1) /* just one window */
263 beep_flush();
264 else
265 {
266 if (Prenum) /* go to specified window */
267 {
268 for (wp = firstwin; --Prenum > 0; )
269 {
270 if (wp->w_next == NULL)
271 break;
272 else
273 wp = wp->w_next;
274 }
275 }
276 else
277 {
278 if (nchar == 'W') /* go to previous window */
279 {
280 wp = curwin->w_prev;
281 if (wp == NULL)
282 wp = lastwin; /* wrap around */
283 }
284 else /* go to next window */
285 {
286 wp = curwin->w_next;
287 if (wp == NULL)
288 wp = firstwin; /* wrap around */
289 }
290 }
291 win_goto(wp);
292 }
293 break;
294
295/* cursor to window below */
296 case 'j':
297 case K_DOWN:
298 case Ctrl_J:
299 CHECK_CMDWIN
300#ifdef FEAT_VERTSPLIT
301 win_goto_ver(FALSE, Prenum1);
302#else
303 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
304 wp = wp->w_next)
305 ;
306 win_goto(wp);
307#endif
308 break;
309
310/* cursor to window above */
311 case 'k':
312 case K_UP:
313 case Ctrl_K:
314 CHECK_CMDWIN
315#ifdef FEAT_VERTSPLIT
316 win_goto_ver(TRUE, Prenum1);
317#else
318 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
319 wp = wp->w_prev)
320 ;
321 win_goto(wp);
322#endif
323 break;
324
325#ifdef FEAT_VERTSPLIT
326/* cursor to left window */
327 case 'h':
328 case K_LEFT:
329 case Ctrl_H:
330 case K_BS:
331 CHECK_CMDWIN
332 win_goto_hor(TRUE, Prenum1);
333 break;
334
335/* cursor to right window */
336 case 'l':
337 case K_RIGHT:
338 case Ctrl_L:
339 CHECK_CMDWIN
340 win_goto_hor(FALSE, Prenum1);
341 break;
342#endif
343
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000344/* move window to new tab page */
345 case 'T':
346 if (firstwin == lastwin)
347 MSG(_(m_onlyone));
348 else
349 {
350 tabpage_T *oldtab = curtab;
351 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000352
353 /* First create a new tab with the window, then go back to
354 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000355 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000356 if (win_new_tabpage((int)Prenum) == OK
357 && valid_tabpage(oldtab))
358 {
359 newtab = curtab;
360 goto_tabpage_tp(oldtab);
361 if (curwin == wp)
362 win_close(curwin, FALSE);
363 if (valid_tabpage(newtab))
364 goto_tabpage_tp(newtab);
365 }
366 }
367 break;
368
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369/* cursor to top-left window */
370 case 't':
371 case Ctrl_T:
372 win_goto(firstwin);
373 break;
374
375/* cursor to bottom-right window */
376 case 'b':
377 case Ctrl_B:
378 win_goto(lastwin);
379 break;
380
381/* cursor to last accessed (previous) window */
382 case 'p':
383 case Ctrl_P:
384 if (prevwin == NULL)
385 beep_flush();
386 else
387 win_goto(prevwin);
388 break;
389
390/* exchange current and next window */
391 case 'x':
392 case Ctrl_X:
393 CHECK_CMDWIN
394 win_exchange(Prenum);
395 break;
396
397/* rotate windows downwards */
398 case Ctrl_R:
399 case 'r':
400 CHECK_CMDWIN
401#ifdef FEAT_VISUAL
402 reset_VIsual_and_resel(); /* stop Visual mode */
403#endif
404 win_rotate(FALSE, (int)Prenum1); /* downwards */
405 break;
406
407/* rotate windows upwards */
408 case 'R':
409 CHECK_CMDWIN
410#ifdef FEAT_VISUAL
411 reset_VIsual_and_resel(); /* stop Visual mode */
412#endif
413 win_rotate(TRUE, (int)Prenum1); /* upwards */
414 break;
415
416/* move window to the very top/bottom/left/right */
417 case 'K':
418 case 'J':
419#ifdef FEAT_VERTSPLIT
420 case 'H':
421 case 'L':
422#endif
423 CHECK_CMDWIN
424 win_totop((int)Prenum,
425 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
426 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
427 break;
428
429/* make all windows the same height */
430 case '=':
431#ifdef FEAT_GUI
432 need_mouse_correct = TRUE;
433#endif
434 win_equal(NULL, FALSE, 'b');
435 break;
436
437/* increase current window height */
438 case '+':
439#ifdef FEAT_GUI
440 need_mouse_correct = TRUE;
441#endif
442 win_setheight(curwin->w_height + (int)Prenum1);
443 break;
444
445/* decrease current window height */
446 case '-':
447#ifdef FEAT_GUI
448 need_mouse_correct = TRUE;
449#endif
450 win_setheight(curwin->w_height - (int)Prenum1);
451 break;
452
453/* set current window height */
454 case Ctrl__:
455 case '_':
456#ifdef FEAT_GUI
457 need_mouse_correct = TRUE;
458#endif
459 win_setheight(Prenum ? (int)Prenum : 9999);
460 break;
461
462#ifdef FEAT_VERTSPLIT
463/* increase current window width */
464 case '>':
465#ifdef FEAT_GUI
466 need_mouse_correct = TRUE;
467#endif
468 win_setwidth(curwin->w_width + (int)Prenum1);
469 break;
470
471/* decrease current window width */
472 case '<':
473#ifdef FEAT_GUI
474 need_mouse_correct = TRUE;
475#endif
476 win_setwidth(curwin->w_width - (int)Prenum1);
477 break;
478
479/* set current window width */
480 case '|':
481#ifdef FEAT_GUI
482 need_mouse_correct = TRUE;
483#endif
484 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
485 break;
486#endif
487
488/* jump to tag and split window if tag exists (in preview window) */
489#if defined(FEAT_QUICKFIX)
490 case '}':
491 CHECK_CMDWIN
492 if (Prenum)
493 g_do_tagpreview = Prenum;
494 else
495 g_do_tagpreview = p_pvh;
496 /*FALLTHROUGH*/
497#endif
498 case ']':
499 case Ctrl_RSB:
500 CHECK_CMDWIN
501#ifdef FEAT_VISUAL
502 reset_VIsual_and_resel(); /* stop Visual mode */
503#endif
504 if (Prenum)
505 postponed_split = Prenum;
506 else
507 postponed_split = -1;
508
509 /* Execute the command right here, required when
510 * "wincmd ]" was used in a function. */
511 do_nv_ident(Ctrl_RSB, NUL);
512 break;
513
514#ifdef FEAT_SEARCHPATH
515/* edit file name under cursor in a new window */
516 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000517 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000519wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000521
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000522 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 if (ptr != NULL)
524 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000525# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000527# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 setpcmark();
529 if (win_split(0, 0) == OK)
530 {
531# ifdef FEAT_SCROLLBIND
532 curwin->w_p_scb = FALSE;
533# endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000534 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
535 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000536 if (nchar == 'F' && lnum >= 0)
537 {
538 curwin->w_cursor.lnum = lnum;
539 check_cursor_lnum();
540 beginline(BL_SOL | BL_FIX);
541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 }
543 vim_free(ptr);
544 }
545 break;
546#endif
547
548#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000549/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 * new window -- webb
551 */
552 case 'i': /* Go to any match */
553 case Ctrl_I:
554 type = FIND_ANY;
555 /* FALLTHROUGH */
556 case 'd': /* Go to definition, using 'define' */
557 case Ctrl_D:
558 CHECK_CMDWIN
559 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
560 break;
561 find_pattern_in_path(ptr, 0, len, TRUE,
562 Prenum == 0 ? TRUE : FALSE, type,
563 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
564 curwin->w_set_curswant = TRUE;
565 break;
566#endif
567
Bram Moolenaar05159a02005-02-26 23:04:13 +0000568 case K_KENTER:
569 case CAR:
570#if defined(FEAT_QUICKFIX)
571 /*
572 * In a quickfix window a <CR> jumps to the error under the
573 * cursor in a new window.
574 */
575 if (bt_quickfix(curbuf))
576 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000577 sprintf((char *)cbuf, "split +%ld%s",
578 (long)curwin->w_cursor.lnum,
579 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000580 do_cmdline_cmd(cbuf);
581 }
582#endif
583 break;
584
585
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586/* CTRL-W g extended commands */
587 case 'g':
588 case Ctrl_G:
589 CHECK_CMDWIN
590#ifdef USE_ON_FLY_SCROLL
591 dont_scroll = TRUE; /* disallow scrolling here */
592#endif
593 ++no_mapping;
594 ++allow_keys; /* no mapping for xchar, but allow key codes */
595 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000596 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597#ifdef FEAT_LANGMAP
598 LANGMAP_ADJUST(xchar, TRUE);
599#endif
600 --no_mapping;
601 --allow_keys;
602#ifdef FEAT_CMDL_INFO
603 (void)add_to_showcmd(xchar);
604#endif
605 switch (xchar)
606 {
607#if defined(FEAT_QUICKFIX)
608 case '}':
609 xchar = Ctrl_RSB;
610 if (Prenum)
611 g_do_tagpreview = Prenum;
612 else
613 g_do_tagpreview = p_pvh;
614 /*FALLTHROUGH*/
615#endif
616 case ']':
617 case Ctrl_RSB:
618#ifdef FEAT_VISUAL
619 reset_VIsual_and_resel(); /* stop Visual mode */
620#endif
621 if (Prenum)
622 postponed_split = Prenum;
623 else
624 postponed_split = -1;
625
626 /* Execute the command right here, required when
627 * "wincmd g}" was used in a function. */
628 do_nv_ident('g', xchar);
629 break;
630
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000631#ifdef FEAT_SEARCHPATH
632 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000633 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000634 cmdmod.tab = TRUE;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000635 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000636 goto wingotofile;
637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 default:
639 beep_flush();
640 break;
641 }
642 break;
643
644 default: beep_flush();
645 break;
646 }
647}
648
649/*
650 * split the current window, implements CTRL-W s and :split
651 *
652 * "size" is the height or width for the new window, 0 to use half of current
653 * height or width.
654 *
655 * "flags":
656 * WSP_ROOM: require enough room for new window
657 * WSP_VERT: vertical split.
658 * WSP_TOP: open window at the top-left of the shell (help window).
659 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
660 * WSP_HELP: creating the help window, keep layout snapshot
661 *
662 * return FAIL for failure, OK otherwise
663 */
664 int
665win_split(size, flags)
666 int size;
667 int flags;
668{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000669 /* When the ":tab" modifier was used open a new tab page instead. */
670 if (may_open_tabpage() == OK)
671 return OK;
672
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 /* Add flags from ":vertical", ":topleft" and ":botright". */
674 flags |= cmdmod.split;
675 if ((flags & WSP_TOP) && (flags & WSP_BOT))
676 {
677 EMSG(_("E442: Can't split topleft and botright at the same time"));
678 return FAIL;
679 }
680
681 /* When creating the help window make a snapshot of the window layout.
682 * Otherwise clear the snapshot, it's now invalid. */
683 if (flags & WSP_HELP)
684 make_snapshot();
685 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000686 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687
688 return win_split_ins(size, flags, NULL, 0);
689}
690
691/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000692 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 * When "newwin" is not NULL: insert this window at the far
694 * top/left/right/bottom.
695 * return FAIL for failure, OK otherwise
696 */
697 static int
698win_split_ins(size, flags, newwin, dir)
699 int size;
700 int flags;
701 win_T *newwin;
702 int dir;
703{
704 win_T *wp = newwin;
705 win_T *oldwin;
706 int new_size = size;
707 int i;
708 int need_status = 0;
709 int do_equal = FALSE;
710 int needed;
711 int available;
712 int oldwin_height = 0;
713 int layout;
714 frame_T *frp, *curfrp;
715 int before;
716
717 if (flags & WSP_TOP)
718 oldwin = firstwin;
719 else if (flags & WSP_BOT)
720 oldwin = lastwin;
721 else
722 oldwin = curwin;
723
724 /* add a status line when p_ls == 1 and splitting the first window */
725 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
726 {
727 if (oldwin->w_height <= p_wmh && newwin == NULL)
728 {
729 EMSG(_(e_noroom));
730 return FAIL;
731 }
732 need_status = STATUS_HEIGHT;
733 }
734
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000735#ifdef FEAT_GUI
736 /* May be needed for the scrollbars that are going to change. */
737 if (gui.in_use)
738 out_flush();
739#endif
740
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741#ifdef FEAT_VERTSPLIT
742 if (flags & WSP_VERT)
743 {
744 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745
746 /*
747 * Check if we are able to split the current window and compute its
748 * width.
749 */
750 needed = p_wmw + 1;
751 if (flags & WSP_ROOM)
752 needed += p_wiw - p_wmw;
753 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
754 {
755 available = topframe->fr_width;
756 needed += frame_minwidth(topframe, NULL);
757 }
758 else
759 available = oldwin->w_width;
760 if (available < needed && newwin == NULL)
761 {
762 EMSG(_(e_noroom));
763 return FAIL;
764 }
765 if (new_size == 0)
766 new_size = oldwin->w_width / 2;
767 if (new_size > oldwin->w_width - p_wmw - 1)
768 new_size = oldwin->w_width - p_wmw - 1;
769 if (new_size < p_wmw)
770 new_size = p_wmw;
771
772 /* if it doesn't fit in the current window, need win_equal() */
773 if (oldwin->w_width - new_size - 1 < p_wmw)
774 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000775
776 /* We don't like to take lines for the new window from a
777 * 'winfixwidth' window. Take them from a window to the left or right
778 * instead, if possible. */
779 if (oldwin->w_p_wfw)
780 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000781
782 /* Only make all windows the same width if one of them (except oldwin)
783 * is wider than one of the split windows. */
784 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
785 && oldwin->w_frame->fr_parent != NULL)
786 {
787 frp = oldwin->w_frame->fr_parent->fr_child;
788 while (frp != NULL)
789 {
790 if (frp->fr_win != oldwin && frp->fr_win != NULL
791 && (frp->fr_win->w_width > new_size
792 || frp->fr_win->w_width > oldwin->w_width
793 - new_size - STATUS_HEIGHT))
794 {
795 do_equal = TRUE;
796 break;
797 }
798 frp = frp->fr_next;
799 }
800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 }
802 else
803#endif
804 {
805 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806
807 /*
808 * Check if we are able to split the current window and compute its
809 * height.
810 */
811 needed = p_wmh + STATUS_HEIGHT + need_status;
812 if (flags & WSP_ROOM)
813 needed += p_wh - p_wmh;
814 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
815 {
816 available = topframe->fr_height;
817 needed += frame_minheight(topframe, NULL);
818 }
819 else
820 {
821 available = oldwin->w_height;
822 needed += p_wmh;
823 }
824 if (available < needed && newwin == NULL)
825 {
826 EMSG(_(e_noroom));
827 return FAIL;
828 }
829 oldwin_height = oldwin->w_height;
830 if (need_status)
831 {
832 oldwin->w_status_height = STATUS_HEIGHT;
833 oldwin_height -= STATUS_HEIGHT;
834 }
835 if (new_size == 0)
836 new_size = oldwin_height / 2;
837
838 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
839 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
840 if (new_size < p_wmh)
841 new_size = p_wmh;
842
843 /* if it doesn't fit in the current window, need win_equal() */
844 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
845 do_equal = TRUE;
846
847 /* We don't like to take lines for the new window from a
848 * 'winfixheight' window. Take them from a window above or below
849 * instead, if possible. */
850 if (oldwin->w_p_wfh)
851 {
852 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
853 oldwin);
854 oldwin_height = oldwin->w_height;
855 if (need_status)
856 oldwin_height -= STATUS_HEIGHT;
857 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000858
859 /* Only make all windows the same height if one of them (except oldwin)
860 * is higher than one of the split windows. */
861 if (!do_equal && p_ea && size == 0
862#ifdef FEAT_VERTSPLIT
863 && *p_ead != 'h'
864#endif
865 && oldwin->w_frame->fr_parent != NULL)
866 {
867 frp = oldwin->w_frame->fr_parent->fr_child;
868 while (frp != NULL)
869 {
870 if (frp->fr_win != oldwin && frp->fr_win != NULL
871 && (frp->fr_win->w_height > new_size
872 || frp->fr_win->w_height > oldwin_height - new_size
873 - STATUS_HEIGHT))
874 {
875 do_equal = TRUE;
876 break;
877 }
878 frp = frp->fr_next;
879 }
880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 }
882
883 /*
884 * allocate new window structure and link it in the window list
885 */
886 if ((flags & WSP_TOP) == 0
887 && ((flags & WSP_BOT)
888 || (flags & WSP_BELOW)
889 || (!(flags & WSP_ABOVE)
890 && (
891#ifdef FEAT_VERTSPLIT
892 (flags & WSP_VERT) ? p_spr :
893#endif
894 p_sb))))
895 {
896 /* new window below/right of current one */
897 if (newwin == NULL)
898 wp = win_alloc(oldwin);
899 else
900 win_append(oldwin, wp);
901 }
902 else
903 {
904 if (newwin == NULL)
905 wp = win_alloc(oldwin->w_prev);
906 else
907 win_append(oldwin->w_prev, wp);
908 }
909
910 if (newwin == NULL)
911 {
912 if (wp == NULL)
913 return FAIL;
914
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000915 /* make the contents of the new window the same as the current one */
916 win_init(wp, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
918
919 /*
920 * Reorganise the tree of frames to insert the new window.
921 */
922 if (flags & (WSP_TOP | WSP_BOT))
923 {
924#ifdef FEAT_VERTSPLIT
925 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
926 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
927#else
928 if (topframe->fr_layout == FR_COL)
929#endif
930 {
931 curfrp = topframe->fr_child;
932 if (flags & WSP_BOT)
933 while (curfrp->fr_next != NULL)
934 curfrp = curfrp->fr_next;
935 }
936 else
937 curfrp = topframe;
938 before = (flags & WSP_TOP);
939 }
940 else
941 {
942 curfrp = oldwin->w_frame;
943 if (flags & WSP_BELOW)
944 before = FALSE;
945 else if (flags & WSP_ABOVE)
946 before = TRUE;
947 else
948#ifdef FEAT_VERTSPLIT
949 if (flags & WSP_VERT)
950 before = !p_spr;
951 else
952#endif
953 before = !p_sb;
954 }
955 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
956 {
957 /* Need to create a new frame in the tree to make a branch. */
958 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
959 *frp = *curfrp;
960 curfrp->fr_layout = layout;
961 frp->fr_parent = curfrp;
962 frp->fr_next = NULL;
963 frp->fr_prev = NULL;
964 curfrp->fr_child = frp;
965 curfrp->fr_win = NULL;
966 curfrp = frp;
967 if (frp->fr_win != NULL)
968 oldwin->w_frame = frp;
969 else
970 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
971 frp->fr_parent = curfrp;
972 }
973
974 if (newwin == NULL)
975 {
976 /* Create a frame for the new window. */
977 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
978 frp->fr_layout = FR_LEAF;
979 frp->fr_win = wp;
980 wp->w_frame = frp;
981 }
982 else
983 frp = newwin->w_frame;
984 frp->fr_parent = curfrp->fr_parent;
985
986 /* Insert the new frame at the right place in the frame list. */
987 if (before)
988 frame_insert(curfrp, frp);
989 else
990 frame_append(curfrp, frp);
991
992#ifdef FEAT_VERTSPLIT
993 if (flags & WSP_VERT)
994 {
995 wp->w_p_scr = curwin->w_p_scr;
996 if (need_status)
997 {
998 --oldwin->w_height;
999 oldwin->w_status_height = need_status;
1000 }
1001 if (flags & (WSP_TOP | WSP_BOT))
1002 {
1003 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001004 wp->w_winrow = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 wp->w_height = curfrp->fr_height - (p_ls > 0);
1006 wp->w_status_height = (p_ls > 0);
1007 }
1008 else
1009 {
1010 /* height and row of new window is same as current window */
1011 wp->w_winrow = oldwin->w_winrow;
1012 wp->w_height = oldwin->w_height;
1013 wp->w_status_height = oldwin->w_status_height;
1014 }
1015 frp->fr_height = curfrp->fr_height;
1016
1017 /* "new_size" of the current window goes to the new window, use
1018 * one column for the vertical separator */
1019 wp->w_width = new_size;
1020 if (before)
1021 wp->w_vsep_width = 1;
1022 else
1023 {
1024 wp->w_vsep_width = oldwin->w_vsep_width;
1025 oldwin->w_vsep_width = 1;
1026 }
1027 if (flags & (WSP_TOP | WSP_BOT))
1028 {
1029 if (flags & WSP_BOT)
1030 frame_add_vsep(curfrp);
1031 /* Set width of neighbor frame */
1032 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001033 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1034 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 }
1036 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001037 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 if (before) /* new window left of current one */
1039 {
1040 wp->w_wincol = oldwin->w_wincol;
1041 oldwin->w_wincol += new_size + 1;
1042 }
1043 else /* new window right of current one */
1044 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1045 frame_fix_width(oldwin);
1046 frame_fix_width(wp);
1047 }
1048 else
1049#endif
1050 {
1051 /* width and column of new window is same as current window */
1052#ifdef FEAT_VERTSPLIT
1053 if (flags & (WSP_TOP | WSP_BOT))
1054 {
1055 wp->w_wincol = 0;
1056 wp->w_width = Columns;
1057 wp->w_vsep_width = 0;
1058 }
1059 else
1060 {
1061 wp->w_wincol = oldwin->w_wincol;
1062 wp->w_width = oldwin->w_width;
1063 wp->w_vsep_width = oldwin->w_vsep_width;
1064 }
1065 frp->fr_width = curfrp->fr_width;
1066#endif
1067
1068 /* "new_size" of the current window goes to the new window, use
1069 * one row for the status line */
1070 win_new_height(wp, new_size);
1071 if (flags & (WSP_TOP | WSP_BOT))
1072 frame_new_height(curfrp, curfrp->fr_height
1073 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1074 else
1075 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1076 if (before) /* new window above current one */
1077 {
1078 wp->w_winrow = oldwin->w_winrow;
1079 wp->w_status_height = STATUS_HEIGHT;
1080 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1081 }
1082 else /* new window below current one */
1083 {
1084 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1085 wp->w_status_height = oldwin->w_status_height;
1086 oldwin->w_status_height = STATUS_HEIGHT;
1087 }
1088#ifdef FEAT_VERTSPLIT
1089 if (flags & WSP_BOT)
1090 frame_add_statusline(curfrp);
1091#endif
1092 frame_fix_height(wp);
1093 frame_fix_height(oldwin);
1094 }
1095
1096 if (flags & (WSP_TOP | WSP_BOT))
1097 (void)win_comp_pos();
1098
1099 /*
1100 * Both windows need redrawing
1101 */
1102 redraw_win_later(wp, NOT_VALID);
1103 wp->w_redr_status = TRUE;
1104 redraw_win_later(oldwin, NOT_VALID);
1105 oldwin->w_redr_status = TRUE;
1106
1107 if (need_status)
1108 {
1109 msg_row = Rows - 1;
1110 msg_col = sc_col;
1111 msg_clr_eos_force(); /* Old command/ruler may still be there */
1112 comp_col();
1113 msg_row = Rows - 1;
1114 msg_col = 0; /* put position back at start of line */
1115 }
1116
1117 /*
1118 * make the new window the current window and redraw
1119 */
1120 if (do_equal || dir != 0)
1121 win_equal(wp, TRUE,
1122#ifdef FEAT_VERTSPLIT
1123 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1124 : dir == 'h' ? 'b' :
1125#endif
1126 'v');
1127
1128 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1129 * size was given. */
1130#ifdef FEAT_VERTSPLIT
1131 if (flags & WSP_VERT)
1132 {
1133 i = p_wiw;
1134 if (size != 0)
1135 p_wiw = size;
1136
1137# ifdef FEAT_GUI
1138 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1139 if (gui.in_use)
1140 gui_init_which_components(NULL);
1141# endif
1142 }
1143 else
1144#endif
1145 {
1146 i = p_wh;
1147 if (size != 0)
1148 p_wh = size;
1149 }
1150 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 Moolenaar2a0449d2006-02-20 21:27:21 +00001161/*
1162 * Initialize window "newp" from window "oldp".
1163 * Used when splitting a window and when creating a new tab page.
1164 * The windows will both edit the same buffer.
1165 */
1166 static void
1167win_init(newp, oldp)
1168 win_T *newp;
1169 win_T *oldp;
1170{
1171 int i;
1172
1173 newp->w_buffer = oldp->w_buffer;
1174 oldp->w_buffer->b_nwindows++;
1175 newp->w_cursor = oldp->w_cursor;
1176 newp->w_valid = 0;
1177 newp->w_curswant = oldp->w_curswant;
1178 newp->w_set_curswant = oldp->w_set_curswant;
1179 newp->w_topline = oldp->w_topline;
1180#ifdef FEAT_DIFF
1181 newp->w_topfill = oldp->w_topfill;
1182#endif
1183 newp->w_leftcol = oldp->w_leftcol;
1184 newp->w_pcmark = oldp->w_pcmark;
1185 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1186 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001187 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001188 newp->w_fraction = oldp->w_fraction;
1189 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1190#ifdef FEAT_JUMPLIST
1191 copy_jumplist(oldp, newp);
1192#endif
1193#ifdef FEAT_QUICKFIX
1194 copy_loclist(oldp, newp);
1195#endif
1196 if (oldp->w_localdir != NULL)
1197 newp->w_localdir = vim_strsave(oldp->w_localdir);
1198
1199 /* Use the same argument list. */
1200 newp->w_alist = oldp->w_alist;
1201 ++newp->w_alist->al_refcount;
1202 newp->w_arg_idx = oldp->w_arg_idx;
1203
1204 /*
1205 * copy tagstack and options from existing window
1206 */
1207 for (i = 0; i < oldp->w_tagstacklen; i++)
1208 {
1209 newp->w_tagstack[i] = oldp->w_tagstack[i];
1210 if (newp->w_tagstack[i].tagname != NULL)
1211 newp->w_tagstack[i].tagname =
1212 vim_strsave(newp->w_tagstack[i].tagname);
1213 }
1214 newp->w_tagstackidx = oldp->w_tagstackidx;
1215 newp->w_tagstacklen = oldp->w_tagstacklen;
1216 win_copy_options(oldp, newp);
1217# ifdef FEAT_FOLDING
1218 copyFoldingState(oldp, newp);
1219# endif
1220}
1221
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222#endif /* FEAT_WINDOWS */
1223
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224#if defined(FEAT_WINDOWS) || defined(PROTO)
1225/*
1226 * Check if "win" is a pointer to an existing window.
1227 */
1228 int
1229win_valid(win)
1230 win_T *win;
1231{
1232 win_T *wp;
1233
1234 if (win == NULL)
1235 return FALSE;
1236 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1237 if (wp == win)
1238 return TRUE;
1239 return FALSE;
1240}
1241
1242/*
1243 * Return the number of windows.
1244 */
1245 int
1246win_count()
1247{
1248 win_T *wp;
1249 int count = 0;
1250
1251 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1252 ++count;
1253 return count;
1254}
1255
1256/*
1257 * Make "count" windows on the screen.
1258 * Return actual number of windows on the screen.
1259 * Must be called when there is just one window, filling the whole screen
1260 * (excluding the command line).
1261 */
1262/*ARGSUSED*/
1263 int
1264make_windows(count, vertical)
1265 int count;
1266 int vertical; /* split windows vertically if TRUE */
1267{
1268 int maxcount;
1269 int todo;
1270
1271#ifdef FEAT_VERTSPLIT
1272 if (vertical)
1273 {
1274 /* Each windows needs at least 'winminwidth' lines and a separator
1275 * column. */
1276 maxcount = (curwin->w_width + curwin->w_vsep_width
1277 - (p_wiw - p_wmw)) / (p_wmw + 1);
1278 }
1279 else
1280#endif
1281 {
1282 /* Each window needs at least 'winminheight' lines and a status line. */
1283 maxcount = (curwin->w_height + curwin->w_status_height
1284 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1285 }
1286
1287 if (maxcount < 2)
1288 maxcount = 2;
1289 if (count > maxcount)
1290 count = maxcount;
1291
1292 /*
1293 * add status line now, otherwise first window will be too big
1294 */
1295 if (count > 1)
1296 last_status(TRUE);
1297
1298#ifdef FEAT_AUTOCMD
1299 /*
1300 * Don't execute autocommands while creating the windows. Must do that
1301 * when putting the buffers in the windows.
1302 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001303 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304#endif
1305
1306 /* todo is number of windows left to create */
1307 for (todo = count - 1; todo > 0; --todo)
1308#ifdef FEAT_VERTSPLIT
1309 if (vertical)
1310 {
1311 if (win_split(curwin->w_width - (curwin->w_width - todo)
1312 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1313 break;
1314 }
1315 else
1316#endif
1317 {
1318 if (win_split(curwin->w_height - (curwin->w_height - todo
1319 * STATUS_HEIGHT) / (todo + 1)
1320 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1321 break;
1322 }
1323
1324#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001325 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326#endif
1327
1328 /* return actual number of windows */
1329 return (count - todo);
1330}
1331
1332/*
1333 * Exchange current and next window
1334 */
1335 static void
1336win_exchange(Prenum)
1337 long Prenum;
1338{
1339 frame_T *frp;
1340 frame_T *frp2;
1341 win_T *wp;
1342 win_T *wp2;
1343 int temp;
1344
1345 if (lastwin == firstwin) /* just one window */
1346 {
1347 beep_flush();
1348 return;
1349 }
1350
1351#ifdef FEAT_GUI
1352 need_mouse_correct = TRUE;
1353#endif
1354
1355 /*
1356 * find window to exchange with
1357 */
1358 if (Prenum)
1359 {
1360 frp = curwin->w_frame->fr_parent->fr_child;
1361 while (frp != NULL && --Prenum > 0)
1362 frp = frp->fr_next;
1363 }
1364 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1365 frp = curwin->w_frame->fr_next;
1366 else /* Swap last window in row/col with previous */
1367 frp = curwin->w_frame->fr_prev;
1368
1369 /* We can only exchange a window with another window, not with a frame
1370 * containing windows. */
1371 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1372 return;
1373 wp = frp->fr_win;
1374
1375/*
1376 * 1. remove curwin from the list. Remember after which window it was in wp2
1377 * 2. insert curwin before wp in the list
1378 * if wp != wp2
1379 * 3. remove wp from the list
1380 * 4. insert wp after wp2
1381 * 5. exchange the status line height and vsep width.
1382 */
1383 wp2 = curwin->w_prev;
1384 frp2 = curwin->w_frame->fr_prev;
1385 if (wp->w_prev != curwin)
1386 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001387 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 frame_remove(curwin->w_frame);
1389 win_append(wp->w_prev, curwin);
1390 frame_insert(frp, curwin->w_frame);
1391 }
1392 if (wp != wp2)
1393 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001394 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 frame_remove(wp->w_frame);
1396 win_append(wp2, wp);
1397 if (frp2 == NULL)
1398 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1399 else
1400 frame_append(frp2, wp->w_frame);
1401 }
1402 temp = curwin->w_status_height;
1403 curwin->w_status_height = wp->w_status_height;
1404 wp->w_status_height = temp;
1405#ifdef FEAT_VERTSPLIT
1406 temp = curwin->w_vsep_width;
1407 curwin->w_vsep_width = wp->w_vsep_width;
1408 wp->w_vsep_width = temp;
1409
1410 /* If the windows are not in the same frame, exchange the sizes to avoid
1411 * messing up the window layout. Otherwise fix the frame sizes. */
1412 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1413 {
1414 temp = curwin->w_height;
1415 curwin->w_height = wp->w_height;
1416 wp->w_height = temp;
1417 temp = curwin->w_width;
1418 curwin->w_width = wp->w_width;
1419 wp->w_width = temp;
1420 }
1421 else
1422 {
1423 frame_fix_height(curwin);
1424 frame_fix_height(wp);
1425 frame_fix_width(curwin);
1426 frame_fix_width(wp);
1427 }
1428#endif
1429
1430 (void)win_comp_pos(); /* recompute window positions */
1431
1432 win_enter(wp, TRUE);
1433 redraw_later(CLEAR);
1434}
1435
1436/*
1437 * rotate windows: if upwards TRUE the second window becomes the first one
1438 * if upwards FALSE the first window becomes the second one
1439 */
1440 static void
1441win_rotate(upwards, count)
1442 int upwards;
1443 int count;
1444{
1445 win_T *wp1;
1446 win_T *wp2;
1447 frame_T *frp;
1448 int n;
1449
1450 if (firstwin == lastwin) /* nothing to do */
1451 {
1452 beep_flush();
1453 return;
1454 }
1455
1456#ifdef FEAT_GUI
1457 need_mouse_correct = TRUE;
1458#endif
1459
1460#ifdef FEAT_VERTSPLIT
1461 /* Check if all frames in this row/col have one window. */
1462 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1463 frp = frp->fr_next)
1464 if (frp->fr_win == NULL)
1465 {
1466 EMSG(_("E443: Cannot rotate when another window is split"));
1467 return;
1468 }
1469#endif
1470
1471 while (count--)
1472 {
1473 if (upwards) /* first window becomes last window */
1474 {
1475 /* remove first window/frame from the list */
1476 frp = curwin->w_frame->fr_parent->fr_child;
1477 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001478 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 frame_remove(frp);
1480
1481 /* find last frame and append removed window/frame after it */
1482 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1483 ;
1484 win_append(frp->fr_win, wp1);
1485 frame_append(frp, wp1->w_frame);
1486
1487 wp2 = frp->fr_win; /* previously last window */
1488 }
1489 else /* last window becomes first window */
1490 {
1491 /* find last window/frame in the list and remove it */
1492 for (frp = curwin->w_frame; frp->fr_next != NULL;
1493 frp = frp->fr_next)
1494 ;
1495 wp1 = frp->fr_win;
1496 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001497 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 frame_remove(frp);
1499
1500 /* append the removed window/frame before the first in the list */
1501 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1502 frame_insert(frp->fr_parent->fr_child, frp);
1503 }
1504
1505 /* exchange status height and vsep width of old and new last window */
1506 n = wp2->w_status_height;
1507 wp2->w_status_height = wp1->w_status_height;
1508 wp1->w_status_height = n;
1509 frame_fix_height(wp1);
1510 frame_fix_height(wp2);
1511#ifdef FEAT_VERTSPLIT
1512 n = wp2->w_vsep_width;
1513 wp2->w_vsep_width = wp1->w_vsep_width;
1514 wp1->w_vsep_width = n;
1515 frame_fix_width(wp1);
1516 frame_fix_width(wp2);
1517#endif
1518
1519 /* recompute w_winrow and w_wincol for all windows */
1520 (void)win_comp_pos();
1521 }
1522
1523 redraw_later(CLEAR);
1524}
1525
1526/*
1527 * Move the current window to the very top/bottom/left/right of the screen.
1528 */
1529 static void
1530win_totop(size, flags)
1531 int size;
1532 int flags;
1533{
1534 int dir;
1535 int height = curwin->w_height;
1536
1537 if (lastwin == firstwin)
1538 {
1539 beep_flush();
1540 return;
1541 }
1542
1543 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001544 (void)winframe_remove(curwin, &dir, NULL);
1545 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 last_status(FALSE); /* may need to remove last status line */
1547 (void)win_comp_pos(); /* recompute window positions */
1548
1549 /* Split a window on the desired side and put the window there. */
1550 (void)win_split_ins(size, flags, curwin, dir);
1551 if (!(flags & WSP_VERT))
1552 {
1553 win_setheight(height);
1554 if (p_ea)
1555 win_equal(curwin, TRUE, 'v');
1556 }
1557
1558#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1559 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1560 * scrollbars. Have to update them anyway. */
1561 if (gui.in_use)
1562 {
1563 out_flush();
1564 gui_init_which_components(NULL);
1565 gui_update_scrollbars(TRUE);
1566 }
1567 need_mouse_correct = TRUE;
1568#endif
1569
1570}
1571
1572/*
1573 * Move window "win1" to below/right of "win2" and make "win1" the current
1574 * window. Only works within the same frame!
1575 */
1576 void
1577win_move_after(win1, win2)
1578 win_T *win1, *win2;
1579{
1580 int height;
1581
1582 /* check if the arguments are reasonable */
1583 if (win1 == win2)
1584 return;
1585
1586 /* check if there is something to do */
1587 if (win2->w_next != win1)
1588 {
1589 /* may need move the status line/vertical separator of the last window
1590 * */
1591 if (win1 == lastwin)
1592 {
1593 height = win1->w_prev->w_status_height;
1594 win1->w_prev->w_status_height = win1->w_status_height;
1595 win1->w_status_height = height;
1596#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001597 if (win1->w_prev->w_vsep_width == 1)
1598 {
1599 /* Remove the vertical separator from the last-but-one window,
1600 * add it to the last window. Adjust the frame widths. */
1601 win1->w_prev->w_vsep_width = 0;
1602 win1->w_prev->w_frame->fr_width -= 1;
1603 win1->w_vsep_width = 1;
1604 win1->w_frame->fr_width += 1;
1605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606#endif
1607 }
1608 else if (win2 == lastwin)
1609 {
1610 height = win1->w_status_height;
1611 win1->w_status_height = win2->w_status_height;
1612 win2->w_status_height = height;
1613#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001614 if (win1->w_vsep_width == 1)
1615 {
1616 /* Remove the vertical separator from win1, add it to the last
1617 * window, win2. Adjust the frame widths. */
1618 win2->w_vsep_width = 1;
1619 win2->w_frame->fr_width += 1;
1620 win1->w_vsep_width = 0;
1621 win1->w_frame->fr_width -= 1;
1622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623#endif
1624 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001625 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 frame_remove(win1->w_frame);
1627 win_append(win2, win1);
1628 frame_append(win2->w_frame, win1->w_frame);
1629
1630 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1631 redraw_later(NOT_VALID);
1632 }
1633 win_enter(win1, FALSE);
1634}
1635
1636/*
1637 * Make all windows the same height.
1638 * 'next_curwin' will soon be the current window, make sure it has enough
1639 * rows.
1640 */
1641 void
1642win_equal(next_curwin, current, dir)
1643 win_T *next_curwin; /* pointer to current window to be or NULL */
1644 int current; /* do only frame with current window */
1645 int dir; /* 'v' for vertically, 'h' for horizontally,
1646 'b' for both, 0 for using p_ead */
1647{
1648 if (dir == 0)
1649#ifdef FEAT_VERTSPLIT
1650 dir = *p_ead;
1651#else
1652 dir = 'b';
1653#endif
1654 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001655 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001656 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657}
1658
1659/*
1660 * Set a frame to a new position and height, spreading the available room
1661 * equally over contained frames.
1662 * The window "next_curwin" (if not NULL) should at least get the size from
1663 * 'winheight' and 'winwidth' if possible.
1664 */
1665 static void
1666win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1667 win_T *next_curwin; /* pointer to current window to be or NULL */
1668 int current; /* do only frame with current window */
1669 frame_T *topfr; /* frame to set size off */
1670 int dir; /* 'v', 'h' or 'b', see win_equal() */
1671 int col; /* horizontal position for frame */
1672 int row; /* vertical position for frame */
1673 int width; /* new width of frame */
1674 int height; /* new height of frame */
1675{
1676 int n, m;
1677 int extra_sep = 0;
1678 int wincount, totwincount = 0;
1679 frame_T *fr;
1680 int next_curwin_size = 0;
1681 int room = 0;
1682 int new_size;
1683 int has_next_curwin = 0;
1684 int hnc;
1685
1686 if (topfr->fr_layout == FR_LEAF)
1687 {
1688 /* Set the width/height of this frame.
1689 * Redraw when size or position changes */
1690 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1691#ifdef FEAT_VERTSPLIT
1692 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1693#endif
1694 )
1695 {
1696 topfr->fr_win->w_winrow = row;
1697 frame_new_height(topfr, height, FALSE, FALSE);
1698#ifdef FEAT_VERTSPLIT
1699 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001700 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701#endif
1702 redraw_all_later(CLEAR);
1703 }
1704 }
1705#ifdef FEAT_VERTSPLIT
1706 else if (topfr->fr_layout == FR_ROW)
1707 {
1708 topfr->fr_width = width;
1709 topfr->fr_height = height;
1710
1711 if (dir != 'v') /* equalize frame widths */
1712 {
1713 /* Compute the maximum number of windows horizontally in this
1714 * frame. */
1715 n = frame_minwidth(topfr, NOWIN);
1716 /* add one for the rightmost window, it doesn't have a separator */
1717 if (col + width == Columns)
1718 extra_sep = 1;
1719 else
1720 extra_sep = 0;
1721 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001722 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001724 /*
1725 * Compute width for "next_curwin" window and room available for
1726 * other windows.
1727 * "m" is the minimal width when counting p_wiw for "next_curwin".
1728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 m = frame_minwidth(topfr, next_curwin);
1730 room = width - m;
1731 if (room < 0)
1732 {
1733 next_curwin_size = p_wiw + room;
1734 room = 0;
1735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 else
1737 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001738 next_curwin_size = -1;
1739 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1740 {
1741 /* If 'winfixwidth' set keep the window width if
1742 * possible.
1743 * Watch out for this window being the next_curwin. */
1744 if (frame_fixed_width(fr))
1745 {
1746 n = frame_minwidth(fr, NOWIN);
1747 new_size = fr->fr_width;
1748 if (frame_has_win(fr, next_curwin))
1749 {
1750 room += p_wiw - p_wmw;
1751 next_curwin_size = 0;
1752 if (new_size < p_wiw)
1753 new_size = p_wiw;
1754 }
1755 else
1756 /* These windows don't use up room. */
1757 totwincount -= (n + (fr->fr_next == NULL
1758 ? extra_sep : 0)) / (p_wmw + 1);
1759 room -= new_size - n;
1760 if (room < 0)
1761 {
1762 new_size += room;
1763 room = 0;
1764 }
1765 fr->fr_newwidth = new_size;
1766 }
1767 }
1768 if (next_curwin_size == -1)
1769 {
1770 if (!has_next_curwin)
1771 next_curwin_size = 0;
1772 else if (totwincount > 1
1773 && (room + (totwincount - 2))
1774 / (totwincount - 1) > p_wiw)
1775 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001776 /* Can make all windows wider than 'winwidth', spread
1777 * the room equally. */
1778 next_curwin_size = (room + p_wiw
1779 + (totwincount - 1) * p_wmw
1780 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001781 room -= next_curwin_size - p_wiw;
1782 }
1783 else
1784 next_curwin_size = p_wiw;
1785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001787
1788 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 --totwincount; /* don't count curwin */
1790 }
1791
1792 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1793 {
1794 n = m = 0;
1795 wincount = 1;
1796 if (fr->fr_next == NULL)
1797 /* last frame gets all that remains (avoid roundoff error) */
1798 new_size = width;
1799 else if (dir == 'v')
1800 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001801 else if (frame_fixed_width(fr))
1802 {
1803 new_size = fr->fr_newwidth;
1804 wincount = 0; /* doesn't count as a sizeable window */
1805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 else
1807 {
1808 /* Compute the maximum number of windows horiz. in "fr". */
1809 n = frame_minwidth(fr, NOWIN);
1810 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1811 / (p_wmw + 1);
1812 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001813 if (has_next_curwin)
1814 hnc = frame_has_win(fr, next_curwin);
1815 else
1816 hnc = FALSE;
1817 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001819 if (totwincount == 0)
1820 new_size = room;
1821 else
1822 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001824 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 {
1826 next_curwin_size -= p_wiw - (m - n);
1827 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001828 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001830 else
1831 room -= new_size;
1832 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
1834
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001835 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 * window, unless equalizing all frames. */
1837 if (!current || dir != 'v' || topfr->fr_parent != NULL
1838 || (new_size != fr->fr_width)
1839 || frame_has_win(fr, next_curwin))
1840 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001841 new_size, height);
1842 col += new_size;
1843 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 totwincount -= wincount;
1845 }
1846 }
1847#endif
1848 else /* topfr->fr_layout == FR_COL */
1849 {
1850#ifdef FEAT_VERTSPLIT
1851 topfr->fr_width = width;
1852#endif
1853 topfr->fr_height = height;
1854
1855 if (dir != 'h') /* equalize frame heights */
1856 {
1857 /* Compute maximum number of windows vertically in this frame. */
1858 n = frame_minheight(topfr, NOWIN);
1859 /* add one for the bottom window if it doesn't have a statusline */
1860 if (row + height == cmdline_row && p_ls == 0)
1861 extra_sep = 1;
1862 else
1863 extra_sep = 0;
1864 totwincount = (n + extra_sep) / (p_wmh + 1);
1865 has_next_curwin = frame_has_win(topfr, next_curwin);
1866
1867 /*
1868 * Compute height for "next_curwin" window and room available for
1869 * other windows.
1870 * "m" is the minimal height when counting p_wh for "next_curwin".
1871 */
1872 m = frame_minheight(topfr, next_curwin);
1873 room = height - m;
1874 if (room < 0)
1875 {
1876 /* The room is less then 'winheight', use all space for the
1877 * current window. */
1878 next_curwin_size = p_wh + room;
1879 room = 0;
1880 }
1881 else
1882 {
1883 next_curwin_size = -1;
1884 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1885 {
1886 /* If 'winfixheight' set keep the window height if
1887 * possible.
1888 * Watch out for this window being the next_curwin. */
1889 if (frame_fixed_height(fr))
1890 {
1891 n = frame_minheight(fr, NOWIN);
1892 new_size = fr->fr_height;
1893 if (frame_has_win(fr, next_curwin))
1894 {
1895 room += p_wh - p_wmh;
1896 next_curwin_size = 0;
1897 if (new_size < p_wh)
1898 new_size = p_wh;
1899 }
1900 else
1901 /* These windows don't use up room. */
1902 totwincount -= (n + (fr->fr_next == NULL
1903 ? extra_sep : 0)) / (p_wmh + 1);
1904 room -= new_size - n;
1905 if (room < 0)
1906 {
1907 new_size += room;
1908 room = 0;
1909 }
1910 fr->fr_newheight = new_size;
1911 }
1912 }
1913 if (next_curwin_size == -1)
1914 {
1915 if (!has_next_curwin)
1916 next_curwin_size = 0;
1917 else if (totwincount > 1
1918 && (room + (totwincount - 2))
1919 / (totwincount - 1) > p_wh)
1920 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001921 /* can make all windows higher than 'winheight',
1922 * spread the room equally. */
1923 next_curwin_size = (room + p_wh
1924 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 + (totwincount - 1)) / totwincount;
1926 room -= next_curwin_size - p_wh;
1927 }
1928 else
1929 next_curwin_size = p_wh;
1930 }
1931 }
1932
1933 if (has_next_curwin)
1934 --totwincount; /* don't count curwin */
1935 }
1936
1937 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1938 {
1939 n = m = 0;
1940 wincount = 1;
1941 if (fr->fr_next == NULL)
1942 /* last frame gets all that remains (avoid roundoff error) */
1943 new_size = height;
1944 else if (dir == 'h')
1945 new_size = fr->fr_height;
1946 else if (frame_fixed_height(fr))
1947 {
1948 new_size = fr->fr_newheight;
1949 wincount = 0; /* doesn't count as a sizeable window */
1950 }
1951 else
1952 {
1953 /* Compute the maximum number of windows vert. in "fr". */
1954 n = frame_minheight(fr, NOWIN);
1955 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1956 / (p_wmh + 1);
1957 m = frame_minheight(fr, next_curwin);
1958 if (has_next_curwin)
1959 hnc = frame_has_win(fr, next_curwin);
1960 else
1961 hnc = FALSE;
1962 if (hnc) /* don't count next_curwin */
1963 --wincount;
1964 if (totwincount == 0)
1965 new_size = room;
1966 else
1967 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1968 / totwincount;
1969 if (hnc) /* add next_curwin size */
1970 {
1971 next_curwin_size -= p_wh - (m - n);
1972 new_size += next_curwin_size;
1973 room -= new_size - next_curwin_size;
1974 }
1975 else
1976 room -= new_size;
1977 new_size += n;
1978 }
1979 /* Skip frame that is full width when splitting or closing a
1980 * window, unless equalizing all frames. */
1981 if (!current || dir != 'h' || topfr->fr_parent != NULL
1982 || (new_size != fr->fr_height)
1983 || frame_has_win(fr, next_curwin))
1984 win_equal_rec(next_curwin, current, fr, dir, col, row,
1985 width, new_size);
1986 row += new_size;
1987 height -= new_size;
1988 totwincount -= wincount;
1989 }
1990 }
1991}
1992
1993/*
1994 * close all windows for buffer 'buf'
1995 */
1996 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00001997close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001999 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002001 win_T *wp;
2002 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002003 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004
2005 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002006
2007 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002009 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002011 win_close(wp, FALSE);
2012
2013 /* Start all over, autocommands may change the window layout. */
2014 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 }
2016 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002017 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002019
2020 /* Also check windows in other tab pages. */
2021 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2022 {
2023 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002024 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002025 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2026 if (wp->w_buffer == buf)
2027 {
2028 win_close_othertab(wp, FALSE, tp);
2029
2030 /* Start all over, the tab page may be closed and
2031 * autocommands may change the window layout. */
2032 nexttp = first_tabpage;
2033 break;
2034 }
2035 }
2036
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002038
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002039 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002040 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041}
2042
2043/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002044 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002045 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002046 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002047 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002048last_window()
2049{
2050 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
2051}
2052
2053/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002054 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 * If "free_buf" is TRUE related buffer may be unloaded.
2056 *
2057 * called by :quit, :close, :xit, :wq and findtag()
2058 */
2059 void
2060win_close(win, free_buf)
2061 win_T *win;
2062 int free_buf;
2063{
2064 win_T *wp;
2065#ifdef FEAT_AUTOCMD
2066 int other_buffer = FALSE;
2067#endif
2068 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 int dir;
2070 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002071 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002073 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 {
2075 EMSG(_("E444: Cannot close last window"));
2076 return;
2077 }
2078
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002079 /*
2080 * When closing the last window in a tab page first go to another tab
2081 * page and then close the window and the tab page. This avoids that
2082 * curwin and curtab are not invalid while we are freeing memory, they may
2083 * be used in GUI events.
2084 */
2085 if (firstwin == lastwin)
2086 {
2087 goto_tabpage_tp(alt_tabpage());
2088 redraw_tabline = TRUE;
2089
2090 /* Safety check: Autocommands may have closed the window when jumping
2091 * to the other tab page. */
2092 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2093 {
2094 int h = tabline_height();
2095
2096 win_close_othertab(win, free_buf, prev_curtab);
2097 if (h != tabline_height())
2098 shell_new_rows();
2099 }
2100 return;
2101 }
2102
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 /* When closing the help window, try restoring a snapshot after closing
2104 * the window. Otherwise clear the snapshot, it's now invalid. */
2105 if (win->w_buffer->b_help)
2106 help_window = TRUE;
2107 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002108 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109
2110#ifdef FEAT_AUTOCMD
2111 if (win == curwin)
2112 {
2113 /*
2114 * Guess which window is going to be the new current window.
2115 * This may change because of the autocommands (sigh).
2116 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002117 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
2119 /*
2120 * Be careful: If autocommands delete the window, return now.
2121 */
2122 if (wp->w_buffer != curbuf)
2123 {
2124 other_buffer = TRUE;
2125 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002126 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 return;
2128 }
2129 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002130 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 return;
2132# ifdef FEAT_EVAL
2133 /* autocmds may abort script processing */
2134 if (aborting())
2135 return;
2136# endif
2137 }
2138#endif
2139
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002140#ifdef FEAT_GUI
2141 /* Avoid trouble with scrollbars that are going to be deleted in
2142 * win_free(). */
2143 if (gui.in_use)
2144 out_flush();
2145#endif
2146
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 /*
2148 * Close the link to the buffer.
2149 */
2150 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002151
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002153 * other window or moved to another tab page. */
2154 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 return;
2156
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002157 /* Free the memory used for the window. */
2158 wp = win_free_mem(win, &dir, NULL);
2159
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 /* Make sure curwin isn't invalid. It can cause severe trouble when
2161 * printing an error message. For win_equal() curbuf needs to be valid
2162 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002163 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 {
2165 curwin = wp;
2166#ifdef FEAT_QUICKFIX
2167 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2168 {
2169 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002170 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 * finding another window to go to.
2172 */
2173 for (;;)
2174 {
2175 if (wp->w_next == NULL)
2176 wp = firstwin;
2177 else
2178 wp = wp->w_next;
2179 if (wp == curwin)
2180 break;
2181 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2182 {
2183 curwin = wp;
2184 break;
2185 }
2186 }
2187 }
2188#endif
2189 curbuf = curwin->w_buffer;
2190 close_curwin = TRUE;
2191 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002192 if (p_ea
2193#ifdef FEAT_VERTSPLIT
2194 && (*p_ead == 'b' || *p_ead == dir)
2195#endif
2196 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 win_equal(curwin, TRUE,
2198#ifdef FEAT_VERTSPLIT
2199 dir
2200#else
2201 0
2202#endif
2203 );
2204 else
2205 win_comp_pos();
2206 if (close_curwin)
2207 {
2208 win_enter_ext(wp, FALSE, TRUE);
2209#ifdef FEAT_AUTOCMD
2210 if (other_buffer)
2211 /* careful: after this wp and win may be invalid! */
2212 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2213#endif
2214 }
2215
2216 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002217 * If last window has a status line now and we don't want one,
2218 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 */
2220 last_status(FALSE);
2221
2222 /* After closing the help window, try restoring the window layout from
2223 * before it was opened. */
2224 if (help_window)
2225 restore_snapshot(close_curwin);
2226
2227#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2228 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2229 if (gui.in_use && !win_hasvertsplit())
2230 gui_init_which_components(NULL);
2231#endif
2232
2233 redraw_all_later(NOT_VALID);
2234}
2235
2236/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002237 * Close window "win" in tab page "tp", which is not the current tab page.
2238 * This may be the last window ih that tab page and result in closing the tab,
2239 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002240 * Caller must check if buffer is hidden and whether the tabline needs to be
2241 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002242 */
2243 void
2244win_close_othertab(win, free_buf, tp)
2245 win_T *win;
2246 int free_buf;
2247 tabpage_T *tp;
2248{
2249 win_T *wp;
2250 int dir;
2251 tabpage_T *ptp = NULL;
2252
2253 /* Close the link to the buffer. */
2254 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2255
2256 /* Careful: Autocommands may have closed the tab page or made it the
2257 * current tab page. */
2258 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2259 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002260 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002261 return;
2262
2263 /* Autocommands may have closed the window already. */
2264 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2265 ;
2266 if (wp == NULL)
2267 return;
2268
2269 /* Free the memory used for the window. */
2270 wp = win_free_mem(win, &dir, tp);
2271
2272 /* When closing the last window in a tab page remove the tab page. */
2273 if (wp == NULL)
2274 {
2275 if (tp == first_tabpage)
2276 first_tabpage = tp->tp_next;
2277 else
2278 {
2279 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2280 ptp = ptp->tp_next)
2281 ;
2282 if (ptp == NULL)
2283 {
2284 EMSG2(_(e_intern2), "win_close_othertab()");
2285 return;
2286 }
2287 ptp->tp_next = tp->tp_next;
2288 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002289 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002290 }
2291}
2292
2293/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002294 * Free the memory used for a window.
2295 * Returns a pointer to the window that got the freed up space.
2296 */
2297 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002298win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002299 win_T *win;
2300 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002301 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002302{
2303 frame_T *frp;
2304 win_T *wp;
2305
Bram Moolenaarea408852005-06-25 22:49:46 +00002306#ifdef FEAT_FOLDING
2307 clearFolding(win);
2308#endif
2309
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002310 /* reduce the reference count to the argument list. */
2311 alist_unlink(win->w_alist);
2312
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002313 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002314 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002315 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002316 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002317 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002318
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002319 /* When deleting the current window of another tab page select a new
2320 * current window. */
2321 if (tp != NULL && win == tp->tp_curwin)
2322 tp->tp_curwin = wp;
2323
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002324 return wp;
2325}
2326
2327#if defined(EXITFREE) || defined(PROTO)
2328 void
2329win_free_all()
2330{
2331 int dummy;
2332
Bram Moolenaarf740b292006-02-16 22:11:02 +00002333# ifdef FEAT_WINDOWS
2334 while (first_tabpage->tp_next != NULL)
2335 tabpage_close(TRUE);
2336# endif
2337
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002338 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002339 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002340}
2341#endif
2342
2343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 * Remove a window and its frame from the tree of frames.
2345 * Returns a pointer to the window that got the freed up space.
2346 */
2347/*ARGSUSED*/
2348 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002349winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 win_T *win;
2351 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002352 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353{
2354 frame_T *frp, *frp2, *frp3;
2355 frame_T *frp_close = win->w_frame;
2356 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002359 * If there is only one window there is nothing to remove.
2360 */
2361 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2362 return NULL;
2363
2364 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 * Remove the window from its frame.
2366 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002367 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 wp = frame2win(frp2);
2369
2370 /* Remove this frame from the list of frames. */
2371 frame_remove(frp_close);
2372
2373#ifdef FEAT_VERTSPLIT
2374 if (frp_close->fr_parent->fr_layout == FR_COL)
2375 {
2376#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002377 /* When 'winfixheight' is set, try to find another frame in the column
2378 * (as close to the closed frame as possible) to distribute the height
2379 * to. */
2380 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2381 {
2382 frp = frp_close->fr_prev;
2383 frp3 = frp_close->fr_next;
2384 while (frp != NULL || frp3 != NULL)
2385 {
2386 if (frp != NULL)
2387 {
2388 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2389 {
2390 frp2 = frp;
2391 wp = frp->fr_win;
2392 break;
2393 }
2394 frp = frp->fr_prev;
2395 }
2396 if (frp3 != NULL)
2397 {
2398 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2399 {
2400 frp2 = frp3;
2401 wp = frp3->fr_win;
2402 break;
2403 }
2404 frp3 = frp3->fr_next;
2405 }
2406 }
2407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2409 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410#ifdef FEAT_VERTSPLIT
2411 *dirp = 'v';
2412 }
2413 else
2414 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002415 /* When 'winfixwidth' is set, try to find another frame in the column
2416 * (as close to the closed frame as possible) to distribute the width
2417 * to. */
2418 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2419 {
2420 frp = frp_close->fr_prev;
2421 frp3 = frp_close->fr_next;
2422 while (frp != NULL || frp3 != NULL)
2423 {
2424 if (frp != NULL)
2425 {
2426 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2427 {
2428 frp2 = frp;
2429 wp = frp->fr_win;
2430 break;
2431 }
2432 frp = frp->fr_prev;
2433 }
2434 if (frp3 != NULL)
2435 {
2436 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2437 {
2438 frp2 = frp3;
2439 wp = frp3->fr_win;
2440 break;
2441 }
2442 frp3 = frp3->fr_next;
2443 }
2444 }
2445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002447 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 *dirp = 'h';
2449 }
2450#endif
2451
2452 /* If rows/columns go to a window below/right its positions need to be
2453 * updated. Can only be done after the sizes have been updated. */
2454 if (frp2 == frp_close->fr_next)
2455 {
2456 int row = win->w_winrow;
2457 int col = W_WINCOL(win);
2458
2459 frame_comp_pos(frp2, &row, &col);
2460 }
2461
2462 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2463 {
2464 /* There is no other frame in this list, move its info to the parent
2465 * and remove it. */
2466 frp2->fr_parent->fr_layout = frp2->fr_layout;
2467 frp2->fr_parent->fr_child = frp2->fr_child;
2468 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2469 frp->fr_parent = frp2->fr_parent;
2470 frp2->fr_parent->fr_win = frp2->fr_win;
2471 if (frp2->fr_win != NULL)
2472 frp2->fr_win->w_frame = frp2->fr_parent;
2473 frp = frp2->fr_parent;
2474 vim_free(frp2);
2475
2476 frp2 = frp->fr_parent;
2477 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2478 {
2479 /* The frame above the parent has the same layout, have to merge
2480 * the frames into this list. */
2481 if (frp2->fr_child == frp)
2482 frp2->fr_child = frp->fr_child;
2483 frp->fr_child->fr_prev = frp->fr_prev;
2484 if (frp->fr_prev != NULL)
2485 frp->fr_prev->fr_next = frp->fr_child;
2486 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2487 {
2488 frp3->fr_parent = frp2;
2489 if (frp3->fr_next == NULL)
2490 {
2491 frp3->fr_next = frp->fr_next;
2492 if (frp->fr_next != NULL)
2493 frp->fr_next->fr_prev = frp3;
2494 break;
2495 }
2496 }
2497 vim_free(frp);
2498 }
2499 }
2500
2501 return wp;
2502}
2503
2504/*
2505 * Find out which frame is going to get the freed up space when "win" is
2506 * closed.
2507 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2508 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2509 * This makes opening a window and closing it immediately keep the same window
2510 * layout.
2511 */
2512 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002513win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002515 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516{
2517 frame_T *frp;
2518 int b;
2519
Bram Moolenaarf740b292006-02-16 22:11:02 +00002520 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002521 /* Last window in this tab page, will go to next tab page. */
2522 return alt_tabpage()->tp_curwin->w_frame;
2523
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 frp = win->w_frame;
2525#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002526 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 b = p_spr;
2528 else
2529#endif
2530 b = p_sb;
2531 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2532 return frp->fr_next;
2533 return frp->fr_prev;
2534}
2535
2536/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002537 * Return the tabpage that will be used if the current one is closed.
2538 */
2539 static tabpage_T *
2540alt_tabpage()
2541{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002542 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002543
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002544 /* Use the next tab page if possible. */
2545 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002546 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002547
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002548 /* Find the last but one tab page. */
2549 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2550 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002551 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002552}
2553
2554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 * Find the left-upper window in frame "frp".
2556 */
2557 static win_T *
2558frame2win(frp)
2559 frame_T *frp;
2560{
2561 while (frp->fr_win == NULL)
2562 frp = frp->fr_child;
2563 return frp->fr_win;
2564}
2565
2566/*
2567 * Return TRUE if frame "frp" contains window "wp".
2568 */
2569 static int
2570frame_has_win(frp, wp)
2571 frame_T *frp;
2572 win_T *wp;
2573{
2574 frame_T *p;
2575
2576 if (frp->fr_layout == FR_LEAF)
2577 return frp->fr_win == wp;
2578
2579 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2580 if (frame_has_win(p, wp))
2581 return TRUE;
2582 return FALSE;
2583}
2584
2585/*
2586 * Set a new height for a frame. Recursively sets the height for contained
2587 * frames and windows. Caller must take care of positions.
2588 */
2589 static void
2590frame_new_height(topfrp, height, topfirst, wfh)
2591 frame_T *topfrp;
2592 int height;
2593 int topfirst; /* resize topmost contained frame first */
2594 int wfh; /* obey 'winfixheight' when there is a choice;
2595 may cause the height not to be set */
2596{
2597 frame_T *frp;
2598 int extra_lines;
2599 int h;
2600
2601 if (topfrp->fr_win != NULL)
2602 {
2603 /* Simple case: just one window. */
2604 win_new_height(topfrp->fr_win,
2605 height - topfrp->fr_win->w_status_height);
2606 }
2607#ifdef FEAT_VERTSPLIT
2608 else if (topfrp->fr_layout == FR_ROW)
2609 {
2610 do
2611 {
2612 /* All frames in this row get the same new height. */
2613 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2614 {
2615 frame_new_height(frp, height, topfirst, wfh);
2616 if (frp->fr_height > height)
2617 {
2618 /* Could not fit the windows, make the whole row higher. */
2619 height = frp->fr_height;
2620 break;
2621 }
2622 }
2623 }
2624 while (frp != NULL);
2625 }
2626#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002627 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 {
2629 /* Complicated case: Resize a column of frames. Resize the bottom
2630 * frame first, frames above that when needed. */
2631
2632 frp = topfrp->fr_child;
2633 if (wfh)
2634 /* Advance past frames with one window with 'wfh' set. */
2635 while (frame_fixed_height(frp))
2636 {
2637 frp = frp->fr_next;
2638 if (frp == NULL)
2639 return; /* no frame without 'wfh', give up */
2640 }
2641 if (!topfirst)
2642 {
2643 /* Find the bottom frame of this column */
2644 while (frp->fr_next != NULL)
2645 frp = frp->fr_next;
2646 if (wfh)
2647 /* Advance back for frames with one window with 'wfh' set. */
2648 while (frame_fixed_height(frp))
2649 frp = frp->fr_prev;
2650 }
2651
2652 extra_lines = height - topfrp->fr_height;
2653 if (extra_lines < 0)
2654 {
2655 /* reduce height of contained frames, bottom or top frame first */
2656 while (frp != NULL)
2657 {
2658 h = frame_minheight(frp, NULL);
2659 if (frp->fr_height + extra_lines < h)
2660 {
2661 extra_lines += frp->fr_height - h;
2662 frame_new_height(frp, h, topfirst, wfh);
2663 }
2664 else
2665 {
2666 frame_new_height(frp, frp->fr_height + extra_lines,
2667 topfirst, wfh);
2668 break;
2669 }
2670 if (topfirst)
2671 {
2672 do
2673 frp = frp->fr_next;
2674 while (wfh && frp != NULL && frame_fixed_height(frp));
2675 }
2676 else
2677 {
2678 do
2679 frp = frp->fr_prev;
2680 while (wfh && frp != NULL && frame_fixed_height(frp));
2681 }
2682 /* Increase "height" if we could not reduce enough frames. */
2683 if (frp == NULL)
2684 height -= extra_lines;
2685 }
2686 }
2687 else if (extra_lines > 0)
2688 {
2689 /* increase height of bottom or top frame */
2690 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2691 }
2692 }
2693 topfrp->fr_height = height;
2694}
2695
2696/*
2697 * Return TRUE if height of frame "frp" should not be changed because of
2698 * the 'winfixheight' option.
2699 */
2700 static int
2701frame_fixed_height(frp)
2702 frame_T *frp;
2703{
2704 /* frame with one window: fixed height if 'winfixheight' set. */
2705 if (frp->fr_win != NULL)
2706 return frp->fr_win->w_p_wfh;
2707
2708 if (frp->fr_layout == FR_ROW)
2709 {
2710 /* The frame is fixed height if one of the frames in the row is fixed
2711 * height. */
2712 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2713 if (frame_fixed_height(frp))
2714 return TRUE;
2715 return FALSE;
2716 }
2717
2718 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2719 * frames in the row are fixed height. */
2720 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2721 if (!frame_fixed_height(frp))
2722 return FALSE;
2723 return TRUE;
2724}
2725
2726#ifdef FEAT_VERTSPLIT
2727/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002728 * Return TRUE if width of frame "frp" should not be changed because of
2729 * the 'winfixwidth' option.
2730 */
2731 static int
2732frame_fixed_width(frp)
2733 frame_T *frp;
2734{
2735 /* frame with one window: fixed width if 'winfixwidth' set. */
2736 if (frp->fr_win != NULL)
2737 return frp->fr_win->w_p_wfw;
2738
2739 if (frp->fr_layout == FR_COL)
2740 {
2741 /* The frame is fixed width if one of the frames in the row is fixed
2742 * width. */
2743 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2744 if (frame_fixed_width(frp))
2745 return TRUE;
2746 return FALSE;
2747 }
2748
2749 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2750 * frames in the row are fixed width. */
2751 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2752 if (!frame_fixed_width(frp))
2753 return FALSE;
2754 return TRUE;
2755}
2756
2757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 * Add a status line to windows at the bottom of "frp".
2759 * Note: Does not check if there is room!
2760 */
2761 static void
2762frame_add_statusline(frp)
2763 frame_T *frp;
2764{
2765 win_T *wp;
2766
2767 if (frp->fr_layout == FR_LEAF)
2768 {
2769 wp = frp->fr_win;
2770 if (wp->w_status_height == 0)
2771 {
2772 if (wp->w_height > 0) /* don't make it negative */
2773 --wp->w_height;
2774 wp->w_status_height = STATUS_HEIGHT;
2775 }
2776 }
2777 else if (frp->fr_layout == FR_ROW)
2778 {
2779 /* Handle all the frames in the row. */
2780 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2781 frame_add_statusline(frp);
2782 }
2783 else /* frp->fr_layout == FR_COL */
2784 {
2785 /* Only need to handle the last frame in the column. */
2786 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2787 ;
2788 frame_add_statusline(frp);
2789 }
2790}
2791
2792/*
2793 * Set width of a frame. Handles recursively going through contained frames.
2794 * May remove separator line for windows at the right side (for win_close()).
2795 */
2796 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002797frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 frame_T *topfrp;
2799 int width;
2800 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002801 int wfw; /* obey 'winfixwidth' when there is a choice;
2802 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803{
2804 frame_T *frp;
2805 int extra_cols;
2806 int w;
2807 win_T *wp;
2808
2809 if (topfrp->fr_layout == FR_LEAF)
2810 {
2811 /* Simple case: just one window. */
2812 wp = topfrp->fr_win;
2813 /* Find out if there are any windows right of this one. */
2814 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2815 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2816 break;
2817 if (frp->fr_parent == NULL)
2818 wp->w_vsep_width = 0;
2819 win_new_width(wp, width - wp->w_vsep_width);
2820 }
2821 else if (topfrp->fr_layout == FR_COL)
2822 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002823 do
2824 {
2825 /* All frames in this column get the same new width. */
2826 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2827 {
2828 frame_new_width(frp, width, leftfirst, wfw);
2829 if (frp->fr_width > width)
2830 {
2831 /* Could not fit the windows, make whole column wider. */
2832 width = frp->fr_width;
2833 break;
2834 }
2835 }
2836 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 }
2838 else /* fr_layout == FR_ROW */
2839 {
2840 /* Complicated case: Resize a row of frames. Resize the rightmost
2841 * frame first, frames left of it when needed. */
2842
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002844 if (wfw)
2845 /* Advance past frames with one window with 'wfw' set. */
2846 while (frame_fixed_width(frp))
2847 {
2848 frp = frp->fr_next;
2849 if (frp == NULL)
2850 return; /* no frame without 'wfw', give up */
2851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002853 {
2854 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 while (frp->fr_next != NULL)
2856 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002857 if (wfw)
2858 /* Advance back for frames with one window with 'wfw' set. */
2859 while (frame_fixed_width(frp))
2860 frp = frp->fr_prev;
2861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862
2863 extra_cols = width - topfrp->fr_width;
2864 if (extra_cols < 0)
2865 {
2866 /* reduce frame width, rightmost frame first */
2867 while (frp != NULL)
2868 {
2869 w = frame_minwidth(frp, NULL);
2870 if (frp->fr_width + extra_cols < w)
2871 {
2872 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002873 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
2875 else
2876 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002877 frame_new_width(frp, frp->fr_width + extra_cols,
2878 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 break;
2880 }
2881 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002882 {
2883 do
2884 frp = frp->fr_next;
2885 while (wfw && frp != NULL && frame_fixed_width(frp));
2886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002888 {
2889 do
2890 frp = frp->fr_prev;
2891 while (wfw && frp != NULL && frame_fixed_width(frp));
2892 }
2893 /* Increase "width" if we could not reduce enough frames. */
2894 if (frp == NULL)
2895 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 }
2897 }
2898 else if (extra_cols > 0)
2899 {
2900 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002901 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 }
2903 }
2904 topfrp->fr_width = width;
2905}
2906
2907/*
2908 * Add the vertical separator to windows at the right side of "frp".
2909 * Note: Does not check if there is room!
2910 */
2911 static void
2912frame_add_vsep(frp)
2913 frame_T *frp;
2914{
2915 win_T *wp;
2916
2917 if (frp->fr_layout == FR_LEAF)
2918 {
2919 wp = frp->fr_win;
2920 if (wp->w_vsep_width == 0)
2921 {
2922 if (wp->w_width > 0) /* don't make it negative */
2923 --wp->w_width;
2924 wp->w_vsep_width = 1;
2925 }
2926 }
2927 else if (frp->fr_layout == FR_COL)
2928 {
2929 /* Handle all the frames in the column. */
2930 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2931 frame_add_vsep(frp);
2932 }
2933 else /* frp->fr_layout == FR_ROW */
2934 {
2935 /* Only need to handle the last frame in the row. */
2936 frp = frp->fr_child;
2937 while (frp->fr_next != NULL)
2938 frp = frp->fr_next;
2939 frame_add_vsep(frp);
2940 }
2941}
2942
2943/*
2944 * Set frame width from the window it contains.
2945 */
2946 static void
2947frame_fix_width(wp)
2948 win_T *wp;
2949{
2950 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2951}
2952#endif
2953
2954/*
2955 * Set frame height from the window it contains.
2956 */
2957 static void
2958frame_fix_height(wp)
2959 win_T *wp;
2960{
2961 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2962}
2963
2964/*
2965 * Compute the minimal height for frame "topfrp".
2966 * Uses the 'winminheight' option.
2967 * When "next_curwin" isn't NULL, use p_wh for this window.
2968 * When "next_curwin" is NOWIN, don't use at least one line for the current
2969 * window.
2970 */
2971 static int
2972frame_minheight(topfrp, next_curwin)
2973 frame_T *topfrp;
2974 win_T *next_curwin;
2975{
2976 frame_T *frp;
2977 int m;
2978#ifdef FEAT_VERTSPLIT
2979 int n;
2980#endif
2981
2982 if (topfrp->fr_win != NULL)
2983 {
2984 if (topfrp->fr_win == next_curwin)
2985 m = p_wh + topfrp->fr_win->w_status_height;
2986 else
2987 {
2988 /* window: minimal height of the window plus status line */
2989 m = p_wmh + topfrp->fr_win->w_status_height;
2990 /* Current window is minimal one line high */
2991 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2992 ++m;
2993 }
2994 }
2995#ifdef FEAT_VERTSPLIT
2996 else if (topfrp->fr_layout == FR_ROW)
2997 {
2998 /* get the minimal height from each frame in this row */
2999 m = 0;
3000 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3001 {
3002 n = frame_minheight(frp, next_curwin);
3003 if (n > m)
3004 m = n;
3005 }
3006 }
3007#endif
3008 else
3009 {
3010 /* Add up the minimal heights for all frames in this column. */
3011 m = 0;
3012 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3013 m += frame_minheight(frp, next_curwin);
3014 }
3015
3016 return m;
3017}
3018
3019#ifdef FEAT_VERTSPLIT
3020/*
3021 * Compute the minimal width for frame "topfrp".
3022 * When "next_curwin" isn't NULL, use p_wiw for this window.
3023 * When "next_curwin" is NOWIN, don't use at least one column for the current
3024 * window.
3025 */
3026 static int
3027frame_minwidth(topfrp, next_curwin)
3028 frame_T *topfrp;
3029 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3030{
3031 frame_T *frp;
3032 int m, n;
3033
3034 if (topfrp->fr_win != NULL)
3035 {
3036 if (topfrp->fr_win == next_curwin)
3037 m = p_wiw + topfrp->fr_win->w_vsep_width;
3038 else
3039 {
3040 /* window: minimal width of the window plus separator column */
3041 m = p_wmw + topfrp->fr_win->w_vsep_width;
3042 /* Current window is minimal one column wide */
3043 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3044 ++m;
3045 }
3046 }
3047 else if (topfrp->fr_layout == FR_COL)
3048 {
3049 /* get the minimal width from each frame in this column */
3050 m = 0;
3051 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3052 {
3053 n = frame_minwidth(frp, next_curwin);
3054 if (n > m)
3055 m = n;
3056 }
3057 }
3058 else
3059 {
3060 /* Add up the minimal widths for all frames in this row. */
3061 m = 0;
3062 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3063 m += frame_minwidth(frp, next_curwin);
3064 }
3065
3066 return m;
3067}
3068#endif
3069
3070
3071/*
3072 * Try to close all windows except current one.
3073 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3074 * used and the buffer was modified.
3075 *
3076 * Used by ":bdel" and ":only".
3077 */
3078 void
3079close_others(message, forceit)
3080 int message;
3081 int forceit; /* always hide all other windows */
3082{
3083 win_T *wp;
3084 win_T *nextwp;
3085 int r;
3086
3087 if (lastwin == firstwin)
3088 {
3089 if (message
3090#ifdef FEAT_AUTOCMD
3091 && !autocmd_busy
3092#endif
3093 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003094 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 return;
3096 }
3097
3098 /* Be very careful here: autocommands may change the window layout. */
3099 for (wp = firstwin; win_valid(wp); wp = nextwp)
3100 {
3101 nextwp = wp->w_next;
3102 if (wp != curwin) /* don't close current window */
3103 {
3104
3105 /* Check if it's allowed to abandon this window */
3106 r = can_abandon(wp->w_buffer, forceit);
3107#ifdef FEAT_AUTOCMD
3108 if (!win_valid(wp)) /* autocommands messed wp up */
3109 {
3110 nextwp = firstwin;
3111 continue;
3112 }
3113#endif
3114 if (!r)
3115 {
3116#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3117 if (message && (p_confirm || cmdmod.confirm) && p_write)
3118 {
3119 dialog_changed(wp->w_buffer, FALSE);
3120# ifdef FEAT_AUTOCMD
3121 if (!win_valid(wp)) /* autocommands messed wp up */
3122 {
3123 nextwp = firstwin;
3124 continue;
3125 }
3126# endif
3127 }
3128 if (bufIsChanged(wp->w_buffer))
3129#endif
3130 continue;
3131 }
3132 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3133 }
3134 }
3135
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003136 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 EMSG(_("E445: Other window contains changes"));
3138}
3139
3140#endif /* FEAT_WINDOWS */
3141
3142/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003143 * Init the current window "curwin".
3144 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 */
3146 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003147curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003149 redraw_win_later(curwin, NOT_VALID);
3150 curwin->w_lines_valid = 0;
3151 curwin->w_cursor.lnum = 1;
3152 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003154 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003156 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3157 curwin->w_pcmark.col = 0;
3158 curwin->w_prev_pcmark.lnum = 0;
3159 curwin->w_prev_pcmark.col = 0;
3160 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003162 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003164 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165#ifdef FEAT_FKMAP
3166 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003167 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003169 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170#endif
3171}
3172
3173/*
3174 * Allocate the first window and put an empty buffer in it.
3175 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003176 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003178 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179win_alloc_first()
3180{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003181 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003182 return FAIL;
3183
3184#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003185 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003186 if (first_tabpage == NULL)
3187 return FAIL;
3188 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003189 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003190#endif
3191 return OK;
3192}
3193
3194/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003195 * Allocate the first window or the first window in a new tab page.
3196 * When "oldwin" is NULL create an empty buffer for it.
3197 * When "oldwin" is not NULL copy info from it to the new window (only with
3198 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003199 * Return FAIL when something goes wrong (out of memory).
3200 */
3201 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003202win_alloc_firstwin(oldwin)
3203 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003204{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003206 if (oldwin == NULL)
3207 {
3208 /* Very first window, need to create an empty buffer for it and
3209 * initialize from scratch. */
3210 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3211 if (curwin == NULL || curbuf == NULL)
3212 return FAIL;
3213 curwin->w_buffer = curbuf;
3214 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003216 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003218 curwin_init(); /* init current window */
3219 }
3220#ifdef FEAT_WINDOWS
3221 else
3222 {
3223 /* First window in new tab page, initialize it from "oldwin". */
3224 win_init(curwin, oldwin);
3225
3226# ifdef FEAT_SCROLLBIND
3227 /* We don't want scroll-binding in the first window. */
3228 curwin->w_p_scb = FALSE;
3229# endif
3230 }
3231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232
3233 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3234 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003235 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 topframe->fr_layout = FR_LEAF;
3237#ifdef FEAT_VERTSPLIT
3238 topframe->fr_width = Columns;
3239#endif
3240 topframe->fr_height = Rows - p_ch;
3241 topframe->fr_win = curwin;
3242 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003243
3244 return OK;
3245}
3246
3247/*
3248 * Initialize the window and frame size to the maximum.
3249 */
3250 void
3251win_init_size()
3252{
3253 firstwin->w_height = ROWS_AVAIL;
3254 topframe->fr_height = ROWS_AVAIL;
3255#ifdef FEAT_VERTSPLIT
3256 firstwin->w_width = Columns;
3257 topframe->fr_width = Columns;
3258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259}
3260
3261#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003262
3263/*
3264 * Allocate a new tabpage_T and init the values.
3265 * Returns NULL when out of memory.
3266 */
3267 static tabpage_T *
3268alloc_tabpage()
3269{
3270 tabpage_T *tp;
3271
3272 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3273 if (tp != NULL)
3274 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003275# ifdef FEAT_GUI
3276 int i;
3277
3278 for (i = 0; i < 3; i++)
3279 tp->tp_prev_which_scrollbars[i] = -1;
3280# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003281# ifdef FEAT_DIFF
3282 tp->tp_diff_invalid = TRUE;
3283# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003284#ifdef FEAT_EVAL
3285 /* init t: variables */
3286 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3287#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003288 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003289 }
3290 return tp;
3291}
3292
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003293 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003294free_tabpage(tp)
3295 tabpage_T *tp;
3296{
3297# ifdef FEAT_DIFF
3298 diff_clear(tp);
3299# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003300 clear_snapshot(tp);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003301#ifdef FEAT_EVAL
3302 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3303#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003304 vim_free(tp);
3305}
3306
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003307/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003308 * Create a new Tab page with one window.
3309 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003310 * When "after" is 0 put it just after the current Tab page.
3311 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003312 * Return FAIL or OK.
3313 */
3314 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003315win_new_tabpage(after)
3316 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003317{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003318 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003319 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003320 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003321
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003322 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003323 if (newtp == NULL)
3324 return FAIL;
3325
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003326 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003327 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003328 {
3329 vim_free(newtp);
3330 return FAIL;
3331 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003332 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003333
3334 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003335 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003336 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003337 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003338 if (after == 1)
3339 {
3340 /* New tab page becomes the first one. */
3341 newtp->tp_next = first_tabpage;
3342 first_tabpage = newtp;
3343 }
3344 else
3345 {
3346 if (after > 0)
3347 {
3348 /* Put new tab page before tab page "after". */
3349 n = 2;
3350 for (tp = first_tabpage; tp->tp_next != NULL
3351 && n < after; tp = tp->tp_next)
3352 ++n;
3353 }
3354 newtp->tp_next = tp->tp_next;
3355 tp->tp_next = newtp;
3356 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003357 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003358 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003359 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003360
3361 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003362 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003363
3364#if defined(FEAT_GUI)
3365 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3366 * scrollbars. Have to update them anyway. */
3367 if (gui.in_use && starting == 0)
3368 {
3369 gui_init_which_components(NULL);
3370 gui_update_scrollbars(TRUE);
3371 }
3372 need_mouse_correct = TRUE;
3373#endif
3374
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003375 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003376#ifdef FEAT_AUTOCMD
3377 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3378 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3379#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003380 return OK;
3381 }
3382
3383 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003384 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003385 return FAIL;
3386}
3387
3388/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003389 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3390 * like with ":split".
3391 * Returns OK if a new tab page was created, FAIL otherwise.
3392 */
3393 int
3394may_open_tabpage()
3395{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003396 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003397
Bram Moolenaard326ce82007-03-11 14:48:29 +00003398 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003399 {
3400 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003401 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003402 return win_new_tabpage(n);
3403 }
3404 return FAIL;
3405}
3406
3407/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003408 * Create up to "maxcount" tabpages with empty windows.
3409 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003410 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003411 int
3412make_tabpages(maxcount)
3413 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003414{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003415 int count = maxcount;
3416 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003417
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003418 /* Limit to 'tabpagemax' tabs. */
3419 if (count > p_tpm)
3420 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003421
3422#ifdef FEAT_AUTOCMD
3423 /*
3424 * Don't execute autocommands while creating the tab pages. Must do that
3425 * when putting the buffers in the windows.
3426 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003427 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003428#endif
3429
3430 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003431 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003432 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003433
3434#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003435 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003436#endif
3437
3438 /* return actual number of tab pages */
3439 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003440}
3441
3442/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003443 * Return TRUE when "tpc" points to a valid tab page.
3444 */
3445 int
3446valid_tabpage(tpc)
3447 tabpage_T *tpc;
3448{
3449 tabpage_T *tp;
3450
3451 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3452 if (tp == tpc)
3453 return TRUE;
3454 return FALSE;
3455}
3456
3457/*
3458 * Find tab page "n" (first one is 1). Returns NULL when not found.
3459 */
3460 tabpage_T *
3461find_tabpage(n)
3462 int n;
3463{
3464 tabpage_T *tp;
3465 int i = 1;
3466
3467 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3468 ++i;
3469 return tp;
3470}
3471
3472/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003473 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003474 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003475 */
3476 int
3477tabpage_index(ftp)
3478 tabpage_T *ftp;
3479{
3480 int i = 1;
3481 tabpage_T *tp;
3482
3483 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3484 ++i;
3485 return i;
3486}
3487
3488/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003489 * Prepare for leaving the current tab page.
3490 * When autocomands change "curtab" we don't leave the tab page and return
3491 * FAIL.
3492 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003493 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003494/*ARGSUSED*/
3495 static int
3496leave_tabpage(new_curbuf)
3497 buf_T *new_curbuf; /* what is going to be the new curbuf,
3498 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003499{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003500 tabpage_T *tp = curtab;
3501
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003502#ifdef FEAT_VISUAL
3503 reset_VIsual_and_resel(); /* stop Visual mode */
3504#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003505#ifdef FEAT_AUTOCMD
3506 if (new_curbuf != curbuf)
3507 {
3508 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3509 if (curtab != tp)
3510 return FAIL;
3511 }
3512 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3513 if (curtab != tp)
3514 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003515 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003516 if (curtab != tp)
3517 return FAIL;
3518#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003519#if defined(FEAT_GUI)
3520 /* Remove the scrollbars. They may be added back later. */
3521 if (gui.in_use)
3522 gui_remove_scrollbars();
3523#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003524 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003525 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003526 tp->tp_firstwin = firstwin;
3527 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003528 tp->tp_old_Rows = Rows;
3529 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003530 firstwin = NULL;
3531 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003532 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003533}
3534
3535/*
3536 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003537 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003538 */
3539/*ARGSUSED*/
3540 static void
3541enter_tabpage(tp, old_curbuf)
3542 tabpage_T *tp;
3543 buf_T *old_curbuf;
3544{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003545 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003546 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003547
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003548 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003549 firstwin = tp->tp_firstwin;
3550 lastwin = tp->tp_lastwin;
3551 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003552
3553 /* We would like doing the TabEnter event first, but we don't have a
3554 * valid current window yet, which may break some commands.
3555 * This triggers autocommands, thus may make "tp" invalid. */
3556 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3557 prevwin = next_prevwin;
3558
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003559#ifdef FEAT_AUTOCMD
3560 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003561
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003562 if (old_curbuf != curbuf)
3563 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3564#endif
3565
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003566 last_status(FALSE); /* status line may appear or disappear */
3567 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003568 must_redraw = CLEAR; /* need to redraw everything */
3569#ifdef FEAT_DIFF
3570 diff_need_scrollbind = TRUE;
3571#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003572
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003573 /* The tabpage line may have appeared or disappeared, may need to resize
3574 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003575 * frame sizes too. Use the stored value of p_ch, so that it can be
3576 * different for each tab page. */
3577 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003578 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3579#ifdef FEAT_GUI_TABLINE
3580 && !gui_use_tabline()
3581#endif
3582 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003583 shell_new_rows();
3584#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003585 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003586 shell_new_columns(); /* update window widths */
3587#endif
3588
3589#if defined(FEAT_GUI)
3590 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3591 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003592 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003593 {
3594 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003595 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003596 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003597 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003598#endif
3599
3600 redraw_all_later(CLEAR);
3601}
3602
3603/*
3604 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003605 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003606 */
3607 void
3608goto_tabpage(n)
3609 int n;
3610{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003611 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003612 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003613 int i;
3614
Bram Moolenaard68071d2006-05-02 22:08:30 +00003615 if (text_locked())
3616 {
3617 /* Not allowed when editing the command line. */
3618#ifdef FEAT_CMDWIN
3619 if (cmdwin_type != 0)
3620 EMSG(_(e_cmdwin));
3621 else
3622#endif
3623 EMSG(_(e_secure));
3624 return;
3625 }
3626
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003627 /* If there is only one it can't work. */
3628 if (first_tabpage->tp_next == NULL)
3629 {
3630 if (n > 1)
3631 beep_flush();
3632 return;
3633 }
3634
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003635 if (n == 0)
3636 {
3637 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003638 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003639 tp = first_tabpage;
3640 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003641 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003642 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003643 else if (n < 0)
3644 {
3645 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3646 * this N times. */
3647 ttp = curtab;
3648 for (i = n; i < 0; ++i)
3649 {
3650 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3651 tp = tp->tp_next)
3652 ;
3653 ttp = tp;
3654 }
3655 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003656 else if (n == 9999)
3657 {
3658 /* Go to last tab page. */
3659 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3660 ;
3661 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003662 else
3663 {
3664 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003665 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003666 if (tp == NULL)
3667 {
3668 beep_flush();
3669 return;
3670 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003671 }
3672
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003673 goto_tabpage_tp(tp);
3674
3675#ifdef FEAT_GUI_TABLINE
3676 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003677 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003678#endif
3679}
3680
3681/*
3682 * Go to tabpage "tp".
3683 * Note: doesn't update the GUI tab.
3684 */
3685 void
3686goto_tabpage_tp(tp)
3687 tabpage_T *tp;
3688{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003689 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003690 {
3691 if (valid_tabpage(tp))
3692 enter_tabpage(tp, curbuf);
3693 else
3694 enter_tabpage(curtab, curbuf);
3695 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003696}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697
3698/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003699 * Enter window "wp" in tab page "tp".
3700 * Also updates the GUI tab.
3701 */
3702 void
3703goto_tabpage_win(tp, wp)
3704 tabpage_T *tp;
3705 win_T *wp;
3706{
3707 goto_tabpage_tp(tp);
3708 if (curtab == tp && win_valid(wp))
3709 {
3710 win_enter(wp, TRUE);
3711# ifdef FEAT_GUI_TABLINE
3712 if (gui_use_tabline())
3713 gui_mch_set_curtab(tabpage_index(curtab));
3714# endif
3715 }
3716}
3717
3718/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003719 * Move the current tab page to before tab page "nr".
3720 */
3721 void
3722tabpage_move(nr)
3723 int nr;
3724{
3725 int n = nr;
3726 tabpage_T *tp;
3727
3728 if (first_tabpage->tp_next == NULL)
3729 return;
3730
3731 /* Remove the current tab page from the list of tab pages. */
3732 if (curtab == first_tabpage)
3733 first_tabpage = curtab->tp_next;
3734 else
3735 {
3736 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3737 if (tp->tp_next == curtab)
3738 break;
3739 if (tp == NULL) /* "cannot happen" */
3740 return;
3741 tp->tp_next = curtab->tp_next;
3742 }
3743
3744 /* Re-insert it at the specified position. */
3745 if (n == 0)
3746 {
3747 curtab->tp_next = first_tabpage;
3748 first_tabpage = curtab;
3749 }
3750 else
3751 {
3752 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3753 --n;
3754 curtab->tp_next = tp->tp_next;
3755 tp->tp_next = curtab;
3756 }
3757
3758 /* Need to redraw the tabline. Tab page contents doesn't change. */
3759 redraw_tabline = TRUE;
3760}
3761
3762
3763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 * Go to another window.
3765 * When jumping to another buffer, stop Visual mode. Do this before
3766 * changing windows so we can yank the selection into the '*' register.
3767 * When jumping to another window on the same buffer, adjust its cursor
3768 * position to keep the same Visual area.
3769 */
3770 void
3771win_goto(wp)
3772 win_T *wp;
3773{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003774 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 {
3776 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003777 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 return;
3779 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003780#ifdef FEAT_AUTOCMD
3781 if (curbuf_locked())
3782 return;
3783#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003784
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785#ifdef FEAT_VISUAL
3786 if (wp->w_buffer != curbuf)
3787 reset_VIsual_and_resel();
3788 else if (VIsual_active)
3789 wp->w_cursor = curwin->w_cursor;
3790#endif
3791
3792#ifdef FEAT_GUI
3793 need_mouse_correct = TRUE;
3794#endif
3795 win_enter(wp, TRUE);
3796}
3797
3798#if defined(FEAT_PERL) || defined(PROTO)
3799/*
3800 * Find window number "winnr" (counting top to bottom).
3801 */
3802 win_T *
3803win_find_nr(winnr)
3804 int winnr;
3805{
3806 win_T *wp;
3807
3808# ifdef FEAT_WINDOWS
3809 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3810 if (--winnr == 0)
3811 break;
3812 return wp;
3813# else
3814 return curwin;
3815# endif
3816}
3817#endif
3818
3819#ifdef FEAT_VERTSPLIT
3820/*
3821 * Move to window above or below "count" times.
3822 */
3823 static void
3824win_goto_ver(up, count)
3825 int up; /* TRUE to go to win above */
3826 long count;
3827{
3828 frame_T *fr;
3829 frame_T *nfr;
3830 frame_T *foundfr;
3831
3832 foundfr = curwin->w_frame;
3833 while (count--)
3834 {
3835 /*
3836 * First go upwards in the tree of frames until we find a upwards or
3837 * downwards neighbor.
3838 */
3839 fr = foundfr;
3840 for (;;)
3841 {
3842 if (fr == topframe)
3843 goto end;
3844 if (up)
3845 nfr = fr->fr_prev;
3846 else
3847 nfr = fr->fr_next;
3848 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3849 break;
3850 fr = fr->fr_parent;
3851 }
3852
3853 /*
3854 * Now go downwards to find the bottom or top frame in it.
3855 */
3856 for (;;)
3857 {
3858 if (nfr->fr_layout == FR_LEAF)
3859 {
3860 foundfr = nfr;
3861 break;
3862 }
3863 fr = nfr->fr_child;
3864 if (nfr->fr_layout == FR_ROW)
3865 {
3866 /* Find the frame at the cursor row. */
3867 while (fr->fr_next != NULL
3868 && frame2win(fr)->w_wincol + fr->fr_width
3869 <= curwin->w_wincol + curwin->w_wcol)
3870 fr = fr->fr_next;
3871 }
3872 if (nfr->fr_layout == FR_COL && up)
3873 while (fr->fr_next != NULL)
3874 fr = fr->fr_next;
3875 nfr = fr;
3876 }
3877 }
3878end:
3879 if (foundfr != NULL)
3880 win_goto(foundfr->fr_win);
3881}
3882
3883/*
3884 * Move to left or right window.
3885 */
3886 static void
3887win_goto_hor(left, count)
3888 int left; /* TRUE to go to left win */
3889 long count;
3890{
3891 frame_T *fr;
3892 frame_T *nfr;
3893 frame_T *foundfr;
3894
3895 foundfr = curwin->w_frame;
3896 while (count--)
3897 {
3898 /*
3899 * First go upwards in the tree of frames until we find a left or
3900 * right neighbor.
3901 */
3902 fr = foundfr;
3903 for (;;)
3904 {
3905 if (fr == topframe)
3906 goto end;
3907 if (left)
3908 nfr = fr->fr_prev;
3909 else
3910 nfr = fr->fr_next;
3911 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3912 break;
3913 fr = fr->fr_parent;
3914 }
3915
3916 /*
3917 * Now go downwards to find the leftmost or rightmost frame in it.
3918 */
3919 for (;;)
3920 {
3921 if (nfr->fr_layout == FR_LEAF)
3922 {
3923 foundfr = nfr;
3924 break;
3925 }
3926 fr = nfr->fr_child;
3927 if (nfr->fr_layout == FR_COL)
3928 {
3929 /* Find the frame at the cursor row. */
3930 while (fr->fr_next != NULL
3931 && frame2win(fr)->w_winrow + fr->fr_height
3932 <= curwin->w_winrow + curwin->w_wrow)
3933 fr = fr->fr_next;
3934 }
3935 if (nfr->fr_layout == FR_ROW && left)
3936 while (fr->fr_next != NULL)
3937 fr = fr->fr_next;
3938 nfr = fr;
3939 }
3940 }
3941end:
3942 if (foundfr != NULL)
3943 win_goto(foundfr->fr_win);
3944}
3945#endif
3946
3947/*
3948 * Make window "wp" the current window.
3949 */
3950 void
3951win_enter(wp, undo_sync)
3952 win_T *wp;
3953 int undo_sync;
3954{
3955 win_enter_ext(wp, undo_sync, FALSE);
3956}
3957
3958/*
3959 * Make window wp the current window.
3960 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3961 * been closed and isn't valid.
3962 */
3963 static void
3964win_enter_ext(wp, undo_sync, curwin_invalid)
3965 win_T *wp;
3966 int undo_sync;
3967 int curwin_invalid;
3968{
3969#ifdef FEAT_AUTOCMD
3970 int other_buffer = FALSE;
3971#endif
3972
3973 if (wp == curwin && !curwin_invalid) /* nothing to do */
3974 return;
3975
3976#ifdef FEAT_AUTOCMD
3977 if (!curwin_invalid)
3978 {
3979 /*
3980 * Be careful: If autocommands delete the window, return now.
3981 */
3982 if (wp->w_buffer != curbuf)
3983 {
3984 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3985 other_buffer = TRUE;
3986 if (!win_valid(wp))
3987 return;
3988 }
3989 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3990 if (!win_valid(wp))
3991 return;
3992# ifdef FEAT_EVAL
3993 /* autocmds may abort script processing */
3994 if (aborting())
3995 return;
3996# endif
3997 }
3998#endif
3999
4000 /* sync undo before leaving the current buffer */
4001 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004002 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 /* may have to copy the buffer options when 'cpo' contains 'S' */
4004 if (wp->w_buffer != curbuf)
4005 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4006 if (!curwin_invalid)
4007 {
4008 prevwin = curwin; /* remember for CTRL-W p */
4009 curwin->w_redr_status = TRUE;
4010 }
4011 curwin = wp;
4012 curbuf = wp->w_buffer;
4013 check_cursor();
4014#ifdef FEAT_VIRTUALEDIT
4015 if (!virtual_active())
4016 curwin->w_cursor.coladd = 0;
4017#endif
4018 changed_line_abv_curs(); /* assume cursor position needs updating */
4019
4020 if (curwin->w_localdir != NULL)
4021 {
4022 /* Window has a local directory: Save current directory as global
4023 * directory (unless that was done already) and change to the local
4024 * directory. */
4025 if (globaldir == NULL)
4026 {
4027 char_u cwd[MAXPATHL];
4028
4029 if (mch_dirname(cwd, MAXPATHL) == OK)
4030 globaldir = vim_strsave(cwd);
4031 }
4032 mch_chdir((char *)curwin->w_localdir);
4033 shorten_fnames(TRUE);
4034 }
4035 else if (globaldir != NULL)
4036 {
4037 /* Window doesn't have a local directory and we are not in the global
4038 * directory: Change to the global directory. */
4039 mch_chdir((char *)globaldir);
4040 vim_free(globaldir);
4041 globaldir = NULL;
4042 shorten_fnames(TRUE);
4043 }
4044
4045#ifdef FEAT_AUTOCMD
4046 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4047 if (other_buffer)
4048 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4049#endif
4050
4051#ifdef FEAT_TITLE
4052 maketitle();
4053#endif
4054 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004055 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 if (restart_edit)
4057 redraw_later(VALID); /* causes status line redraw */
4058
4059 /* set window height to desired minimal value */
4060 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4061 win_setheight((int)p_wh);
4062 else if (curwin->w_height == 0)
4063 win_setheight(1);
4064
4065#ifdef FEAT_VERTSPLIT
4066 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004067 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 win_setwidth((int)p_wiw);
4069#endif
4070
4071#ifdef FEAT_MOUSE
4072 setmouse(); /* in case jumped to/from help buffer */
4073#endif
4074
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004075 /* Change directories when the 'acd' option is set. */
4076 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077}
4078
4079#endif /* FEAT_WINDOWS */
4080
4081#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4082/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004083 * Jump to the first open window that contains buffer "buf", if one exists.
4084 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 */
4086 win_T *
4087buf_jump_open_win(buf)
4088 buf_T *buf;
4089{
4090# ifdef FEAT_WINDOWS
4091 win_T *wp;
4092
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004093 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 if (wp->w_buffer == buf)
4095 break;
4096 if (wp != NULL)
4097 win_enter(wp, FALSE);
4098 return wp;
4099# else
4100 if (curwin->w_buffer == buf)
4101 return curwin;
4102 return NULL;
4103# endif
4104}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004105
4106/*
4107 * Jump to the first open window in any tab page that contains buffer "buf",
4108 * if one exists.
4109 * Returns a pointer to the window found, otherwise NULL.
4110 */
4111 win_T *
4112buf_jump_open_tab(buf)
4113 buf_T *buf;
4114{
4115# ifdef FEAT_WINDOWS
4116 win_T *wp;
4117 tabpage_T *tp;
4118
4119 /* First try the current tab page. */
4120 wp = buf_jump_open_win(buf);
4121 if (wp != NULL)
4122 return wp;
4123
4124 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4125 if (tp != curtab)
4126 {
4127 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4128 if (wp->w_buffer == buf)
4129 break;
4130 if (wp != NULL)
4131 {
4132 goto_tabpage_win(tp, wp);
4133 if (curwin != wp)
4134 wp = NULL; /* something went wrong */
4135 break;
4136 }
4137 }
4138
4139 return wp;
4140# else
4141 if (curwin->w_buffer == buf)
4142 return curwin;
4143 return NULL;
4144# endif
4145}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146#endif
4147
4148/*
4149 * allocate a window structure and link it in the window list
4150 */
4151/*ARGSUSED*/
4152 static win_T *
4153win_alloc(after)
4154 win_T *after;
4155{
4156 win_T *newwin;
4157
4158 /*
4159 * allocate window structure and linesizes arrays
4160 */
4161 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4162 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4163 {
4164 vim_free(newwin);
4165 newwin = NULL;
4166 }
4167
4168 if (newwin != NULL)
4169 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004170#ifdef FEAT_AUTOCMD
4171 /* Don't execute autocommands while the window is not properly
4172 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4173 * event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004174 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 /*
4177 * link the window in the window list
4178 */
4179#ifdef FEAT_WINDOWS
4180 win_append(after, newwin);
4181#endif
4182#ifdef FEAT_VERTSPLIT
4183 newwin->w_wincol = 0;
4184 newwin->w_width = Columns;
4185#endif
4186
4187 /* position the display and the cursor at the top of the file. */
4188 newwin->w_topline = 1;
4189#ifdef FEAT_DIFF
4190 newwin->w_topfill = 0;
4191#endif
4192 newwin->w_botline = 2;
4193 newwin->w_cursor.lnum = 1;
4194#ifdef FEAT_SCROLLBIND
4195 newwin->w_scbind_pos = 1;
4196#endif
4197
4198 /* We won't calculate w_fraction until resizing the window */
4199 newwin->w_fraction = 0;
4200 newwin->w_prev_fraction_row = -1;
4201
4202#ifdef FEAT_GUI
4203 if (gui.in_use)
4204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4206 SBAR_LEFT, newwin);
4207 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4208 SBAR_RIGHT, newwin);
4209 }
4210#endif
4211#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004212 /* init w: variables */
4213 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214#endif
4215#ifdef FEAT_FOLDING
4216 foldInitWin(newwin);
4217#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004218#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004219 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004220#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004221#ifdef FEAT_SEARCH_EXTRA
4222 newwin->w_match_head = NULL;
4223 newwin->w_next_match_id = 4;
4224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
4226 return newwin;
4227}
4228
4229#if defined(FEAT_WINDOWS) || defined(PROTO)
4230
4231/*
4232 * remove window 'wp' from the window list and free the structure
4233 */
4234 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004235win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004237 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238{
4239 int i;
4240
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004241#ifdef FEAT_AUTOCMD
4242 /* Don't execute autocommands while the window is halfway being deleted.
4243 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004244 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004245#endif
4246
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004247#ifdef FEAT_MZSCHEME
4248 mzscheme_window_free(wp);
4249#endif
4250
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251#ifdef FEAT_PERL
4252 perl_win_free(wp);
4253#endif
4254
4255#ifdef FEAT_PYTHON
4256 python_window_free(wp);
4257#endif
4258
4259#ifdef FEAT_TCL
4260 tcl_window_free(wp);
4261#endif
4262
4263#ifdef FEAT_RUBY
4264 ruby_window_free(wp);
4265#endif
4266
4267 clear_winopt(&wp->w_onebuf_opt);
4268 clear_winopt(&wp->w_allbuf_opt);
4269
4270#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004271 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272#endif
4273
4274 if (prevwin == wp)
4275 prevwin = NULL;
4276 win_free_lsize(wp);
4277
4278 for (i = 0; i < wp->w_tagstacklen; ++i)
4279 vim_free(wp->w_tagstack[i].tagname);
4280
4281 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004282
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004284 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004286
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287#ifdef FEAT_JUMPLIST
4288 free_jumplist(wp);
4289#endif
4290
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004291#ifdef FEAT_QUICKFIX
4292 qf_free_all(wp);
4293#endif
4294
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295#ifdef FEAT_GUI
4296 if (gui.in_use)
4297 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4299 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4300 }
4301#endif /* FEAT_GUI */
4302
Bram Moolenaarf740b292006-02-16 22:11:02 +00004303 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004305
4306#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004307 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309}
4310
4311/*
4312 * Append window "wp" in the window list after window "after".
4313 */
4314 static void
4315win_append(after, wp)
4316 win_T *after, *wp;
4317{
4318 win_T *before;
4319
4320 if (after == NULL) /* after NULL is in front of the first */
4321 before = firstwin;
4322 else
4323 before = after->w_next;
4324
4325 wp->w_next = before;
4326 wp->w_prev = after;
4327 if (after == NULL)
4328 firstwin = wp;
4329 else
4330 after->w_next = wp;
4331 if (before == NULL)
4332 lastwin = wp;
4333 else
4334 before->w_prev = wp;
4335}
4336
4337/*
4338 * Remove a window from the window list.
4339 */
4340 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004341win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004343 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344{
4345 if (wp->w_prev != NULL)
4346 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004347 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004349 else
4350 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 if (wp->w_next != NULL)
4352 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004353 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004355 else
4356 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357}
4358
4359/*
4360 * Append frame "frp" in a frame list after frame "after".
4361 */
4362 static void
4363frame_append(after, frp)
4364 frame_T *after, *frp;
4365{
4366 frp->fr_next = after->fr_next;
4367 after->fr_next = frp;
4368 if (frp->fr_next != NULL)
4369 frp->fr_next->fr_prev = frp;
4370 frp->fr_prev = after;
4371}
4372
4373/*
4374 * Insert frame "frp" in a frame list before frame "before".
4375 */
4376 static void
4377frame_insert(before, frp)
4378 frame_T *before, *frp;
4379{
4380 frp->fr_next = before;
4381 frp->fr_prev = before->fr_prev;
4382 before->fr_prev = frp;
4383 if (frp->fr_prev != NULL)
4384 frp->fr_prev->fr_next = frp;
4385 else
4386 frp->fr_parent->fr_child = frp;
4387}
4388
4389/*
4390 * Remove a frame from a frame list.
4391 */
4392 static void
4393frame_remove(frp)
4394 frame_T *frp;
4395{
4396 if (frp->fr_prev != NULL)
4397 frp->fr_prev->fr_next = frp->fr_next;
4398 else
4399 frp->fr_parent->fr_child = frp->fr_next;
4400 if (frp->fr_next != NULL)
4401 frp->fr_next->fr_prev = frp->fr_prev;
4402}
4403
4404#endif /* FEAT_WINDOWS */
4405
4406/*
4407 * Allocate w_lines[] for window "wp".
4408 * Return FAIL for failure, OK for success.
4409 */
4410 int
4411win_alloc_lines(wp)
4412 win_T *wp;
4413{
4414 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004415 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 if (wp->w_lines == NULL)
4417 return FAIL;
4418 return OK;
4419}
4420
4421/*
4422 * free lsize arrays for a window
4423 */
4424 void
4425win_free_lsize(wp)
4426 win_T *wp;
4427{
4428 vim_free(wp->w_lines);
4429 wp->w_lines = NULL;
4430}
4431
4432/*
4433 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004434 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 */
4436 void
4437shell_new_rows()
4438{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004439 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
4441 if (firstwin == NULL) /* not initialized yet */
4442 return;
4443#ifdef FEAT_WINDOWS
4444 if (h < frame_minheight(topframe, NULL))
4445 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004446
4447 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 * that doesn't result in the right height, forget about that option. */
4449 frame_new_height(topframe, h, FALSE, TRUE);
4450 if (topframe->fr_height != h)
4451 frame_new_height(topframe, h, FALSE, FALSE);
4452
4453 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4454#else
4455 if (h < 1)
4456 h = 1;
4457 win_new_height(firstwin, h);
4458#endif
4459 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004460#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004461 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004462#endif
4463
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464#if 0
4465 /* Disabled: don't want making the screen smaller make a window larger. */
4466 if (p_ea)
4467 win_equal(curwin, FALSE, 'v');
4468#endif
4469}
4470
4471#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4472/*
4473 * Called from win_new_shellsize() after Columns changed.
4474 */
4475 void
4476shell_new_columns()
4477{
4478 if (firstwin == NULL) /* not initialized yet */
4479 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004480
4481 /* First try setting the widths of windows with 'winfixwidth'. If that
4482 * doesn't result in the right width, forget about that option. */
4483 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4484 if (topframe->fr_width != Columns)
4485 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4486
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4488#if 0
4489 /* Disabled: don't want making the screen smaller make a window larger. */
4490 if (p_ea)
4491 win_equal(curwin, FALSE, 'h');
4492#endif
4493}
4494#endif
4495
4496#if defined(FEAT_CMDWIN) || defined(PROTO)
4497/*
4498 * Save the size of all windows in "gap".
4499 */
4500 void
4501win_size_save(gap)
4502 garray_T *gap;
4503
4504{
4505 win_T *wp;
4506
4507 ga_init2(gap, (int)sizeof(int), 1);
4508 if (ga_grow(gap, win_count() * 2) == OK)
4509 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4510 {
4511 ((int *)gap->ga_data)[gap->ga_len++] =
4512 wp->w_width + wp->w_vsep_width;
4513 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4514 }
4515}
4516
4517/*
4518 * Restore window sizes, but only if the number of windows is still the same.
4519 * Does not free the growarray.
4520 */
4521 void
4522win_size_restore(gap)
4523 garray_T *gap;
4524{
4525 win_T *wp;
4526 int i;
4527
4528 if (win_count() * 2 == gap->ga_len)
4529 {
4530 i = 0;
4531 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4532 {
4533 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4534 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4535 }
4536 /* recompute the window positions */
4537 (void)win_comp_pos();
4538 }
4539}
4540#endif /* FEAT_CMDWIN */
4541
4542#if defined(FEAT_WINDOWS) || defined(PROTO)
4543/*
4544 * Update the position for all windows, using the width and height of the
4545 * frames.
4546 * Returns the row just after the last window.
4547 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004548 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549win_comp_pos()
4550{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004551 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 int col = 0;
4553
4554 frame_comp_pos(topframe, &row, &col);
4555 return row;
4556}
4557
4558/*
4559 * Update the position of the windows in frame "topfrp", using the width and
4560 * height of the frames.
4561 * "*row" and "*col" are the top-left position of the frame. They are updated
4562 * to the bottom-right position plus one.
4563 */
4564 static void
4565frame_comp_pos(topfrp, row, col)
4566 frame_T *topfrp;
4567 int *row;
4568 int *col;
4569{
4570 win_T *wp;
4571 frame_T *frp;
4572#ifdef FEAT_VERTSPLIT
4573 int startcol;
4574 int startrow;
4575#endif
4576
4577 wp = topfrp->fr_win;
4578 if (wp != NULL)
4579 {
4580 if (wp->w_winrow != *row
4581#ifdef FEAT_VERTSPLIT
4582 || wp->w_wincol != *col
4583#endif
4584 )
4585 {
4586 /* position changed, redraw */
4587 wp->w_winrow = *row;
4588#ifdef FEAT_VERTSPLIT
4589 wp->w_wincol = *col;
4590#endif
4591 redraw_win_later(wp, NOT_VALID);
4592 wp->w_redr_status = TRUE;
4593 }
4594 *row += wp->w_height + wp->w_status_height;
4595#ifdef FEAT_VERTSPLIT
4596 *col += wp->w_width + wp->w_vsep_width;
4597#endif
4598 }
4599 else
4600 {
4601#ifdef FEAT_VERTSPLIT
4602 startrow = *row;
4603 startcol = *col;
4604#endif
4605 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4606 {
4607#ifdef FEAT_VERTSPLIT
4608 if (topfrp->fr_layout == FR_ROW)
4609 *row = startrow; /* all frames are at the same row */
4610 else
4611 *col = startcol; /* all frames are at the same col */
4612#endif
4613 frame_comp_pos(frp, row, col);
4614 }
4615 }
4616}
4617
4618#endif /* FEAT_WINDOWS */
4619
4620/*
4621 * Set current window height and take care of repositioning other windows to
4622 * fit around it.
4623 */
4624 void
4625win_setheight(height)
4626 int height;
4627{
4628 win_setheight_win(height, curwin);
4629}
4630
4631/*
4632 * Set the window height of window "win" and take care of repositioning other
4633 * windows to fit around it.
4634 */
4635 void
4636win_setheight_win(height, win)
4637 int height;
4638 win_T *win;
4639{
4640 int row;
4641
4642 if (win == curwin)
4643 {
4644 /* Always keep current window at least one line high, even when
4645 * 'winminheight' is zero. */
4646#ifdef FEAT_WINDOWS
4647 if (height < p_wmh)
4648 height = p_wmh;
4649#endif
4650 if (height == 0)
4651 height = 1;
4652 }
4653
4654#ifdef FEAT_WINDOWS
4655 frame_setheight(win->w_frame, height + win->w_status_height);
4656
4657 /* recompute the window positions */
4658 row = win_comp_pos();
4659#else
4660 if (height > topframe->fr_height)
4661 height = topframe->fr_height;
4662 win->w_height = height;
4663 row = height;
4664#endif
4665
4666 /*
4667 * If there is extra space created between the last window and the command
4668 * line, clear it.
4669 */
4670 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4671 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4672 cmdline_row = row;
4673 msg_row = row;
4674 msg_col = 0;
4675
4676 redraw_all_later(NOT_VALID);
4677}
4678
4679#if defined(FEAT_WINDOWS) || defined(PROTO)
4680
4681/*
4682 * Set the height of a frame to "height" and take care that all frames and
4683 * windows inside it are resized. Also resize frames on the left and right if
4684 * the are in the same FR_ROW frame.
4685 *
4686 * Strategy:
4687 * If the frame is part of a FR_COL frame, try fitting the frame in that
4688 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4689 * go to containing frames to resize them and make room.
4690 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4691 * Check for the minimal height of the FR_ROW frame.
4692 * At the top level we can also use change the command line height.
4693 */
4694 static void
4695frame_setheight(curfrp, height)
4696 frame_T *curfrp;
4697 int height;
4698{
4699 int room; /* total number of lines available */
4700 int take; /* number of lines taken from other windows */
4701 int room_cmdline; /* lines available from cmdline */
4702 int run;
4703 frame_T *frp;
4704 int h;
4705 int room_reserved;
4706
4707 /* If the height already is the desired value, nothing to do. */
4708 if (curfrp->fr_height == height)
4709 return;
4710
4711 if (curfrp->fr_parent == NULL)
4712 {
4713 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004714 if (height > ROWS_AVAIL)
4715 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 if (height > 0)
4717 frame_new_height(curfrp, height, FALSE, FALSE);
4718 }
4719 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4720 {
4721 /* Row of frames: Also need to resize frames left and right of this
4722 * one. First check for the minimal height of these. */
4723 h = frame_minheight(curfrp->fr_parent, NULL);
4724 if (height < h)
4725 height = h;
4726 frame_setheight(curfrp->fr_parent, height);
4727 }
4728 else
4729 {
4730 /*
4731 * Column of frames: try to change only frames in this column.
4732 */
4733#ifdef FEAT_VERTSPLIT
4734 /*
4735 * Do this twice:
4736 * 1: compute room available, if it's not enough try resizing the
4737 * containing frame.
4738 * 2: compute the room available and adjust the height to it.
4739 * Try not to reduce the height of a window with 'winfixheight' set.
4740 */
4741 for (run = 1; run <= 2; ++run)
4742#else
4743 for (;;)
4744#endif
4745 {
4746 room = 0;
4747 room_reserved = 0;
4748 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4749 frp = frp->fr_next)
4750 {
4751 if (frp != curfrp
4752 && frp->fr_win != NULL
4753 && frp->fr_win->w_p_wfh)
4754 room_reserved += frp->fr_height;
4755 room += frp->fr_height;
4756 if (frp != curfrp)
4757 room -= frame_minheight(frp, NULL);
4758 }
4759#ifdef FEAT_VERTSPLIT
4760 if (curfrp->fr_width != Columns)
4761 room_cmdline = 0;
4762 else
4763#endif
4764 {
4765 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4766 + lastwin->w_height + lastwin->w_status_height);
4767 if (room_cmdline < 0)
4768 room_cmdline = 0;
4769 }
4770
4771 if (height <= room + room_cmdline)
4772 break;
4773#ifdef FEAT_VERTSPLIT
4774 if (run == 2 || curfrp->fr_width == Columns)
4775#endif
4776 {
4777 if (height > room + room_cmdline)
4778 height = room + room_cmdline;
4779 break;
4780 }
4781#ifdef FEAT_VERTSPLIT
4782 frame_setheight(curfrp->fr_parent, height
4783 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4784#endif
4785 /*NOTREACHED*/
4786 }
4787
4788 /*
4789 * Compute the number of lines we will take from others frames (can be
4790 * negative!).
4791 */
4792 take = height - curfrp->fr_height;
4793
4794 /* If there is not enough room, also reduce the height of a window
4795 * with 'winfixheight' set. */
4796 if (height > room + room_cmdline - room_reserved)
4797 room_reserved = room + room_cmdline - height;
4798 /* If there is only a 'winfixheight' window and making the
4799 * window smaller, need to make the other window taller. */
4800 if (take < 0 && room - curfrp->fr_height < room_reserved)
4801 room_reserved = 0;
4802
4803 if (take > 0 && room_cmdline > 0)
4804 {
4805 /* use lines from cmdline first */
4806 if (take < room_cmdline)
4807 room_cmdline = take;
4808 take -= room_cmdline;
4809 topframe->fr_height += room_cmdline;
4810 }
4811
4812 /*
4813 * set the current frame to the new height
4814 */
4815 frame_new_height(curfrp, height, FALSE, FALSE);
4816
4817 /*
4818 * First take lines from the frames after the current frame. If
4819 * that is not enough, takes lines from frames above the current
4820 * frame.
4821 */
4822 for (run = 0; run < 2; ++run)
4823 {
4824 if (run == 0)
4825 frp = curfrp->fr_next; /* 1st run: start with next window */
4826 else
4827 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4828 while (frp != NULL && take != 0)
4829 {
4830 h = frame_minheight(frp, NULL);
4831 if (room_reserved > 0
4832 && frp->fr_win != NULL
4833 && frp->fr_win->w_p_wfh)
4834 {
4835 if (room_reserved >= frp->fr_height)
4836 room_reserved -= frp->fr_height;
4837 else
4838 {
4839 if (frp->fr_height - room_reserved > take)
4840 room_reserved = frp->fr_height - take;
4841 take -= frp->fr_height - room_reserved;
4842 frame_new_height(frp, room_reserved, FALSE, FALSE);
4843 room_reserved = 0;
4844 }
4845 }
4846 else
4847 {
4848 if (frp->fr_height - take < h)
4849 {
4850 take -= frp->fr_height - h;
4851 frame_new_height(frp, h, FALSE, FALSE);
4852 }
4853 else
4854 {
4855 frame_new_height(frp, frp->fr_height - take,
4856 FALSE, FALSE);
4857 take = 0;
4858 }
4859 }
4860 if (run == 0)
4861 frp = frp->fr_next;
4862 else
4863 frp = frp->fr_prev;
4864 }
4865 }
4866 }
4867}
4868
4869#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4870/*
4871 * Set current window width and take care of repositioning other windows to
4872 * fit around it.
4873 */
4874 void
4875win_setwidth(width)
4876 int width;
4877{
4878 win_setwidth_win(width, curwin);
4879}
4880
4881 void
4882win_setwidth_win(width, wp)
4883 int width;
4884 win_T *wp;
4885{
4886 /* Always keep current window at least one column wide, even when
4887 * 'winminwidth' is zero. */
4888 if (wp == curwin)
4889 {
4890 if (width < p_wmw)
4891 width = p_wmw;
4892 if (width == 0)
4893 width = 1;
4894 }
4895
4896 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4897
4898 /* recompute the window positions */
4899 (void)win_comp_pos();
4900
4901 redraw_all_later(NOT_VALID);
4902}
4903
4904/*
4905 * Set the width of a frame to "width" and take care that all frames and
4906 * windows inside it are resized. Also resize frames above and below if the
4907 * are in the same FR_ROW frame.
4908 *
4909 * Strategy is similar to frame_setheight().
4910 */
4911 static void
4912frame_setwidth(curfrp, width)
4913 frame_T *curfrp;
4914 int width;
4915{
4916 int room; /* total number of lines available */
4917 int take; /* number of lines taken from other windows */
4918 int run;
4919 frame_T *frp;
4920 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004921 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
4923 /* If the width already is the desired value, nothing to do. */
4924 if (curfrp->fr_width == width)
4925 return;
4926
4927 if (curfrp->fr_parent == NULL)
4928 /* topframe: can't change width */
4929 return;
4930
4931 if (curfrp->fr_parent->fr_layout == FR_COL)
4932 {
4933 /* Column of frames: Also need to resize frames above and below of
4934 * this one. First check for the minimal width of these. */
4935 w = frame_minwidth(curfrp->fr_parent, NULL);
4936 if (width < w)
4937 width = w;
4938 frame_setwidth(curfrp->fr_parent, width);
4939 }
4940 else
4941 {
4942 /*
4943 * Row of frames: try to change only frames in this row.
4944 *
4945 * Do this twice:
4946 * 1: compute room available, if it's not enough try resizing the
4947 * containing frame.
4948 * 2: compute the room available and adjust the width to it.
4949 */
4950 for (run = 1; run <= 2; ++run)
4951 {
4952 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004953 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4955 frp = frp->fr_next)
4956 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004957 if (frp != curfrp
4958 && frp->fr_win != NULL
4959 && frp->fr_win->w_p_wfw)
4960 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 room += frp->fr_width;
4962 if (frp != curfrp)
4963 room -= frame_minwidth(frp, NULL);
4964 }
4965
4966 if (width <= room)
4967 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004968 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
4970 if (width > room)
4971 width = room;
4972 break;
4973 }
4974 frame_setwidth(curfrp->fr_parent, width
4975 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4976 }
4977
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 /*
4979 * Compute the number of lines we will take from others frames (can be
4980 * negative!).
4981 */
4982 take = width - curfrp->fr_width;
4983
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004984 /* If there is not enough room, also reduce the width of a window
4985 * with 'winfixwidth' set. */
4986 if (width > room - room_reserved)
4987 room_reserved = room - width;
4988 /* If there is only a 'winfixwidth' window and making the
4989 * window smaller, need to make the other window narrower. */
4990 if (take < 0 && room - curfrp->fr_width < room_reserved)
4991 room_reserved = 0;
4992
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 /*
4994 * set the current frame to the new width
4995 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004996 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997
4998 /*
4999 * First take lines from the frames right of the current frame. If
5000 * that is not enough, takes lines from frames left of the current
5001 * frame.
5002 */
5003 for (run = 0; run < 2; ++run)
5004 {
5005 if (run == 0)
5006 frp = curfrp->fr_next; /* 1st run: start with next window */
5007 else
5008 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5009 while (frp != NULL && take != 0)
5010 {
5011 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005012 if (room_reserved > 0
5013 && frp->fr_win != NULL
5014 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005016 if (room_reserved >= frp->fr_width)
5017 room_reserved -= frp->fr_width;
5018 else
5019 {
5020 if (frp->fr_width - room_reserved > take)
5021 room_reserved = frp->fr_width - take;
5022 take -= frp->fr_width - room_reserved;
5023 frame_new_width(frp, room_reserved, FALSE, FALSE);
5024 room_reserved = 0;
5025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
5027 else
5028 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005029 if (frp->fr_width - take < w)
5030 {
5031 take -= frp->fr_width - w;
5032 frame_new_width(frp, w, FALSE, FALSE);
5033 }
5034 else
5035 {
5036 frame_new_width(frp, frp->fr_width - take,
5037 FALSE, FALSE);
5038 take = 0;
5039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
5041 if (run == 0)
5042 frp = frp->fr_next;
5043 else
5044 frp = frp->fr_prev;
5045 }
5046 }
5047 }
5048}
5049#endif /* FEAT_VERTSPLIT */
5050
5051/*
5052 * Check 'winminheight' for a valid value.
5053 */
5054 void
5055win_setminheight()
5056{
5057 int room;
5058 int first = TRUE;
5059 win_T *wp;
5060
5061 /* loop until there is a 'winminheight' that is possible */
5062 while (p_wmh > 0)
5063 {
5064 /* TODO: handle vertical splits */
5065 room = -p_wh;
5066 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5067 room += wp->w_height - p_wmh;
5068 if (room >= 0)
5069 break;
5070 --p_wmh;
5071 if (first)
5072 {
5073 EMSG(_(e_noroom));
5074 first = FALSE;
5075 }
5076 }
5077}
5078
5079#ifdef FEAT_MOUSE
5080
5081/*
5082 * Status line of dragwin is dragged "offset" lines down (negative is up).
5083 */
5084 void
5085win_drag_status_line(dragwin, offset)
5086 win_T *dragwin;
5087 int offset;
5088{
5089 frame_T *curfr;
5090 frame_T *fr;
5091 int room;
5092 int row;
5093 int up; /* if TRUE, drag status line up, otherwise down */
5094 int n;
5095
5096 fr = dragwin->w_frame;
5097 curfr = fr;
5098 if (fr != topframe) /* more than one window */
5099 {
5100 fr = fr->fr_parent;
5101 /* When the parent frame is not a column of frames, its parent should
5102 * be. */
5103 if (fr->fr_layout != FR_COL)
5104 {
5105 curfr = fr;
5106 if (fr != topframe) /* only a row of windows, may drag statusline */
5107 fr = fr->fr_parent;
5108 }
5109 }
5110
5111 /* If this is the last frame in a column, may want to resize the parent
5112 * frame instead (go two up to skip a row of frames). */
5113 while (curfr != topframe && curfr->fr_next == NULL)
5114 {
5115 if (fr != topframe)
5116 fr = fr->fr_parent;
5117 curfr = fr;
5118 if (fr != topframe)
5119 fr = fr->fr_parent;
5120 }
5121
5122 if (offset < 0) /* drag up */
5123 {
5124 up = TRUE;
5125 offset = -offset;
5126 /* sum up the room of the current frame and above it */
5127 if (fr == curfr)
5128 {
5129 /* only one window */
5130 room = fr->fr_height - frame_minheight(fr, NULL);
5131 }
5132 else
5133 {
5134 room = 0;
5135 for (fr = fr->fr_child; ; fr = fr->fr_next)
5136 {
5137 room += fr->fr_height - frame_minheight(fr, NULL);
5138 if (fr == curfr)
5139 break;
5140 }
5141 }
5142 fr = curfr->fr_next; /* put fr at frame that grows */
5143 }
5144 else /* drag down */
5145 {
5146 up = FALSE;
5147 /*
5148 * Only dragging the last status line can reduce p_ch.
5149 */
5150 room = Rows - cmdline_row;
5151 if (curfr->fr_next == NULL)
5152 room -= 1;
5153 else
5154 room -= p_ch;
5155 if (room < 0)
5156 room = 0;
5157 /* sum up the room of frames below of the current one */
5158 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5159 room += fr->fr_height - frame_minheight(fr, NULL);
5160 fr = curfr; /* put fr at window that grows */
5161 }
5162
5163 if (room < offset) /* Not enough room */
5164 offset = room; /* Move as far as we can */
5165 if (offset <= 0)
5166 return;
5167
5168 /*
5169 * Grow frame fr by "offset" lines.
5170 * Doesn't happen when dragging the last status line up.
5171 */
5172 if (fr != NULL)
5173 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5174
5175 if (up)
5176 fr = curfr; /* current frame gets smaller */
5177 else
5178 fr = curfr->fr_next; /* next frame gets smaller */
5179
5180 /*
5181 * Now make the other frames smaller.
5182 */
5183 while (fr != NULL && offset > 0)
5184 {
5185 n = frame_minheight(fr, NULL);
5186 if (fr->fr_height - offset <= n)
5187 {
5188 offset -= fr->fr_height - n;
5189 frame_new_height(fr, n, !up, FALSE);
5190 }
5191 else
5192 {
5193 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5194 break;
5195 }
5196 if (up)
5197 fr = fr->fr_prev;
5198 else
5199 fr = fr->fr_next;
5200 }
5201 row = win_comp_pos();
5202 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5203 cmdline_row = row;
5204 p_ch = Rows - cmdline_row;
5205 if (p_ch < 1)
5206 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005207 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005208 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 showmode();
5210}
5211
5212#ifdef FEAT_VERTSPLIT
5213/*
5214 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5215 */
5216 void
5217win_drag_vsep_line(dragwin, offset)
5218 win_T *dragwin;
5219 int offset;
5220{
5221 frame_T *curfr;
5222 frame_T *fr;
5223 int room;
5224 int left; /* if TRUE, drag separator line left, otherwise right */
5225 int n;
5226
5227 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005228 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 return;
5230 curfr = fr;
5231 fr = fr->fr_parent;
5232 /* When the parent frame is not a row of frames, its parent should be. */
5233 if (fr->fr_layout != FR_ROW)
5234 {
5235 if (fr == topframe) /* only a column of windows (cannot happen?) */
5236 return;
5237 curfr = fr;
5238 fr = fr->fr_parent;
5239 }
5240
5241 /* If this is the last frame in a row, may want to resize a parent
5242 * frame instead. */
5243 while (curfr->fr_next == NULL)
5244 {
5245 if (fr == topframe)
5246 break;
5247 curfr = fr;
5248 fr = fr->fr_parent;
5249 if (fr != topframe)
5250 {
5251 curfr = fr;
5252 fr = fr->fr_parent;
5253 }
5254 }
5255
5256 if (offset < 0) /* drag left */
5257 {
5258 left = TRUE;
5259 offset = -offset;
5260 /* sum up the room of the current frame and left of it */
5261 room = 0;
5262 for (fr = fr->fr_child; ; fr = fr->fr_next)
5263 {
5264 room += fr->fr_width - frame_minwidth(fr, NULL);
5265 if (fr == curfr)
5266 break;
5267 }
5268 fr = curfr->fr_next; /* put fr at frame that grows */
5269 }
5270 else /* drag right */
5271 {
5272 left = FALSE;
5273 /* sum up the room of frames right of the current one */
5274 room = 0;
5275 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5276 room += fr->fr_width - frame_minwidth(fr, NULL);
5277 fr = curfr; /* put fr at window that grows */
5278 }
5279
5280 if (room < offset) /* Not enough room */
5281 offset = room; /* Move as far as we can */
5282 if (offset <= 0) /* No room at all, quit. */
5283 return;
5284
5285 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005286 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287
5288 /* shrink other frames: current and at the left or at the right */
5289 if (left)
5290 fr = curfr; /* current frame gets smaller */
5291 else
5292 fr = curfr->fr_next; /* next frame gets smaller */
5293
5294 while (fr != NULL && offset > 0)
5295 {
5296 n = frame_minwidth(fr, NULL);
5297 if (fr->fr_width - offset <= n)
5298 {
5299 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005300 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 }
5302 else
5303 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005304 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 break;
5306 }
5307 if (left)
5308 fr = fr->fr_prev;
5309 else
5310 fr = fr->fr_next;
5311 }
5312 (void)win_comp_pos();
5313 redraw_all_later(NOT_VALID);
5314}
5315#endif /* FEAT_VERTSPLIT */
5316#endif /* FEAT_MOUSE */
5317
5318#endif /* FEAT_WINDOWS */
5319
5320/*
5321 * Set the height of a window.
5322 * This takes care of the things inside the window, not what happens to the
5323 * window position, the frame or to other windows.
5324 */
5325 static void
5326win_new_height(wp, height)
5327 win_T *wp;
5328 int height;
5329{
5330 linenr_T lnum;
5331 int sline, line_size;
5332#define FRACTION_MULT 16384L
5333
5334 /* Don't want a negative height. Happens when splitting a tiny window.
5335 * Will equalize heights soon to fix it. */
5336 if (height < 0)
5337 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005338 if (wp->w_height == height)
5339 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340
5341 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5342 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5343 + FRACTION_MULT / 2) / (long)wp->w_height;
5344
5345 wp->w_height = height;
5346 wp->w_skipcol = 0;
5347
5348 /* Don't change w_topline when height is zero. Don't set w_topline when
5349 * 'scrollbind' is set and this isn't the current window. */
5350 if (height > 0
5351#ifdef FEAT_SCROLLBIND
5352 && (!wp->w_p_scb || wp == curwin)
5353#endif
5354 )
5355 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005356 /*
5357 * Find a value for w_topline that shows the cursor at the same
5358 * relative position in the window as before (more or less).
5359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 lnum = wp->w_cursor.lnum;
5361 if (lnum < 1) /* can happen when starting up */
5362 lnum = 1;
5363 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5364 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5365 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005366
5367 if (sline >= 0)
5368 {
5369 /* Make sure the whole cursor line is visible, if possible. */
5370 int rows = plines_win(wp, lnum, FALSE);
5371
5372 if (sline > wp->w_height - rows)
5373 {
5374 sline = wp->w_height - rows;
5375 wp->w_wrow -= rows - line_size;
5376 }
5377 }
5378
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 if (sline < 0)
5380 {
5381 /*
5382 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005383 * Make cursor line the first line in the window. If not enough
5384 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 */
5386 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005387 if (wp->w_wrow >= wp->w_height
5388 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5389 {
5390 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5391 --wp->w_wrow;
5392 while (wp->w_wrow >= wp->w_height)
5393 {
5394 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5395 + win_col_off2(wp);
5396 --wp->w_wrow;
5397 }
5398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 }
5400 else
5401 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005402 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 {
5404#ifdef FEAT_FOLDING
5405 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5406 if (lnum == 1)
5407 {
5408 /* first line in buffer is folded */
5409 line_size = 1;
5410 --sline;
5411 break;
5412 }
5413#endif
5414 --lnum;
5415#ifdef FEAT_DIFF
5416 if (lnum == wp->w_topline)
5417 line_size = plines_win_nofill(wp, lnum, TRUE)
5418 + wp->w_topfill;
5419 else
5420#endif
5421 line_size = plines_win(wp, lnum, TRUE);
5422 sline -= line_size;
5423 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005424
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 if (sline < 0)
5426 {
5427 /*
5428 * Line we want at top would go off top of screen. Use next
5429 * line instead.
5430 */
5431#ifdef FEAT_FOLDING
5432 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5433#endif
5434 lnum++;
5435 wp->w_wrow -= line_size + sline;
5436 }
5437 else if (sline > 0)
5438 {
5439 /* First line of file reached, use that as topline. */
5440 lnum = 1;
5441 wp->w_wrow -= sline;
5442 }
5443 }
5444 set_topline(wp, lnum);
5445 }
5446
5447 if (wp == curwin)
5448 {
5449 if (p_so)
5450 update_topline();
5451 curs_columns(FALSE); /* validate w_wrow */
5452 }
5453 wp->w_prev_fraction_row = wp->w_wrow;
5454
5455 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005456 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457#ifdef FEAT_WINDOWS
5458 wp->w_redr_status = TRUE;
5459#endif
5460 invalidate_botline_win(wp);
5461}
5462
5463#ifdef FEAT_VERTSPLIT
5464/*
5465 * Set the width of a window.
5466 */
5467 static void
5468win_new_width(wp, width)
5469 win_T *wp;
5470 int width;
5471{
5472 wp->w_width = width;
5473 wp->w_lines_valid = 0;
5474 changed_line_abv_curs_win(wp);
5475 invalidate_botline_win(wp);
5476 if (wp == curwin)
5477 {
5478 update_topline();
5479 curs_columns(TRUE); /* validate w_wrow */
5480 }
5481 redraw_win_later(wp, NOT_VALID);
5482 wp->w_redr_status = TRUE;
5483}
5484#endif
5485
5486 void
5487win_comp_scroll(wp)
5488 win_T *wp;
5489{
5490 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5491 if (wp->w_p_scr == 0)
5492 wp->w_p_scr = 1;
5493}
5494
5495/*
5496 * command_height: called whenever p_ch has been changed
5497 */
5498 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005499command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500{
5501#ifdef FEAT_WINDOWS
5502 int h;
5503 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005504 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005506 /* Use the value of p_ch that we remembered. This is needed for when the
5507 * GUI starts up, we can't be sure in what order things happen. And when
5508 * p_ch was changed in another tab page. */
5509 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005510
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 /* Find bottom frame with width of screen. */
5512 frp = lastwin->w_frame;
5513# ifdef FEAT_VERTSPLIT
5514 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5515 frp = frp->fr_parent;
5516# endif
5517
5518 /* Avoid changing the height of a window with 'winfixheight' set. */
5519 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5520 && frp->fr_win->w_p_wfh)
5521 frp = frp->fr_prev;
5522
5523 if (starting != NO_SCREEN)
5524 {
5525 cmdline_row = Rows - p_ch;
5526
5527 if (p_ch > old_p_ch) /* p_ch got bigger */
5528 {
5529 while (p_ch > old_p_ch)
5530 {
5531 if (frp == NULL)
5532 {
5533 EMSG(_(e_noroom));
5534 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005535 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 cmdline_row = Rows - p_ch;
5537 break;
5538 }
5539 h = frp->fr_height - frame_minheight(frp, NULL);
5540 if (h > p_ch - old_p_ch)
5541 h = p_ch - old_p_ch;
5542 old_p_ch += h;
5543 frame_add_height(frp, -h);
5544 frp = frp->fr_prev;
5545 }
5546
5547 /* Recompute window positions. */
5548 (void)win_comp_pos();
5549
5550 /* clear the lines added to cmdline */
5551 if (full_screen)
5552 screen_fill((int)(cmdline_row), (int)Rows, 0,
5553 (int)Columns, ' ', ' ', 0);
5554 msg_row = cmdline_row;
5555 redraw_cmdline = TRUE;
5556 return;
5557 }
5558
5559 if (msg_row < cmdline_row)
5560 msg_row = cmdline_row;
5561 redraw_cmdline = TRUE;
5562 }
5563 frame_add_height(frp, (int)(old_p_ch - p_ch));
5564
5565 /* Recompute window positions. */
5566 if (frp != lastwin->w_frame)
5567 (void)win_comp_pos();
5568#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005570 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571#endif
5572}
5573
5574#if defined(FEAT_WINDOWS) || defined(PROTO)
5575/*
5576 * Resize frame "frp" to be "n" lines higher (negative for less high).
5577 * Also resize the frames it is contained in.
5578 */
5579 static void
5580frame_add_height(frp, n)
5581 frame_T *frp;
5582 int n;
5583{
5584 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5585 for (;;)
5586 {
5587 frp = frp->fr_parent;
5588 if (frp == NULL)
5589 break;
5590 frp->fr_height += n;
5591 }
5592}
5593
5594/*
5595 * Add or remove a status line for the bottom window(s), according to the
5596 * value of 'laststatus'.
5597 */
5598 void
5599last_status(morewin)
5600 int morewin; /* pretend there are two or more windows */
5601{
5602 /* Don't make a difference between horizontal or vertical split. */
5603 last_status_rec(topframe, (p_ls == 2
5604 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5605}
5606
5607 static void
5608last_status_rec(fr, statusline)
5609 frame_T *fr;
5610 int statusline;
5611{
5612 frame_T *fp;
5613 win_T *wp;
5614
5615 if (fr->fr_layout == FR_LEAF)
5616 {
5617 wp = fr->fr_win;
5618 if (wp->w_status_height != 0 && !statusline)
5619 {
5620 /* remove status line */
5621 win_new_height(wp, wp->w_height + 1);
5622 wp->w_status_height = 0;
5623 comp_col();
5624 }
5625 else if (wp->w_status_height == 0 && statusline)
5626 {
5627 /* Find a frame to take a line from. */
5628 fp = fr;
5629 while (fp->fr_height <= frame_minheight(fp, NULL))
5630 {
5631 if (fp == topframe)
5632 {
5633 EMSG(_(e_noroom));
5634 return;
5635 }
5636 /* In a column of frames: go to frame above. If already at
5637 * the top or in a row of frames: go to parent. */
5638 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5639 fp = fp->fr_prev;
5640 else
5641 fp = fp->fr_parent;
5642 }
5643 wp->w_status_height = 1;
5644 if (fp != fr)
5645 {
5646 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5647 frame_fix_height(wp);
5648 (void)win_comp_pos();
5649 }
5650 else
5651 win_new_height(wp, wp->w_height - 1);
5652 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005653 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 }
5655 }
5656#ifdef FEAT_VERTSPLIT
5657 else if (fr->fr_layout == FR_ROW)
5658 {
5659 /* vertically split windows, set status line for each one */
5660 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5661 last_status_rec(fp, statusline);
5662 }
5663#endif
5664 else
5665 {
5666 /* horizontally split window, set status line for last one */
5667 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5668 ;
5669 last_status_rec(fp, statusline);
5670 }
5671}
5672
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005673/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005674 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005675 */
5676 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005677tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005678{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005679#ifdef FEAT_GUI_TABLINE
5680 /* When the GUI has the tabline then this always returns zero. */
5681 if (gui_use_tabline())
5682 return 0;
5683#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005684 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005685 {
5686 case 0: return 0;
5687 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5688 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005689 return 1;
5690}
5691
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692#endif /* FEAT_WINDOWS */
5693
5694#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5695/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005696 * Get the file name at the cursor.
5697 * If Visual mode is active, use the selected text if it's in one line.
5698 * Returns the name in allocated memory, NULL for failure.
5699 */
5700 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005701grab_file_name(count, file_lnum)
5702 long count;
5703 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005704{
5705# ifdef FEAT_VISUAL
5706 if (VIsual_active)
5707 {
5708 int len;
5709 char_u *ptr;
5710
5711 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5712 return NULL;
5713 return find_file_name_in_path(ptr, len,
5714 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5715 }
5716# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005717 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5718 file_lnum);
5719
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005720}
5721
5722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 * Return the file name under or after the cursor.
5724 *
5725 * The 'path' option is searched if the file name is not absolute.
5726 * The string returned has been alloc'ed and should be freed by the caller.
5727 * NULL is returned if the file name or file is not found.
5728 *
5729 * options:
5730 * FNAME_MESS give error messages
5731 * FNAME_EXP expand to path
5732 * FNAME_HYP check for hypertext link
5733 * FNAME_INCL apply "includeexpr"
5734 */
5735 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005736file_name_at_cursor(options, count, file_lnum)
5737 int options;
5738 long count;
5739 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
5741 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005742 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5743 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744}
5745
5746/*
5747 * Return the name of the file under or after ptr[col].
5748 * Otherwise like file_name_at_cursor().
5749 */
5750 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005751file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 char_u *line;
5753 int col;
5754 int options;
5755 long count;
5756 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005757 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758{
5759 char_u *ptr;
5760 int len;
5761
5762 /*
5763 * search forward for what could be the start of a file name
5764 */
5765 ptr = line + col;
5766 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005767 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 if (*ptr == NUL) /* nothing found */
5769 {
5770 if (options & FNAME_MESS)
5771 EMSG(_("E446: No file name under cursor"));
5772 return NULL;
5773 }
5774
5775 /*
5776 * Search backward for first char of the file name.
5777 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5778 */
5779 while (ptr > line)
5780 {
5781#ifdef FEAT_MBYTE
5782 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5783 ptr -= len + 1;
5784 else
5785#endif
5786 if (vim_isfilec(ptr[-1])
5787 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5788 --ptr;
5789 else
5790 break;
5791 }
5792
5793 /*
5794 * Search forward for the last char of the file name.
5795 * Also allow "://" when ':' is not in 'isfname'.
5796 */
5797 len = 0;
5798 while (vim_isfilec(ptr[len])
5799 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5800#ifdef FEAT_MBYTE
5801 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005802 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 else
5804#endif
5805 ++len;
5806
5807 /*
5808 * If there is trailing punctuation, remove it.
5809 * But don't remove "..", could be a directory name.
5810 */
5811 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5812 && ptr[len - 2] != '.')
5813 --len;
5814
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005815 if (file_lnum != NULL)
5816 {
5817 char_u *p;
5818
5819 /* Get the number after the file name and a separator character */
5820 p = ptr + len;
5821 p = skipwhite(p);
5822 if (*p != NUL)
5823 {
5824 if (!isdigit(*p))
5825 ++p; /* skip the separator */
5826 p = skipwhite(p);
5827 if (isdigit(*p))
5828 *file_lnum = (int)getdigits(&p);
5829 }
5830 }
5831
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5833}
5834
5835# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5836static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5837
5838 static char_u *
5839eval_includeexpr(ptr, len)
5840 char_u *ptr;
5841 int len;
5842{
5843 char_u *res;
5844
5845 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005846 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005847 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 set_vim_var_string(VV_FNAME, NULL, 0);
5849 return res;
5850}
5851#endif
5852
5853/*
5854 * Return the name of the file ptr[len] in 'path'.
5855 * Otherwise like file_name_at_cursor().
5856 */
5857 char_u *
5858find_file_name_in_path(ptr, len, options, count, rel_fname)
5859 char_u *ptr;
5860 int len;
5861 int options;
5862 long count;
5863 char_u *rel_fname; /* file we are searching relative to */
5864{
5865 char_u *file_name;
5866 int c;
5867# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5868 char_u *tofree = NULL;
5869
5870 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5871 {
5872 tofree = eval_includeexpr(ptr, len);
5873 if (tofree != NULL)
5874 {
5875 ptr = tofree;
5876 len = (int)STRLEN(ptr);
5877 }
5878 }
5879# endif
5880
5881 if (options & FNAME_EXP)
5882 {
5883 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5884 TRUE, rel_fname);
5885
5886# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5887 /*
5888 * If the file could not be found in a normal way, try applying
5889 * 'includeexpr' (unless done already).
5890 */
5891 if (file_name == NULL
5892 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5893 {
5894 tofree = eval_includeexpr(ptr, len);
5895 if (tofree != NULL)
5896 {
5897 ptr = tofree;
5898 len = (int)STRLEN(ptr);
5899 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5900 TRUE, rel_fname);
5901 }
5902 }
5903# endif
5904 if (file_name == NULL && (options & FNAME_MESS))
5905 {
5906 c = ptr[len];
5907 ptr[len] = NUL;
5908 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5909 ptr[len] = c;
5910 }
5911
5912 /* Repeat finding the file "count" times. This matters when it
5913 * appears several times in the path. */
5914 while (file_name != NULL && --count > 0)
5915 {
5916 vim_free(file_name);
5917 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5918 }
5919 }
5920 else
5921 file_name = vim_strnsave(ptr, len);
5922
5923# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5924 vim_free(tofree);
5925# endif
5926
5927 return file_name;
5928}
5929#endif /* FEAT_SEARCHPATH */
5930
5931/*
5932 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5933 * Also check for ":\\", which MS Internet Explorer accepts, return
5934 * URL_BACKSLASH.
5935 */
5936 static int
5937path_is_url(p)
5938 char_u *p;
5939{
5940 if (STRNCMP(p, "://", (size_t)3) == 0)
5941 return URL_SLASH;
5942 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5943 return URL_BACKSLASH;
5944 return 0;
5945}
5946
5947/*
5948 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5949 * Return URL_BACKSLASH for "name:\\".
5950 * Return zero otherwise.
5951 */
5952 int
5953path_with_url(fname)
5954 char_u *fname;
5955{
5956 char_u *p;
5957
5958 for (p = fname; isalpha(*p); ++p)
5959 ;
5960 return path_is_url(p);
5961}
5962
5963/*
5964 * Return TRUE if "name" is a full (absolute) path name or URL.
5965 */
5966 int
5967vim_isAbsName(name)
5968 char_u *name;
5969{
5970 return (path_with_url(name) != 0 || mch_isFullName(name));
5971}
5972
5973/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005974 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 *
5976 * return FAIL for failure, OK otherwise
5977 */
5978 int
5979vim_FullName(fname, buf, len, force)
5980 char_u *fname, *buf;
5981 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005982 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983{
5984 int retval = OK;
5985 int url;
5986
5987 *buf = NUL;
5988 if (fname == NULL)
5989 return FAIL;
5990
5991 url = path_with_url(fname);
5992 if (!url)
5993 retval = mch_FullName(fname, buf, len, force);
5994 if (url || retval == FAIL)
5995 {
5996 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005997 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 }
5999#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6000 slash_adjust(buf);
6001#endif
6002 return retval;
6003}
6004
6005/*
6006 * Return the minimal number of rows that is needed on the screen to display
6007 * the current number of windows.
6008 */
6009 int
6010min_rows()
6011{
6012 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006013#ifdef FEAT_WINDOWS
6014 tabpage_T *tp;
6015 int n;
6016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017
6018 if (firstwin == NULL) /* not initialized yet */
6019 return MIN_LINES;
6020
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006022 total = 0;
6023 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6024 {
6025 n = frame_minheight(tp->tp_topframe, NULL);
6026 if (total < n)
6027 total = n;
6028 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006029 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006031 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006033 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 return total;
6035}
6036
6037/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006038 * Return TRUE if there is only one window (in the current tab page), not
6039 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 */
6041 int
6042only_one_window()
6043{
6044#ifdef FEAT_WINDOWS
6045 int count = 0;
6046 win_T *wp;
6047
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006048 /* If there is another tab page there always is another window. */
6049 if (first_tabpage->tp_next != NULL)
6050 return FALSE;
6051
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00006053 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054# ifdef FEAT_QUICKFIX
6055 || wp->w_p_pvw
6056# endif
6057 ) || wp == curwin)
6058 ++count;
6059 return (count <= 1);
6060#else
6061 return TRUE;
6062#endif
6063}
6064
6065#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6066/*
6067 * Correct the cursor line number in other windows. Used after changing the
6068 * current buffer, and before applying autocommands.
6069 * When "do_curwin" is TRUE, also check current window.
6070 */
6071 void
6072check_lnums(do_curwin)
6073 int do_curwin;
6074{
6075 win_T *wp;
6076
6077#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006078 tabpage_T *tp;
6079
6080 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6082#else
6083 wp = curwin;
6084 if (do_curwin)
6085#endif
6086 {
6087 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6088 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6089 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6090 wp->w_topline = curbuf->b_ml.ml_line_count;
6091 }
6092}
6093#endif
6094
6095#if defined(FEAT_WINDOWS) || defined(PROTO)
6096
6097/*
6098 * A snapshot of the window sizes, to restore them after closing the help
6099 * window.
6100 * Only these fields are used:
6101 * fr_layout
6102 * fr_width
6103 * fr_height
6104 * fr_next
6105 * fr_child
6106 * fr_win (only valid for the old curwin, NULL otherwise)
6107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108
6109/*
6110 * Create a snapshot of the current frame sizes.
6111 */
6112 static void
6113make_snapshot()
6114{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006115 clear_snapshot(curtab);
6116 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117}
6118
6119 static void
6120make_snapshot_rec(fr, frp)
6121 frame_T *fr;
6122 frame_T **frp;
6123{
6124 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6125 if (*frp == NULL)
6126 return;
6127 (*frp)->fr_layout = fr->fr_layout;
6128# ifdef FEAT_VERTSPLIT
6129 (*frp)->fr_width = fr->fr_width;
6130# endif
6131 (*frp)->fr_height = fr->fr_height;
6132 if (fr->fr_next != NULL)
6133 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6134 if (fr->fr_child != NULL)
6135 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6136 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6137 (*frp)->fr_win = curwin;
6138}
6139
6140/*
6141 * Remove any existing snapshot.
6142 */
6143 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006144clear_snapshot(tp)
6145 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006147 clear_snapshot_rec(tp->tp_snapshot);
6148 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149}
6150
6151 static void
6152clear_snapshot_rec(fr)
6153 frame_T *fr;
6154{
6155 if (fr != NULL)
6156 {
6157 clear_snapshot_rec(fr->fr_next);
6158 clear_snapshot_rec(fr->fr_child);
6159 vim_free(fr);
6160 }
6161}
6162
6163/*
6164 * Restore a previously created snapshot, if there is any.
6165 * This is only done if the screen size didn't change and the window layout is
6166 * still the same.
6167 */
6168 static void
6169restore_snapshot(close_curwin)
6170 int close_curwin; /* closing current window */
6171{
6172 win_T *wp;
6173
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006174 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006176 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006178 && curtab->tp_snapshot->fr_height == topframe->fr_height
6179 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006181 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 win_comp_pos();
6183 if (wp != NULL && close_curwin)
6184 win_goto(wp);
6185 redraw_all_later(CLEAR);
6186 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006187 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188}
6189
6190/*
6191 * Check if frames "sn" and "fr" have the same layout, same following frames
6192 * and same children.
6193 */
6194 static int
6195check_snapshot_rec(sn, fr)
6196 frame_T *sn;
6197 frame_T *fr;
6198{
6199 if (sn->fr_layout != fr->fr_layout
6200 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6201 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6202 || (sn->fr_next != NULL
6203 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6204 || (sn->fr_child != NULL
6205 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6206 return FAIL;
6207 return OK;
6208}
6209
6210/*
6211 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6212 * following frames and children.
6213 * Returns a pointer to the old current window, or NULL.
6214 */
6215 static win_T *
6216restore_snapshot_rec(sn, fr)
6217 frame_T *sn;
6218 frame_T *fr;
6219{
6220 win_T *wp = NULL;
6221 win_T *wp2;
6222
6223 fr->fr_height = sn->fr_height;
6224# ifdef FEAT_VERTSPLIT
6225 fr->fr_width = sn->fr_width;
6226# endif
6227 if (fr->fr_layout == FR_LEAF)
6228 {
6229 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6230# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006231 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232# endif
6233 wp = sn->fr_win;
6234 }
6235 if (sn->fr_next != NULL)
6236 {
6237 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6238 if (wp2 != NULL)
6239 wp = wp2;
6240 }
6241 if (sn->fr_child != NULL)
6242 {
6243 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6244 if (wp2 != NULL)
6245 wp = wp2;
6246 }
6247 return wp;
6248}
6249
6250#endif
6251
6252#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6253/*
6254 * Return TRUE if there is any vertically split window.
6255 */
6256 int
6257win_hasvertsplit()
6258{
6259 frame_T *fr;
6260
6261 if (topframe->fr_layout == FR_ROW)
6262 return TRUE;
6263
6264 if (topframe->fr_layout == FR_COL)
6265 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6266 if (fr->fr_layout == FR_ROW)
6267 return TRUE;
6268
6269 return FALSE;
6270}
6271#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006272
6273#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6274/*
6275 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006276 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006277 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6278 * If no particular ID is desired, -1 must be specified for 'id'.
6279 * Return ID of added match, -1 on failure.
6280 */
6281 int
6282match_add(wp, grp, pat, prio, id)
6283 win_T *wp;
6284 char_u *grp;
6285 char_u *pat;
6286 int prio;
6287 int id;
6288{
6289 matchitem_T *cur;
6290 matchitem_T *prev;
6291 matchitem_T *m;
6292 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006293 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006294
6295 if (*grp == NUL || *pat == NUL)
6296 return -1;
6297 if (id < -1 || id == 0)
6298 {
6299 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6300 return -1;
6301 }
6302 if (id != -1)
6303 {
6304 cur = wp->w_match_head;
6305 while (cur != NULL)
6306 {
6307 if (cur->id == id)
6308 {
6309 EMSGN("E801: ID already taken: %ld", id);
6310 return -1;
6311 }
6312 cur = cur->next;
6313 }
6314 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006315 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006316 {
6317 EMSG2(_(e_nogroup), grp);
6318 return -1;
6319 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006320 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006321 {
6322 EMSG2(_(e_invarg2), pat);
6323 return -1;
6324 }
6325
6326 /* Find available match ID. */
6327 while (id == -1)
6328 {
6329 cur = wp->w_match_head;
6330 while (cur != NULL && cur->id != wp->w_next_match_id)
6331 cur = cur->next;
6332 if (cur == NULL)
6333 id = wp->w_next_match_id;
6334 wp->w_next_match_id++;
6335 }
6336
6337 /* Build new match. */
6338 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6339 m->id = id;
6340 m->priority = prio;
6341 m->pattern = vim_strsave(pat);
6342 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006343 m->match.regprog = regprog;
6344 m->match.rmm_ic = FALSE;
6345 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006346
6347 /* Insert new match. The match list is in ascending order with regard to
6348 * the match priorities. */
6349 cur = wp->w_match_head;
6350 prev = cur;
6351 while (cur != NULL && prio >= cur->priority)
6352 {
6353 prev = cur;
6354 cur = cur->next;
6355 }
6356 if (cur == prev)
6357 wp->w_match_head = m;
6358 else
6359 prev->next = m;
6360 m->next = cur;
6361
6362 redraw_later(SOME_VALID);
6363 return id;
6364}
6365
6366/*
6367 * Delete match with ID 'id' in the match list of window 'wp'.
6368 * Print error messages if 'perr' is TRUE.
6369 */
6370 int
6371match_delete(wp, id, perr)
6372 win_T *wp;
6373 int id;
6374 int perr;
6375{
6376 matchitem_T *cur = wp->w_match_head;
6377 matchitem_T *prev = cur;
6378
6379 if (id < 1)
6380 {
6381 if (perr == TRUE)
6382 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6383 id);
6384 return -1;
6385 }
6386 while (cur != NULL && cur->id != id)
6387 {
6388 prev = cur;
6389 cur = cur->next;
6390 }
6391 if (cur == NULL)
6392 {
6393 if (perr == TRUE)
6394 EMSGN("E803: ID not found: %ld", id);
6395 return -1;
6396 }
6397 if (cur == prev)
6398 wp->w_match_head = cur->next;
6399 else
6400 prev->next = cur->next;
6401 vim_free(cur->match.regprog);
6402 vim_free(cur->pattern);
6403 vim_free(cur);
6404 redraw_later(SOME_VALID);
6405 return 0;
6406}
6407
6408/*
6409 * Delete all matches in the match list of window 'wp'.
6410 */
6411 void
6412clear_matches(wp)
6413 win_T *wp;
6414{
6415 matchitem_T *m;
6416
6417 while (wp->w_match_head != NULL)
6418 {
6419 m = wp->w_match_head->next;
6420 vim_free(wp->w_match_head->match.regprog);
6421 vim_free(wp->w_match_head->pattern);
6422 vim_free(wp->w_match_head);
6423 wp->w_match_head = m;
6424 }
6425 redraw_later(SOME_VALID);
6426}
6427
6428/*
6429 * Get match from ID 'id' in window 'wp'.
6430 * Return NULL if match not found.
6431 */
6432 matchitem_T *
6433get_match(wp, id)
6434 win_T *wp;
6435 int id;
6436{
6437 matchitem_T *cur = wp->w_match_head;
6438
6439 while (cur != NULL && cur->id != id)
6440 cur = cur->next;
6441 return cur;
6442}
6443#endif