blob: 6b803f59605c096cfd92d1aee273a487d3bccad6 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#include "vim.h"
11
Bram Moolenaar071d4272004-06-13 20:20:40 +000012static int path_is_url __ARGS((char_u *p));
13#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar884ae642009-02-22 01:37:59 +000014static void win_init __ARGS((win_T *newp, win_T *oldp, int flags));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000015static void win_init_some __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
17static void frame_setheight __ARGS((frame_T *curfrp, int height));
18#ifdef FEAT_VERTSPLIT
19static void frame_setwidth __ARGS((frame_T *curfrp, int width));
20#endif
21static void win_exchange __ARGS((long));
22static void win_rotate __ARGS((int, int));
23static void win_totop __ARGS((int size, int flags));
24static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000025static int last_window __ARGS((void));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000026static int one_window __ARGS((void));
Bram Moolenaarf740b292006-02-16 22:11:02 +000027static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
Bram Moolenaarf740b292006-02-16 22:11:02 +000028static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000029static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000030static win_T *frame2win __ARGS((frame_T *frp));
31static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
32static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
33static int frame_fixed_height __ARGS((frame_T *frp));
34#ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000035static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000036static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000037static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000038static void frame_add_vsep __ARGS((frame_T *frp));
39static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
40static void frame_fix_width __ARGS((win_T *wp));
41#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000042#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000043static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000044static void new_frame __ARGS((win_T *wp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000045#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000046static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000047static int leave_tabpage __ARGS((buf_T *new_curbuf));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000048static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000049static void frame_fix_height __ARGS((win_T *wp));
50static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
51static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
Bram Moolenaarf740b292006-02-16 22:11:02 +000052static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000053static void frame_append __ARGS((frame_T *after, frame_T *frp));
54static void frame_insert __ARGS((frame_T *before, frame_T *frp));
55static void frame_remove __ARGS((frame_T *frp));
56#ifdef FEAT_VERTSPLIT
57static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static void win_goto_ver __ARGS((int up, long count));
59static void win_goto_hor __ARGS((int left, long count));
60#endif
61static void frame_add_height __ARGS((frame_T *frp, int n));
62static void last_status_rec __ARGS((frame_T *fr, int statusline));
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000065static void clear_snapshot __ARGS((tabpage_T *tp, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static void clear_snapshot_rec __ARGS((frame_T *fr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000067static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
68static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
69
70#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000071
Bram Moolenaar746ebd32009-06-16 14:01:43 +000072static win_T *win_alloc __ARGS((win_T *after, int hidden));
Bram 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 Moolenaar2a0449d2006-02-20 21:27:21 +0000686 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 * When "newwin" is not NULL: insert this window at the far
688 * top/left/right/bottom.
689 * return FAIL for failure, OK otherwise
690 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000691 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692win_split_ins(size, flags, newwin, dir)
693 int size;
694 int flags;
695 win_T *newwin;
696 int dir;
697{
698 win_T *wp = newwin;
699 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 {
721 if (oldwin->w_height <= p_wmh && newwin == NULL)
722 {
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;
754 if (available < needed && newwin == NULL)
755 {
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 }
818 if (available < needed && newwin == NULL)
819 {
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 */
891 if (newwin == 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 {
898 if (newwin == 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
904 if (newwin == NULL)
905 {
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
975 if (newwin == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000976 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 else
978 frp = newwin->w_frame;
979 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 */
2086 static int
2087one_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 Moolenaar910f66f2006-04-05 20:41:53 +00002109 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 * If "free_buf" is TRUE related buffer may be unloaded.
2111 *
2112 * called by :quit, :close, :xit, :wq and findtag()
2113 */
2114 void
2115win_close(win, free_buf)
2116 win_T *win;
2117 int free_buf;
2118{
2119 win_T *wp;
2120#ifdef FEAT_AUTOCMD
2121 int other_buffer = FALSE;
2122#endif
2123 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 int dir;
2125 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002126 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002128 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
2130 EMSG(_("E444: Cannot close last window"));
2131 return;
2132 }
2133
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002134#ifdef FEAT_AUTOCMD
2135 if (win == aucmd_win)
2136 {
2137 EMSG(_("E813: Cannot close autocmd window"));
2138 return;
2139 }
2140 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2141 {
2142 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2143 return;
2144 }
2145#endif
2146
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002147 /*
2148 * When closing the last window in a tab page first go to another tab
2149 * page and then close the window and the tab page. This avoids that
2150 * curwin and curtab are not invalid while we are freeing memory, they may
2151 * be used in GUI events.
2152 */
2153 if (firstwin == lastwin)
2154 {
2155 goto_tabpage_tp(alt_tabpage());
2156 redraw_tabline = TRUE;
2157
2158 /* Safety check: Autocommands may have closed the window when jumping
2159 * to the other tab page. */
2160 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2161 {
2162 int h = tabline_height();
2163
2164 win_close_othertab(win, free_buf, prev_curtab);
2165 if (h != tabline_height())
2166 shell_new_rows();
2167 }
2168 return;
2169 }
2170
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 /* When closing the help window, try restoring a snapshot after closing
2172 * the window. Otherwise clear the snapshot, it's now invalid. */
2173 if (win->w_buffer->b_help)
2174 help_window = TRUE;
2175 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002176 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
2178#ifdef FEAT_AUTOCMD
2179 if (win == curwin)
2180 {
2181 /*
2182 * Guess which window is going to be the new current window.
2183 * This may change because of the autocommands (sigh).
2184 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002185 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186
2187 /*
2188 * Be careful: If autocommands delete the window, return now.
2189 */
2190 if (wp->w_buffer != curbuf)
2191 {
2192 other_buffer = TRUE;
2193 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002194 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 return;
2196 }
2197 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002198 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 return;
2200# ifdef FEAT_EVAL
2201 /* autocmds may abort script processing */
2202 if (aborting())
2203 return;
2204# endif
2205 }
2206#endif
2207
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002208#ifdef FEAT_GUI
2209 /* Avoid trouble with scrollbars that are going to be deleted in
2210 * win_free(). */
2211 if (gui.in_use)
2212 out_flush();
2213#endif
2214
Bram Moolenaara971b822011-09-14 14:43:25 +02002215#ifdef FEAT_SYN_HL
2216 /* Free independent synblock before the buffer is freed. */
2217 reset_synblock(win);
2218#endif
2219
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 /*
2221 * Close the link to the buffer.
2222 */
2223 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002226 * other window or moved to another tab page. */
2227 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 return;
2229
Bram Moolenaara971b822011-09-14 14:43:25 +02002230 /* Free the memory used for the window and get the window that received
2231 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002232 wp = win_free_mem(win, &dir, NULL);
2233
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 /* Make sure curwin isn't invalid. It can cause severe trouble when
2235 * printing an error message. For win_equal() curbuf needs to be valid
2236 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002237 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 {
2239 curwin = wp;
2240#ifdef FEAT_QUICKFIX
2241 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2242 {
2243 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002244 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 * finding another window to go to.
2246 */
2247 for (;;)
2248 {
2249 if (wp->w_next == NULL)
2250 wp = firstwin;
2251 else
2252 wp = wp->w_next;
2253 if (wp == curwin)
2254 break;
2255 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2256 {
2257 curwin = wp;
2258 break;
2259 }
2260 }
2261 }
2262#endif
2263 curbuf = curwin->w_buffer;
2264 close_curwin = TRUE;
2265 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002266 if (p_ea
2267#ifdef FEAT_VERTSPLIT
2268 && (*p_ead == 'b' || *p_ead == dir)
2269#endif
2270 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 win_equal(curwin, TRUE,
2272#ifdef FEAT_VERTSPLIT
2273 dir
2274#else
2275 0
2276#endif
2277 );
2278 else
2279 win_comp_pos();
2280 if (close_curwin)
2281 {
2282 win_enter_ext(wp, FALSE, TRUE);
2283#ifdef FEAT_AUTOCMD
2284 if (other_buffer)
2285 /* careful: after this wp and win may be invalid! */
2286 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2287#endif
2288 }
2289
2290 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002291 * If last window has a status line now and we don't want one,
2292 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 */
2294 last_status(FALSE);
2295
2296 /* After closing the help window, try restoring the window layout from
2297 * before it was opened. */
2298 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002299 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300
2301#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2302 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2303 if (gui.in_use && !win_hasvertsplit())
2304 gui_init_which_components(NULL);
2305#endif
2306
2307 redraw_all_later(NOT_VALID);
2308}
2309
2310/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002311 * Close window "win" in tab page "tp", which is not the current tab page.
2312 * This may be the last window ih that tab page and result in closing the tab,
2313 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002314 * Caller must check if buffer is hidden and whether the tabline needs to be
2315 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002316 */
2317 void
2318win_close_othertab(win, free_buf, tp)
2319 win_T *win;
2320 int free_buf;
2321 tabpage_T *tp;
2322{
2323 win_T *wp;
2324 int dir;
2325 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002326 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002327
2328 /* Close the link to the buffer. */
2329 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2330
2331 /* Careful: Autocommands may have closed the tab page or made it the
2332 * current tab page. */
2333 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2334 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002335 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002336 return;
2337
2338 /* Autocommands may have closed the window already. */
2339 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2340 ;
2341 if (wp == NULL)
2342 return;
2343
Bram Moolenaarf740b292006-02-16 22:11:02 +00002344 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002345 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002346 {
2347 if (tp == first_tabpage)
2348 first_tabpage = tp->tp_next;
2349 else
2350 {
2351 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2352 ptp = ptp->tp_next)
2353 ;
2354 if (ptp == NULL)
2355 {
2356 EMSG2(_(e_intern2), "win_close_othertab()");
2357 return;
2358 }
2359 ptp->tp_next = tp->tp_next;
2360 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002361 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002362 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002363
2364 /* Free the memory used for the window. */
2365 win_free_mem(win, &dir, tp);
2366
2367 if (free_tp)
2368 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002369}
2370
2371/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002372 * Free the memory used for a window.
2373 * Returns a pointer to the window that got the freed up space.
2374 */
2375 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002376win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002377 win_T *win;
2378 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002379 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002380{
2381 frame_T *frp;
2382 win_T *wp;
2383
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002384 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002385 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002386 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002387 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002388 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002389
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002390 /* When deleting the current window of another tab page select a new
2391 * current window. */
2392 if (tp != NULL && win == tp->tp_curwin)
2393 tp->tp_curwin = wp;
2394
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002395 return wp;
2396}
2397
2398#if defined(EXITFREE) || defined(PROTO)
2399 void
2400win_free_all()
2401{
2402 int dummy;
2403
Bram Moolenaarf740b292006-02-16 22:11:02 +00002404# ifdef FEAT_WINDOWS
2405 while (first_tabpage->tp_next != NULL)
2406 tabpage_close(TRUE);
2407# endif
2408
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002409# ifdef FEAT_AUTOCMD
2410 if (aucmd_win != NULL)
2411 {
2412 (void)win_free_mem(aucmd_win, &dummy, NULL);
2413 aucmd_win = NULL;
2414 }
2415# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002416
2417 while (firstwin != NULL)
2418 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002419}
2420#endif
2421
2422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 * Remove a window and its frame from the tree of frames.
2424 * Returns a pointer to the window that got the freed up space.
2425 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002426 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002427winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002429 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002430 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431{
2432 frame_T *frp, *frp2, *frp3;
2433 frame_T *frp_close = win->w_frame;
2434 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435
2436 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002437 * If there is only one window there is nothing to remove.
2438 */
2439 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2440 return NULL;
2441
2442 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 * Remove the window from its frame.
2444 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002445 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 wp = frame2win(frp2);
2447
2448 /* Remove this frame from the list of frames. */
2449 frame_remove(frp_close);
2450
2451#ifdef FEAT_VERTSPLIT
2452 if (frp_close->fr_parent->fr_layout == FR_COL)
2453 {
2454#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002455 /* When 'winfixheight' is set, try to find another frame in the column
2456 * (as close to the closed frame as possible) to distribute the height
2457 * to. */
2458 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2459 {
2460 frp = frp_close->fr_prev;
2461 frp3 = frp_close->fr_next;
2462 while (frp != NULL || frp3 != NULL)
2463 {
2464 if (frp != NULL)
2465 {
2466 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2467 {
2468 frp2 = frp;
2469 wp = frp->fr_win;
2470 break;
2471 }
2472 frp = frp->fr_prev;
2473 }
2474 if (frp3 != NULL)
2475 {
2476 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2477 {
2478 frp2 = frp3;
2479 wp = frp3->fr_win;
2480 break;
2481 }
2482 frp3 = frp3->fr_next;
2483 }
2484 }
2485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2487 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488#ifdef FEAT_VERTSPLIT
2489 *dirp = 'v';
2490 }
2491 else
2492 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002493 /* When 'winfixwidth' is set, try to find another frame in the column
2494 * (as close to the closed frame as possible) to distribute the width
2495 * to. */
2496 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2497 {
2498 frp = frp_close->fr_prev;
2499 frp3 = frp_close->fr_next;
2500 while (frp != NULL || frp3 != NULL)
2501 {
2502 if (frp != NULL)
2503 {
2504 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2505 {
2506 frp2 = frp;
2507 wp = frp->fr_win;
2508 break;
2509 }
2510 frp = frp->fr_prev;
2511 }
2512 if (frp3 != NULL)
2513 {
2514 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2515 {
2516 frp2 = frp3;
2517 wp = frp3->fr_win;
2518 break;
2519 }
2520 frp3 = frp3->fr_next;
2521 }
2522 }
2523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002525 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 *dirp = 'h';
2527 }
2528#endif
2529
2530 /* If rows/columns go to a window below/right its positions need to be
2531 * updated. Can only be done after the sizes have been updated. */
2532 if (frp2 == frp_close->fr_next)
2533 {
2534 int row = win->w_winrow;
2535 int col = W_WINCOL(win);
2536
2537 frame_comp_pos(frp2, &row, &col);
2538 }
2539
2540 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2541 {
2542 /* There is no other frame in this list, move its info to the parent
2543 * and remove it. */
2544 frp2->fr_parent->fr_layout = frp2->fr_layout;
2545 frp2->fr_parent->fr_child = frp2->fr_child;
2546 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2547 frp->fr_parent = frp2->fr_parent;
2548 frp2->fr_parent->fr_win = frp2->fr_win;
2549 if (frp2->fr_win != NULL)
2550 frp2->fr_win->w_frame = frp2->fr_parent;
2551 frp = frp2->fr_parent;
2552 vim_free(frp2);
2553
2554 frp2 = frp->fr_parent;
2555 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2556 {
2557 /* The frame above the parent has the same layout, have to merge
2558 * the frames into this list. */
2559 if (frp2->fr_child == frp)
2560 frp2->fr_child = frp->fr_child;
2561 frp->fr_child->fr_prev = frp->fr_prev;
2562 if (frp->fr_prev != NULL)
2563 frp->fr_prev->fr_next = frp->fr_child;
2564 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2565 {
2566 frp3->fr_parent = frp2;
2567 if (frp3->fr_next == NULL)
2568 {
2569 frp3->fr_next = frp->fr_next;
2570 if (frp->fr_next != NULL)
2571 frp->fr_next->fr_prev = frp3;
2572 break;
2573 }
2574 }
2575 vim_free(frp);
2576 }
2577 }
2578
2579 return wp;
2580}
2581
2582/*
2583 * Find out which frame is going to get the freed up space when "win" is
2584 * closed.
2585 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2586 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2587 * This makes opening a window and closing it immediately keep the same window
2588 * layout.
2589 */
2590 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002591win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002593 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594{
2595 frame_T *frp;
2596 int b;
2597
Bram Moolenaarf740b292006-02-16 22:11:02 +00002598 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002599 /* Last window in this tab page, will go to next tab page. */
2600 return alt_tabpage()->tp_curwin->w_frame;
2601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 frp = win->w_frame;
2603#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002604 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 b = p_spr;
2606 else
2607#endif
2608 b = p_sb;
2609 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2610 return frp->fr_next;
2611 return frp->fr_prev;
2612}
2613
2614/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002615 * Return the tabpage that will be used if the current one is closed.
2616 */
2617 static tabpage_T *
2618alt_tabpage()
2619{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002620 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002621
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002622 /* Use the next tab page if possible. */
2623 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002624 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002625
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002626 /* Find the last but one tab page. */
2627 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2628 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002629 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002630}
2631
2632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 * Find the left-upper window in frame "frp".
2634 */
2635 static win_T *
2636frame2win(frp)
2637 frame_T *frp;
2638{
2639 while (frp->fr_win == NULL)
2640 frp = frp->fr_child;
2641 return frp->fr_win;
2642}
2643
2644/*
2645 * Return TRUE if frame "frp" contains window "wp".
2646 */
2647 static int
2648frame_has_win(frp, wp)
2649 frame_T *frp;
2650 win_T *wp;
2651{
2652 frame_T *p;
2653
2654 if (frp->fr_layout == FR_LEAF)
2655 return frp->fr_win == wp;
2656
2657 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2658 if (frame_has_win(p, wp))
2659 return TRUE;
2660 return FALSE;
2661}
2662
2663/*
2664 * Set a new height for a frame. Recursively sets the height for contained
2665 * frames and windows. Caller must take care of positions.
2666 */
2667 static void
2668frame_new_height(topfrp, height, topfirst, wfh)
2669 frame_T *topfrp;
2670 int height;
2671 int topfirst; /* resize topmost contained frame first */
2672 int wfh; /* obey 'winfixheight' when there is a choice;
2673 may cause the height not to be set */
2674{
2675 frame_T *frp;
2676 int extra_lines;
2677 int h;
2678
2679 if (topfrp->fr_win != NULL)
2680 {
2681 /* Simple case: just one window. */
2682 win_new_height(topfrp->fr_win,
2683 height - topfrp->fr_win->w_status_height);
2684 }
2685#ifdef FEAT_VERTSPLIT
2686 else if (topfrp->fr_layout == FR_ROW)
2687 {
2688 do
2689 {
2690 /* All frames in this row get the same new height. */
2691 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2692 {
2693 frame_new_height(frp, height, topfirst, wfh);
2694 if (frp->fr_height > height)
2695 {
2696 /* Could not fit the windows, make the whole row higher. */
2697 height = frp->fr_height;
2698 break;
2699 }
2700 }
2701 }
2702 while (frp != NULL);
2703 }
2704#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002705 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
2707 /* Complicated case: Resize a column of frames. Resize the bottom
2708 * frame first, frames above that when needed. */
2709
2710 frp = topfrp->fr_child;
2711 if (wfh)
2712 /* Advance past frames with one window with 'wfh' set. */
2713 while (frame_fixed_height(frp))
2714 {
2715 frp = frp->fr_next;
2716 if (frp == NULL)
2717 return; /* no frame without 'wfh', give up */
2718 }
2719 if (!topfirst)
2720 {
2721 /* Find the bottom frame of this column */
2722 while (frp->fr_next != NULL)
2723 frp = frp->fr_next;
2724 if (wfh)
2725 /* Advance back for frames with one window with 'wfh' set. */
2726 while (frame_fixed_height(frp))
2727 frp = frp->fr_prev;
2728 }
2729
2730 extra_lines = height - topfrp->fr_height;
2731 if (extra_lines < 0)
2732 {
2733 /* reduce height of contained frames, bottom or top frame first */
2734 while (frp != NULL)
2735 {
2736 h = frame_minheight(frp, NULL);
2737 if (frp->fr_height + extra_lines < h)
2738 {
2739 extra_lines += frp->fr_height - h;
2740 frame_new_height(frp, h, topfirst, wfh);
2741 }
2742 else
2743 {
2744 frame_new_height(frp, frp->fr_height + extra_lines,
2745 topfirst, wfh);
2746 break;
2747 }
2748 if (topfirst)
2749 {
2750 do
2751 frp = frp->fr_next;
2752 while (wfh && frp != NULL && frame_fixed_height(frp));
2753 }
2754 else
2755 {
2756 do
2757 frp = frp->fr_prev;
2758 while (wfh && frp != NULL && frame_fixed_height(frp));
2759 }
2760 /* Increase "height" if we could not reduce enough frames. */
2761 if (frp == NULL)
2762 height -= extra_lines;
2763 }
2764 }
2765 else if (extra_lines > 0)
2766 {
2767 /* increase height of bottom or top frame */
2768 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2769 }
2770 }
2771 topfrp->fr_height = height;
2772}
2773
2774/*
2775 * Return TRUE if height of frame "frp" should not be changed because of
2776 * the 'winfixheight' option.
2777 */
2778 static int
2779frame_fixed_height(frp)
2780 frame_T *frp;
2781{
2782 /* frame with one window: fixed height if 'winfixheight' set. */
2783 if (frp->fr_win != NULL)
2784 return frp->fr_win->w_p_wfh;
2785
2786 if (frp->fr_layout == FR_ROW)
2787 {
2788 /* The frame is fixed height if one of the frames in the row is fixed
2789 * height. */
2790 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2791 if (frame_fixed_height(frp))
2792 return TRUE;
2793 return FALSE;
2794 }
2795
2796 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2797 * frames in the row are fixed height. */
2798 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2799 if (!frame_fixed_height(frp))
2800 return FALSE;
2801 return TRUE;
2802}
2803
2804#ifdef FEAT_VERTSPLIT
2805/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002806 * Return TRUE if width of frame "frp" should not be changed because of
2807 * the 'winfixwidth' option.
2808 */
2809 static int
2810frame_fixed_width(frp)
2811 frame_T *frp;
2812{
2813 /* frame with one window: fixed width if 'winfixwidth' set. */
2814 if (frp->fr_win != NULL)
2815 return frp->fr_win->w_p_wfw;
2816
2817 if (frp->fr_layout == FR_COL)
2818 {
2819 /* The frame is fixed width if one of the frames in the row is fixed
2820 * width. */
2821 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2822 if (frame_fixed_width(frp))
2823 return TRUE;
2824 return FALSE;
2825 }
2826
2827 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2828 * frames in the row are fixed width. */
2829 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2830 if (!frame_fixed_width(frp))
2831 return FALSE;
2832 return TRUE;
2833}
2834
2835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 * Add a status line to windows at the bottom of "frp".
2837 * Note: Does not check if there is room!
2838 */
2839 static void
2840frame_add_statusline(frp)
2841 frame_T *frp;
2842{
2843 win_T *wp;
2844
2845 if (frp->fr_layout == FR_LEAF)
2846 {
2847 wp = frp->fr_win;
2848 if (wp->w_status_height == 0)
2849 {
2850 if (wp->w_height > 0) /* don't make it negative */
2851 --wp->w_height;
2852 wp->w_status_height = STATUS_HEIGHT;
2853 }
2854 }
2855 else if (frp->fr_layout == FR_ROW)
2856 {
2857 /* Handle all the frames in the row. */
2858 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2859 frame_add_statusline(frp);
2860 }
2861 else /* frp->fr_layout == FR_COL */
2862 {
2863 /* Only need to handle the last frame in the column. */
2864 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2865 ;
2866 frame_add_statusline(frp);
2867 }
2868}
2869
2870/*
2871 * Set width of a frame. Handles recursively going through contained frames.
2872 * May remove separator line for windows at the right side (for win_close()).
2873 */
2874 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002875frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 frame_T *topfrp;
2877 int width;
2878 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002879 int wfw; /* obey 'winfixwidth' when there is a choice;
2880 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
2882 frame_T *frp;
2883 int extra_cols;
2884 int w;
2885 win_T *wp;
2886
2887 if (topfrp->fr_layout == FR_LEAF)
2888 {
2889 /* Simple case: just one window. */
2890 wp = topfrp->fr_win;
2891 /* Find out if there are any windows right of this one. */
2892 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2893 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2894 break;
2895 if (frp->fr_parent == NULL)
2896 wp->w_vsep_width = 0;
2897 win_new_width(wp, width - wp->w_vsep_width);
2898 }
2899 else if (topfrp->fr_layout == FR_COL)
2900 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002901 do
2902 {
2903 /* All frames in this column get the same new width. */
2904 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2905 {
2906 frame_new_width(frp, width, leftfirst, wfw);
2907 if (frp->fr_width > width)
2908 {
2909 /* Could not fit the windows, make whole column wider. */
2910 width = frp->fr_width;
2911 break;
2912 }
2913 }
2914 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 }
2916 else /* fr_layout == FR_ROW */
2917 {
2918 /* Complicated case: Resize a row of frames. Resize the rightmost
2919 * frame first, frames left of it when needed. */
2920
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002922 if (wfw)
2923 /* Advance past frames with one window with 'wfw' set. */
2924 while (frame_fixed_width(frp))
2925 {
2926 frp = frp->fr_next;
2927 if (frp == NULL)
2928 return; /* no frame without 'wfw', give up */
2929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002931 {
2932 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 while (frp->fr_next != NULL)
2934 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002935 if (wfw)
2936 /* Advance back for frames with one window with 'wfw' set. */
2937 while (frame_fixed_width(frp))
2938 frp = frp->fr_prev;
2939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940
2941 extra_cols = width - topfrp->fr_width;
2942 if (extra_cols < 0)
2943 {
2944 /* reduce frame width, rightmost frame first */
2945 while (frp != NULL)
2946 {
2947 w = frame_minwidth(frp, NULL);
2948 if (frp->fr_width + extra_cols < w)
2949 {
2950 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002951 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 }
2953 else
2954 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002955 frame_new_width(frp, frp->fr_width + extra_cols,
2956 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 break;
2958 }
2959 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002960 {
2961 do
2962 frp = frp->fr_next;
2963 while (wfw && frp != NULL && frame_fixed_width(frp));
2964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002966 {
2967 do
2968 frp = frp->fr_prev;
2969 while (wfw && frp != NULL && frame_fixed_width(frp));
2970 }
2971 /* Increase "width" if we could not reduce enough frames. */
2972 if (frp == NULL)
2973 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 }
2975 }
2976 else if (extra_cols > 0)
2977 {
2978 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002979 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 }
2981 }
2982 topfrp->fr_width = width;
2983}
2984
2985/*
2986 * Add the vertical separator to windows at the right side of "frp".
2987 * Note: Does not check if there is room!
2988 */
2989 static void
2990frame_add_vsep(frp)
2991 frame_T *frp;
2992{
2993 win_T *wp;
2994
2995 if (frp->fr_layout == FR_LEAF)
2996 {
2997 wp = frp->fr_win;
2998 if (wp->w_vsep_width == 0)
2999 {
3000 if (wp->w_width > 0) /* don't make it negative */
3001 --wp->w_width;
3002 wp->w_vsep_width = 1;
3003 }
3004 }
3005 else if (frp->fr_layout == FR_COL)
3006 {
3007 /* Handle all the frames in the column. */
3008 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3009 frame_add_vsep(frp);
3010 }
3011 else /* frp->fr_layout == FR_ROW */
3012 {
3013 /* Only need to handle the last frame in the row. */
3014 frp = frp->fr_child;
3015 while (frp->fr_next != NULL)
3016 frp = frp->fr_next;
3017 frame_add_vsep(frp);
3018 }
3019}
3020
3021/*
3022 * Set frame width from the window it contains.
3023 */
3024 static void
3025frame_fix_width(wp)
3026 win_T *wp;
3027{
3028 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3029}
3030#endif
3031
3032/*
3033 * Set frame height from the window it contains.
3034 */
3035 static void
3036frame_fix_height(wp)
3037 win_T *wp;
3038{
3039 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3040}
3041
3042/*
3043 * Compute the minimal height for frame "topfrp".
3044 * Uses the 'winminheight' option.
3045 * When "next_curwin" isn't NULL, use p_wh for this window.
3046 * When "next_curwin" is NOWIN, don't use at least one line for the current
3047 * window.
3048 */
3049 static int
3050frame_minheight(topfrp, next_curwin)
3051 frame_T *topfrp;
3052 win_T *next_curwin;
3053{
3054 frame_T *frp;
3055 int m;
3056#ifdef FEAT_VERTSPLIT
3057 int n;
3058#endif
3059
3060 if (topfrp->fr_win != NULL)
3061 {
3062 if (topfrp->fr_win == next_curwin)
3063 m = p_wh + topfrp->fr_win->w_status_height;
3064 else
3065 {
3066 /* window: minimal height of the window plus status line */
3067 m = p_wmh + topfrp->fr_win->w_status_height;
3068 /* Current window is minimal one line high */
3069 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3070 ++m;
3071 }
3072 }
3073#ifdef FEAT_VERTSPLIT
3074 else if (topfrp->fr_layout == FR_ROW)
3075 {
3076 /* get the minimal height from each frame in this row */
3077 m = 0;
3078 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3079 {
3080 n = frame_minheight(frp, next_curwin);
3081 if (n > m)
3082 m = n;
3083 }
3084 }
3085#endif
3086 else
3087 {
3088 /* Add up the minimal heights for all frames in this column. */
3089 m = 0;
3090 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3091 m += frame_minheight(frp, next_curwin);
3092 }
3093
3094 return m;
3095}
3096
3097#ifdef FEAT_VERTSPLIT
3098/*
3099 * Compute the minimal width for frame "topfrp".
3100 * When "next_curwin" isn't NULL, use p_wiw for this window.
3101 * When "next_curwin" is NOWIN, don't use at least one column for the current
3102 * window.
3103 */
3104 static int
3105frame_minwidth(topfrp, next_curwin)
3106 frame_T *topfrp;
3107 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3108{
3109 frame_T *frp;
3110 int m, n;
3111
3112 if (topfrp->fr_win != NULL)
3113 {
3114 if (topfrp->fr_win == next_curwin)
3115 m = p_wiw + topfrp->fr_win->w_vsep_width;
3116 else
3117 {
3118 /* window: minimal width of the window plus separator column */
3119 m = p_wmw + topfrp->fr_win->w_vsep_width;
3120 /* Current window is minimal one column wide */
3121 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3122 ++m;
3123 }
3124 }
3125 else if (topfrp->fr_layout == FR_COL)
3126 {
3127 /* get the minimal width from each frame in this column */
3128 m = 0;
3129 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3130 {
3131 n = frame_minwidth(frp, next_curwin);
3132 if (n > m)
3133 m = n;
3134 }
3135 }
3136 else
3137 {
3138 /* Add up the minimal widths for all frames in this row. */
3139 m = 0;
3140 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3141 m += frame_minwidth(frp, next_curwin);
3142 }
3143
3144 return m;
3145}
3146#endif
3147
3148
3149/*
3150 * Try to close all windows except current one.
3151 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3152 * used and the buffer was modified.
3153 *
3154 * Used by ":bdel" and ":only".
3155 */
3156 void
3157close_others(message, forceit)
3158 int message;
3159 int forceit; /* always hide all other windows */
3160{
3161 win_T *wp;
3162 win_T *nextwp;
3163 int r;
3164
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003165 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 {
3167 if (message
3168#ifdef FEAT_AUTOCMD
3169 && !autocmd_busy
3170#endif
3171 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003172 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 return;
3174 }
3175
3176 /* Be very careful here: autocommands may change the window layout. */
3177 for (wp = firstwin; win_valid(wp); wp = nextwp)
3178 {
3179 nextwp = wp->w_next;
3180 if (wp != curwin) /* don't close current window */
3181 {
3182
3183 /* Check if it's allowed to abandon this window */
3184 r = can_abandon(wp->w_buffer, forceit);
3185#ifdef FEAT_AUTOCMD
3186 if (!win_valid(wp)) /* autocommands messed wp up */
3187 {
3188 nextwp = firstwin;
3189 continue;
3190 }
3191#endif
3192 if (!r)
3193 {
3194#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3195 if (message && (p_confirm || cmdmod.confirm) && p_write)
3196 {
3197 dialog_changed(wp->w_buffer, FALSE);
3198# ifdef FEAT_AUTOCMD
3199 if (!win_valid(wp)) /* autocommands messed wp up */
3200 {
3201 nextwp = firstwin;
3202 continue;
3203 }
3204# endif
3205 }
3206 if (bufIsChanged(wp->w_buffer))
3207#endif
3208 continue;
3209 }
3210 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3211 }
3212 }
3213
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003214 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 EMSG(_("E445: Other window contains changes"));
3216}
3217
3218#endif /* FEAT_WINDOWS */
3219
3220/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003221 * Init the current window "curwin".
3222 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 */
3224 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003225curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003227 win_init_empty(curwin);
3228}
3229
3230 void
3231win_init_empty(wp)
3232 win_T *wp;
3233{
3234 redraw_win_later(wp, NOT_VALID);
3235 wp->w_lines_valid = 0;
3236 wp->w_cursor.lnum = 1;
3237 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003239 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003241 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3242 wp->w_pcmark.col = 0;
3243 wp->w_prev_pcmark.lnum = 0;
3244 wp->w_prev_pcmark.col = 0;
3245 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003247 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003249 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003251 if (wp->w_p_rl)
3252 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003254 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003256#ifdef FEAT_SYN_HL
3257 wp->w_s = &wp->w_buffer->b_s;
3258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259}
3260
3261/*
3262 * Allocate the first window and put an empty buffer in it.
3263 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003264 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003266 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267win_alloc_first()
3268{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003269 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003270 return FAIL;
3271
3272#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003273 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003274 if (first_tabpage == NULL)
3275 return FAIL;
3276 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003277 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003278#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003279
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003280 return OK;
3281}
3282
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003283#if defined(FEAT_AUTOCMD) || defined(PROTO)
3284/*
3285 * Init "aucmd_win". This can only be done after the first
3286 * window is fully initialized, thus it can't be in win_alloc_first().
3287 */
3288 void
3289win_alloc_aucmd_win()
3290{
3291 aucmd_win = win_alloc(NULL, TRUE);
3292 if (aucmd_win != NULL)
3293 {
3294 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003295 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003296 new_frame(aucmd_win);
3297 }
3298}
3299#endif
3300
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003301/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003302 * Allocate the first window or the first window in a new tab page.
3303 * When "oldwin" is NULL create an empty buffer for it.
3304 * When "oldwin" is not NULL copy info from it to the new window (only with
3305 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003306 * Return FAIL when something goes wrong (out of memory).
3307 */
3308 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003309win_alloc_firstwin(oldwin)
3310 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003311{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003312 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003313 if (oldwin == NULL)
3314 {
3315 /* Very first window, need to create an empty buffer for it and
3316 * initialize from scratch. */
3317 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3318 if (curwin == NULL || curbuf == NULL)
3319 return FAIL;
3320 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003321#ifdef FEAT_SYN_HL
3322 curwin->w_s = &(curbuf->b_s);
3323#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003324 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003326 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003328 curwin_init(); /* init current window */
3329 }
3330#ifdef FEAT_WINDOWS
3331 else
3332 {
3333 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003334 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003335
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003336 /* We don't want cursor- and scroll-binding in the first window. */
3337 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003338 }
3339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003341 new_frame(curwin);
3342 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003343 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003344 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345#ifdef FEAT_VERTSPLIT
3346 topframe->fr_width = Columns;
3347#endif
3348 topframe->fr_height = Rows - p_ch;
3349 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003350
3351 return OK;
3352}
3353
3354/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003355 * Create a frame for window "wp".
3356 */
3357 static void
3358new_frame(win_T *wp)
3359{
3360 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3361
3362 wp->w_frame = frp;
3363 if (frp != NULL)
3364 {
3365 frp->fr_layout = FR_LEAF;
3366 frp->fr_win = wp;
3367 }
3368}
3369
3370/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003371 * Initialize the window and frame size to the maximum.
3372 */
3373 void
3374win_init_size()
3375{
3376 firstwin->w_height = ROWS_AVAIL;
3377 topframe->fr_height = ROWS_AVAIL;
3378#ifdef FEAT_VERTSPLIT
3379 firstwin->w_width = Columns;
3380 topframe->fr_width = Columns;
3381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382}
3383
3384#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003385
3386/*
3387 * Allocate a new tabpage_T and init the values.
3388 * Returns NULL when out of memory.
3389 */
3390 static tabpage_T *
3391alloc_tabpage()
3392{
3393 tabpage_T *tp;
3394
3395 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3396 if (tp != NULL)
3397 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003398# ifdef FEAT_GUI
3399 int i;
3400
3401 for (i = 0; i < 3; i++)
3402 tp->tp_prev_which_scrollbars[i] = -1;
3403# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003404# ifdef FEAT_DIFF
3405 tp->tp_diff_invalid = TRUE;
3406# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003407#ifdef FEAT_EVAL
3408 /* init t: variables */
3409 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3410#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003411 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003412 }
3413 return tp;
3414}
3415
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003416 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003417free_tabpage(tp)
3418 tabpage_T *tp;
3419{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003420 int idx;
3421
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003422# ifdef FEAT_DIFF
3423 diff_clear(tp);
3424# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003425 for (idx = 0; idx < SNAP_COUNT; ++idx)
3426 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003427#ifdef FEAT_EVAL
3428 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3429#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003430 vim_free(tp);
3431}
3432
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003433/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003434 * Create a new Tab page with one window.
3435 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003436 * When "after" is 0 put it just after the current Tab page.
3437 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003438 * Return FAIL or OK.
3439 */
3440 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003441win_new_tabpage(after)
3442 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003443{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003444 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003445 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003446 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003447
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003448 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003449 if (newtp == NULL)
3450 return FAIL;
3451
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003452 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003453 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003454 {
3455 vim_free(newtp);
3456 return FAIL;
3457 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003458 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003459
3460 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003461 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003462 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003463 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003464 if (after == 1)
3465 {
3466 /* New tab page becomes the first one. */
3467 newtp->tp_next = first_tabpage;
3468 first_tabpage = newtp;
3469 }
3470 else
3471 {
3472 if (after > 0)
3473 {
3474 /* Put new tab page before tab page "after". */
3475 n = 2;
3476 for (tp = first_tabpage; tp->tp_next != NULL
3477 && n < after; tp = tp->tp_next)
3478 ++n;
3479 }
3480 newtp->tp_next = tp->tp_next;
3481 tp->tp_next = newtp;
3482 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003483 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003484 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003485 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003486
3487 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003488 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003489
3490#if defined(FEAT_GUI)
3491 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3492 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003493 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003494#endif
3495
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003496 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003497#ifdef FEAT_AUTOCMD
3498 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3499 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3500#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003501 return OK;
3502 }
3503
3504 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003505 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003506 return FAIL;
3507}
3508
3509/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003510 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3511 * like with ":split".
3512 * Returns OK if a new tab page was created, FAIL otherwise.
3513 */
3514 int
3515may_open_tabpage()
3516{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003517 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003518
Bram Moolenaard326ce82007-03-11 14:48:29 +00003519 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003520 {
3521 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003522 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003523 return win_new_tabpage(n);
3524 }
3525 return FAIL;
3526}
3527
3528/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003529 * Create up to "maxcount" tabpages with empty windows.
3530 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003531 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003532 int
3533make_tabpages(maxcount)
3534 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003535{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003536 int count = maxcount;
3537 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003538
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003539 /* Limit to 'tabpagemax' tabs. */
3540 if (count > p_tpm)
3541 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003542
3543#ifdef FEAT_AUTOCMD
3544 /*
3545 * Don't execute autocommands while creating the tab pages. Must do that
3546 * when putting the buffers in the windows.
3547 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003548 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003549#endif
3550
3551 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003552 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003553 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003554
3555#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003556 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003557#endif
3558
3559 /* return actual number of tab pages */
3560 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003561}
3562
3563/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003564 * Return TRUE when "tpc" points to a valid tab page.
3565 */
3566 int
3567valid_tabpage(tpc)
3568 tabpage_T *tpc;
3569{
3570 tabpage_T *tp;
3571
3572 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3573 if (tp == tpc)
3574 return TRUE;
3575 return FALSE;
3576}
3577
3578/*
3579 * Find tab page "n" (first one is 1). Returns NULL when not found.
3580 */
3581 tabpage_T *
3582find_tabpage(n)
3583 int n;
3584{
3585 tabpage_T *tp;
3586 int i = 1;
3587
3588 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3589 ++i;
3590 return tp;
3591}
3592
3593/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003594 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003595 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003596 */
3597 int
3598tabpage_index(ftp)
3599 tabpage_T *ftp;
3600{
3601 int i = 1;
3602 tabpage_T *tp;
3603
3604 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3605 ++i;
3606 return i;
3607}
3608
3609/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003610 * Prepare for leaving the current tab page.
3611 * When autocomands change "curtab" we don't leave the tab page and return
3612 * FAIL.
3613 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003614 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003615 static int
3616leave_tabpage(new_curbuf)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003617 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003618 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003619{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003620 tabpage_T *tp = curtab;
3621
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003622#ifdef FEAT_VISUAL
3623 reset_VIsual_and_resel(); /* stop Visual mode */
3624#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003625#ifdef FEAT_AUTOCMD
3626 if (new_curbuf != curbuf)
3627 {
3628 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3629 if (curtab != tp)
3630 return FAIL;
3631 }
3632 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3633 if (curtab != tp)
3634 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003635 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003636 if (curtab != tp)
3637 return FAIL;
3638#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003639#if defined(FEAT_GUI)
3640 /* Remove the scrollbars. They may be added back later. */
3641 if (gui.in_use)
3642 gui_remove_scrollbars();
3643#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003644 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003645 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003646 tp->tp_firstwin = firstwin;
3647 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003648 tp->tp_old_Rows = Rows;
3649 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003650 firstwin = NULL;
3651 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003652 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003653}
3654
3655/*
3656 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003657 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003658 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003659 static void
3660enter_tabpage(tp, old_curbuf)
3661 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003662 buf_T *old_curbuf UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003663{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003664 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003665 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003666
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003667 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003668 firstwin = tp->tp_firstwin;
3669 lastwin = tp->tp_lastwin;
3670 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003671
3672 /* We would like doing the TabEnter event first, but we don't have a
3673 * valid current window yet, which may break some commands.
3674 * This triggers autocommands, thus may make "tp" invalid. */
3675 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3676 prevwin = next_prevwin;
3677
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003678#ifdef FEAT_AUTOCMD
3679 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003680
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003681 if (old_curbuf != curbuf)
3682 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3683#endif
3684
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003685 last_status(FALSE); /* status line may appear or disappear */
3686 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003687 must_redraw = CLEAR; /* need to redraw everything */
3688#ifdef FEAT_DIFF
3689 diff_need_scrollbind = TRUE;
3690#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003691
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003692 /* The tabpage line may have appeared or disappeared, may need to resize
3693 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003694 * frame sizes too. Use the stored value of p_ch, so that it can be
3695 * different for each tab page. */
3696 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003697 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3698#ifdef FEAT_GUI_TABLINE
3699 && !gui_use_tabline()
3700#endif
3701 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003702 shell_new_rows();
3703#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003704 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003705 shell_new_columns(); /* update window widths */
3706#endif
3707
3708#if defined(FEAT_GUI)
3709 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3710 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003711 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003712#endif
3713
3714 redraw_all_later(CLEAR);
3715}
3716
3717/*
3718 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003719 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003720 */
3721 void
3722goto_tabpage(n)
3723 int n;
3724{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003725 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003726 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003727 int i;
3728
Bram Moolenaard68071d2006-05-02 22:08:30 +00003729 if (text_locked())
3730 {
3731 /* Not allowed when editing the command line. */
3732#ifdef FEAT_CMDWIN
3733 if (cmdwin_type != 0)
3734 EMSG(_(e_cmdwin));
3735 else
3736#endif
3737 EMSG(_(e_secure));
3738 return;
3739 }
3740
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003741 /* If there is only one it can't work. */
3742 if (first_tabpage->tp_next == NULL)
3743 {
3744 if (n > 1)
3745 beep_flush();
3746 return;
3747 }
3748
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003749 if (n == 0)
3750 {
3751 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003752 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003753 tp = first_tabpage;
3754 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003755 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003756 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003757 else if (n < 0)
3758 {
3759 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3760 * this N times. */
3761 ttp = curtab;
3762 for (i = n; i < 0; ++i)
3763 {
3764 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3765 tp = tp->tp_next)
3766 ;
3767 ttp = tp;
3768 }
3769 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003770 else if (n == 9999)
3771 {
3772 /* Go to last tab page. */
3773 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3774 ;
3775 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003776 else
3777 {
3778 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003779 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003780 if (tp == NULL)
3781 {
3782 beep_flush();
3783 return;
3784 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003785 }
3786
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003787 goto_tabpage_tp(tp);
3788
3789#ifdef FEAT_GUI_TABLINE
3790 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003791 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003792#endif
3793}
3794
3795/*
3796 * Go to tabpage "tp".
3797 * Note: doesn't update the GUI tab.
3798 */
3799 void
3800goto_tabpage_tp(tp)
3801 tabpage_T *tp;
3802{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003803 /* Don't repeat a message in another tab page. */
3804 set_keep_msg(NULL, 0);
3805
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003806 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003807 {
3808 if (valid_tabpage(tp))
3809 enter_tabpage(tp, curbuf);
3810 else
3811 enter_tabpage(curtab, curbuf);
3812 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003813}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
3815/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003816 * Enter window "wp" in tab page "tp".
3817 * Also updates the GUI tab.
3818 */
3819 void
3820goto_tabpage_win(tp, wp)
3821 tabpage_T *tp;
3822 win_T *wp;
3823{
3824 goto_tabpage_tp(tp);
3825 if (curtab == tp && win_valid(wp))
3826 {
3827 win_enter(wp, TRUE);
3828# ifdef FEAT_GUI_TABLINE
3829 if (gui_use_tabline())
3830 gui_mch_set_curtab(tabpage_index(curtab));
3831# endif
3832 }
3833}
3834
3835/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003836 * Move the current tab page to before tab page "nr".
3837 */
3838 void
3839tabpage_move(nr)
3840 int nr;
3841{
3842 int n = nr;
3843 tabpage_T *tp;
3844
3845 if (first_tabpage->tp_next == NULL)
3846 return;
3847
3848 /* Remove the current tab page from the list of tab pages. */
3849 if (curtab == first_tabpage)
3850 first_tabpage = curtab->tp_next;
3851 else
3852 {
3853 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3854 if (tp->tp_next == curtab)
3855 break;
3856 if (tp == NULL) /* "cannot happen" */
3857 return;
3858 tp->tp_next = curtab->tp_next;
3859 }
3860
3861 /* Re-insert it at the specified position. */
3862 if (n == 0)
3863 {
3864 curtab->tp_next = first_tabpage;
3865 first_tabpage = curtab;
3866 }
3867 else
3868 {
3869 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3870 --n;
3871 curtab->tp_next = tp->tp_next;
3872 tp->tp_next = curtab;
3873 }
3874
3875 /* Need to redraw the tabline. Tab page contents doesn't change. */
3876 redraw_tabline = TRUE;
3877}
3878
3879
3880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 * Go to another window.
3882 * When jumping to another buffer, stop Visual mode. Do this before
3883 * changing windows so we can yank the selection into the '*' register.
3884 * When jumping to another window on the same buffer, adjust its cursor
3885 * position to keep the same Visual area.
3886 */
3887 void
3888win_goto(wp)
3889 win_T *wp;
3890{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02003891#ifdef FEAT_CONCEAL
3892 win_T *owp = curwin;
3893#endif
3894
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003895 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
3897 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003898 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 return;
3900 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003901#ifdef FEAT_AUTOCMD
3902 if (curbuf_locked())
3903 return;
3904#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003905
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906#ifdef FEAT_VISUAL
3907 if (wp->w_buffer != curbuf)
3908 reset_VIsual_and_resel();
3909 else if (VIsual_active)
3910 wp->w_cursor = curwin->w_cursor;
3911#endif
3912
3913#ifdef FEAT_GUI
3914 need_mouse_correct = TRUE;
3915#endif
3916 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02003917
3918#ifdef FEAT_CONCEAL
3919 /* Conceal cursor line in previous window, unconceal in current window. */
3920 if (win_valid(owp))
3921 update_single_line(owp, owp->w_cursor.lnum);
3922 update_single_line(curwin, curwin->w_cursor.lnum);
3923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924}
3925
3926#if defined(FEAT_PERL) || defined(PROTO)
3927/*
3928 * Find window number "winnr" (counting top to bottom).
3929 */
3930 win_T *
3931win_find_nr(winnr)
3932 int winnr;
3933{
3934 win_T *wp;
3935
3936# ifdef FEAT_WINDOWS
3937 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3938 if (--winnr == 0)
3939 break;
3940 return wp;
3941# else
3942 return curwin;
3943# endif
3944}
3945#endif
3946
3947#ifdef FEAT_VERTSPLIT
3948/*
3949 * Move to window above or below "count" times.
3950 */
3951 static void
3952win_goto_ver(up, count)
3953 int up; /* TRUE to go to win above */
3954 long count;
3955{
3956 frame_T *fr;
3957 frame_T *nfr;
3958 frame_T *foundfr;
3959
3960 foundfr = curwin->w_frame;
3961 while (count--)
3962 {
3963 /*
3964 * First go upwards in the tree of frames until we find a upwards or
3965 * downwards neighbor.
3966 */
3967 fr = foundfr;
3968 for (;;)
3969 {
3970 if (fr == topframe)
3971 goto end;
3972 if (up)
3973 nfr = fr->fr_prev;
3974 else
3975 nfr = fr->fr_next;
3976 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3977 break;
3978 fr = fr->fr_parent;
3979 }
3980
3981 /*
3982 * Now go downwards to find the bottom or top frame in it.
3983 */
3984 for (;;)
3985 {
3986 if (nfr->fr_layout == FR_LEAF)
3987 {
3988 foundfr = nfr;
3989 break;
3990 }
3991 fr = nfr->fr_child;
3992 if (nfr->fr_layout == FR_ROW)
3993 {
3994 /* Find the frame at the cursor row. */
3995 while (fr->fr_next != NULL
3996 && frame2win(fr)->w_wincol + fr->fr_width
3997 <= curwin->w_wincol + curwin->w_wcol)
3998 fr = fr->fr_next;
3999 }
4000 if (nfr->fr_layout == FR_COL && up)
4001 while (fr->fr_next != NULL)
4002 fr = fr->fr_next;
4003 nfr = fr;
4004 }
4005 }
4006end:
4007 if (foundfr != NULL)
4008 win_goto(foundfr->fr_win);
4009}
4010
4011/*
4012 * Move to left or right window.
4013 */
4014 static void
4015win_goto_hor(left, count)
4016 int left; /* TRUE to go to left win */
4017 long count;
4018{
4019 frame_T *fr;
4020 frame_T *nfr;
4021 frame_T *foundfr;
4022
4023 foundfr = curwin->w_frame;
4024 while (count--)
4025 {
4026 /*
4027 * First go upwards in the tree of frames until we find a left or
4028 * right neighbor.
4029 */
4030 fr = foundfr;
4031 for (;;)
4032 {
4033 if (fr == topframe)
4034 goto end;
4035 if (left)
4036 nfr = fr->fr_prev;
4037 else
4038 nfr = fr->fr_next;
4039 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4040 break;
4041 fr = fr->fr_parent;
4042 }
4043
4044 /*
4045 * Now go downwards to find the leftmost or rightmost frame in it.
4046 */
4047 for (;;)
4048 {
4049 if (nfr->fr_layout == FR_LEAF)
4050 {
4051 foundfr = nfr;
4052 break;
4053 }
4054 fr = nfr->fr_child;
4055 if (nfr->fr_layout == FR_COL)
4056 {
4057 /* Find the frame at the cursor row. */
4058 while (fr->fr_next != NULL
4059 && frame2win(fr)->w_winrow + fr->fr_height
4060 <= curwin->w_winrow + curwin->w_wrow)
4061 fr = fr->fr_next;
4062 }
4063 if (nfr->fr_layout == FR_ROW && left)
4064 while (fr->fr_next != NULL)
4065 fr = fr->fr_next;
4066 nfr = fr;
4067 }
4068 }
4069end:
4070 if (foundfr != NULL)
4071 win_goto(foundfr->fr_win);
4072}
4073#endif
4074
4075/*
4076 * Make window "wp" the current window.
4077 */
4078 void
4079win_enter(wp, undo_sync)
4080 win_T *wp;
4081 int undo_sync;
4082{
4083 win_enter_ext(wp, undo_sync, FALSE);
4084}
4085
4086/*
4087 * Make window wp the current window.
4088 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4089 * been closed and isn't valid.
4090 */
4091 static void
4092win_enter_ext(wp, undo_sync, curwin_invalid)
4093 win_T *wp;
4094 int undo_sync;
4095 int curwin_invalid;
4096{
4097#ifdef FEAT_AUTOCMD
4098 int other_buffer = FALSE;
4099#endif
4100
4101 if (wp == curwin && !curwin_invalid) /* nothing to do */
4102 return;
4103
4104#ifdef FEAT_AUTOCMD
4105 if (!curwin_invalid)
4106 {
4107 /*
4108 * Be careful: If autocommands delete the window, return now.
4109 */
4110 if (wp->w_buffer != curbuf)
4111 {
4112 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4113 other_buffer = TRUE;
4114 if (!win_valid(wp))
4115 return;
4116 }
4117 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4118 if (!win_valid(wp))
4119 return;
4120# ifdef FEAT_EVAL
4121 /* autocmds may abort script processing */
4122 if (aborting())
4123 return;
4124# endif
4125 }
4126#endif
4127
4128 /* sync undo before leaving the current buffer */
4129 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004130 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 /* may have to copy the buffer options when 'cpo' contains 'S' */
4132 if (wp->w_buffer != curbuf)
4133 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4134 if (!curwin_invalid)
4135 {
4136 prevwin = curwin; /* remember for CTRL-W p */
4137 curwin->w_redr_status = TRUE;
4138 }
4139 curwin = wp;
4140 curbuf = wp->w_buffer;
4141 check_cursor();
4142#ifdef FEAT_VIRTUALEDIT
4143 if (!virtual_active())
4144 curwin->w_cursor.coladd = 0;
4145#endif
4146 changed_line_abv_curs(); /* assume cursor position needs updating */
4147
4148 if (curwin->w_localdir != NULL)
4149 {
4150 /* Window has a local directory: Save current directory as global
4151 * directory (unless that was done already) and change to the local
4152 * directory. */
4153 if (globaldir == NULL)
4154 {
4155 char_u cwd[MAXPATHL];
4156
4157 if (mch_dirname(cwd, MAXPATHL) == OK)
4158 globaldir = vim_strsave(cwd);
4159 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004160 if (mch_chdir((char *)curwin->w_localdir) == 0)
4161 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 }
4163 else if (globaldir != NULL)
4164 {
4165 /* Window doesn't have a local directory and we are not in the global
4166 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004167 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 vim_free(globaldir);
4169 globaldir = NULL;
4170 shorten_fnames(TRUE);
4171 }
4172
4173#ifdef FEAT_AUTOCMD
4174 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4175 if (other_buffer)
4176 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4177#endif
4178
4179#ifdef FEAT_TITLE
4180 maketitle();
4181#endif
4182 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004183 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 if (restart_edit)
4185 redraw_later(VALID); /* causes status line redraw */
4186
4187 /* set window height to desired minimal value */
4188 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4189 win_setheight((int)p_wh);
4190 else if (curwin->w_height == 0)
4191 win_setheight(1);
4192
4193#ifdef FEAT_VERTSPLIT
4194 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004195 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 win_setwidth((int)p_wiw);
4197#endif
4198
4199#ifdef FEAT_MOUSE
4200 setmouse(); /* in case jumped to/from help buffer */
4201#endif
4202
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004203 /* Change directories when the 'acd' option is set. */
4204 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205}
4206
4207#endif /* FEAT_WINDOWS */
4208
4209#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4210/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004211 * Jump to the first open window that contains buffer "buf", if one exists.
4212 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 */
4214 win_T *
4215buf_jump_open_win(buf)
4216 buf_T *buf;
4217{
4218# ifdef FEAT_WINDOWS
4219 win_T *wp;
4220
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004221 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 if (wp->w_buffer == buf)
4223 break;
4224 if (wp != NULL)
4225 win_enter(wp, FALSE);
4226 return wp;
4227# else
4228 if (curwin->w_buffer == buf)
4229 return curwin;
4230 return NULL;
4231# endif
4232}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004233
4234/*
4235 * Jump to the first open window in any tab page that contains buffer "buf",
4236 * if one exists.
4237 * Returns a pointer to the window found, otherwise NULL.
4238 */
4239 win_T *
4240buf_jump_open_tab(buf)
4241 buf_T *buf;
4242{
4243# ifdef FEAT_WINDOWS
4244 win_T *wp;
4245 tabpage_T *tp;
4246
4247 /* First try the current tab page. */
4248 wp = buf_jump_open_win(buf);
4249 if (wp != NULL)
4250 return wp;
4251
4252 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4253 if (tp != curtab)
4254 {
4255 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4256 if (wp->w_buffer == buf)
4257 break;
4258 if (wp != NULL)
4259 {
4260 goto_tabpage_win(tp, wp);
4261 if (curwin != wp)
4262 wp = NULL; /* something went wrong */
4263 break;
4264 }
4265 }
4266
4267 return wp;
4268# else
4269 if (curwin->w_buffer == buf)
4270 return curwin;
4271 return NULL;
4272# endif
4273}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274#endif
4275
4276/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004277 * Allocate a window structure and link it in the window list when "hidden" is
4278 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004281win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004282 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004283 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284{
4285 win_T *newwin;
4286
4287 /*
4288 * allocate window structure and linesizes arrays
4289 */
4290 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4291 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4292 {
4293 vim_free(newwin);
4294 newwin = NULL;
4295 }
4296
4297 if (newwin != NULL)
4298 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004299#ifdef FEAT_AUTOCMD
4300 /* Don't execute autocommands while the window is not properly
4301 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4302 * event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004303 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 /*
4306 * link the window in the window list
4307 */
4308#ifdef FEAT_WINDOWS
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004309 if (!hidden)
4310 win_append(after, newwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311#endif
4312#ifdef FEAT_VERTSPLIT
4313 newwin->w_wincol = 0;
4314 newwin->w_width = Columns;
4315#endif
4316
4317 /* position the display and the cursor at the top of the file. */
4318 newwin->w_topline = 1;
4319#ifdef FEAT_DIFF
4320 newwin->w_topfill = 0;
4321#endif
4322 newwin->w_botline = 2;
4323 newwin->w_cursor.lnum = 1;
4324#ifdef FEAT_SCROLLBIND
4325 newwin->w_scbind_pos = 1;
4326#endif
4327
4328 /* We won't calculate w_fraction until resizing the window */
4329 newwin->w_fraction = 0;
4330 newwin->w_prev_fraction_row = -1;
4331
4332#ifdef FEAT_GUI
4333 if (gui.in_use)
4334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4336 SBAR_LEFT, newwin);
4337 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4338 SBAR_RIGHT, newwin);
4339 }
4340#endif
4341#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004342 /* init w: variables */
4343 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344#endif
4345#ifdef FEAT_FOLDING
4346 foldInitWin(newwin);
4347#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004348#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004349 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004350#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004351#ifdef FEAT_SEARCH_EXTRA
4352 newwin->w_match_head = NULL;
4353 newwin->w_next_match_id = 4;
4354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
4356 return newwin;
4357}
4358
4359#if defined(FEAT_WINDOWS) || defined(PROTO)
4360
4361/*
4362 * remove window 'wp' from the window list and free the structure
4363 */
4364 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004365win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004367 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368{
4369 int i;
4370
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004371#ifdef FEAT_FOLDING
4372 clearFolding(wp);
4373#endif
4374
4375 /* reduce the reference count to the argument list. */
4376 alist_unlink(wp->w_alist);
4377
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004378#ifdef FEAT_AUTOCMD
4379 /* Don't execute autocommands while the window is halfway being deleted.
4380 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004381 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004382#endif
4383
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004384#ifdef FEAT_LUA
4385 lua_window_free(wp);
4386#endif
4387
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004388#ifdef FEAT_MZSCHEME
4389 mzscheme_window_free(wp);
4390#endif
4391
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392#ifdef FEAT_PERL
4393 perl_win_free(wp);
4394#endif
4395
4396#ifdef FEAT_PYTHON
4397 python_window_free(wp);
4398#endif
4399
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004400#ifdef FEAT_PYTHON3
4401 python3_window_free(wp);
4402#endif
4403
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404#ifdef FEAT_TCL
4405 tcl_window_free(wp);
4406#endif
4407
4408#ifdef FEAT_RUBY
4409 ruby_window_free(wp);
4410#endif
4411
4412 clear_winopt(&wp->w_onebuf_opt);
4413 clear_winopt(&wp->w_allbuf_opt);
4414
4415#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004416 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417#endif
4418
4419 if (prevwin == wp)
4420 prevwin = NULL;
4421 win_free_lsize(wp);
4422
4423 for (i = 0; i < wp->w_tagstacklen; ++i)
4424 vim_free(wp->w_tagstack[i].tagname);
4425
4426 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004427
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004429 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004431
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432#ifdef FEAT_JUMPLIST
4433 free_jumplist(wp);
4434#endif
4435
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004436#ifdef FEAT_QUICKFIX
4437 qf_free_all(wp);
4438#endif
4439
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440#ifdef FEAT_GUI
4441 if (gui.in_use)
4442 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4444 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4445 }
4446#endif /* FEAT_GUI */
4447
Bram Moolenaar860cae12010-06-05 23:22:07 +02004448#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004449 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004450#endif
4451
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004452#ifdef FEAT_AUTOCMD
4453 if (wp != aucmd_win)
4454#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004455 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004457
4458#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004459 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461}
4462
4463/*
4464 * Append window "wp" in the window list after window "after".
4465 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004466 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467win_append(after, wp)
4468 win_T *after, *wp;
4469{
4470 win_T *before;
4471
4472 if (after == NULL) /* after NULL is in front of the first */
4473 before = firstwin;
4474 else
4475 before = after->w_next;
4476
4477 wp->w_next = before;
4478 wp->w_prev = after;
4479 if (after == NULL)
4480 firstwin = wp;
4481 else
4482 after->w_next = wp;
4483 if (before == NULL)
4484 lastwin = wp;
4485 else
4486 before->w_prev = wp;
4487}
4488
4489/*
4490 * Remove a window from the window list.
4491 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004492 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004493win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004495 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496{
4497 if (wp->w_prev != NULL)
4498 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004499 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004501 else
4502 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 if (wp->w_next != NULL)
4504 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004505 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004507 else
4508 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509}
4510
4511/*
4512 * Append frame "frp" in a frame list after frame "after".
4513 */
4514 static void
4515frame_append(after, frp)
4516 frame_T *after, *frp;
4517{
4518 frp->fr_next = after->fr_next;
4519 after->fr_next = frp;
4520 if (frp->fr_next != NULL)
4521 frp->fr_next->fr_prev = frp;
4522 frp->fr_prev = after;
4523}
4524
4525/*
4526 * Insert frame "frp" in a frame list before frame "before".
4527 */
4528 static void
4529frame_insert(before, frp)
4530 frame_T *before, *frp;
4531{
4532 frp->fr_next = before;
4533 frp->fr_prev = before->fr_prev;
4534 before->fr_prev = frp;
4535 if (frp->fr_prev != NULL)
4536 frp->fr_prev->fr_next = frp;
4537 else
4538 frp->fr_parent->fr_child = frp;
4539}
4540
4541/*
4542 * Remove a frame from a frame list.
4543 */
4544 static void
4545frame_remove(frp)
4546 frame_T *frp;
4547{
4548 if (frp->fr_prev != NULL)
4549 frp->fr_prev->fr_next = frp->fr_next;
4550 else
4551 frp->fr_parent->fr_child = frp->fr_next;
4552 if (frp->fr_next != NULL)
4553 frp->fr_next->fr_prev = frp->fr_prev;
4554}
4555
4556#endif /* FEAT_WINDOWS */
4557
4558/*
4559 * Allocate w_lines[] for window "wp".
4560 * Return FAIL for failure, OK for success.
4561 */
4562 int
4563win_alloc_lines(wp)
4564 win_T *wp;
4565{
4566 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004567 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 if (wp->w_lines == NULL)
4569 return FAIL;
4570 return OK;
4571}
4572
4573/*
4574 * free lsize arrays for a window
4575 */
4576 void
4577win_free_lsize(wp)
4578 win_T *wp;
4579{
4580 vim_free(wp->w_lines);
4581 wp->w_lines = NULL;
4582}
4583
4584/*
4585 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004586 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 */
4588 void
4589shell_new_rows()
4590{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004591 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
4593 if (firstwin == NULL) /* not initialized yet */
4594 return;
4595#ifdef FEAT_WINDOWS
4596 if (h < frame_minheight(topframe, NULL))
4597 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004598
4599 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 * that doesn't result in the right height, forget about that option. */
4601 frame_new_height(topframe, h, FALSE, TRUE);
4602 if (topframe->fr_height != h)
4603 frame_new_height(topframe, h, FALSE, FALSE);
4604
4605 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4606#else
4607 if (h < 1)
4608 h = 1;
4609 win_new_height(firstwin, h);
4610#endif
4611 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004612#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004613 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004614#endif
4615
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616#if 0
4617 /* Disabled: don't want making the screen smaller make a window larger. */
4618 if (p_ea)
4619 win_equal(curwin, FALSE, 'v');
4620#endif
4621}
4622
4623#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4624/*
4625 * Called from win_new_shellsize() after Columns changed.
4626 */
4627 void
4628shell_new_columns()
4629{
4630 if (firstwin == NULL) /* not initialized yet */
4631 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004632
4633 /* First try setting the widths of windows with 'winfixwidth'. If that
4634 * doesn't result in the right width, forget about that option. */
4635 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4636 if (topframe->fr_width != Columns)
4637 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4640#if 0
4641 /* Disabled: don't want making the screen smaller make a window larger. */
4642 if (p_ea)
4643 win_equal(curwin, FALSE, 'h');
4644#endif
4645}
4646#endif
4647
4648#if defined(FEAT_CMDWIN) || defined(PROTO)
4649/*
4650 * Save the size of all windows in "gap".
4651 */
4652 void
4653win_size_save(gap)
4654 garray_T *gap;
4655
4656{
4657 win_T *wp;
4658
4659 ga_init2(gap, (int)sizeof(int), 1);
4660 if (ga_grow(gap, win_count() * 2) == OK)
4661 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4662 {
4663 ((int *)gap->ga_data)[gap->ga_len++] =
4664 wp->w_width + wp->w_vsep_width;
4665 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4666 }
4667}
4668
4669/*
4670 * Restore window sizes, but only if the number of windows is still the same.
4671 * Does not free the growarray.
4672 */
4673 void
4674win_size_restore(gap)
4675 garray_T *gap;
4676{
4677 win_T *wp;
4678 int i;
4679
4680 if (win_count() * 2 == gap->ga_len)
4681 {
4682 i = 0;
4683 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4684 {
4685 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4686 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4687 }
4688 /* recompute the window positions */
4689 (void)win_comp_pos();
4690 }
4691}
4692#endif /* FEAT_CMDWIN */
4693
4694#if defined(FEAT_WINDOWS) || defined(PROTO)
4695/*
4696 * Update the position for all windows, using the width and height of the
4697 * frames.
4698 * Returns the row just after the last window.
4699 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004700 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701win_comp_pos()
4702{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004703 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 int col = 0;
4705
4706 frame_comp_pos(topframe, &row, &col);
4707 return row;
4708}
4709
4710/*
4711 * Update the position of the windows in frame "topfrp", using the width and
4712 * height of the frames.
4713 * "*row" and "*col" are the top-left position of the frame. They are updated
4714 * to the bottom-right position plus one.
4715 */
4716 static void
4717frame_comp_pos(topfrp, row, col)
4718 frame_T *topfrp;
4719 int *row;
4720 int *col;
4721{
4722 win_T *wp;
4723 frame_T *frp;
4724#ifdef FEAT_VERTSPLIT
4725 int startcol;
4726 int startrow;
4727#endif
4728
4729 wp = topfrp->fr_win;
4730 if (wp != NULL)
4731 {
4732 if (wp->w_winrow != *row
4733#ifdef FEAT_VERTSPLIT
4734 || wp->w_wincol != *col
4735#endif
4736 )
4737 {
4738 /* position changed, redraw */
4739 wp->w_winrow = *row;
4740#ifdef FEAT_VERTSPLIT
4741 wp->w_wincol = *col;
4742#endif
4743 redraw_win_later(wp, NOT_VALID);
4744 wp->w_redr_status = TRUE;
4745 }
4746 *row += wp->w_height + wp->w_status_height;
4747#ifdef FEAT_VERTSPLIT
4748 *col += wp->w_width + wp->w_vsep_width;
4749#endif
4750 }
4751 else
4752 {
4753#ifdef FEAT_VERTSPLIT
4754 startrow = *row;
4755 startcol = *col;
4756#endif
4757 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4758 {
4759#ifdef FEAT_VERTSPLIT
4760 if (topfrp->fr_layout == FR_ROW)
4761 *row = startrow; /* all frames are at the same row */
4762 else
4763 *col = startcol; /* all frames are at the same col */
4764#endif
4765 frame_comp_pos(frp, row, col);
4766 }
4767 }
4768}
4769
4770#endif /* FEAT_WINDOWS */
4771
4772/*
4773 * Set current window height and take care of repositioning other windows to
4774 * fit around it.
4775 */
4776 void
4777win_setheight(height)
4778 int height;
4779{
4780 win_setheight_win(height, curwin);
4781}
4782
4783/*
4784 * Set the window height of window "win" and take care of repositioning other
4785 * windows to fit around it.
4786 */
4787 void
4788win_setheight_win(height, win)
4789 int height;
4790 win_T *win;
4791{
4792 int row;
4793
4794 if (win == curwin)
4795 {
4796 /* Always keep current window at least one line high, even when
4797 * 'winminheight' is zero. */
4798#ifdef FEAT_WINDOWS
4799 if (height < p_wmh)
4800 height = p_wmh;
4801#endif
4802 if (height == 0)
4803 height = 1;
4804 }
4805
4806#ifdef FEAT_WINDOWS
4807 frame_setheight(win->w_frame, height + win->w_status_height);
4808
4809 /* recompute the window positions */
4810 row = win_comp_pos();
4811#else
4812 if (height > topframe->fr_height)
4813 height = topframe->fr_height;
4814 win->w_height = height;
4815 row = height;
4816#endif
4817
4818 /*
4819 * If there is extra space created between the last window and the command
4820 * line, clear it.
4821 */
4822 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4823 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4824 cmdline_row = row;
4825 msg_row = row;
4826 msg_col = 0;
4827
4828 redraw_all_later(NOT_VALID);
4829}
4830
4831#if defined(FEAT_WINDOWS) || defined(PROTO)
4832
4833/*
4834 * Set the height of a frame to "height" and take care that all frames and
4835 * windows inside it are resized. Also resize frames on the left and right if
4836 * the are in the same FR_ROW frame.
4837 *
4838 * Strategy:
4839 * If the frame is part of a FR_COL frame, try fitting the frame in that
4840 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4841 * go to containing frames to resize them and make room.
4842 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4843 * Check for the minimal height of the FR_ROW frame.
4844 * At the top level we can also use change the command line height.
4845 */
4846 static void
4847frame_setheight(curfrp, height)
4848 frame_T *curfrp;
4849 int height;
4850{
4851 int room; /* total number of lines available */
4852 int take; /* number of lines taken from other windows */
4853 int room_cmdline; /* lines available from cmdline */
4854 int run;
4855 frame_T *frp;
4856 int h;
4857 int room_reserved;
4858
4859 /* If the height already is the desired value, nothing to do. */
4860 if (curfrp->fr_height == height)
4861 return;
4862
4863 if (curfrp->fr_parent == NULL)
4864 {
4865 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004866 if (height > ROWS_AVAIL)
4867 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 if (height > 0)
4869 frame_new_height(curfrp, height, FALSE, FALSE);
4870 }
4871 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4872 {
4873 /* Row of frames: Also need to resize frames left and right of this
4874 * one. First check for the minimal height of these. */
4875 h = frame_minheight(curfrp->fr_parent, NULL);
4876 if (height < h)
4877 height = h;
4878 frame_setheight(curfrp->fr_parent, height);
4879 }
4880 else
4881 {
4882 /*
4883 * Column of frames: try to change only frames in this column.
4884 */
4885#ifdef FEAT_VERTSPLIT
4886 /*
4887 * Do this twice:
4888 * 1: compute room available, if it's not enough try resizing the
4889 * containing frame.
4890 * 2: compute the room available and adjust the height to it.
4891 * Try not to reduce the height of a window with 'winfixheight' set.
4892 */
4893 for (run = 1; run <= 2; ++run)
4894#else
4895 for (;;)
4896#endif
4897 {
4898 room = 0;
4899 room_reserved = 0;
4900 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4901 frp = frp->fr_next)
4902 {
4903 if (frp != curfrp
4904 && frp->fr_win != NULL
4905 && frp->fr_win->w_p_wfh)
4906 room_reserved += frp->fr_height;
4907 room += frp->fr_height;
4908 if (frp != curfrp)
4909 room -= frame_minheight(frp, NULL);
4910 }
4911#ifdef FEAT_VERTSPLIT
4912 if (curfrp->fr_width != Columns)
4913 room_cmdline = 0;
4914 else
4915#endif
4916 {
4917 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4918 + lastwin->w_height + lastwin->w_status_height);
4919 if (room_cmdline < 0)
4920 room_cmdline = 0;
4921 }
4922
4923 if (height <= room + room_cmdline)
4924 break;
4925#ifdef FEAT_VERTSPLIT
4926 if (run == 2 || curfrp->fr_width == Columns)
4927#endif
4928 {
4929 if (height > room + room_cmdline)
4930 height = room + room_cmdline;
4931 break;
4932 }
4933#ifdef FEAT_VERTSPLIT
4934 frame_setheight(curfrp->fr_parent, height
4935 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4936#endif
4937 /*NOTREACHED*/
4938 }
4939
4940 /*
4941 * Compute the number of lines we will take from others frames (can be
4942 * negative!).
4943 */
4944 take = height - curfrp->fr_height;
4945
4946 /* If there is not enough room, also reduce the height of a window
4947 * with 'winfixheight' set. */
4948 if (height > room + room_cmdline - room_reserved)
4949 room_reserved = room + room_cmdline - height;
4950 /* If there is only a 'winfixheight' window and making the
4951 * window smaller, need to make the other window taller. */
4952 if (take < 0 && room - curfrp->fr_height < room_reserved)
4953 room_reserved = 0;
4954
4955 if (take > 0 && room_cmdline > 0)
4956 {
4957 /* use lines from cmdline first */
4958 if (take < room_cmdline)
4959 room_cmdline = take;
4960 take -= room_cmdline;
4961 topframe->fr_height += room_cmdline;
4962 }
4963
4964 /*
4965 * set the current frame to the new height
4966 */
4967 frame_new_height(curfrp, height, FALSE, FALSE);
4968
4969 /*
4970 * First take lines from the frames after the current frame. If
4971 * that is not enough, takes lines from frames above the current
4972 * frame.
4973 */
4974 for (run = 0; run < 2; ++run)
4975 {
4976 if (run == 0)
4977 frp = curfrp->fr_next; /* 1st run: start with next window */
4978 else
4979 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4980 while (frp != NULL && take != 0)
4981 {
4982 h = frame_minheight(frp, NULL);
4983 if (room_reserved > 0
4984 && frp->fr_win != NULL
4985 && frp->fr_win->w_p_wfh)
4986 {
4987 if (room_reserved >= frp->fr_height)
4988 room_reserved -= frp->fr_height;
4989 else
4990 {
4991 if (frp->fr_height - room_reserved > take)
4992 room_reserved = frp->fr_height - take;
4993 take -= frp->fr_height - room_reserved;
4994 frame_new_height(frp, room_reserved, FALSE, FALSE);
4995 room_reserved = 0;
4996 }
4997 }
4998 else
4999 {
5000 if (frp->fr_height - take < h)
5001 {
5002 take -= frp->fr_height - h;
5003 frame_new_height(frp, h, FALSE, FALSE);
5004 }
5005 else
5006 {
5007 frame_new_height(frp, frp->fr_height - take,
5008 FALSE, FALSE);
5009 take = 0;
5010 }
5011 }
5012 if (run == 0)
5013 frp = frp->fr_next;
5014 else
5015 frp = frp->fr_prev;
5016 }
5017 }
5018 }
5019}
5020
5021#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5022/*
5023 * Set current window width and take care of repositioning other windows to
5024 * fit around it.
5025 */
5026 void
5027win_setwidth(width)
5028 int width;
5029{
5030 win_setwidth_win(width, curwin);
5031}
5032
5033 void
5034win_setwidth_win(width, wp)
5035 int width;
5036 win_T *wp;
5037{
5038 /* Always keep current window at least one column wide, even when
5039 * 'winminwidth' is zero. */
5040 if (wp == curwin)
5041 {
5042 if (width < p_wmw)
5043 width = p_wmw;
5044 if (width == 0)
5045 width = 1;
5046 }
5047
5048 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5049
5050 /* recompute the window positions */
5051 (void)win_comp_pos();
5052
5053 redraw_all_later(NOT_VALID);
5054}
5055
5056/*
5057 * Set the width of a frame to "width" and take care that all frames and
5058 * windows inside it are resized. Also resize frames above and below if the
5059 * are in the same FR_ROW frame.
5060 *
5061 * Strategy is similar to frame_setheight().
5062 */
5063 static void
5064frame_setwidth(curfrp, width)
5065 frame_T *curfrp;
5066 int width;
5067{
5068 int room; /* total number of lines available */
5069 int take; /* number of lines taken from other windows */
5070 int run;
5071 frame_T *frp;
5072 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005073 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
5075 /* If the width already is the desired value, nothing to do. */
5076 if (curfrp->fr_width == width)
5077 return;
5078
5079 if (curfrp->fr_parent == NULL)
5080 /* topframe: can't change width */
5081 return;
5082
5083 if (curfrp->fr_parent->fr_layout == FR_COL)
5084 {
5085 /* Column of frames: Also need to resize frames above and below of
5086 * this one. First check for the minimal width of these. */
5087 w = frame_minwidth(curfrp->fr_parent, NULL);
5088 if (width < w)
5089 width = w;
5090 frame_setwidth(curfrp->fr_parent, width);
5091 }
5092 else
5093 {
5094 /*
5095 * Row of frames: try to change only frames in this row.
5096 *
5097 * Do this twice:
5098 * 1: compute room available, if it's not enough try resizing the
5099 * containing frame.
5100 * 2: compute the room available and adjust the width to it.
5101 */
5102 for (run = 1; run <= 2; ++run)
5103 {
5104 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005105 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5107 frp = frp->fr_next)
5108 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005109 if (frp != curfrp
5110 && frp->fr_win != NULL
5111 && frp->fr_win->w_p_wfw)
5112 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 room += frp->fr_width;
5114 if (frp != curfrp)
5115 room -= frame_minwidth(frp, NULL);
5116 }
5117
5118 if (width <= room)
5119 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005120 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 {
5122 if (width > room)
5123 width = room;
5124 break;
5125 }
5126 frame_setwidth(curfrp->fr_parent, width
5127 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5128 }
5129
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 /*
5131 * Compute the number of lines we will take from others frames (can be
5132 * negative!).
5133 */
5134 take = width - curfrp->fr_width;
5135
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005136 /* If there is not enough room, also reduce the width of a window
5137 * with 'winfixwidth' set. */
5138 if (width > room - room_reserved)
5139 room_reserved = room - width;
5140 /* If there is only a 'winfixwidth' window and making the
5141 * window smaller, need to make the other window narrower. */
5142 if (take < 0 && room - curfrp->fr_width < room_reserved)
5143 room_reserved = 0;
5144
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 /*
5146 * set the current frame to the new width
5147 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005148 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149
5150 /*
5151 * First take lines from the frames right of the current frame. If
5152 * that is not enough, takes lines from frames left of the current
5153 * frame.
5154 */
5155 for (run = 0; run < 2; ++run)
5156 {
5157 if (run == 0)
5158 frp = curfrp->fr_next; /* 1st run: start with next window */
5159 else
5160 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5161 while (frp != NULL && take != 0)
5162 {
5163 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005164 if (room_reserved > 0
5165 && frp->fr_win != NULL
5166 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005168 if (room_reserved >= frp->fr_width)
5169 room_reserved -= frp->fr_width;
5170 else
5171 {
5172 if (frp->fr_width - room_reserved > take)
5173 room_reserved = frp->fr_width - take;
5174 take -= frp->fr_width - room_reserved;
5175 frame_new_width(frp, room_reserved, FALSE, FALSE);
5176 room_reserved = 0;
5177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
5179 else
5180 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005181 if (frp->fr_width - take < w)
5182 {
5183 take -= frp->fr_width - w;
5184 frame_new_width(frp, w, FALSE, FALSE);
5185 }
5186 else
5187 {
5188 frame_new_width(frp, frp->fr_width - take,
5189 FALSE, FALSE);
5190 take = 0;
5191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
5193 if (run == 0)
5194 frp = frp->fr_next;
5195 else
5196 frp = frp->fr_prev;
5197 }
5198 }
5199 }
5200}
5201#endif /* FEAT_VERTSPLIT */
5202
5203/*
5204 * Check 'winminheight' for a valid value.
5205 */
5206 void
5207win_setminheight()
5208{
5209 int room;
5210 int first = TRUE;
5211 win_T *wp;
5212
5213 /* loop until there is a 'winminheight' that is possible */
5214 while (p_wmh > 0)
5215 {
5216 /* TODO: handle vertical splits */
5217 room = -p_wh;
5218 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5219 room += wp->w_height - p_wmh;
5220 if (room >= 0)
5221 break;
5222 --p_wmh;
5223 if (first)
5224 {
5225 EMSG(_(e_noroom));
5226 first = FALSE;
5227 }
5228 }
5229}
5230
5231#ifdef FEAT_MOUSE
5232
5233/*
5234 * Status line of dragwin is dragged "offset" lines down (negative is up).
5235 */
5236 void
5237win_drag_status_line(dragwin, offset)
5238 win_T *dragwin;
5239 int offset;
5240{
5241 frame_T *curfr;
5242 frame_T *fr;
5243 int room;
5244 int row;
5245 int up; /* if TRUE, drag status line up, otherwise down */
5246 int n;
5247
5248 fr = dragwin->w_frame;
5249 curfr = fr;
5250 if (fr != topframe) /* more than one window */
5251 {
5252 fr = fr->fr_parent;
5253 /* When the parent frame is not a column of frames, its parent should
5254 * be. */
5255 if (fr->fr_layout != FR_COL)
5256 {
5257 curfr = fr;
5258 if (fr != topframe) /* only a row of windows, may drag statusline */
5259 fr = fr->fr_parent;
5260 }
5261 }
5262
5263 /* If this is the last frame in a column, may want to resize the parent
5264 * frame instead (go two up to skip a row of frames). */
5265 while (curfr != topframe && curfr->fr_next == NULL)
5266 {
5267 if (fr != topframe)
5268 fr = fr->fr_parent;
5269 curfr = fr;
5270 if (fr != topframe)
5271 fr = fr->fr_parent;
5272 }
5273
5274 if (offset < 0) /* drag up */
5275 {
5276 up = TRUE;
5277 offset = -offset;
5278 /* sum up the room of the current frame and above it */
5279 if (fr == curfr)
5280 {
5281 /* only one window */
5282 room = fr->fr_height - frame_minheight(fr, NULL);
5283 }
5284 else
5285 {
5286 room = 0;
5287 for (fr = fr->fr_child; ; fr = fr->fr_next)
5288 {
5289 room += fr->fr_height - frame_minheight(fr, NULL);
5290 if (fr == curfr)
5291 break;
5292 }
5293 }
5294 fr = curfr->fr_next; /* put fr at frame that grows */
5295 }
5296 else /* drag down */
5297 {
5298 up = FALSE;
5299 /*
5300 * Only dragging the last status line can reduce p_ch.
5301 */
5302 room = Rows - cmdline_row;
5303 if (curfr->fr_next == NULL)
5304 room -= 1;
5305 else
5306 room -= p_ch;
5307 if (room < 0)
5308 room = 0;
5309 /* sum up the room of frames below of the current one */
5310 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5311 room += fr->fr_height - frame_minheight(fr, NULL);
5312 fr = curfr; /* put fr at window that grows */
5313 }
5314
5315 if (room < offset) /* Not enough room */
5316 offset = room; /* Move as far as we can */
5317 if (offset <= 0)
5318 return;
5319
5320 /*
5321 * Grow frame fr by "offset" lines.
5322 * Doesn't happen when dragging the last status line up.
5323 */
5324 if (fr != NULL)
5325 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5326
5327 if (up)
5328 fr = curfr; /* current frame gets smaller */
5329 else
5330 fr = curfr->fr_next; /* next frame gets smaller */
5331
5332 /*
5333 * Now make the other frames smaller.
5334 */
5335 while (fr != NULL && offset > 0)
5336 {
5337 n = frame_minheight(fr, NULL);
5338 if (fr->fr_height - offset <= n)
5339 {
5340 offset -= fr->fr_height - n;
5341 frame_new_height(fr, n, !up, FALSE);
5342 }
5343 else
5344 {
5345 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5346 break;
5347 }
5348 if (up)
5349 fr = fr->fr_prev;
5350 else
5351 fr = fr->fr_next;
5352 }
5353 row = win_comp_pos();
5354 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5355 cmdline_row = row;
5356 p_ch = Rows - cmdline_row;
5357 if (p_ch < 1)
5358 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005359 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005360 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 showmode();
5362}
5363
5364#ifdef FEAT_VERTSPLIT
5365/*
5366 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5367 */
5368 void
5369win_drag_vsep_line(dragwin, offset)
5370 win_T *dragwin;
5371 int offset;
5372{
5373 frame_T *curfr;
5374 frame_T *fr;
5375 int room;
5376 int left; /* if TRUE, drag separator line left, otherwise right */
5377 int n;
5378
5379 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005380 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 return;
5382 curfr = fr;
5383 fr = fr->fr_parent;
5384 /* When the parent frame is not a row of frames, its parent should be. */
5385 if (fr->fr_layout != FR_ROW)
5386 {
5387 if (fr == topframe) /* only a column of windows (cannot happen?) */
5388 return;
5389 curfr = fr;
5390 fr = fr->fr_parent;
5391 }
5392
5393 /* If this is the last frame in a row, may want to resize a parent
5394 * frame instead. */
5395 while (curfr->fr_next == NULL)
5396 {
5397 if (fr == topframe)
5398 break;
5399 curfr = fr;
5400 fr = fr->fr_parent;
5401 if (fr != topframe)
5402 {
5403 curfr = fr;
5404 fr = fr->fr_parent;
5405 }
5406 }
5407
5408 if (offset < 0) /* drag left */
5409 {
5410 left = TRUE;
5411 offset = -offset;
5412 /* sum up the room of the current frame and left of it */
5413 room = 0;
5414 for (fr = fr->fr_child; ; fr = fr->fr_next)
5415 {
5416 room += fr->fr_width - frame_minwidth(fr, NULL);
5417 if (fr == curfr)
5418 break;
5419 }
5420 fr = curfr->fr_next; /* put fr at frame that grows */
5421 }
5422 else /* drag right */
5423 {
5424 left = FALSE;
5425 /* sum up the room of frames right of the current one */
5426 room = 0;
5427 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5428 room += fr->fr_width - frame_minwidth(fr, NULL);
5429 fr = curfr; /* put fr at window that grows */
5430 }
5431
5432 if (room < offset) /* Not enough room */
5433 offset = room; /* Move as far as we can */
5434 if (offset <= 0) /* No room at all, quit. */
5435 return;
5436
5437 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005438 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439
5440 /* shrink other frames: current and at the left or at the right */
5441 if (left)
5442 fr = curfr; /* current frame gets smaller */
5443 else
5444 fr = curfr->fr_next; /* next frame gets smaller */
5445
5446 while (fr != NULL && offset > 0)
5447 {
5448 n = frame_minwidth(fr, NULL);
5449 if (fr->fr_width - offset <= n)
5450 {
5451 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005452 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 }
5454 else
5455 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005456 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 break;
5458 }
5459 if (left)
5460 fr = fr->fr_prev;
5461 else
5462 fr = fr->fr_next;
5463 }
5464 (void)win_comp_pos();
5465 redraw_all_later(NOT_VALID);
5466}
5467#endif /* FEAT_VERTSPLIT */
5468#endif /* FEAT_MOUSE */
5469
5470#endif /* FEAT_WINDOWS */
5471
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005472#define FRACTION_MULT 16384L
5473
5474/*
5475 * Set wp->w_fraction for the current w_wrow and w_height.
5476 */
5477 static void
5478set_fraction(wp)
5479 win_T *wp;
5480{
5481 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5482 + FRACTION_MULT / 2) / (long)wp->w_height;
5483}
5484
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485/*
5486 * Set the height of a window.
5487 * This takes care of the things inside the window, not what happens to the
5488 * window position, the frame or to other windows.
5489 */
5490 static void
5491win_new_height(wp, height)
5492 win_T *wp;
5493 int height;
5494{
5495 linenr_T lnum;
5496 int sline, line_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497
5498 /* Don't want a negative height. Happens when splitting a tiny window.
5499 * Will equalize heights soon to fix it. */
5500 if (height < 0)
5501 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005502 if (wp->w_height == height)
5503 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
5505 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005506 set_fraction(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507
5508 wp->w_height = height;
5509 wp->w_skipcol = 0;
5510
5511 /* Don't change w_topline when height is zero. Don't set w_topline when
5512 * 'scrollbind' is set and this isn't the current window. */
5513 if (height > 0
5514#ifdef FEAT_SCROLLBIND
5515 && (!wp->w_p_scb || wp == curwin)
5516#endif
5517 )
5518 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005519 /*
5520 * Find a value for w_topline that shows the cursor at the same
5521 * relative position in the window as before (more or less).
5522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 lnum = wp->w_cursor.lnum;
5524 if (lnum < 1) /* can happen when starting up */
5525 lnum = 1;
5526 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5527 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5528 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005529
5530 if (sline >= 0)
5531 {
5532 /* Make sure the whole cursor line is visible, if possible. */
5533 int rows = plines_win(wp, lnum, FALSE);
5534
5535 if (sline > wp->w_height - rows)
5536 {
5537 sline = wp->w_height - rows;
5538 wp->w_wrow -= rows - line_size;
5539 }
5540 }
5541
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 if (sline < 0)
5543 {
5544 /*
5545 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005546 * Make cursor line the first line in the window. If not enough
5547 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 */
5549 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005550 if (wp->w_wrow >= wp->w_height
5551 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5552 {
5553 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5554 --wp->w_wrow;
5555 while (wp->w_wrow >= wp->w_height)
5556 {
5557 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5558 + win_col_off2(wp);
5559 --wp->w_wrow;
5560 }
5561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 }
5563 else
5564 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005565 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {
5567#ifdef FEAT_FOLDING
5568 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5569 if (lnum == 1)
5570 {
5571 /* first line in buffer is folded */
5572 line_size = 1;
5573 --sline;
5574 break;
5575 }
5576#endif
5577 --lnum;
5578#ifdef FEAT_DIFF
5579 if (lnum == wp->w_topline)
5580 line_size = plines_win_nofill(wp, lnum, TRUE)
5581 + wp->w_topfill;
5582 else
5583#endif
5584 line_size = plines_win(wp, lnum, TRUE);
5585 sline -= line_size;
5586 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005587
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 if (sline < 0)
5589 {
5590 /*
5591 * Line we want at top would go off top of screen. Use next
5592 * line instead.
5593 */
5594#ifdef FEAT_FOLDING
5595 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5596#endif
5597 lnum++;
5598 wp->w_wrow -= line_size + sline;
5599 }
5600 else if (sline > 0)
5601 {
5602 /* First line of file reached, use that as topline. */
5603 lnum = 1;
5604 wp->w_wrow -= sline;
5605 }
5606 }
5607 set_topline(wp, lnum);
5608 }
5609
5610 if (wp == curwin)
5611 {
5612 if (p_so)
5613 update_topline();
5614 curs_columns(FALSE); /* validate w_wrow */
5615 }
5616 wp->w_prev_fraction_row = wp->w_wrow;
5617
5618 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005619 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620#ifdef FEAT_WINDOWS
5621 wp->w_redr_status = TRUE;
5622#endif
5623 invalidate_botline_win(wp);
5624}
5625
5626#ifdef FEAT_VERTSPLIT
5627/*
5628 * Set the width of a window.
5629 */
5630 static void
5631win_new_width(wp, width)
5632 win_T *wp;
5633 int width;
5634{
5635 wp->w_width = width;
5636 wp->w_lines_valid = 0;
5637 changed_line_abv_curs_win(wp);
5638 invalidate_botline_win(wp);
5639 if (wp == curwin)
5640 {
5641 update_topline();
5642 curs_columns(TRUE); /* validate w_wrow */
5643 }
5644 redraw_win_later(wp, NOT_VALID);
5645 wp->w_redr_status = TRUE;
5646}
5647#endif
5648
5649 void
5650win_comp_scroll(wp)
5651 win_T *wp;
5652{
5653 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5654 if (wp->w_p_scr == 0)
5655 wp->w_p_scr = 1;
5656}
5657
5658/*
5659 * command_height: called whenever p_ch has been changed
5660 */
5661 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005662command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663{
5664#ifdef FEAT_WINDOWS
5665 int h;
5666 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005667 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005669 /* Use the value of p_ch that we remembered. This is needed for when the
5670 * GUI starts up, we can't be sure in what order things happen. And when
5671 * p_ch was changed in another tab page. */
5672 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005673
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 /* Find bottom frame with width of screen. */
5675 frp = lastwin->w_frame;
5676# ifdef FEAT_VERTSPLIT
5677 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5678 frp = frp->fr_parent;
5679# endif
5680
5681 /* Avoid changing the height of a window with 'winfixheight' set. */
5682 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5683 && frp->fr_win->w_p_wfh)
5684 frp = frp->fr_prev;
5685
5686 if (starting != NO_SCREEN)
5687 {
5688 cmdline_row = Rows - p_ch;
5689
5690 if (p_ch > old_p_ch) /* p_ch got bigger */
5691 {
5692 while (p_ch > old_p_ch)
5693 {
5694 if (frp == NULL)
5695 {
5696 EMSG(_(e_noroom));
5697 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005698 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 cmdline_row = Rows - p_ch;
5700 break;
5701 }
5702 h = frp->fr_height - frame_minheight(frp, NULL);
5703 if (h > p_ch - old_p_ch)
5704 h = p_ch - old_p_ch;
5705 old_p_ch += h;
5706 frame_add_height(frp, -h);
5707 frp = frp->fr_prev;
5708 }
5709
5710 /* Recompute window positions. */
5711 (void)win_comp_pos();
5712
5713 /* clear the lines added to cmdline */
5714 if (full_screen)
5715 screen_fill((int)(cmdline_row), (int)Rows, 0,
5716 (int)Columns, ' ', ' ', 0);
5717 msg_row = cmdline_row;
5718 redraw_cmdline = TRUE;
5719 return;
5720 }
5721
5722 if (msg_row < cmdline_row)
5723 msg_row = cmdline_row;
5724 redraw_cmdline = TRUE;
5725 }
5726 frame_add_height(frp, (int)(old_p_ch - p_ch));
5727
5728 /* Recompute window positions. */
5729 if (frp != lastwin->w_frame)
5730 (void)win_comp_pos();
5731#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005733 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734#endif
5735}
5736
5737#if defined(FEAT_WINDOWS) || defined(PROTO)
5738/*
5739 * Resize frame "frp" to be "n" lines higher (negative for less high).
5740 * Also resize the frames it is contained in.
5741 */
5742 static void
5743frame_add_height(frp, n)
5744 frame_T *frp;
5745 int n;
5746{
5747 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5748 for (;;)
5749 {
5750 frp = frp->fr_parent;
5751 if (frp == NULL)
5752 break;
5753 frp->fr_height += n;
5754 }
5755}
5756
5757/*
5758 * Add or remove a status line for the bottom window(s), according to the
5759 * value of 'laststatus'.
5760 */
5761 void
5762last_status(morewin)
5763 int morewin; /* pretend there are two or more windows */
5764{
5765 /* Don't make a difference between horizontal or vertical split. */
5766 last_status_rec(topframe, (p_ls == 2
5767 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5768}
5769
5770 static void
5771last_status_rec(fr, statusline)
5772 frame_T *fr;
5773 int statusline;
5774{
5775 frame_T *fp;
5776 win_T *wp;
5777
5778 if (fr->fr_layout == FR_LEAF)
5779 {
5780 wp = fr->fr_win;
5781 if (wp->w_status_height != 0 && !statusline)
5782 {
5783 /* remove status line */
5784 win_new_height(wp, wp->w_height + 1);
5785 wp->w_status_height = 0;
5786 comp_col();
5787 }
5788 else if (wp->w_status_height == 0 && statusline)
5789 {
5790 /* Find a frame to take a line from. */
5791 fp = fr;
5792 while (fp->fr_height <= frame_minheight(fp, NULL))
5793 {
5794 if (fp == topframe)
5795 {
5796 EMSG(_(e_noroom));
5797 return;
5798 }
5799 /* In a column of frames: go to frame above. If already at
5800 * the top or in a row of frames: go to parent. */
5801 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5802 fp = fp->fr_prev;
5803 else
5804 fp = fp->fr_parent;
5805 }
5806 wp->w_status_height = 1;
5807 if (fp != fr)
5808 {
5809 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5810 frame_fix_height(wp);
5811 (void)win_comp_pos();
5812 }
5813 else
5814 win_new_height(wp, wp->w_height - 1);
5815 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005816 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 }
5818 }
5819#ifdef FEAT_VERTSPLIT
5820 else if (fr->fr_layout == FR_ROW)
5821 {
5822 /* vertically split windows, set status line for each one */
5823 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5824 last_status_rec(fp, statusline);
5825 }
5826#endif
5827 else
5828 {
5829 /* horizontally split window, set status line for last one */
5830 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5831 ;
5832 last_status_rec(fp, statusline);
5833 }
5834}
5835
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005836/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005837 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005838 */
5839 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005840tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005841{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005842#ifdef FEAT_GUI_TABLINE
5843 /* When the GUI has the tabline then this always returns zero. */
5844 if (gui_use_tabline())
5845 return 0;
5846#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005847 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005848 {
5849 case 0: return 0;
5850 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5851 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005852 return 1;
5853}
5854
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855#endif /* FEAT_WINDOWS */
5856
5857#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5858/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005859 * Get the file name at the cursor.
5860 * If Visual mode is active, use the selected text if it's in one line.
5861 * Returns the name in allocated memory, NULL for failure.
5862 */
5863 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005864grab_file_name(count, file_lnum)
5865 long count;
5866 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005867{
5868# ifdef FEAT_VISUAL
5869 if (VIsual_active)
5870 {
5871 int len;
5872 char_u *ptr;
5873
5874 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5875 return NULL;
5876 return find_file_name_in_path(ptr, len,
5877 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5878 }
5879# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005880 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5881 file_lnum);
5882
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005883}
5884
5885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 * Return the file name under or after the cursor.
5887 *
5888 * The 'path' option is searched if the file name is not absolute.
5889 * The string returned has been alloc'ed and should be freed by the caller.
5890 * NULL is returned if the file name or file is not found.
5891 *
5892 * options:
5893 * FNAME_MESS give error messages
5894 * FNAME_EXP expand to path
5895 * FNAME_HYP check for hypertext link
5896 * FNAME_INCL apply "includeexpr"
5897 */
5898 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005899file_name_at_cursor(options, count, file_lnum)
5900 int options;
5901 long count;
5902 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903{
5904 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005905 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5906 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907}
5908
5909/*
5910 * Return the name of the file under or after ptr[col].
5911 * Otherwise like file_name_at_cursor().
5912 */
5913 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005914file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 char_u *line;
5916 int col;
5917 int options;
5918 long count;
5919 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005920 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921{
5922 char_u *ptr;
5923 int len;
5924
5925 /*
5926 * search forward for what could be the start of a file name
5927 */
5928 ptr = line + col;
5929 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005930 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 if (*ptr == NUL) /* nothing found */
5932 {
5933 if (options & FNAME_MESS)
5934 EMSG(_("E446: No file name under cursor"));
5935 return NULL;
5936 }
5937
5938 /*
5939 * Search backward for first char of the file name.
5940 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5941 */
5942 while (ptr > line)
5943 {
5944#ifdef FEAT_MBYTE
5945 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5946 ptr -= len + 1;
5947 else
5948#endif
5949 if (vim_isfilec(ptr[-1])
5950 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5951 --ptr;
5952 else
5953 break;
5954 }
5955
5956 /*
5957 * Search forward for the last char of the file name.
5958 * Also allow "://" when ':' is not in 'isfname'.
5959 */
5960 len = 0;
5961 while (vim_isfilec(ptr[len])
5962 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5963#ifdef FEAT_MBYTE
5964 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005965 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 else
5967#endif
5968 ++len;
5969
5970 /*
5971 * If there is trailing punctuation, remove it.
5972 * But don't remove "..", could be a directory name.
5973 */
5974 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5975 && ptr[len - 2] != '.')
5976 --len;
5977
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005978 if (file_lnum != NULL)
5979 {
5980 char_u *p;
5981
5982 /* Get the number after the file name and a separator character */
5983 p = ptr + len;
5984 p = skipwhite(p);
5985 if (*p != NUL)
5986 {
5987 if (!isdigit(*p))
5988 ++p; /* skip the separator */
5989 p = skipwhite(p);
5990 if (isdigit(*p))
5991 *file_lnum = (int)getdigits(&p);
5992 }
5993 }
5994
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5996}
5997
5998# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5999static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6000
6001 static char_u *
6002eval_includeexpr(ptr, len)
6003 char_u *ptr;
6004 int len;
6005{
6006 char_u *res;
6007
6008 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006009 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006010 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 set_vim_var_string(VV_FNAME, NULL, 0);
6012 return res;
6013}
6014#endif
6015
6016/*
6017 * Return the name of the file ptr[len] in 'path'.
6018 * Otherwise like file_name_at_cursor().
6019 */
6020 char_u *
6021find_file_name_in_path(ptr, len, options, count, rel_fname)
6022 char_u *ptr;
6023 int len;
6024 int options;
6025 long count;
6026 char_u *rel_fname; /* file we are searching relative to */
6027{
6028 char_u *file_name;
6029 int c;
6030# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6031 char_u *tofree = NULL;
6032
6033 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6034 {
6035 tofree = eval_includeexpr(ptr, len);
6036 if (tofree != NULL)
6037 {
6038 ptr = tofree;
6039 len = (int)STRLEN(ptr);
6040 }
6041 }
6042# endif
6043
6044 if (options & FNAME_EXP)
6045 {
6046 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6047 TRUE, rel_fname);
6048
6049# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6050 /*
6051 * If the file could not be found in a normal way, try applying
6052 * 'includeexpr' (unless done already).
6053 */
6054 if (file_name == NULL
6055 && !(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 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6063 TRUE, rel_fname);
6064 }
6065 }
6066# endif
6067 if (file_name == NULL && (options & FNAME_MESS))
6068 {
6069 c = ptr[len];
6070 ptr[len] = NUL;
6071 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6072 ptr[len] = c;
6073 }
6074
6075 /* Repeat finding the file "count" times. This matters when it
6076 * appears several times in the path. */
6077 while (file_name != NULL && --count > 0)
6078 {
6079 vim_free(file_name);
6080 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6081 }
6082 }
6083 else
6084 file_name = vim_strnsave(ptr, len);
6085
6086# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6087 vim_free(tofree);
6088# endif
6089
6090 return file_name;
6091}
6092#endif /* FEAT_SEARCHPATH */
6093
6094/*
6095 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6096 * Also check for ":\\", which MS Internet Explorer accepts, return
6097 * URL_BACKSLASH.
6098 */
6099 static int
6100path_is_url(p)
6101 char_u *p;
6102{
6103 if (STRNCMP(p, "://", (size_t)3) == 0)
6104 return URL_SLASH;
6105 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6106 return URL_BACKSLASH;
6107 return 0;
6108}
6109
6110/*
6111 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6112 * Return URL_BACKSLASH for "name:\\".
6113 * Return zero otherwise.
6114 */
6115 int
6116path_with_url(fname)
6117 char_u *fname;
6118{
6119 char_u *p;
6120
6121 for (p = fname; isalpha(*p); ++p)
6122 ;
6123 return path_is_url(p);
6124}
6125
6126/*
6127 * Return TRUE if "name" is a full (absolute) path name or URL.
6128 */
6129 int
6130vim_isAbsName(name)
6131 char_u *name;
6132{
6133 return (path_with_url(name) != 0 || mch_isFullName(name));
6134}
6135
6136/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006137 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 *
6139 * return FAIL for failure, OK otherwise
6140 */
6141 int
6142vim_FullName(fname, buf, len, force)
6143 char_u *fname, *buf;
6144 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006145 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146{
6147 int retval = OK;
6148 int url;
6149
6150 *buf = NUL;
6151 if (fname == NULL)
6152 return FAIL;
6153
6154 url = path_with_url(fname);
6155 if (!url)
6156 retval = mch_FullName(fname, buf, len, force);
6157 if (url || retval == FAIL)
6158 {
6159 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006160 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 }
6162#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6163 slash_adjust(buf);
6164#endif
6165 return retval;
6166}
6167
6168/*
6169 * Return the minimal number of rows that is needed on the screen to display
6170 * the current number of windows.
6171 */
6172 int
6173min_rows()
6174{
6175 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006176#ifdef FEAT_WINDOWS
6177 tabpage_T *tp;
6178 int n;
6179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180
6181 if (firstwin == NULL) /* not initialized yet */
6182 return MIN_LINES;
6183
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006185 total = 0;
6186 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6187 {
6188 n = frame_minheight(tp->tp_topframe, NULL);
6189 if (total < n)
6190 total = n;
6191 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006192 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006194 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006196 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 return total;
6198}
6199
6200/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006201 * Return TRUE if there is only one window (in the current tab page), not
6202 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006203 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 */
6205 int
6206only_one_window()
6207{
6208#ifdef FEAT_WINDOWS
6209 int count = 0;
6210 win_T *wp;
6211
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006212 /* If there is another tab page there always is another window. */
6213 if (first_tabpage->tp_next != NULL)
6214 return FALSE;
6215
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006217 if ((!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218# ifdef FEAT_QUICKFIX
6219 || wp->w_p_pvw
6220# endif
6221 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006222# ifdef FEAT_AUTOCMD
6223 && wp != aucmd_win
6224# endif
6225 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 ++count;
6227 return (count <= 1);
6228#else
6229 return TRUE;
6230#endif
6231}
6232
6233#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6234/*
6235 * Correct the cursor line number in other windows. Used after changing the
6236 * current buffer, and before applying autocommands.
6237 * When "do_curwin" is TRUE, also check current window.
6238 */
6239 void
6240check_lnums(do_curwin)
6241 int do_curwin;
6242{
6243 win_T *wp;
6244
6245#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006246 tabpage_T *tp;
6247
6248 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6250#else
6251 wp = curwin;
6252 if (do_curwin)
6253#endif
6254 {
6255 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6256 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6257 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6258 wp->w_topline = curbuf->b_ml.ml_line_count;
6259 }
6260}
6261#endif
6262
6263#if defined(FEAT_WINDOWS) || defined(PROTO)
6264
6265/*
6266 * A snapshot of the window sizes, to restore them after closing the help
6267 * window.
6268 * Only these fields are used:
6269 * fr_layout
6270 * fr_width
6271 * fr_height
6272 * fr_next
6273 * fr_child
6274 * fr_win (only valid for the old curwin, NULL otherwise)
6275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276
6277/*
6278 * Create a snapshot of the current frame sizes.
6279 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006280 void
6281make_snapshot(idx)
6282 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006284 clear_snapshot(curtab, idx);
6285 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286}
6287
6288 static void
6289make_snapshot_rec(fr, frp)
6290 frame_T *fr;
6291 frame_T **frp;
6292{
6293 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6294 if (*frp == NULL)
6295 return;
6296 (*frp)->fr_layout = fr->fr_layout;
6297# ifdef FEAT_VERTSPLIT
6298 (*frp)->fr_width = fr->fr_width;
6299# endif
6300 (*frp)->fr_height = fr->fr_height;
6301 if (fr->fr_next != NULL)
6302 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6303 if (fr->fr_child != NULL)
6304 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6305 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6306 (*frp)->fr_win = curwin;
6307}
6308
6309/*
6310 * Remove any existing snapshot.
6311 */
6312 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006313clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006314 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006315 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006317 clear_snapshot_rec(tp->tp_snapshot[idx]);
6318 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319}
6320
6321 static void
6322clear_snapshot_rec(fr)
6323 frame_T *fr;
6324{
6325 if (fr != NULL)
6326 {
6327 clear_snapshot_rec(fr->fr_next);
6328 clear_snapshot_rec(fr->fr_child);
6329 vim_free(fr);
6330 }
6331}
6332
6333/*
6334 * Restore a previously created snapshot, if there is any.
6335 * This is only done if the screen size didn't change and the window layout is
6336 * still the same.
6337 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006338 void
6339restore_snapshot(idx, close_curwin)
6340 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 int close_curwin; /* closing current window */
6342{
6343 win_T *wp;
6344
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006345 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006347 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006349 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6350 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006352 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 win_comp_pos();
6354 if (wp != NULL && close_curwin)
6355 win_goto(wp);
6356 redraw_all_later(CLEAR);
6357 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006358 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359}
6360
6361/*
6362 * Check if frames "sn" and "fr" have the same layout, same following frames
6363 * and same children.
6364 */
6365 static int
6366check_snapshot_rec(sn, fr)
6367 frame_T *sn;
6368 frame_T *fr;
6369{
6370 if (sn->fr_layout != fr->fr_layout
6371 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6372 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6373 || (sn->fr_next != NULL
6374 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6375 || (sn->fr_child != NULL
6376 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6377 return FAIL;
6378 return OK;
6379}
6380
6381/*
6382 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6383 * following frames and children.
6384 * Returns a pointer to the old current window, or NULL.
6385 */
6386 static win_T *
6387restore_snapshot_rec(sn, fr)
6388 frame_T *sn;
6389 frame_T *fr;
6390{
6391 win_T *wp = NULL;
6392 win_T *wp2;
6393
6394 fr->fr_height = sn->fr_height;
6395# ifdef FEAT_VERTSPLIT
6396 fr->fr_width = sn->fr_width;
6397# endif
6398 if (fr->fr_layout == FR_LEAF)
6399 {
6400 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6401# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006402 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403# endif
6404 wp = sn->fr_win;
6405 }
6406 if (sn->fr_next != NULL)
6407 {
6408 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6409 if (wp2 != NULL)
6410 wp = wp2;
6411 }
6412 if (sn->fr_child != NULL)
6413 {
6414 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6415 if (wp2 != NULL)
6416 wp = wp2;
6417 }
6418 return wp;
6419}
6420
6421#endif
6422
6423#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6424/*
6425 * Return TRUE if there is any vertically split window.
6426 */
6427 int
6428win_hasvertsplit()
6429{
6430 frame_T *fr;
6431
6432 if (topframe->fr_layout == FR_ROW)
6433 return TRUE;
6434
6435 if (topframe->fr_layout == FR_COL)
6436 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6437 if (fr->fr_layout == FR_ROW)
6438 return TRUE;
6439
6440 return FALSE;
6441}
6442#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006443
6444#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6445/*
6446 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006447 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006448 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6449 * If no particular ID is desired, -1 must be specified for 'id'.
6450 * Return ID of added match, -1 on failure.
6451 */
6452 int
6453match_add(wp, grp, pat, prio, id)
6454 win_T *wp;
6455 char_u *grp;
6456 char_u *pat;
6457 int prio;
6458 int id;
6459{
6460 matchitem_T *cur;
6461 matchitem_T *prev;
6462 matchitem_T *m;
6463 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006464 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006465
6466 if (*grp == NUL || *pat == NUL)
6467 return -1;
6468 if (id < -1 || id == 0)
6469 {
6470 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6471 return -1;
6472 }
6473 if (id != -1)
6474 {
6475 cur = wp->w_match_head;
6476 while (cur != NULL)
6477 {
6478 if (cur->id == id)
6479 {
6480 EMSGN("E801: ID already taken: %ld", id);
6481 return -1;
6482 }
6483 cur = cur->next;
6484 }
6485 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006486 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006487 {
6488 EMSG2(_(e_nogroup), grp);
6489 return -1;
6490 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006491 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006492 {
6493 EMSG2(_(e_invarg2), pat);
6494 return -1;
6495 }
6496
6497 /* Find available match ID. */
6498 while (id == -1)
6499 {
6500 cur = wp->w_match_head;
6501 while (cur != NULL && cur->id != wp->w_next_match_id)
6502 cur = cur->next;
6503 if (cur == NULL)
6504 id = wp->w_next_match_id;
6505 wp->w_next_match_id++;
6506 }
6507
6508 /* Build new match. */
6509 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6510 m->id = id;
6511 m->priority = prio;
6512 m->pattern = vim_strsave(pat);
6513 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006514 m->match.regprog = regprog;
6515 m->match.rmm_ic = FALSE;
6516 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006517
6518 /* Insert new match. The match list is in ascending order with regard to
6519 * the match priorities. */
6520 cur = wp->w_match_head;
6521 prev = cur;
6522 while (cur != NULL && prio >= cur->priority)
6523 {
6524 prev = cur;
6525 cur = cur->next;
6526 }
6527 if (cur == prev)
6528 wp->w_match_head = m;
6529 else
6530 prev->next = m;
6531 m->next = cur;
6532
6533 redraw_later(SOME_VALID);
6534 return id;
6535}
6536
6537/*
6538 * Delete match with ID 'id' in the match list of window 'wp'.
6539 * Print error messages if 'perr' is TRUE.
6540 */
6541 int
6542match_delete(wp, id, perr)
6543 win_T *wp;
6544 int id;
6545 int perr;
6546{
6547 matchitem_T *cur = wp->w_match_head;
6548 matchitem_T *prev = cur;
6549
6550 if (id < 1)
6551 {
6552 if (perr == TRUE)
6553 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6554 id);
6555 return -1;
6556 }
6557 while (cur != NULL && cur->id != id)
6558 {
6559 prev = cur;
6560 cur = cur->next;
6561 }
6562 if (cur == NULL)
6563 {
6564 if (perr == TRUE)
6565 EMSGN("E803: ID not found: %ld", id);
6566 return -1;
6567 }
6568 if (cur == prev)
6569 wp->w_match_head = cur->next;
6570 else
6571 prev->next = cur->next;
6572 vim_free(cur->match.regprog);
6573 vim_free(cur->pattern);
6574 vim_free(cur);
6575 redraw_later(SOME_VALID);
6576 return 0;
6577}
6578
6579/*
6580 * Delete all matches in the match list of window 'wp'.
6581 */
6582 void
6583clear_matches(wp)
6584 win_T *wp;
6585{
6586 matchitem_T *m;
6587
6588 while (wp->w_match_head != NULL)
6589 {
6590 m = wp->w_match_head->next;
6591 vim_free(wp->w_match_head->match.regprog);
6592 vim_free(wp->w_match_head->pattern);
6593 vim_free(wp->w_match_head);
6594 wp->w_match_head = m;
6595 }
6596 redraw_later(SOME_VALID);
6597}
6598
6599/*
6600 * Get match from ID 'id' in window 'wp'.
6601 * Return NULL if match not found.
6602 */
6603 matchitem_T *
6604get_match(wp, id)
6605 win_T *wp;
6606 int id;
6607{
6608 matchitem_T *cur = wp->w_match_head;
6609
6610 while (cur != NULL && cur->id != id)
6611 cur = cur->next;
6612 return cur;
6613}
6614#endif