blob: 81caee932c4a1d228dbf5e807d6188b91c720890 [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 Moolenaar884ae642009-02-22 01:37:59 +000015static void win_init __ARGS((win_T *newp, win_T *oldp, int flags));
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 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 --no_mapping;
599 --allow_keys;
600#ifdef FEAT_CMDL_INFO
601 (void)add_to_showcmd(xchar);
602#endif
603 switch (xchar)
604 {
605#if defined(FEAT_QUICKFIX)
606 case '}':
607 xchar = Ctrl_RSB;
608 if (Prenum)
609 g_do_tagpreview = Prenum;
610 else
611 g_do_tagpreview = p_pvh;
612 /*FALLTHROUGH*/
613#endif
614 case ']':
615 case Ctrl_RSB:
616#ifdef FEAT_VISUAL
617 reset_VIsual_and_resel(); /* stop Visual mode */
618#endif
619 if (Prenum)
620 postponed_split = Prenum;
621 else
622 postponed_split = -1;
623
624 /* Execute the command right here, required when
625 * "wincmd g}" was used in a function. */
626 do_nv_ident('g', xchar);
627 break;
628
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000629#ifdef FEAT_SEARCHPATH
630 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000631 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000632 cmdmod.tab = TRUE;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000633 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000634 goto wingotofile;
635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 default:
637 beep_flush();
638 break;
639 }
640 break;
641
642 default: beep_flush();
643 break;
644 }
645}
646
647/*
648 * split the current window, implements CTRL-W s and :split
649 *
650 * "size" is the height or width for the new window, 0 to use half of current
651 * height or width.
652 *
653 * "flags":
654 * WSP_ROOM: require enough room for new window
655 * WSP_VERT: vertical split.
656 * WSP_TOP: open window at the top-left of the shell (help window).
657 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
658 * WSP_HELP: creating the help window, keep layout snapshot
659 *
660 * return FAIL for failure, OK otherwise
661 */
662 int
663win_split(size, flags)
664 int size;
665 int flags;
666{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000667 /* When the ":tab" modifier was used open a new tab page instead. */
668 if (may_open_tabpage() == OK)
669 return OK;
670
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 /* Add flags from ":vertical", ":topleft" and ":botright". */
672 flags |= cmdmod.split;
673 if ((flags & WSP_TOP) && (flags & WSP_BOT))
674 {
675 EMSG(_("E442: Can't split topleft and botright at the same time"));
676 return FAIL;
677 }
678
679 /* When creating the help window make a snapshot of the window layout.
680 * Otherwise clear the snapshot, it's now invalid. */
681 if (flags & WSP_HELP)
682 make_snapshot();
683 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000684 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685
686 return win_split_ins(size, flags, NULL, 0);
687}
688
689/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000690 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 * When "newwin" is not NULL: insert this window at the far
692 * top/left/right/bottom.
693 * return FAIL for failure, OK otherwise
694 */
695 static int
696win_split_ins(size, flags, newwin, dir)
697 int size;
698 int flags;
699 win_T *newwin;
700 int dir;
701{
702 win_T *wp = newwin;
703 win_T *oldwin;
704 int new_size = size;
705 int i;
706 int need_status = 0;
707 int do_equal = FALSE;
708 int needed;
709 int available;
710 int oldwin_height = 0;
711 int layout;
712 frame_T *frp, *curfrp;
713 int before;
714
715 if (flags & WSP_TOP)
716 oldwin = firstwin;
717 else if (flags & WSP_BOT)
718 oldwin = lastwin;
719 else
720 oldwin = curwin;
721
722 /* add a status line when p_ls == 1 and splitting the first window */
723 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
724 {
725 if (oldwin->w_height <= p_wmh && newwin == NULL)
726 {
727 EMSG(_(e_noroom));
728 return FAIL;
729 }
730 need_status = STATUS_HEIGHT;
731 }
732
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000733#ifdef FEAT_GUI
734 /* May be needed for the scrollbars that are going to change. */
735 if (gui.in_use)
736 out_flush();
737#endif
738
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739#ifdef FEAT_VERTSPLIT
740 if (flags & WSP_VERT)
741 {
742 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744 /*
745 * Check if we are able to split the current window and compute its
746 * width.
747 */
748 needed = p_wmw + 1;
749 if (flags & WSP_ROOM)
750 needed += p_wiw - p_wmw;
751 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
752 {
753 available = topframe->fr_width;
754 needed += frame_minwidth(topframe, NULL);
755 }
756 else
757 available = oldwin->w_width;
758 if (available < needed && newwin == NULL)
759 {
760 EMSG(_(e_noroom));
761 return FAIL;
762 }
763 if (new_size == 0)
764 new_size = oldwin->w_width / 2;
765 if (new_size > oldwin->w_width - p_wmw - 1)
766 new_size = oldwin->w_width - p_wmw - 1;
767 if (new_size < p_wmw)
768 new_size = p_wmw;
769
770 /* if it doesn't fit in the current window, need win_equal() */
771 if (oldwin->w_width - new_size - 1 < p_wmw)
772 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000773
774 /* We don't like to take lines for the new window from a
775 * 'winfixwidth' window. Take them from a window to the left or right
776 * instead, if possible. */
777 if (oldwin->w_p_wfw)
778 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000779
780 /* Only make all windows the same width if one of them (except oldwin)
781 * is wider than one of the split windows. */
782 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
783 && oldwin->w_frame->fr_parent != NULL)
784 {
785 frp = oldwin->w_frame->fr_parent->fr_child;
786 while (frp != NULL)
787 {
788 if (frp->fr_win != oldwin && frp->fr_win != NULL
789 && (frp->fr_win->w_width > new_size
790 || frp->fr_win->w_width > oldwin->w_width
791 - new_size - STATUS_HEIGHT))
792 {
793 do_equal = TRUE;
794 break;
795 }
796 frp = frp->fr_next;
797 }
798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 }
800 else
801#endif
802 {
803 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
805 /*
806 * Check if we are able to split the current window and compute its
807 * height.
808 */
809 needed = p_wmh + STATUS_HEIGHT + need_status;
810 if (flags & WSP_ROOM)
811 needed += p_wh - p_wmh;
812 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
813 {
814 available = topframe->fr_height;
815 needed += frame_minheight(topframe, NULL);
816 }
817 else
818 {
819 available = oldwin->w_height;
820 needed += p_wmh;
821 }
822 if (available < needed && newwin == NULL)
823 {
824 EMSG(_(e_noroom));
825 return FAIL;
826 }
827 oldwin_height = oldwin->w_height;
828 if (need_status)
829 {
830 oldwin->w_status_height = STATUS_HEIGHT;
831 oldwin_height -= STATUS_HEIGHT;
832 }
833 if (new_size == 0)
834 new_size = oldwin_height / 2;
835
836 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
837 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
838 if (new_size < p_wmh)
839 new_size = p_wmh;
840
841 /* if it doesn't fit in the current window, need win_equal() */
842 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
843 do_equal = TRUE;
844
845 /* We don't like to take lines for the new window from a
846 * 'winfixheight' window. Take them from a window above or below
847 * instead, if possible. */
848 if (oldwin->w_p_wfh)
849 {
850 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
851 oldwin);
852 oldwin_height = oldwin->w_height;
853 if (need_status)
854 oldwin_height -= STATUS_HEIGHT;
855 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000856
857 /* Only make all windows the same height if one of them (except oldwin)
858 * is higher than one of the split windows. */
859 if (!do_equal && p_ea && size == 0
860#ifdef FEAT_VERTSPLIT
861 && *p_ead != 'h'
862#endif
863 && oldwin->w_frame->fr_parent != NULL)
864 {
865 frp = oldwin->w_frame->fr_parent->fr_child;
866 while (frp != NULL)
867 {
868 if (frp->fr_win != oldwin && frp->fr_win != NULL
869 && (frp->fr_win->w_height > new_size
870 || frp->fr_win->w_height > oldwin_height - new_size
871 - STATUS_HEIGHT))
872 {
873 do_equal = TRUE;
874 break;
875 }
876 frp = frp->fr_next;
877 }
878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 }
880
881 /*
882 * allocate new window structure and link it in the window list
883 */
884 if ((flags & WSP_TOP) == 0
885 && ((flags & WSP_BOT)
886 || (flags & WSP_BELOW)
887 || (!(flags & WSP_ABOVE)
888 && (
889#ifdef FEAT_VERTSPLIT
890 (flags & WSP_VERT) ? p_spr :
891#endif
892 p_sb))))
893 {
894 /* new window below/right of current one */
895 if (newwin == NULL)
896 wp = win_alloc(oldwin);
897 else
898 win_append(oldwin, wp);
899 }
900 else
901 {
902 if (newwin == NULL)
903 wp = win_alloc(oldwin->w_prev);
904 else
905 win_append(oldwin->w_prev, wp);
906 }
907
908 if (newwin == NULL)
909 {
910 if (wp == NULL)
911 return FAIL;
912
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000913 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000914 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 }
916
917 /*
918 * Reorganise the tree of frames to insert the new window.
919 */
920 if (flags & (WSP_TOP | WSP_BOT))
921 {
922#ifdef FEAT_VERTSPLIT
923 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
924 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
925#else
926 if (topframe->fr_layout == FR_COL)
927#endif
928 {
929 curfrp = topframe->fr_child;
930 if (flags & WSP_BOT)
931 while (curfrp->fr_next != NULL)
932 curfrp = curfrp->fr_next;
933 }
934 else
935 curfrp = topframe;
936 before = (flags & WSP_TOP);
937 }
938 else
939 {
940 curfrp = oldwin->w_frame;
941 if (flags & WSP_BELOW)
942 before = FALSE;
943 else if (flags & WSP_ABOVE)
944 before = TRUE;
945 else
946#ifdef FEAT_VERTSPLIT
947 if (flags & WSP_VERT)
948 before = !p_spr;
949 else
950#endif
951 before = !p_sb;
952 }
953 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
954 {
955 /* Need to create a new frame in the tree to make a branch. */
956 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
957 *frp = *curfrp;
958 curfrp->fr_layout = layout;
959 frp->fr_parent = curfrp;
960 frp->fr_next = NULL;
961 frp->fr_prev = NULL;
962 curfrp->fr_child = frp;
963 curfrp->fr_win = NULL;
964 curfrp = frp;
965 if (frp->fr_win != NULL)
966 oldwin->w_frame = frp;
967 else
968 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
969 frp->fr_parent = curfrp;
970 }
971
972 if (newwin == NULL)
973 {
974 /* Create a frame for the new window. */
975 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
976 frp->fr_layout = FR_LEAF;
977 frp->fr_win = wp;
978 wp->w_frame = frp;
979 }
980 else
981 frp = newwin->w_frame;
982 frp->fr_parent = curfrp->fr_parent;
983
984 /* Insert the new frame at the right place in the frame list. */
985 if (before)
986 frame_insert(curfrp, frp);
987 else
988 frame_append(curfrp, frp);
989
990#ifdef FEAT_VERTSPLIT
991 if (flags & WSP_VERT)
992 {
993 wp->w_p_scr = curwin->w_p_scr;
994 if (need_status)
995 {
996 --oldwin->w_height;
997 oldwin->w_status_height = need_status;
998 }
999 if (flags & (WSP_TOP | WSP_BOT))
1000 {
1001 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001002 wp->w_winrow = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 wp->w_height = curfrp->fr_height - (p_ls > 0);
1004 wp->w_status_height = (p_ls > 0);
1005 }
1006 else
1007 {
1008 /* height and row of new window is same as current window */
1009 wp->w_winrow = oldwin->w_winrow;
1010 wp->w_height = oldwin->w_height;
1011 wp->w_status_height = oldwin->w_status_height;
1012 }
1013 frp->fr_height = curfrp->fr_height;
1014
1015 /* "new_size" of the current window goes to the new window, use
1016 * one column for the vertical separator */
1017 wp->w_width = new_size;
1018 if (before)
1019 wp->w_vsep_width = 1;
1020 else
1021 {
1022 wp->w_vsep_width = oldwin->w_vsep_width;
1023 oldwin->w_vsep_width = 1;
1024 }
1025 if (flags & (WSP_TOP | WSP_BOT))
1026 {
1027 if (flags & WSP_BOT)
1028 frame_add_vsep(curfrp);
1029 /* Set width of neighbor frame */
1030 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001031 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1032 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 }
1034 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001035 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 if (before) /* new window left of current one */
1037 {
1038 wp->w_wincol = oldwin->w_wincol;
1039 oldwin->w_wincol += new_size + 1;
1040 }
1041 else /* new window right of current one */
1042 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1043 frame_fix_width(oldwin);
1044 frame_fix_width(wp);
1045 }
1046 else
1047#endif
1048 {
1049 /* width and column of new window is same as current window */
1050#ifdef FEAT_VERTSPLIT
1051 if (flags & (WSP_TOP | WSP_BOT))
1052 {
1053 wp->w_wincol = 0;
1054 wp->w_width = Columns;
1055 wp->w_vsep_width = 0;
1056 }
1057 else
1058 {
1059 wp->w_wincol = oldwin->w_wincol;
1060 wp->w_width = oldwin->w_width;
1061 wp->w_vsep_width = oldwin->w_vsep_width;
1062 }
1063 frp->fr_width = curfrp->fr_width;
1064#endif
1065
1066 /* "new_size" of the current window goes to the new window, use
1067 * one row for the status line */
1068 win_new_height(wp, new_size);
1069 if (flags & (WSP_TOP | WSP_BOT))
1070 frame_new_height(curfrp, curfrp->fr_height
1071 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1072 else
1073 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1074 if (before) /* new window above current one */
1075 {
1076 wp->w_winrow = oldwin->w_winrow;
1077 wp->w_status_height = STATUS_HEIGHT;
1078 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1079 }
1080 else /* new window below current one */
1081 {
1082 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1083 wp->w_status_height = oldwin->w_status_height;
1084 oldwin->w_status_height = STATUS_HEIGHT;
1085 }
1086#ifdef FEAT_VERTSPLIT
1087 if (flags & WSP_BOT)
1088 frame_add_statusline(curfrp);
1089#endif
1090 frame_fix_height(wp);
1091 frame_fix_height(oldwin);
1092 }
1093
1094 if (flags & (WSP_TOP | WSP_BOT))
1095 (void)win_comp_pos();
1096
1097 /*
1098 * Both windows need redrawing
1099 */
1100 redraw_win_later(wp, NOT_VALID);
1101 wp->w_redr_status = TRUE;
1102 redraw_win_later(oldwin, NOT_VALID);
1103 oldwin->w_redr_status = TRUE;
1104
1105 if (need_status)
1106 {
1107 msg_row = Rows - 1;
1108 msg_col = sc_col;
1109 msg_clr_eos_force(); /* Old command/ruler may still be there */
1110 comp_col();
1111 msg_row = Rows - 1;
1112 msg_col = 0; /* put position back at start of line */
1113 }
1114
1115 /*
1116 * make the new window the current window and redraw
1117 */
1118 if (do_equal || dir != 0)
1119 win_equal(wp, TRUE,
1120#ifdef FEAT_VERTSPLIT
1121 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1122 : dir == 'h' ? 'b' :
1123#endif
1124 'v');
1125
1126 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1127 * size was given. */
1128#ifdef FEAT_VERTSPLIT
1129 if (flags & WSP_VERT)
1130 {
1131 i = p_wiw;
1132 if (size != 0)
1133 p_wiw = size;
1134
1135# ifdef FEAT_GUI
1136 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1137 if (gui.in_use)
1138 gui_init_which_components(NULL);
1139# endif
1140 }
1141 else
1142#endif
1143 {
1144 i = p_wh;
1145 if (size != 0)
1146 p_wh = size;
1147 }
1148 win_enter(wp, FALSE);
1149#ifdef FEAT_VERTSPLIT
1150 if (flags & WSP_VERT)
1151 p_wiw = i;
1152 else
1153#endif
1154 p_wh = i;
1155
1156 return OK;
1157}
1158
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001159/*
1160 * Initialize window "newp" from window "oldp".
1161 * Used when splitting a window and when creating a new tab page.
1162 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001163 * WSP_NEWLOC may be specified in flags to prevent the location list from
1164 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001165 */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001166/*ARGSUSED*/
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001167 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001168win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001169 win_T *newp;
1170 win_T *oldp;
Bram Moolenaar884ae642009-02-22 01:37:59 +00001171 int flags;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001172{
1173 int i;
1174
1175 newp->w_buffer = oldp->w_buffer;
1176 oldp->w_buffer->b_nwindows++;
1177 newp->w_cursor = oldp->w_cursor;
1178 newp->w_valid = 0;
1179 newp->w_curswant = oldp->w_curswant;
1180 newp->w_set_curswant = oldp->w_set_curswant;
1181 newp->w_topline = oldp->w_topline;
1182#ifdef FEAT_DIFF
1183 newp->w_topfill = oldp->w_topfill;
1184#endif
1185 newp->w_leftcol = oldp->w_leftcol;
1186 newp->w_pcmark = oldp->w_pcmark;
1187 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1188 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001189 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001190 newp->w_fraction = oldp->w_fraction;
1191 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1192#ifdef FEAT_JUMPLIST
1193 copy_jumplist(oldp, newp);
1194#endif
1195#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001196 if (flags & WSP_NEWLOC)
1197 {
1198 /* Don't copy the location list. */
1199 newp->w_llist = NULL;
1200 newp->w_llist_ref = NULL;
1201 }
1202 else
1203 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001204#endif
1205 if (oldp->w_localdir != NULL)
1206 newp->w_localdir = vim_strsave(oldp->w_localdir);
1207
1208 /* Use the same argument list. */
1209 newp->w_alist = oldp->w_alist;
1210 ++newp->w_alist->al_refcount;
1211 newp->w_arg_idx = oldp->w_arg_idx;
1212
1213 /*
1214 * copy tagstack and options from existing window
1215 */
1216 for (i = 0; i < oldp->w_tagstacklen; i++)
1217 {
1218 newp->w_tagstack[i] = oldp->w_tagstack[i];
1219 if (newp->w_tagstack[i].tagname != NULL)
1220 newp->w_tagstack[i].tagname =
1221 vim_strsave(newp->w_tagstack[i].tagname);
1222 }
1223 newp->w_tagstackidx = oldp->w_tagstackidx;
1224 newp->w_tagstacklen = oldp->w_tagstacklen;
1225 win_copy_options(oldp, newp);
1226# ifdef FEAT_FOLDING
1227 copyFoldingState(oldp, newp);
1228# endif
1229}
1230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231#endif /* FEAT_WINDOWS */
1232
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233#if defined(FEAT_WINDOWS) || defined(PROTO)
1234/*
1235 * Check if "win" is a pointer to an existing window.
1236 */
1237 int
1238win_valid(win)
1239 win_T *win;
1240{
1241 win_T *wp;
1242
1243 if (win == NULL)
1244 return FALSE;
1245 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1246 if (wp == win)
1247 return TRUE;
1248 return FALSE;
1249}
1250
1251/*
1252 * Return the number of windows.
1253 */
1254 int
1255win_count()
1256{
1257 win_T *wp;
1258 int count = 0;
1259
1260 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1261 ++count;
1262 return count;
1263}
1264
1265/*
1266 * Make "count" windows on the screen.
1267 * Return actual number of windows on the screen.
1268 * Must be called when there is just one window, filling the whole screen
1269 * (excluding the command line).
1270 */
1271/*ARGSUSED*/
1272 int
1273make_windows(count, vertical)
1274 int count;
1275 int vertical; /* split windows vertically if TRUE */
1276{
1277 int maxcount;
1278 int todo;
1279
1280#ifdef FEAT_VERTSPLIT
1281 if (vertical)
1282 {
1283 /* Each windows needs at least 'winminwidth' lines and a separator
1284 * column. */
1285 maxcount = (curwin->w_width + curwin->w_vsep_width
1286 - (p_wiw - p_wmw)) / (p_wmw + 1);
1287 }
1288 else
1289#endif
1290 {
1291 /* Each window needs at least 'winminheight' lines and a status line. */
1292 maxcount = (curwin->w_height + curwin->w_status_height
1293 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1294 }
1295
1296 if (maxcount < 2)
1297 maxcount = 2;
1298 if (count > maxcount)
1299 count = maxcount;
1300
1301 /*
1302 * add status line now, otherwise first window will be too big
1303 */
1304 if (count > 1)
1305 last_status(TRUE);
1306
1307#ifdef FEAT_AUTOCMD
1308 /*
1309 * Don't execute autocommands while creating the windows. Must do that
1310 * when putting the buffers in the windows.
1311 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001312 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313#endif
1314
1315 /* todo is number of windows left to create */
1316 for (todo = count - 1; todo > 0; --todo)
1317#ifdef FEAT_VERTSPLIT
1318 if (vertical)
1319 {
1320 if (win_split(curwin->w_width - (curwin->w_width - todo)
1321 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1322 break;
1323 }
1324 else
1325#endif
1326 {
1327 if (win_split(curwin->w_height - (curwin->w_height - todo
1328 * STATUS_HEIGHT) / (todo + 1)
1329 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1330 break;
1331 }
1332
1333#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001334 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335#endif
1336
1337 /* return actual number of windows */
1338 return (count - todo);
1339}
1340
1341/*
1342 * Exchange current and next window
1343 */
1344 static void
1345win_exchange(Prenum)
1346 long Prenum;
1347{
1348 frame_T *frp;
1349 frame_T *frp2;
1350 win_T *wp;
1351 win_T *wp2;
1352 int temp;
1353
1354 if (lastwin == firstwin) /* just one window */
1355 {
1356 beep_flush();
1357 return;
1358 }
1359
1360#ifdef FEAT_GUI
1361 need_mouse_correct = TRUE;
1362#endif
1363
1364 /*
1365 * find window to exchange with
1366 */
1367 if (Prenum)
1368 {
1369 frp = curwin->w_frame->fr_parent->fr_child;
1370 while (frp != NULL && --Prenum > 0)
1371 frp = frp->fr_next;
1372 }
1373 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1374 frp = curwin->w_frame->fr_next;
1375 else /* Swap last window in row/col with previous */
1376 frp = curwin->w_frame->fr_prev;
1377
1378 /* We can only exchange a window with another window, not with a frame
1379 * containing windows. */
1380 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1381 return;
1382 wp = frp->fr_win;
1383
1384/*
1385 * 1. remove curwin from the list. Remember after which window it was in wp2
1386 * 2. insert curwin before wp in the list
1387 * if wp != wp2
1388 * 3. remove wp from the list
1389 * 4. insert wp after wp2
1390 * 5. exchange the status line height and vsep width.
1391 */
1392 wp2 = curwin->w_prev;
1393 frp2 = curwin->w_frame->fr_prev;
1394 if (wp->w_prev != curwin)
1395 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001396 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 frame_remove(curwin->w_frame);
1398 win_append(wp->w_prev, curwin);
1399 frame_insert(frp, curwin->w_frame);
1400 }
1401 if (wp != wp2)
1402 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001403 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 frame_remove(wp->w_frame);
1405 win_append(wp2, wp);
1406 if (frp2 == NULL)
1407 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1408 else
1409 frame_append(frp2, wp->w_frame);
1410 }
1411 temp = curwin->w_status_height;
1412 curwin->w_status_height = wp->w_status_height;
1413 wp->w_status_height = temp;
1414#ifdef FEAT_VERTSPLIT
1415 temp = curwin->w_vsep_width;
1416 curwin->w_vsep_width = wp->w_vsep_width;
1417 wp->w_vsep_width = temp;
1418
1419 /* If the windows are not in the same frame, exchange the sizes to avoid
1420 * messing up the window layout. Otherwise fix the frame sizes. */
1421 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1422 {
1423 temp = curwin->w_height;
1424 curwin->w_height = wp->w_height;
1425 wp->w_height = temp;
1426 temp = curwin->w_width;
1427 curwin->w_width = wp->w_width;
1428 wp->w_width = temp;
1429 }
1430 else
1431 {
1432 frame_fix_height(curwin);
1433 frame_fix_height(wp);
1434 frame_fix_width(curwin);
1435 frame_fix_width(wp);
1436 }
1437#endif
1438
1439 (void)win_comp_pos(); /* recompute window positions */
1440
1441 win_enter(wp, TRUE);
1442 redraw_later(CLEAR);
1443}
1444
1445/*
1446 * rotate windows: if upwards TRUE the second window becomes the first one
1447 * if upwards FALSE the first window becomes the second one
1448 */
1449 static void
1450win_rotate(upwards, count)
1451 int upwards;
1452 int count;
1453{
1454 win_T *wp1;
1455 win_T *wp2;
1456 frame_T *frp;
1457 int n;
1458
1459 if (firstwin == lastwin) /* nothing to do */
1460 {
1461 beep_flush();
1462 return;
1463 }
1464
1465#ifdef FEAT_GUI
1466 need_mouse_correct = TRUE;
1467#endif
1468
1469#ifdef FEAT_VERTSPLIT
1470 /* Check if all frames in this row/col have one window. */
1471 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1472 frp = frp->fr_next)
1473 if (frp->fr_win == NULL)
1474 {
1475 EMSG(_("E443: Cannot rotate when another window is split"));
1476 return;
1477 }
1478#endif
1479
1480 while (count--)
1481 {
1482 if (upwards) /* first window becomes last window */
1483 {
1484 /* remove first window/frame from the list */
1485 frp = curwin->w_frame->fr_parent->fr_child;
1486 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001487 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 frame_remove(frp);
1489
1490 /* find last frame and append removed window/frame after it */
1491 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1492 ;
1493 win_append(frp->fr_win, wp1);
1494 frame_append(frp, wp1->w_frame);
1495
1496 wp2 = frp->fr_win; /* previously last window */
1497 }
1498 else /* last window becomes first window */
1499 {
1500 /* find last window/frame in the list and remove it */
1501 for (frp = curwin->w_frame; frp->fr_next != NULL;
1502 frp = frp->fr_next)
1503 ;
1504 wp1 = frp->fr_win;
1505 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001506 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 frame_remove(frp);
1508
1509 /* append the removed window/frame before the first in the list */
1510 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1511 frame_insert(frp->fr_parent->fr_child, frp);
1512 }
1513
1514 /* exchange status height and vsep width of old and new last window */
1515 n = wp2->w_status_height;
1516 wp2->w_status_height = wp1->w_status_height;
1517 wp1->w_status_height = n;
1518 frame_fix_height(wp1);
1519 frame_fix_height(wp2);
1520#ifdef FEAT_VERTSPLIT
1521 n = wp2->w_vsep_width;
1522 wp2->w_vsep_width = wp1->w_vsep_width;
1523 wp1->w_vsep_width = n;
1524 frame_fix_width(wp1);
1525 frame_fix_width(wp2);
1526#endif
1527
1528 /* recompute w_winrow and w_wincol for all windows */
1529 (void)win_comp_pos();
1530 }
1531
1532 redraw_later(CLEAR);
1533}
1534
1535/*
1536 * Move the current window to the very top/bottom/left/right of the screen.
1537 */
1538 static void
1539win_totop(size, flags)
1540 int size;
1541 int flags;
1542{
1543 int dir;
1544 int height = curwin->w_height;
1545
1546 if (lastwin == firstwin)
1547 {
1548 beep_flush();
1549 return;
1550 }
1551
1552 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001553 (void)winframe_remove(curwin, &dir, NULL);
1554 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 last_status(FALSE); /* may need to remove last status line */
1556 (void)win_comp_pos(); /* recompute window positions */
1557
1558 /* Split a window on the desired side and put the window there. */
1559 (void)win_split_ins(size, flags, curwin, dir);
1560 if (!(flags & WSP_VERT))
1561 {
1562 win_setheight(height);
1563 if (p_ea)
1564 win_equal(curwin, TRUE, 'v');
1565 }
1566
1567#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1568 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1569 * scrollbars. Have to update them anyway. */
1570 if (gui.in_use)
1571 {
1572 out_flush();
1573 gui_init_which_components(NULL);
1574 gui_update_scrollbars(TRUE);
1575 }
1576 need_mouse_correct = TRUE;
1577#endif
1578
1579}
1580
1581/*
1582 * Move window "win1" to below/right of "win2" and make "win1" the current
1583 * window. Only works within the same frame!
1584 */
1585 void
1586win_move_after(win1, win2)
1587 win_T *win1, *win2;
1588{
1589 int height;
1590
1591 /* check if the arguments are reasonable */
1592 if (win1 == win2)
1593 return;
1594
1595 /* check if there is something to do */
1596 if (win2->w_next != win1)
1597 {
1598 /* may need move the status line/vertical separator of the last window
1599 * */
1600 if (win1 == lastwin)
1601 {
1602 height = win1->w_prev->w_status_height;
1603 win1->w_prev->w_status_height = win1->w_status_height;
1604 win1->w_status_height = height;
1605#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001606 if (win1->w_prev->w_vsep_width == 1)
1607 {
1608 /* Remove the vertical separator from the last-but-one window,
1609 * add it to the last window. Adjust the frame widths. */
1610 win1->w_prev->w_vsep_width = 0;
1611 win1->w_prev->w_frame->fr_width -= 1;
1612 win1->w_vsep_width = 1;
1613 win1->w_frame->fr_width += 1;
1614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615#endif
1616 }
1617 else if (win2 == lastwin)
1618 {
1619 height = win1->w_status_height;
1620 win1->w_status_height = win2->w_status_height;
1621 win2->w_status_height = height;
1622#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001623 if (win1->w_vsep_width == 1)
1624 {
1625 /* Remove the vertical separator from win1, add it to the last
1626 * window, win2. Adjust the frame widths. */
1627 win2->w_vsep_width = 1;
1628 win2->w_frame->fr_width += 1;
1629 win1->w_vsep_width = 0;
1630 win1->w_frame->fr_width -= 1;
1631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632#endif
1633 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001634 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 frame_remove(win1->w_frame);
1636 win_append(win2, win1);
1637 frame_append(win2->w_frame, win1->w_frame);
1638
1639 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1640 redraw_later(NOT_VALID);
1641 }
1642 win_enter(win1, FALSE);
1643}
1644
1645/*
1646 * Make all windows the same height.
1647 * 'next_curwin' will soon be the current window, make sure it has enough
1648 * rows.
1649 */
1650 void
1651win_equal(next_curwin, current, dir)
1652 win_T *next_curwin; /* pointer to current window to be or NULL */
1653 int current; /* do only frame with current window */
1654 int dir; /* 'v' for vertically, 'h' for horizontally,
1655 'b' for both, 0 for using p_ead */
1656{
1657 if (dir == 0)
1658#ifdef FEAT_VERTSPLIT
1659 dir = *p_ead;
1660#else
1661 dir = 'b';
1662#endif
1663 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001664 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001665 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666}
1667
1668/*
1669 * Set a frame to a new position and height, spreading the available room
1670 * equally over contained frames.
1671 * The window "next_curwin" (if not NULL) should at least get the size from
1672 * 'winheight' and 'winwidth' if possible.
1673 */
1674 static void
1675win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1676 win_T *next_curwin; /* pointer to current window to be or NULL */
1677 int current; /* do only frame with current window */
1678 frame_T *topfr; /* frame to set size off */
1679 int dir; /* 'v', 'h' or 'b', see win_equal() */
1680 int col; /* horizontal position for frame */
1681 int row; /* vertical position for frame */
1682 int width; /* new width of frame */
1683 int height; /* new height of frame */
1684{
1685 int n, m;
1686 int extra_sep = 0;
1687 int wincount, totwincount = 0;
1688 frame_T *fr;
1689 int next_curwin_size = 0;
1690 int room = 0;
1691 int new_size;
1692 int has_next_curwin = 0;
1693 int hnc;
1694
1695 if (topfr->fr_layout == FR_LEAF)
1696 {
1697 /* Set the width/height of this frame.
1698 * Redraw when size or position changes */
1699 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1700#ifdef FEAT_VERTSPLIT
1701 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1702#endif
1703 )
1704 {
1705 topfr->fr_win->w_winrow = row;
1706 frame_new_height(topfr, height, FALSE, FALSE);
1707#ifdef FEAT_VERTSPLIT
1708 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001709 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710#endif
1711 redraw_all_later(CLEAR);
1712 }
1713 }
1714#ifdef FEAT_VERTSPLIT
1715 else if (topfr->fr_layout == FR_ROW)
1716 {
1717 topfr->fr_width = width;
1718 topfr->fr_height = height;
1719
1720 if (dir != 'v') /* equalize frame widths */
1721 {
1722 /* Compute the maximum number of windows horizontally in this
1723 * frame. */
1724 n = frame_minwidth(topfr, NOWIN);
1725 /* add one for the rightmost window, it doesn't have a separator */
1726 if (col + width == Columns)
1727 extra_sep = 1;
1728 else
1729 extra_sep = 0;
1730 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001731 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001733 /*
1734 * Compute width for "next_curwin" window and room available for
1735 * other windows.
1736 * "m" is the minimal width when counting p_wiw for "next_curwin".
1737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 m = frame_minwidth(topfr, next_curwin);
1739 room = width - m;
1740 if (room < 0)
1741 {
1742 next_curwin_size = p_wiw + room;
1743 room = 0;
1744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 else
1746 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001747 next_curwin_size = -1;
1748 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1749 {
1750 /* If 'winfixwidth' set keep the window width if
1751 * possible.
1752 * Watch out for this window being the next_curwin. */
1753 if (frame_fixed_width(fr))
1754 {
1755 n = frame_minwidth(fr, NOWIN);
1756 new_size = fr->fr_width;
1757 if (frame_has_win(fr, next_curwin))
1758 {
1759 room += p_wiw - p_wmw;
1760 next_curwin_size = 0;
1761 if (new_size < p_wiw)
1762 new_size = p_wiw;
1763 }
1764 else
1765 /* These windows don't use up room. */
1766 totwincount -= (n + (fr->fr_next == NULL
1767 ? extra_sep : 0)) / (p_wmw + 1);
1768 room -= new_size - n;
1769 if (room < 0)
1770 {
1771 new_size += room;
1772 room = 0;
1773 }
1774 fr->fr_newwidth = new_size;
1775 }
1776 }
1777 if (next_curwin_size == -1)
1778 {
1779 if (!has_next_curwin)
1780 next_curwin_size = 0;
1781 else if (totwincount > 1
1782 && (room + (totwincount - 2))
1783 / (totwincount - 1) > p_wiw)
1784 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001785 /* Can make all windows wider than 'winwidth', spread
1786 * the room equally. */
1787 next_curwin_size = (room + p_wiw
1788 + (totwincount - 1) * p_wmw
1789 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001790 room -= next_curwin_size - p_wiw;
1791 }
1792 else
1793 next_curwin_size = p_wiw;
1794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001796
1797 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 --totwincount; /* don't count curwin */
1799 }
1800
1801 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1802 {
1803 n = m = 0;
1804 wincount = 1;
1805 if (fr->fr_next == NULL)
1806 /* last frame gets all that remains (avoid roundoff error) */
1807 new_size = width;
1808 else if (dir == 'v')
1809 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001810 else if (frame_fixed_width(fr))
1811 {
1812 new_size = fr->fr_newwidth;
1813 wincount = 0; /* doesn't count as a sizeable window */
1814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 else
1816 {
1817 /* Compute the maximum number of windows horiz. in "fr". */
1818 n = frame_minwidth(fr, NOWIN);
1819 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1820 / (p_wmw + 1);
1821 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001822 if (has_next_curwin)
1823 hnc = frame_has_win(fr, next_curwin);
1824 else
1825 hnc = FALSE;
1826 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001828 if (totwincount == 0)
1829 new_size = room;
1830 else
1831 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001833 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 {
1835 next_curwin_size -= p_wiw - (m - n);
1836 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001837 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001839 else
1840 room -= new_size;
1841 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 }
1843
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001844 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 * window, unless equalizing all frames. */
1846 if (!current || dir != 'v' || topfr->fr_parent != NULL
1847 || (new_size != fr->fr_width)
1848 || frame_has_win(fr, next_curwin))
1849 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001850 new_size, height);
1851 col += new_size;
1852 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 totwincount -= wincount;
1854 }
1855 }
1856#endif
1857 else /* topfr->fr_layout == FR_COL */
1858 {
1859#ifdef FEAT_VERTSPLIT
1860 topfr->fr_width = width;
1861#endif
1862 topfr->fr_height = height;
1863
1864 if (dir != 'h') /* equalize frame heights */
1865 {
1866 /* Compute maximum number of windows vertically in this frame. */
1867 n = frame_minheight(topfr, NOWIN);
1868 /* add one for the bottom window if it doesn't have a statusline */
1869 if (row + height == cmdline_row && p_ls == 0)
1870 extra_sep = 1;
1871 else
1872 extra_sep = 0;
1873 totwincount = (n + extra_sep) / (p_wmh + 1);
1874 has_next_curwin = frame_has_win(topfr, next_curwin);
1875
1876 /*
1877 * Compute height for "next_curwin" window and room available for
1878 * other windows.
1879 * "m" is the minimal height when counting p_wh for "next_curwin".
1880 */
1881 m = frame_minheight(topfr, next_curwin);
1882 room = height - m;
1883 if (room < 0)
1884 {
1885 /* The room is less then 'winheight', use all space for the
1886 * current window. */
1887 next_curwin_size = p_wh + room;
1888 room = 0;
1889 }
1890 else
1891 {
1892 next_curwin_size = -1;
1893 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1894 {
1895 /* If 'winfixheight' set keep the window height if
1896 * possible.
1897 * Watch out for this window being the next_curwin. */
1898 if (frame_fixed_height(fr))
1899 {
1900 n = frame_minheight(fr, NOWIN);
1901 new_size = fr->fr_height;
1902 if (frame_has_win(fr, next_curwin))
1903 {
1904 room += p_wh - p_wmh;
1905 next_curwin_size = 0;
1906 if (new_size < p_wh)
1907 new_size = p_wh;
1908 }
1909 else
1910 /* These windows don't use up room. */
1911 totwincount -= (n + (fr->fr_next == NULL
1912 ? extra_sep : 0)) / (p_wmh + 1);
1913 room -= new_size - n;
1914 if (room < 0)
1915 {
1916 new_size += room;
1917 room = 0;
1918 }
1919 fr->fr_newheight = new_size;
1920 }
1921 }
1922 if (next_curwin_size == -1)
1923 {
1924 if (!has_next_curwin)
1925 next_curwin_size = 0;
1926 else if (totwincount > 1
1927 && (room + (totwincount - 2))
1928 / (totwincount - 1) > p_wh)
1929 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001930 /* can make all windows higher than 'winheight',
1931 * spread the room equally. */
1932 next_curwin_size = (room + p_wh
1933 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 + (totwincount - 1)) / totwincount;
1935 room -= next_curwin_size - p_wh;
1936 }
1937 else
1938 next_curwin_size = p_wh;
1939 }
1940 }
1941
1942 if (has_next_curwin)
1943 --totwincount; /* don't count curwin */
1944 }
1945
1946 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1947 {
1948 n = m = 0;
1949 wincount = 1;
1950 if (fr->fr_next == NULL)
1951 /* last frame gets all that remains (avoid roundoff error) */
1952 new_size = height;
1953 else if (dir == 'h')
1954 new_size = fr->fr_height;
1955 else if (frame_fixed_height(fr))
1956 {
1957 new_size = fr->fr_newheight;
1958 wincount = 0; /* doesn't count as a sizeable window */
1959 }
1960 else
1961 {
1962 /* Compute the maximum number of windows vert. in "fr". */
1963 n = frame_minheight(fr, NOWIN);
1964 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1965 / (p_wmh + 1);
1966 m = frame_minheight(fr, next_curwin);
1967 if (has_next_curwin)
1968 hnc = frame_has_win(fr, next_curwin);
1969 else
1970 hnc = FALSE;
1971 if (hnc) /* don't count next_curwin */
1972 --wincount;
1973 if (totwincount == 0)
1974 new_size = room;
1975 else
1976 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1977 / totwincount;
1978 if (hnc) /* add next_curwin size */
1979 {
1980 next_curwin_size -= p_wh - (m - n);
1981 new_size += next_curwin_size;
1982 room -= new_size - next_curwin_size;
1983 }
1984 else
1985 room -= new_size;
1986 new_size += n;
1987 }
1988 /* Skip frame that is full width when splitting or closing a
1989 * window, unless equalizing all frames. */
1990 if (!current || dir != 'h' || topfr->fr_parent != NULL
1991 || (new_size != fr->fr_height)
1992 || frame_has_win(fr, next_curwin))
1993 win_equal_rec(next_curwin, current, fr, dir, col, row,
1994 width, new_size);
1995 row += new_size;
1996 height -= new_size;
1997 totwincount -= wincount;
1998 }
1999 }
2000}
2001
2002/*
2003 * close all windows for buffer 'buf'
2004 */
2005 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002006close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002008 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002010 win_T *wp;
2011 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002012 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013
2014 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002015
2016 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002018 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002020 win_close(wp, FALSE);
2021
2022 /* Start all over, autocommands may change the window layout. */
2023 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 }
2025 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002026 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002028
2029 /* Also check windows in other tab pages. */
2030 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2031 {
2032 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002033 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002034 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2035 if (wp->w_buffer == buf)
2036 {
2037 win_close_othertab(wp, FALSE, tp);
2038
2039 /* Start all over, the tab page may be closed and
2040 * autocommands may change the window layout. */
2041 nexttp = first_tabpage;
2042 break;
2043 }
2044 }
2045
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002047
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002048 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002049 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050}
2051
2052/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002053 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002054 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002055 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002056 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002057last_window()
2058{
2059 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
2060}
2061
2062/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002063 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 * If "free_buf" is TRUE related buffer may be unloaded.
2065 *
2066 * called by :quit, :close, :xit, :wq and findtag()
2067 */
2068 void
2069win_close(win, free_buf)
2070 win_T *win;
2071 int free_buf;
2072{
2073 win_T *wp;
2074#ifdef FEAT_AUTOCMD
2075 int other_buffer = FALSE;
2076#endif
2077 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 int dir;
2079 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002080 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002082 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
2084 EMSG(_("E444: Cannot close last window"));
2085 return;
2086 }
2087
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002088 /*
2089 * When closing the last window in a tab page first go to another tab
2090 * page and then close the window and the tab page. This avoids that
2091 * curwin and curtab are not invalid while we are freeing memory, they may
2092 * be used in GUI events.
2093 */
2094 if (firstwin == lastwin)
2095 {
2096 goto_tabpage_tp(alt_tabpage());
2097 redraw_tabline = TRUE;
2098
2099 /* Safety check: Autocommands may have closed the window when jumping
2100 * to the other tab page. */
2101 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2102 {
2103 int h = tabline_height();
2104
2105 win_close_othertab(win, free_buf, prev_curtab);
2106 if (h != tabline_height())
2107 shell_new_rows();
2108 }
2109 return;
2110 }
2111
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 /* When closing the help window, try restoring a snapshot after closing
2113 * the window. Otherwise clear the snapshot, it's now invalid. */
2114 if (win->w_buffer->b_help)
2115 help_window = TRUE;
2116 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002117 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
2119#ifdef FEAT_AUTOCMD
2120 if (win == curwin)
2121 {
2122 /*
2123 * Guess which window is going to be the new current window.
2124 * This may change because of the autocommands (sigh).
2125 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002126 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127
2128 /*
2129 * Be careful: If autocommands delete the window, return now.
2130 */
2131 if (wp->w_buffer != curbuf)
2132 {
2133 other_buffer = TRUE;
2134 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002135 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 return;
2137 }
2138 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002139 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 return;
2141# ifdef FEAT_EVAL
2142 /* autocmds may abort script processing */
2143 if (aborting())
2144 return;
2145# endif
2146 }
2147#endif
2148
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002149#ifdef FEAT_GUI
2150 /* Avoid trouble with scrollbars that are going to be deleted in
2151 * win_free(). */
2152 if (gui.in_use)
2153 out_flush();
2154#endif
2155
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 /*
2157 * Close the link to the buffer.
2158 */
2159 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002160
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002162 * other window or moved to another tab page. */
2163 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 return;
2165
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002166 /* Free the memory used for the window. */
2167 wp = win_free_mem(win, &dir, NULL);
2168
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 /* Make sure curwin isn't invalid. It can cause severe trouble when
2170 * printing an error message. For win_equal() curbuf needs to be valid
2171 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002172 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 {
2174 curwin = wp;
2175#ifdef FEAT_QUICKFIX
2176 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2177 {
2178 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002179 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 * finding another window to go to.
2181 */
2182 for (;;)
2183 {
2184 if (wp->w_next == NULL)
2185 wp = firstwin;
2186 else
2187 wp = wp->w_next;
2188 if (wp == curwin)
2189 break;
2190 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2191 {
2192 curwin = wp;
2193 break;
2194 }
2195 }
2196 }
2197#endif
2198 curbuf = curwin->w_buffer;
2199 close_curwin = TRUE;
2200 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002201 if (p_ea
2202#ifdef FEAT_VERTSPLIT
2203 && (*p_ead == 'b' || *p_ead == dir)
2204#endif
2205 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 win_equal(curwin, TRUE,
2207#ifdef FEAT_VERTSPLIT
2208 dir
2209#else
2210 0
2211#endif
2212 );
2213 else
2214 win_comp_pos();
2215 if (close_curwin)
2216 {
2217 win_enter_ext(wp, FALSE, TRUE);
2218#ifdef FEAT_AUTOCMD
2219 if (other_buffer)
2220 /* careful: after this wp and win may be invalid! */
2221 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2222#endif
2223 }
2224
2225 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002226 * If last window has a status line now and we don't want one,
2227 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 */
2229 last_status(FALSE);
2230
2231 /* After closing the help window, try restoring the window layout from
2232 * before it was opened. */
2233 if (help_window)
2234 restore_snapshot(close_curwin);
2235
2236#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2237 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2238 if (gui.in_use && !win_hasvertsplit())
2239 gui_init_which_components(NULL);
2240#endif
2241
2242 redraw_all_later(NOT_VALID);
2243}
2244
2245/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002246 * Close window "win" in tab page "tp", which is not the current tab page.
2247 * This may be the last window ih that tab page and result in closing the tab,
2248 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002249 * Caller must check if buffer is hidden and whether the tabline needs to be
2250 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002251 */
2252 void
2253win_close_othertab(win, free_buf, tp)
2254 win_T *win;
2255 int free_buf;
2256 tabpage_T *tp;
2257{
2258 win_T *wp;
2259 int dir;
2260 tabpage_T *ptp = NULL;
2261
2262 /* Close the link to the buffer. */
2263 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2264
2265 /* Careful: Autocommands may have closed the tab page or made it the
2266 * current tab page. */
2267 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2268 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002269 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002270 return;
2271
2272 /* Autocommands may have closed the window already. */
2273 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2274 ;
2275 if (wp == NULL)
2276 return;
2277
2278 /* Free the memory used for the window. */
2279 wp = win_free_mem(win, &dir, tp);
2280
2281 /* When closing the last window in a tab page remove the tab page. */
2282 if (wp == NULL)
2283 {
2284 if (tp == first_tabpage)
2285 first_tabpage = tp->tp_next;
2286 else
2287 {
2288 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2289 ptp = ptp->tp_next)
2290 ;
2291 if (ptp == NULL)
2292 {
2293 EMSG2(_(e_intern2), "win_close_othertab()");
2294 return;
2295 }
2296 ptp->tp_next = tp->tp_next;
2297 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002298 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002299 }
2300}
2301
2302/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002303 * Free the memory used for a window.
2304 * Returns a pointer to the window that got the freed up space.
2305 */
2306 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002307win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002308 win_T *win;
2309 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002310 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002311{
2312 frame_T *frp;
2313 win_T *wp;
2314
Bram Moolenaarea408852005-06-25 22:49:46 +00002315#ifdef FEAT_FOLDING
2316 clearFolding(win);
2317#endif
2318
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002319 /* reduce the reference count to the argument list. */
2320 alist_unlink(win->w_alist);
2321
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002322 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002323 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002324 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002325 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002326 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002327
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002328 /* When deleting the current window of another tab page select a new
2329 * current window. */
2330 if (tp != NULL && win == tp->tp_curwin)
2331 tp->tp_curwin = wp;
2332
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002333 return wp;
2334}
2335
2336#if defined(EXITFREE) || defined(PROTO)
2337 void
2338win_free_all()
2339{
2340 int dummy;
2341
Bram Moolenaarf740b292006-02-16 22:11:02 +00002342# ifdef FEAT_WINDOWS
2343 while (first_tabpage->tp_next != NULL)
2344 tabpage_close(TRUE);
2345# endif
2346
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002347 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002348 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002349}
2350#endif
2351
2352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 * Remove a window and its frame from the tree of frames.
2354 * Returns a pointer to the window that got the freed up space.
2355 */
2356/*ARGSUSED*/
2357 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002358winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 win_T *win;
2360 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002361 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362{
2363 frame_T *frp, *frp2, *frp3;
2364 frame_T *frp_close = win->w_frame;
2365 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
2367 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002368 * If there is only one window there is nothing to remove.
2369 */
2370 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2371 return NULL;
2372
2373 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 * Remove the window from its frame.
2375 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002376 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 wp = frame2win(frp2);
2378
2379 /* Remove this frame from the list of frames. */
2380 frame_remove(frp_close);
2381
2382#ifdef FEAT_VERTSPLIT
2383 if (frp_close->fr_parent->fr_layout == FR_COL)
2384 {
2385#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002386 /* When 'winfixheight' is set, try to find another frame in the column
2387 * (as close to the closed frame as possible) to distribute the height
2388 * to. */
2389 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2390 {
2391 frp = frp_close->fr_prev;
2392 frp3 = frp_close->fr_next;
2393 while (frp != NULL || frp3 != NULL)
2394 {
2395 if (frp != NULL)
2396 {
2397 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2398 {
2399 frp2 = frp;
2400 wp = frp->fr_win;
2401 break;
2402 }
2403 frp = frp->fr_prev;
2404 }
2405 if (frp3 != NULL)
2406 {
2407 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2408 {
2409 frp2 = frp3;
2410 wp = frp3->fr_win;
2411 break;
2412 }
2413 frp3 = frp3->fr_next;
2414 }
2415 }
2416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2418 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419#ifdef FEAT_VERTSPLIT
2420 *dirp = 'v';
2421 }
2422 else
2423 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002424 /* When 'winfixwidth' is set, try to find another frame in the column
2425 * (as close to the closed frame as possible) to distribute the width
2426 * to. */
2427 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2428 {
2429 frp = frp_close->fr_prev;
2430 frp3 = frp_close->fr_next;
2431 while (frp != NULL || frp3 != NULL)
2432 {
2433 if (frp != NULL)
2434 {
2435 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2436 {
2437 frp2 = frp;
2438 wp = frp->fr_win;
2439 break;
2440 }
2441 frp = frp->fr_prev;
2442 }
2443 if (frp3 != NULL)
2444 {
2445 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2446 {
2447 frp2 = frp3;
2448 wp = frp3->fr_win;
2449 break;
2450 }
2451 frp3 = frp3->fr_next;
2452 }
2453 }
2454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002456 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 *dirp = 'h';
2458 }
2459#endif
2460
2461 /* If rows/columns go to a window below/right its positions need to be
2462 * updated. Can only be done after the sizes have been updated. */
2463 if (frp2 == frp_close->fr_next)
2464 {
2465 int row = win->w_winrow;
2466 int col = W_WINCOL(win);
2467
2468 frame_comp_pos(frp2, &row, &col);
2469 }
2470
2471 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2472 {
2473 /* There is no other frame in this list, move its info to the parent
2474 * and remove it. */
2475 frp2->fr_parent->fr_layout = frp2->fr_layout;
2476 frp2->fr_parent->fr_child = frp2->fr_child;
2477 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2478 frp->fr_parent = frp2->fr_parent;
2479 frp2->fr_parent->fr_win = frp2->fr_win;
2480 if (frp2->fr_win != NULL)
2481 frp2->fr_win->w_frame = frp2->fr_parent;
2482 frp = frp2->fr_parent;
2483 vim_free(frp2);
2484
2485 frp2 = frp->fr_parent;
2486 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2487 {
2488 /* The frame above the parent has the same layout, have to merge
2489 * the frames into this list. */
2490 if (frp2->fr_child == frp)
2491 frp2->fr_child = frp->fr_child;
2492 frp->fr_child->fr_prev = frp->fr_prev;
2493 if (frp->fr_prev != NULL)
2494 frp->fr_prev->fr_next = frp->fr_child;
2495 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2496 {
2497 frp3->fr_parent = frp2;
2498 if (frp3->fr_next == NULL)
2499 {
2500 frp3->fr_next = frp->fr_next;
2501 if (frp->fr_next != NULL)
2502 frp->fr_next->fr_prev = frp3;
2503 break;
2504 }
2505 }
2506 vim_free(frp);
2507 }
2508 }
2509
2510 return wp;
2511}
2512
2513/*
2514 * Find out which frame is going to get the freed up space when "win" is
2515 * closed.
2516 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2517 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2518 * This makes opening a window and closing it immediately keep the same window
2519 * layout.
2520 */
2521 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002522win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002524 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525{
2526 frame_T *frp;
2527 int b;
2528
Bram Moolenaarf740b292006-02-16 22:11:02 +00002529 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002530 /* Last window in this tab page, will go to next tab page. */
2531 return alt_tabpage()->tp_curwin->w_frame;
2532
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 frp = win->w_frame;
2534#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002535 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 b = p_spr;
2537 else
2538#endif
2539 b = p_sb;
2540 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2541 return frp->fr_next;
2542 return frp->fr_prev;
2543}
2544
2545/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002546 * Return the tabpage that will be used if the current one is closed.
2547 */
2548 static tabpage_T *
2549alt_tabpage()
2550{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002551 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002552
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002553 /* Use the next tab page if possible. */
2554 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002555 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002556
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002557 /* Find the last but one tab page. */
2558 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2559 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002560 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002561}
2562
2563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 * Find the left-upper window in frame "frp".
2565 */
2566 static win_T *
2567frame2win(frp)
2568 frame_T *frp;
2569{
2570 while (frp->fr_win == NULL)
2571 frp = frp->fr_child;
2572 return frp->fr_win;
2573}
2574
2575/*
2576 * Return TRUE if frame "frp" contains window "wp".
2577 */
2578 static int
2579frame_has_win(frp, wp)
2580 frame_T *frp;
2581 win_T *wp;
2582{
2583 frame_T *p;
2584
2585 if (frp->fr_layout == FR_LEAF)
2586 return frp->fr_win == wp;
2587
2588 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2589 if (frame_has_win(p, wp))
2590 return TRUE;
2591 return FALSE;
2592}
2593
2594/*
2595 * Set a new height for a frame. Recursively sets the height for contained
2596 * frames and windows. Caller must take care of positions.
2597 */
2598 static void
2599frame_new_height(topfrp, height, topfirst, wfh)
2600 frame_T *topfrp;
2601 int height;
2602 int topfirst; /* resize topmost contained frame first */
2603 int wfh; /* obey 'winfixheight' when there is a choice;
2604 may cause the height not to be set */
2605{
2606 frame_T *frp;
2607 int extra_lines;
2608 int h;
2609
2610 if (topfrp->fr_win != NULL)
2611 {
2612 /* Simple case: just one window. */
2613 win_new_height(topfrp->fr_win,
2614 height - topfrp->fr_win->w_status_height);
2615 }
2616#ifdef FEAT_VERTSPLIT
2617 else if (topfrp->fr_layout == FR_ROW)
2618 {
2619 do
2620 {
2621 /* All frames in this row get the same new height. */
2622 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2623 {
2624 frame_new_height(frp, height, topfirst, wfh);
2625 if (frp->fr_height > height)
2626 {
2627 /* Could not fit the windows, make the whole row higher. */
2628 height = frp->fr_height;
2629 break;
2630 }
2631 }
2632 }
2633 while (frp != NULL);
2634 }
2635#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002636 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 {
2638 /* Complicated case: Resize a column of frames. Resize the bottom
2639 * frame first, frames above that when needed. */
2640
2641 frp = topfrp->fr_child;
2642 if (wfh)
2643 /* Advance past frames with one window with 'wfh' set. */
2644 while (frame_fixed_height(frp))
2645 {
2646 frp = frp->fr_next;
2647 if (frp == NULL)
2648 return; /* no frame without 'wfh', give up */
2649 }
2650 if (!topfirst)
2651 {
2652 /* Find the bottom frame of this column */
2653 while (frp->fr_next != NULL)
2654 frp = frp->fr_next;
2655 if (wfh)
2656 /* Advance back for frames with one window with 'wfh' set. */
2657 while (frame_fixed_height(frp))
2658 frp = frp->fr_prev;
2659 }
2660
2661 extra_lines = height - topfrp->fr_height;
2662 if (extra_lines < 0)
2663 {
2664 /* reduce height of contained frames, bottom or top frame first */
2665 while (frp != NULL)
2666 {
2667 h = frame_minheight(frp, NULL);
2668 if (frp->fr_height + extra_lines < h)
2669 {
2670 extra_lines += frp->fr_height - h;
2671 frame_new_height(frp, h, topfirst, wfh);
2672 }
2673 else
2674 {
2675 frame_new_height(frp, frp->fr_height + extra_lines,
2676 topfirst, wfh);
2677 break;
2678 }
2679 if (topfirst)
2680 {
2681 do
2682 frp = frp->fr_next;
2683 while (wfh && frp != NULL && frame_fixed_height(frp));
2684 }
2685 else
2686 {
2687 do
2688 frp = frp->fr_prev;
2689 while (wfh && frp != NULL && frame_fixed_height(frp));
2690 }
2691 /* Increase "height" if we could not reduce enough frames. */
2692 if (frp == NULL)
2693 height -= extra_lines;
2694 }
2695 }
2696 else if (extra_lines > 0)
2697 {
2698 /* increase height of bottom or top frame */
2699 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2700 }
2701 }
2702 topfrp->fr_height = height;
2703}
2704
2705/*
2706 * Return TRUE if height of frame "frp" should not be changed because of
2707 * the 'winfixheight' option.
2708 */
2709 static int
2710frame_fixed_height(frp)
2711 frame_T *frp;
2712{
2713 /* frame with one window: fixed height if 'winfixheight' set. */
2714 if (frp->fr_win != NULL)
2715 return frp->fr_win->w_p_wfh;
2716
2717 if (frp->fr_layout == FR_ROW)
2718 {
2719 /* The frame is fixed height if one of the frames in the row is fixed
2720 * height. */
2721 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2722 if (frame_fixed_height(frp))
2723 return TRUE;
2724 return FALSE;
2725 }
2726
2727 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2728 * frames in the row are fixed height. */
2729 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2730 if (!frame_fixed_height(frp))
2731 return FALSE;
2732 return TRUE;
2733}
2734
2735#ifdef FEAT_VERTSPLIT
2736/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002737 * Return TRUE if width of frame "frp" should not be changed because of
2738 * the 'winfixwidth' option.
2739 */
2740 static int
2741frame_fixed_width(frp)
2742 frame_T *frp;
2743{
2744 /* frame with one window: fixed width if 'winfixwidth' set. */
2745 if (frp->fr_win != NULL)
2746 return frp->fr_win->w_p_wfw;
2747
2748 if (frp->fr_layout == FR_COL)
2749 {
2750 /* The frame is fixed width if one of the frames in the row is fixed
2751 * width. */
2752 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2753 if (frame_fixed_width(frp))
2754 return TRUE;
2755 return FALSE;
2756 }
2757
2758 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2759 * frames in the row are fixed width. */
2760 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2761 if (!frame_fixed_width(frp))
2762 return FALSE;
2763 return TRUE;
2764}
2765
2766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 * Add a status line to windows at the bottom of "frp".
2768 * Note: Does not check if there is room!
2769 */
2770 static void
2771frame_add_statusline(frp)
2772 frame_T *frp;
2773{
2774 win_T *wp;
2775
2776 if (frp->fr_layout == FR_LEAF)
2777 {
2778 wp = frp->fr_win;
2779 if (wp->w_status_height == 0)
2780 {
2781 if (wp->w_height > 0) /* don't make it negative */
2782 --wp->w_height;
2783 wp->w_status_height = STATUS_HEIGHT;
2784 }
2785 }
2786 else if (frp->fr_layout == FR_ROW)
2787 {
2788 /* Handle all the frames in the row. */
2789 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2790 frame_add_statusline(frp);
2791 }
2792 else /* frp->fr_layout == FR_COL */
2793 {
2794 /* Only need to handle the last frame in the column. */
2795 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2796 ;
2797 frame_add_statusline(frp);
2798 }
2799}
2800
2801/*
2802 * Set width of a frame. Handles recursively going through contained frames.
2803 * May remove separator line for windows at the right side (for win_close()).
2804 */
2805 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002806frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 frame_T *topfrp;
2808 int width;
2809 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002810 int wfw; /* obey 'winfixwidth' when there is a choice;
2811 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812{
2813 frame_T *frp;
2814 int extra_cols;
2815 int w;
2816 win_T *wp;
2817
2818 if (topfrp->fr_layout == FR_LEAF)
2819 {
2820 /* Simple case: just one window. */
2821 wp = topfrp->fr_win;
2822 /* Find out if there are any windows right of this one. */
2823 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2824 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2825 break;
2826 if (frp->fr_parent == NULL)
2827 wp->w_vsep_width = 0;
2828 win_new_width(wp, width - wp->w_vsep_width);
2829 }
2830 else if (topfrp->fr_layout == FR_COL)
2831 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002832 do
2833 {
2834 /* All frames in this column get the same new width. */
2835 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2836 {
2837 frame_new_width(frp, width, leftfirst, wfw);
2838 if (frp->fr_width > width)
2839 {
2840 /* Could not fit the windows, make whole column wider. */
2841 width = frp->fr_width;
2842 break;
2843 }
2844 }
2845 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 }
2847 else /* fr_layout == FR_ROW */
2848 {
2849 /* Complicated case: Resize a row of frames. Resize the rightmost
2850 * frame first, frames left of it when needed. */
2851
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002853 if (wfw)
2854 /* Advance past frames with one window with 'wfw' set. */
2855 while (frame_fixed_width(frp))
2856 {
2857 frp = frp->fr_next;
2858 if (frp == NULL)
2859 return; /* no frame without 'wfw', give up */
2860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002862 {
2863 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 while (frp->fr_next != NULL)
2865 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002866 if (wfw)
2867 /* Advance back for frames with one window with 'wfw' set. */
2868 while (frame_fixed_width(frp))
2869 frp = frp->fr_prev;
2870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871
2872 extra_cols = width - topfrp->fr_width;
2873 if (extra_cols < 0)
2874 {
2875 /* reduce frame width, rightmost frame first */
2876 while (frp != NULL)
2877 {
2878 w = frame_minwidth(frp, NULL);
2879 if (frp->fr_width + extra_cols < w)
2880 {
2881 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002882 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 }
2884 else
2885 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002886 frame_new_width(frp, frp->fr_width + extra_cols,
2887 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 break;
2889 }
2890 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002891 {
2892 do
2893 frp = frp->fr_next;
2894 while (wfw && frp != NULL && frame_fixed_width(frp));
2895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002897 {
2898 do
2899 frp = frp->fr_prev;
2900 while (wfw && frp != NULL && frame_fixed_width(frp));
2901 }
2902 /* Increase "width" if we could not reduce enough frames. */
2903 if (frp == NULL)
2904 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 }
2906 }
2907 else if (extra_cols > 0)
2908 {
2909 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002910 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 }
2912 }
2913 topfrp->fr_width = width;
2914}
2915
2916/*
2917 * Add the vertical separator to windows at the right side of "frp".
2918 * Note: Does not check if there is room!
2919 */
2920 static void
2921frame_add_vsep(frp)
2922 frame_T *frp;
2923{
2924 win_T *wp;
2925
2926 if (frp->fr_layout == FR_LEAF)
2927 {
2928 wp = frp->fr_win;
2929 if (wp->w_vsep_width == 0)
2930 {
2931 if (wp->w_width > 0) /* don't make it negative */
2932 --wp->w_width;
2933 wp->w_vsep_width = 1;
2934 }
2935 }
2936 else if (frp->fr_layout == FR_COL)
2937 {
2938 /* Handle all the frames in the column. */
2939 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2940 frame_add_vsep(frp);
2941 }
2942 else /* frp->fr_layout == FR_ROW */
2943 {
2944 /* Only need to handle the last frame in the row. */
2945 frp = frp->fr_child;
2946 while (frp->fr_next != NULL)
2947 frp = frp->fr_next;
2948 frame_add_vsep(frp);
2949 }
2950}
2951
2952/*
2953 * Set frame width from the window it contains.
2954 */
2955 static void
2956frame_fix_width(wp)
2957 win_T *wp;
2958{
2959 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2960}
2961#endif
2962
2963/*
2964 * Set frame height from the window it contains.
2965 */
2966 static void
2967frame_fix_height(wp)
2968 win_T *wp;
2969{
2970 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2971}
2972
2973/*
2974 * Compute the minimal height for frame "topfrp".
2975 * Uses the 'winminheight' option.
2976 * When "next_curwin" isn't NULL, use p_wh for this window.
2977 * When "next_curwin" is NOWIN, don't use at least one line for the current
2978 * window.
2979 */
2980 static int
2981frame_minheight(topfrp, next_curwin)
2982 frame_T *topfrp;
2983 win_T *next_curwin;
2984{
2985 frame_T *frp;
2986 int m;
2987#ifdef FEAT_VERTSPLIT
2988 int n;
2989#endif
2990
2991 if (topfrp->fr_win != NULL)
2992 {
2993 if (topfrp->fr_win == next_curwin)
2994 m = p_wh + topfrp->fr_win->w_status_height;
2995 else
2996 {
2997 /* window: minimal height of the window plus status line */
2998 m = p_wmh + topfrp->fr_win->w_status_height;
2999 /* Current window is minimal one line high */
3000 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3001 ++m;
3002 }
3003 }
3004#ifdef FEAT_VERTSPLIT
3005 else if (topfrp->fr_layout == FR_ROW)
3006 {
3007 /* get the minimal height from each frame in this row */
3008 m = 0;
3009 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3010 {
3011 n = frame_minheight(frp, next_curwin);
3012 if (n > m)
3013 m = n;
3014 }
3015 }
3016#endif
3017 else
3018 {
3019 /* Add up the minimal heights for all frames in this column. */
3020 m = 0;
3021 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3022 m += frame_minheight(frp, next_curwin);
3023 }
3024
3025 return m;
3026}
3027
3028#ifdef FEAT_VERTSPLIT
3029/*
3030 * Compute the minimal width for frame "topfrp".
3031 * When "next_curwin" isn't NULL, use p_wiw for this window.
3032 * When "next_curwin" is NOWIN, don't use at least one column for the current
3033 * window.
3034 */
3035 static int
3036frame_minwidth(topfrp, next_curwin)
3037 frame_T *topfrp;
3038 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3039{
3040 frame_T *frp;
3041 int m, n;
3042
3043 if (topfrp->fr_win != NULL)
3044 {
3045 if (topfrp->fr_win == next_curwin)
3046 m = p_wiw + topfrp->fr_win->w_vsep_width;
3047 else
3048 {
3049 /* window: minimal width of the window plus separator column */
3050 m = p_wmw + topfrp->fr_win->w_vsep_width;
3051 /* Current window is minimal one column wide */
3052 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3053 ++m;
3054 }
3055 }
3056 else if (topfrp->fr_layout == FR_COL)
3057 {
3058 /* get the minimal width from each frame in this column */
3059 m = 0;
3060 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3061 {
3062 n = frame_minwidth(frp, next_curwin);
3063 if (n > m)
3064 m = n;
3065 }
3066 }
3067 else
3068 {
3069 /* Add up the minimal widths for all frames in this row. */
3070 m = 0;
3071 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3072 m += frame_minwidth(frp, next_curwin);
3073 }
3074
3075 return m;
3076}
3077#endif
3078
3079
3080/*
3081 * Try to close all windows except current one.
3082 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3083 * used and the buffer was modified.
3084 *
3085 * Used by ":bdel" and ":only".
3086 */
3087 void
3088close_others(message, forceit)
3089 int message;
3090 int forceit; /* always hide all other windows */
3091{
3092 win_T *wp;
3093 win_T *nextwp;
3094 int r;
3095
3096 if (lastwin == firstwin)
3097 {
3098 if (message
3099#ifdef FEAT_AUTOCMD
3100 && !autocmd_busy
3101#endif
3102 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003103 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 return;
3105 }
3106
3107 /* Be very careful here: autocommands may change the window layout. */
3108 for (wp = firstwin; win_valid(wp); wp = nextwp)
3109 {
3110 nextwp = wp->w_next;
3111 if (wp != curwin) /* don't close current window */
3112 {
3113
3114 /* Check if it's allowed to abandon this window */
3115 r = can_abandon(wp->w_buffer, forceit);
3116#ifdef FEAT_AUTOCMD
3117 if (!win_valid(wp)) /* autocommands messed wp up */
3118 {
3119 nextwp = firstwin;
3120 continue;
3121 }
3122#endif
3123 if (!r)
3124 {
3125#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3126 if (message && (p_confirm || cmdmod.confirm) && p_write)
3127 {
3128 dialog_changed(wp->w_buffer, FALSE);
3129# ifdef FEAT_AUTOCMD
3130 if (!win_valid(wp)) /* autocommands messed wp up */
3131 {
3132 nextwp = firstwin;
3133 continue;
3134 }
3135# endif
3136 }
3137 if (bufIsChanged(wp->w_buffer))
3138#endif
3139 continue;
3140 }
3141 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3142 }
3143 }
3144
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003145 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 EMSG(_("E445: Other window contains changes"));
3147}
3148
3149#endif /* FEAT_WINDOWS */
3150
3151/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003152 * Init the current window "curwin".
3153 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 */
3155 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003156curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003158 redraw_win_later(curwin, NOT_VALID);
3159 curwin->w_lines_valid = 0;
3160 curwin->w_cursor.lnum = 1;
3161 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003163 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003165 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3166 curwin->w_pcmark.col = 0;
3167 curwin->w_prev_pcmark.lnum = 0;
3168 curwin->w_prev_pcmark.col = 0;
3169 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003171 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003173 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174#ifdef FEAT_FKMAP
3175 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003176 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003178 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179#endif
3180}
3181
3182/*
3183 * Allocate the first window and put an empty buffer in it.
3184 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003185 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003187 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188win_alloc_first()
3189{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003190 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003191 return FAIL;
3192
3193#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003194 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003195 if (first_tabpage == NULL)
3196 return FAIL;
3197 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003198 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003199#endif
3200 return OK;
3201}
3202
3203/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003204 * Allocate the first window or the first window in a new tab page.
3205 * When "oldwin" is NULL create an empty buffer for it.
3206 * When "oldwin" is not NULL copy info from it to the new window (only with
3207 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003208 * Return FAIL when something goes wrong (out of memory).
3209 */
3210 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003211win_alloc_firstwin(oldwin)
3212 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003213{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003215 if (oldwin == NULL)
3216 {
3217 /* Very first window, need to create an empty buffer for it and
3218 * initialize from scratch. */
3219 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3220 if (curwin == NULL || curbuf == NULL)
3221 return FAIL;
3222 curwin->w_buffer = curbuf;
3223 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003225 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003227 curwin_init(); /* init current window */
3228 }
3229#ifdef FEAT_WINDOWS
3230 else
3231 {
3232 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003233 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003234
3235# ifdef FEAT_SCROLLBIND
3236 /* We don't want scroll-binding in the first window. */
3237 curwin->w_p_scb = FALSE;
3238# endif
3239 }
3240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241
3242 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3243 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003244 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 topframe->fr_layout = FR_LEAF;
3246#ifdef FEAT_VERTSPLIT
3247 topframe->fr_width = Columns;
3248#endif
3249 topframe->fr_height = Rows - p_ch;
3250 topframe->fr_win = curwin;
3251 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003252
3253 return OK;
3254}
3255
3256/*
3257 * Initialize the window and frame size to the maximum.
3258 */
3259 void
3260win_init_size()
3261{
3262 firstwin->w_height = ROWS_AVAIL;
3263 topframe->fr_height = ROWS_AVAIL;
3264#ifdef FEAT_VERTSPLIT
3265 firstwin->w_width = Columns;
3266 topframe->fr_width = Columns;
3267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268}
3269
3270#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003271
3272/*
3273 * Allocate a new tabpage_T and init the values.
3274 * Returns NULL when out of memory.
3275 */
3276 static tabpage_T *
3277alloc_tabpage()
3278{
3279 tabpage_T *tp;
3280
3281 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3282 if (tp != NULL)
3283 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003284# ifdef FEAT_GUI
3285 int i;
3286
3287 for (i = 0; i < 3; i++)
3288 tp->tp_prev_which_scrollbars[i] = -1;
3289# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003290# ifdef FEAT_DIFF
3291 tp->tp_diff_invalid = TRUE;
3292# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003293#ifdef FEAT_EVAL
3294 /* init t: variables */
3295 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3296#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003297 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003298 }
3299 return tp;
3300}
3301
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003302 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003303free_tabpage(tp)
3304 tabpage_T *tp;
3305{
3306# ifdef FEAT_DIFF
3307 diff_clear(tp);
3308# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003309 clear_snapshot(tp);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003310#ifdef FEAT_EVAL
3311 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3312#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003313 vim_free(tp);
3314}
3315
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003316/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003317 * Create a new Tab page with one window.
3318 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003319 * When "after" is 0 put it just after the current Tab page.
3320 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003321 * Return FAIL or OK.
3322 */
3323 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003324win_new_tabpage(after)
3325 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003326{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003327 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003328 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003329 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003330
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003331 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003332 if (newtp == NULL)
3333 return FAIL;
3334
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003335 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003336 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003337 {
3338 vim_free(newtp);
3339 return FAIL;
3340 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003341 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003342
3343 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003344 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003345 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003346 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003347 if (after == 1)
3348 {
3349 /* New tab page becomes the first one. */
3350 newtp->tp_next = first_tabpage;
3351 first_tabpage = newtp;
3352 }
3353 else
3354 {
3355 if (after > 0)
3356 {
3357 /* Put new tab page before tab page "after". */
3358 n = 2;
3359 for (tp = first_tabpage; tp->tp_next != NULL
3360 && n < after; tp = tp->tp_next)
3361 ++n;
3362 }
3363 newtp->tp_next = tp->tp_next;
3364 tp->tp_next = newtp;
3365 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003366 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003367 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003368 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003369
3370 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003371 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003372
3373#if defined(FEAT_GUI)
3374 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3375 * scrollbars. Have to update them anyway. */
3376 if (gui.in_use && starting == 0)
3377 {
3378 gui_init_which_components(NULL);
3379 gui_update_scrollbars(TRUE);
3380 }
3381 need_mouse_correct = TRUE;
3382#endif
3383
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003384 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003385#ifdef FEAT_AUTOCMD
3386 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3387 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3388#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003389 return OK;
3390 }
3391
3392 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003393 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003394 return FAIL;
3395}
3396
3397/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003398 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3399 * like with ":split".
3400 * Returns OK if a new tab page was created, FAIL otherwise.
3401 */
3402 int
3403may_open_tabpage()
3404{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003405 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003406
Bram Moolenaard326ce82007-03-11 14:48:29 +00003407 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003408 {
3409 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003410 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003411 return win_new_tabpage(n);
3412 }
3413 return FAIL;
3414}
3415
3416/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003417 * Create up to "maxcount" tabpages with empty windows.
3418 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003419 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003420 int
3421make_tabpages(maxcount)
3422 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003423{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003424 int count = maxcount;
3425 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003426
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003427 /* Limit to 'tabpagemax' tabs. */
3428 if (count > p_tpm)
3429 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003430
3431#ifdef FEAT_AUTOCMD
3432 /*
3433 * Don't execute autocommands while creating the tab pages. Must do that
3434 * when putting the buffers in the windows.
3435 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003436 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003437#endif
3438
3439 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003440 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003441 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003442
3443#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003444 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003445#endif
3446
3447 /* return actual number of tab pages */
3448 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003449}
3450
3451/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003452 * Return TRUE when "tpc" points to a valid tab page.
3453 */
3454 int
3455valid_tabpage(tpc)
3456 tabpage_T *tpc;
3457{
3458 tabpage_T *tp;
3459
3460 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3461 if (tp == tpc)
3462 return TRUE;
3463 return FALSE;
3464}
3465
3466/*
3467 * Find tab page "n" (first one is 1). Returns NULL when not found.
3468 */
3469 tabpage_T *
3470find_tabpage(n)
3471 int n;
3472{
3473 tabpage_T *tp;
3474 int i = 1;
3475
3476 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3477 ++i;
3478 return tp;
3479}
3480
3481/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003482 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003483 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003484 */
3485 int
3486tabpage_index(ftp)
3487 tabpage_T *ftp;
3488{
3489 int i = 1;
3490 tabpage_T *tp;
3491
3492 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3493 ++i;
3494 return i;
3495}
3496
3497/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003498 * Prepare for leaving the current tab page.
3499 * When autocomands change "curtab" we don't leave the tab page and return
3500 * FAIL.
3501 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003502 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003503/*ARGSUSED*/
3504 static int
3505leave_tabpage(new_curbuf)
3506 buf_T *new_curbuf; /* what is going to be the new curbuf,
3507 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003508{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003509 tabpage_T *tp = curtab;
3510
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003511#ifdef FEAT_VISUAL
3512 reset_VIsual_and_resel(); /* stop Visual mode */
3513#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003514#ifdef FEAT_AUTOCMD
3515 if (new_curbuf != curbuf)
3516 {
3517 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3518 if (curtab != tp)
3519 return FAIL;
3520 }
3521 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3522 if (curtab != tp)
3523 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003524 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003525 if (curtab != tp)
3526 return FAIL;
3527#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003528#if defined(FEAT_GUI)
3529 /* Remove the scrollbars. They may be added back later. */
3530 if (gui.in_use)
3531 gui_remove_scrollbars();
3532#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003533 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003534 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003535 tp->tp_firstwin = firstwin;
3536 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003537 tp->tp_old_Rows = Rows;
3538 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003539 firstwin = NULL;
3540 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003541 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003542}
3543
3544/*
3545 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003546 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003547 */
3548/*ARGSUSED*/
3549 static void
3550enter_tabpage(tp, old_curbuf)
3551 tabpage_T *tp;
3552 buf_T *old_curbuf;
3553{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003554 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003555 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003556
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003557 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003558 firstwin = tp->tp_firstwin;
3559 lastwin = tp->tp_lastwin;
3560 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003561
3562 /* We would like doing the TabEnter event first, but we don't have a
3563 * valid current window yet, which may break some commands.
3564 * This triggers autocommands, thus may make "tp" invalid. */
3565 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3566 prevwin = next_prevwin;
3567
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003568#ifdef FEAT_AUTOCMD
3569 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003570
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003571 if (old_curbuf != curbuf)
3572 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3573#endif
3574
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003575 last_status(FALSE); /* status line may appear or disappear */
3576 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003577 must_redraw = CLEAR; /* need to redraw everything */
3578#ifdef FEAT_DIFF
3579 diff_need_scrollbind = TRUE;
3580#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003581
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003582 /* The tabpage line may have appeared or disappeared, may need to resize
3583 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003584 * frame sizes too. Use the stored value of p_ch, so that it can be
3585 * different for each tab page. */
3586 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003587 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3588#ifdef FEAT_GUI_TABLINE
3589 && !gui_use_tabline()
3590#endif
3591 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003592 shell_new_rows();
3593#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003594 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003595 shell_new_columns(); /* update window widths */
3596#endif
3597
3598#if defined(FEAT_GUI)
3599 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3600 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003601 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003602 {
3603 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003604 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003605 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003606 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003607#endif
3608
3609 redraw_all_later(CLEAR);
3610}
3611
3612/*
3613 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003614 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003615 */
3616 void
3617goto_tabpage(n)
3618 int n;
3619{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003620 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003621 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003622 int i;
3623
Bram Moolenaard68071d2006-05-02 22:08:30 +00003624 if (text_locked())
3625 {
3626 /* Not allowed when editing the command line. */
3627#ifdef FEAT_CMDWIN
3628 if (cmdwin_type != 0)
3629 EMSG(_(e_cmdwin));
3630 else
3631#endif
3632 EMSG(_(e_secure));
3633 return;
3634 }
3635
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003636 /* If there is only one it can't work. */
3637 if (first_tabpage->tp_next == NULL)
3638 {
3639 if (n > 1)
3640 beep_flush();
3641 return;
3642 }
3643
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003644 if (n == 0)
3645 {
3646 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003647 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003648 tp = first_tabpage;
3649 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003650 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003651 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003652 else if (n < 0)
3653 {
3654 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3655 * this N times. */
3656 ttp = curtab;
3657 for (i = n; i < 0; ++i)
3658 {
3659 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3660 tp = tp->tp_next)
3661 ;
3662 ttp = tp;
3663 }
3664 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003665 else if (n == 9999)
3666 {
3667 /* Go to last tab page. */
3668 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3669 ;
3670 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003671 else
3672 {
3673 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003674 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003675 if (tp == NULL)
3676 {
3677 beep_flush();
3678 return;
3679 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003680 }
3681
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003682 goto_tabpage_tp(tp);
3683
3684#ifdef FEAT_GUI_TABLINE
3685 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003686 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003687#endif
3688}
3689
3690/*
3691 * Go to tabpage "tp".
3692 * Note: doesn't update the GUI tab.
3693 */
3694 void
3695goto_tabpage_tp(tp)
3696 tabpage_T *tp;
3697{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003698 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003699 {
3700 if (valid_tabpage(tp))
3701 enter_tabpage(tp, curbuf);
3702 else
3703 enter_tabpage(curtab, curbuf);
3704 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003705}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
3707/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003708 * Enter window "wp" in tab page "tp".
3709 * Also updates the GUI tab.
3710 */
3711 void
3712goto_tabpage_win(tp, wp)
3713 tabpage_T *tp;
3714 win_T *wp;
3715{
3716 goto_tabpage_tp(tp);
3717 if (curtab == tp && win_valid(wp))
3718 {
3719 win_enter(wp, TRUE);
3720# ifdef FEAT_GUI_TABLINE
3721 if (gui_use_tabline())
3722 gui_mch_set_curtab(tabpage_index(curtab));
3723# endif
3724 }
3725}
3726
3727/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003728 * Move the current tab page to before tab page "nr".
3729 */
3730 void
3731tabpage_move(nr)
3732 int nr;
3733{
3734 int n = nr;
3735 tabpage_T *tp;
3736
3737 if (first_tabpage->tp_next == NULL)
3738 return;
3739
3740 /* Remove the current tab page from the list of tab pages. */
3741 if (curtab == first_tabpage)
3742 first_tabpage = curtab->tp_next;
3743 else
3744 {
3745 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3746 if (tp->tp_next == curtab)
3747 break;
3748 if (tp == NULL) /* "cannot happen" */
3749 return;
3750 tp->tp_next = curtab->tp_next;
3751 }
3752
3753 /* Re-insert it at the specified position. */
3754 if (n == 0)
3755 {
3756 curtab->tp_next = first_tabpage;
3757 first_tabpage = curtab;
3758 }
3759 else
3760 {
3761 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3762 --n;
3763 curtab->tp_next = tp->tp_next;
3764 tp->tp_next = curtab;
3765 }
3766
3767 /* Need to redraw the tabline. Tab page contents doesn't change. */
3768 redraw_tabline = TRUE;
3769}
3770
3771
3772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 * Go to another window.
3774 * When jumping to another buffer, stop Visual mode. Do this before
3775 * changing windows so we can yank the selection into the '*' register.
3776 * When jumping to another window on the same buffer, adjust its cursor
3777 * position to keep the same Visual area.
3778 */
3779 void
3780win_goto(wp)
3781 win_T *wp;
3782{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003783 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 {
3785 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003786 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 return;
3788 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003789#ifdef FEAT_AUTOCMD
3790 if (curbuf_locked())
3791 return;
3792#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003793
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794#ifdef FEAT_VISUAL
3795 if (wp->w_buffer != curbuf)
3796 reset_VIsual_and_resel();
3797 else if (VIsual_active)
3798 wp->w_cursor = curwin->w_cursor;
3799#endif
3800
3801#ifdef FEAT_GUI
3802 need_mouse_correct = TRUE;
3803#endif
3804 win_enter(wp, TRUE);
3805}
3806
3807#if defined(FEAT_PERL) || defined(PROTO)
3808/*
3809 * Find window number "winnr" (counting top to bottom).
3810 */
3811 win_T *
3812win_find_nr(winnr)
3813 int winnr;
3814{
3815 win_T *wp;
3816
3817# ifdef FEAT_WINDOWS
3818 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3819 if (--winnr == 0)
3820 break;
3821 return wp;
3822# else
3823 return curwin;
3824# endif
3825}
3826#endif
3827
3828#ifdef FEAT_VERTSPLIT
3829/*
3830 * Move to window above or below "count" times.
3831 */
3832 static void
3833win_goto_ver(up, count)
3834 int up; /* TRUE to go to win above */
3835 long count;
3836{
3837 frame_T *fr;
3838 frame_T *nfr;
3839 frame_T *foundfr;
3840
3841 foundfr = curwin->w_frame;
3842 while (count--)
3843 {
3844 /*
3845 * First go upwards in the tree of frames until we find a upwards or
3846 * downwards neighbor.
3847 */
3848 fr = foundfr;
3849 for (;;)
3850 {
3851 if (fr == topframe)
3852 goto end;
3853 if (up)
3854 nfr = fr->fr_prev;
3855 else
3856 nfr = fr->fr_next;
3857 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3858 break;
3859 fr = fr->fr_parent;
3860 }
3861
3862 /*
3863 * Now go downwards to find the bottom or top frame in it.
3864 */
3865 for (;;)
3866 {
3867 if (nfr->fr_layout == FR_LEAF)
3868 {
3869 foundfr = nfr;
3870 break;
3871 }
3872 fr = nfr->fr_child;
3873 if (nfr->fr_layout == FR_ROW)
3874 {
3875 /* Find the frame at the cursor row. */
3876 while (fr->fr_next != NULL
3877 && frame2win(fr)->w_wincol + fr->fr_width
3878 <= curwin->w_wincol + curwin->w_wcol)
3879 fr = fr->fr_next;
3880 }
3881 if (nfr->fr_layout == FR_COL && up)
3882 while (fr->fr_next != NULL)
3883 fr = fr->fr_next;
3884 nfr = fr;
3885 }
3886 }
3887end:
3888 if (foundfr != NULL)
3889 win_goto(foundfr->fr_win);
3890}
3891
3892/*
3893 * Move to left or right window.
3894 */
3895 static void
3896win_goto_hor(left, count)
3897 int left; /* TRUE to go to left win */
3898 long count;
3899{
3900 frame_T *fr;
3901 frame_T *nfr;
3902 frame_T *foundfr;
3903
3904 foundfr = curwin->w_frame;
3905 while (count--)
3906 {
3907 /*
3908 * First go upwards in the tree of frames until we find a left or
3909 * right neighbor.
3910 */
3911 fr = foundfr;
3912 for (;;)
3913 {
3914 if (fr == topframe)
3915 goto end;
3916 if (left)
3917 nfr = fr->fr_prev;
3918 else
3919 nfr = fr->fr_next;
3920 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3921 break;
3922 fr = fr->fr_parent;
3923 }
3924
3925 /*
3926 * Now go downwards to find the leftmost or rightmost frame in it.
3927 */
3928 for (;;)
3929 {
3930 if (nfr->fr_layout == FR_LEAF)
3931 {
3932 foundfr = nfr;
3933 break;
3934 }
3935 fr = nfr->fr_child;
3936 if (nfr->fr_layout == FR_COL)
3937 {
3938 /* Find the frame at the cursor row. */
3939 while (fr->fr_next != NULL
3940 && frame2win(fr)->w_winrow + fr->fr_height
3941 <= curwin->w_winrow + curwin->w_wrow)
3942 fr = fr->fr_next;
3943 }
3944 if (nfr->fr_layout == FR_ROW && left)
3945 while (fr->fr_next != NULL)
3946 fr = fr->fr_next;
3947 nfr = fr;
3948 }
3949 }
3950end:
3951 if (foundfr != NULL)
3952 win_goto(foundfr->fr_win);
3953}
3954#endif
3955
3956/*
3957 * Make window "wp" the current window.
3958 */
3959 void
3960win_enter(wp, undo_sync)
3961 win_T *wp;
3962 int undo_sync;
3963{
3964 win_enter_ext(wp, undo_sync, FALSE);
3965}
3966
3967/*
3968 * Make window wp the current window.
3969 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3970 * been closed and isn't valid.
3971 */
3972 static void
3973win_enter_ext(wp, undo_sync, curwin_invalid)
3974 win_T *wp;
3975 int undo_sync;
3976 int curwin_invalid;
3977{
3978#ifdef FEAT_AUTOCMD
3979 int other_buffer = FALSE;
3980#endif
3981
3982 if (wp == curwin && !curwin_invalid) /* nothing to do */
3983 return;
3984
3985#ifdef FEAT_AUTOCMD
3986 if (!curwin_invalid)
3987 {
3988 /*
3989 * Be careful: If autocommands delete the window, return now.
3990 */
3991 if (wp->w_buffer != curbuf)
3992 {
3993 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3994 other_buffer = TRUE;
3995 if (!win_valid(wp))
3996 return;
3997 }
3998 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3999 if (!win_valid(wp))
4000 return;
4001# ifdef FEAT_EVAL
4002 /* autocmds may abort script processing */
4003 if (aborting())
4004 return;
4005# endif
4006 }
4007#endif
4008
4009 /* sync undo before leaving the current buffer */
4010 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004011 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 /* may have to copy the buffer options when 'cpo' contains 'S' */
4013 if (wp->w_buffer != curbuf)
4014 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4015 if (!curwin_invalid)
4016 {
4017 prevwin = curwin; /* remember for CTRL-W p */
4018 curwin->w_redr_status = TRUE;
4019 }
4020 curwin = wp;
4021 curbuf = wp->w_buffer;
4022 check_cursor();
4023#ifdef FEAT_VIRTUALEDIT
4024 if (!virtual_active())
4025 curwin->w_cursor.coladd = 0;
4026#endif
4027 changed_line_abv_curs(); /* assume cursor position needs updating */
4028
4029 if (curwin->w_localdir != NULL)
4030 {
4031 /* Window has a local directory: Save current directory as global
4032 * directory (unless that was done already) and change to the local
4033 * directory. */
4034 if (globaldir == NULL)
4035 {
4036 char_u cwd[MAXPATHL];
4037
4038 if (mch_dirname(cwd, MAXPATHL) == OK)
4039 globaldir = vim_strsave(cwd);
4040 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004041 if (mch_chdir((char *)curwin->w_localdir) == 0)
4042 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 }
4044 else if (globaldir != NULL)
4045 {
4046 /* Window doesn't have a local directory and we are not in the global
4047 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004048 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 vim_free(globaldir);
4050 globaldir = NULL;
4051 shorten_fnames(TRUE);
4052 }
4053
4054#ifdef FEAT_AUTOCMD
4055 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4056 if (other_buffer)
4057 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4058#endif
4059
4060#ifdef FEAT_TITLE
4061 maketitle();
4062#endif
4063 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004064 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 if (restart_edit)
4066 redraw_later(VALID); /* causes status line redraw */
4067
4068 /* set window height to desired minimal value */
4069 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4070 win_setheight((int)p_wh);
4071 else if (curwin->w_height == 0)
4072 win_setheight(1);
4073
4074#ifdef FEAT_VERTSPLIT
4075 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004076 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 win_setwidth((int)p_wiw);
4078#endif
4079
4080#ifdef FEAT_MOUSE
4081 setmouse(); /* in case jumped to/from help buffer */
4082#endif
4083
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004084 /* Change directories when the 'acd' option is set. */
4085 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086}
4087
4088#endif /* FEAT_WINDOWS */
4089
4090#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4091/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004092 * Jump to the first open window that contains buffer "buf", if one exists.
4093 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 */
4095 win_T *
4096buf_jump_open_win(buf)
4097 buf_T *buf;
4098{
4099# ifdef FEAT_WINDOWS
4100 win_T *wp;
4101
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004102 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 if (wp->w_buffer == buf)
4104 break;
4105 if (wp != NULL)
4106 win_enter(wp, FALSE);
4107 return wp;
4108# else
4109 if (curwin->w_buffer == buf)
4110 return curwin;
4111 return NULL;
4112# endif
4113}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004114
4115/*
4116 * Jump to the first open window in any tab page that contains buffer "buf",
4117 * if one exists.
4118 * Returns a pointer to the window found, otherwise NULL.
4119 */
4120 win_T *
4121buf_jump_open_tab(buf)
4122 buf_T *buf;
4123{
4124# ifdef FEAT_WINDOWS
4125 win_T *wp;
4126 tabpage_T *tp;
4127
4128 /* First try the current tab page. */
4129 wp = buf_jump_open_win(buf);
4130 if (wp != NULL)
4131 return wp;
4132
4133 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4134 if (tp != curtab)
4135 {
4136 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4137 if (wp->w_buffer == buf)
4138 break;
4139 if (wp != NULL)
4140 {
4141 goto_tabpage_win(tp, wp);
4142 if (curwin != wp)
4143 wp = NULL; /* something went wrong */
4144 break;
4145 }
4146 }
4147
4148 return wp;
4149# else
4150 if (curwin->w_buffer == buf)
4151 return curwin;
4152 return NULL;
4153# endif
4154}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155#endif
4156
4157/*
4158 * allocate a window structure and link it in the window list
4159 */
4160/*ARGSUSED*/
4161 static win_T *
4162win_alloc(after)
4163 win_T *after;
4164{
4165 win_T *newwin;
4166
4167 /*
4168 * allocate window structure and linesizes arrays
4169 */
4170 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4171 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4172 {
4173 vim_free(newwin);
4174 newwin = NULL;
4175 }
4176
4177 if (newwin != NULL)
4178 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004179#ifdef FEAT_AUTOCMD
4180 /* Don't execute autocommands while the window is not properly
4181 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4182 * event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004183 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 /*
4186 * link the window in the window list
4187 */
4188#ifdef FEAT_WINDOWS
4189 win_append(after, newwin);
4190#endif
4191#ifdef FEAT_VERTSPLIT
4192 newwin->w_wincol = 0;
4193 newwin->w_width = Columns;
4194#endif
4195
4196 /* position the display and the cursor at the top of the file. */
4197 newwin->w_topline = 1;
4198#ifdef FEAT_DIFF
4199 newwin->w_topfill = 0;
4200#endif
4201 newwin->w_botline = 2;
4202 newwin->w_cursor.lnum = 1;
4203#ifdef FEAT_SCROLLBIND
4204 newwin->w_scbind_pos = 1;
4205#endif
4206
4207 /* We won't calculate w_fraction until resizing the window */
4208 newwin->w_fraction = 0;
4209 newwin->w_prev_fraction_row = -1;
4210
4211#ifdef FEAT_GUI
4212 if (gui.in_use)
4213 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4215 SBAR_LEFT, newwin);
4216 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4217 SBAR_RIGHT, newwin);
4218 }
4219#endif
4220#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004221 /* init w: variables */
4222 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223#endif
4224#ifdef FEAT_FOLDING
4225 foldInitWin(newwin);
4226#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004227#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004228 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004229#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004230#ifdef FEAT_SEARCH_EXTRA
4231 newwin->w_match_head = NULL;
4232 newwin->w_next_match_id = 4;
4233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
4235 return newwin;
4236}
4237
4238#if defined(FEAT_WINDOWS) || defined(PROTO)
4239
4240/*
4241 * remove window 'wp' from the window list and free the structure
4242 */
4243 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004244win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004246 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247{
4248 int i;
4249
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004250#ifdef FEAT_AUTOCMD
4251 /* Don't execute autocommands while the window is halfway being deleted.
4252 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004253 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004254#endif
4255
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004256#ifdef FEAT_MZSCHEME
4257 mzscheme_window_free(wp);
4258#endif
4259
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260#ifdef FEAT_PERL
4261 perl_win_free(wp);
4262#endif
4263
4264#ifdef FEAT_PYTHON
4265 python_window_free(wp);
4266#endif
4267
4268#ifdef FEAT_TCL
4269 tcl_window_free(wp);
4270#endif
4271
4272#ifdef FEAT_RUBY
4273 ruby_window_free(wp);
4274#endif
4275
4276 clear_winopt(&wp->w_onebuf_opt);
4277 clear_winopt(&wp->w_allbuf_opt);
4278
4279#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004280 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281#endif
4282
4283 if (prevwin == wp)
4284 prevwin = NULL;
4285 win_free_lsize(wp);
4286
4287 for (i = 0; i < wp->w_tagstacklen; ++i)
4288 vim_free(wp->w_tagstack[i].tagname);
4289
4290 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004291
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004293 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004295
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296#ifdef FEAT_JUMPLIST
4297 free_jumplist(wp);
4298#endif
4299
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004300#ifdef FEAT_QUICKFIX
4301 qf_free_all(wp);
4302#endif
4303
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304#ifdef FEAT_GUI
4305 if (gui.in_use)
4306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4308 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4309 }
4310#endif /* FEAT_GUI */
4311
Bram Moolenaarf740b292006-02-16 22:11:02 +00004312 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004314
4315#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004316 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318}
4319
4320/*
4321 * Append window "wp" in the window list after window "after".
4322 */
4323 static void
4324win_append(after, wp)
4325 win_T *after, *wp;
4326{
4327 win_T *before;
4328
4329 if (after == NULL) /* after NULL is in front of the first */
4330 before = firstwin;
4331 else
4332 before = after->w_next;
4333
4334 wp->w_next = before;
4335 wp->w_prev = after;
4336 if (after == NULL)
4337 firstwin = wp;
4338 else
4339 after->w_next = wp;
4340 if (before == NULL)
4341 lastwin = wp;
4342 else
4343 before->w_prev = wp;
4344}
4345
4346/*
4347 * Remove a window from the window list.
4348 */
4349 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004350win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004352 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353{
4354 if (wp->w_prev != NULL)
4355 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004356 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004358 else
4359 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 if (wp->w_next != NULL)
4361 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004362 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004364 else
4365 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366}
4367
4368/*
4369 * Append frame "frp" in a frame list after frame "after".
4370 */
4371 static void
4372frame_append(after, frp)
4373 frame_T *after, *frp;
4374{
4375 frp->fr_next = after->fr_next;
4376 after->fr_next = frp;
4377 if (frp->fr_next != NULL)
4378 frp->fr_next->fr_prev = frp;
4379 frp->fr_prev = after;
4380}
4381
4382/*
4383 * Insert frame "frp" in a frame list before frame "before".
4384 */
4385 static void
4386frame_insert(before, frp)
4387 frame_T *before, *frp;
4388{
4389 frp->fr_next = before;
4390 frp->fr_prev = before->fr_prev;
4391 before->fr_prev = frp;
4392 if (frp->fr_prev != NULL)
4393 frp->fr_prev->fr_next = frp;
4394 else
4395 frp->fr_parent->fr_child = frp;
4396}
4397
4398/*
4399 * Remove a frame from a frame list.
4400 */
4401 static void
4402frame_remove(frp)
4403 frame_T *frp;
4404{
4405 if (frp->fr_prev != NULL)
4406 frp->fr_prev->fr_next = frp->fr_next;
4407 else
4408 frp->fr_parent->fr_child = frp->fr_next;
4409 if (frp->fr_next != NULL)
4410 frp->fr_next->fr_prev = frp->fr_prev;
4411}
4412
4413#endif /* FEAT_WINDOWS */
4414
4415/*
4416 * Allocate w_lines[] for window "wp".
4417 * Return FAIL for failure, OK for success.
4418 */
4419 int
4420win_alloc_lines(wp)
4421 win_T *wp;
4422{
4423 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004424 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 if (wp->w_lines == NULL)
4426 return FAIL;
4427 return OK;
4428}
4429
4430/*
4431 * free lsize arrays for a window
4432 */
4433 void
4434win_free_lsize(wp)
4435 win_T *wp;
4436{
4437 vim_free(wp->w_lines);
4438 wp->w_lines = NULL;
4439}
4440
4441/*
4442 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004443 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 */
4445 void
4446shell_new_rows()
4447{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004448 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
4450 if (firstwin == NULL) /* not initialized yet */
4451 return;
4452#ifdef FEAT_WINDOWS
4453 if (h < frame_minheight(topframe, NULL))
4454 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004455
4456 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 * that doesn't result in the right height, forget about that option. */
4458 frame_new_height(topframe, h, FALSE, TRUE);
4459 if (topframe->fr_height != h)
4460 frame_new_height(topframe, h, FALSE, FALSE);
4461
4462 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4463#else
4464 if (h < 1)
4465 h = 1;
4466 win_new_height(firstwin, h);
4467#endif
4468 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004469#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004470 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004471#endif
4472
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473#if 0
4474 /* Disabled: don't want making the screen smaller make a window larger. */
4475 if (p_ea)
4476 win_equal(curwin, FALSE, 'v');
4477#endif
4478}
4479
4480#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4481/*
4482 * Called from win_new_shellsize() after Columns changed.
4483 */
4484 void
4485shell_new_columns()
4486{
4487 if (firstwin == NULL) /* not initialized yet */
4488 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004489
4490 /* First try setting the widths of windows with 'winfixwidth'. If that
4491 * doesn't result in the right width, forget about that option. */
4492 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4493 if (topframe->fr_width != Columns)
4494 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4495
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4497#if 0
4498 /* Disabled: don't want making the screen smaller make a window larger. */
4499 if (p_ea)
4500 win_equal(curwin, FALSE, 'h');
4501#endif
4502}
4503#endif
4504
4505#if defined(FEAT_CMDWIN) || defined(PROTO)
4506/*
4507 * Save the size of all windows in "gap".
4508 */
4509 void
4510win_size_save(gap)
4511 garray_T *gap;
4512
4513{
4514 win_T *wp;
4515
4516 ga_init2(gap, (int)sizeof(int), 1);
4517 if (ga_grow(gap, win_count() * 2) == OK)
4518 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4519 {
4520 ((int *)gap->ga_data)[gap->ga_len++] =
4521 wp->w_width + wp->w_vsep_width;
4522 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4523 }
4524}
4525
4526/*
4527 * Restore window sizes, but only if the number of windows is still the same.
4528 * Does not free the growarray.
4529 */
4530 void
4531win_size_restore(gap)
4532 garray_T *gap;
4533{
4534 win_T *wp;
4535 int i;
4536
4537 if (win_count() * 2 == gap->ga_len)
4538 {
4539 i = 0;
4540 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4541 {
4542 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4543 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4544 }
4545 /* recompute the window positions */
4546 (void)win_comp_pos();
4547 }
4548}
4549#endif /* FEAT_CMDWIN */
4550
4551#if defined(FEAT_WINDOWS) || defined(PROTO)
4552/*
4553 * Update the position for all windows, using the width and height of the
4554 * frames.
4555 * Returns the row just after the last window.
4556 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004557 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558win_comp_pos()
4559{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004560 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 int col = 0;
4562
4563 frame_comp_pos(topframe, &row, &col);
4564 return row;
4565}
4566
4567/*
4568 * Update the position of the windows in frame "topfrp", using the width and
4569 * height of the frames.
4570 * "*row" and "*col" are the top-left position of the frame. They are updated
4571 * to the bottom-right position plus one.
4572 */
4573 static void
4574frame_comp_pos(topfrp, row, col)
4575 frame_T *topfrp;
4576 int *row;
4577 int *col;
4578{
4579 win_T *wp;
4580 frame_T *frp;
4581#ifdef FEAT_VERTSPLIT
4582 int startcol;
4583 int startrow;
4584#endif
4585
4586 wp = topfrp->fr_win;
4587 if (wp != NULL)
4588 {
4589 if (wp->w_winrow != *row
4590#ifdef FEAT_VERTSPLIT
4591 || wp->w_wincol != *col
4592#endif
4593 )
4594 {
4595 /* position changed, redraw */
4596 wp->w_winrow = *row;
4597#ifdef FEAT_VERTSPLIT
4598 wp->w_wincol = *col;
4599#endif
4600 redraw_win_later(wp, NOT_VALID);
4601 wp->w_redr_status = TRUE;
4602 }
4603 *row += wp->w_height + wp->w_status_height;
4604#ifdef FEAT_VERTSPLIT
4605 *col += wp->w_width + wp->w_vsep_width;
4606#endif
4607 }
4608 else
4609 {
4610#ifdef FEAT_VERTSPLIT
4611 startrow = *row;
4612 startcol = *col;
4613#endif
4614 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4615 {
4616#ifdef FEAT_VERTSPLIT
4617 if (topfrp->fr_layout == FR_ROW)
4618 *row = startrow; /* all frames are at the same row */
4619 else
4620 *col = startcol; /* all frames are at the same col */
4621#endif
4622 frame_comp_pos(frp, row, col);
4623 }
4624 }
4625}
4626
4627#endif /* FEAT_WINDOWS */
4628
4629/*
4630 * Set current window height and take care of repositioning other windows to
4631 * fit around it.
4632 */
4633 void
4634win_setheight(height)
4635 int height;
4636{
4637 win_setheight_win(height, curwin);
4638}
4639
4640/*
4641 * Set the window height of window "win" and take care of repositioning other
4642 * windows to fit around it.
4643 */
4644 void
4645win_setheight_win(height, win)
4646 int height;
4647 win_T *win;
4648{
4649 int row;
4650
4651 if (win == curwin)
4652 {
4653 /* Always keep current window at least one line high, even when
4654 * 'winminheight' is zero. */
4655#ifdef FEAT_WINDOWS
4656 if (height < p_wmh)
4657 height = p_wmh;
4658#endif
4659 if (height == 0)
4660 height = 1;
4661 }
4662
4663#ifdef FEAT_WINDOWS
4664 frame_setheight(win->w_frame, height + win->w_status_height);
4665
4666 /* recompute the window positions */
4667 row = win_comp_pos();
4668#else
4669 if (height > topframe->fr_height)
4670 height = topframe->fr_height;
4671 win->w_height = height;
4672 row = height;
4673#endif
4674
4675 /*
4676 * If there is extra space created between the last window and the command
4677 * line, clear it.
4678 */
4679 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4680 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4681 cmdline_row = row;
4682 msg_row = row;
4683 msg_col = 0;
4684
4685 redraw_all_later(NOT_VALID);
4686}
4687
4688#if defined(FEAT_WINDOWS) || defined(PROTO)
4689
4690/*
4691 * Set the height of a frame to "height" and take care that all frames and
4692 * windows inside it are resized. Also resize frames on the left and right if
4693 * the are in the same FR_ROW frame.
4694 *
4695 * Strategy:
4696 * If the frame is part of a FR_COL frame, try fitting the frame in that
4697 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4698 * go to containing frames to resize them and make room.
4699 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4700 * Check for the minimal height of the FR_ROW frame.
4701 * At the top level we can also use change the command line height.
4702 */
4703 static void
4704frame_setheight(curfrp, height)
4705 frame_T *curfrp;
4706 int height;
4707{
4708 int room; /* total number of lines available */
4709 int take; /* number of lines taken from other windows */
4710 int room_cmdline; /* lines available from cmdline */
4711 int run;
4712 frame_T *frp;
4713 int h;
4714 int room_reserved;
4715
4716 /* If the height already is the desired value, nothing to do. */
4717 if (curfrp->fr_height == height)
4718 return;
4719
4720 if (curfrp->fr_parent == NULL)
4721 {
4722 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004723 if (height > ROWS_AVAIL)
4724 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 if (height > 0)
4726 frame_new_height(curfrp, height, FALSE, FALSE);
4727 }
4728 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4729 {
4730 /* Row of frames: Also need to resize frames left and right of this
4731 * one. First check for the minimal height of these. */
4732 h = frame_minheight(curfrp->fr_parent, NULL);
4733 if (height < h)
4734 height = h;
4735 frame_setheight(curfrp->fr_parent, height);
4736 }
4737 else
4738 {
4739 /*
4740 * Column of frames: try to change only frames in this column.
4741 */
4742#ifdef FEAT_VERTSPLIT
4743 /*
4744 * Do this twice:
4745 * 1: compute room available, if it's not enough try resizing the
4746 * containing frame.
4747 * 2: compute the room available and adjust the height to it.
4748 * Try not to reduce the height of a window with 'winfixheight' set.
4749 */
4750 for (run = 1; run <= 2; ++run)
4751#else
4752 for (;;)
4753#endif
4754 {
4755 room = 0;
4756 room_reserved = 0;
4757 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4758 frp = frp->fr_next)
4759 {
4760 if (frp != curfrp
4761 && frp->fr_win != NULL
4762 && frp->fr_win->w_p_wfh)
4763 room_reserved += frp->fr_height;
4764 room += frp->fr_height;
4765 if (frp != curfrp)
4766 room -= frame_minheight(frp, NULL);
4767 }
4768#ifdef FEAT_VERTSPLIT
4769 if (curfrp->fr_width != Columns)
4770 room_cmdline = 0;
4771 else
4772#endif
4773 {
4774 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4775 + lastwin->w_height + lastwin->w_status_height);
4776 if (room_cmdline < 0)
4777 room_cmdline = 0;
4778 }
4779
4780 if (height <= room + room_cmdline)
4781 break;
4782#ifdef FEAT_VERTSPLIT
4783 if (run == 2 || curfrp->fr_width == Columns)
4784#endif
4785 {
4786 if (height > room + room_cmdline)
4787 height = room + room_cmdline;
4788 break;
4789 }
4790#ifdef FEAT_VERTSPLIT
4791 frame_setheight(curfrp->fr_parent, height
4792 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4793#endif
4794 /*NOTREACHED*/
4795 }
4796
4797 /*
4798 * Compute the number of lines we will take from others frames (can be
4799 * negative!).
4800 */
4801 take = height - curfrp->fr_height;
4802
4803 /* If there is not enough room, also reduce the height of a window
4804 * with 'winfixheight' set. */
4805 if (height > room + room_cmdline - room_reserved)
4806 room_reserved = room + room_cmdline - height;
4807 /* If there is only a 'winfixheight' window and making the
4808 * window smaller, need to make the other window taller. */
4809 if (take < 0 && room - curfrp->fr_height < room_reserved)
4810 room_reserved = 0;
4811
4812 if (take > 0 && room_cmdline > 0)
4813 {
4814 /* use lines from cmdline first */
4815 if (take < room_cmdline)
4816 room_cmdline = take;
4817 take -= room_cmdline;
4818 topframe->fr_height += room_cmdline;
4819 }
4820
4821 /*
4822 * set the current frame to the new height
4823 */
4824 frame_new_height(curfrp, height, FALSE, FALSE);
4825
4826 /*
4827 * First take lines from the frames after the current frame. If
4828 * that is not enough, takes lines from frames above the current
4829 * frame.
4830 */
4831 for (run = 0; run < 2; ++run)
4832 {
4833 if (run == 0)
4834 frp = curfrp->fr_next; /* 1st run: start with next window */
4835 else
4836 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4837 while (frp != NULL && take != 0)
4838 {
4839 h = frame_minheight(frp, NULL);
4840 if (room_reserved > 0
4841 && frp->fr_win != NULL
4842 && frp->fr_win->w_p_wfh)
4843 {
4844 if (room_reserved >= frp->fr_height)
4845 room_reserved -= frp->fr_height;
4846 else
4847 {
4848 if (frp->fr_height - room_reserved > take)
4849 room_reserved = frp->fr_height - take;
4850 take -= frp->fr_height - room_reserved;
4851 frame_new_height(frp, room_reserved, FALSE, FALSE);
4852 room_reserved = 0;
4853 }
4854 }
4855 else
4856 {
4857 if (frp->fr_height - take < h)
4858 {
4859 take -= frp->fr_height - h;
4860 frame_new_height(frp, h, FALSE, FALSE);
4861 }
4862 else
4863 {
4864 frame_new_height(frp, frp->fr_height - take,
4865 FALSE, FALSE);
4866 take = 0;
4867 }
4868 }
4869 if (run == 0)
4870 frp = frp->fr_next;
4871 else
4872 frp = frp->fr_prev;
4873 }
4874 }
4875 }
4876}
4877
4878#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4879/*
4880 * Set current window width and take care of repositioning other windows to
4881 * fit around it.
4882 */
4883 void
4884win_setwidth(width)
4885 int width;
4886{
4887 win_setwidth_win(width, curwin);
4888}
4889
4890 void
4891win_setwidth_win(width, wp)
4892 int width;
4893 win_T *wp;
4894{
4895 /* Always keep current window at least one column wide, even when
4896 * 'winminwidth' is zero. */
4897 if (wp == curwin)
4898 {
4899 if (width < p_wmw)
4900 width = p_wmw;
4901 if (width == 0)
4902 width = 1;
4903 }
4904
4905 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4906
4907 /* recompute the window positions */
4908 (void)win_comp_pos();
4909
4910 redraw_all_later(NOT_VALID);
4911}
4912
4913/*
4914 * Set the width of a frame to "width" and take care that all frames and
4915 * windows inside it are resized. Also resize frames above and below if the
4916 * are in the same FR_ROW frame.
4917 *
4918 * Strategy is similar to frame_setheight().
4919 */
4920 static void
4921frame_setwidth(curfrp, width)
4922 frame_T *curfrp;
4923 int width;
4924{
4925 int room; /* total number of lines available */
4926 int take; /* number of lines taken from other windows */
4927 int run;
4928 frame_T *frp;
4929 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004930 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931
4932 /* If the width already is the desired value, nothing to do. */
4933 if (curfrp->fr_width == width)
4934 return;
4935
4936 if (curfrp->fr_parent == NULL)
4937 /* topframe: can't change width */
4938 return;
4939
4940 if (curfrp->fr_parent->fr_layout == FR_COL)
4941 {
4942 /* Column of frames: Also need to resize frames above and below of
4943 * this one. First check for the minimal width of these. */
4944 w = frame_minwidth(curfrp->fr_parent, NULL);
4945 if (width < w)
4946 width = w;
4947 frame_setwidth(curfrp->fr_parent, width);
4948 }
4949 else
4950 {
4951 /*
4952 * Row of frames: try to change only frames in this row.
4953 *
4954 * Do this twice:
4955 * 1: compute room available, if it's not enough try resizing the
4956 * containing frame.
4957 * 2: compute the room available and adjust the width to it.
4958 */
4959 for (run = 1; run <= 2; ++run)
4960 {
4961 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004962 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4964 frp = frp->fr_next)
4965 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004966 if (frp != curfrp
4967 && frp->fr_win != NULL
4968 && frp->fr_win->w_p_wfw)
4969 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 room += frp->fr_width;
4971 if (frp != curfrp)
4972 room -= frame_minwidth(frp, NULL);
4973 }
4974
4975 if (width <= room)
4976 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004977 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
4979 if (width > room)
4980 width = room;
4981 break;
4982 }
4983 frame_setwidth(curfrp->fr_parent, width
4984 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4985 }
4986
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 /*
4988 * Compute the number of lines we will take from others frames (can be
4989 * negative!).
4990 */
4991 take = width - curfrp->fr_width;
4992
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004993 /* If there is not enough room, also reduce the width of a window
4994 * with 'winfixwidth' set. */
4995 if (width > room - room_reserved)
4996 room_reserved = room - width;
4997 /* If there is only a 'winfixwidth' window and making the
4998 * window smaller, need to make the other window narrower. */
4999 if (take < 0 && room - curfrp->fr_width < room_reserved)
5000 room_reserved = 0;
5001
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 /*
5003 * set the current frame to the new width
5004 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005005 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006
5007 /*
5008 * First take lines from the frames right of the current frame. If
5009 * that is not enough, takes lines from frames left of the current
5010 * frame.
5011 */
5012 for (run = 0; run < 2; ++run)
5013 {
5014 if (run == 0)
5015 frp = curfrp->fr_next; /* 1st run: start with next window */
5016 else
5017 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5018 while (frp != NULL && take != 0)
5019 {
5020 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005021 if (room_reserved > 0
5022 && frp->fr_win != NULL
5023 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005025 if (room_reserved >= frp->fr_width)
5026 room_reserved -= frp->fr_width;
5027 else
5028 {
5029 if (frp->fr_width - room_reserved > take)
5030 room_reserved = frp->fr_width - take;
5031 take -= frp->fr_width - room_reserved;
5032 frame_new_width(frp, room_reserved, FALSE, FALSE);
5033 room_reserved = 0;
5034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 }
5036 else
5037 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005038 if (frp->fr_width - take < w)
5039 {
5040 take -= frp->fr_width - w;
5041 frame_new_width(frp, w, FALSE, FALSE);
5042 }
5043 else
5044 {
5045 frame_new_width(frp, frp->fr_width - take,
5046 FALSE, FALSE);
5047 take = 0;
5048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 }
5050 if (run == 0)
5051 frp = frp->fr_next;
5052 else
5053 frp = frp->fr_prev;
5054 }
5055 }
5056 }
5057}
5058#endif /* FEAT_VERTSPLIT */
5059
5060/*
5061 * Check 'winminheight' for a valid value.
5062 */
5063 void
5064win_setminheight()
5065{
5066 int room;
5067 int first = TRUE;
5068 win_T *wp;
5069
5070 /* loop until there is a 'winminheight' that is possible */
5071 while (p_wmh > 0)
5072 {
5073 /* TODO: handle vertical splits */
5074 room = -p_wh;
5075 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5076 room += wp->w_height - p_wmh;
5077 if (room >= 0)
5078 break;
5079 --p_wmh;
5080 if (first)
5081 {
5082 EMSG(_(e_noroom));
5083 first = FALSE;
5084 }
5085 }
5086}
5087
5088#ifdef FEAT_MOUSE
5089
5090/*
5091 * Status line of dragwin is dragged "offset" lines down (negative is up).
5092 */
5093 void
5094win_drag_status_line(dragwin, offset)
5095 win_T *dragwin;
5096 int offset;
5097{
5098 frame_T *curfr;
5099 frame_T *fr;
5100 int room;
5101 int row;
5102 int up; /* if TRUE, drag status line up, otherwise down */
5103 int n;
5104
5105 fr = dragwin->w_frame;
5106 curfr = fr;
5107 if (fr != topframe) /* more than one window */
5108 {
5109 fr = fr->fr_parent;
5110 /* When the parent frame is not a column of frames, its parent should
5111 * be. */
5112 if (fr->fr_layout != FR_COL)
5113 {
5114 curfr = fr;
5115 if (fr != topframe) /* only a row of windows, may drag statusline */
5116 fr = fr->fr_parent;
5117 }
5118 }
5119
5120 /* If this is the last frame in a column, may want to resize the parent
5121 * frame instead (go two up to skip a row of frames). */
5122 while (curfr != topframe && curfr->fr_next == NULL)
5123 {
5124 if (fr != topframe)
5125 fr = fr->fr_parent;
5126 curfr = fr;
5127 if (fr != topframe)
5128 fr = fr->fr_parent;
5129 }
5130
5131 if (offset < 0) /* drag up */
5132 {
5133 up = TRUE;
5134 offset = -offset;
5135 /* sum up the room of the current frame and above it */
5136 if (fr == curfr)
5137 {
5138 /* only one window */
5139 room = fr->fr_height - frame_minheight(fr, NULL);
5140 }
5141 else
5142 {
5143 room = 0;
5144 for (fr = fr->fr_child; ; fr = fr->fr_next)
5145 {
5146 room += fr->fr_height - frame_minheight(fr, NULL);
5147 if (fr == curfr)
5148 break;
5149 }
5150 }
5151 fr = curfr->fr_next; /* put fr at frame that grows */
5152 }
5153 else /* drag down */
5154 {
5155 up = FALSE;
5156 /*
5157 * Only dragging the last status line can reduce p_ch.
5158 */
5159 room = Rows - cmdline_row;
5160 if (curfr->fr_next == NULL)
5161 room -= 1;
5162 else
5163 room -= p_ch;
5164 if (room < 0)
5165 room = 0;
5166 /* sum up the room of frames below of the current one */
5167 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5168 room += fr->fr_height - frame_minheight(fr, NULL);
5169 fr = curfr; /* put fr at window that grows */
5170 }
5171
5172 if (room < offset) /* Not enough room */
5173 offset = room; /* Move as far as we can */
5174 if (offset <= 0)
5175 return;
5176
5177 /*
5178 * Grow frame fr by "offset" lines.
5179 * Doesn't happen when dragging the last status line up.
5180 */
5181 if (fr != NULL)
5182 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5183
5184 if (up)
5185 fr = curfr; /* current frame gets smaller */
5186 else
5187 fr = curfr->fr_next; /* next frame gets smaller */
5188
5189 /*
5190 * Now make the other frames smaller.
5191 */
5192 while (fr != NULL && offset > 0)
5193 {
5194 n = frame_minheight(fr, NULL);
5195 if (fr->fr_height - offset <= n)
5196 {
5197 offset -= fr->fr_height - n;
5198 frame_new_height(fr, n, !up, FALSE);
5199 }
5200 else
5201 {
5202 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5203 break;
5204 }
5205 if (up)
5206 fr = fr->fr_prev;
5207 else
5208 fr = fr->fr_next;
5209 }
5210 row = win_comp_pos();
5211 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5212 cmdline_row = row;
5213 p_ch = Rows - cmdline_row;
5214 if (p_ch < 1)
5215 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005216 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005217 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 showmode();
5219}
5220
5221#ifdef FEAT_VERTSPLIT
5222/*
5223 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5224 */
5225 void
5226win_drag_vsep_line(dragwin, offset)
5227 win_T *dragwin;
5228 int offset;
5229{
5230 frame_T *curfr;
5231 frame_T *fr;
5232 int room;
5233 int left; /* if TRUE, drag separator line left, otherwise right */
5234 int n;
5235
5236 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005237 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 return;
5239 curfr = fr;
5240 fr = fr->fr_parent;
5241 /* When the parent frame is not a row of frames, its parent should be. */
5242 if (fr->fr_layout != FR_ROW)
5243 {
5244 if (fr == topframe) /* only a column of windows (cannot happen?) */
5245 return;
5246 curfr = fr;
5247 fr = fr->fr_parent;
5248 }
5249
5250 /* If this is the last frame in a row, may want to resize a parent
5251 * frame instead. */
5252 while (curfr->fr_next == NULL)
5253 {
5254 if (fr == topframe)
5255 break;
5256 curfr = fr;
5257 fr = fr->fr_parent;
5258 if (fr != topframe)
5259 {
5260 curfr = fr;
5261 fr = fr->fr_parent;
5262 }
5263 }
5264
5265 if (offset < 0) /* drag left */
5266 {
5267 left = TRUE;
5268 offset = -offset;
5269 /* sum up the room of the current frame and left of it */
5270 room = 0;
5271 for (fr = fr->fr_child; ; fr = fr->fr_next)
5272 {
5273 room += fr->fr_width - frame_minwidth(fr, NULL);
5274 if (fr == curfr)
5275 break;
5276 }
5277 fr = curfr->fr_next; /* put fr at frame that grows */
5278 }
5279 else /* drag right */
5280 {
5281 left = FALSE;
5282 /* sum up the room of frames right of the current one */
5283 room = 0;
5284 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5285 room += fr->fr_width - frame_minwidth(fr, NULL);
5286 fr = curfr; /* put fr at window that grows */
5287 }
5288
5289 if (room < offset) /* Not enough room */
5290 offset = room; /* Move as far as we can */
5291 if (offset <= 0) /* No room at all, quit. */
5292 return;
5293
5294 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005295 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
5297 /* shrink other frames: current and at the left or at the right */
5298 if (left)
5299 fr = curfr; /* current frame gets smaller */
5300 else
5301 fr = curfr->fr_next; /* next frame gets smaller */
5302
5303 while (fr != NULL && offset > 0)
5304 {
5305 n = frame_minwidth(fr, NULL);
5306 if (fr->fr_width - offset <= n)
5307 {
5308 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005309 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 }
5311 else
5312 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005313 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 break;
5315 }
5316 if (left)
5317 fr = fr->fr_prev;
5318 else
5319 fr = fr->fr_next;
5320 }
5321 (void)win_comp_pos();
5322 redraw_all_later(NOT_VALID);
5323}
5324#endif /* FEAT_VERTSPLIT */
5325#endif /* FEAT_MOUSE */
5326
5327#endif /* FEAT_WINDOWS */
5328
5329/*
5330 * Set the height of a window.
5331 * This takes care of the things inside the window, not what happens to the
5332 * window position, the frame or to other windows.
5333 */
5334 static void
5335win_new_height(wp, height)
5336 win_T *wp;
5337 int height;
5338{
5339 linenr_T lnum;
5340 int sline, line_size;
5341#define FRACTION_MULT 16384L
5342
5343 /* Don't want a negative height. Happens when splitting a tiny window.
5344 * Will equalize heights soon to fix it. */
5345 if (height < 0)
5346 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005347 if (wp->w_height == height)
5348 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349
5350 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5351 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5352 + FRACTION_MULT / 2) / (long)wp->w_height;
5353
5354 wp->w_height = height;
5355 wp->w_skipcol = 0;
5356
5357 /* Don't change w_topline when height is zero. Don't set w_topline when
5358 * 'scrollbind' is set and this isn't the current window. */
5359 if (height > 0
5360#ifdef FEAT_SCROLLBIND
5361 && (!wp->w_p_scb || wp == curwin)
5362#endif
5363 )
5364 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005365 /*
5366 * Find a value for w_topline that shows the cursor at the same
5367 * relative position in the window as before (more or less).
5368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 lnum = wp->w_cursor.lnum;
5370 if (lnum < 1) /* can happen when starting up */
5371 lnum = 1;
5372 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5373 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5374 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005375
5376 if (sline >= 0)
5377 {
5378 /* Make sure the whole cursor line is visible, if possible. */
5379 int rows = plines_win(wp, lnum, FALSE);
5380
5381 if (sline > wp->w_height - rows)
5382 {
5383 sline = wp->w_height - rows;
5384 wp->w_wrow -= rows - line_size;
5385 }
5386 }
5387
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 if (sline < 0)
5389 {
5390 /*
5391 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005392 * Make cursor line the first line in the window. If not enough
5393 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 */
5395 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005396 if (wp->w_wrow >= wp->w_height
5397 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5398 {
5399 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5400 --wp->w_wrow;
5401 while (wp->w_wrow >= wp->w_height)
5402 {
5403 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5404 + win_col_off2(wp);
5405 --wp->w_wrow;
5406 }
5407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 }
5409 else
5410 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005411 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 {
5413#ifdef FEAT_FOLDING
5414 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5415 if (lnum == 1)
5416 {
5417 /* first line in buffer is folded */
5418 line_size = 1;
5419 --sline;
5420 break;
5421 }
5422#endif
5423 --lnum;
5424#ifdef FEAT_DIFF
5425 if (lnum == wp->w_topline)
5426 line_size = plines_win_nofill(wp, lnum, TRUE)
5427 + wp->w_topfill;
5428 else
5429#endif
5430 line_size = plines_win(wp, lnum, TRUE);
5431 sline -= line_size;
5432 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005433
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 if (sline < 0)
5435 {
5436 /*
5437 * Line we want at top would go off top of screen. Use next
5438 * line instead.
5439 */
5440#ifdef FEAT_FOLDING
5441 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5442#endif
5443 lnum++;
5444 wp->w_wrow -= line_size + sline;
5445 }
5446 else if (sline > 0)
5447 {
5448 /* First line of file reached, use that as topline. */
5449 lnum = 1;
5450 wp->w_wrow -= sline;
5451 }
5452 }
5453 set_topline(wp, lnum);
5454 }
5455
5456 if (wp == curwin)
5457 {
5458 if (p_so)
5459 update_topline();
5460 curs_columns(FALSE); /* validate w_wrow */
5461 }
5462 wp->w_prev_fraction_row = wp->w_wrow;
5463
5464 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005465 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466#ifdef FEAT_WINDOWS
5467 wp->w_redr_status = TRUE;
5468#endif
5469 invalidate_botline_win(wp);
5470}
5471
5472#ifdef FEAT_VERTSPLIT
5473/*
5474 * Set the width of a window.
5475 */
5476 static void
5477win_new_width(wp, width)
5478 win_T *wp;
5479 int width;
5480{
5481 wp->w_width = width;
5482 wp->w_lines_valid = 0;
5483 changed_line_abv_curs_win(wp);
5484 invalidate_botline_win(wp);
5485 if (wp == curwin)
5486 {
5487 update_topline();
5488 curs_columns(TRUE); /* validate w_wrow */
5489 }
5490 redraw_win_later(wp, NOT_VALID);
5491 wp->w_redr_status = TRUE;
5492}
5493#endif
5494
5495 void
5496win_comp_scroll(wp)
5497 win_T *wp;
5498{
5499 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5500 if (wp->w_p_scr == 0)
5501 wp->w_p_scr = 1;
5502}
5503
5504/*
5505 * command_height: called whenever p_ch has been changed
5506 */
5507 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005508command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509{
5510#ifdef FEAT_WINDOWS
5511 int h;
5512 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005513 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005515 /* Use the value of p_ch that we remembered. This is needed for when the
5516 * GUI starts up, we can't be sure in what order things happen. And when
5517 * p_ch was changed in another tab page. */
5518 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005519
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 /* Find bottom frame with width of screen. */
5521 frp = lastwin->w_frame;
5522# ifdef FEAT_VERTSPLIT
5523 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5524 frp = frp->fr_parent;
5525# endif
5526
5527 /* Avoid changing the height of a window with 'winfixheight' set. */
5528 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5529 && frp->fr_win->w_p_wfh)
5530 frp = frp->fr_prev;
5531
5532 if (starting != NO_SCREEN)
5533 {
5534 cmdline_row = Rows - p_ch;
5535
5536 if (p_ch > old_p_ch) /* p_ch got bigger */
5537 {
5538 while (p_ch > old_p_ch)
5539 {
5540 if (frp == NULL)
5541 {
5542 EMSG(_(e_noroom));
5543 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005544 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 cmdline_row = Rows - p_ch;
5546 break;
5547 }
5548 h = frp->fr_height - frame_minheight(frp, NULL);
5549 if (h > p_ch - old_p_ch)
5550 h = p_ch - old_p_ch;
5551 old_p_ch += h;
5552 frame_add_height(frp, -h);
5553 frp = frp->fr_prev;
5554 }
5555
5556 /* Recompute window positions. */
5557 (void)win_comp_pos();
5558
5559 /* clear the lines added to cmdline */
5560 if (full_screen)
5561 screen_fill((int)(cmdline_row), (int)Rows, 0,
5562 (int)Columns, ' ', ' ', 0);
5563 msg_row = cmdline_row;
5564 redraw_cmdline = TRUE;
5565 return;
5566 }
5567
5568 if (msg_row < cmdline_row)
5569 msg_row = cmdline_row;
5570 redraw_cmdline = TRUE;
5571 }
5572 frame_add_height(frp, (int)(old_p_ch - p_ch));
5573
5574 /* Recompute window positions. */
5575 if (frp != lastwin->w_frame)
5576 (void)win_comp_pos();
5577#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005579 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580#endif
5581}
5582
5583#if defined(FEAT_WINDOWS) || defined(PROTO)
5584/*
5585 * Resize frame "frp" to be "n" lines higher (negative for less high).
5586 * Also resize the frames it is contained in.
5587 */
5588 static void
5589frame_add_height(frp, n)
5590 frame_T *frp;
5591 int n;
5592{
5593 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5594 for (;;)
5595 {
5596 frp = frp->fr_parent;
5597 if (frp == NULL)
5598 break;
5599 frp->fr_height += n;
5600 }
5601}
5602
5603/*
5604 * Add or remove a status line for the bottom window(s), according to the
5605 * value of 'laststatus'.
5606 */
5607 void
5608last_status(morewin)
5609 int morewin; /* pretend there are two or more windows */
5610{
5611 /* Don't make a difference between horizontal or vertical split. */
5612 last_status_rec(topframe, (p_ls == 2
5613 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5614}
5615
5616 static void
5617last_status_rec(fr, statusline)
5618 frame_T *fr;
5619 int statusline;
5620{
5621 frame_T *fp;
5622 win_T *wp;
5623
5624 if (fr->fr_layout == FR_LEAF)
5625 {
5626 wp = fr->fr_win;
5627 if (wp->w_status_height != 0 && !statusline)
5628 {
5629 /* remove status line */
5630 win_new_height(wp, wp->w_height + 1);
5631 wp->w_status_height = 0;
5632 comp_col();
5633 }
5634 else if (wp->w_status_height == 0 && statusline)
5635 {
5636 /* Find a frame to take a line from. */
5637 fp = fr;
5638 while (fp->fr_height <= frame_minheight(fp, NULL))
5639 {
5640 if (fp == topframe)
5641 {
5642 EMSG(_(e_noroom));
5643 return;
5644 }
5645 /* In a column of frames: go to frame above. If already at
5646 * the top or in a row of frames: go to parent. */
5647 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5648 fp = fp->fr_prev;
5649 else
5650 fp = fp->fr_parent;
5651 }
5652 wp->w_status_height = 1;
5653 if (fp != fr)
5654 {
5655 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5656 frame_fix_height(wp);
5657 (void)win_comp_pos();
5658 }
5659 else
5660 win_new_height(wp, wp->w_height - 1);
5661 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005662 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 }
5664 }
5665#ifdef FEAT_VERTSPLIT
5666 else if (fr->fr_layout == FR_ROW)
5667 {
5668 /* vertically split windows, set status line for each one */
5669 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5670 last_status_rec(fp, statusline);
5671 }
5672#endif
5673 else
5674 {
5675 /* horizontally split window, set status line for last one */
5676 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5677 ;
5678 last_status_rec(fp, statusline);
5679 }
5680}
5681
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005682/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005683 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005684 */
5685 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005686tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005687{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005688#ifdef FEAT_GUI_TABLINE
5689 /* When the GUI has the tabline then this always returns zero. */
5690 if (gui_use_tabline())
5691 return 0;
5692#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005693 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005694 {
5695 case 0: return 0;
5696 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5697 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005698 return 1;
5699}
5700
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701#endif /* FEAT_WINDOWS */
5702
5703#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5704/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005705 * Get the file name at the cursor.
5706 * If Visual mode is active, use the selected text if it's in one line.
5707 * Returns the name in allocated memory, NULL for failure.
5708 */
5709 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005710grab_file_name(count, file_lnum)
5711 long count;
5712 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005713{
5714# ifdef FEAT_VISUAL
5715 if (VIsual_active)
5716 {
5717 int len;
5718 char_u *ptr;
5719
5720 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5721 return NULL;
5722 return find_file_name_in_path(ptr, len,
5723 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5724 }
5725# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005726 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5727 file_lnum);
5728
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005729}
5730
5731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 * Return the file name under or after the cursor.
5733 *
5734 * The 'path' option is searched if the file name is not absolute.
5735 * The string returned has been alloc'ed and should be freed by the caller.
5736 * NULL is returned if the file name or file is not found.
5737 *
5738 * options:
5739 * FNAME_MESS give error messages
5740 * FNAME_EXP expand to path
5741 * FNAME_HYP check for hypertext link
5742 * FNAME_INCL apply "includeexpr"
5743 */
5744 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005745file_name_at_cursor(options, count, file_lnum)
5746 int options;
5747 long count;
5748 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
5750 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005751 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5752 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753}
5754
5755/*
5756 * Return the name of the file under or after ptr[col].
5757 * Otherwise like file_name_at_cursor().
5758 */
5759 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005760file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 char_u *line;
5762 int col;
5763 int options;
5764 long count;
5765 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005766 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767{
5768 char_u *ptr;
5769 int len;
5770
5771 /*
5772 * search forward for what could be the start of a file name
5773 */
5774 ptr = line + col;
5775 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005776 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 if (*ptr == NUL) /* nothing found */
5778 {
5779 if (options & FNAME_MESS)
5780 EMSG(_("E446: No file name under cursor"));
5781 return NULL;
5782 }
5783
5784 /*
5785 * Search backward for first char of the file name.
5786 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5787 */
5788 while (ptr > line)
5789 {
5790#ifdef FEAT_MBYTE
5791 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5792 ptr -= len + 1;
5793 else
5794#endif
5795 if (vim_isfilec(ptr[-1])
5796 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5797 --ptr;
5798 else
5799 break;
5800 }
5801
5802 /*
5803 * Search forward for the last char of the file name.
5804 * Also allow "://" when ':' is not in 'isfname'.
5805 */
5806 len = 0;
5807 while (vim_isfilec(ptr[len])
5808 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5809#ifdef FEAT_MBYTE
5810 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005811 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 else
5813#endif
5814 ++len;
5815
5816 /*
5817 * If there is trailing punctuation, remove it.
5818 * But don't remove "..", could be a directory name.
5819 */
5820 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5821 && ptr[len - 2] != '.')
5822 --len;
5823
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005824 if (file_lnum != NULL)
5825 {
5826 char_u *p;
5827
5828 /* Get the number after the file name and a separator character */
5829 p = ptr + len;
5830 p = skipwhite(p);
5831 if (*p != NUL)
5832 {
5833 if (!isdigit(*p))
5834 ++p; /* skip the separator */
5835 p = skipwhite(p);
5836 if (isdigit(*p))
5837 *file_lnum = (int)getdigits(&p);
5838 }
5839 }
5840
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5842}
5843
5844# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5845static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5846
5847 static char_u *
5848eval_includeexpr(ptr, len)
5849 char_u *ptr;
5850 int len;
5851{
5852 char_u *res;
5853
5854 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005855 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005856 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 set_vim_var_string(VV_FNAME, NULL, 0);
5858 return res;
5859}
5860#endif
5861
5862/*
5863 * Return the name of the file ptr[len] in 'path'.
5864 * Otherwise like file_name_at_cursor().
5865 */
5866 char_u *
5867find_file_name_in_path(ptr, len, options, count, rel_fname)
5868 char_u *ptr;
5869 int len;
5870 int options;
5871 long count;
5872 char_u *rel_fname; /* file we are searching relative to */
5873{
5874 char_u *file_name;
5875 int c;
5876# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5877 char_u *tofree = NULL;
5878
5879 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5880 {
5881 tofree = eval_includeexpr(ptr, len);
5882 if (tofree != NULL)
5883 {
5884 ptr = tofree;
5885 len = (int)STRLEN(ptr);
5886 }
5887 }
5888# endif
5889
5890 if (options & FNAME_EXP)
5891 {
5892 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5893 TRUE, rel_fname);
5894
5895# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5896 /*
5897 * If the file could not be found in a normal way, try applying
5898 * 'includeexpr' (unless done already).
5899 */
5900 if (file_name == NULL
5901 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5902 {
5903 tofree = eval_includeexpr(ptr, len);
5904 if (tofree != NULL)
5905 {
5906 ptr = tofree;
5907 len = (int)STRLEN(ptr);
5908 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5909 TRUE, rel_fname);
5910 }
5911 }
5912# endif
5913 if (file_name == NULL && (options & FNAME_MESS))
5914 {
5915 c = ptr[len];
5916 ptr[len] = NUL;
5917 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5918 ptr[len] = c;
5919 }
5920
5921 /* Repeat finding the file "count" times. This matters when it
5922 * appears several times in the path. */
5923 while (file_name != NULL && --count > 0)
5924 {
5925 vim_free(file_name);
5926 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5927 }
5928 }
5929 else
5930 file_name = vim_strnsave(ptr, len);
5931
5932# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5933 vim_free(tofree);
5934# endif
5935
5936 return file_name;
5937}
5938#endif /* FEAT_SEARCHPATH */
5939
5940/*
5941 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5942 * Also check for ":\\", which MS Internet Explorer accepts, return
5943 * URL_BACKSLASH.
5944 */
5945 static int
5946path_is_url(p)
5947 char_u *p;
5948{
5949 if (STRNCMP(p, "://", (size_t)3) == 0)
5950 return URL_SLASH;
5951 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5952 return URL_BACKSLASH;
5953 return 0;
5954}
5955
5956/*
5957 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5958 * Return URL_BACKSLASH for "name:\\".
5959 * Return zero otherwise.
5960 */
5961 int
5962path_with_url(fname)
5963 char_u *fname;
5964{
5965 char_u *p;
5966
5967 for (p = fname; isalpha(*p); ++p)
5968 ;
5969 return path_is_url(p);
5970}
5971
5972/*
5973 * Return TRUE if "name" is a full (absolute) path name or URL.
5974 */
5975 int
5976vim_isAbsName(name)
5977 char_u *name;
5978{
5979 return (path_with_url(name) != 0 || mch_isFullName(name));
5980}
5981
5982/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005983 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 *
5985 * return FAIL for failure, OK otherwise
5986 */
5987 int
5988vim_FullName(fname, buf, len, force)
5989 char_u *fname, *buf;
5990 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005991 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992{
5993 int retval = OK;
5994 int url;
5995
5996 *buf = NUL;
5997 if (fname == NULL)
5998 return FAIL;
5999
6000 url = path_with_url(fname);
6001 if (!url)
6002 retval = mch_FullName(fname, buf, len, force);
6003 if (url || retval == FAIL)
6004 {
6005 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006006 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 }
6008#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6009 slash_adjust(buf);
6010#endif
6011 return retval;
6012}
6013
6014/*
6015 * Return the minimal number of rows that is needed on the screen to display
6016 * the current number of windows.
6017 */
6018 int
6019min_rows()
6020{
6021 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006022#ifdef FEAT_WINDOWS
6023 tabpage_T *tp;
6024 int n;
6025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026
6027 if (firstwin == NULL) /* not initialized yet */
6028 return MIN_LINES;
6029
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006031 total = 0;
6032 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6033 {
6034 n = frame_minheight(tp->tp_topframe, NULL);
6035 if (total < n)
6036 total = n;
6037 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006038 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006040 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006042 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 return total;
6044}
6045
6046/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006047 * Return TRUE if there is only one window (in the current tab page), not
6048 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 */
6050 int
6051only_one_window()
6052{
6053#ifdef FEAT_WINDOWS
6054 int count = 0;
6055 win_T *wp;
6056
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006057 /* If there is another tab page there always is another window. */
6058 if (first_tabpage->tp_next != NULL)
6059 return FALSE;
6060
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00006062 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063# ifdef FEAT_QUICKFIX
6064 || wp->w_p_pvw
6065# endif
6066 ) || wp == curwin)
6067 ++count;
6068 return (count <= 1);
6069#else
6070 return TRUE;
6071#endif
6072}
6073
6074#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6075/*
6076 * Correct the cursor line number in other windows. Used after changing the
6077 * current buffer, and before applying autocommands.
6078 * When "do_curwin" is TRUE, also check current window.
6079 */
6080 void
6081check_lnums(do_curwin)
6082 int do_curwin;
6083{
6084 win_T *wp;
6085
6086#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006087 tabpage_T *tp;
6088
6089 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6091#else
6092 wp = curwin;
6093 if (do_curwin)
6094#endif
6095 {
6096 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6097 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6098 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6099 wp->w_topline = curbuf->b_ml.ml_line_count;
6100 }
6101}
6102#endif
6103
6104#if defined(FEAT_WINDOWS) || defined(PROTO)
6105
6106/*
6107 * A snapshot of the window sizes, to restore them after closing the help
6108 * window.
6109 * Only these fields are used:
6110 * fr_layout
6111 * fr_width
6112 * fr_height
6113 * fr_next
6114 * fr_child
6115 * fr_win (only valid for the old curwin, NULL otherwise)
6116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117
6118/*
6119 * Create a snapshot of the current frame sizes.
6120 */
6121 static void
6122make_snapshot()
6123{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006124 clear_snapshot(curtab);
6125 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126}
6127
6128 static void
6129make_snapshot_rec(fr, frp)
6130 frame_T *fr;
6131 frame_T **frp;
6132{
6133 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6134 if (*frp == NULL)
6135 return;
6136 (*frp)->fr_layout = fr->fr_layout;
6137# ifdef FEAT_VERTSPLIT
6138 (*frp)->fr_width = fr->fr_width;
6139# endif
6140 (*frp)->fr_height = fr->fr_height;
6141 if (fr->fr_next != NULL)
6142 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6143 if (fr->fr_child != NULL)
6144 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6145 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6146 (*frp)->fr_win = curwin;
6147}
6148
6149/*
6150 * Remove any existing snapshot.
6151 */
6152 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006153clear_snapshot(tp)
6154 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006156 clear_snapshot_rec(tp->tp_snapshot);
6157 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158}
6159
6160 static void
6161clear_snapshot_rec(fr)
6162 frame_T *fr;
6163{
6164 if (fr != NULL)
6165 {
6166 clear_snapshot_rec(fr->fr_next);
6167 clear_snapshot_rec(fr->fr_child);
6168 vim_free(fr);
6169 }
6170}
6171
6172/*
6173 * Restore a previously created snapshot, if there is any.
6174 * This is only done if the screen size didn't change and the window layout is
6175 * still the same.
6176 */
6177 static void
6178restore_snapshot(close_curwin)
6179 int close_curwin; /* closing current window */
6180{
6181 win_T *wp;
6182
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006183 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006185 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006187 && curtab->tp_snapshot->fr_height == topframe->fr_height
6188 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006190 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 win_comp_pos();
6192 if (wp != NULL && close_curwin)
6193 win_goto(wp);
6194 redraw_all_later(CLEAR);
6195 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006196 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197}
6198
6199/*
6200 * Check if frames "sn" and "fr" have the same layout, same following frames
6201 * and same children.
6202 */
6203 static int
6204check_snapshot_rec(sn, fr)
6205 frame_T *sn;
6206 frame_T *fr;
6207{
6208 if (sn->fr_layout != fr->fr_layout
6209 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6210 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6211 || (sn->fr_next != NULL
6212 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6213 || (sn->fr_child != NULL
6214 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6215 return FAIL;
6216 return OK;
6217}
6218
6219/*
6220 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6221 * following frames and children.
6222 * Returns a pointer to the old current window, or NULL.
6223 */
6224 static win_T *
6225restore_snapshot_rec(sn, fr)
6226 frame_T *sn;
6227 frame_T *fr;
6228{
6229 win_T *wp = NULL;
6230 win_T *wp2;
6231
6232 fr->fr_height = sn->fr_height;
6233# ifdef FEAT_VERTSPLIT
6234 fr->fr_width = sn->fr_width;
6235# endif
6236 if (fr->fr_layout == FR_LEAF)
6237 {
6238 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6239# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006240 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241# endif
6242 wp = sn->fr_win;
6243 }
6244 if (sn->fr_next != NULL)
6245 {
6246 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6247 if (wp2 != NULL)
6248 wp = wp2;
6249 }
6250 if (sn->fr_child != NULL)
6251 {
6252 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6253 if (wp2 != NULL)
6254 wp = wp2;
6255 }
6256 return wp;
6257}
6258
6259#endif
6260
6261#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6262/*
6263 * Return TRUE if there is any vertically split window.
6264 */
6265 int
6266win_hasvertsplit()
6267{
6268 frame_T *fr;
6269
6270 if (topframe->fr_layout == FR_ROW)
6271 return TRUE;
6272
6273 if (topframe->fr_layout == FR_COL)
6274 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6275 if (fr->fr_layout == FR_ROW)
6276 return TRUE;
6277
6278 return FALSE;
6279}
6280#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006281
6282#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6283/*
6284 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006285 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006286 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6287 * If no particular ID is desired, -1 must be specified for 'id'.
6288 * Return ID of added match, -1 on failure.
6289 */
6290 int
6291match_add(wp, grp, pat, prio, id)
6292 win_T *wp;
6293 char_u *grp;
6294 char_u *pat;
6295 int prio;
6296 int id;
6297{
6298 matchitem_T *cur;
6299 matchitem_T *prev;
6300 matchitem_T *m;
6301 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006302 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006303
6304 if (*grp == NUL || *pat == NUL)
6305 return -1;
6306 if (id < -1 || id == 0)
6307 {
6308 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6309 return -1;
6310 }
6311 if (id != -1)
6312 {
6313 cur = wp->w_match_head;
6314 while (cur != NULL)
6315 {
6316 if (cur->id == id)
6317 {
6318 EMSGN("E801: ID already taken: %ld", id);
6319 return -1;
6320 }
6321 cur = cur->next;
6322 }
6323 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006324 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006325 {
6326 EMSG2(_(e_nogroup), grp);
6327 return -1;
6328 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006329 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006330 {
6331 EMSG2(_(e_invarg2), pat);
6332 return -1;
6333 }
6334
6335 /* Find available match ID. */
6336 while (id == -1)
6337 {
6338 cur = wp->w_match_head;
6339 while (cur != NULL && cur->id != wp->w_next_match_id)
6340 cur = cur->next;
6341 if (cur == NULL)
6342 id = wp->w_next_match_id;
6343 wp->w_next_match_id++;
6344 }
6345
6346 /* Build new match. */
6347 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6348 m->id = id;
6349 m->priority = prio;
6350 m->pattern = vim_strsave(pat);
6351 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006352 m->match.regprog = regprog;
6353 m->match.rmm_ic = FALSE;
6354 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006355
6356 /* Insert new match. The match list is in ascending order with regard to
6357 * the match priorities. */
6358 cur = wp->w_match_head;
6359 prev = cur;
6360 while (cur != NULL && prio >= cur->priority)
6361 {
6362 prev = cur;
6363 cur = cur->next;
6364 }
6365 if (cur == prev)
6366 wp->w_match_head = m;
6367 else
6368 prev->next = m;
6369 m->next = cur;
6370
6371 redraw_later(SOME_VALID);
6372 return id;
6373}
6374
6375/*
6376 * Delete match with ID 'id' in the match list of window 'wp'.
6377 * Print error messages if 'perr' is TRUE.
6378 */
6379 int
6380match_delete(wp, id, perr)
6381 win_T *wp;
6382 int id;
6383 int perr;
6384{
6385 matchitem_T *cur = wp->w_match_head;
6386 matchitem_T *prev = cur;
6387
6388 if (id < 1)
6389 {
6390 if (perr == TRUE)
6391 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6392 id);
6393 return -1;
6394 }
6395 while (cur != NULL && cur->id != id)
6396 {
6397 prev = cur;
6398 cur = cur->next;
6399 }
6400 if (cur == NULL)
6401 {
6402 if (perr == TRUE)
6403 EMSGN("E803: ID not found: %ld", id);
6404 return -1;
6405 }
6406 if (cur == prev)
6407 wp->w_match_head = cur->next;
6408 else
6409 prev->next = cur->next;
6410 vim_free(cur->match.regprog);
6411 vim_free(cur->pattern);
6412 vim_free(cur);
6413 redraw_later(SOME_VALID);
6414 return 0;
6415}
6416
6417/*
6418 * Delete all matches in the match list of window 'wp'.
6419 */
6420 void
6421clear_matches(wp)
6422 win_T *wp;
6423{
6424 matchitem_T *m;
6425
6426 while (wp->w_match_head != NULL)
6427 {
6428 m = wp->w_match_head->next;
6429 vim_free(wp->w_match_head->match.regprog);
6430 vim_free(wp->w_match_head->pattern);
6431 vim_free(wp->w_match_head);
6432 wp->w_match_head = m;
6433 }
6434 redraw_later(SOME_VALID);
6435}
6436
6437/*
6438 * Get match from ID 'id' in window 'wp'.
6439 * Return NULL if match not found.
6440 */
6441 matchitem_T *
6442get_match(wp, id)
6443 win_T *wp;
6444 int id;
6445{
6446 matchitem_T *cur = wp->w_match_head;
6447
6448 while (cur != NULL && cur->id != id)
6449 cur = cur->next;
6450 return cur;
6451}
6452#endif