blob: 9f5e39efcdc5758ac86581a1753ac262121a360f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#include "vim.h"
11
Bram Moolenaar071d4272004-06-13 20:20:40 +000012static int path_is_url __ARGS((char_u *p));
13#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar884ae642009-02-22 01:37:59 +000014static void win_init __ARGS((win_T *newp, win_T *oldp, int flags));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000015static void win_init_some __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
17static void frame_setheight __ARGS((frame_T *curfrp, int height));
18#ifdef FEAT_VERTSPLIT
19static void frame_setwidth __ARGS((frame_T *curfrp, int width));
20#endif
21static void win_exchange __ARGS((long));
22static void win_rotate __ARGS((int, int));
23static void win_totop __ARGS((int size, int flags));
24static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000025static int last_window __ARGS((void));
Bram Moolenaarbef1c362012-05-25 12:39:00 +020026static int close_last_window_tabpage __ARGS((win_T *win, int free_buf, tabpage_T *prev_curtab));
Bram Moolenaarf740b292006-02-16 22:11:02 +000027static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
Bram Moolenaarf740b292006-02-16 22:11:02 +000028static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000029static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000030static win_T *frame2win __ARGS((frame_T *frp));
31static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
32static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
33static int frame_fixed_height __ARGS((frame_T *frp));
34#ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000035static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000036static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000037static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000038static void frame_add_vsep __ARGS((frame_T *frp));
39static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
40static void frame_fix_width __ARGS((win_T *wp));
41#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000042#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000043static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000044static void new_frame __ARGS((win_T *wp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000045#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000046static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000047static int leave_tabpage __ARGS((buf_T *new_curbuf));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000048static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000049static void frame_fix_height __ARGS((win_T *wp));
50static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
51static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
Bram Moolenaarf740b292006-02-16 22:11:02 +000052static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000053static void frame_append __ARGS((frame_T *after, frame_T *frp));
54static void frame_insert __ARGS((frame_T *before, frame_T *frp));
55static void frame_remove __ARGS((frame_T *frp));
56#ifdef FEAT_VERTSPLIT
57static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static void win_goto_ver __ARGS((int up, long count));
59static void win_goto_hor __ARGS((int left, long count));
60#endif
61static void frame_add_height __ARGS((frame_T *frp, int n));
62static void last_status_rec __ARGS((frame_T *fr, int statusline));
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000065static void clear_snapshot __ARGS((tabpage_T *tp, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static void clear_snapshot_rec __ARGS((frame_T *fr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000067static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
68static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
69
70#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000071
Bram Moolenaar746ebd32009-06-16 14:01:43 +000072static win_T *win_alloc __ARGS((win_T *after, int hidden));
Bram Moolenaar0215e8e2010-12-17 17:35:10 +010073static void set_fraction __ARGS((win_T *wp));
74static void win_new_height __ARGS((win_T *wp, int height));
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
76#define URL_SLASH 1 /* path_is_url() has found "://" */
77#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
78
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000079#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaar05159a02005-02-26 23:04:13 +000081#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000082# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000083#else
84# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000085#endif
86
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000088
89static char *m_onlyone = N_("Already only one window");
90
Bram Moolenaar071d4272004-06-13 20:20:40 +000091/*
92 * all CTRL-W window commands are handled here, called from normal_cmd().
93 */
94 void
95do_window(nchar, Prenum, xchar)
96 int nchar;
97 long Prenum;
98 int xchar; /* extra char from ":wincmd gx" or NUL */
99{
100 long Prenum1;
101 win_T *wp;
102#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
103 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000104 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#endif
106#ifdef FEAT_FIND_ID
107 int type = FIND_DEFINE;
108 int len;
109#endif
110 char_u cbuf[40];
111
112 if (Prenum == 0)
113 Prenum1 = 1;
114 else
115 Prenum1 = Prenum;
116
117#ifdef FEAT_CMDWIN
118# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
119#else
120# define CHECK_CMDWIN
121#endif
122
123 switch (nchar)
124 {
125/* split current window in two parts, horizontally */
126 case 'S':
127 case Ctrl_S:
128 case 's':
129 CHECK_CMDWIN
130#ifdef FEAT_VISUAL
131 reset_VIsual_and_resel(); /* stop Visual mode */
132#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000133#ifdef FEAT_QUICKFIX
134 /* When splitting the quickfix window open a new buffer in it,
135 * don't replicate the quickfix buffer. */
136 if (bt_quickfix(curbuf))
137 goto newwindow;
138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#ifdef FEAT_GUI
140 need_mouse_correct = TRUE;
141#endif
142 win_split((int)Prenum, 0);
143 break;
144
145#ifdef FEAT_VERTSPLIT
146/* split current window in two parts, vertically */
147 case Ctrl_V:
148 case 'v':
149 CHECK_CMDWIN
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000150# ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000152# endif
153# ifdef FEAT_QUICKFIX
154 /* When splitting the quickfix window open a new buffer in it,
155 * don't replicate the quickfix buffer. */
156 if (bt_quickfix(curbuf))
157 goto newwindow;
158# endif
159# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 win_split((int)Prenum, WSP_VERT);
163 break;
164#endif
165
166/* split current window and edit alternate file */
167 case Ctrl_HAT:
168 case '^':
169 CHECK_CMDWIN
170#ifdef FEAT_VISUAL
171 reset_VIsual_and_resel(); /* stop Visual mode */
172#endif
173 STRCPY(cbuf, "split #");
174 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000175 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
176 "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 do_cmdline_cmd(cbuf);
178 break;
179
180/* open new window */
181 case Ctrl_N:
182 case 'n':
183 CHECK_CMDWIN
184#ifdef FEAT_VISUAL
185 reset_VIsual_and_resel(); /* stop Visual mode */
186#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000187#ifdef FEAT_QUICKFIX
188newwindow:
189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000191 /* window height */
192 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 else
194 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000195#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
196 if (nchar == 'v' || nchar == Ctrl_V)
197 STRCAT(cbuf, "v");
198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 STRCAT(cbuf, "new");
200 do_cmdline_cmd(cbuf);
201 break;
202
203/* quit current window */
204 case Ctrl_Q:
205 case 'q':
206#ifdef FEAT_VISUAL
207 reset_VIsual_and_resel(); /* stop Visual mode */
208#endif
209 do_cmdline_cmd((char_u *)"quit");
210 break;
211
212/* close current window */
213 case Ctrl_C:
214 case 'c':
215#ifdef FEAT_VISUAL
216 reset_VIsual_and_resel(); /* stop Visual mode */
217#endif
218 do_cmdline_cmd((char_u *)"close");
219 break;
220
221#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
222/* close preview window */
223 case Ctrl_Z:
224 case 'z':
225 CHECK_CMDWIN
226#ifdef FEAT_VISUAL
227 reset_VIsual_and_resel(); /* stop Visual mode */
228#endif
229 do_cmdline_cmd((char_u *)"pclose");
230 break;
231
232/* cursor to preview window */
233 case 'P':
234 for (wp = firstwin; wp != NULL; wp = wp->w_next)
235 if (wp->w_p_pvw)
236 break;
237 if (wp == NULL)
238 EMSG(_("E441: There is no preview window"));
239 else
240 win_goto(wp);
241 break;
242#endif
243
244/* close all but current window */
245 case Ctrl_O:
246 case 'o':
247 CHECK_CMDWIN
248#ifdef FEAT_VISUAL
249 reset_VIsual_and_resel(); /* stop Visual mode */
250#endif
251 do_cmdline_cmd((char_u *)"only");
252 break;
253
254/* cursor to next window with wrap around */
255 case Ctrl_W:
256 case 'w':
257/* cursor to previous window with wrap around */
258 case 'W':
259 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000260 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 beep_flush();
262 else
263 {
264 if (Prenum) /* go to specified window */
265 {
266 for (wp = firstwin; --Prenum > 0; )
267 {
268 if (wp->w_next == NULL)
269 break;
270 else
271 wp = wp->w_next;
272 }
273 }
274 else
275 {
276 if (nchar == 'W') /* go to previous window */
277 {
278 wp = curwin->w_prev;
279 if (wp == NULL)
280 wp = lastwin; /* wrap around */
281 }
282 else /* go to next window */
283 {
284 wp = curwin->w_next;
285 if (wp == NULL)
286 wp = firstwin; /* wrap around */
287 }
288 }
289 win_goto(wp);
290 }
291 break;
292
293/* cursor to window below */
294 case 'j':
295 case K_DOWN:
296 case Ctrl_J:
297 CHECK_CMDWIN
298#ifdef FEAT_VERTSPLIT
299 win_goto_ver(FALSE, Prenum1);
300#else
301 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
302 wp = wp->w_next)
303 ;
304 win_goto(wp);
305#endif
306 break;
307
308/* cursor to window above */
309 case 'k':
310 case K_UP:
311 case Ctrl_K:
312 CHECK_CMDWIN
313#ifdef FEAT_VERTSPLIT
314 win_goto_ver(TRUE, Prenum1);
315#else
316 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
317 wp = wp->w_prev)
318 ;
319 win_goto(wp);
320#endif
321 break;
322
323#ifdef FEAT_VERTSPLIT
324/* cursor to left window */
325 case 'h':
326 case K_LEFT:
327 case Ctrl_H:
328 case K_BS:
329 CHECK_CMDWIN
330 win_goto_hor(TRUE, Prenum1);
331 break;
332
333/* cursor to right window */
334 case 'l':
335 case K_RIGHT:
336 case Ctrl_L:
337 CHECK_CMDWIN
338 win_goto_hor(FALSE, Prenum1);
339 break;
340#endif
341
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000342/* move window to new tab page */
343 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000344 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000345 MSG(_(m_onlyone));
346 else
347 {
348 tabpage_T *oldtab = curtab;
349 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000350
351 /* First create a new tab with the window, then go back to
352 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000353 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000354 if (win_new_tabpage((int)Prenum) == OK
355 && valid_tabpage(oldtab))
356 {
357 newtab = curtab;
358 goto_tabpage_tp(oldtab);
359 if (curwin == wp)
360 win_close(curwin, FALSE);
361 if (valid_tabpage(newtab))
362 goto_tabpage_tp(newtab);
363 }
364 }
365 break;
366
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367/* cursor to top-left window */
368 case 't':
369 case Ctrl_T:
370 win_goto(firstwin);
371 break;
372
373/* cursor to bottom-right window */
374 case 'b':
375 case Ctrl_B:
376 win_goto(lastwin);
377 break;
378
379/* cursor to last accessed (previous) window */
380 case 'p':
381 case Ctrl_P:
382 if (prevwin == NULL)
383 beep_flush();
384 else
385 win_goto(prevwin);
386 break;
387
388/* exchange current and next window */
389 case 'x':
390 case Ctrl_X:
391 CHECK_CMDWIN
392 win_exchange(Prenum);
393 break;
394
395/* rotate windows downwards */
396 case Ctrl_R:
397 case 'r':
398 CHECK_CMDWIN
399#ifdef FEAT_VISUAL
400 reset_VIsual_and_resel(); /* stop Visual mode */
401#endif
402 win_rotate(FALSE, (int)Prenum1); /* downwards */
403 break;
404
405/* rotate windows upwards */
406 case 'R':
407 CHECK_CMDWIN
408#ifdef FEAT_VISUAL
409 reset_VIsual_and_resel(); /* stop Visual mode */
410#endif
411 win_rotate(TRUE, (int)Prenum1); /* upwards */
412 break;
413
414/* move window to the very top/bottom/left/right */
415 case 'K':
416 case 'J':
417#ifdef FEAT_VERTSPLIT
418 case 'H':
419 case 'L':
420#endif
421 CHECK_CMDWIN
422 win_totop((int)Prenum,
423 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
424 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
425 break;
426
427/* make all windows the same height */
428 case '=':
429#ifdef FEAT_GUI
430 need_mouse_correct = TRUE;
431#endif
432 win_equal(NULL, FALSE, 'b');
433 break;
434
435/* increase current window height */
436 case '+':
437#ifdef FEAT_GUI
438 need_mouse_correct = TRUE;
439#endif
440 win_setheight(curwin->w_height + (int)Prenum1);
441 break;
442
443/* decrease current window height */
444 case '-':
445#ifdef FEAT_GUI
446 need_mouse_correct = TRUE;
447#endif
448 win_setheight(curwin->w_height - (int)Prenum1);
449 break;
450
451/* set current window height */
452 case Ctrl__:
453 case '_':
454#ifdef FEAT_GUI
455 need_mouse_correct = TRUE;
456#endif
457 win_setheight(Prenum ? (int)Prenum : 9999);
458 break;
459
460#ifdef FEAT_VERTSPLIT
461/* increase current window width */
462 case '>':
463#ifdef FEAT_GUI
464 need_mouse_correct = TRUE;
465#endif
466 win_setwidth(curwin->w_width + (int)Prenum1);
467 break;
468
469/* decrease current window width */
470 case '<':
471#ifdef FEAT_GUI
472 need_mouse_correct = TRUE;
473#endif
474 win_setwidth(curwin->w_width - (int)Prenum1);
475 break;
476
477/* set current window width */
478 case '|':
479#ifdef FEAT_GUI
480 need_mouse_correct = TRUE;
481#endif
482 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
483 break;
484#endif
485
486/* jump to tag and split window if tag exists (in preview window) */
487#if defined(FEAT_QUICKFIX)
488 case '}':
489 CHECK_CMDWIN
490 if (Prenum)
491 g_do_tagpreview = Prenum;
492 else
493 g_do_tagpreview = p_pvh;
494 /*FALLTHROUGH*/
495#endif
496 case ']':
497 case Ctrl_RSB:
498 CHECK_CMDWIN
499#ifdef FEAT_VISUAL
500 reset_VIsual_and_resel(); /* stop Visual mode */
501#endif
502 if (Prenum)
503 postponed_split = Prenum;
504 else
505 postponed_split = -1;
506
507 /* Execute the command right here, required when
508 * "wincmd ]" was used in a function. */
509 do_nv_ident(Ctrl_RSB, NUL);
510 break;
511
512#ifdef FEAT_SEARCHPATH
513/* edit file name under cursor in a new window */
514 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000515 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000517wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000519
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000520 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 if (ptr != NULL)
522 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000523# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000525# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 setpcmark();
527 if (win_split(0, 0) == OK)
528 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200529 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000530 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
531 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000532 if (nchar == 'F' && lnum >= 0)
533 {
534 curwin->w_cursor.lnum = lnum;
535 check_cursor_lnum();
536 beginline(BL_SOL | BL_FIX);
537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 }
539 vim_free(ptr);
540 }
541 break;
542#endif
543
544#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000545/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 * new window -- webb
547 */
548 case 'i': /* Go to any match */
549 case Ctrl_I:
550 type = FIND_ANY;
551 /* FALLTHROUGH */
552 case 'd': /* Go to definition, using 'define' */
553 case Ctrl_D:
554 CHECK_CMDWIN
555 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
556 break;
557 find_pattern_in_path(ptr, 0, len, TRUE,
558 Prenum == 0 ? TRUE : FALSE, type,
559 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
560 curwin->w_set_curswant = TRUE;
561 break;
562#endif
563
Bram Moolenaar05159a02005-02-26 23:04:13 +0000564 case K_KENTER:
565 case CAR:
566#if defined(FEAT_QUICKFIX)
567 /*
568 * In a quickfix window a <CR> jumps to the error under the
569 * cursor in a new window.
570 */
571 if (bt_quickfix(curbuf))
572 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000573 sprintf((char *)cbuf, "split +%ld%s",
574 (long)curwin->w_cursor.lnum,
575 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000576 do_cmdline_cmd(cbuf);
577 }
578#endif
579 break;
580
581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582/* CTRL-W g extended commands */
583 case 'g':
584 case Ctrl_G:
585 CHECK_CMDWIN
586#ifdef USE_ON_FLY_SCROLL
587 dont_scroll = TRUE; /* disallow scrolling here */
588#endif
589 ++no_mapping;
590 ++allow_keys; /* no mapping for xchar, but allow key codes */
591 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000592 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 --no_mapping;
595 --allow_keys;
596#ifdef FEAT_CMDL_INFO
597 (void)add_to_showcmd(xchar);
598#endif
599 switch (xchar)
600 {
601#if defined(FEAT_QUICKFIX)
602 case '}':
603 xchar = Ctrl_RSB;
604 if (Prenum)
605 g_do_tagpreview = Prenum;
606 else
607 g_do_tagpreview = p_pvh;
608 /*FALLTHROUGH*/
609#endif
610 case ']':
611 case Ctrl_RSB:
612#ifdef FEAT_VISUAL
613 reset_VIsual_and_resel(); /* stop Visual mode */
614#endif
615 if (Prenum)
616 postponed_split = Prenum;
617 else
618 postponed_split = -1;
619
620 /* Execute the command right here, required when
621 * "wincmd g}" was used in a function. */
622 do_nv_ident('g', xchar);
623 break;
624
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000625#ifdef FEAT_SEARCHPATH
626 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000627 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100628 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000629 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000630 goto wingotofile;
631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 default:
633 beep_flush();
634 break;
635 }
636 break;
637
638 default: beep_flush();
639 break;
640 }
641}
642
643/*
644 * split the current window, implements CTRL-W s and :split
645 *
646 * "size" is the height or width for the new window, 0 to use half of current
647 * height or width.
648 *
649 * "flags":
650 * WSP_ROOM: require enough room for new window
651 * WSP_VERT: vertical split.
652 * WSP_TOP: open window at the top-left of the shell (help window).
653 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
654 * WSP_HELP: creating the help window, keep layout snapshot
655 *
656 * return FAIL for failure, OK otherwise
657 */
658 int
659win_split(size, flags)
660 int size;
661 int flags;
662{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000663 /* When the ":tab" modifier was used open a new tab page instead. */
664 if (may_open_tabpage() == OK)
665 return OK;
666
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 /* Add flags from ":vertical", ":topleft" and ":botright". */
668 flags |= cmdmod.split;
669 if ((flags & WSP_TOP) && (flags & WSP_BOT))
670 {
671 EMSG(_("E442: Can't split topleft and botright at the same time"));
672 return FAIL;
673 }
674
675 /* When creating the help window make a snapshot of the window layout.
676 * Otherwise clear the snapshot, it's now invalid. */
677 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000678 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000680 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
682 return win_split_ins(size, flags, NULL, 0);
683}
684
685/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100686 * When "new_wp" is NULL: split the current window in two.
687 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 * top/left/right/bottom.
689 * return FAIL for failure, OK otherwise
690 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000691 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100692win_split_ins(size, flags, new_wp, dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 int size;
694 int flags;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100695 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 int dir;
697{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100698 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 win_T *oldwin;
700 int new_size = size;
701 int i;
702 int need_status = 0;
703 int do_equal = FALSE;
704 int needed;
705 int available;
706 int oldwin_height = 0;
707 int layout;
708 frame_T *frp, *curfrp;
709 int before;
710
711 if (flags & WSP_TOP)
712 oldwin = firstwin;
713 else if (flags & WSP_BOT)
714 oldwin = lastwin;
715 else
716 oldwin = curwin;
717
718 /* add a status line when p_ls == 1 and splitting the first window */
719 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
720 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100721 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {
723 EMSG(_(e_noroom));
724 return FAIL;
725 }
726 need_status = STATUS_HEIGHT;
727 }
728
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000729#ifdef FEAT_GUI
730 /* May be needed for the scrollbars that are going to change. */
731 if (gui.in_use)
732 out_flush();
733#endif
734
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735#ifdef FEAT_VERTSPLIT
736 if (flags & WSP_VERT)
737 {
738 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
740 /*
741 * Check if we are able to split the current window and compute its
742 * width.
743 */
744 needed = p_wmw + 1;
745 if (flags & WSP_ROOM)
746 needed += p_wiw - p_wmw;
747 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
748 {
749 available = topframe->fr_width;
750 needed += frame_minwidth(topframe, NULL);
751 }
752 else
753 available = oldwin->w_width;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100754 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
756 EMSG(_(e_noroom));
757 return FAIL;
758 }
759 if (new_size == 0)
760 new_size = oldwin->w_width / 2;
761 if (new_size > oldwin->w_width - p_wmw - 1)
762 new_size = oldwin->w_width - p_wmw - 1;
763 if (new_size < p_wmw)
764 new_size = p_wmw;
765
766 /* if it doesn't fit in the current window, need win_equal() */
767 if (oldwin->w_width - new_size - 1 < p_wmw)
768 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000769
770 /* We don't like to take lines for the new window from a
771 * 'winfixwidth' window. Take them from a window to the left or right
772 * instead, if possible. */
773 if (oldwin->w_p_wfw)
774 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000775
776 /* Only make all windows the same width if one of them (except oldwin)
777 * is wider than one of the split windows. */
778 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
779 && oldwin->w_frame->fr_parent != NULL)
780 {
781 frp = oldwin->w_frame->fr_parent->fr_child;
782 while (frp != NULL)
783 {
784 if (frp->fr_win != oldwin && frp->fr_win != NULL
785 && (frp->fr_win->w_width > new_size
786 || frp->fr_win->w_width > oldwin->w_width
787 - new_size - STATUS_HEIGHT))
788 {
789 do_equal = TRUE;
790 break;
791 }
792 frp = frp->fr_next;
793 }
794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 }
796 else
797#endif
798 {
799 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801 /*
802 * Check if we are able to split the current window and compute its
803 * height.
804 */
805 needed = p_wmh + STATUS_HEIGHT + need_status;
806 if (flags & WSP_ROOM)
807 needed += p_wh - p_wmh;
808 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
809 {
810 available = topframe->fr_height;
811 needed += frame_minheight(topframe, NULL);
812 }
813 else
814 {
815 available = oldwin->w_height;
816 needed += p_wmh;
817 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100818 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 {
820 EMSG(_(e_noroom));
821 return FAIL;
822 }
823 oldwin_height = oldwin->w_height;
824 if (need_status)
825 {
826 oldwin->w_status_height = STATUS_HEIGHT;
827 oldwin_height -= STATUS_HEIGHT;
828 }
829 if (new_size == 0)
830 new_size = oldwin_height / 2;
831
832 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
833 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
834 if (new_size < p_wmh)
835 new_size = p_wmh;
836
837 /* if it doesn't fit in the current window, need win_equal() */
838 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
839 do_equal = TRUE;
840
841 /* We don't like to take lines for the new window from a
842 * 'winfixheight' window. Take them from a window above or below
843 * instead, if possible. */
844 if (oldwin->w_p_wfh)
845 {
846 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
847 oldwin);
848 oldwin_height = oldwin->w_height;
849 if (need_status)
850 oldwin_height -= STATUS_HEIGHT;
851 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000852
853 /* Only make all windows the same height if one of them (except oldwin)
854 * is higher than one of the split windows. */
855 if (!do_equal && p_ea && size == 0
856#ifdef FEAT_VERTSPLIT
857 && *p_ead != 'h'
858#endif
859 && oldwin->w_frame->fr_parent != NULL)
860 {
861 frp = oldwin->w_frame->fr_parent->fr_child;
862 while (frp != NULL)
863 {
864 if (frp->fr_win != oldwin && frp->fr_win != NULL
865 && (frp->fr_win->w_height > new_size
866 || frp->fr_win->w_height > oldwin_height - new_size
867 - STATUS_HEIGHT))
868 {
869 do_equal = TRUE;
870 break;
871 }
872 frp = frp->fr_next;
873 }
874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 }
876
877 /*
878 * allocate new window structure and link it in the window list
879 */
880 if ((flags & WSP_TOP) == 0
881 && ((flags & WSP_BOT)
882 || (flags & WSP_BELOW)
883 || (!(flags & WSP_ABOVE)
884 && (
885#ifdef FEAT_VERTSPLIT
886 (flags & WSP_VERT) ? p_spr :
887#endif
888 p_sb))))
889 {
890 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100891 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000892 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 else
894 win_append(oldwin, wp);
895 }
896 else
897 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100898 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000899 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 else
901 win_append(oldwin->w_prev, wp);
902 }
903
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100904 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 {
906 if (wp == NULL)
907 return FAIL;
908
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000909 new_frame(wp);
910 if (wp->w_frame == NULL)
911 {
912 win_free(wp, NULL);
913 return FAIL;
914 }
915
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000916 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000917 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 }
919
920 /*
921 * Reorganise the tree of frames to insert the new window.
922 */
923 if (flags & (WSP_TOP | WSP_BOT))
924 {
925#ifdef FEAT_VERTSPLIT
926 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
927 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
928#else
929 if (topframe->fr_layout == FR_COL)
930#endif
931 {
932 curfrp = topframe->fr_child;
933 if (flags & WSP_BOT)
934 while (curfrp->fr_next != NULL)
935 curfrp = curfrp->fr_next;
936 }
937 else
938 curfrp = topframe;
939 before = (flags & WSP_TOP);
940 }
941 else
942 {
943 curfrp = oldwin->w_frame;
944 if (flags & WSP_BELOW)
945 before = FALSE;
946 else if (flags & WSP_ABOVE)
947 before = TRUE;
948 else
949#ifdef FEAT_VERTSPLIT
950 if (flags & WSP_VERT)
951 before = !p_spr;
952 else
953#endif
954 before = !p_sb;
955 }
956 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
957 {
958 /* Need to create a new frame in the tree to make a branch. */
959 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
960 *frp = *curfrp;
961 curfrp->fr_layout = layout;
962 frp->fr_parent = curfrp;
963 frp->fr_next = NULL;
964 frp->fr_prev = NULL;
965 curfrp->fr_child = frp;
966 curfrp->fr_win = NULL;
967 curfrp = frp;
968 if (frp->fr_win != NULL)
969 oldwin->w_frame = frp;
970 else
971 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
972 frp->fr_parent = curfrp;
973 }
974
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100975 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000976 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100978 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 frp->fr_parent = curfrp->fr_parent;
980
981 /* Insert the new frame at the right place in the frame list. */
982 if (before)
983 frame_insert(curfrp, frp);
984 else
985 frame_append(curfrp, frp);
986
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100987 /* Set w_fraction now so that the cursor keeps the same relative
988 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +0100989 if (oldwin->w_height > 0)
990 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100991 wp->w_fraction = oldwin->w_fraction;
992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#ifdef FEAT_VERTSPLIT
994 if (flags & WSP_VERT)
995 {
996 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (need_status)
999 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001000 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 oldwin->w_status_height = need_status;
1002 }
1003 if (flags & (WSP_TOP | WSP_BOT))
1004 {
1005 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001006 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001007 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 wp->w_status_height = (p_ls > 0);
1009 }
1010 else
1011 {
1012 /* height and row of new window is same as current window */
1013 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001014 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 wp->w_status_height = oldwin->w_status_height;
1016 }
1017 frp->fr_height = curfrp->fr_height;
1018
1019 /* "new_size" of the current window goes to the new window, use
1020 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001021 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (before)
1023 wp->w_vsep_width = 1;
1024 else
1025 {
1026 wp->w_vsep_width = oldwin->w_vsep_width;
1027 oldwin->w_vsep_width = 1;
1028 }
1029 if (flags & (WSP_TOP | WSP_BOT))
1030 {
1031 if (flags & WSP_BOT)
1032 frame_add_vsep(curfrp);
1033 /* Set width of neighbor frame */
1034 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001035 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1036 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 }
1038 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001039 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (before) /* new window left of current one */
1041 {
1042 wp->w_wincol = oldwin->w_wincol;
1043 oldwin->w_wincol += new_size + 1;
1044 }
1045 else /* new window right of current one */
1046 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1047 frame_fix_width(oldwin);
1048 frame_fix_width(wp);
1049 }
1050 else
1051#endif
1052 {
1053 /* width and column of new window is same as current window */
1054#ifdef FEAT_VERTSPLIT
1055 if (flags & (WSP_TOP | WSP_BOT))
1056 {
1057 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001058 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 wp->w_vsep_width = 0;
1060 }
1061 else
1062 {
1063 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001064 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 wp->w_vsep_width = oldwin->w_vsep_width;
1066 }
1067 frp->fr_width = curfrp->fr_width;
1068#endif
1069
1070 /* "new_size" of the current window goes to the new window, use
1071 * one row for the status line */
1072 win_new_height(wp, new_size);
1073 if (flags & (WSP_TOP | WSP_BOT))
1074 frame_new_height(curfrp, curfrp->fr_height
1075 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1076 else
1077 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1078 if (before) /* new window above current one */
1079 {
1080 wp->w_winrow = oldwin->w_winrow;
1081 wp->w_status_height = STATUS_HEIGHT;
1082 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1083 }
1084 else /* new window below current one */
1085 {
1086 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1087 wp->w_status_height = oldwin->w_status_height;
1088 oldwin->w_status_height = STATUS_HEIGHT;
1089 }
1090#ifdef FEAT_VERTSPLIT
1091 if (flags & WSP_BOT)
1092 frame_add_statusline(curfrp);
1093#endif
1094 frame_fix_height(wp);
1095 frame_fix_height(oldwin);
1096 }
1097
1098 if (flags & (WSP_TOP | WSP_BOT))
1099 (void)win_comp_pos();
1100
1101 /*
1102 * Both windows need redrawing
1103 */
1104 redraw_win_later(wp, NOT_VALID);
1105 wp->w_redr_status = TRUE;
1106 redraw_win_later(oldwin, NOT_VALID);
1107 oldwin->w_redr_status = TRUE;
1108
1109 if (need_status)
1110 {
1111 msg_row = Rows - 1;
1112 msg_col = sc_col;
1113 msg_clr_eos_force(); /* Old command/ruler may still be there */
1114 comp_col();
1115 msg_row = Rows - 1;
1116 msg_col = 0; /* put position back at start of line */
1117 }
1118
1119 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001120 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 */
1122 if (do_equal || dir != 0)
1123 win_equal(wp, TRUE,
1124#ifdef FEAT_VERTSPLIT
1125 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1126 : dir == 'h' ? 'b' :
1127#endif
1128 'v');
1129
1130 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1131 * size was given. */
1132#ifdef FEAT_VERTSPLIT
1133 if (flags & WSP_VERT)
1134 {
1135 i = p_wiw;
1136 if (size != 0)
1137 p_wiw = size;
1138
1139# ifdef FEAT_GUI
1140 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1141 if (gui.in_use)
1142 gui_init_which_components(NULL);
1143# endif
1144 }
1145 else
1146#endif
1147 {
1148 i = p_wh;
1149 if (size != 0)
1150 p_wh = size;
1151 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001152
1153 /*
1154 * make the new window the current window
1155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 win_enter(wp, FALSE);
1157#ifdef FEAT_VERTSPLIT
1158 if (flags & WSP_VERT)
1159 p_wiw = i;
1160 else
1161#endif
1162 p_wh = i;
1163
1164 return OK;
1165}
1166
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001167
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001168/*
1169 * Initialize window "newp" from window "oldp".
1170 * Used when splitting a window and when creating a new tab page.
1171 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001172 * WSP_NEWLOC may be specified in flags to prevent the location list from
1173 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001174 */
1175 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001176win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001177 win_T *newp;
1178 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001179 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001180{
1181 int i;
1182
1183 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001184#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001185 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001186#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001187 oldp->w_buffer->b_nwindows++;
1188 newp->w_cursor = oldp->w_cursor;
1189 newp->w_valid = 0;
1190 newp->w_curswant = oldp->w_curswant;
1191 newp->w_set_curswant = oldp->w_set_curswant;
1192 newp->w_topline = oldp->w_topline;
1193#ifdef FEAT_DIFF
1194 newp->w_topfill = oldp->w_topfill;
1195#endif
1196 newp->w_leftcol = oldp->w_leftcol;
1197 newp->w_pcmark = oldp->w_pcmark;
1198 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1199 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001200 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001201 newp->w_fraction = oldp->w_fraction;
1202 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1203#ifdef FEAT_JUMPLIST
1204 copy_jumplist(oldp, newp);
1205#endif
1206#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001207 if (flags & WSP_NEWLOC)
1208 {
1209 /* Don't copy the location list. */
1210 newp->w_llist = NULL;
1211 newp->w_llist_ref = NULL;
1212 }
1213 else
1214 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001215#endif
1216 if (oldp->w_localdir != NULL)
1217 newp->w_localdir = vim_strsave(oldp->w_localdir);
1218
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001219 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001220 for (i = 0; i < oldp->w_tagstacklen; i++)
1221 {
1222 newp->w_tagstack[i] = oldp->w_tagstack[i];
1223 if (newp->w_tagstack[i].tagname != NULL)
1224 newp->w_tagstack[i].tagname =
1225 vim_strsave(newp->w_tagstack[i].tagname);
1226 }
1227 newp->w_tagstackidx = oldp->w_tagstackidx;
1228 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001229#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001230 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001231#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001232
1233 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001234
Bram Moolenaara971b822011-09-14 14:43:25 +02001235#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001236 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001237#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001238}
1239
1240/*
1241 * Initialize window "newp" from window"old".
1242 * Only the essential things are copied.
1243 */
1244 static void
1245win_init_some(newp, oldp)
1246 win_T *newp;
1247 win_T *oldp;
1248{
1249 /* Use the same argument list. */
1250 newp->w_alist = oldp->w_alist;
1251 ++newp->w_alist->al_refcount;
1252 newp->w_arg_idx = oldp->w_arg_idx;
1253
1254 /* copy options from existing window */
1255 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001256}
1257
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258#endif /* FEAT_WINDOWS */
1259
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260#if defined(FEAT_WINDOWS) || defined(PROTO)
1261/*
1262 * Check if "win" is a pointer to an existing window.
1263 */
1264 int
1265win_valid(win)
1266 win_T *win;
1267{
1268 win_T *wp;
1269
1270 if (win == NULL)
1271 return FALSE;
1272 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1273 if (wp == win)
1274 return TRUE;
1275 return FALSE;
1276}
1277
1278/*
1279 * Return the number of windows.
1280 */
1281 int
1282win_count()
1283{
1284 win_T *wp;
1285 int count = 0;
1286
1287 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1288 ++count;
1289 return count;
1290}
1291
1292/*
1293 * Make "count" windows on the screen.
1294 * Return actual number of windows on the screen.
1295 * Must be called when there is just one window, filling the whole screen
1296 * (excluding the command line).
1297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 int
1299make_windows(count, vertical)
1300 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001301 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302{
1303 int maxcount;
1304 int todo;
1305
1306#ifdef FEAT_VERTSPLIT
1307 if (vertical)
1308 {
1309 /* Each windows needs at least 'winminwidth' lines and a separator
1310 * column. */
1311 maxcount = (curwin->w_width + curwin->w_vsep_width
1312 - (p_wiw - p_wmw)) / (p_wmw + 1);
1313 }
1314 else
1315#endif
1316 {
1317 /* Each window needs at least 'winminheight' lines and a status line. */
1318 maxcount = (curwin->w_height + curwin->w_status_height
1319 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1320 }
1321
1322 if (maxcount < 2)
1323 maxcount = 2;
1324 if (count > maxcount)
1325 count = maxcount;
1326
1327 /*
1328 * add status line now, otherwise first window will be too big
1329 */
1330 if (count > 1)
1331 last_status(TRUE);
1332
1333#ifdef FEAT_AUTOCMD
1334 /*
1335 * Don't execute autocommands while creating the windows. Must do that
1336 * when putting the buffers in the windows.
1337 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001338 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339#endif
1340
1341 /* todo is number of windows left to create */
1342 for (todo = count - 1; todo > 0; --todo)
1343#ifdef FEAT_VERTSPLIT
1344 if (vertical)
1345 {
1346 if (win_split(curwin->w_width - (curwin->w_width - todo)
1347 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1348 break;
1349 }
1350 else
1351#endif
1352 {
1353 if (win_split(curwin->w_height - (curwin->w_height - todo
1354 * STATUS_HEIGHT) / (todo + 1)
1355 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1356 break;
1357 }
1358
1359#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001360 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361#endif
1362
1363 /* return actual number of windows */
1364 return (count - todo);
1365}
1366
1367/*
1368 * Exchange current and next window
1369 */
1370 static void
1371win_exchange(Prenum)
1372 long Prenum;
1373{
1374 frame_T *frp;
1375 frame_T *frp2;
1376 win_T *wp;
1377 win_T *wp2;
1378 int temp;
1379
1380 if (lastwin == firstwin) /* just one window */
1381 {
1382 beep_flush();
1383 return;
1384 }
1385
1386#ifdef FEAT_GUI
1387 need_mouse_correct = TRUE;
1388#endif
1389
1390 /*
1391 * find window to exchange with
1392 */
1393 if (Prenum)
1394 {
1395 frp = curwin->w_frame->fr_parent->fr_child;
1396 while (frp != NULL && --Prenum > 0)
1397 frp = frp->fr_next;
1398 }
1399 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1400 frp = curwin->w_frame->fr_next;
1401 else /* Swap last window in row/col with previous */
1402 frp = curwin->w_frame->fr_prev;
1403
1404 /* We can only exchange a window with another window, not with a frame
1405 * containing windows. */
1406 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1407 return;
1408 wp = frp->fr_win;
1409
1410/*
1411 * 1. remove curwin from the list. Remember after which window it was in wp2
1412 * 2. insert curwin before wp in the list
1413 * if wp != wp2
1414 * 3. remove wp from the list
1415 * 4. insert wp after wp2
1416 * 5. exchange the status line height and vsep width.
1417 */
1418 wp2 = curwin->w_prev;
1419 frp2 = curwin->w_frame->fr_prev;
1420 if (wp->w_prev != curwin)
1421 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001422 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 frame_remove(curwin->w_frame);
1424 win_append(wp->w_prev, curwin);
1425 frame_insert(frp, curwin->w_frame);
1426 }
1427 if (wp != wp2)
1428 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001429 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 frame_remove(wp->w_frame);
1431 win_append(wp2, wp);
1432 if (frp2 == NULL)
1433 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1434 else
1435 frame_append(frp2, wp->w_frame);
1436 }
1437 temp = curwin->w_status_height;
1438 curwin->w_status_height = wp->w_status_height;
1439 wp->w_status_height = temp;
1440#ifdef FEAT_VERTSPLIT
1441 temp = curwin->w_vsep_width;
1442 curwin->w_vsep_width = wp->w_vsep_width;
1443 wp->w_vsep_width = temp;
1444
1445 /* If the windows are not in the same frame, exchange the sizes to avoid
1446 * messing up the window layout. Otherwise fix the frame sizes. */
1447 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1448 {
1449 temp = curwin->w_height;
1450 curwin->w_height = wp->w_height;
1451 wp->w_height = temp;
1452 temp = curwin->w_width;
1453 curwin->w_width = wp->w_width;
1454 wp->w_width = temp;
1455 }
1456 else
1457 {
1458 frame_fix_height(curwin);
1459 frame_fix_height(wp);
1460 frame_fix_width(curwin);
1461 frame_fix_width(wp);
1462 }
1463#endif
1464
1465 (void)win_comp_pos(); /* recompute window positions */
1466
1467 win_enter(wp, TRUE);
1468 redraw_later(CLEAR);
1469}
1470
1471/*
1472 * rotate windows: if upwards TRUE the second window becomes the first one
1473 * if upwards FALSE the first window becomes the second one
1474 */
1475 static void
1476win_rotate(upwards, count)
1477 int upwards;
1478 int count;
1479{
1480 win_T *wp1;
1481 win_T *wp2;
1482 frame_T *frp;
1483 int n;
1484
1485 if (firstwin == lastwin) /* nothing to do */
1486 {
1487 beep_flush();
1488 return;
1489 }
1490
1491#ifdef FEAT_GUI
1492 need_mouse_correct = TRUE;
1493#endif
1494
1495#ifdef FEAT_VERTSPLIT
1496 /* Check if all frames in this row/col have one window. */
1497 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1498 frp = frp->fr_next)
1499 if (frp->fr_win == NULL)
1500 {
1501 EMSG(_("E443: Cannot rotate when another window is split"));
1502 return;
1503 }
1504#endif
1505
1506 while (count--)
1507 {
1508 if (upwards) /* first window becomes last window */
1509 {
1510 /* remove first window/frame from the list */
1511 frp = curwin->w_frame->fr_parent->fr_child;
1512 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001513 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 frame_remove(frp);
1515
1516 /* find last frame and append removed window/frame after it */
1517 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1518 ;
1519 win_append(frp->fr_win, wp1);
1520 frame_append(frp, wp1->w_frame);
1521
1522 wp2 = frp->fr_win; /* previously last window */
1523 }
1524 else /* last window becomes first window */
1525 {
1526 /* find last window/frame in the list and remove it */
1527 for (frp = curwin->w_frame; frp->fr_next != NULL;
1528 frp = frp->fr_next)
1529 ;
1530 wp1 = frp->fr_win;
1531 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001532 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 frame_remove(frp);
1534
1535 /* append the removed window/frame before the first in the list */
1536 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1537 frame_insert(frp->fr_parent->fr_child, frp);
1538 }
1539
1540 /* exchange status height and vsep width of old and new last window */
1541 n = wp2->w_status_height;
1542 wp2->w_status_height = wp1->w_status_height;
1543 wp1->w_status_height = n;
1544 frame_fix_height(wp1);
1545 frame_fix_height(wp2);
1546#ifdef FEAT_VERTSPLIT
1547 n = wp2->w_vsep_width;
1548 wp2->w_vsep_width = wp1->w_vsep_width;
1549 wp1->w_vsep_width = n;
1550 frame_fix_width(wp1);
1551 frame_fix_width(wp2);
1552#endif
1553
1554 /* recompute w_winrow and w_wincol for all windows */
1555 (void)win_comp_pos();
1556 }
1557
1558 redraw_later(CLEAR);
1559}
1560
1561/*
1562 * Move the current window to the very top/bottom/left/right of the screen.
1563 */
1564 static void
1565win_totop(size, flags)
1566 int size;
1567 int flags;
1568{
1569 int dir;
1570 int height = curwin->w_height;
1571
1572 if (lastwin == firstwin)
1573 {
1574 beep_flush();
1575 return;
1576 }
1577
1578 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001579 (void)winframe_remove(curwin, &dir, NULL);
1580 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 last_status(FALSE); /* may need to remove last status line */
1582 (void)win_comp_pos(); /* recompute window positions */
1583
1584 /* Split a window on the desired side and put the window there. */
1585 (void)win_split_ins(size, flags, curwin, dir);
1586 if (!(flags & WSP_VERT))
1587 {
1588 win_setheight(height);
1589 if (p_ea)
1590 win_equal(curwin, TRUE, 'v');
1591 }
1592
1593#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1594 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1595 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001596 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598}
1599
1600/*
1601 * Move window "win1" to below/right of "win2" and make "win1" the current
1602 * window. Only works within the same frame!
1603 */
1604 void
1605win_move_after(win1, win2)
1606 win_T *win1, *win2;
1607{
1608 int height;
1609
1610 /* check if the arguments are reasonable */
1611 if (win1 == win2)
1612 return;
1613
1614 /* check if there is something to do */
1615 if (win2->w_next != win1)
1616 {
1617 /* may need move the status line/vertical separator of the last window
1618 * */
1619 if (win1 == lastwin)
1620 {
1621 height = win1->w_prev->w_status_height;
1622 win1->w_prev->w_status_height = win1->w_status_height;
1623 win1->w_status_height = height;
1624#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001625 if (win1->w_prev->w_vsep_width == 1)
1626 {
1627 /* Remove the vertical separator from the last-but-one window,
1628 * add it to the last window. Adjust the frame widths. */
1629 win1->w_prev->w_vsep_width = 0;
1630 win1->w_prev->w_frame->fr_width -= 1;
1631 win1->w_vsep_width = 1;
1632 win1->w_frame->fr_width += 1;
1633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634#endif
1635 }
1636 else if (win2 == lastwin)
1637 {
1638 height = win1->w_status_height;
1639 win1->w_status_height = win2->w_status_height;
1640 win2->w_status_height = height;
1641#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001642 if (win1->w_vsep_width == 1)
1643 {
1644 /* Remove the vertical separator from win1, add it to the last
1645 * window, win2. Adjust the frame widths. */
1646 win2->w_vsep_width = 1;
1647 win2->w_frame->fr_width += 1;
1648 win1->w_vsep_width = 0;
1649 win1->w_frame->fr_width -= 1;
1650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651#endif
1652 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001653 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 frame_remove(win1->w_frame);
1655 win_append(win2, win1);
1656 frame_append(win2->w_frame, win1->w_frame);
1657
1658 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1659 redraw_later(NOT_VALID);
1660 }
1661 win_enter(win1, FALSE);
1662}
1663
1664/*
1665 * Make all windows the same height.
1666 * 'next_curwin' will soon be the current window, make sure it has enough
1667 * rows.
1668 */
1669 void
1670win_equal(next_curwin, current, dir)
1671 win_T *next_curwin; /* pointer to current window to be or NULL */
1672 int current; /* do only frame with current window */
1673 int dir; /* 'v' for vertically, 'h' for horizontally,
1674 'b' for both, 0 for using p_ead */
1675{
1676 if (dir == 0)
1677#ifdef FEAT_VERTSPLIT
1678 dir = *p_ead;
1679#else
1680 dir = 'b';
1681#endif
1682 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001683 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001684 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685}
1686
1687/*
1688 * Set a frame to a new position and height, spreading the available room
1689 * equally over contained frames.
1690 * The window "next_curwin" (if not NULL) should at least get the size from
1691 * 'winheight' and 'winwidth' if possible.
1692 */
1693 static void
1694win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1695 win_T *next_curwin; /* pointer to current window to be or NULL */
1696 int current; /* do only frame with current window */
1697 frame_T *topfr; /* frame to set size off */
1698 int dir; /* 'v', 'h' or 'b', see win_equal() */
1699 int col; /* horizontal position for frame */
1700 int row; /* vertical position for frame */
1701 int width; /* new width of frame */
1702 int height; /* new height of frame */
1703{
1704 int n, m;
1705 int extra_sep = 0;
1706 int wincount, totwincount = 0;
1707 frame_T *fr;
1708 int next_curwin_size = 0;
1709 int room = 0;
1710 int new_size;
1711 int has_next_curwin = 0;
1712 int hnc;
1713
1714 if (topfr->fr_layout == FR_LEAF)
1715 {
1716 /* Set the width/height of this frame.
1717 * Redraw when size or position changes */
1718 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1719#ifdef FEAT_VERTSPLIT
1720 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1721#endif
1722 )
1723 {
1724 topfr->fr_win->w_winrow = row;
1725 frame_new_height(topfr, height, FALSE, FALSE);
1726#ifdef FEAT_VERTSPLIT
1727 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001728 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729#endif
1730 redraw_all_later(CLEAR);
1731 }
1732 }
1733#ifdef FEAT_VERTSPLIT
1734 else if (topfr->fr_layout == FR_ROW)
1735 {
1736 topfr->fr_width = width;
1737 topfr->fr_height = height;
1738
1739 if (dir != 'v') /* equalize frame widths */
1740 {
1741 /* Compute the maximum number of windows horizontally in this
1742 * frame. */
1743 n = frame_minwidth(topfr, NOWIN);
1744 /* add one for the rightmost window, it doesn't have a separator */
1745 if (col + width == Columns)
1746 extra_sep = 1;
1747 else
1748 extra_sep = 0;
1749 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001750 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001752 /*
1753 * Compute width for "next_curwin" window and room available for
1754 * other windows.
1755 * "m" is the minimal width when counting p_wiw for "next_curwin".
1756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 m = frame_minwidth(topfr, next_curwin);
1758 room = width - m;
1759 if (room < 0)
1760 {
1761 next_curwin_size = p_wiw + room;
1762 room = 0;
1763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 else
1765 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001766 next_curwin_size = -1;
1767 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1768 {
1769 /* If 'winfixwidth' set keep the window width if
1770 * possible.
1771 * Watch out for this window being the next_curwin. */
1772 if (frame_fixed_width(fr))
1773 {
1774 n = frame_minwidth(fr, NOWIN);
1775 new_size = fr->fr_width;
1776 if (frame_has_win(fr, next_curwin))
1777 {
1778 room += p_wiw - p_wmw;
1779 next_curwin_size = 0;
1780 if (new_size < p_wiw)
1781 new_size = p_wiw;
1782 }
1783 else
1784 /* These windows don't use up room. */
1785 totwincount -= (n + (fr->fr_next == NULL
1786 ? extra_sep : 0)) / (p_wmw + 1);
1787 room -= new_size - n;
1788 if (room < 0)
1789 {
1790 new_size += room;
1791 room = 0;
1792 }
1793 fr->fr_newwidth = new_size;
1794 }
1795 }
1796 if (next_curwin_size == -1)
1797 {
1798 if (!has_next_curwin)
1799 next_curwin_size = 0;
1800 else if (totwincount > 1
1801 && (room + (totwincount - 2))
1802 / (totwincount - 1) > p_wiw)
1803 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001804 /* Can make all windows wider than 'winwidth', spread
1805 * the room equally. */
1806 next_curwin_size = (room + p_wiw
1807 + (totwincount - 1) * p_wmw
1808 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001809 room -= next_curwin_size - p_wiw;
1810 }
1811 else
1812 next_curwin_size = p_wiw;
1813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001815
1816 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 --totwincount; /* don't count curwin */
1818 }
1819
1820 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1821 {
1822 n = m = 0;
1823 wincount = 1;
1824 if (fr->fr_next == NULL)
1825 /* last frame gets all that remains (avoid roundoff error) */
1826 new_size = width;
1827 else if (dir == 'v')
1828 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001829 else if (frame_fixed_width(fr))
1830 {
1831 new_size = fr->fr_newwidth;
1832 wincount = 0; /* doesn't count as a sizeable window */
1833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 else
1835 {
1836 /* Compute the maximum number of windows horiz. in "fr". */
1837 n = frame_minwidth(fr, NOWIN);
1838 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1839 / (p_wmw + 1);
1840 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001841 if (has_next_curwin)
1842 hnc = frame_has_win(fr, next_curwin);
1843 else
1844 hnc = FALSE;
1845 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001847 if (totwincount == 0)
1848 new_size = room;
1849 else
1850 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001852 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
1854 next_curwin_size -= p_wiw - (m - n);
1855 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001856 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001858 else
1859 room -= new_size;
1860 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 }
1862
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001863 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 * window, unless equalizing all frames. */
1865 if (!current || dir != 'v' || topfr->fr_parent != NULL
1866 || (new_size != fr->fr_width)
1867 || frame_has_win(fr, next_curwin))
1868 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001869 new_size, height);
1870 col += new_size;
1871 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 totwincount -= wincount;
1873 }
1874 }
1875#endif
1876 else /* topfr->fr_layout == FR_COL */
1877 {
1878#ifdef FEAT_VERTSPLIT
1879 topfr->fr_width = width;
1880#endif
1881 topfr->fr_height = height;
1882
1883 if (dir != 'h') /* equalize frame heights */
1884 {
1885 /* Compute maximum number of windows vertically in this frame. */
1886 n = frame_minheight(topfr, NOWIN);
1887 /* add one for the bottom window if it doesn't have a statusline */
1888 if (row + height == cmdline_row && p_ls == 0)
1889 extra_sep = 1;
1890 else
1891 extra_sep = 0;
1892 totwincount = (n + extra_sep) / (p_wmh + 1);
1893 has_next_curwin = frame_has_win(topfr, next_curwin);
1894
1895 /*
1896 * Compute height for "next_curwin" window and room available for
1897 * other windows.
1898 * "m" is the minimal height when counting p_wh for "next_curwin".
1899 */
1900 m = frame_minheight(topfr, next_curwin);
1901 room = height - m;
1902 if (room < 0)
1903 {
1904 /* The room is less then 'winheight', use all space for the
1905 * current window. */
1906 next_curwin_size = p_wh + room;
1907 room = 0;
1908 }
1909 else
1910 {
1911 next_curwin_size = -1;
1912 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1913 {
1914 /* If 'winfixheight' set keep the window height if
1915 * possible.
1916 * Watch out for this window being the next_curwin. */
1917 if (frame_fixed_height(fr))
1918 {
1919 n = frame_minheight(fr, NOWIN);
1920 new_size = fr->fr_height;
1921 if (frame_has_win(fr, next_curwin))
1922 {
1923 room += p_wh - p_wmh;
1924 next_curwin_size = 0;
1925 if (new_size < p_wh)
1926 new_size = p_wh;
1927 }
1928 else
1929 /* These windows don't use up room. */
1930 totwincount -= (n + (fr->fr_next == NULL
1931 ? extra_sep : 0)) / (p_wmh + 1);
1932 room -= new_size - n;
1933 if (room < 0)
1934 {
1935 new_size += room;
1936 room = 0;
1937 }
1938 fr->fr_newheight = new_size;
1939 }
1940 }
1941 if (next_curwin_size == -1)
1942 {
1943 if (!has_next_curwin)
1944 next_curwin_size = 0;
1945 else if (totwincount > 1
1946 && (room + (totwincount - 2))
1947 / (totwincount - 1) > p_wh)
1948 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001949 /* can make all windows higher than 'winheight',
1950 * spread the room equally. */
1951 next_curwin_size = (room + p_wh
1952 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 + (totwincount - 1)) / totwincount;
1954 room -= next_curwin_size - p_wh;
1955 }
1956 else
1957 next_curwin_size = p_wh;
1958 }
1959 }
1960
1961 if (has_next_curwin)
1962 --totwincount; /* don't count curwin */
1963 }
1964
1965 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1966 {
1967 n = m = 0;
1968 wincount = 1;
1969 if (fr->fr_next == NULL)
1970 /* last frame gets all that remains (avoid roundoff error) */
1971 new_size = height;
1972 else if (dir == 'h')
1973 new_size = fr->fr_height;
1974 else if (frame_fixed_height(fr))
1975 {
1976 new_size = fr->fr_newheight;
1977 wincount = 0; /* doesn't count as a sizeable window */
1978 }
1979 else
1980 {
1981 /* Compute the maximum number of windows vert. in "fr". */
1982 n = frame_minheight(fr, NOWIN);
1983 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1984 / (p_wmh + 1);
1985 m = frame_minheight(fr, next_curwin);
1986 if (has_next_curwin)
1987 hnc = frame_has_win(fr, next_curwin);
1988 else
1989 hnc = FALSE;
1990 if (hnc) /* don't count next_curwin */
1991 --wincount;
1992 if (totwincount == 0)
1993 new_size = room;
1994 else
1995 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1996 / totwincount;
1997 if (hnc) /* add next_curwin size */
1998 {
1999 next_curwin_size -= p_wh - (m - n);
2000 new_size += next_curwin_size;
2001 room -= new_size - next_curwin_size;
2002 }
2003 else
2004 room -= new_size;
2005 new_size += n;
2006 }
2007 /* Skip frame that is full width when splitting or closing a
2008 * window, unless equalizing all frames. */
2009 if (!current || dir != 'h' || topfr->fr_parent != NULL
2010 || (new_size != fr->fr_height)
2011 || frame_has_win(fr, next_curwin))
2012 win_equal_rec(next_curwin, current, fr, dir, col, row,
2013 width, new_size);
2014 row += new_size;
2015 height -= new_size;
2016 totwincount -= wincount;
2017 }
2018 }
2019}
2020
2021/*
2022 * close all windows for buffer 'buf'
2023 */
2024 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002025close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002027 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002029 win_T *wp;
2030 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002031 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032
2033 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002034
2035 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002037 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002039 win_close(wp, FALSE);
2040
2041 /* Start all over, autocommands may change the window layout. */
2042 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
2044 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002045 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002047
2048 /* Also check windows in other tab pages. */
2049 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2050 {
2051 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002052 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002053 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2054 if (wp->w_buffer == buf)
2055 {
2056 win_close_othertab(wp, FALSE, tp);
2057
2058 /* Start all over, the tab page may be closed and
2059 * autocommands may change the window layout. */
2060 nexttp = first_tabpage;
2061 break;
2062 }
2063 }
2064
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002066
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002067 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002068 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069}
2070
2071/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002072 * Return TRUE if the current window is the only window that exists (ignoring
2073 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002074 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002075 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002076 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002077last_window()
2078{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002079 return (one_window() && first_tabpage->tp_next == NULL);
2080}
2081
2082/*
2083 * Return TRUE if there is only one window other than "aucmd_win" in the
2084 * current tab page.
2085 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002086 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002087one_window()
2088{
2089#ifdef FEAT_AUTOCMD
2090 win_T *wp;
2091 int seen_one = FALSE;
2092
2093 FOR_ALL_WINDOWS(wp)
2094 {
2095 if (wp != aucmd_win)
2096 {
2097 if (seen_one)
2098 return FALSE;
2099 seen_one = TRUE;
2100 }
2101 }
2102 return TRUE;
2103#else
2104 return firstwin == lastwin;
2105#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002106}
2107
2108/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002109 * Close the possibly last window in a tab page.
2110 * Returns TRUE when the window was closed already.
2111 */
2112 static int
2113close_last_window_tabpage(win, free_buf, prev_curtab)
2114 win_T *win;
2115 int free_buf;
2116 tabpage_T *prev_curtab;
2117{
2118 if (firstwin == lastwin)
2119 {
2120 /*
2121 * Closing the last window in a tab page. First go to another tab
2122 * page and then close the window and the tab page. This avoids that
2123 * curwin and curtab are invalid while we are freeing memory, they may
2124 * be used in GUI events.
2125 */
2126 goto_tabpage_tp(alt_tabpage());
2127 redraw_tabline = TRUE;
2128
2129 /* Safety check: Autocommands may have closed the window when jumping
2130 * to the other tab page. */
2131 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2132 {
2133 int h = tabline_height();
2134
2135 win_close_othertab(win, free_buf, prev_curtab);
2136 if (h != tabline_height())
2137 shell_new_rows();
2138 }
2139 return TRUE;
2140 }
2141 return FALSE;
2142}
2143
2144/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002145 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 * If "free_buf" is TRUE related buffer may be unloaded.
2147 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002148 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 */
2150 void
2151win_close(win, free_buf)
2152 win_T *win;
2153 int free_buf;
2154{
2155 win_T *wp;
2156#ifdef FEAT_AUTOCMD
2157 int other_buffer = FALSE;
2158#endif
2159 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 int dir;
2161 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002162 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002164 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
2166 EMSG(_("E444: Cannot close last window"));
2167 return;
2168 }
2169
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002170#ifdef FEAT_AUTOCMD
2171 if (win == aucmd_win)
2172 {
2173 EMSG(_("E813: Cannot close autocmd window"));
2174 return;
2175 }
2176 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2177 {
2178 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2179 return;
2180 }
2181#endif
2182
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002183 /* When closing the last window in a tab page first go to another tab page
2184 * and then close the window and the tab page to avoid that curwin and
2185 * curtab are invalid while we are freeing memory. */
2186 if (close_last_window_tabpage(win, free_buf, prev_curtab))
2187 return;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002188
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 /* When closing the help window, try restoring a snapshot after closing
2190 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002191 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 help_window = TRUE;
2193 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002194 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
2196#ifdef FEAT_AUTOCMD
2197 if (win == curwin)
2198 {
2199 /*
2200 * Guess which window is going to be the new current window.
2201 * This may change because of the autocommands (sigh).
2202 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002203 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204
2205 /*
2206 * Be careful: If autocommands delete the window, return now.
2207 */
2208 if (wp->w_buffer != curbuf)
2209 {
2210 other_buffer = TRUE;
2211 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002212 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 return;
2214 }
2215 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002216 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 return;
2218# ifdef FEAT_EVAL
2219 /* autocmds may abort script processing */
2220 if (aborting())
2221 return;
2222# endif
2223 }
2224#endif
2225
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002226#ifdef FEAT_GUI
2227 /* Avoid trouble with scrollbars that are going to be deleted in
2228 * win_free(). */
2229 if (gui.in_use)
2230 out_flush();
2231#endif
2232
Bram Moolenaara971b822011-09-14 14:43:25 +02002233#ifdef FEAT_SYN_HL
2234 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002235 if (win->w_buffer != NULL)
2236 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002237#endif
2238
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 /*
2240 * Close the link to the buffer.
2241 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002242 if (win->w_buffer != NULL)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002243 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002244
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002246 * other window or moved to another tab page. */
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002247 if (!win_valid(win) || last_window() || curtab != prev_curtab
2248 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 return;
2250
Bram Moolenaara971b822011-09-14 14:43:25 +02002251 /* Free the memory used for the window and get the window that received
2252 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002253 wp = win_free_mem(win, &dir, NULL);
2254
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 /* Make sure curwin isn't invalid. It can cause severe trouble when
2256 * printing an error message. For win_equal() curbuf needs to be valid
2257 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002258 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
2260 curwin = wp;
2261#ifdef FEAT_QUICKFIX
2262 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2263 {
2264 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002265 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 * finding another window to go to.
2267 */
2268 for (;;)
2269 {
2270 if (wp->w_next == NULL)
2271 wp = firstwin;
2272 else
2273 wp = wp->w_next;
2274 if (wp == curwin)
2275 break;
2276 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2277 {
2278 curwin = wp;
2279 break;
2280 }
2281 }
2282 }
2283#endif
2284 curbuf = curwin->w_buffer;
2285 close_curwin = TRUE;
2286 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002287 if (p_ea
2288#ifdef FEAT_VERTSPLIT
2289 && (*p_ead == 'b' || *p_ead == dir)
2290#endif
2291 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 win_equal(curwin, TRUE,
2293#ifdef FEAT_VERTSPLIT
2294 dir
2295#else
2296 0
2297#endif
2298 );
2299 else
2300 win_comp_pos();
2301 if (close_curwin)
2302 {
2303 win_enter_ext(wp, FALSE, TRUE);
2304#ifdef FEAT_AUTOCMD
2305 if (other_buffer)
2306 /* careful: after this wp and win may be invalid! */
2307 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2308#endif
2309 }
2310
2311 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002312 * If last window has a status line now and we don't want one,
2313 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 */
2315 last_status(FALSE);
2316
2317 /* After closing the help window, try restoring the window layout from
2318 * before it was opened. */
2319 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002320 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321
2322#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2323 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2324 if (gui.in_use && !win_hasvertsplit())
2325 gui_init_which_components(NULL);
2326#endif
2327
2328 redraw_all_later(NOT_VALID);
2329}
2330
2331/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002332 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002333 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002334 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002335 * Caller must check if buffer is hidden and whether the tabline needs to be
2336 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002337 */
2338 void
2339win_close_othertab(win, free_buf, tp)
2340 win_T *win;
2341 int free_buf;
2342 tabpage_T *tp;
2343{
2344 win_T *wp;
2345 int dir;
2346 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002347 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002348
2349 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002350 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002351
2352 /* Careful: Autocommands may have closed the tab page or made it the
2353 * current tab page. */
2354 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2355 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002356 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002357 return;
2358
2359 /* Autocommands may have closed the window already. */
2360 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2361 ;
2362 if (wp == NULL)
2363 return;
2364
Bram Moolenaarf740b292006-02-16 22:11:02 +00002365 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002366 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002367 {
2368 if (tp == first_tabpage)
2369 first_tabpage = tp->tp_next;
2370 else
2371 {
2372 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2373 ptp = ptp->tp_next)
2374 ;
2375 if (ptp == NULL)
2376 {
2377 EMSG2(_(e_intern2), "win_close_othertab()");
2378 return;
2379 }
2380 ptp->tp_next = tp->tp_next;
2381 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002382 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002383 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002384
2385 /* Free the memory used for the window. */
2386 win_free_mem(win, &dir, tp);
2387
2388 if (free_tp)
2389 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002390}
2391
2392/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002393 * Free the memory used for a window.
2394 * Returns a pointer to the window that got the freed up space.
2395 */
2396 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002397win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002398 win_T *win;
2399 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002400 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002401{
2402 frame_T *frp;
2403 win_T *wp;
2404
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002405 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002406 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002407 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002408 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002409 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002410
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002411 /* When deleting the current window of another tab page select a new
2412 * current window. */
2413 if (tp != NULL && win == tp->tp_curwin)
2414 tp->tp_curwin = wp;
2415
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002416 return wp;
2417}
2418
2419#if defined(EXITFREE) || defined(PROTO)
2420 void
2421win_free_all()
2422{
2423 int dummy;
2424
Bram Moolenaarf740b292006-02-16 22:11:02 +00002425# ifdef FEAT_WINDOWS
2426 while (first_tabpage->tp_next != NULL)
2427 tabpage_close(TRUE);
2428# endif
2429
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002430# ifdef FEAT_AUTOCMD
2431 if (aucmd_win != NULL)
2432 {
2433 (void)win_free_mem(aucmd_win, &dummy, NULL);
2434 aucmd_win = NULL;
2435 }
2436# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002437
2438 while (firstwin != NULL)
2439 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002440}
2441#endif
2442
2443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 * Remove a window and its frame from the tree of frames.
2445 * Returns a pointer to the window that got the freed up space.
2446 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002447 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002448winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002450 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002451 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452{
2453 frame_T *frp, *frp2, *frp3;
2454 frame_T *frp_close = win->w_frame;
2455 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
2457 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002458 * If there is only one window there is nothing to remove.
2459 */
2460 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2461 return NULL;
2462
2463 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 * Remove the window from its frame.
2465 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002466 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 wp = frame2win(frp2);
2468
2469 /* Remove this frame from the list of frames. */
2470 frame_remove(frp_close);
2471
2472#ifdef FEAT_VERTSPLIT
2473 if (frp_close->fr_parent->fr_layout == FR_COL)
2474 {
2475#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002476 /* When 'winfixheight' is set, try to find another frame in the column
2477 * (as close to the closed frame as possible) to distribute the height
2478 * to. */
2479 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2480 {
2481 frp = frp_close->fr_prev;
2482 frp3 = frp_close->fr_next;
2483 while (frp != NULL || frp3 != NULL)
2484 {
2485 if (frp != NULL)
2486 {
2487 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2488 {
2489 frp2 = frp;
2490 wp = frp->fr_win;
2491 break;
2492 }
2493 frp = frp->fr_prev;
2494 }
2495 if (frp3 != NULL)
2496 {
2497 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2498 {
2499 frp2 = frp3;
2500 wp = frp3->fr_win;
2501 break;
2502 }
2503 frp3 = frp3->fr_next;
2504 }
2505 }
2506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2508 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509#ifdef FEAT_VERTSPLIT
2510 *dirp = 'v';
2511 }
2512 else
2513 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002514 /* When 'winfixwidth' is set, try to find another frame in the column
2515 * (as close to the closed frame as possible) to distribute the width
2516 * to. */
2517 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2518 {
2519 frp = frp_close->fr_prev;
2520 frp3 = frp_close->fr_next;
2521 while (frp != NULL || frp3 != NULL)
2522 {
2523 if (frp != NULL)
2524 {
2525 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2526 {
2527 frp2 = frp;
2528 wp = frp->fr_win;
2529 break;
2530 }
2531 frp = frp->fr_prev;
2532 }
2533 if (frp3 != NULL)
2534 {
2535 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2536 {
2537 frp2 = frp3;
2538 wp = frp3->fr_win;
2539 break;
2540 }
2541 frp3 = frp3->fr_next;
2542 }
2543 }
2544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002546 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 *dirp = 'h';
2548 }
2549#endif
2550
2551 /* If rows/columns go to a window below/right its positions need to be
2552 * updated. Can only be done after the sizes have been updated. */
2553 if (frp2 == frp_close->fr_next)
2554 {
2555 int row = win->w_winrow;
2556 int col = W_WINCOL(win);
2557
2558 frame_comp_pos(frp2, &row, &col);
2559 }
2560
2561 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2562 {
2563 /* There is no other frame in this list, move its info to the parent
2564 * and remove it. */
2565 frp2->fr_parent->fr_layout = frp2->fr_layout;
2566 frp2->fr_parent->fr_child = frp2->fr_child;
2567 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2568 frp->fr_parent = frp2->fr_parent;
2569 frp2->fr_parent->fr_win = frp2->fr_win;
2570 if (frp2->fr_win != NULL)
2571 frp2->fr_win->w_frame = frp2->fr_parent;
2572 frp = frp2->fr_parent;
2573 vim_free(frp2);
2574
2575 frp2 = frp->fr_parent;
2576 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2577 {
2578 /* The frame above the parent has the same layout, have to merge
2579 * the frames into this list. */
2580 if (frp2->fr_child == frp)
2581 frp2->fr_child = frp->fr_child;
2582 frp->fr_child->fr_prev = frp->fr_prev;
2583 if (frp->fr_prev != NULL)
2584 frp->fr_prev->fr_next = frp->fr_child;
2585 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2586 {
2587 frp3->fr_parent = frp2;
2588 if (frp3->fr_next == NULL)
2589 {
2590 frp3->fr_next = frp->fr_next;
2591 if (frp->fr_next != NULL)
2592 frp->fr_next->fr_prev = frp3;
2593 break;
2594 }
2595 }
2596 vim_free(frp);
2597 }
2598 }
2599
2600 return wp;
2601}
2602
2603/*
2604 * Find out which frame is going to get the freed up space when "win" is
2605 * closed.
2606 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2607 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2608 * This makes opening a window and closing it immediately keep the same window
2609 * layout.
2610 */
2611 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002612win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002614 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
2616 frame_T *frp;
2617 int b;
2618
Bram Moolenaarf740b292006-02-16 22:11:02 +00002619 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002620 /* Last window in this tab page, will go to next tab page. */
2621 return alt_tabpage()->tp_curwin->w_frame;
2622
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 frp = win->w_frame;
2624#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002625 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 b = p_spr;
2627 else
2628#endif
2629 b = p_sb;
2630 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2631 return frp->fr_next;
2632 return frp->fr_prev;
2633}
2634
2635/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002636 * Return the tabpage that will be used if the current one is closed.
2637 */
2638 static tabpage_T *
2639alt_tabpage()
2640{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002641 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002642
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002643 /* Use the next tab page if possible. */
2644 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002645 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002646
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002647 /* Find the last but one tab page. */
2648 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2649 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002650 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002651}
2652
2653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 * Find the left-upper window in frame "frp".
2655 */
2656 static win_T *
2657frame2win(frp)
2658 frame_T *frp;
2659{
2660 while (frp->fr_win == NULL)
2661 frp = frp->fr_child;
2662 return frp->fr_win;
2663}
2664
2665/*
2666 * Return TRUE if frame "frp" contains window "wp".
2667 */
2668 static int
2669frame_has_win(frp, wp)
2670 frame_T *frp;
2671 win_T *wp;
2672{
2673 frame_T *p;
2674
2675 if (frp->fr_layout == FR_LEAF)
2676 return frp->fr_win == wp;
2677
2678 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2679 if (frame_has_win(p, wp))
2680 return TRUE;
2681 return FALSE;
2682}
2683
2684/*
2685 * Set a new height for a frame. Recursively sets the height for contained
2686 * frames and windows. Caller must take care of positions.
2687 */
2688 static void
2689frame_new_height(topfrp, height, topfirst, wfh)
2690 frame_T *topfrp;
2691 int height;
2692 int topfirst; /* resize topmost contained frame first */
2693 int wfh; /* obey 'winfixheight' when there is a choice;
2694 may cause the height not to be set */
2695{
2696 frame_T *frp;
2697 int extra_lines;
2698 int h;
2699
2700 if (topfrp->fr_win != NULL)
2701 {
2702 /* Simple case: just one window. */
2703 win_new_height(topfrp->fr_win,
2704 height - topfrp->fr_win->w_status_height);
2705 }
2706#ifdef FEAT_VERTSPLIT
2707 else if (topfrp->fr_layout == FR_ROW)
2708 {
2709 do
2710 {
2711 /* All frames in this row get the same new height. */
2712 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2713 {
2714 frame_new_height(frp, height, topfirst, wfh);
2715 if (frp->fr_height > height)
2716 {
2717 /* Could not fit the windows, make the whole row higher. */
2718 height = frp->fr_height;
2719 break;
2720 }
2721 }
2722 }
2723 while (frp != NULL);
2724 }
2725#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002726 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 {
2728 /* Complicated case: Resize a column of frames. Resize the bottom
2729 * frame first, frames above that when needed. */
2730
2731 frp = topfrp->fr_child;
2732 if (wfh)
2733 /* Advance past frames with one window with 'wfh' set. */
2734 while (frame_fixed_height(frp))
2735 {
2736 frp = frp->fr_next;
2737 if (frp == NULL)
2738 return; /* no frame without 'wfh', give up */
2739 }
2740 if (!topfirst)
2741 {
2742 /* Find the bottom frame of this column */
2743 while (frp->fr_next != NULL)
2744 frp = frp->fr_next;
2745 if (wfh)
2746 /* Advance back for frames with one window with 'wfh' set. */
2747 while (frame_fixed_height(frp))
2748 frp = frp->fr_prev;
2749 }
2750
2751 extra_lines = height - topfrp->fr_height;
2752 if (extra_lines < 0)
2753 {
2754 /* reduce height of contained frames, bottom or top frame first */
2755 while (frp != NULL)
2756 {
2757 h = frame_minheight(frp, NULL);
2758 if (frp->fr_height + extra_lines < h)
2759 {
2760 extra_lines += frp->fr_height - h;
2761 frame_new_height(frp, h, topfirst, wfh);
2762 }
2763 else
2764 {
2765 frame_new_height(frp, frp->fr_height + extra_lines,
2766 topfirst, wfh);
2767 break;
2768 }
2769 if (topfirst)
2770 {
2771 do
2772 frp = frp->fr_next;
2773 while (wfh && frp != NULL && frame_fixed_height(frp));
2774 }
2775 else
2776 {
2777 do
2778 frp = frp->fr_prev;
2779 while (wfh && frp != NULL && frame_fixed_height(frp));
2780 }
2781 /* Increase "height" if we could not reduce enough frames. */
2782 if (frp == NULL)
2783 height -= extra_lines;
2784 }
2785 }
2786 else if (extra_lines > 0)
2787 {
2788 /* increase height of bottom or top frame */
2789 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2790 }
2791 }
2792 topfrp->fr_height = height;
2793}
2794
2795/*
2796 * Return TRUE if height of frame "frp" should not be changed because of
2797 * the 'winfixheight' option.
2798 */
2799 static int
2800frame_fixed_height(frp)
2801 frame_T *frp;
2802{
2803 /* frame with one window: fixed height if 'winfixheight' set. */
2804 if (frp->fr_win != NULL)
2805 return frp->fr_win->w_p_wfh;
2806
2807 if (frp->fr_layout == FR_ROW)
2808 {
2809 /* The frame is fixed height if one of the frames in the row is fixed
2810 * height. */
2811 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2812 if (frame_fixed_height(frp))
2813 return TRUE;
2814 return FALSE;
2815 }
2816
2817 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2818 * frames in the row are fixed height. */
2819 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2820 if (!frame_fixed_height(frp))
2821 return FALSE;
2822 return TRUE;
2823}
2824
2825#ifdef FEAT_VERTSPLIT
2826/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002827 * Return TRUE if width of frame "frp" should not be changed because of
2828 * the 'winfixwidth' option.
2829 */
2830 static int
2831frame_fixed_width(frp)
2832 frame_T *frp;
2833{
2834 /* frame with one window: fixed width if 'winfixwidth' set. */
2835 if (frp->fr_win != NULL)
2836 return frp->fr_win->w_p_wfw;
2837
2838 if (frp->fr_layout == FR_COL)
2839 {
2840 /* The frame is fixed width if one of the frames in the row is fixed
2841 * width. */
2842 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2843 if (frame_fixed_width(frp))
2844 return TRUE;
2845 return FALSE;
2846 }
2847
2848 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2849 * frames in the row are fixed width. */
2850 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2851 if (!frame_fixed_width(frp))
2852 return FALSE;
2853 return TRUE;
2854}
2855
2856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 * Add a status line to windows at the bottom of "frp".
2858 * Note: Does not check if there is room!
2859 */
2860 static void
2861frame_add_statusline(frp)
2862 frame_T *frp;
2863{
2864 win_T *wp;
2865
2866 if (frp->fr_layout == FR_LEAF)
2867 {
2868 wp = frp->fr_win;
2869 if (wp->w_status_height == 0)
2870 {
2871 if (wp->w_height > 0) /* don't make it negative */
2872 --wp->w_height;
2873 wp->w_status_height = STATUS_HEIGHT;
2874 }
2875 }
2876 else if (frp->fr_layout == FR_ROW)
2877 {
2878 /* Handle all the frames in the row. */
2879 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2880 frame_add_statusline(frp);
2881 }
2882 else /* frp->fr_layout == FR_COL */
2883 {
2884 /* Only need to handle the last frame in the column. */
2885 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2886 ;
2887 frame_add_statusline(frp);
2888 }
2889}
2890
2891/*
2892 * Set width of a frame. Handles recursively going through contained frames.
2893 * May remove separator line for windows at the right side (for win_close()).
2894 */
2895 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002896frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 frame_T *topfrp;
2898 int width;
2899 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002900 int wfw; /* obey 'winfixwidth' when there is a choice;
2901 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902{
2903 frame_T *frp;
2904 int extra_cols;
2905 int w;
2906 win_T *wp;
2907
2908 if (topfrp->fr_layout == FR_LEAF)
2909 {
2910 /* Simple case: just one window. */
2911 wp = topfrp->fr_win;
2912 /* Find out if there are any windows right of this one. */
2913 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2914 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2915 break;
2916 if (frp->fr_parent == NULL)
2917 wp->w_vsep_width = 0;
2918 win_new_width(wp, width - wp->w_vsep_width);
2919 }
2920 else if (topfrp->fr_layout == FR_COL)
2921 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002922 do
2923 {
2924 /* All frames in this column get the same new width. */
2925 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2926 {
2927 frame_new_width(frp, width, leftfirst, wfw);
2928 if (frp->fr_width > width)
2929 {
2930 /* Could not fit the windows, make whole column wider. */
2931 width = frp->fr_width;
2932 break;
2933 }
2934 }
2935 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 }
2937 else /* fr_layout == FR_ROW */
2938 {
2939 /* Complicated case: Resize a row of frames. Resize the rightmost
2940 * frame first, frames left of it when needed. */
2941
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002943 if (wfw)
2944 /* Advance past frames with one window with 'wfw' set. */
2945 while (frame_fixed_width(frp))
2946 {
2947 frp = frp->fr_next;
2948 if (frp == NULL)
2949 return; /* no frame without 'wfw', give up */
2950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002952 {
2953 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 while (frp->fr_next != NULL)
2955 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002956 if (wfw)
2957 /* Advance back for frames with one window with 'wfw' set. */
2958 while (frame_fixed_width(frp))
2959 frp = frp->fr_prev;
2960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
2962 extra_cols = width - topfrp->fr_width;
2963 if (extra_cols < 0)
2964 {
2965 /* reduce frame width, rightmost frame first */
2966 while (frp != NULL)
2967 {
2968 w = frame_minwidth(frp, NULL);
2969 if (frp->fr_width + extra_cols < w)
2970 {
2971 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002972 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 }
2974 else
2975 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002976 frame_new_width(frp, frp->fr_width + extra_cols,
2977 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 break;
2979 }
2980 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002981 {
2982 do
2983 frp = frp->fr_next;
2984 while (wfw && frp != NULL && frame_fixed_width(frp));
2985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002987 {
2988 do
2989 frp = frp->fr_prev;
2990 while (wfw && frp != NULL && frame_fixed_width(frp));
2991 }
2992 /* Increase "width" if we could not reduce enough frames. */
2993 if (frp == NULL)
2994 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996 }
2997 else if (extra_cols > 0)
2998 {
2999 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003000 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
3002 }
3003 topfrp->fr_width = width;
3004}
3005
3006/*
3007 * Add the vertical separator to windows at the right side of "frp".
3008 * Note: Does not check if there is room!
3009 */
3010 static void
3011frame_add_vsep(frp)
3012 frame_T *frp;
3013{
3014 win_T *wp;
3015
3016 if (frp->fr_layout == FR_LEAF)
3017 {
3018 wp = frp->fr_win;
3019 if (wp->w_vsep_width == 0)
3020 {
3021 if (wp->w_width > 0) /* don't make it negative */
3022 --wp->w_width;
3023 wp->w_vsep_width = 1;
3024 }
3025 }
3026 else if (frp->fr_layout == FR_COL)
3027 {
3028 /* Handle all the frames in the column. */
3029 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3030 frame_add_vsep(frp);
3031 }
3032 else /* frp->fr_layout == FR_ROW */
3033 {
3034 /* Only need to handle the last frame in the row. */
3035 frp = frp->fr_child;
3036 while (frp->fr_next != NULL)
3037 frp = frp->fr_next;
3038 frame_add_vsep(frp);
3039 }
3040}
3041
3042/*
3043 * Set frame width from the window it contains.
3044 */
3045 static void
3046frame_fix_width(wp)
3047 win_T *wp;
3048{
3049 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3050}
3051#endif
3052
3053/*
3054 * Set frame height from the window it contains.
3055 */
3056 static void
3057frame_fix_height(wp)
3058 win_T *wp;
3059{
3060 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3061}
3062
3063/*
3064 * Compute the minimal height for frame "topfrp".
3065 * Uses the 'winminheight' option.
3066 * When "next_curwin" isn't NULL, use p_wh for this window.
3067 * When "next_curwin" is NOWIN, don't use at least one line for the current
3068 * window.
3069 */
3070 static int
3071frame_minheight(topfrp, next_curwin)
3072 frame_T *topfrp;
3073 win_T *next_curwin;
3074{
3075 frame_T *frp;
3076 int m;
3077#ifdef FEAT_VERTSPLIT
3078 int n;
3079#endif
3080
3081 if (topfrp->fr_win != NULL)
3082 {
3083 if (topfrp->fr_win == next_curwin)
3084 m = p_wh + topfrp->fr_win->w_status_height;
3085 else
3086 {
3087 /* window: minimal height of the window plus status line */
3088 m = p_wmh + topfrp->fr_win->w_status_height;
3089 /* Current window is minimal one line high */
3090 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3091 ++m;
3092 }
3093 }
3094#ifdef FEAT_VERTSPLIT
3095 else if (topfrp->fr_layout == FR_ROW)
3096 {
3097 /* get the minimal height from each frame in this row */
3098 m = 0;
3099 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3100 {
3101 n = frame_minheight(frp, next_curwin);
3102 if (n > m)
3103 m = n;
3104 }
3105 }
3106#endif
3107 else
3108 {
3109 /* Add up the minimal heights for all frames in this column. */
3110 m = 0;
3111 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3112 m += frame_minheight(frp, next_curwin);
3113 }
3114
3115 return m;
3116}
3117
3118#ifdef FEAT_VERTSPLIT
3119/*
3120 * Compute the minimal width for frame "topfrp".
3121 * When "next_curwin" isn't NULL, use p_wiw for this window.
3122 * When "next_curwin" is NOWIN, don't use at least one column for the current
3123 * window.
3124 */
3125 static int
3126frame_minwidth(topfrp, next_curwin)
3127 frame_T *topfrp;
3128 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3129{
3130 frame_T *frp;
3131 int m, n;
3132
3133 if (topfrp->fr_win != NULL)
3134 {
3135 if (topfrp->fr_win == next_curwin)
3136 m = p_wiw + topfrp->fr_win->w_vsep_width;
3137 else
3138 {
3139 /* window: minimal width of the window plus separator column */
3140 m = p_wmw + topfrp->fr_win->w_vsep_width;
3141 /* Current window is minimal one column wide */
3142 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3143 ++m;
3144 }
3145 }
3146 else if (topfrp->fr_layout == FR_COL)
3147 {
3148 /* get the minimal width from each frame in this column */
3149 m = 0;
3150 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3151 {
3152 n = frame_minwidth(frp, next_curwin);
3153 if (n > m)
3154 m = n;
3155 }
3156 }
3157 else
3158 {
3159 /* Add up the minimal widths for all frames in this row. */
3160 m = 0;
3161 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3162 m += frame_minwidth(frp, next_curwin);
3163 }
3164
3165 return m;
3166}
3167#endif
3168
3169
3170/*
3171 * Try to close all windows except current one.
3172 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3173 * used and the buffer was modified.
3174 *
3175 * Used by ":bdel" and ":only".
3176 */
3177 void
3178close_others(message, forceit)
3179 int message;
3180 int forceit; /* always hide all other windows */
3181{
3182 win_T *wp;
3183 win_T *nextwp;
3184 int r;
3185
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003186 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
3188 if (message
3189#ifdef FEAT_AUTOCMD
3190 && !autocmd_busy
3191#endif
3192 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003193 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 return;
3195 }
3196
3197 /* Be very careful here: autocommands may change the window layout. */
3198 for (wp = firstwin; win_valid(wp); wp = nextwp)
3199 {
3200 nextwp = wp->w_next;
3201 if (wp != curwin) /* don't close current window */
3202 {
3203
3204 /* Check if it's allowed to abandon this window */
3205 r = can_abandon(wp->w_buffer, forceit);
3206#ifdef FEAT_AUTOCMD
3207 if (!win_valid(wp)) /* autocommands messed wp up */
3208 {
3209 nextwp = firstwin;
3210 continue;
3211 }
3212#endif
3213 if (!r)
3214 {
3215#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3216 if (message && (p_confirm || cmdmod.confirm) && p_write)
3217 {
3218 dialog_changed(wp->w_buffer, FALSE);
3219# ifdef FEAT_AUTOCMD
3220 if (!win_valid(wp)) /* autocommands messed wp up */
3221 {
3222 nextwp = firstwin;
3223 continue;
3224 }
3225# endif
3226 }
3227 if (bufIsChanged(wp->w_buffer))
3228#endif
3229 continue;
3230 }
3231 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3232 }
3233 }
3234
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003235 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 EMSG(_("E445: Other window contains changes"));
3237}
3238
3239#endif /* FEAT_WINDOWS */
3240
3241/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003242 * Init the current window "curwin".
3243 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 */
3245 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003246curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003248 win_init_empty(curwin);
3249}
3250
3251 void
3252win_init_empty(wp)
3253 win_T *wp;
3254{
3255 redraw_win_later(wp, NOT_VALID);
3256 wp->w_lines_valid = 0;
3257 wp->w_cursor.lnum = 1;
3258 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003260 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003262 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3263 wp->w_pcmark.col = 0;
3264 wp->w_prev_pcmark.lnum = 0;
3265 wp->w_prev_pcmark.col = 0;
3266 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003268 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003270 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003272 if (wp->w_p_rl)
3273 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003275 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003277#ifdef FEAT_SYN_HL
3278 wp->w_s = &wp->w_buffer->b_s;
3279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280}
3281
3282/*
3283 * Allocate the first window and put an empty buffer in it.
3284 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003285 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003287 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288win_alloc_first()
3289{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003290 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003291 return FAIL;
3292
3293#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003294 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003295 if (first_tabpage == NULL)
3296 return FAIL;
3297 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003298 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003299#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003300
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003301 return OK;
3302}
3303
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003304#if defined(FEAT_AUTOCMD) || defined(PROTO)
3305/*
3306 * Init "aucmd_win". This can only be done after the first
3307 * window is fully initialized, thus it can't be in win_alloc_first().
3308 */
3309 void
3310win_alloc_aucmd_win()
3311{
3312 aucmd_win = win_alloc(NULL, TRUE);
3313 if (aucmd_win != NULL)
3314 {
3315 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003316 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003317 new_frame(aucmd_win);
3318 }
3319}
3320#endif
3321
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003322/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003323 * Allocate the first window or the first window in a new tab page.
3324 * When "oldwin" is NULL create an empty buffer for it.
3325 * When "oldwin" is not NULL copy info from it to the new window (only with
3326 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003327 * Return FAIL when something goes wrong (out of memory).
3328 */
3329 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003330win_alloc_firstwin(oldwin)
3331 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003332{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003333 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003334 if (oldwin == NULL)
3335 {
3336 /* Very first window, need to create an empty buffer for it and
3337 * initialize from scratch. */
3338 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3339 if (curwin == NULL || curbuf == NULL)
3340 return FAIL;
3341 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003342#ifdef FEAT_SYN_HL
3343 curwin->w_s = &(curbuf->b_s);
3344#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003345 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003347 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003349 curwin_init(); /* init current window */
3350 }
3351#ifdef FEAT_WINDOWS
3352 else
3353 {
3354 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003355 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003356
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003357 /* We don't want cursor- and scroll-binding in the first window. */
3358 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003359 }
3360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003362 new_frame(curwin);
3363 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003364 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003365 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366#ifdef FEAT_VERTSPLIT
3367 topframe->fr_width = Columns;
3368#endif
3369 topframe->fr_height = Rows - p_ch;
3370 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003371
3372 return OK;
3373}
3374
3375/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003376 * Create a frame for window "wp".
3377 */
3378 static void
3379new_frame(win_T *wp)
3380{
3381 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3382
3383 wp->w_frame = frp;
3384 if (frp != NULL)
3385 {
3386 frp->fr_layout = FR_LEAF;
3387 frp->fr_win = wp;
3388 }
3389}
3390
3391/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003392 * Initialize the window and frame size to the maximum.
3393 */
3394 void
3395win_init_size()
3396{
3397 firstwin->w_height = ROWS_AVAIL;
3398 topframe->fr_height = ROWS_AVAIL;
3399#ifdef FEAT_VERTSPLIT
3400 firstwin->w_width = Columns;
3401 topframe->fr_width = Columns;
3402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403}
3404
3405#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003406
3407/*
3408 * Allocate a new tabpage_T and init the values.
3409 * Returns NULL when out of memory.
3410 */
3411 static tabpage_T *
3412alloc_tabpage()
3413{
3414 tabpage_T *tp;
3415
3416 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3417 if (tp != NULL)
3418 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003419# ifdef FEAT_GUI
3420 int i;
3421
3422 for (i = 0; i < 3; i++)
3423 tp->tp_prev_which_scrollbars[i] = -1;
3424# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003425# ifdef FEAT_DIFF
3426 tp->tp_diff_invalid = TRUE;
3427# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003428#ifdef FEAT_EVAL
3429 /* init t: variables */
3430 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3431#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003432 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003433 }
3434 return tp;
3435}
3436
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003437 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003438free_tabpage(tp)
3439 tabpage_T *tp;
3440{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003441 int idx;
3442
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003443# ifdef FEAT_DIFF
3444 diff_clear(tp);
3445# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003446 for (idx = 0; idx < SNAP_COUNT; ++idx)
3447 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003448#ifdef FEAT_EVAL
3449 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3450#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003451 vim_free(tp);
3452}
3453
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003454/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003455 * Create a new Tab page with one window.
3456 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003457 * When "after" is 0 put it just after the current Tab page.
3458 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003459 * Return FAIL or OK.
3460 */
3461 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003462win_new_tabpage(after)
3463 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003464{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003465 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003466 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003467 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003468
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003469 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003470 if (newtp == NULL)
3471 return FAIL;
3472
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003473 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003474 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003475 {
3476 vim_free(newtp);
3477 return FAIL;
3478 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003479 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003480
3481 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003482 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003483 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003484 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003485 if (after == 1)
3486 {
3487 /* New tab page becomes the first one. */
3488 newtp->tp_next = first_tabpage;
3489 first_tabpage = newtp;
3490 }
3491 else
3492 {
3493 if (after > 0)
3494 {
3495 /* Put new tab page before tab page "after". */
3496 n = 2;
3497 for (tp = first_tabpage; tp->tp_next != NULL
3498 && n < after; tp = tp->tp_next)
3499 ++n;
3500 }
3501 newtp->tp_next = tp->tp_next;
3502 tp->tp_next = newtp;
3503 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003504 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003505 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003506 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003507
3508 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003509 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003510
3511#if defined(FEAT_GUI)
3512 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3513 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003514 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003515#endif
3516
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003517 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003518#ifdef FEAT_AUTOCMD
3519 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3520 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3521#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003522 return OK;
3523 }
3524
3525 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003526 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003527 return FAIL;
3528}
3529
3530/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003531 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3532 * like with ":split".
3533 * Returns OK if a new tab page was created, FAIL otherwise.
3534 */
3535 int
3536may_open_tabpage()
3537{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003538 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003539
Bram Moolenaard326ce82007-03-11 14:48:29 +00003540 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003541 {
3542 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003543 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003544 return win_new_tabpage(n);
3545 }
3546 return FAIL;
3547}
3548
3549/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003550 * Create up to "maxcount" tabpages with empty windows.
3551 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003552 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003553 int
3554make_tabpages(maxcount)
3555 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003556{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003557 int count = maxcount;
3558 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003559
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003560 /* Limit to 'tabpagemax' tabs. */
3561 if (count > p_tpm)
3562 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003563
3564#ifdef FEAT_AUTOCMD
3565 /*
3566 * Don't execute autocommands while creating the tab pages. Must do that
3567 * when putting the buffers in the windows.
3568 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003569 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003570#endif
3571
3572 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003573 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003574 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003575
3576#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003577 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003578#endif
3579
3580 /* return actual number of tab pages */
3581 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003582}
3583
3584/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003585 * Return TRUE when "tpc" points to a valid tab page.
3586 */
3587 int
3588valid_tabpage(tpc)
3589 tabpage_T *tpc;
3590{
3591 tabpage_T *tp;
3592
3593 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3594 if (tp == tpc)
3595 return TRUE;
3596 return FALSE;
3597}
3598
3599/*
3600 * Find tab page "n" (first one is 1). Returns NULL when not found.
3601 */
3602 tabpage_T *
3603find_tabpage(n)
3604 int n;
3605{
3606 tabpage_T *tp;
3607 int i = 1;
3608
3609 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3610 ++i;
3611 return tp;
3612}
3613
3614/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003615 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003616 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003617 */
3618 int
3619tabpage_index(ftp)
3620 tabpage_T *ftp;
3621{
3622 int i = 1;
3623 tabpage_T *tp;
3624
3625 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3626 ++i;
3627 return i;
3628}
3629
3630/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003631 * Prepare for leaving the current tab page.
3632 * When autocomands change "curtab" we don't leave the tab page and return
3633 * FAIL.
3634 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003635 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003636 static int
3637leave_tabpage(new_curbuf)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003638 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003639 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003640{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003641 tabpage_T *tp = curtab;
3642
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003643#ifdef FEAT_VISUAL
3644 reset_VIsual_and_resel(); /* stop Visual mode */
3645#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003646#ifdef FEAT_AUTOCMD
3647 if (new_curbuf != curbuf)
3648 {
3649 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3650 if (curtab != tp)
3651 return FAIL;
3652 }
3653 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3654 if (curtab != tp)
3655 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003656 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003657 if (curtab != tp)
3658 return FAIL;
3659#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003660#if defined(FEAT_GUI)
3661 /* Remove the scrollbars. They may be added back later. */
3662 if (gui.in_use)
3663 gui_remove_scrollbars();
3664#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003665 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003666 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003667 tp->tp_firstwin = firstwin;
3668 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003669 tp->tp_old_Rows = Rows;
3670 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003671 firstwin = NULL;
3672 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003673 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003674}
3675
3676/*
3677 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003678 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003679 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003680 static void
3681enter_tabpage(tp, old_curbuf)
3682 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003683 buf_T *old_curbuf UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003684{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003685 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003686 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003687
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003688 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003689 firstwin = tp->tp_firstwin;
3690 lastwin = tp->tp_lastwin;
3691 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003692
3693 /* We would like doing the TabEnter event first, but we don't have a
3694 * valid current window yet, which may break some commands.
3695 * This triggers autocommands, thus may make "tp" invalid. */
3696 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3697 prevwin = next_prevwin;
3698
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003699 last_status(FALSE); /* status line may appear or disappear */
3700 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003701 must_redraw = CLEAR; /* need to redraw everything */
3702#ifdef FEAT_DIFF
3703 diff_need_scrollbind = TRUE;
3704#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003705
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003706 /* The tabpage line may have appeared or disappeared, may need to resize
3707 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003708 * frame sizes too. Use the stored value of p_ch, so that it can be
3709 * different for each tab page. */
3710 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003711 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3712#ifdef FEAT_GUI_TABLINE
3713 && !gui_use_tabline()
3714#endif
3715 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003716 shell_new_rows();
3717#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003718 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003719 shell_new_columns(); /* update window widths */
3720#endif
3721
3722#if defined(FEAT_GUI)
3723 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3724 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003725 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003726#endif
3727
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003728#ifdef FEAT_AUTOCMD
3729 /* Apply autocommands after updating the display, when 'rows' and
3730 * 'columns' have been set correctly. */
3731 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3732 if (old_curbuf != curbuf)
3733 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3734#endif
3735
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003736 redraw_all_later(CLEAR);
3737}
3738
3739/*
3740 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003741 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003742 */
3743 void
3744goto_tabpage(n)
3745 int n;
3746{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003747 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003748 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003749 int i;
3750
Bram Moolenaard68071d2006-05-02 22:08:30 +00003751 if (text_locked())
3752 {
3753 /* Not allowed when editing the command line. */
3754#ifdef FEAT_CMDWIN
3755 if (cmdwin_type != 0)
3756 EMSG(_(e_cmdwin));
3757 else
3758#endif
3759 EMSG(_(e_secure));
3760 return;
3761 }
3762
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003763 /* If there is only one it can't work. */
3764 if (first_tabpage->tp_next == NULL)
3765 {
3766 if (n > 1)
3767 beep_flush();
3768 return;
3769 }
3770
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003771 if (n == 0)
3772 {
3773 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003774 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003775 tp = first_tabpage;
3776 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003777 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003778 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003779 else if (n < 0)
3780 {
3781 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3782 * this N times. */
3783 ttp = curtab;
3784 for (i = n; i < 0; ++i)
3785 {
3786 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3787 tp = tp->tp_next)
3788 ;
3789 ttp = tp;
3790 }
3791 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003792 else if (n == 9999)
3793 {
3794 /* Go to last tab page. */
3795 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3796 ;
3797 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003798 else
3799 {
3800 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003801 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003802 if (tp == NULL)
3803 {
3804 beep_flush();
3805 return;
3806 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003807 }
3808
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003809 goto_tabpage_tp(tp);
3810
3811#ifdef FEAT_GUI_TABLINE
3812 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003813 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003814#endif
3815}
3816
3817/*
3818 * Go to tabpage "tp".
3819 * Note: doesn't update the GUI tab.
3820 */
3821 void
3822goto_tabpage_tp(tp)
3823 tabpage_T *tp;
3824{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003825 /* Don't repeat a message in another tab page. */
3826 set_keep_msg(NULL, 0);
3827
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003828 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003829 {
3830 if (valid_tabpage(tp))
3831 enter_tabpage(tp, curbuf);
3832 else
3833 enter_tabpage(curtab, curbuf);
3834 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003835}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836
3837/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003838 * Enter window "wp" in tab page "tp".
3839 * Also updates the GUI tab.
3840 */
3841 void
3842goto_tabpage_win(tp, wp)
3843 tabpage_T *tp;
3844 win_T *wp;
3845{
3846 goto_tabpage_tp(tp);
3847 if (curtab == tp && win_valid(wp))
3848 {
3849 win_enter(wp, TRUE);
3850# ifdef FEAT_GUI_TABLINE
3851 if (gui_use_tabline())
3852 gui_mch_set_curtab(tabpage_index(curtab));
3853# endif
3854 }
3855}
3856
3857/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003858 * Move the current tab page to before tab page "nr".
3859 */
3860 void
3861tabpage_move(nr)
3862 int nr;
3863{
3864 int n = nr;
3865 tabpage_T *tp;
3866
3867 if (first_tabpage->tp_next == NULL)
3868 return;
3869
3870 /* Remove the current tab page from the list of tab pages. */
3871 if (curtab == first_tabpage)
3872 first_tabpage = curtab->tp_next;
3873 else
3874 {
3875 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3876 if (tp->tp_next == curtab)
3877 break;
3878 if (tp == NULL) /* "cannot happen" */
3879 return;
3880 tp->tp_next = curtab->tp_next;
3881 }
3882
3883 /* Re-insert it at the specified position. */
3884 if (n == 0)
3885 {
3886 curtab->tp_next = first_tabpage;
3887 first_tabpage = curtab;
3888 }
3889 else
3890 {
3891 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3892 --n;
3893 curtab->tp_next = tp->tp_next;
3894 tp->tp_next = curtab;
3895 }
3896
3897 /* Need to redraw the tabline. Tab page contents doesn't change. */
3898 redraw_tabline = TRUE;
3899}
3900
3901
3902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 * Go to another window.
3904 * When jumping to another buffer, stop Visual mode. Do this before
3905 * changing windows so we can yank the selection into the '*' register.
3906 * When jumping to another window on the same buffer, adjust its cursor
3907 * position to keep the same Visual area.
3908 */
3909 void
3910win_goto(wp)
3911 win_T *wp;
3912{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02003913#ifdef FEAT_CONCEAL
3914 win_T *owp = curwin;
3915#endif
3916
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003917 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 {
3919 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003920 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 return;
3922 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003923#ifdef FEAT_AUTOCMD
3924 if (curbuf_locked())
3925 return;
3926#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003927
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928#ifdef FEAT_VISUAL
3929 if (wp->w_buffer != curbuf)
3930 reset_VIsual_and_resel();
3931 else if (VIsual_active)
3932 wp->w_cursor = curwin->w_cursor;
3933#endif
3934
3935#ifdef FEAT_GUI
3936 need_mouse_correct = TRUE;
3937#endif
3938 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02003939
3940#ifdef FEAT_CONCEAL
3941 /* Conceal cursor line in previous window, unconceal in current window. */
3942 if (win_valid(owp))
3943 update_single_line(owp, owp->w_cursor.lnum);
3944 update_single_line(curwin, curwin->w_cursor.lnum);
3945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946}
3947
3948#if defined(FEAT_PERL) || defined(PROTO)
3949/*
3950 * Find window number "winnr" (counting top to bottom).
3951 */
3952 win_T *
3953win_find_nr(winnr)
3954 int winnr;
3955{
3956 win_T *wp;
3957
3958# ifdef FEAT_WINDOWS
3959 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3960 if (--winnr == 0)
3961 break;
3962 return wp;
3963# else
3964 return curwin;
3965# endif
3966}
3967#endif
3968
3969#ifdef FEAT_VERTSPLIT
3970/*
3971 * Move to window above or below "count" times.
3972 */
3973 static void
3974win_goto_ver(up, count)
3975 int up; /* TRUE to go to win above */
3976 long count;
3977{
3978 frame_T *fr;
3979 frame_T *nfr;
3980 frame_T *foundfr;
3981
3982 foundfr = curwin->w_frame;
3983 while (count--)
3984 {
3985 /*
3986 * First go upwards in the tree of frames until we find a upwards or
3987 * downwards neighbor.
3988 */
3989 fr = foundfr;
3990 for (;;)
3991 {
3992 if (fr == topframe)
3993 goto end;
3994 if (up)
3995 nfr = fr->fr_prev;
3996 else
3997 nfr = fr->fr_next;
3998 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3999 break;
4000 fr = fr->fr_parent;
4001 }
4002
4003 /*
4004 * Now go downwards to find the bottom or top frame in it.
4005 */
4006 for (;;)
4007 {
4008 if (nfr->fr_layout == FR_LEAF)
4009 {
4010 foundfr = nfr;
4011 break;
4012 }
4013 fr = nfr->fr_child;
4014 if (nfr->fr_layout == FR_ROW)
4015 {
4016 /* Find the frame at the cursor row. */
4017 while (fr->fr_next != NULL
4018 && frame2win(fr)->w_wincol + fr->fr_width
4019 <= curwin->w_wincol + curwin->w_wcol)
4020 fr = fr->fr_next;
4021 }
4022 if (nfr->fr_layout == FR_COL && up)
4023 while (fr->fr_next != NULL)
4024 fr = fr->fr_next;
4025 nfr = fr;
4026 }
4027 }
4028end:
4029 if (foundfr != NULL)
4030 win_goto(foundfr->fr_win);
4031}
4032
4033/*
4034 * Move to left or right window.
4035 */
4036 static void
4037win_goto_hor(left, count)
4038 int left; /* TRUE to go to left win */
4039 long count;
4040{
4041 frame_T *fr;
4042 frame_T *nfr;
4043 frame_T *foundfr;
4044
4045 foundfr = curwin->w_frame;
4046 while (count--)
4047 {
4048 /*
4049 * First go upwards in the tree of frames until we find a left or
4050 * right neighbor.
4051 */
4052 fr = foundfr;
4053 for (;;)
4054 {
4055 if (fr == topframe)
4056 goto end;
4057 if (left)
4058 nfr = fr->fr_prev;
4059 else
4060 nfr = fr->fr_next;
4061 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4062 break;
4063 fr = fr->fr_parent;
4064 }
4065
4066 /*
4067 * Now go downwards to find the leftmost or rightmost frame in it.
4068 */
4069 for (;;)
4070 {
4071 if (nfr->fr_layout == FR_LEAF)
4072 {
4073 foundfr = nfr;
4074 break;
4075 }
4076 fr = nfr->fr_child;
4077 if (nfr->fr_layout == FR_COL)
4078 {
4079 /* Find the frame at the cursor row. */
4080 while (fr->fr_next != NULL
4081 && frame2win(fr)->w_winrow + fr->fr_height
4082 <= curwin->w_winrow + curwin->w_wrow)
4083 fr = fr->fr_next;
4084 }
4085 if (nfr->fr_layout == FR_ROW && left)
4086 while (fr->fr_next != NULL)
4087 fr = fr->fr_next;
4088 nfr = fr;
4089 }
4090 }
4091end:
4092 if (foundfr != NULL)
4093 win_goto(foundfr->fr_win);
4094}
4095#endif
4096
4097/*
4098 * Make window "wp" the current window.
4099 */
4100 void
4101win_enter(wp, undo_sync)
4102 win_T *wp;
4103 int undo_sync;
4104{
4105 win_enter_ext(wp, undo_sync, FALSE);
4106}
4107
4108/*
4109 * Make window wp the current window.
4110 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4111 * been closed and isn't valid.
4112 */
4113 static void
4114win_enter_ext(wp, undo_sync, curwin_invalid)
4115 win_T *wp;
4116 int undo_sync;
4117 int curwin_invalid;
4118{
4119#ifdef FEAT_AUTOCMD
4120 int other_buffer = FALSE;
4121#endif
4122
4123 if (wp == curwin && !curwin_invalid) /* nothing to do */
4124 return;
4125
4126#ifdef FEAT_AUTOCMD
4127 if (!curwin_invalid)
4128 {
4129 /*
4130 * Be careful: If autocommands delete the window, return now.
4131 */
4132 if (wp->w_buffer != curbuf)
4133 {
4134 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4135 other_buffer = TRUE;
4136 if (!win_valid(wp))
4137 return;
4138 }
4139 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4140 if (!win_valid(wp))
4141 return;
4142# ifdef FEAT_EVAL
4143 /* autocmds may abort script processing */
4144 if (aborting())
4145 return;
4146# endif
4147 }
4148#endif
4149
4150 /* sync undo before leaving the current buffer */
4151 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004152 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 /* may have to copy the buffer options when 'cpo' contains 'S' */
4154 if (wp->w_buffer != curbuf)
4155 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4156 if (!curwin_invalid)
4157 {
4158 prevwin = curwin; /* remember for CTRL-W p */
4159 curwin->w_redr_status = TRUE;
4160 }
4161 curwin = wp;
4162 curbuf = wp->w_buffer;
4163 check_cursor();
4164#ifdef FEAT_VIRTUALEDIT
4165 if (!virtual_active())
4166 curwin->w_cursor.coladd = 0;
4167#endif
4168 changed_line_abv_curs(); /* assume cursor position needs updating */
4169
4170 if (curwin->w_localdir != NULL)
4171 {
4172 /* Window has a local directory: Save current directory as global
4173 * directory (unless that was done already) and change to the local
4174 * directory. */
4175 if (globaldir == NULL)
4176 {
4177 char_u cwd[MAXPATHL];
4178
4179 if (mch_dirname(cwd, MAXPATHL) == OK)
4180 globaldir = vim_strsave(cwd);
4181 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004182 if (mch_chdir((char *)curwin->w_localdir) == 0)
4183 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185 else if (globaldir != NULL)
4186 {
4187 /* Window doesn't have a local directory and we are not in the global
4188 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004189 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 vim_free(globaldir);
4191 globaldir = NULL;
4192 shorten_fnames(TRUE);
4193 }
4194
4195#ifdef FEAT_AUTOCMD
4196 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4197 if (other_buffer)
4198 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4199#endif
4200
4201#ifdef FEAT_TITLE
4202 maketitle();
4203#endif
4204 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004205 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 if (restart_edit)
4207 redraw_later(VALID); /* causes status line redraw */
4208
4209 /* set window height to desired minimal value */
4210 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4211 win_setheight((int)p_wh);
4212 else if (curwin->w_height == 0)
4213 win_setheight(1);
4214
4215#ifdef FEAT_VERTSPLIT
4216 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004217 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 win_setwidth((int)p_wiw);
4219#endif
4220
4221#ifdef FEAT_MOUSE
4222 setmouse(); /* in case jumped to/from help buffer */
4223#endif
4224
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004225 /* Change directories when the 'acd' option is set. */
4226 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227}
4228
4229#endif /* FEAT_WINDOWS */
4230
4231#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4232/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004233 * Jump to the first open window that contains buffer "buf", if one exists.
4234 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 */
4236 win_T *
4237buf_jump_open_win(buf)
4238 buf_T *buf;
4239{
4240# ifdef FEAT_WINDOWS
4241 win_T *wp;
4242
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004243 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 if (wp->w_buffer == buf)
4245 break;
4246 if (wp != NULL)
4247 win_enter(wp, FALSE);
4248 return wp;
4249# else
4250 if (curwin->w_buffer == buf)
4251 return curwin;
4252 return NULL;
4253# endif
4254}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004255
4256/*
4257 * Jump to the first open window in any tab page that contains buffer "buf",
4258 * if one exists.
4259 * Returns a pointer to the window found, otherwise NULL.
4260 */
4261 win_T *
4262buf_jump_open_tab(buf)
4263 buf_T *buf;
4264{
4265# ifdef FEAT_WINDOWS
4266 win_T *wp;
4267 tabpage_T *tp;
4268
4269 /* First try the current tab page. */
4270 wp = buf_jump_open_win(buf);
4271 if (wp != NULL)
4272 return wp;
4273
4274 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4275 if (tp != curtab)
4276 {
4277 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4278 if (wp->w_buffer == buf)
4279 break;
4280 if (wp != NULL)
4281 {
4282 goto_tabpage_win(tp, wp);
4283 if (curwin != wp)
4284 wp = NULL; /* something went wrong */
4285 break;
4286 }
4287 }
4288
4289 return wp;
4290# else
4291 if (curwin->w_buffer == buf)
4292 return curwin;
4293 return NULL;
4294# endif
4295}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296#endif
4297
4298/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004299 * Allocate a window structure and link it in the window list when "hidden" is
4300 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004303win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004304 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004305 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004307 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308
4309 /*
4310 * allocate window structure and linesizes arrays
4311 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004312 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4313 if (new_wp != NULL && win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004315 vim_free(new_wp);
4316 new_wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 }
4318
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004319 if (new_wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004321#ifdef FEAT_AUTOCMD
4322 /* Don't execute autocommands while the window is not properly
4323 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4324 * event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004325 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 /*
4328 * link the window in the window list
4329 */
4330#ifdef FEAT_WINDOWS
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004331 if (!hidden)
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004332 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333#endif
4334#ifdef FEAT_VERTSPLIT
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004335 new_wp->w_wincol = 0;
4336 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337#endif
4338
4339 /* position the display and the cursor at the top of the file. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004340 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341#ifdef FEAT_DIFF
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004342 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004344 new_wp->w_botline = 2;
4345 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346#ifdef FEAT_SCROLLBIND
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004347 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348#endif
4349
4350 /* We won't calculate w_fraction until resizing the window */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004351 new_wp->w_fraction = 0;
4352 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
4354#ifdef FEAT_GUI
4355 if (gui.in_use)
4356 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004357 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4358 SBAR_LEFT, new_wp);
4359 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4360 SBAR_RIGHT, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362#endif
4363#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004364 /* init w: variables */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004365 init_var_dict(&new_wp->w_vars, &new_wp->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366#endif
4367#ifdef FEAT_FOLDING
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004368 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004370#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004371 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004372#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004373#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004374 new_wp->w_match_head = NULL;
4375 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004378 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379}
4380
4381#if defined(FEAT_WINDOWS) || defined(PROTO)
4382
4383/*
4384 * remove window 'wp' from the window list and free the structure
4385 */
4386 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004387win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004389 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390{
4391 int i;
4392
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004393#ifdef FEAT_FOLDING
4394 clearFolding(wp);
4395#endif
4396
4397 /* reduce the reference count to the argument list. */
4398 alist_unlink(wp->w_alist);
4399
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004400#ifdef FEAT_AUTOCMD
4401 /* Don't execute autocommands while the window is halfway being deleted.
4402 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004403 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004404#endif
4405
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004406#ifdef FEAT_LUA
4407 lua_window_free(wp);
4408#endif
4409
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004410#ifdef FEAT_MZSCHEME
4411 mzscheme_window_free(wp);
4412#endif
4413
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414#ifdef FEAT_PERL
4415 perl_win_free(wp);
4416#endif
4417
4418#ifdef FEAT_PYTHON
4419 python_window_free(wp);
4420#endif
4421
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004422#ifdef FEAT_PYTHON3
4423 python3_window_free(wp);
4424#endif
4425
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426#ifdef FEAT_TCL
4427 tcl_window_free(wp);
4428#endif
4429
4430#ifdef FEAT_RUBY
4431 ruby_window_free(wp);
4432#endif
4433
4434 clear_winopt(&wp->w_onebuf_opt);
4435 clear_winopt(&wp->w_allbuf_opt);
4436
4437#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004438 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439#endif
4440
4441 if (prevwin == wp)
4442 prevwin = NULL;
4443 win_free_lsize(wp);
4444
4445 for (i = 0; i < wp->w_tagstacklen; ++i)
4446 vim_free(wp->w_tagstack[i].tagname);
4447
4448 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004449
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004451 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004453
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454#ifdef FEAT_JUMPLIST
4455 free_jumplist(wp);
4456#endif
4457
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004458#ifdef FEAT_QUICKFIX
4459 qf_free_all(wp);
4460#endif
4461
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462#ifdef FEAT_GUI
4463 if (gui.in_use)
4464 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4466 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4467 }
4468#endif /* FEAT_GUI */
4469
Bram Moolenaar860cae12010-06-05 23:22:07 +02004470#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004471 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004472#endif
4473
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004474#ifdef FEAT_AUTOCMD
4475 if (wp != aucmd_win)
4476#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004477 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004479
4480#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004481 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483}
4484
4485/*
4486 * Append window "wp" in the window list after window "after".
4487 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004488 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489win_append(after, wp)
4490 win_T *after, *wp;
4491{
4492 win_T *before;
4493
4494 if (after == NULL) /* after NULL is in front of the first */
4495 before = firstwin;
4496 else
4497 before = after->w_next;
4498
4499 wp->w_next = before;
4500 wp->w_prev = after;
4501 if (after == NULL)
4502 firstwin = wp;
4503 else
4504 after->w_next = wp;
4505 if (before == NULL)
4506 lastwin = wp;
4507 else
4508 before->w_prev = wp;
4509}
4510
4511/*
4512 * Remove a window from the window list.
4513 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004514 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004515win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004517 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518{
4519 if (wp->w_prev != NULL)
4520 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004521 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004523 else
4524 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 if (wp->w_next != NULL)
4526 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004527 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004529 else
4530 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531}
4532
4533/*
4534 * Append frame "frp" in a frame list after frame "after".
4535 */
4536 static void
4537frame_append(after, frp)
4538 frame_T *after, *frp;
4539{
4540 frp->fr_next = after->fr_next;
4541 after->fr_next = frp;
4542 if (frp->fr_next != NULL)
4543 frp->fr_next->fr_prev = frp;
4544 frp->fr_prev = after;
4545}
4546
4547/*
4548 * Insert frame "frp" in a frame list before frame "before".
4549 */
4550 static void
4551frame_insert(before, frp)
4552 frame_T *before, *frp;
4553{
4554 frp->fr_next = before;
4555 frp->fr_prev = before->fr_prev;
4556 before->fr_prev = frp;
4557 if (frp->fr_prev != NULL)
4558 frp->fr_prev->fr_next = frp;
4559 else
4560 frp->fr_parent->fr_child = frp;
4561}
4562
4563/*
4564 * Remove a frame from a frame list.
4565 */
4566 static void
4567frame_remove(frp)
4568 frame_T *frp;
4569{
4570 if (frp->fr_prev != NULL)
4571 frp->fr_prev->fr_next = frp->fr_next;
4572 else
4573 frp->fr_parent->fr_child = frp->fr_next;
4574 if (frp->fr_next != NULL)
4575 frp->fr_next->fr_prev = frp->fr_prev;
4576}
4577
4578#endif /* FEAT_WINDOWS */
4579
4580/*
4581 * Allocate w_lines[] for window "wp".
4582 * Return FAIL for failure, OK for success.
4583 */
4584 int
4585win_alloc_lines(wp)
4586 win_T *wp;
4587{
4588 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004589 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 if (wp->w_lines == NULL)
4591 return FAIL;
4592 return OK;
4593}
4594
4595/*
4596 * free lsize arrays for a window
4597 */
4598 void
4599win_free_lsize(wp)
4600 win_T *wp;
4601{
4602 vim_free(wp->w_lines);
4603 wp->w_lines = NULL;
4604}
4605
4606/*
4607 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004608 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 */
4610 void
4611shell_new_rows()
4612{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004613 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
4615 if (firstwin == NULL) /* not initialized yet */
4616 return;
4617#ifdef FEAT_WINDOWS
4618 if (h < frame_minheight(topframe, NULL))
4619 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004620
4621 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 * that doesn't result in the right height, forget about that option. */
4623 frame_new_height(topframe, h, FALSE, TRUE);
4624 if (topframe->fr_height != h)
4625 frame_new_height(topframe, h, FALSE, FALSE);
4626
4627 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4628#else
4629 if (h < 1)
4630 h = 1;
4631 win_new_height(firstwin, h);
4632#endif
4633 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004634#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004635 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004636#endif
4637
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638#if 0
4639 /* Disabled: don't want making the screen smaller make a window larger. */
4640 if (p_ea)
4641 win_equal(curwin, FALSE, 'v');
4642#endif
4643}
4644
4645#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4646/*
4647 * Called from win_new_shellsize() after Columns changed.
4648 */
4649 void
4650shell_new_columns()
4651{
4652 if (firstwin == NULL) /* not initialized yet */
4653 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004654
4655 /* First try setting the widths of windows with 'winfixwidth'. If that
4656 * doesn't result in the right width, forget about that option. */
4657 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4658 if (topframe->fr_width != Columns)
4659 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4660
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4662#if 0
4663 /* Disabled: don't want making the screen smaller make a window larger. */
4664 if (p_ea)
4665 win_equal(curwin, FALSE, 'h');
4666#endif
4667}
4668#endif
4669
4670#if defined(FEAT_CMDWIN) || defined(PROTO)
4671/*
4672 * Save the size of all windows in "gap".
4673 */
4674 void
4675win_size_save(gap)
4676 garray_T *gap;
4677
4678{
4679 win_T *wp;
4680
4681 ga_init2(gap, (int)sizeof(int), 1);
4682 if (ga_grow(gap, win_count() * 2) == OK)
4683 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4684 {
4685 ((int *)gap->ga_data)[gap->ga_len++] =
4686 wp->w_width + wp->w_vsep_width;
4687 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4688 }
4689}
4690
4691/*
4692 * Restore window sizes, but only if the number of windows is still the same.
4693 * Does not free the growarray.
4694 */
4695 void
4696win_size_restore(gap)
4697 garray_T *gap;
4698{
4699 win_T *wp;
4700 int i;
4701
4702 if (win_count() * 2 == gap->ga_len)
4703 {
4704 i = 0;
4705 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4706 {
4707 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4708 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4709 }
4710 /* recompute the window positions */
4711 (void)win_comp_pos();
4712 }
4713}
4714#endif /* FEAT_CMDWIN */
4715
4716#if defined(FEAT_WINDOWS) || defined(PROTO)
4717/*
4718 * Update the position for all windows, using the width and height of the
4719 * frames.
4720 * Returns the row just after the last window.
4721 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004722 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723win_comp_pos()
4724{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004725 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 int col = 0;
4727
4728 frame_comp_pos(topframe, &row, &col);
4729 return row;
4730}
4731
4732/*
4733 * Update the position of the windows in frame "topfrp", using the width and
4734 * height of the frames.
4735 * "*row" and "*col" are the top-left position of the frame. They are updated
4736 * to the bottom-right position plus one.
4737 */
4738 static void
4739frame_comp_pos(topfrp, row, col)
4740 frame_T *topfrp;
4741 int *row;
4742 int *col;
4743{
4744 win_T *wp;
4745 frame_T *frp;
4746#ifdef FEAT_VERTSPLIT
4747 int startcol;
4748 int startrow;
4749#endif
4750
4751 wp = topfrp->fr_win;
4752 if (wp != NULL)
4753 {
4754 if (wp->w_winrow != *row
4755#ifdef FEAT_VERTSPLIT
4756 || wp->w_wincol != *col
4757#endif
4758 )
4759 {
4760 /* position changed, redraw */
4761 wp->w_winrow = *row;
4762#ifdef FEAT_VERTSPLIT
4763 wp->w_wincol = *col;
4764#endif
4765 redraw_win_later(wp, NOT_VALID);
4766 wp->w_redr_status = TRUE;
4767 }
4768 *row += wp->w_height + wp->w_status_height;
4769#ifdef FEAT_VERTSPLIT
4770 *col += wp->w_width + wp->w_vsep_width;
4771#endif
4772 }
4773 else
4774 {
4775#ifdef FEAT_VERTSPLIT
4776 startrow = *row;
4777 startcol = *col;
4778#endif
4779 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4780 {
4781#ifdef FEAT_VERTSPLIT
4782 if (topfrp->fr_layout == FR_ROW)
4783 *row = startrow; /* all frames are at the same row */
4784 else
4785 *col = startcol; /* all frames are at the same col */
4786#endif
4787 frame_comp_pos(frp, row, col);
4788 }
4789 }
4790}
4791
4792#endif /* FEAT_WINDOWS */
4793
4794/*
4795 * Set current window height and take care of repositioning other windows to
4796 * fit around it.
4797 */
4798 void
4799win_setheight(height)
4800 int height;
4801{
4802 win_setheight_win(height, curwin);
4803}
4804
4805/*
4806 * Set the window height of window "win" and take care of repositioning other
4807 * windows to fit around it.
4808 */
4809 void
4810win_setheight_win(height, win)
4811 int height;
4812 win_T *win;
4813{
4814 int row;
4815
4816 if (win == curwin)
4817 {
4818 /* Always keep current window at least one line high, even when
4819 * 'winminheight' is zero. */
4820#ifdef FEAT_WINDOWS
4821 if (height < p_wmh)
4822 height = p_wmh;
4823#endif
4824 if (height == 0)
4825 height = 1;
4826 }
4827
4828#ifdef FEAT_WINDOWS
4829 frame_setheight(win->w_frame, height + win->w_status_height);
4830
4831 /* recompute the window positions */
4832 row = win_comp_pos();
4833#else
4834 if (height > topframe->fr_height)
4835 height = topframe->fr_height;
4836 win->w_height = height;
4837 row = height;
4838#endif
4839
4840 /*
4841 * If there is extra space created between the last window and the command
4842 * line, clear it.
4843 */
4844 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4845 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4846 cmdline_row = row;
4847 msg_row = row;
4848 msg_col = 0;
4849
4850 redraw_all_later(NOT_VALID);
4851}
4852
4853#if defined(FEAT_WINDOWS) || defined(PROTO)
4854
4855/*
4856 * Set the height of a frame to "height" and take care that all frames and
4857 * windows inside it are resized. Also resize frames on the left and right if
4858 * the are in the same FR_ROW frame.
4859 *
4860 * Strategy:
4861 * If the frame is part of a FR_COL frame, try fitting the frame in that
4862 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4863 * go to containing frames to resize them and make room.
4864 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4865 * Check for the minimal height of the FR_ROW frame.
4866 * At the top level we can also use change the command line height.
4867 */
4868 static void
4869frame_setheight(curfrp, height)
4870 frame_T *curfrp;
4871 int height;
4872{
4873 int room; /* total number of lines available */
4874 int take; /* number of lines taken from other windows */
4875 int room_cmdline; /* lines available from cmdline */
4876 int run;
4877 frame_T *frp;
4878 int h;
4879 int room_reserved;
4880
4881 /* If the height already is the desired value, nothing to do. */
4882 if (curfrp->fr_height == height)
4883 return;
4884
4885 if (curfrp->fr_parent == NULL)
4886 {
4887 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004888 if (height > ROWS_AVAIL)
4889 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 if (height > 0)
4891 frame_new_height(curfrp, height, FALSE, FALSE);
4892 }
4893 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4894 {
4895 /* Row of frames: Also need to resize frames left and right of this
4896 * one. First check for the minimal height of these. */
4897 h = frame_minheight(curfrp->fr_parent, NULL);
4898 if (height < h)
4899 height = h;
4900 frame_setheight(curfrp->fr_parent, height);
4901 }
4902 else
4903 {
4904 /*
4905 * Column of frames: try to change only frames in this column.
4906 */
4907#ifdef FEAT_VERTSPLIT
4908 /*
4909 * Do this twice:
4910 * 1: compute room available, if it's not enough try resizing the
4911 * containing frame.
4912 * 2: compute the room available and adjust the height to it.
4913 * Try not to reduce the height of a window with 'winfixheight' set.
4914 */
4915 for (run = 1; run <= 2; ++run)
4916#else
4917 for (;;)
4918#endif
4919 {
4920 room = 0;
4921 room_reserved = 0;
4922 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4923 frp = frp->fr_next)
4924 {
4925 if (frp != curfrp
4926 && frp->fr_win != NULL
4927 && frp->fr_win->w_p_wfh)
4928 room_reserved += frp->fr_height;
4929 room += frp->fr_height;
4930 if (frp != curfrp)
4931 room -= frame_minheight(frp, NULL);
4932 }
4933#ifdef FEAT_VERTSPLIT
4934 if (curfrp->fr_width != Columns)
4935 room_cmdline = 0;
4936 else
4937#endif
4938 {
4939 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4940 + lastwin->w_height + lastwin->w_status_height);
4941 if (room_cmdline < 0)
4942 room_cmdline = 0;
4943 }
4944
4945 if (height <= room + room_cmdline)
4946 break;
4947#ifdef FEAT_VERTSPLIT
4948 if (run == 2 || curfrp->fr_width == Columns)
4949#endif
4950 {
4951 if (height > room + room_cmdline)
4952 height = room + room_cmdline;
4953 break;
4954 }
4955#ifdef FEAT_VERTSPLIT
4956 frame_setheight(curfrp->fr_parent, height
4957 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4958#endif
4959 /*NOTREACHED*/
4960 }
4961
4962 /*
4963 * Compute the number of lines we will take from others frames (can be
4964 * negative!).
4965 */
4966 take = height - curfrp->fr_height;
4967
4968 /* If there is not enough room, also reduce the height of a window
4969 * with 'winfixheight' set. */
4970 if (height > room + room_cmdline - room_reserved)
4971 room_reserved = room + room_cmdline - height;
4972 /* If there is only a 'winfixheight' window and making the
4973 * window smaller, need to make the other window taller. */
4974 if (take < 0 && room - curfrp->fr_height < room_reserved)
4975 room_reserved = 0;
4976
4977 if (take > 0 && room_cmdline > 0)
4978 {
4979 /* use lines from cmdline first */
4980 if (take < room_cmdline)
4981 room_cmdline = take;
4982 take -= room_cmdline;
4983 topframe->fr_height += room_cmdline;
4984 }
4985
4986 /*
4987 * set the current frame to the new height
4988 */
4989 frame_new_height(curfrp, height, FALSE, FALSE);
4990
4991 /*
4992 * First take lines from the frames after the current frame. If
4993 * that is not enough, takes lines from frames above the current
4994 * frame.
4995 */
4996 for (run = 0; run < 2; ++run)
4997 {
4998 if (run == 0)
4999 frp = curfrp->fr_next; /* 1st run: start with next window */
5000 else
5001 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5002 while (frp != NULL && take != 0)
5003 {
5004 h = frame_minheight(frp, NULL);
5005 if (room_reserved > 0
5006 && frp->fr_win != NULL
5007 && frp->fr_win->w_p_wfh)
5008 {
5009 if (room_reserved >= frp->fr_height)
5010 room_reserved -= frp->fr_height;
5011 else
5012 {
5013 if (frp->fr_height - room_reserved > take)
5014 room_reserved = frp->fr_height - take;
5015 take -= frp->fr_height - room_reserved;
5016 frame_new_height(frp, room_reserved, FALSE, FALSE);
5017 room_reserved = 0;
5018 }
5019 }
5020 else
5021 {
5022 if (frp->fr_height - take < h)
5023 {
5024 take -= frp->fr_height - h;
5025 frame_new_height(frp, h, FALSE, FALSE);
5026 }
5027 else
5028 {
5029 frame_new_height(frp, frp->fr_height - take,
5030 FALSE, FALSE);
5031 take = 0;
5032 }
5033 }
5034 if (run == 0)
5035 frp = frp->fr_next;
5036 else
5037 frp = frp->fr_prev;
5038 }
5039 }
5040 }
5041}
5042
5043#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5044/*
5045 * Set current window width and take care of repositioning other windows to
5046 * fit around it.
5047 */
5048 void
5049win_setwidth(width)
5050 int width;
5051{
5052 win_setwidth_win(width, curwin);
5053}
5054
5055 void
5056win_setwidth_win(width, wp)
5057 int width;
5058 win_T *wp;
5059{
5060 /* Always keep current window at least one column wide, even when
5061 * 'winminwidth' is zero. */
5062 if (wp == curwin)
5063 {
5064 if (width < p_wmw)
5065 width = p_wmw;
5066 if (width == 0)
5067 width = 1;
5068 }
5069
5070 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5071
5072 /* recompute the window positions */
5073 (void)win_comp_pos();
5074
5075 redraw_all_later(NOT_VALID);
5076}
5077
5078/*
5079 * Set the width of a frame to "width" and take care that all frames and
5080 * windows inside it are resized. Also resize frames above and below if the
5081 * are in the same FR_ROW frame.
5082 *
5083 * Strategy is similar to frame_setheight().
5084 */
5085 static void
5086frame_setwidth(curfrp, width)
5087 frame_T *curfrp;
5088 int width;
5089{
5090 int room; /* total number of lines available */
5091 int take; /* number of lines taken from other windows */
5092 int run;
5093 frame_T *frp;
5094 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005095 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096
5097 /* If the width already is the desired value, nothing to do. */
5098 if (curfrp->fr_width == width)
5099 return;
5100
5101 if (curfrp->fr_parent == NULL)
5102 /* topframe: can't change width */
5103 return;
5104
5105 if (curfrp->fr_parent->fr_layout == FR_COL)
5106 {
5107 /* Column of frames: Also need to resize frames above and below of
5108 * this one. First check for the minimal width of these. */
5109 w = frame_minwidth(curfrp->fr_parent, NULL);
5110 if (width < w)
5111 width = w;
5112 frame_setwidth(curfrp->fr_parent, width);
5113 }
5114 else
5115 {
5116 /*
5117 * Row of frames: try to change only frames in this row.
5118 *
5119 * Do this twice:
5120 * 1: compute room available, if it's not enough try resizing the
5121 * containing frame.
5122 * 2: compute the room available and adjust the width to it.
5123 */
5124 for (run = 1; run <= 2; ++run)
5125 {
5126 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005127 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5129 frp = frp->fr_next)
5130 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005131 if (frp != curfrp
5132 && frp->fr_win != NULL
5133 && frp->fr_win->w_p_wfw)
5134 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 room += frp->fr_width;
5136 if (frp != curfrp)
5137 room -= frame_minwidth(frp, NULL);
5138 }
5139
5140 if (width <= room)
5141 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005142 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 {
5144 if (width > room)
5145 width = room;
5146 break;
5147 }
5148 frame_setwidth(curfrp->fr_parent, width
5149 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5150 }
5151
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 /*
5153 * Compute the number of lines we will take from others frames (can be
5154 * negative!).
5155 */
5156 take = width - curfrp->fr_width;
5157
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005158 /* If there is not enough room, also reduce the width of a window
5159 * with 'winfixwidth' set. */
5160 if (width > room - room_reserved)
5161 room_reserved = room - width;
5162 /* If there is only a 'winfixwidth' window and making the
5163 * window smaller, need to make the other window narrower. */
5164 if (take < 0 && room - curfrp->fr_width < room_reserved)
5165 room_reserved = 0;
5166
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 /*
5168 * set the current frame to the new width
5169 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005170 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171
5172 /*
5173 * First take lines from the frames right of the current frame. If
5174 * that is not enough, takes lines from frames left of the current
5175 * frame.
5176 */
5177 for (run = 0; run < 2; ++run)
5178 {
5179 if (run == 0)
5180 frp = curfrp->fr_next; /* 1st run: start with next window */
5181 else
5182 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5183 while (frp != NULL && take != 0)
5184 {
5185 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005186 if (room_reserved > 0
5187 && frp->fr_win != NULL
5188 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005190 if (room_reserved >= frp->fr_width)
5191 room_reserved -= frp->fr_width;
5192 else
5193 {
5194 if (frp->fr_width - room_reserved > take)
5195 room_reserved = frp->fr_width - take;
5196 take -= frp->fr_width - room_reserved;
5197 frame_new_width(frp, room_reserved, FALSE, FALSE);
5198 room_reserved = 0;
5199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 }
5201 else
5202 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005203 if (frp->fr_width - take < w)
5204 {
5205 take -= frp->fr_width - w;
5206 frame_new_width(frp, w, FALSE, FALSE);
5207 }
5208 else
5209 {
5210 frame_new_width(frp, frp->fr_width - take,
5211 FALSE, FALSE);
5212 take = 0;
5213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 }
5215 if (run == 0)
5216 frp = frp->fr_next;
5217 else
5218 frp = frp->fr_prev;
5219 }
5220 }
5221 }
5222}
5223#endif /* FEAT_VERTSPLIT */
5224
5225/*
5226 * Check 'winminheight' for a valid value.
5227 */
5228 void
5229win_setminheight()
5230{
5231 int room;
5232 int first = TRUE;
5233 win_T *wp;
5234
5235 /* loop until there is a 'winminheight' that is possible */
5236 while (p_wmh > 0)
5237 {
5238 /* TODO: handle vertical splits */
5239 room = -p_wh;
5240 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5241 room += wp->w_height - p_wmh;
5242 if (room >= 0)
5243 break;
5244 --p_wmh;
5245 if (first)
5246 {
5247 EMSG(_(e_noroom));
5248 first = FALSE;
5249 }
5250 }
5251}
5252
5253#ifdef FEAT_MOUSE
5254
5255/*
5256 * Status line of dragwin is dragged "offset" lines down (negative is up).
5257 */
5258 void
5259win_drag_status_line(dragwin, offset)
5260 win_T *dragwin;
5261 int offset;
5262{
5263 frame_T *curfr;
5264 frame_T *fr;
5265 int room;
5266 int row;
5267 int up; /* if TRUE, drag status line up, otherwise down */
5268 int n;
5269
5270 fr = dragwin->w_frame;
5271 curfr = fr;
5272 if (fr != topframe) /* more than one window */
5273 {
5274 fr = fr->fr_parent;
5275 /* When the parent frame is not a column of frames, its parent should
5276 * be. */
5277 if (fr->fr_layout != FR_COL)
5278 {
5279 curfr = fr;
5280 if (fr != topframe) /* only a row of windows, may drag statusline */
5281 fr = fr->fr_parent;
5282 }
5283 }
5284
5285 /* If this is the last frame in a column, may want to resize the parent
5286 * frame instead (go two up to skip a row of frames). */
5287 while (curfr != topframe && curfr->fr_next == NULL)
5288 {
5289 if (fr != topframe)
5290 fr = fr->fr_parent;
5291 curfr = fr;
5292 if (fr != topframe)
5293 fr = fr->fr_parent;
5294 }
5295
5296 if (offset < 0) /* drag up */
5297 {
5298 up = TRUE;
5299 offset = -offset;
5300 /* sum up the room of the current frame and above it */
5301 if (fr == curfr)
5302 {
5303 /* only one window */
5304 room = fr->fr_height - frame_minheight(fr, NULL);
5305 }
5306 else
5307 {
5308 room = 0;
5309 for (fr = fr->fr_child; ; fr = fr->fr_next)
5310 {
5311 room += fr->fr_height - frame_minheight(fr, NULL);
5312 if (fr == curfr)
5313 break;
5314 }
5315 }
5316 fr = curfr->fr_next; /* put fr at frame that grows */
5317 }
5318 else /* drag down */
5319 {
5320 up = FALSE;
5321 /*
5322 * Only dragging the last status line can reduce p_ch.
5323 */
5324 room = Rows - cmdline_row;
5325 if (curfr->fr_next == NULL)
5326 room -= 1;
5327 else
5328 room -= p_ch;
5329 if (room < 0)
5330 room = 0;
5331 /* sum up the room of frames below of the current one */
5332 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5333 room += fr->fr_height - frame_minheight(fr, NULL);
5334 fr = curfr; /* put fr at window that grows */
5335 }
5336
5337 if (room < offset) /* Not enough room */
5338 offset = room; /* Move as far as we can */
5339 if (offset <= 0)
5340 return;
5341
5342 /*
5343 * Grow frame fr by "offset" lines.
5344 * Doesn't happen when dragging the last status line up.
5345 */
5346 if (fr != NULL)
5347 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5348
5349 if (up)
5350 fr = curfr; /* current frame gets smaller */
5351 else
5352 fr = curfr->fr_next; /* next frame gets smaller */
5353
5354 /*
5355 * Now make the other frames smaller.
5356 */
5357 while (fr != NULL && offset > 0)
5358 {
5359 n = frame_minheight(fr, NULL);
5360 if (fr->fr_height - offset <= n)
5361 {
5362 offset -= fr->fr_height - n;
5363 frame_new_height(fr, n, !up, FALSE);
5364 }
5365 else
5366 {
5367 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5368 break;
5369 }
5370 if (up)
5371 fr = fr->fr_prev;
5372 else
5373 fr = fr->fr_next;
5374 }
5375 row = win_comp_pos();
5376 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5377 cmdline_row = row;
5378 p_ch = Rows - cmdline_row;
5379 if (p_ch < 1)
5380 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005381 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005382 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 showmode();
5384}
5385
5386#ifdef FEAT_VERTSPLIT
5387/*
5388 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5389 */
5390 void
5391win_drag_vsep_line(dragwin, offset)
5392 win_T *dragwin;
5393 int offset;
5394{
5395 frame_T *curfr;
5396 frame_T *fr;
5397 int room;
5398 int left; /* if TRUE, drag separator line left, otherwise right */
5399 int n;
5400
5401 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005402 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 return;
5404 curfr = fr;
5405 fr = fr->fr_parent;
5406 /* When the parent frame is not a row of frames, its parent should be. */
5407 if (fr->fr_layout != FR_ROW)
5408 {
5409 if (fr == topframe) /* only a column of windows (cannot happen?) */
5410 return;
5411 curfr = fr;
5412 fr = fr->fr_parent;
5413 }
5414
5415 /* If this is the last frame in a row, may want to resize a parent
5416 * frame instead. */
5417 while (curfr->fr_next == NULL)
5418 {
5419 if (fr == topframe)
5420 break;
5421 curfr = fr;
5422 fr = fr->fr_parent;
5423 if (fr != topframe)
5424 {
5425 curfr = fr;
5426 fr = fr->fr_parent;
5427 }
5428 }
5429
5430 if (offset < 0) /* drag left */
5431 {
5432 left = TRUE;
5433 offset = -offset;
5434 /* sum up the room of the current frame and left of it */
5435 room = 0;
5436 for (fr = fr->fr_child; ; fr = fr->fr_next)
5437 {
5438 room += fr->fr_width - frame_minwidth(fr, NULL);
5439 if (fr == curfr)
5440 break;
5441 }
5442 fr = curfr->fr_next; /* put fr at frame that grows */
5443 }
5444 else /* drag right */
5445 {
5446 left = FALSE;
5447 /* sum up the room of frames right of the current one */
5448 room = 0;
5449 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5450 room += fr->fr_width - frame_minwidth(fr, NULL);
5451 fr = curfr; /* put fr at window that grows */
5452 }
5453
5454 if (room < offset) /* Not enough room */
5455 offset = room; /* Move as far as we can */
5456 if (offset <= 0) /* No room at all, quit. */
5457 return;
5458
5459 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005460 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461
5462 /* shrink other frames: current and at the left or at the right */
5463 if (left)
5464 fr = curfr; /* current frame gets smaller */
5465 else
5466 fr = curfr->fr_next; /* next frame gets smaller */
5467
5468 while (fr != NULL && offset > 0)
5469 {
5470 n = frame_minwidth(fr, NULL);
5471 if (fr->fr_width - offset <= n)
5472 {
5473 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005474 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 }
5476 else
5477 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005478 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 break;
5480 }
5481 if (left)
5482 fr = fr->fr_prev;
5483 else
5484 fr = fr->fr_next;
5485 }
5486 (void)win_comp_pos();
5487 redraw_all_later(NOT_VALID);
5488}
5489#endif /* FEAT_VERTSPLIT */
5490#endif /* FEAT_MOUSE */
5491
5492#endif /* FEAT_WINDOWS */
5493
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005494#define FRACTION_MULT 16384L
5495
5496/*
5497 * Set wp->w_fraction for the current w_wrow and w_height.
5498 */
5499 static void
5500set_fraction(wp)
5501 win_T *wp;
5502{
5503 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5504 + FRACTION_MULT / 2) / (long)wp->w_height;
5505}
5506
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507/*
5508 * Set the height of a window.
5509 * This takes care of the things inside the window, not what happens to the
5510 * window position, the frame or to other windows.
5511 */
5512 static void
5513win_new_height(wp, height)
5514 win_T *wp;
5515 int height;
5516{
5517 linenr_T lnum;
5518 int sline, line_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519
5520 /* Don't want a negative height. Happens when splitting a tiny window.
5521 * Will equalize heights soon to fix it. */
5522 if (height < 0)
5523 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005524 if (wp->w_height == height)
5525 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
5527 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005528 set_fraction(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529
5530 wp->w_height = height;
5531 wp->w_skipcol = 0;
5532
5533 /* Don't change w_topline when height is zero. Don't set w_topline when
5534 * 'scrollbind' is set and this isn't the current window. */
5535 if (height > 0
5536#ifdef FEAT_SCROLLBIND
5537 && (!wp->w_p_scb || wp == curwin)
5538#endif
5539 )
5540 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005541 /*
5542 * Find a value for w_topline that shows the cursor at the same
5543 * relative position in the window as before (more or less).
5544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 lnum = wp->w_cursor.lnum;
5546 if (lnum < 1) /* can happen when starting up */
5547 lnum = 1;
5548 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5549 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5550 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005551
5552 if (sline >= 0)
5553 {
5554 /* Make sure the whole cursor line is visible, if possible. */
5555 int rows = plines_win(wp, lnum, FALSE);
5556
5557 if (sline > wp->w_height - rows)
5558 {
5559 sline = wp->w_height - rows;
5560 wp->w_wrow -= rows - line_size;
5561 }
5562 }
5563
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 if (sline < 0)
5565 {
5566 /*
5567 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005568 * Make cursor line the first line in the window. If not enough
5569 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 */
5571 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005572 if (wp->w_wrow >= wp->w_height
5573 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5574 {
5575 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5576 --wp->w_wrow;
5577 while (wp->w_wrow >= wp->w_height)
5578 {
5579 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5580 + win_col_off2(wp);
5581 --wp->w_wrow;
5582 }
5583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 }
5585 else
5586 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005587 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 {
5589#ifdef FEAT_FOLDING
5590 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5591 if (lnum == 1)
5592 {
5593 /* first line in buffer is folded */
5594 line_size = 1;
5595 --sline;
5596 break;
5597 }
5598#endif
5599 --lnum;
5600#ifdef FEAT_DIFF
5601 if (lnum == wp->w_topline)
5602 line_size = plines_win_nofill(wp, lnum, TRUE)
5603 + wp->w_topfill;
5604 else
5605#endif
5606 line_size = plines_win(wp, lnum, TRUE);
5607 sline -= line_size;
5608 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005609
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 if (sline < 0)
5611 {
5612 /*
5613 * Line we want at top would go off top of screen. Use next
5614 * line instead.
5615 */
5616#ifdef FEAT_FOLDING
5617 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5618#endif
5619 lnum++;
5620 wp->w_wrow -= line_size + sline;
5621 }
5622 else if (sline > 0)
5623 {
5624 /* First line of file reached, use that as topline. */
5625 lnum = 1;
5626 wp->w_wrow -= sline;
5627 }
5628 }
5629 set_topline(wp, lnum);
5630 }
5631
5632 if (wp == curwin)
5633 {
5634 if (p_so)
5635 update_topline();
5636 curs_columns(FALSE); /* validate w_wrow */
5637 }
5638 wp->w_prev_fraction_row = wp->w_wrow;
5639
5640 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005641 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642#ifdef FEAT_WINDOWS
5643 wp->w_redr_status = TRUE;
5644#endif
5645 invalidate_botline_win(wp);
5646}
5647
5648#ifdef FEAT_VERTSPLIT
5649/*
5650 * Set the width of a window.
5651 */
5652 static void
5653win_new_width(wp, width)
5654 win_T *wp;
5655 int width;
5656{
5657 wp->w_width = width;
5658 wp->w_lines_valid = 0;
5659 changed_line_abv_curs_win(wp);
5660 invalidate_botline_win(wp);
5661 if (wp == curwin)
5662 {
5663 update_topline();
5664 curs_columns(TRUE); /* validate w_wrow */
5665 }
5666 redraw_win_later(wp, NOT_VALID);
5667 wp->w_redr_status = TRUE;
5668}
5669#endif
5670
5671 void
5672win_comp_scroll(wp)
5673 win_T *wp;
5674{
5675 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5676 if (wp->w_p_scr == 0)
5677 wp->w_p_scr = 1;
5678}
5679
5680/*
5681 * command_height: called whenever p_ch has been changed
5682 */
5683 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005684command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685{
5686#ifdef FEAT_WINDOWS
5687 int h;
5688 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005689 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005691 /* Use the value of p_ch that we remembered. This is needed for when the
5692 * GUI starts up, we can't be sure in what order things happen. And when
5693 * p_ch was changed in another tab page. */
5694 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005695
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 /* Find bottom frame with width of screen. */
5697 frp = lastwin->w_frame;
5698# ifdef FEAT_VERTSPLIT
5699 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5700 frp = frp->fr_parent;
5701# endif
5702
5703 /* Avoid changing the height of a window with 'winfixheight' set. */
5704 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5705 && frp->fr_win->w_p_wfh)
5706 frp = frp->fr_prev;
5707
5708 if (starting != NO_SCREEN)
5709 {
5710 cmdline_row = Rows - p_ch;
5711
5712 if (p_ch > old_p_ch) /* p_ch got bigger */
5713 {
5714 while (p_ch > old_p_ch)
5715 {
5716 if (frp == NULL)
5717 {
5718 EMSG(_(e_noroom));
5719 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005720 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 cmdline_row = Rows - p_ch;
5722 break;
5723 }
5724 h = frp->fr_height - frame_minheight(frp, NULL);
5725 if (h > p_ch - old_p_ch)
5726 h = p_ch - old_p_ch;
5727 old_p_ch += h;
5728 frame_add_height(frp, -h);
5729 frp = frp->fr_prev;
5730 }
5731
5732 /* Recompute window positions. */
5733 (void)win_comp_pos();
5734
5735 /* clear the lines added to cmdline */
5736 if (full_screen)
5737 screen_fill((int)(cmdline_row), (int)Rows, 0,
5738 (int)Columns, ' ', ' ', 0);
5739 msg_row = cmdline_row;
5740 redraw_cmdline = TRUE;
5741 return;
5742 }
5743
5744 if (msg_row < cmdline_row)
5745 msg_row = cmdline_row;
5746 redraw_cmdline = TRUE;
5747 }
5748 frame_add_height(frp, (int)(old_p_ch - p_ch));
5749
5750 /* Recompute window positions. */
5751 if (frp != lastwin->w_frame)
5752 (void)win_comp_pos();
5753#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005755 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756#endif
5757}
5758
5759#if defined(FEAT_WINDOWS) || defined(PROTO)
5760/*
5761 * Resize frame "frp" to be "n" lines higher (negative for less high).
5762 * Also resize the frames it is contained in.
5763 */
5764 static void
5765frame_add_height(frp, n)
5766 frame_T *frp;
5767 int n;
5768{
5769 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5770 for (;;)
5771 {
5772 frp = frp->fr_parent;
5773 if (frp == NULL)
5774 break;
5775 frp->fr_height += n;
5776 }
5777}
5778
5779/*
5780 * Add or remove a status line for the bottom window(s), according to the
5781 * value of 'laststatus'.
5782 */
5783 void
5784last_status(morewin)
5785 int morewin; /* pretend there are two or more windows */
5786{
5787 /* Don't make a difference between horizontal or vertical split. */
5788 last_status_rec(topframe, (p_ls == 2
5789 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5790}
5791
5792 static void
5793last_status_rec(fr, statusline)
5794 frame_T *fr;
5795 int statusline;
5796{
5797 frame_T *fp;
5798 win_T *wp;
5799
5800 if (fr->fr_layout == FR_LEAF)
5801 {
5802 wp = fr->fr_win;
5803 if (wp->w_status_height != 0 && !statusline)
5804 {
5805 /* remove status line */
5806 win_new_height(wp, wp->w_height + 1);
5807 wp->w_status_height = 0;
5808 comp_col();
5809 }
5810 else if (wp->w_status_height == 0 && statusline)
5811 {
5812 /* Find a frame to take a line from. */
5813 fp = fr;
5814 while (fp->fr_height <= frame_minheight(fp, NULL))
5815 {
5816 if (fp == topframe)
5817 {
5818 EMSG(_(e_noroom));
5819 return;
5820 }
5821 /* In a column of frames: go to frame above. If already at
5822 * the top or in a row of frames: go to parent. */
5823 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5824 fp = fp->fr_prev;
5825 else
5826 fp = fp->fr_parent;
5827 }
5828 wp->w_status_height = 1;
5829 if (fp != fr)
5830 {
5831 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5832 frame_fix_height(wp);
5833 (void)win_comp_pos();
5834 }
5835 else
5836 win_new_height(wp, wp->w_height - 1);
5837 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005838 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 }
5840 }
5841#ifdef FEAT_VERTSPLIT
5842 else if (fr->fr_layout == FR_ROW)
5843 {
5844 /* vertically split windows, set status line for each one */
5845 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5846 last_status_rec(fp, statusline);
5847 }
5848#endif
5849 else
5850 {
5851 /* horizontally split window, set status line for last one */
5852 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5853 ;
5854 last_status_rec(fp, statusline);
5855 }
5856}
5857
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005858/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005859 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005860 */
5861 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005862tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005863{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005864#ifdef FEAT_GUI_TABLINE
5865 /* When the GUI has the tabline then this always returns zero. */
5866 if (gui_use_tabline())
5867 return 0;
5868#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005869 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005870 {
5871 case 0: return 0;
5872 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5873 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005874 return 1;
5875}
5876
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877#endif /* FEAT_WINDOWS */
5878
5879#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5880/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005881 * Get the file name at the cursor.
5882 * If Visual mode is active, use the selected text if it's in one line.
5883 * Returns the name in allocated memory, NULL for failure.
5884 */
5885 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005886grab_file_name(count, file_lnum)
5887 long count;
5888 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005889{
5890# ifdef FEAT_VISUAL
5891 if (VIsual_active)
5892 {
5893 int len;
5894 char_u *ptr;
5895
5896 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5897 return NULL;
5898 return find_file_name_in_path(ptr, len,
5899 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5900 }
5901# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005902 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5903 file_lnum);
5904
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005905}
5906
5907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 * Return the file name under or after the cursor.
5909 *
5910 * The 'path' option is searched if the file name is not absolute.
5911 * The string returned has been alloc'ed and should be freed by the caller.
5912 * NULL is returned if the file name or file is not found.
5913 *
5914 * options:
5915 * FNAME_MESS give error messages
5916 * FNAME_EXP expand to path
5917 * FNAME_HYP check for hypertext link
5918 * FNAME_INCL apply "includeexpr"
5919 */
5920 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005921file_name_at_cursor(options, count, file_lnum)
5922 int options;
5923 long count;
5924 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925{
5926 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005927 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5928 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929}
5930
5931/*
5932 * Return the name of the file under or after ptr[col].
5933 * Otherwise like file_name_at_cursor().
5934 */
5935 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005936file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 char_u *line;
5938 int col;
5939 int options;
5940 long count;
5941 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005942 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943{
5944 char_u *ptr;
5945 int len;
5946
5947 /*
5948 * search forward for what could be the start of a file name
5949 */
5950 ptr = line + col;
5951 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005952 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 if (*ptr == NUL) /* nothing found */
5954 {
5955 if (options & FNAME_MESS)
5956 EMSG(_("E446: No file name under cursor"));
5957 return NULL;
5958 }
5959
5960 /*
5961 * Search backward for first char of the file name.
5962 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5963 */
5964 while (ptr > line)
5965 {
5966#ifdef FEAT_MBYTE
5967 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5968 ptr -= len + 1;
5969 else
5970#endif
5971 if (vim_isfilec(ptr[-1])
5972 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5973 --ptr;
5974 else
5975 break;
5976 }
5977
5978 /*
5979 * Search forward for the last char of the file name.
5980 * Also allow "://" when ':' is not in 'isfname'.
5981 */
5982 len = 0;
5983 while (vim_isfilec(ptr[len])
5984 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5985#ifdef FEAT_MBYTE
5986 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005987 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 else
5989#endif
5990 ++len;
5991
5992 /*
5993 * If there is trailing punctuation, remove it.
5994 * But don't remove "..", could be a directory name.
5995 */
5996 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5997 && ptr[len - 2] != '.')
5998 --len;
5999
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006000 if (file_lnum != NULL)
6001 {
6002 char_u *p;
6003
6004 /* Get the number after the file name and a separator character */
6005 p = ptr + len;
6006 p = skipwhite(p);
6007 if (*p != NUL)
6008 {
6009 if (!isdigit(*p))
6010 ++p; /* skip the separator */
6011 p = skipwhite(p);
6012 if (isdigit(*p))
6013 *file_lnum = (int)getdigits(&p);
6014 }
6015 }
6016
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6018}
6019
6020# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6021static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6022
6023 static char_u *
6024eval_includeexpr(ptr, len)
6025 char_u *ptr;
6026 int len;
6027{
6028 char_u *res;
6029
6030 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006031 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006032 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 set_vim_var_string(VV_FNAME, NULL, 0);
6034 return res;
6035}
6036#endif
6037
6038/*
6039 * Return the name of the file ptr[len] in 'path'.
6040 * Otherwise like file_name_at_cursor().
6041 */
6042 char_u *
6043find_file_name_in_path(ptr, len, options, count, rel_fname)
6044 char_u *ptr;
6045 int len;
6046 int options;
6047 long count;
6048 char_u *rel_fname; /* file we are searching relative to */
6049{
6050 char_u *file_name;
6051 int c;
6052# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6053 char_u *tofree = NULL;
6054
6055 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6056 {
6057 tofree = eval_includeexpr(ptr, len);
6058 if (tofree != NULL)
6059 {
6060 ptr = tofree;
6061 len = (int)STRLEN(ptr);
6062 }
6063 }
6064# endif
6065
6066 if (options & FNAME_EXP)
6067 {
6068 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6069 TRUE, rel_fname);
6070
6071# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6072 /*
6073 * If the file could not be found in a normal way, try applying
6074 * 'includeexpr' (unless done already).
6075 */
6076 if (file_name == NULL
6077 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6078 {
6079 tofree = eval_includeexpr(ptr, len);
6080 if (tofree != NULL)
6081 {
6082 ptr = tofree;
6083 len = (int)STRLEN(ptr);
6084 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6085 TRUE, rel_fname);
6086 }
6087 }
6088# endif
6089 if (file_name == NULL && (options & FNAME_MESS))
6090 {
6091 c = ptr[len];
6092 ptr[len] = NUL;
6093 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6094 ptr[len] = c;
6095 }
6096
6097 /* Repeat finding the file "count" times. This matters when it
6098 * appears several times in the path. */
6099 while (file_name != NULL && --count > 0)
6100 {
6101 vim_free(file_name);
6102 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6103 }
6104 }
6105 else
6106 file_name = vim_strnsave(ptr, len);
6107
6108# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6109 vim_free(tofree);
6110# endif
6111
6112 return file_name;
6113}
6114#endif /* FEAT_SEARCHPATH */
6115
6116/*
6117 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6118 * Also check for ":\\", which MS Internet Explorer accepts, return
6119 * URL_BACKSLASH.
6120 */
6121 static int
6122path_is_url(p)
6123 char_u *p;
6124{
6125 if (STRNCMP(p, "://", (size_t)3) == 0)
6126 return URL_SLASH;
6127 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6128 return URL_BACKSLASH;
6129 return 0;
6130}
6131
6132/*
6133 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6134 * Return URL_BACKSLASH for "name:\\".
6135 * Return zero otherwise.
6136 */
6137 int
6138path_with_url(fname)
6139 char_u *fname;
6140{
6141 char_u *p;
6142
6143 for (p = fname; isalpha(*p); ++p)
6144 ;
6145 return path_is_url(p);
6146}
6147
6148/*
6149 * Return TRUE if "name" is a full (absolute) path name or URL.
6150 */
6151 int
6152vim_isAbsName(name)
6153 char_u *name;
6154{
6155 return (path_with_url(name) != 0 || mch_isFullName(name));
6156}
6157
6158/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006159 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 *
6161 * return FAIL for failure, OK otherwise
6162 */
6163 int
6164vim_FullName(fname, buf, len, force)
6165 char_u *fname, *buf;
6166 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006167 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168{
6169 int retval = OK;
6170 int url;
6171
6172 *buf = NUL;
6173 if (fname == NULL)
6174 return FAIL;
6175
6176 url = path_with_url(fname);
6177 if (!url)
6178 retval = mch_FullName(fname, buf, len, force);
6179 if (url || retval == FAIL)
6180 {
6181 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006182 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 }
6184#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6185 slash_adjust(buf);
6186#endif
6187 return retval;
6188}
6189
6190/*
6191 * Return the minimal number of rows that is needed on the screen to display
6192 * the current number of windows.
6193 */
6194 int
6195min_rows()
6196{
6197 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006198#ifdef FEAT_WINDOWS
6199 tabpage_T *tp;
6200 int n;
6201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202
6203 if (firstwin == NULL) /* not initialized yet */
6204 return MIN_LINES;
6205
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006207 total = 0;
6208 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6209 {
6210 n = frame_minheight(tp->tp_topframe, NULL);
6211 if (total < n)
6212 total = n;
6213 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006214 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006216 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006218 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 return total;
6220}
6221
6222/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006223 * Return TRUE if there is only one window (in the current tab page), not
6224 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006225 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 */
6227 int
6228only_one_window()
6229{
6230#ifdef FEAT_WINDOWS
6231 int count = 0;
6232 win_T *wp;
6233
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006234 /* If there is another tab page there always is another window. */
6235 if (first_tabpage->tp_next != NULL)
6236 return FALSE;
6237
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006239 if ((!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240# ifdef FEAT_QUICKFIX
6241 || wp->w_p_pvw
6242# endif
6243 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006244# ifdef FEAT_AUTOCMD
6245 && wp != aucmd_win
6246# endif
6247 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 ++count;
6249 return (count <= 1);
6250#else
6251 return TRUE;
6252#endif
6253}
6254
6255#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6256/*
6257 * Correct the cursor line number in other windows. Used after changing the
6258 * current buffer, and before applying autocommands.
6259 * When "do_curwin" is TRUE, also check current window.
6260 */
6261 void
6262check_lnums(do_curwin)
6263 int do_curwin;
6264{
6265 win_T *wp;
6266
6267#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006268 tabpage_T *tp;
6269
6270 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6272#else
6273 wp = curwin;
6274 if (do_curwin)
6275#endif
6276 {
6277 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6278 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6279 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6280 wp->w_topline = curbuf->b_ml.ml_line_count;
6281 }
6282}
6283#endif
6284
6285#if defined(FEAT_WINDOWS) || defined(PROTO)
6286
6287/*
6288 * A snapshot of the window sizes, to restore them after closing the help
6289 * window.
6290 * Only these fields are used:
6291 * fr_layout
6292 * fr_width
6293 * fr_height
6294 * fr_next
6295 * fr_child
6296 * fr_win (only valid for the old curwin, NULL otherwise)
6297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298
6299/*
6300 * Create a snapshot of the current frame sizes.
6301 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006302 void
6303make_snapshot(idx)
6304 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006306 clear_snapshot(curtab, idx);
6307 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308}
6309
6310 static void
6311make_snapshot_rec(fr, frp)
6312 frame_T *fr;
6313 frame_T **frp;
6314{
6315 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6316 if (*frp == NULL)
6317 return;
6318 (*frp)->fr_layout = fr->fr_layout;
6319# ifdef FEAT_VERTSPLIT
6320 (*frp)->fr_width = fr->fr_width;
6321# endif
6322 (*frp)->fr_height = fr->fr_height;
6323 if (fr->fr_next != NULL)
6324 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6325 if (fr->fr_child != NULL)
6326 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6327 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6328 (*frp)->fr_win = curwin;
6329}
6330
6331/*
6332 * Remove any existing snapshot.
6333 */
6334 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006335clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006336 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006337 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006339 clear_snapshot_rec(tp->tp_snapshot[idx]);
6340 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341}
6342
6343 static void
6344clear_snapshot_rec(fr)
6345 frame_T *fr;
6346{
6347 if (fr != NULL)
6348 {
6349 clear_snapshot_rec(fr->fr_next);
6350 clear_snapshot_rec(fr->fr_child);
6351 vim_free(fr);
6352 }
6353}
6354
6355/*
6356 * Restore a previously created snapshot, if there is any.
6357 * This is only done if the screen size didn't change and the window layout is
6358 * still the same.
6359 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006360 void
6361restore_snapshot(idx, close_curwin)
6362 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 int close_curwin; /* closing current window */
6364{
6365 win_T *wp;
6366
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006367 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006369 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006371 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6372 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006374 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 win_comp_pos();
6376 if (wp != NULL && close_curwin)
6377 win_goto(wp);
6378 redraw_all_later(CLEAR);
6379 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006380 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381}
6382
6383/*
6384 * Check if frames "sn" and "fr" have the same layout, same following frames
6385 * and same children.
6386 */
6387 static int
6388check_snapshot_rec(sn, fr)
6389 frame_T *sn;
6390 frame_T *fr;
6391{
6392 if (sn->fr_layout != fr->fr_layout
6393 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6394 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6395 || (sn->fr_next != NULL
6396 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6397 || (sn->fr_child != NULL
6398 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6399 return FAIL;
6400 return OK;
6401}
6402
6403/*
6404 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6405 * following frames and children.
6406 * Returns a pointer to the old current window, or NULL.
6407 */
6408 static win_T *
6409restore_snapshot_rec(sn, fr)
6410 frame_T *sn;
6411 frame_T *fr;
6412{
6413 win_T *wp = NULL;
6414 win_T *wp2;
6415
6416 fr->fr_height = sn->fr_height;
6417# ifdef FEAT_VERTSPLIT
6418 fr->fr_width = sn->fr_width;
6419# endif
6420 if (fr->fr_layout == FR_LEAF)
6421 {
6422 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6423# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006424 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425# endif
6426 wp = sn->fr_win;
6427 }
6428 if (sn->fr_next != NULL)
6429 {
6430 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6431 if (wp2 != NULL)
6432 wp = wp2;
6433 }
6434 if (sn->fr_child != NULL)
6435 {
6436 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6437 if (wp2 != NULL)
6438 wp = wp2;
6439 }
6440 return wp;
6441}
6442
6443#endif
6444
6445#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6446/*
6447 * Return TRUE if there is any vertically split window.
6448 */
6449 int
6450win_hasvertsplit()
6451{
6452 frame_T *fr;
6453
6454 if (topframe->fr_layout == FR_ROW)
6455 return TRUE;
6456
6457 if (topframe->fr_layout == FR_COL)
6458 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6459 if (fr->fr_layout == FR_ROW)
6460 return TRUE;
6461
6462 return FALSE;
6463}
6464#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006465
6466#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6467/*
6468 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006469 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006470 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6471 * If no particular ID is desired, -1 must be specified for 'id'.
6472 * Return ID of added match, -1 on failure.
6473 */
6474 int
6475match_add(wp, grp, pat, prio, id)
6476 win_T *wp;
6477 char_u *grp;
6478 char_u *pat;
6479 int prio;
6480 int id;
6481{
6482 matchitem_T *cur;
6483 matchitem_T *prev;
6484 matchitem_T *m;
6485 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006486 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006487
6488 if (*grp == NUL || *pat == NUL)
6489 return -1;
6490 if (id < -1 || id == 0)
6491 {
6492 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6493 return -1;
6494 }
6495 if (id != -1)
6496 {
6497 cur = wp->w_match_head;
6498 while (cur != NULL)
6499 {
6500 if (cur->id == id)
6501 {
6502 EMSGN("E801: ID already taken: %ld", id);
6503 return -1;
6504 }
6505 cur = cur->next;
6506 }
6507 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006508 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006509 {
6510 EMSG2(_(e_nogroup), grp);
6511 return -1;
6512 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006513 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006514 {
6515 EMSG2(_(e_invarg2), pat);
6516 return -1;
6517 }
6518
6519 /* Find available match ID. */
6520 while (id == -1)
6521 {
6522 cur = wp->w_match_head;
6523 while (cur != NULL && cur->id != wp->w_next_match_id)
6524 cur = cur->next;
6525 if (cur == NULL)
6526 id = wp->w_next_match_id;
6527 wp->w_next_match_id++;
6528 }
6529
6530 /* Build new match. */
6531 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6532 m->id = id;
6533 m->priority = prio;
6534 m->pattern = vim_strsave(pat);
6535 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006536 m->match.regprog = regprog;
6537 m->match.rmm_ic = FALSE;
6538 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006539
6540 /* Insert new match. The match list is in ascending order with regard to
6541 * the match priorities. */
6542 cur = wp->w_match_head;
6543 prev = cur;
6544 while (cur != NULL && prio >= cur->priority)
6545 {
6546 prev = cur;
6547 cur = cur->next;
6548 }
6549 if (cur == prev)
6550 wp->w_match_head = m;
6551 else
6552 prev->next = m;
6553 m->next = cur;
6554
6555 redraw_later(SOME_VALID);
6556 return id;
6557}
6558
6559/*
6560 * Delete match with ID 'id' in the match list of window 'wp'.
6561 * Print error messages if 'perr' is TRUE.
6562 */
6563 int
6564match_delete(wp, id, perr)
6565 win_T *wp;
6566 int id;
6567 int perr;
6568{
6569 matchitem_T *cur = wp->w_match_head;
6570 matchitem_T *prev = cur;
6571
6572 if (id < 1)
6573 {
6574 if (perr == TRUE)
6575 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6576 id);
6577 return -1;
6578 }
6579 while (cur != NULL && cur->id != id)
6580 {
6581 prev = cur;
6582 cur = cur->next;
6583 }
6584 if (cur == NULL)
6585 {
6586 if (perr == TRUE)
6587 EMSGN("E803: ID not found: %ld", id);
6588 return -1;
6589 }
6590 if (cur == prev)
6591 wp->w_match_head = cur->next;
6592 else
6593 prev->next = cur->next;
6594 vim_free(cur->match.regprog);
6595 vim_free(cur->pattern);
6596 vim_free(cur);
6597 redraw_later(SOME_VALID);
6598 return 0;
6599}
6600
6601/*
6602 * Delete all matches in the match list of window 'wp'.
6603 */
6604 void
6605clear_matches(wp)
6606 win_T *wp;
6607{
6608 matchitem_T *m;
6609
6610 while (wp->w_match_head != NULL)
6611 {
6612 m = wp->w_match_head->next;
6613 vim_free(wp->w_match_head->match.regprog);
6614 vim_free(wp->w_match_head->pattern);
6615 vim_free(wp->w_match_head);
6616 wp->w_match_head = m;
6617 }
6618 redraw_later(SOME_VALID);
6619}
6620
6621/*
6622 * Get match from ID 'id' in window 'wp'.
6623 * Return NULL if match not found.
6624 */
6625 matchitem_T *
6626get_match(wp, id)
6627 win_T *wp;
6628 int id;
6629{
6630 matchitem_T *cur = wp->w_match_head;
6631
6632 while (cur != NULL && cur->id != id)
6633 cur = cur->next;
6634 return cur;
6635}
6636#endif