blob: 4b721eaa76c924a88e1aa0ea5873a79c0864a214 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#include "vim.h"
11
Bram Moolenaar071d4272004-06-13 20:20:40 +000012static int path_is_url __ARGS((char_u *p));
13#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar884ae642009-02-22 01:37:59 +000014static void win_init __ARGS((win_T *newp, win_T *oldp, int flags));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000015static void win_init_some __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
17static void frame_setheight __ARGS((frame_T *curfrp, int height));
18#ifdef FEAT_VERTSPLIT
19static void frame_setwidth __ARGS((frame_T *curfrp, int width));
20#endif
21static void win_exchange __ARGS((long));
22static void win_rotate __ARGS((int, int));
23static void win_totop __ARGS((int size, int flags));
24static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000025static int last_window __ARGS((void));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000026static int one_window __ARGS((void));
Bram Moolenaarf740b292006-02-16 22:11:02 +000027static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
Bram Moolenaarf740b292006-02-16 22:11:02 +000028static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000029static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000030static win_T *frame2win __ARGS((frame_T *frp));
31static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
32static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
33static int frame_fixed_height __ARGS((frame_T *frp));
34#ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000035static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000036static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000037static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000038static void frame_add_vsep __ARGS((frame_T *frp));
39static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
40static void frame_fix_width __ARGS((win_T *wp));
41#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000042#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000043static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000044static void new_frame __ARGS((win_T *wp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000045#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000046static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000047static int leave_tabpage __ARGS((buf_T *new_curbuf));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000048static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000049static void frame_fix_height __ARGS((win_T *wp));
50static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
51static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
Bram Moolenaarf740b292006-02-16 22:11:02 +000052static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000053static void frame_append __ARGS((frame_T *after, frame_T *frp));
54static void frame_insert __ARGS((frame_T *before, frame_T *frp));
55static void frame_remove __ARGS((frame_T *frp));
56#ifdef FEAT_VERTSPLIT
57static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static void win_goto_ver __ARGS((int up, long count));
59static void win_goto_hor __ARGS((int left, long count));
60#endif
61static void frame_add_height __ARGS((frame_T *frp, int n));
62static void last_status_rec __ARGS((frame_T *fr, int statusline));
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000065static void clear_snapshot __ARGS((tabpage_T *tp, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static void clear_snapshot_rec __ARGS((frame_T *fr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000067static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
68static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
69
70#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000071
Bram Moolenaar746ebd32009-06-16 14:01:43 +000072static win_T *win_alloc __ARGS((win_T *after, int hidden));
Bram Moolenaar071d4272004-06-13 20:20:40 +000073static void win_new_height __ARGS((win_T *, int));
74
75#define URL_SLASH 1 /* path_is_url() has found "://" */
76#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
77
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000078#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Bram Moolenaar05159a02005-02-26 23:04:13 +000080#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000081# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000082#else
83# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000084#endif
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000087
88static char *m_onlyone = N_("Already only one window");
89
Bram Moolenaar071d4272004-06-13 20:20:40 +000090/*
91 * all CTRL-W window commands are handled here, called from normal_cmd().
92 */
93 void
94do_window(nchar, Prenum, xchar)
95 int nchar;
96 long Prenum;
97 int xchar; /* extra char from ":wincmd gx" or NUL */
98{
99 long Prenum1;
100 win_T *wp;
101#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
102 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000103 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105#ifdef FEAT_FIND_ID
106 int type = FIND_DEFINE;
107 int len;
108#endif
109 char_u cbuf[40];
110
111 if (Prenum == 0)
112 Prenum1 = 1;
113 else
114 Prenum1 = Prenum;
115
116#ifdef FEAT_CMDWIN
117# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
118#else
119# define CHECK_CMDWIN
120#endif
121
122 switch (nchar)
123 {
124/* split current window in two parts, horizontally */
125 case 'S':
126 case Ctrl_S:
127 case 's':
128 CHECK_CMDWIN
129#ifdef FEAT_VISUAL
130 reset_VIsual_and_resel(); /* stop Visual mode */
131#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000132#ifdef FEAT_QUICKFIX
133 /* When splitting the quickfix window open a new buffer in it,
134 * don't replicate the quickfix buffer. */
135 if (bt_quickfix(curbuf))
136 goto newwindow;
137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#ifdef FEAT_GUI
139 need_mouse_correct = TRUE;
140#endif
141 win_split((int)Prenum, 0);
142 break;
143
144#ifdef FEAT_VERTSPLIT
145/* split current window in two parts, vertically */
146 case Ctrl_V:
147 case 'v':
148 CHECK_CMDWIN
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000149# ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000151# endif
152# ifdef FEAT_QUICKFIX
153 /* When splitting the quickfix window open a new buffer in it,
154 * don't replicate the quickfix buffer. */
155 if (bt_quickfix(curbuf))
156 goto newwindow;
157# endif
158# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 win_split((int)Prenum, WSP_VERT);
162 break;
163#endif
164
165/* split current window and edit alternate file */
166 case Ctrl_HAT:
167 case '^':
168 CHECK_CMDWIN
169#ifdef FEAT_VISUAL
170 reset_VIsual_and_resel(); /* stop Visual mode */
171#endif
172 STRCPY(cbuf, "split #");
173 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000174 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
175 "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 do_cmdline_cmd(cbuf);
177 break;
178
179/* open new window */
180 case Ctrl_N:
181 case 'n':
182 CHECK_CMDWIN
183#ifdef FEAT_VISUAL
184 reset_VIsual_and_resel(); /* stop Visual mode */
185#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000186#ifdef FEAT_QUICKFIX
187newwindow:
188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000190 /* window height */
191 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 else
193 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000194#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
195 if (nchar == 'v' || nchar == Ctrl_V)
196 STRCAT(cbuf, "v");
197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 STRCAT(cbuf, "new");
199 do_cmdline_cmd(cbuf);
200 break;
201
202/* quit current window */
203 case Ctrl_Q:
204 case 'q':
205#ifdef FEAT_VISUAL
206 reset_VIsual_and_resel(); /* stop Visual mode */
207#endif
208 do_cmdline_cmd((char_u *)"quit");
209 break;
210
211/* close current window */
212 case Ctrl_C:
213 case 'c':
214#ifdef FEAT_VISUAL
215 reset_VIsual_and_resel(); /* stop Visual mode */
216#endif
217 do_cmdline_cmd((char_u *)"close");
218 break;
219
220#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
221/* close preview window */
222 case Ctrl_Z:
223 case 'z':
224 CHECK_CMDWIN
225#ifdef FEAT_VISUAL
226 reset_VIsual_and_resel(); /* stop Visual mode */
227#endif
228 do_cmdline_cmd((char_u *)"pclose");
229 break;
230
231/* cursor to preview window */
232 case 'P':
233 for (wp = firstwin; wp != NULL; wp = wp->w_next)
234 if (wp->w_p_pvw)
235 break;
236 if (wp == NULL)
237 EMSG(_("E441: There is no preview window"));
238 else
239 win_goto(wp);
240 break;
241#endif
242
243/* close all but current window */
244 case Ctrl_O:
245 case 'o':
246 CHECK_CMDWIN
247#ifdef FEAT_VISUAL
248 reset_VIsual_and_resel(); /* stop Visual mode */
249#endif
250 do_cmdline_cmd((char_u *)"only");
251 break;
252
253/* cursor to next window with wrap around */
254 case Ctrl_W:
255 case 'w':
256/* cursor to previous window with wrap around */
257 case 'W':
258 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000259 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 beep_flush();
261 else
262 {
263 if (Prenum) /* go to specified window */
264 {
265 for (wp = firstwin; --Prenum > 0; )
266 {
267 if (wp->w_next == NULL)
268 break;
269 else
270 wp = wp->w_next;
271 }
272 }
273 else
274 {
275 if (nchar == 'W') /* go to previous window */
276 {
277 wp = curwin->w_prev;
278 if (wp == NULL)
279 wp = lastwin; /* wrap around */
280 }
281 else /* go to next window */
282 {
283 wp = curwin->w_next;
284 if (wp == NULL)
285 wp = firstwin; /* wrap around */
286 }
287 }
288 win_goto(wp);
289 }
290 break;
291
292/* cursor to window below */
293 case 'j':
294 case K_DOWN:
295 case Ctrl_J:
296 CHECK_CMDWIN
297#ifdef FEAT_VERTSPLIT
298 win_goto_ver(FALSE, Prenum1);
299#else
300 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
301 wp = wp->w_next)
302 ;
303 win_goto(wp);
304#endif
305 break;
306
307/* cursor to window above */
308 case 'k':
309 case K_UP:
310 case Ctrl_K:
311 CHECK_CMDWIN
312#ifdef FEAT_VERTSPLIT
313 win_goto_ver(TRUE, Prenum1);
314#else
315 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
316 wp = wp->w_prev)
317 ;
318 win_goto(wp);
319#endif
320 break;
321
322#ifdef FEAT_VERTSPLIT
323/* cursor to left window */
324 case 'h':
325 case K_LEFT:
326 case Ctrl_H:
327 case K_BS:
328 CHECK_CMDWIN
329 win_goto_hor(TRUE, Prenum1);
330 break;
331
332/* cursor to right window */
333 case 'l':
334 case K_RIGHT:
335 case Ctrl_L:
336 CHECK_CMDWIN
337 win_goto_hor(FALSE, Prenum1);
338 break;
339#endif
340
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000341/* move window to new tab page */
342 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000343 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000344 MSG(_(m_onlyone));
345 else
346 {
347 tabpage_T *oldtab = curtab;
348 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000349
350 /* First create a new tab with the window, then go back to
351 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000352 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000353 if (win_new_tabpage((int)Prenum) == OK
354 && valid_tabpage(oldtab))
355 {
356 newtab = curtab;
357 goto_tabpage_tp(oldtab);
358 if (curwin == wp)
359 win_close(curwin, FALSE);
360 if (valid_tabpage(newtab))
361 goto_tabpage_tp(newtab);
362 }
363 }
364 break;
365
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366/* cursor to top-left window */
367 case 't':
368 case Ctrl_T:
369 win_goto(firstwin);
370 break;
371
372/* cursor to bottom-right window */
373 case 'b':
374 case Ctrl_B:
375 win_goto(lastwin);
376 break;
377
378/* cursor to last accessed (previous) window */
379 case 'p':
380 case Ctrl_P:
381 if (prevwin == NULL)
382 beep_flush();
383 else
384 win_goto(prevwin);
385 break;
386
387/* exchange current and next window */
388 case 'x':
389 case Ctrl_X:
390 CHECK_CMDWIN
391 win_exchange(Prenum);
392 break;
393
394/* rotate windows downwards */
395 case Ctrl_R:
396 case 'r':
397 CHECK_CMDWIN
398#ifdef FEAT_VISUAL
399 reset_VIsual_and_resel(); /* stop Visual mode */
400#endif
401 win_rotate(FALSE, (int)Prenum1); /* downwards */
402 break;
403
404/* rotate windows upwards */
405 case 'R':
406 CHECK_CMDWIN
407#ifdef FEAT_VISUAL
408 reset_VIsual_and_resel(); /* stop Visual mode */
409#endif
410 win_rotate(TRUE, (int)Prenum1); /* upwards */
411 break;
412
413/* move window to the very top/bottom/left/right */
414 case 'K':
415 case 'J':
416#ifdef FEAT_VERTSPLIT
417 case 'H':
418 case 'L':
419#endif
420 CHECK_CMDWIN
421 win_totop((int)Prenum,
422 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
423 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
424 break;
425
426/* make all windows the same height */
427 case '=':
428#ifdef FEAT_GUI
429 need_mouse_correct = TRUE;
430#endif
431 win_equal(NULL, FALSE, 'b');
432 break;
433
434/* increase current window height */
435 case '+':
436#ifdef FEAT_GUI
437 need_mouse_correct = TRUE;
438#endif
439 win_setheight(curwin->w_height + (int)Prenum1);
440 break;
441
442/* decrease current window height */
443 case '-':
444#ifdef FEAT_GUI
445 need_mouse_correct = TRUE;
446#endif
447 win_setheight(curwin->w_height - (int)Prenum1);
448 break;
449
450/* set current window height */
451 case Ctrl__:
452 case '_':
453#ifdef FEAT_GUI
454 need_mouse_correct = TRUE;
455#endif
456 win_setheight(Prenum ? (int)Prenum : 9999);
457 break;
458
459#ifdef FEAT_VERTSPLIT
460/* increase current window width */
461 case '>':
462#ifdef FEAT_GUI
463 need_mouse_correct = TRUE;
464#endif
465 win_setwidth(curwin->w_width + (int)Prenum1);
466 break;
467
468/* decrease current window width */
469 case '<':
470#ifdef FEAT_GUI
471 need_mouse_correct = TRUE;
472#endif
473 win_setwidth(curwin->w_width - (int)Prenum1);
474 break;
475
476/* set current window width */
477 case '|':
478#ifdef FEAT_GUI
479 need_mouse_correct = TRUE;
480#endif
481 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
482 break;
483#endif
484
485/* jump to tag and split window if tag exists (in preview window) */
486#if defined(FEAT_QUICKFIX)
487 case '}':
488 CHECK_CMDWIN
489 if (Prenum)
490 g_do_tagpreview = Prenum;
491 else
492 g_do_tagpreview = p_pvh;
493 /*FALLTHROUGH*/
494#endif
495 case ']':
496 case Ctrl_RSB:
497 CHECK_CMDWIN
498#ifdef FEAT_VISUAL
499 reset_VIsual_and_resel(); /* stop Visual mode */
500#endif
501 if (Prenum)
502 postponed_split = Prenum;
503 else
504 postponed_split = -1;
505
506 /* Execute the command right here, required when
507 * "wincmd ]" was used in a function. */
508 do_nv_ident(Ctrl_RSB, NUL);
509 break;
510
511#ifdef FEAT_SEARCHPATH
512/* edit file name under cursor in a new window */
513 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000514 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000516wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000518
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000519 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 if (ptr != NULL)
521 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000522# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000524# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 setpcmark();
526 if (win_split(0, 0) == OK)
527 {
528# ifdef FEAT_SCROLLBIND
529 curwin->w_p_scb = FALSE;
530# endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000531 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
532 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000533 if (nchar == 'F' && lnum >= 0)
534 {
535 curwin->w_cursor.lnum = lnum;
536 check_cursor_lnum();
537 beginline(BL_SOL | BL_FIX);
538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 }
540 vim_free(ptr);
541 }
542 break;
543#endif
544
545#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000546/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 * new window -- webb
548 */
549 case 'i': /* Go to any match */
550 case Ctrl_I:
551 type = FIND_ANY;
552 /* FALLTHROUGH */
553 case 'd': /* Go to definition, using 'define' */
554 case Ctrl_D:
555 CHECK_CMDWIN
556 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
557 break;
558 find_pattern_in_path(ptr, 0, len, TRUE,
559 Prenum == 0 ? TRUE : FALSE, type,
560 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
561 curwin->w_set_curswant = TRUE;
562 break;
563#endif
564
Bram Moolenaar05159a02005-02-26 23:04:13 +0000565 case K_KENTER:
566 case CAR:
567#if defined(FEAT_QUICKFIX)
568 /*
569 * In a quickfix window a <CR> jumps to the error under the
570 * cursor in a new window.
571 */
572 if (bt_quickfix(curbuf))
573 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000574 sprintf((char *)cbuf, "split +%ld%s",
575 (long)curwin->w_cursor.lnum,
576 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000577 do_cmdline_cmd(cbuf);
578 }
579#endif
580 break;
581
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583/* CTRL-W g extended commands */
584 case 'g':
585 case Ctrl_G:
586 CHECK_CMDWIN
587#ifdef USE_ON_FLY_SCROLL
588 dont_scroll = TRUE; /* disallow scrolling here */
589#endif
590 ++no_mapping;
591 ++allow_keys; /* no mapping for xchar, but allow key codes */
592 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000593 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 --no_mapping;
596 --allow_keys;
597#ifdef FEAT_CMDL_INFO
598 (void)add_to_showcmd(xchar);
599#endif
600 switch (xchar)
601 {
602#if defined(FEAT_QUICKFIX)
603 case '}':
604 xchar = Ctrl_RSB;
605 if (Prenum)
606 g_do_tagpreview = Prenum;
607 else
608 g_do_tagpreview = p_pvh;
609 /*FALLTHROUGH*/
610#endif
611 case ']':
612 case Ctrl_RSB:
613#ifdef FEAT_VISUAL
614 reset_VIsual_and_resel(); /* stop Visual mode */
615#endif
616 if (Prenum)
617 postponed_split = Prenum;
618 else
619 postponed_split = -1;
620
621 /* Execute the command right here, required when
622 * "wincmd g}" was used in a function. */
623 do_nv_ident('g', xchar);
624 break;
625
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000626#ifdef FEAT_SEARCHPATH
627 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000628 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100629 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000630 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000631 goto wingotofile;
632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 default:
634 beep_flush();
635 break;
636 }
637 break;
638
639 default: beep_flush();
640 break;
641 }
642}
643
644/*
645 * split the current window, implements CTRL-W s and :split
646 *
647 * "size" is the height or width for the new window, 0 to use half of current
648 * height or width.
649 *
650 * "flags":
651 * WSP_ROOM: require enough room for new window
652 * WSP_VERT: vertical split.
653 * WSP_TOP: open window at the top-left of the shell (help window).
654 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
655 * WSP_HELP: creating the help window, keep layout snapshot
656 *
657 * return FAIL for failure, OK otherwise
658 */
659 int
660win_split(size, flags)
661 int size;
662 int flags;
663{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000664 /* When the ":tab" modifier was used open a new tab page instead. */
665 if (may_open_tabpage() == OK)
666 return OK;
667
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 /* Add flags from ":vertical", ":topleft" and ":botright". */
669 flags |= cmdmod.split;
670 if ((flags & WSP_TOP) && (flags & WSP_BOT))
671 {
672 EMSG(_("E442: Can't split topleft and botright at the same time"));
673 return FAIL;
674 }
675
676 /* When creating the help window make a snapshot of the window layout.
677 * Otherwise clear the snapshot, it's now invalid. */
678 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000679 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000681 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682
683 return win_split_ins(size, flags, NULL, 0);
684}
685
686/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000687 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 * When "newwin" is not NULL: insert this window at the far
689 * top/left/right/bottom.
690 * return FAIL for failure, OK otherwise
691 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000692 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693win_split_ins(size, flags, newwin, dir)
694 int size;
695 int flags;
696 win_T *newwin;
697 int dir;
698{
699 win_T *wp = newwin;
700 win_T *oldwin;
701 int new_size = size;
702 int i;
703 int need_status = 0;
704 int do_equal = FALSE;
705 int needed;
706 int available;
707 int oldwin_height = 0;
708 int layout;
709 frame_T *frp, *curfrp;
710 int before;
711
712 if (flags & WSP_TOP)
713 oldwin = firstwin;
714 else if (flags & WSP_BOT)
715 oldwin = lastwin;
716 else
717 oldwin = curwin;
718
719 /* add a status line when p_ls == 1 and splitting the first window */
720 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
721 {
722 if (oldwin->w_height <= p_wmh && newwin == NULL)
723 {
724 EMSG(_(e_noroom));
725 return FAIL;
726 }
727 need_status = STATUS_HEIGHT;
728 }
729
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000730#ifdef FEAT_GUI
731 /* May be needed for the scrollbars that are going to change. */
732 if (gui.in_use)
733 out_flush();
734#endif
735
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#ifdef FEAT_VERTSPLIT
737 if (flags & WSP_VERT)
738 {
739 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
741 /*
742 * Check if we are able to split the current window and compute its
743 * width.
744 */
745 needed = p_wmw + 1;
746 if (flags & WSP_ROOM)
747 needed += p_wiw - p_wmw;
748 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
749 {
750 available = topframe->fr_width;
751 needed += frame_minwidth(topframe, NULL);
752 }
753 else
754 available = oldwin->w_width;
755 if (available < needed && newwin == NULL)
756 {
757 EMSG(_(e_noroom));
758 return FAIL;
759 }
760 if (new_size == 0)
761 new_size = oldwin->w_width / 2;
762 if (new_size > oldwin->w_width - p_wmw - 1)
763 new_size = oldwin->w_width - p_wmw - 1;
764 if (new_size < p_wmw)
765 new_size = p_wmw;
766
767 /* if it doesn't fit in the current window, need win_equal() */
768 if (oldwin->w_width - new_size - 1 < p_wmw)
769 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000770
771 /* We don't like to take lines for the new window from a
772 * 'winfixwidth' window. Take them from a window to the left or right
773 * instead, if possible. */
774 if (oldwin->w_p_wfw)
775 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000776
777 /* Only make all windows the same width if one of them (except oldwin)
778 * is wider than one of the split windows. */
779 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
780 && oldwin->w_frame->fr_parent != NULL)
781 {
782 frp = oldwin->w_frame->fr_parent->fr_child;
783 while (frp != NULL)
784 {
785 if (frp->fr_win != oldwin && frp->fr_win != NULL
786 && (frp->fr_win->w_width > new_size
787 || frp->fr_win->w_width > oldwin->w_width
788 - new_size - STATUS_HEIGHT))
789 {
790 do_equal = TRUE;
791 break;
792 }
793 frp = frp->fr_next;
794 }
795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 }
797 else
798#endif
799 {
800 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
802 /*
803 * Check if we are able to split the current window and compute its
804 * height.
805 */
806 needed = p_wmh + STATUS_HEIGHT + need_status;
807 if (flags & WSP_ROOM)
808 needed += p_wh - p_wmh;
809 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
810 {
811 available = topframe->fr_height;
812 needed += frame_minheight(topframe, NULL);
813 }
814 else
815 {
816 available = oldwin->w_height;
817 needed += p_wmh;
818 }
819 if (available < needed && newwin == NULL)
820 {
821 EMSG(_(e_noroom));
822 return FAIL;
823 }
824 oldwin_height = oldwin->w_height;
825 if (need_status)
826 {
827 oldwin->w_status_height = STATUS_HEIGHT;
828 oldwin_height -= STATUS_HEIGHT;
829 }
830 if (new_size == 0)
831 new_size = oldwin_height / 2;
832
833 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
834 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
835 if (new_size < p_wmh)
836 new_size = p_wmh;
837
838 /* if it doesn't fit in the current window, need win_equal() */
839 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
840 do_equal = TRUE;
841
842 /* We don't like to take lines for the new window from a
843 * 'winfixheight' window. Take them from a window above or below
844 * instead, if possible. */
845 if (oldwin->w_p_wfh)
846 {
847 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
848 oldwin);
849 oldwin_height = oldwin->w_height;
850 if (need_status)
851 oldwin_height -= STATUS_HEIGHT;
852 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000853
854 /* Only make all windows the same height if one of them (except oldwin)
855 * is higher than one of the split windows. */
856 if (!do_equal && p_ea && size == 0
857#ifdef FEAT_VERTSPLIT
858 && *p_ead != 'h'
859#endif
860 && oldwin->w_frame->fr_parent != NULL)
861 {
862 frp = oldwin->w_frame->fr_parent->fr_child;
863 while (frp != NULL)
864 {
865 if (frp->fr_win != oldwin && frp->fr_win != NULL
866 && (frp->fr_win->w_height > new_size
867 || frp->fr_win->w_height > oldwin_height - new_size
868 - STATUS_HEIGHT))
869 {
870 do_equal = TRUE;
871 break;
872 }
873 frp = frp->fr_next;
874 }
875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 }
877
878 /*
879 * allocate new window structure and link it in the window list
880 */
881 if ((flags & WSP_TOP) == 0
882 && ((flags & WSP_BOT)
883 || (flags & WSP_BELOW)
884 || (!(flags & WSP_ABOVE)
885 && (
886#ifdef FEAT_VERTSPLIT
887 (flags & WSP_VERT) ? p_spr :
888#endif
889 p_sb))))
890 {
891 /* new window below/right of current one */
892 if (newwin == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000893 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 else
895 win_append(oldwin, wp);
896 }
897 else
898 {
899 if (newwin == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000900 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 else
902 win_append(oldwin->w_prev, wp);
903 }
904
905 if (newwin == NULL)
906 {
907 if (wp == NULL)
908 return FAIL;
909
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000910 new_frame(wp);
911 if (wp->w_frame == NULL)
912 {
913 win_free(wp, NULL);
914 return FAIL;
915 }
916
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000917 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000918 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
920
921 /*
922 * Reorganise the tree of frames to insert the new window.
923 */
924 if (flags & (WSP_TOP | WSP_BOT))
925 {
926#ifdef FEAT_VERTSPLIT
927 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
928 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
929#else
930 if (topframe->fr_layout == FR_COL)
931#endif
932 {
933 curfrp = topframe->fr_child;
934 if (flags & WSP_BOT)
935 while (curfrp->fr_next != NULL)
936 curfrp = curfrp->fr_next;
937 }
938 else
939 curfrp = topframe;
940 before = (flags & WSP_TOP);
941 }
942 else
943 {
944 curfrp = oldwin->w_frame;
945 if (flags & WSP_BELOW)
946 before = FALSE;
947 else if (flags & WSP_ABOVE)
948 before = TRUE;
949 else
950#ifdef FEAT_VERTSPLIT
951 if (flags & WSP_VERT)
952 before = !p_spr;
953 else
954#endif
955 before = !p_sb;
956 }
957 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
958 {
959 /* Need to create a new frame in the tree to make a branch. */
960 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
961 *frp = *curfrp;
962 curfrp->fr_layout = layout;
963 frp->fr_parent = curfrp;
964 frp->fr_next = NULL;
965 frp->fr_prev = NULL;
966 curfrp->fr_child = frp;
967 curfrp->fr_win = NULL;
968 curfrp = frp;
969 if (frp->fr_win != NULL)
970 oldwin->w_frame = frp;
971 else
972 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
973 frp->fr_parent = curfrp;
974 }
975
976 if (newwin == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000977 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 else
979 frp = newwin->w_frame;
980 frp->fr_parent = curfrp->fr_parent;
981
982 /* Insert the new frame at the right place in the frame list. */
983 if (before)
984 frame_insert(curfrp, frp);
985 else
986 frame_append(curfrp, frp);
987
988#ifdef FEAT_VERTSPLIT
989 if (flags & WSP_VERT)
990 {
991 wp->w_p_scr = curwin->w_p_scr;
992 if (need_status)
993 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +0100994 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 oldwin->w_status_height = need_status;
996 }
997 if (flags & (WSP_TOP | WSP_BOT))
998 {
999 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001000 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001001 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 wp->w_status_height = (p_ls > 0);
1003 }
1004 else
1005 {
1006 /* height and row of new window is same as current window */
1007 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001008 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 wp->w_status_height = oldwin->w_status_height;
1010 }
1011 frp->fr_height = curfrp->fr_height;
1012
1013 /* "new_size" of the current window goes to the new window, use
1014 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001015 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (before)
1017 wp->w_vsep_width = 1;
1018 else
1019 {
1020 wp->w_vsep_width = oldwin->w_vsep_width;
1021 oldwin->w_vsep_width = 1;
1022 }
1023 if (flags & (WSP_TOP | WSP_BOT))
1024 {
1025 if (flags & WSP_BOT)
1026 frame_add_vsep(curfrp);
1027 /* Set width of neighbor frame */
1028 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001029 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1030 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
1032 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001033 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (before) /* new window left of current one */
1035 {
1036 wp->w_wincol = oldwin->w_wincol;
1037 oldwin->w_wincol += new_size + 1;
1038 }
1039 else /* new window right of current one */
1040 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1041 frame_fix_width(oldwin);
1042 frame_fix_width(wp);
1043 }
1044 else
1045#endif
1046 {
1047 /* width and column of new window is same as current window */
1048#ifdef FEAT_VERTSPLIT
1049 if (flags & (WSP_TOP | WSP_BOT))
1050 {
1051 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001052 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 wp->w_vsep_width = 0;
1054 }
1055 else
1056 {
1057 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001058 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 wp->w_vsep_width = oldwin->w_vsep_width;
1060 }
1061 frp->fr_width = curfrp->fr_width;
1062#endif
1063
1064 /* "new_size" of the current window goes to the new window, use
1065 * one row for the status line */
1066 win_new_height(wp, new_size);
1067 if (flags & (WSP_TOP | WSP_BOT))
1068 frame_new_height(curfrp, curfrp->fr_height
1069 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1070 else
1071 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1072 if (before) /* new window above current one */
1073 {
1074 wp->w_winrow = oldwin->w_winrow;
1075 wp->w_status_height = STATUS_HEIGHT;
1076 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1077 }
1078 else /* new window below current one */
1079 {
1080 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1081 wp->w_status_height = oldwin->w_status_height;
1082 oldwin->w_status_height = STATUS_HEIGHT;
1083 }
1084#ifdef FEAT_VERTSPLIT
1085 if (flags & WSP_BOT)
1086 frame_add_statusline(curfrp);
1087#endif
1088 frame_fix_height(wp);
1089 frame_fix_height(oldwin);
1090 }
1091
1092 if (flags & (WSP_TOP | WSP_BOT))
1093 (void)win_comp_pos();
1094
1095 /*
1096 * Both windows need redrawing
1097 */
1098 redraw_win_later(wp, NOT_VALID);
1099 wp->w_redr_status = TRUE;
1100 redraw_win_later(oldwin, NOT_VALID);
1101 oldwin->w_redr_status = TRUE;
1102
1103 if (need_status)
1104 {
1105 msg_row = Rows - 1;
1106 msg_col = sc_col;
1107 msg_clr_eos_force(); /* Old command/ruler may still be there */
1108 comp_col();
1109 msg_row = Rows - 1;
1110 msg_col = 0; /* put position back at start of line */
1111 }
1112
1113 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001114 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 */
1116 if (do_equal || dir != 0)
1117 win_equal(wp, TRUE,
1118#ifdef FEAT_VERTSPLIT
1119 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1120 : dir == 'h' ? 'b' :
1121#endif
1122 'v');
1123
1124 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1125 * size was given. */
1126#ifdef FEAT_VERTSPLIT
1127 if (flags & WSP_VERT)
1128 {
1129 i = p_wiw;
1130 if (size != 0)
1131 p_wiw = size;
1132
1133# ifdef FEAT_GUI
1134 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1135 if (gui.in_use)
1136 gui_init_which_components(NULL);
1137# endif
1138 }
1139 else
1140#endif
1141 {
1142 i = p_wh;
1143 if (size != 0)
1144 p_wh = size;
1145 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001146
1147 /*
1148 * make the new window the current window
1149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 win_enter(wp, FALSE);
1151#ifdef FEAT_VERTSPLIT
1152 if (flags & WSP_VERT)
1153 p_wiw = i;
1154 else
1155#endif
1156 p_wh = i;
1157
1158 return OK;
1159}
1160
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001161
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001162/*
1163 * Initialize window "newp" from window "oldp".
1164 * Used when splitting a window and when creating a new tab page.
1165 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001166 * WSP_NEWLOC may be specified in flags to prevent the location list from
1167 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001168 */
1169 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001170win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001171 win_T *newp;
1172 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001173 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001174{
1175 int i;
1176
1177 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001178#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001179 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001180#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001181 oldp->w_buffer->b_nwindows++;
1182 newp->w_cursor = oldp->w_cursor;
1183 newp->w_valid = 0;
1184 newp->w_curswant = oldp->w_curswant;
1185 newp->w_set_curswant = oldp->w_set_curswant;
1186 newp->w_topline = oldp->w_topline;
1187#ifdef FEAT_DIFF
1188 newp->w_topfill = oldp->w_topfill;
1189#endif
1190 newp->w_leftcol = oldp->w_leftcol;
1191 newp->w_pcmark = oldp->w_pcmark;
1192 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1193 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001194 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001195 newp->w_fraction = oldp->w_fraction;
1196 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1197#ifdef FEAT_JUMPLIST
1198 copy_jumplist(oldp, newp);
1199#endif
1200#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001201 if (flags & WSP_NEWLOC)
1202 {
1203 /* Don't copy the location list. */
1204 newp->w_llist = NULL;
1205 newp->w_llist_ref = NULL;
1206 }
1207 else
1208 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001209#endif
1210 if (oldp->w_localdir != NULL)
1211 newp->w_localdir = vim_strsave(oldp->w_localdir);
1212
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001213 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001214 for (i = 0; i < oldp->w_tagstacklen; i++)
1215 {
1216 newp->w_tagstack[i] = oldp->w_tagstack[i];
1217 if (newp->w_tagstack[i].tagname != NULL)
1218 newp->w_tagstack[i].tagname =
1219 vim_strsave(newp->w_tagstack[i].tagname);
1220 }
1221 newp->w_tagstackidx = oldp->w_tagstackidx;
1222 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001223# ifdef FEAT_FOLDING
1224 copyFoldingState(oldp, newp);
1225# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001226
1227 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001228
1229# ifdef FEAT_SYN_HL
1230 check_colorcolumn(newp);
1231# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001232}
1233
1234/*
1235 * Initialize window "newp" from window"old".
1236 * Only the essential things are copied.
1237 */
1238 static void
1239win_init_some(newp, oldp)
1240 win_T *newp;
1241 win_T *oldp;
1242{
1243 /* Use the same argument list. */
1244 newp->w_alist = oldp->w_alist;
1245 ++newp->w_alist->al_refcount;
1246 newp->w_arg_idx = oldp->w_arg_idx;
1247
1248 /* copy options from existing window */
1249 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001250}
1251
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252#endif /* FEAT_WINDOWS */
1253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254#if defined(FEAT_WINDOWS) || defined(PROTO)
1255/*
1256 * Check if "win" is a pointer to an existing window.
1257 */
1258 int
1259win_valid(win)
1260 win_T *win;
1261{
1262 win_T *wp;
1263
1264 if (win == NULL)
1265 return FALSE;
1266 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1267 if (wp == win)
1268 return TRUE;
1269 return FALSE;
1270}
1271
1272/*
1273 * Return the number of windows.
1274 */
1275 int
1276win_count()
1277{
1278 win_T *wp;
1279 int count = 0;
1280
1281 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1282 ++count;
1283 return count;
1284}
1285
1286/*
1287 * Make "count" windows on the screen.
1288 * Return actual number of windows on the screen.
1289 * Must be called when there is just one window, filling the whole screen
1290 * (excluding the command line).
1291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 int
1293make_windows(count, vertical)
1294 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001295 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 int maxcount;
1298 int todo;
1299
1300#ifdef FEAT_VERTSPLIT
1301 if (vertical)
1302 {
1303 /* Each windows needs at least 'winminwidth' lines and a separator
1304 * column. */
1305 maxcount = (curwin->w_width + curwin->w_vsep_width
1306 - (p_wiw - p_wmw)) / (p_wmw + 1);
1307 }
1308 else
1309#endif
1310 {
1311 /* Each window needs at least 'winminheight' lines and a status line. */
1312 maxcount = (curwin->w_height + curwin->w_status_height
1313 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1314 }
1315
1316 if (maxcount < 2)
1317 maxcount = 2;
1318 if (count > maxcount)
1319 count = maxcount;
1320
1321 /*
1322 * add status line now, otherwise first window will be too big
1323 */
1324 if (count > 1)
1325 last_status(TRUE);
1326
1327#ifdef FEAT_AUTOCMD
1328 /*
1329 * Don't execute autocommands while creating the windows. Must do that
1330 * when putting the buffers in the windows.
1331 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001332 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333#endif
1334
1335 /* todo is number of windows left to create */
1336 for (todo = count - 1; todo > 0; --todo)
1337#ifdef FEAT_VERTSPLIT
1338 if (vertical)
1339 {
1340 if (win_split(curwin->w_width - (curwin->w_width - todo)
1341 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1342 break;
1343 }
1344 else
1345#endif
1346 {
1347 if (win_split(curwin->w_height - (curwin->w_height - todo
1348 * STATUS_HEIGHT) / (todo + 1)
1349 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1350 break;
1351 }
1352
1353#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001354 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355#endif
1356
1357 /* return actual number of windows */
1358 return (count - todo);
1359}
1360
1361/*
1362 * Exchange current and next window
1363 */
1364 static void
1365win_exchange(Prenum)
1366 long Prenum;
1367{
1368 frame_T *frp;
1369 frame_T *frp2;
1370 win_T *wp;
1371 win_T *wp2;
1372 int temp;
1373
1374 if (lastwin == firstwin) /* just one window */
1375 {
1376 beep_flush();
1377 return;
1378 }
1379
1380#ifdef FEAT_GUI
1381 need_mouse_correct = TRUE;
1382#endif
1383
1384 /*
1385 * find window to exchange with
1386 */
1387 if (Prenum)
1388 {
1389 frp = curwin->w_frame->fr_parent->fr_child;
1390 while (frp != NULL && --Prenum > 0)
1391 frp = frp->fr_next;
1392 }
1393 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1394 frp = curwin->w_frame->fr_next;
1395 else /* Swap last window in row/col with previous */
1396 frp = curwin->w_frame->fr_prev;
1397
1398 /* We can only exchange a window with another window, not with a frame
1399 * containing windows. */
1400 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1401 return;
1402 wp = frp->fr_win;
1403
1404/*
1405 * 1. remove curwin from the list. Remember after which window it was in wp2
1406 * 2. insert curwin before wp in the list
1407 * if wp != wp2
1408 * 3. remove wp from the list
1409 * 4. insert wp after wp2
1410 * 5. exchange the status line height and vsep width.
1411 */
1412 wp2 = curwin->w_prev;
1413 frp2 = curwin->w_frame->fr_prev;
1414 if (wp->w_prev != curwin)
1415 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001416 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 frame_remove(curwin->w_frame);
1418 win_append(wp->w_prev, curwin);
1419 frame_insert(frp, curwin->w_frame);
1420 }
1421 if (wp != wp2)
1422 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001423 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 frame_remove(wp->w_frame);
1425 win_append(wp2, wp);
1426 if (frp2 == NULL)
1427 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1428 else
1429 frame_append(frp2, wp->w_frame);
1430 }
1431 temp = curwin->w_status_height;
1432 curwin->w_status_height = wp->w_status_height;
1433 wp->w_status_height = temp;
1434#ifdef FEAT_VERTSPLIT
1435 temp = curwin->w_vsep_width;
1436 curwin->w_vsep_width = wp->w_vsep_width;
1437 wp->w_vsep_width = temp;
1438
1439 /* If the windows are not in the same frame, exchange the sizes to avoid
1440 * messing up the window layout. Otherwise fix the frame sizes. */
1441 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1442 {
1443 temp = curwin->w_height;
1444 curwin->w_height = wp->w_height;
1445 wp->w_height = temp;
1446 temp = curwin->w_width;
1447 curwin->w_width = wp->w_width;
1448 wp->w_width = temp;
1449 }
1450 else
1451 {
1452 frame_fix_height(curwin);
1453 frame_fix_height(wp);
1454 frame_fix_width(curwin);
1455 frame_fix_width(wp);
1456 }
1457#endif
1458
1459 (void)win_comp_pos(); /* recompute window positions */
1460
1461 win_enter(wp, TRUE);
1462 redraw_later(CLEAR);
1463}
1464
1465/*
1466 * rotate windows: if upwards TRUE the second window becomes the first one
1467 * if upwards FALSE the first window becomes the second one
1468 */
1469 static void
1470win_rotate(upwards, count)
1471 int upwards;
1472 int count;
1473{
1474 win_T *wp1;
1475 win_T *wp2;
1476 frame_T *frp;
1477 int n;
1478
1479 if (firstwin == lastwin) /* nothing to do */
1480 {
1481 beep_flush();
1482 return;
1483 }
1484
1485#ifdef FEAT_GUI
1486 need_mouse_correct = TRUE;
1487#endif
1488
1489#ifdef FEAT_VERTSPLIT
1490 /* Check if all frames in this row/col have one window. */
1491 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1492 frp = frp->fr_next)
1493 if (frp->fr_win == NULL)
1494 {
1495 EMSG(_("E443: Cannot rotate when another window is split"));
1496 return;
1497 }
1498#endif
1499
1500 while (count--)
1501 {
1502 if (upwards) /* first window becomes last window */
1503 {
1504 /* remove first window/frame from the list */
1505 frp = curwin->w_frame->fr_parent->fr_child;
1506 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001507 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 frame_remove(frp);
1509
1510 /* find last frame and append removed window/frame after it */
1511 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1512 ;
1513 win_append(frp->fr_win, wp1);
1514 frame_append(frp, wp1->w_frame);
1515
1516 wp2 = frp->fr_win; /* previously last window */
1517 }
1518 else /* last window becomes first window */
1519 {
1520 /* find last window/frame in the list and remove it */
1521 for (frp = curwin->w_frame; frp->fr_next != NULL;
1522 frp = frp->fr_next)
1523 ;
1524 wp1 = frp->fr_win;
1525 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001526 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 frame_remove(frp);
1528
1529 /* append the removed window/frame before the first in the list */
1530 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1531 frame_insert(frp->fr_parent->fr_child, frp);
1532 }
1533
1534 /* exchange status height and vsep width of old and new last window */
1535 n = wp2->w_status_height;
1536 wp2->w_status_height = wp1->w_status_height;
1537 wp1->w_status_height = n;
1538 frame_fix_height(wp1);
1539 frame_fix_height(wp2);
1540#ifdef FEAT_VERTSPLIT
1541 n = wp2->w_vsep_width;
1542 wp2->w_vsep_width = wp1->w_vsep_width;
1543 wp1->w_vsep_width = n;
1544 frame_fix_width(wp1);
1545 frame_fix_width(wp2);
1546#endif
1547
1548 /* recompute w_winrow and w_wincol for all windows */
1549 (void)win_comp_pos();
1550 }
1551
1552 redraw_later(CLEAR);
1553}
1554
1555/*
1556 * Move the current window to the very top/bottom/left/right of the screen.
1557 */
1558 static void
1559win_totop(size, flags)
1560 int size;
1561 int flags;
1562{
1563 int dir;
1564 int height = curwin->w_height;
1565
1566 if (lastwin == firstwin)
1567 {
1568 beep_flush();
1569 return;
1570 }
1571
1572 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001573 (void)winframe_remove(curwin, &dir, NULL);
1574 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 last_status(FALSE); /* may need to remove last status line */
1576 (void)win_comp_pos(); /* recompute window positions */
1577
1578 /* Split a window on the desired side and put the window there. */
1579 (void)win_split_ins(size, flags, curwin, dir);
1580 if (!(flags & WSP_VERT))
1581 {
1582 win_setheight(height);
1583 if (p_ea)
1584 win_equal(curwin, TRUE, 'v');
1585 }
1586
1587#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1588 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1589 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001590 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592}
1593
1594/*
1595 * Move window "win1" to below/right of "win2" and make "win1" the current
1596 * window. Only works within the same frame!
1597 */
1598 void
1599win_move_after(win1, win2)
1600 win_T *win1, *win2;
1601{
1602 int height;
1603
1604 /* check if the arguments are reasonable */
1605 if (win1 == win2)
1606 return;
1607
1608 /* check if there is something to do */
1609 if (win2->w_next != win1)
1610 {
1611 /* may need move the status line/vertical separator of the last window
1612 * */
1613 if (win1 == lastwin)
1614 {
1615 height = win1->w_prev->w_status_height;
1616 win1->w_prev->w_status_height = win1->w_status_height;
1617 win1->w_status_height = height;
1618#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001619 if (win1->w_prev->w_vsep_width == 1)
1620 {
1621 /* Remove the vertical separator from the last-but-one window,
1622 * add it to the last window. Adjust the frame widths. */
1623 win1->w_prev->w_vsep_width = 0;
1624 win1->w_prev->w_frame->fr_width -= 1;
1625 win1->w_vsep_width = 1;
1626 win1->w_frame->fr_width += 1;
1627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628#endif
1629 }
1630 else if (win2 == lastwin)
1631 {
1632 height = win1->w_status_height;
1633 win1->w_status_height = win2->w_status_height;
1634 win2->w_status_height = height;
1635#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001636 if (win1->w_vsep_width == 1)
1637 {
1638 /* Remove the vertical separator from win1, add it to the last
1639 * window, win2. Adjust the frame widths. */
1640 win2->w_vsep_width = 1;
1641 win2->w_frame->fr_width += 1;
1642 win1->w_vsep_width = 0;
1643 win1->w_frame->fr_width -= 1;
1644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645#endif
1646 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001647 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 frame_remove(win1->w_frame);
1649 win_append(win2, win1);
1650 frame_append(win2->w_frame, win1->w_frame);
1651
1652 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1653 redraw_later(NOT_VALID);
1654 }
1655 win_enter(win1, FALSE);
1656}
1657
1658/*
1659 * Make all windows the same height.
1660 * 'next_curwin' will soon be the current window, make sure it has enough
1661 * rows.
1662 */
1663 void
1664win_equal(next_curwin, current, dir)
1665 win_T *next_curwin; /* pointer to current window to be or NULL */
1666 int current; /* do only frame with current window */
1667 int dir; /* 'v' for vertically, 'h' for horizontally,
1668 'b' for both, 0 for using p_ead */
1669{
1670 if (dir == 0)
1671#ifdef FEAT_VERTSPLIT
1672 dir = *p_ead;
1673#else
1674 dir = 'b';
1675#endif
1676 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001677 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001678 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679}
1680
1681/*
1682 * Set a frame to a new position and height, spreading the available room
1683 * equally over contained frames.
1684 * The window "next_curwin" (if not NULL) should at least get the size from
1685 * 'winheight' and 'winwidth' if possible.
1686 */
1687 static void
1688win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1689 win_T *next_curwin; /* pointer to current window to be or NULL */
1690 int current; /* do only frame with current window */
1691 frame_T *topfr; /* frame to set size off */
1692 int dir; /* 'v', 'h' or 'b', see win_equal() */
1693 int col; /* horizontal position for frame */
1694 int row; /* vertical position for frame */
1695 int width; /* new width of frame */
1696 int height; /* new height of frame */
1697{
1698 int n, m;
1699 int extra_sep = 0;
1700 int wincount, totwincount = 0;
1701 frame_T *fr;
1702 int next_curwin_size = 0;
1703 int room = 0;
1704 int new_size;
1705 int has_next_curwin = 0;
1706 int hnc;
1707
1708 if (topfr->fr_layout == FR_LEAF)
1709 {
1710 /* Set the width/height of this frame.
1711 * Redraw when size or position changes */
1712 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1713#ifdef FEAT_VERTSPLIT
1714 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1715#endif
1716 )
1717 {
1718 topfr->fr_win->w_winrow = row;
1719 frame_new_height(topfr, height, FALSE, FALSE);
1720#ifdef FEAT_VERTSPLIT
1721 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001722 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
1724 redraw_all_later(CLEAR);
1725 }
1726 }
1727#ifdef FEAT_VERTSPLIT
1728 else if (topfr->fr_layout == FR_ROW)
1729 {
1730 topfr->fr_width = width;
1731 topfr->fr_height = height;
1732
1733 if (dir != 'v') /* equalize frame widths */
1734 {
1735 /* Compute the maximum number of windows horizontally in this
1736 * frame. */
1737 n = frame_minwidth(topfr, NOWIN);
1738 /* add one for the rightmost window, it doesn't have a separator */
1739 if (col + width == Columns)
1740 extra_sep = 1;
1741 else
1742 extra_sep = 0;
1743 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001744 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001746 /*
1747 * Compute width for "next_curwin" window and room available for
1748 * other windows.
1749 * "m" is the minimal width when counting p_wiw for "next_curwin".
1750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 m = frame_minwidth(topfr, next_curwin);
1752 room = width - m;
1753 if (room < 0)
1754 {
1755 next_curwin_size = p_wiw + room;
1756 room = 0;
1757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 else
1759 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001760 next_curwin_size = -1;
1761 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1762 {
1763 /* If 'winfixwidth' set keep the window width if
1764 * possible.
1765 * Watch out for this window being the next_curwin. */
1766 if (frame_fixed_width(fr))
1767 {
1768 n = frame_minwidth(fr, NOWIN);
1769 new_size = fr->fr_width;
1770 if (frame_has_win(fr, next_curwin))
1771 {
1772 room += p_wiw - p_wmw;
1773 next_curwin_size = 0;
1774 if (new_size < p_wiw)
1775 new_size = p_wiw;
1776 }
1777 else
1778 /* These windows don't use up room. */
1779 totwincount -= (n + (fr->fr_next == NULL
1780 ? extra_sep : 0)) / (p_wmw + 1);
1781 room -= new_size - n;
1782 if (room < 0)
1783 {
1784 new_size += room;
1785 room = 0;
1786 }
1787 fr->fr_newwidth = new_size;
1788 }
1789 }
1790 if (next_curwin_size == -1)
1791 {
1792 if (!has_next_curwin)
1793 next_curwin_size = 0;
1794 else if (totwincount > 1
1795 && (room + (totwincount - 2))
1796 / (totwincount - 1) > p_wiw)
1797 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001798 /* Can make all windows wider than 'winwidth', spread
1799 * the room equally. */
1800 next_curwin_size = (room + p_wiw
1801 + (totwincount - 1) * p_wmw
1802 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001803 room -= next_curwin_size - p_wiw;
1804 }
1805 else
1806 next_curwin_size = p_wiw;
1807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001809
1810 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 --totwincount; /* don't count curwin */
1812 }
1813
1814 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1815 {
1816 n = m = 0;
1817 wincount = 1;
1818 if (fr->fr_next == NULL)
1819 /* last frame gets all that remains (avoid roundoff error) */
1820 new_size = width;
1821 else if (dir == 'v')
1822 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001823 else if (frame_fixed_width(fr))
1824 {
1825 new_size = fr->fr_newwidth;
1826 wincount = 0; /* doesn't count as a sizeable window */
1827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 else
1829 {
1830 /* Compute the maximum number of windows horiz. in "fr". */
1831 n = frame_minwidth(fr, NOWIN);
1832 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1833 / (p_wmw + 1);
1834 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001835 if (has_next_curwin)
1836 hnc = frame_has_win(fr, next_curwin);
1837 else
1838 hnc = FALSE;
1839 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001841 if (totwincount == 0)
1842 new_size = room;
1843 else
1844 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001846 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
1848 next_curwin_size -= p_wiw - (m - n);
1849 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001850 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001852 else
1853 room -= new_size;
1854 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 }
1856
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001857 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 * window, unless equalizing all frames. */
1859 if (!current || dir != 'v' || topfr->fr_parent != NULL
1860 || (new_size != fr->fr_width)
1861 || frame_has_win(fr, next_curwin))
1862 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001863 new_size, height);
1864 col += new_size;
1865 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 totwincount -= wincount;
1867 }
1868 }
1869#endif
1870 else /* topfr->fr_layout == FR_COL */
1871 {
1872#ifdef FEAT_VERTSPLIT
1873 topfr->fr_width = width;
1874#endif
1875 topfr->fr_height = height;
1876
1877 if (dir != 'h') /* equalize frame heights */
1878 {
1879 /* Compute maximum number of windows vertically in this frame. */
1880 n = frame_minheight(topfr, NOWIN);
1881 /* add one for the bottom window if it doesn't have a statusline */
1882 if (row + height == cmdline_row && p_ls == 0)
1883 extra_sep = 1;
1884 else
1885 extra_sep = 0;
1886 totwincount = (n + extra_sep) / (p_wmh + 1);
1887 has_next_curwin = frame_has_win(topfr, next_curwin);
1888
1889 /*
1890 * Compute height for "next_curwin" window and room available for
1891 * other windows.
1892 * "m" is the minimal height when counting p_wh for "next_curwin".
1893 */
1894 m = frame_minheight(topfr, next_curwin);
1895 room = height - m;
1896 if (room < 0)
1897 {
1898 /* The room is less then 'winheight', use all space for the
1899 * current window. */
1900 next_curwin_size = p_wh + room;
1901 room = 0;
1902 }
1903 else
1904 {
1905 next_curwin_size = -1;
1906 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1907 {
1908 /* If 'winfixheight' set keep the window height if
1909 * possible.
1910 * Watch out for this window being the next_curwin. */
1911 if (frame_fixed_height(fr))
1912 {
1913 n = frame_minheight(fr, NOWIN);
1914 new_size = fr->fr_height;
1915 if (frame_has_win(fr, next_curwin))
1916 {
1917 room += p_wh - p_wmh;
1918 next_curwin_size = 0;
1919 if (new_size < p_wh)
1920 new_size = p_wh;
1921 }
1922 else
1923 /* These windows don't use up room. */
1924 totwincount -= (n + (fr->fr_next == NULL
1925 ? extra_sep : 0)) / (p_wmh + 1);
1926 room -= new_size - n;
1927 if (room < 0)
1928 {
1929 new_size += room;
1930 room = 0;
1931 }
1932 fr->fr_newheight = new_size;
1933 }
1934 }
1935 if (next_curwin_size == -1)
1936 {
1937 if (!has_next_curwin)
1938 next_curwin_size = 0;
1939 else if (totwincount > 1
1940 && (room + (totwincount - 2))
1941 / (totwincount - 1) > p_wh)
1942 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001943 /* can make all windows higher than 'winheight',
1944 * spread the room equally. */
1945 next_curwin_size = (room + p_wh
1946 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 + (totwincount - 1)) / totwincount;
1948 room -= next_curwin_size - p_wh;
1949 }
1950 else
1951 next_curwin_size = p_wh;
1952 }
1953 }
1954
1955 if (has_next_curwin)
1956 --totwincount; /* don't count curwin */
1957 }
1958
1959 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1960 {
1961 n = m = 0;
1962 wincount = 1;
1963 if (fr->fr_next == NULL)
1964 /* last frame gets all that remains (avoid roundoff error) */
1965 new_size = height;
1966 else if (dir == 'h')
1967 new_size = fr->fr_height;
1968 else if (frame_fixed_height(fr))
1969 {
1970 new_size = fr->fr_newheight;
1971 wincount = 0; /* doesn't count as a sizeable window */
1972 }
1973 else
1974 {
1975 /* Compute the maximum number of windows vert. in "fr". */
1976 n = frame_minheight(fr, NOWIN);
1977 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1978 / (p_wmh + 1);
1979 m = frame_minheight(fr, next_curwin);
1980 if (has_next_curwin)
1981 hnc = frame_has_win(fr, next_curwin);
1982 else
1983 hnc = FALSE;
1984 if (hnc) /* don't count next_curwin */
1985 --wincount;
1986 if (totwincount == 0)
1987 new_size = room;
1988 else
1989 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1990 / totwincount;
1991 if (hnc) /* add next_curwin size */
1992 {
1993 next_curwin_size -= p_wh - (m - n);
1994 new_size += next_curwin_size;
1995 room -= new_size - next_curwin_size;
1996 }
1997 else
1998 room -= new_size;
1999 new_size += n;
2000 }
2001 /* Skip frame that is full width when splitting or closing a
2002 * window, unless equalizing all frames. */
2003 if (!current || dir != 'h' || topfr->fr_parent != NULL
2004 || (new_size != fr->fr_height)
2005 || frame_has_win(fr, next_curwin))
2006 win_equal_rec(next_curwin, current, fr, dir, col, row,
2007 width, new_size);
2008 row += new_size;
2009 height -= new_size;
2010 totwincount -= wincount;
2011 }
2012 }
2013}
2014
2015/*
2016 * close all windows for buffer 'buf'
2017 */
2018 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002019close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002021 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002023 win_T *wp;
2024 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002025 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
2027 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002028
2029 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002031 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002033 win_close(wp, FALSE);
2034
2035 /* Start all over, autocommands may change the window layout. */
2036 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
2038 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002039 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002041
2042 /* Also check windows in other tab pages. */
2043 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2044 {
2045 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002046 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002047 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2048 if (wp->w_buffer == buf)
2049 {
2050 win_close_othertab(wp, FALSE, tp);
2051
2052 /* Start all over, the tab page may be closed and
2053 * autocommands may change the window layout. */
2054 nexttp = first_tabpage;
2055 break;
2056 }
2057 }
2058
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002060
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002061 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002062 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063}
2064
2065/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002066 * Return TRUE if the current window is the only window that exists (ignoring
2067 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002068 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002069 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002070 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002071last_window()
2072{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002073 return (one_window() && first_tabpage->tp_next == NULL);
2074}
2075
2076/*
2077 * Return TRUE if there is only one window other than "aucmd_win" in the
2078 * current tab page.
2079 */
2080 static int
2081one_window()
2082{
2083#ifdef FEAT_AUTOCMD
2084 win_T *wp;
2085 int seen_one = FALSE;
2086
2087 FOR_ALL_WINDOWS(wp)
2088 {
2089 if (wp != aucmd_win)
2090 {
2091 if (seen_one)
2092 return FALSE;
2093 seen_one = TRUE;
2094 }
2095 }
2096 return TRUE;
2097#else
2098 return firstwin == lastwin;
2099#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002100}
2101
2102/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002103 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 * If "free_buf" is TRUE related buffer may be unloaded.
2105 *
2106 * called by :quit, :close, :xit, :wq and findtag()
2107 */
2108 void
2109win_close(win, free_buf)
2110 win_T *win;
2111 int free_buf;
2112{
2113 win_T *wp;
2114#ifdef FEAT_AUTOCMD
2115 int other_buffer = FALSE;
2116#endif
2117 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 int dir;
2119 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002120 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002122 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 {
2124 EMSG(_("E444: Cannot close last window"));
2125 return;
2126 }
2127
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002128#ifdef FEAT_AUTOCMD
2129 if (win == aucmd_win)
2130 {
2131 EMSG(_("E813: Cannot close autocmd window"));
2132 return;
2133 }
2134 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2135 {
2136 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2137 return;
2138 }
2139#endif
2140
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002141 /*
2142 * When closing the last window in a tab page first go to another tab
2143 * page and then close the window and the tab page. This avoids that
2144 * curwin and curtab are not invalid while we are freeing memory, they may
2145 * be used in GUI events.
2146 */
2147 if (firstwin == lastwin)
2148 {
2149 goto_tabpage_tp(alt_tabpage());
2150 redraw_tabline = TRUE;
2151
2152 /* Safety check: Autocommands may have closed the window when jumping
2153 * to the other tab page. */
2154 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2155 {
2156 int h = tabline_height();
2157
2158 win_close_othertab(win, free_buf, prev_curtab);
2159 if (h != tabline_height())
2160 shell_new_rows();
2161 }
2162 return;
2163 }
2164
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 /* When closing the help window, try restoring a snapshot after closing
2166 * the window. Otherwise clear the snapshot, it's now invalid. */
2167 if (win->w_buffer->b_help)
2168 help_window = TRUE;
2169 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002170 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171
2172#ifdef FEAT_AUTOCMD
2173 if (win == curwin)
2174 {
2175 /*
2176 * Guess which window is going to be the new current window.
2177 * This may change because of the autocommands (sigh).
2178 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002179 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180
2181 /*
2182 * Be careful: If autocommands delete the window, return now.
2183 */
2184 if (wp->w_buffer != curbuf)
2185 {
2186 other_buffer = TRUE;
2187 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002188 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 return;
2190 }
2191 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002192 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 return;
2194# ifdef FEAT_EVAL
2195 /* autocmds may abort script processing */
2196 if (aborting())
2197 return;
2198# endif
2199 }
2200#endif
2201
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002202#ifdef FEAT_GUI
2203 /* Avoid trouble with scrollbars that are going to be deleted in
2204 * win_free(). */
2205 if (gui.in_use)
2206 out_flush();
2207#endif
2208
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 /*
2210 * Close the link to the buffer.
2211 */
2212 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002213
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002215 * other window or moved to another tab page. */
2216 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 return;
2218
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002219 /* Free the memory used for the window. */
2220 wp = win_free_mem(win, &dir, NULL);
2221
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 /* Make sure curwin isn't invalid. It can cause severe trouble when
2223 * printing an error message. For win_equal() curbuf needs to be valid
2224 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002225 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
2227 curwin = wp;
2228#ifdef FEAT_QUICKFIX
2229 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2230 {
2231 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002232 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 * finding another window to go to.
2234 */
2235 for (;;)
2236 {
2237 if (wp->w_next == NULL)
2238 wp = firstwin;
2239 else
2240 wp = wp->w_next;
2241 if (wp == curwin)
2242 break;
2243 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2244 {
2245 curwin = wp;
2246 break;
2247 }
2248 }
2249 }
2250#endif
2251 curbuf = curwin->w_buffer;
2252 close_curwin = TRUE;
2253 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002254 if (p_ea
2255#ifdef FEAT_VERTSPLIT
2256 && (*p_ead == 'b' || *p_ead == dir)
2257#endif
2258 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 win_equal(curwin, TRUE,
2260#ifdef FEAT_VERTSPLIT
2261 dir
2262#else
2263 0
2264#endif
2265 );
2266 else
2267 win_comp_pos();
2268 if (close_curwin)
2269 {
2270 win_enter_ext(wp, FALSE, TRUE);
2271#ifdef FEAT_AUTOCMD
2272 if (other_buffer)
2273 /* careful: after this wp and win may be invalid! */
2274 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2275#endif
2276 }
2277
2278 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002279 * If last window has a status line now and we don't want one,
2280 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 */
2282 last_status(FALSE);
2283
2284 /* After closing the help window, try restoring the window layout from
2285 * before it was opened. */
2286 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002287 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
2289#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2290 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2291 if (gui.in_use && !win_hasvertsplit())
2292 gui_init_which_components(NULL);
2293#endif
2294
2295 redraw_all_later(NOT_VALID);
2296}
2297
2298/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002299 * Close window "win" in tab page "tp", which is not the current tab page.
2300 * This may be the last window ih that tab page and result in closing the tab,
2301 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002302 * Caller must check if buffer is hidden and whether the tabline needs to be
2303 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002304 */
2305 void
2306win_close_othertab(win, free_buf, tp)
2307 win_T *win;
2308 int free_buf;
2309 tabpage_T *tp;
2310{
2311 win_T *wp;
2312 int dir;
2313 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002314 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002315
2316 /* Close the link to the buffer. */
2317 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2318
2319 /* Careful: Autocommands may have closed the tab page or made it the
2320 * current tab page. */
2321 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2322 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002323 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002324 return;
2325
2326 /* Autocommands may have closed the window already. */
2327 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2328 ;
2329 if (wp == NULL)
2330 return;
2331
Bram Moolenaarf740b292006-02-16 22:11:02 +00002332 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002333 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002334 {
2335 if (tp == first_tabpage)
2336 first_tabpage = tp->tp_next;
2337 else
2338 {
2339 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2340 ptp = ptp->tp_next)
2341 ;
2342 if (ptp == NULL)
2343 {
2344 EMSG2(_(e_intern2), "win_close_othertab()");
2345 return;
2346 }
2347 ptp->tp_next = tp->tp_next;
2348 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002349 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002350 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002351
2352 /* Free the memory used for the window. */
2353 win_free_mem(win, &dir, tp);
2354
2355 if (free_tp)
2356 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002357}
2358
2359/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002360 * Free the memory used for a window.
2361 * Returns a pointer to the window that got the freed up space.
2362 */
2363 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002364win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002365 win_T *win;
2366 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002367 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002368{
2369 frame_T *frp;
2370 win_T *wp;
2371
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002372 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002373 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002374 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002375 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002376 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002377
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002378 /* When deleting the current window of another tab page select a new
2379 * current window. */
2380 if (tp != NULL && win == tp->tp_curwin)
2381 tp->tp_curwin = wp;
2382
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002383 return wp;
2384}
2385
2386#if defined(EXITFREE) || defined(PROTO)
2387 void
2388win_free_all()
2389{
2390 int dummy;
2391
Bram Moolenaarf740b292006-02-16 22:11:02 +00002392# ifdef FEAT_WINDOWS
2393 while (first_tabpage->tp_next != NULL)
2394 tabpage_close(TRUE);
2395# endif
2396
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002397# ifdef FEAT_AUTOCMD
2398 if (aucmd_win != NULL)
2399 {
2400 (void)win_free_mem(aucmd_win, &dummy, NULL);
2401 aucmd_win = NULL;
2402 }
2403# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002404
2405 while (firstwin != NULL)
2406 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002407}
2408#endif
2409
2410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 * Remove a window and its frame from the tree of frames.
2412 * Returns a pointer to the window that got the freed up space.
2413 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002414 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002415winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002417 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002418 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419{
2420 frame_T *frp, *frp2, *frp3;
2421 frame_T *frp_close = win->w_frame;
2422 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
2424 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002425 * If there is only one window there is nothing to remove.
2426 */
2427 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2428 return NULL;
2429
2430 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 * Remove the window from its frame.
2432 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002433 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 wp = frame2win(frp2);
2435
2436 /* Remove this frame from the list of frames. */
2437 frame_remove(frp_close);
2438
2439#ifdef FEAT_VERTSPLIT
2440 if (frp_close->fr_parent->fr_layout == FR_COL)
2441 {
2442#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002443 /* When 'winfixheight' is set, try to find another frame in the column
2444 * (as close to the closed frame as possible) to distribute the height
2445 * to. */
2446 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2447 {
2448 frp = frp_close->fr_prev;
2449 frp3 = frp_close->fr_next;
2450 while (frp != NULL || frp3 != NULL)
2451 {
2452 if (frp != NULL)
2453 {
2454 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2455 {
2456 frp2 = frp;
2457 wp = frp->fr_win;
2458 break;
2459 }
2460 frp = frp->fr_prev;
2461 }
2462 if (frp3 != NULL)
2463 {
2464 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2465 {
2466 frp2 = frp3;
2467 wp = frp3->fr_win;
2468 break;
2469 }
2470 frp3 = frp3->fr_next;
2471 }
2472 }
2473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2475 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476#ifdef FEAT_VERTSPLIT
2477 *dirp = 'v';
2478 }
2479 else
2480 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002481 /* When 'winfixwidth' is set, try to find another frame in the column
2482 * (as close to the closed frame as possible) to distribute the width
2483 * to. */
2484 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2485 {
2486 frp = frp_close->fr_prev;
2487 frp3 = frp_close->fr_next;
2488 while (frp != NULL || frp3 != NULL)
2489 {
2490 if (frp != NULL)
2491 {
2492 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2493 {
2494 frp2 = frp;
2495 wp = frp->fr_win;
2496 break;
2497 }
2498 frp = frp->fr_prev;
2499 }
2500 if (frp3 != NULL)
2501 {
2502 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2503 {
2504 frp2 = frp3;
2505 wp = frp3->fr_win;
2506 break;
2507 }
2508 frp3 = frp3->fr_next;
2509 }
2510 }
2511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002513 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 *dirp = 'h';
2515 }
2516#endif
2517
2518 /* If rows/columns go to a window below/right its positions need to be
2519 * updated. Can only be done after the sizes have been updated. */
2520 if (frp2 == frp_close->fr_next)
2521 {
2522 int row = win->w_winrow;
2523 int col = W_WINCOL(win);
2524
2525 frame_comp_pos(frp2, &row, &col);
2526 }
2527
2528 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2529 {
2530 /* There is no other frame in this list, move its info to the parent
2531 * and remove it. */
2532 frp2->fr_parent->fr_layout = frp2->fr_layout;
2533 frp2->fr_parent->fr_child = frp2->fr_child;
2534 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2535 frp->fr_parent = frp2->fr_parent;
2536 frp2->fr_parent->fr_win = frp2->fr_win;
2537 if (frp2->fr_win != NULL)
2538 frp2->fr_win->w_frame = frp2->fr_parent;
2539 frp = frp2->fr_parent;
2540 vim_free(frp2);
2541
2542 frp2 = frp->fr_parent;
2543 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2544 {
2545 /* The frame above the parent has the same layout, have to merge
2546 * the frames into this list. */
2547 if (frp2->fr_child == frp)
2548 frp2->fr_child = frp->fr_child;
2549 frp->fr_child->fr_prev = frp->fr_prev;
2550 if (frp->fr_prev != NULL)
2551 frp->fr_prev->fr_next = frp->fr_child;
2552 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2553 {
2554 frp3->fr_parent = frp2;
2555 if (frp3->fr_next == NULL)
2556 {
2557 frp3->fr_next = frp->fr_next;
2558 if (frp->fr_next != NULL)
2559 frp->fr_next->fr_prev = frp3;
2560 break;
2561 }
2562 }
2563 vim_free(frp);
2564 }
2565 }
2566
2567 return wp;
2568}
2569
2570/*
2571 * Find out which frame is going to get the freed up space when "win" is
2572 * closed.
2573 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2574 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2575 * This makes opening a window and closing it immediately keep the same window
2576 * layout.
2577 */
2578 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002579win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002581 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
2583 frame_T *frp;
2584 int b;
2585
Bram Moolenaarf740b292006-02-16 22:11:02 +00002586 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002587 /* Last window in this tab page, will go to next tab page. */
2588 return alt_tabpage()->tp_curwin->w_frame;
2589
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 frp = win->w_frame;
2591#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002592 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 b = p_spr;
2594 else
2595#endif
2596 b = p_sb;
2597 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2598 return frp->fr_next;
2599 return frp->fr_prev;
2600}
2601
2602/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002603 * Return the tabpage that will be used if the current one is closed.
2604 */
2605 static tabpage_T *
2606alt_tabpage()
2607{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002608 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002609
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002610 /* Use the next tab page if possible. */
2611 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002612 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002613
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002614 /* Find the last but one tab page. */
2615 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2616 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002617 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002618}
2619
2620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 * Find the left-upper window in frame "frp".
2622 */
2623 static win_T *
2624frame2win(frp)
2625 frame_T *frp;
2626{
2627 while (frp->fr_win == NULL)
2628 frp = frp->fr_child;
2629 return frp->fr_win;
2630}
2631
2632/*
2633 * Return TRUE if frame "frp" contains window "wp".
2634 */
2635 static int
2636frame_has_win(frp, wp)
2637 frame_T *frp;
2638 win_T *wp;
2639{
2640 frame_T *p;
2641
2642 if (frp->fr_layout == FR_LEAF)
2643 return frp->fr_win == wp;
2644
2645 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2646 if (frame_has_win(p, wp))
2647 return TRUE;
2648 return FALSE;
2649}
2650
2651/*
2652 * Set a new height for a frame. Recursively sets the height for contained
2653 * frames and windows. Caller must take care of positions.
2654 */
2655 static void
2656frame_new_height(topfrp, height, topfirst, wfh)
2657 frame_T *topfrp;
2658 int height;
2659 int topfirst; /* resize topmost contained frame first */
2660 int wfh; /* obey 'winfixheight' when there is a choice;
2661 may cause the height not to be set */
2662{
2663 frame_T *frp;
2664 int extra_lines;
2665 int h;
2666
2667 if (topfrp->fr_win != NULL)
2668 {
2669 /* Simple case: just one window. */
2670 win_new_height(topfrp->fr_win,
2671 height - topfrp->fr_win->w_status_height);
2672 }
2673#ifdef FEAT_VERTSPLIT
2674 else if (topfrp->fr_layout == FR_ROW)
2675 {
2676 do
2677 {
2678 /* All frames in this row get the same new height. */
2679 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2680 {
2681 frame_new_height(frp, height, topfirst, wfh);
2682 if (frp->fr_height > height)
2683 {
2684 /* Could not fit the windows, make the whole row higher. */
2685 height = frp->fr_height;
2686 break;
2687 }
2688 }
2689 }
2690 while (frp != NULL);
2691 }
2692#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002693 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 {
2695 /* Complicated case: Resize a column of frames. Resize the bottom
2696 * frame first, frames above that when needed. */
2697
2698 frp = topfrp->fr_child;
2699 if (wfh)
2700 /* Advance past frames with one window with 'wfh' set. */
2701 while (frame_fixed_height(frp))
2702 {
2703 frp = frp->fr_next;
2704 if (frp == NULL)
2705 return; /* no frame without 'wfh', give up */
2706 }
2707 if (!topfirst)
2708 {
2709 /* Find the bottom frame of this column */
2710 while (frp->fr_next != NULL)
2711 frp = frp->fr_next;
2712 if (wfh)
2713 /* Advance back for frames with one window with 'wfh' set. */
2714 while (frame_fixed_height(frp))
2715 frp = frp->fr_prev;
2716 }
2717
2718 extra_lines = height - topfrp->fr_height;
2719 if (extra_lines < 0)
2720 {
2721 /* reduce height of contained frames, bottom or top frame first */
2722 while (frp != NULL)
2723 {
2724 h = frame_minheight(frp, NULL);
2725 if (frp->fr_height + extra_lines < h)
2726 {
2727 extra_lines += frp->fr_height - h;
2728 frame_new_height(frp, h, topfirst, wfh);
2729 }
2730 else
2731 {
2732 frame_new_height(frp, frp->fr_height + extra_lines,
2733 topfirst, wfh);
2734 break;
2735 }
2736 if (topfirst)
2737 {
2738 do
2739 frp = frp->fr_next;
2740 while (wfh && frp != NULL && frame_fixed_height(frp));
2741 }
2742 else
2743 {
2744 do
2745 frp = frp->fr_prev;
2746 while (wfh && frp != NULL && frame_fixed_height(frp));
2747 }
2748 /* Increase "height" if we could not reduce enough frames. */
2749 if (frp == NULL)
2750 height -= extra_lines;
2751 }
2752 }
2753 else if (extra_lines > 0)
2754 {
2755 /* increase height of bottom or top frame */
2756 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2757 }
2758 }
2759 topfrp->fr_height = height;
2760}
2761
2762/*
2763 * Return TRUE if height of frame "frp" should not be changed because of
2764 * the 'winfixheight' option.
2765 */
2766 static int
2767frame_fixed_height(frp)
2768 frame_T *frp;
2769{
2770 /* frame with one window: fixed height if 'winfixheight' set. */
2771 if (frp->fr_win != NULL)
2772 return frp->fr_win->w_p_wfh;
2773
2774 if (frp->fr_layout == FR_ROW)
2775 {
2776 /* The frame is fixed height if one of the frames in the row is fixed
2777 * height. */
2778 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2779 if (frame_fixed_height(frp))
2780 return TRUE;
2781 return FALSE;
2782 }
2783
2784 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2785 * frames in the row are fixed height. */
2786 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2787 if (!frame_fixed_height(frp))
2788 return FALSE;
2789 return TRUE;
2790}
2791
2792#ifdef FEAT_VERTSPLIT
2793/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002794 * Return TRUE if width of frame "frp" should not be changed because of
2795 * the 'winfixwidth' option.
2796 */
2797 static int
2798frame_fixed_width(frp)
2799 frame_T *frp;
2800{
2801 /* frame with one window: fixed width if 'winfixwidth' set. */
2802 if (frp->fr_win != NULL)
2803 return frp->fr_win->w_p_wfw;
2804
2805 if (frp->fr_layout == FR_COL)
2806 {
2807 /* The frame is fixed width if one of the frames in the row is fixed
2808 * width. */
2809 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2810 if (frame_fixed_width(frp))
2811 return TRUE;
2812 return FALSE;
2813 }
2814
2815 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2816 * frames in the row are fixed width. */
2817 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2818 if (!frame_fixed_width(frp))
2819 return FALSE;
2820 return TRUE;
2821}
2822
2823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 * Add a status line to windows at the bottom of "frp".
2825 * Note: Does not check if there is room!
2826 */
2827 static void
2828frame_add_statusline(frp)
2829 frame_T *frp;
2830{
2831 win_T *wp;
2832
2833 if (frp->fr_layout == FR_LEAF)
2834 {
2835 wp = frp->fr_win;
2836 if (wp->w_status_height == 0)
2837 {
2838 if (wp->w_height > 0) /* don't make it negative */
2839 --wp->w_height;
2840 wp->w_status_height = STATUS_HEIGHT;
2841 }
2842 }
2843 else if (frp->fr_layout == FR_ROW)
2844 {
2845 /* Handle all the frames in the row. */
2846 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2847 frame_add_statusline(frp);
2848 }
2849 else /* frp->fr_layout == FR_COL */
2850 {
2851 /* Only need to handle the last frame in the column. */
2852 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2853 ;
2854 frame_add_statusline(frp);
2855 }
2856}
2857
2858/*
2859 * Set width of a frame. Handles recursively going through contained frames.
2860 * May remove separator line for windows at the right side (for win_close()).
2861 */
2862 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002863frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 frame_T *topfrp;
2865 int width;
2866 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002867 int wfw; /* obey 'winfixwidth' when there is a choice;
2868 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869{
2870 frame_T *frp;
2871 int extra_cols;
2872 int w;
2873 win_T *wp;
2874
2875 if (topfrp->fr_layout == FR_LEAF)
2876 {
2877 /* Simple case: just one window. */
2878 wp = topfrp->fr_win;
2879 /* Find out if there are any windows right of this one. */
2880 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2881 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2882 break;
2883 if (frp->fr_parent == NULL)
2884 wp->w_vsep_width = 0;
2885 win_new_width(wp, width - wp->w_vsep_width);
2886 }
2887 else if (topfrp->fr_layout == FR_COL)
2888 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002889 do
2890 {
2891 /* All frames in this column get the same new width. */
2892 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2893 {
2894 frame_new_width(frp, width, leftfirst, wfw);
2895 if (frp->fr_width > width)
2896 {
2897 /* Could not fit the windows, make whole column wider. */
2898 width = frp->fr_width;
2899 break;
2900 }
2901 }
2902 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 }
2904 else /* fr_layout == FR_ROW */
2905 {
2906 /* Complicated case: Resize a row of frames. Resize the rightmost
2907 * frame first, frames left of it when needed. */
2908
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002910 if (wfw)
2911 /* Advance past frames with one window with 'wfw' set. */
2912 while (frame_fixed_width(frp))
2913 {
2914 frp = frp->fr_next;
2915 if (frp == NULL)
2916 return; /* no frame without 'wfw', give up */
2917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002919 {
2920 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 while (frp->fr_next != NULL)
2922 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002923 if (wfw)
2924 /* Advance back for frames with one window with 'wfw' set. */
2925 while (frame_fixed_width(frp))
2926 frp = frp->fr_prev;
2927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
2929 extra_cols = width - topfrp->fr_width;
2930 if (extra_cols < 0)
2931 {
2932 /* reduce frame width, rightmost frame first */
2933 while (frp != NULL)
2934 {
2935 w = frame_minwidth(frp, NULL);
2936 if (frp->fr_width + extra_cols < w)
2937 {
2938 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002939 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 }
2941 else
2942 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002943 frame_new_width(frp, frp->fr_width + extra_cols,
2944 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 break;
2946 }
2947 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002948 {
2949 do
2950 frp = frp->fr_next;
2951 while (wfw && frp != NULL && frame_fixed_width(frp));
2952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002954 {
2955 do
2956 frp = frp->fr_prev;
2957 while (wfw && frp != NULL && frame_fixed_width(frp));
2958 }
2959 /* Increase "width" if we could not reduce enough frames. */
2960 if (frp == NULL)
2961 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 }
2963 }
2964 else if (extra_cols > 0)
2965 {
2966 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002967 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
2969 }
2970 topfrp->fr_width = width;
2971}
2972
2973/*
2974 * Add the vertical separator to windows at the right side of "frp".
2975 * Note: Does not check if there is room!
2976 */
2977 static void
2978frame_add_vsep(frp)
2979 frame_T *frp;
2980{
2981 win_T *wp;
2982
2983 if (frp->fr_layout == FR_LEAF)
2984 {
2985 wp = frp->fr_win;
2986 if (wp->w_vsep_width == 0)
2987 {
2988 if (wp->w_width > 0) /* don't make it negative */
2989 --wp->w_width;
2990 wp->w_vsep_width = 1;
2991 }
2992 }
2993 else if (frp->fr_layout == FR_COL)
2994 {
2995 /* Handle all the frames in the column. */
2996 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2997 frame_add_vsep(frp);
2998 }
2999 else /* frp->fr_layout == FR_ROW */
3000 {
3001 /* Only need to handle the last frame in the row. */
3002 frp = frp->fr_child;
3003 while (frp->fr_next != NULL)
3004 frp = frp->fr_next;
3005 frame_add_vsep(frp);
3006 }
3007}
3008
3009/*
3010 * Set frame width from the window it contains.
3011 */
3012 static void
3013frame_fix_width(wp)
3014 win_T *wp;
3015{
3016 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3017}
3018#endif
3019
3020/*
3021 * Set frame height from the window it contains.
3022 */
3023 static void
3024frame_fix_height(wp)
3025 win_T *wp;
3026{
3027 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3028}
3029
3030/*
3031 * Compute the minimal height for frame "topfrp".
3032 * Uses the 'winminheight' option.
3033 * When "next_curwin" isn't NULL, use p_wh for this window.
3034 * When "next_curwin" is NOWIN, don't use at least one line for the current
3035 * window.
3036 */
3037 static int
3038frame_minheight(topfrp, next_curwin)
3039 frame_T *topfrp;
3040 win_T *next_curwin;
3041{
3042 frame_T *frp;
3043 int m;
3044#ifdef FEAT_VERTSPLIT
3045 int n;
3046#endif
3047
3048 if (topfrp->fr_win != NULL)
3049 {
3050 if (topfrp->fr_win == next_curwin)
3051 m = p_wh + topfrp->fr_win->w_status_height;
3052 else
3053 {
3054 /* window: minimal height of the window plus status line */
3055 m = p_wmh + topfrp->fr_win->w_status_height;
3056 /* Current window is minimal one line high */
3057 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3058 ++m;
3059 }
3060 }
3061#ifdef FEAT_VERTSPLIT
3062 else if (topfrp->fr_layout == FR_ROW)
3063 {
3064 /* get the minimal height from each frame in this row */
3065 m = 0;
3066 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3067 {
3068 n = frame_minheight(frp, next_curwin);
3069 if (n > m)
3070 m = n;
3071 }
3072 }
3073#endif
3074 else
3075 {
3076 /* Add up the minimal heights for all frames in this column. */
3077 m = 0;
3078 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3079 m += frame_minheight(frp, next_curwin);
3080 }
3081
3082 return m;
3083}
3084
3085#ifdef FEAT_VERTSPLIT
3086/*
3087 * Compute the minimal width for frame "topfrp".
3088 * When "next_curwin" isn't NULL, use p_wiw for this window.
3089 * When "next_curwin" is NOWIN, don't use at least one column for the current
3090 * window.
3091 */
3092 static int
3093frame_minwidth(topfrp, next_curwin)
3094 frame_T *topfrp;
3095 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3096{
3097 frame_T *frp;
3098 int m, n;
3099
3100 if (topfrp->fr_win != NULL)
3101 {
3102 if (topfrp->fr_win == next_curwin)
3103 m = p_wiw + topfrp->fr_win->w_vsep_width;
3104 else
3105 {
3106 /* window: minimal width of the window plus separator column */
3107 m = p_wmw + topfrp->fr_win->w_vsep_width;
3108 /* Current window is minimal one column wide */
3109 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3110 ++m;
3111 }
3112 }
3113 else if (topfrp->fr_layout == FR_COL)
3114 {
3115 /* get the minimal width from each frame in this column */
3116 m = 0;
3117 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3118 {
3119 n = frame_minwidth(frp, next_curwin);
3120 if (n > m)
3121 m = n;
3122 }
3123 }
3124 else
3125 {
3126 /* Add up the minimal widths for all frames in this row. */
3127 m = 0;
3128 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3129 m += frame_minwidth(frp, next_curwin);
3130 }
3131
3132 return m;
3133}
3134#endif
3135
3136
3137/*
3138 * Try to close all windows except current one.
3139 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3140 * used and the buffer was modified.
3141 *
3142 * Used by ":bdel" and ":only".
3143 */
3144 void
3145close_others(message, forceit)
3146 int message;
3147 int forceit; /* always hide all other windows */
3148{
3149 win_T *wp;
3150 win_T *nextwp;
3151 int r;
3152
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003153 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 {
3155 if (message
3156#ifdef FEAT_AUTOCMD
3157 && !autocmd_busy
3158#endif
3159 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003160 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 return;
3162 }
3163
3164 /* Be very careful here: autocommands may change the window layout. */
3165 for (wp = firstwin; win_valid(wp); wp = nextwp)
3166 {
3167 nextwp = wp->w_next;
3168 if (wp != curwin) /* don't close current window */
3169 {
3170
3171 /* Check if it's allowed to abandon this window */
3172 r = can_abandon(wp->w_buffer, forceit);
3173#ifdef FEAT_AUTOCMD
3174 if (!win_valid(wp)) /* autocommands messed wp up */
3175 {
3176 nextwp = firstwin;
3177 continue;
3178 }
3179#endif
3180 if (!r)
3181 {
3182#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3183 if (message && (p_confirm || cmdmod.confirm) && p_write)
3184 {
3185 dialog_changed(wp->w_buffer, FALSE);
3186# ifdef FEAT_AUTOCMD
3187 if (!win_valid(wp)) /* autocommands messed wp up */
3188 {
3189 nextwp = firstwin;
3190 continue;
3191 }
3192# endif
3193 }
3194 if (bufIsChanged(wp->w_buffer))
3195#endif
3196 continue;
3197 }
3198 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3199 }
3200 }
3201
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003202 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 EMSG(_("E445: Other window contains changes"));
3204}
3205
3206#endif /* FEAT_WINDOWS */
3207
3208/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003209 * Init the current window "curwin".
3210 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 */
3212 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003213curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003215 win_init_empty(curwin);
3216}
3217
3218 void
3219win_init_empty(wp)
3220 win_T *wp;
3221{
3222 redraw_win_later(wp, NOT_VALID);
3223 wp->w_lines_valid = 0;
3224 wp->w_cursor.lnum = 1;
3225 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003227 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003229 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3230 wp->w_pcmark.col = 0;
3231 wp->w_prev_pcmark.lnum = 0;
3232 wp->w_prev_pcmark.col = 0;
3233 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003235 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003237 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003239 if (wp->w_p_rl)
3240 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003242 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243#endif
3244}
3245
3246/*
3247 * Allocate the first window and put an empty buffer in it.
3248 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003249 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003251 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252win_alloc_first()
3253{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003254 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003255 return FAIL;
3256
3257#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003258 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003259 if (first_tabpage == NULL)
3260 return FAIL;
3261 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003262 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003263#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003264
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003265 return OK;
3266}
3267
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003268#if defined(FEAT_AUTOCMD) || defined(PROTO)
3269/*
3270 * Init "aucmd_win". This can only be done after the first
3271 * window is fully initialized, thus it can't be in win_alloc_first().
3272 */
3273 void
3274win_alloc_aucmd_win()
3275{
3276 aucmd_win = win_alloc(NULL, TRUE);
3277 if (aucmd_win != NULL)
3278 {
3279 win_init_some(aucmd_win, curwin);
3280# ifdef FEAT_SCROLLBIND
3281 aucmd_win->w_p_scb = FALSE;
3282# endif
3283 new_frame(aucmd_win);
3284 }
3285}
3286#endif
3287
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003288/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003289 * Allocate the first window or the first window in a new tab page.
3290 * When "oldwin" is NULL create an empty buffer for it.
3291 * When "oldwin" is not NULL copy info from it to the new window (only with
3292 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003293 * Return FAIL when something goes wrong (out of memory).
3294 */
3295 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003296win_alloc_firstwin(oldwin)
3297 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003298{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003299 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003300 if (oldwin == NULL)
3301 {
3302 /* Very first window, need to create an empty buffer for it and
3303 * initialize from scratch. */
3304 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3305 if (curwin == NULL || curbuf == NULL)
3306 return FAIL;
3307 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003308#ifdef FEAT_SYN_HL
3309 curwin->w_s = &(curbuf->b_s);
3310#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003311 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003313 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003315 curwin_init(); /* init current window */
3316 }
3317#ifdef FEAT_WINDOWS
3318 else
3319 {
3320 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003321 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003322
3323# ifdef FEAT_SCROLLBIND
3324 /* We don't want scroll-binding in the first window. */
3325 curwin->w_p_scb = FALSE;
3326# endif
3327 }
3328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003330 new_frame(curwin);
3331 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003332 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003333 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334#ifdef FEAT_VERTSPLIT
3335 topframe->fr_width = Columns;
3336#endif
3337 topframe->fr_height = Rows - p_ch;
3338 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003339
3340 return OK;
3341}
3342
3343/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003344 * Create a frame for window "wp".
3345 */
3346 static void
3347new_frame(win_T *wp)
3348{
3349 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3350
3351 wp->w_frame = frp;
3352 if (frp != NULL)
3353 {
3354 frp->fr_layout = FR_LEAF;
3355 frp->fr_win = wp;
3356 }
3357}
3358
3359/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003360 * Initialize the window and frame size to the maximum.
3361 */
3362 void
3363win_init_size()
3364{
3365 firstwin->w_height = ROWS_AVAIL;
3366 topframe->fr_height = ROWS_AVAIL;
3367#ifdef FEAT_VERTSPLIT
3368 firstwin->w_width = Columns;
3369 topframe->fr_width = Columns;
3370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371}
3372
3373#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003374
3375/*
3376 * Allocate a new tabpage_T and init the values.
3377 * Returns NULL when out of memory.
3378 */
3379 static tabpage_T *
3380alloc_tabpage()
3381{
3382 tabpage_T *tp;
3383
3384 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3385 if (tp != NULL)
3386 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003387# ifdef FEAT_GUI
3388 int i;
3389
3390 for (i = 0; i < 3; i++)
3391 tp->tp_prev_which_scrollbars[i] = -1;
3392# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003393# ifdef FEAT_DIFF
3394 tp->tp_diff_invalid = TRUE;
3395# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003396#ifdef FEAT_EVAL
3397 /* init t: variables */
3398 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3399#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003400 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003401 }
3402 return tp;
3403}
3404
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003405 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003406free_tabpage(tp)
3407 tabpage_T *tp;
3408{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003409 int idx;
3410
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003411# ifdef FEAT_DIFF
3412 diff_clear(tp);
3413# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003414 for (idx = 0; idx < SNAP_COUNT; ++idx)
3415 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003416#ifdef FEAT_EVAL
3417 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3418#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003419 vim_free(tp);
3420}
3421
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003422/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003423 * Create a new Tab page with one window.
3424 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003425 * When "after" is 0 put it just after the current Tab page.
3426 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003427 * Return FAIL or OK.
3428 */
3429 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003430win_new_tabpage(after)
3431 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003432{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003433 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003434 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003435 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003436
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003437 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003438 if (newtp == NULL)
3439 return FAIL;
3440
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003441 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003442 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003443 {
3444 vim_free(newtp);
3445 return FAIL;
3446 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003447 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003448
3449 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003450 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003451 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003452 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003453 if (after == 1)
3454 {
3455 /* New tab page becomes the first one. */
3456 newtp->tp_next = first_tabpage;
3457 first_tabpage = newtp;
3458 }
3459 else
3460 {
3461 if (after > 0)
3462 {
3463 /* Put new tab page before tab page "after". */
3464 n = 2;
3465 for (tp = first_tabpage; tp->tp_next != NULL
3466 && n < after; tp = tp->tp_next)
3467 ++n;
3468 }
3469 newtp->tp_next = tp->tp_next;
3470 tp->tp_next = newtp;
3471 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003472 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003473 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003474 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003475
3476 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003477 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003478
3479#if defined(FEAT_GUI)
3480 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3481 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003482 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003483#endif
3484
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003485 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003486#ifdef FEAT_AUTOCMD
3487 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3488 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3489#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003490 return OK;
3491 }
3492
3493 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003494 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003495 return FAIL;
3496}
3497
3498/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003499 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3500 * like with ":split".
3501 * Returns OK if a new tab page was created, FAIL otherwise.
3502 */
3503 int
3504may_open_tabpage()
3505{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003506 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003507
Bram Moolenaard326ce82007-03-11 14:48:29 +00003508 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003509 {
3510 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003511 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003512 return win_new_tabpage(n);
3513 }
3514 return FAIL;
3515}
3516
3517/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003518 * Create up to "maxcount" tabpages with empty windows.
3519 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003520 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003521 int
3522make_tabpages(maxcount)
3523 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003524{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003525 int count = maxcount;
3526 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003527
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003528 /* Limit to 'tabpagemax' tabs. */
3529 if (count > p_tpm)
3530 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003531
3532#ifdef FEAT_AUTOCMD
3533 /*
3534 * Don't execute autocommands while creating the tab pages. Must do that
3535 * when putting the buffers in the windows.
3536 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003537 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003538#endif
3539
3540 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003541 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003542 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003543
3544#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003545 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003546#endif
3547
3548 /* return actual number of tab pages */
3549 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003550}
3551
3552/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003553 * Return TRUE when "tpc" points to a valid tab page.
3554 */
3555 int
3556valid_tabpage(tpc)
3557 tabpage_T *tpc;
3558{
3559 tabpage_T *tp;
3560
3561 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3562 if (tp == tpc)
3563 return TRUE;
3564 return FALSE;
3565}
3566
3567/*
3568 * Find tab page "n" (first one is 1). Returns NULL when not found.
3569 */
3570 tabpage_T *
3571find_tabpage(n)
3572 int n;
3573{
3574 tabpage_T *tp;
3575 int i = 1;
3576
3577 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3578 ++i;
3579 return tp;
3580}
3581
3582/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003583 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003584 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003585 */
3586 int
3587tabpage_index(ftp)
3588 tabpage_T *ftp;
3589{
3590 int i = 1;
3591 tabpage_T *tp;
3592
3593 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3594 ++i;
3595 return i;
3596}
3597
3598/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003599 * Prepare for leaving the current tab page.
3600 * When autocomands change "curtab" we don't leave the tab page and return
3601 * FAIL.
3602 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003603 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003604 static int
3605leave_tabpage(new_curbuf)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003606 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003607 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003608{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003609 tabpage_T *tp = curtab;
3610
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003611#ifdef FEAT_VISUAL
3612 reset_VIsual_and_resel(); /* stop Visual mode */
3613#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003614#ifdef FEAT_AUTOCMD
3615 if (new_curbuf != curbuf)
3616 {
3617 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3618 if (curtab != tp)
3619 return FAIL;
3620 }
3621 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3622 if (curtab != tp)
3623 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003624 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003625 if (curtab != tp)
3626 return FAIL;
3627#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003628#if defined(FEAT_GUI)
3629 /* Remove the scrollbars. They may be added back later. */
3630 if (gui.in_use)
3631 gui_remove_scrollbars();
3632#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003633 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003634 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003635 tp->tp_firstwin = firstwin;
3636 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003637 tp->tp_old_Rows = Rows;
3638 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003639 firstwin = NULL;
3640 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003641 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003642}
3643
3644/*
3645 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003646 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003647 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003648 static void
3649enter_tabpage(tp, old_curbuf)
3650 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003651 buf_T *old_curbuf UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003652{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003653 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003654 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003655
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003656 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003657 firstwin = tp->tp_firstwin;
3658 lastwin = tp->tp_lastwin;
3659 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003660
3661 /* We would like doing the TabEnter event first, but we don't have a
3662 * valid current window yet, which may break some commands.
3663 * This triggers autocommands, thus may make "tp" invalid. */
3664 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3665 prevwin = next_prevwin;
3666
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003667#ifdef FEAT_AUTOCMD
3668 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003669
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003670 if (old_curbuf != curbuf)
3671 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3672#endif
3673
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003674 last_status(FALSE); /* status line may appear or disappear */
3675 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003676 must_redraw = CLEAR; /* need to redraw everything */
3677#ifdef FEAT_DIFF
3678 diff_need_scrollbind = TRUE;
3679#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003680
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003681 /* The tabpage line may have appeared or disappeared, may need to resize
3682 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003683 * frame sizes too. Use the stored value of p_ch, so that it can be
3684 * different for each tab page. */
3685 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003686 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3687#ifdef FEAT_GUI_TABLINE
3688 && !gui_use_tabline()
3689#endif
3690 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003691 shell_new_rows();
3692#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003693 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003694 shell_new_columns(); /* update window widths */
3695#endif
3696
3697#if defined(FEAT_GUI)
3698 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3699 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003700 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003701#endif
3702
3703 redraw_all_later(CLEAR);
3704}
3705
3706/*
3707 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003708 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003709 */
3710 void
3711goto_tabpage(n)
3712 int n;
3713{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003714 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003715 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003716 int i;
3717
Bram Moolenaard68071d2006-05-02 22:08:30 +00003718 if (text_locked())
3719 {
3720 /* Not allowed when editing the command line. */
3721#ifdef FEAT_CMDWIN
3722 if (cmdwin_type != 0)
3723 EMSG(_(e_cmdwin));
3724 else
3725#endif
3726 EMSG(_(e_secure));
3727 return;
3728 }
3729
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003730 /* If there is only one it can't work. */
3731 if (first_tabpage->tp_next == NULL)
3732 {
3733 if (n > 1)
3734 beep_flush();
3735 return;
3736 }
3737
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003738 if (n == 0)
3739 {
3740 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003741 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003742 tp = first_tabpage;
3743 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003744 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003745 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003746 else if (n < 0)
3747 {
3748 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3749 * this N times. */
3750 ttp = curtab;
3751 for (i = n; i < 0; ++i)
3752 {
3753 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3754 tp = tp->tp_next)
3755 ;
3756 ttp = tp;
3757 }
3758 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003759 else if (n == 9999)
3760 {
3761 /* Go to last tab page. */
3762 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3763 ;
3764 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003765 else
3766 {
3767 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003768 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003769 if (tp == NULL)
3770 {
3771 beep_flush();
3772 return;
3773 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003774 }
3775
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003776 goto_tabpage_tp(tp);
3777
3778#ifdef FEAT_GUI_TABLINE
3779 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003780 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003781#endif
3782}
3783
3784/*
3785 * Go to tabpage "tp".
3786 * Note: doesn't update the GUI tab.
3787 */
3788 void
3789goto_tabpage_tp(tp)
3790 tabpage_T *tp;
3791{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003792 /* Don't repeat a message in another tab page. */
3793 set_keep_msg(NULL, 0);
3794
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003795 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003796 {
3797 if (valid_tabpage(tp))
3798 enter_tabpage(tp, curbuf);
3799 else
3800 enter_tabpage(curtab, curbuf);
3801 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003802}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
3804/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003805 * Enter window "wp" in tab page "tp".
3806 * Also updates the GUI tab.
3807 */
3808 void
3809goto_tabpage_win(tp, wp)
3810 tabpage_T *tp;
3811 win_T *wp;
3812{
3813 goto_tabpage_tp(tp);
3814 if (curtab == tp && win_valid(wp))
3815 {
3816 win_enter(wp, TRUE);
3817# ifdef FEAT_GUI_TABLINE
3818 if (gui_use_tabline())
3819 gui_mch_set_curtab(tabpage_index(curtab));
3820# endif
3821 }
3822}
3823
3824/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003825 * Move the current tab page to before tab page "nr".
3826 */
3827 void
3828tabpage_move(nr)
3829 int nr;
3830{
3831 int n = nr;
3832 tabpage_T *tp;
3833
3834 if (first_tabpage->tp_next == NULL)
3835 return;
3836
3837 /* Remove the current tab page from the list of tab pages. */
3838 if (curtab == first_tabpage)
3839 first_tabpage = curtab->tp_next;
3840 else
3841 {
3842 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3843 if (tp->tp_next == curtab)
3844 break;
3845 if (tp == NULL) /* "cannot happen" */
3846 return;
3847 tp->tp_next = curtab->tp_next;
3848 }
3849
3850 /* Re-insert it at the specified position. */
3851 if (n == 0)
3852 {
3853 curtab->tp_next = first_tabpage;
3854 first_tabpage = curtab;
3855 }
3856 else
3857 {
3858 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3859 --n;
3860 curtab->tp_next = tp->tp_next;
3861 tp->tp_next = curtab;
3862 }
3863
3864 /* Need to redraw the tabline. Tab page contents doesn't change. */
3865 redraw_tabline = TRUE;
3866}
3867
3868
3869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 * Go to another window.
3871 * When jumping to another buffer, stop Visual mode. Do this before
3872 * changing windows so we can yank the selection into the '*' register.
3873 * When jumping to another window on the same buffer, adjust its cursor
3874 * position to keep the same Visual area.
3875 */
3876 void
3877win_goto(wp)
3878 win_T *wp;
3879{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02003880#ifdef FEAT_CONCEAL
3881 win_T *owp = curwin;
3882#endif
3883
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003884 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 {
3886 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003887 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 return;
3889 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003890#ifdef FEAT_AUTOCMD
3891 if (curbuf_locked())
3892 return;
3893#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003894
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895#ifdef FEAT_VISUAL
3896 if (wp->w_buffer != curbuf)
3897 reset_VIsual_and_resel();
3898 else if (VIsual_active)
3899 wp->w_cursor = curwin->w_cursor;
3900#endif
3901
3902#ifdef FEAT_GUI
3903 need_mouse_correct = TRUE;
3904#endif
3905 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02003906
3907#ifdef FEAT_CONCEAL
3908 /* Conceal cursor line in previous window, unconceal in current window. */
3909 if (win_valid(owp))
3910 update_single_line(owp, owp->w_cursor.lnum);
3911 update_single_line(curwin, curwin->w_cursor.lnum);
3912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913}
3914
3915#if defined(FEAT_PERL) || defined(PROTO)
3916/*
3917 * Find window number "winnr" (counting top to bottom).
3918 */
3919 win_T *
3920win_find_nr(winnr)
3921 int winnr;
3922{
3923 win_T *wp;
3924
3925# ifdef FEAT_WINDOWS
3926 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3927 if (--winnr == 0)
3928 break;
3929 return wp;
3930# else
3931 return curwin;
3932# endif
3933}
3934#endif
3935
3936#ifdef FEAT_VERTSPLIT
3937/*
3938 * Move to window above or below "count" times.
3939 */
3940 static void
3941win_goto_ver(up, count)
3942 int up; /* TRUE to go to win above */
3943 long count;
3944{
3945 frame_T *fr;
3946 frame_T *nfr;
3947 frame_T *foundfr;
3948
3949 foundfr = curwin->w_frame;
3950 while (count--)
3951 {
3952 /*
3953 * First go upwards in the tree of frames until we find a upwards or
3954 * downwards neighbor.
3955 */
3956 fr = foundfr;
3957 for (;;)
3958 {
3959 if (fr == topframe)
3960 goto end;
3961 if (up)
3962 nfr = fr->fr_prev;
3963 else
3964 nfr = fr->fr_next;
3965 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3966 break;
3967 fr = fr->fr_parent;
3968 }
3969
3970 /*
3971 * Now go downwards to find the bottom or top frame in it.
3972 */
3973 for (;;)
3974 {
3975 if (nfr->fr_layout == FR_LEAF)
3976 {
3977 foundfr = nfr;
3978 break;
3979 }
3980 fr = nfr->fr_child;
3981 if (nfr->fr_layout == FR_ROW)
3982 {
3983 /* Find the frame at the cursor row. */
3984 while (fr->fr_next != NULL
3985 && frame2win(fr)->w_wincol + fr->fr_width
3986 <= curwin->w_wincol + curwin->w_wcol)
3987 fr = fr->fr_next;
3988 }
3989 if (nfr->fr_layout == FR_COL && up)
3990 while (fr->fr_next != NULL)
3991 fr = fr->fr_next;
3992 nfr = fr;
3993 }
3994 }
3995end:
3996 if (foundfr != NULL)
3997 win_goto(foundfr->fr_win);
3998}
3999
4000/*
4001 * Move to left or right window.
4002 */
4003 static void
4004win_goto_hor(left, count)
4005 int left; /* TRUE to go to left win */
4006 long count;
4007{
4008 frame_T *fr;
4009 frame_T *nfr;
4010 frame_T *foundfr;
4011
4012 foundfr = curwin->w_frame;
4013 while (count--)
4014 {
4015 /*
4016 * First go upwards in the tree of frames until we find a left or
4017 * right neighbor.
4018 */
4019 fr = foundfr;
4020 for (;;)
4021 {
4022 if (fr == topframe)
4023 goto end;
4024 if (left)
4025 nfr = fr->fr_prev;
4026 else
4027 nfr = fr->fr_next;
4028 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4029 break;
4030 fr = fr->fr_parent;
4031 }
4032
4033 /*
4034 * Now go downwards to find the leftmost or rightmost frame in it.
4035 */
4036 for (;;)
4037 {
4038 if (nfr->fr_layout == FR_LEAF)
4039 {
4040 foundfr = nfr;
4041 break;
4042 }
4043 fr = nfr->fr_child;
4044 if (nfr->fr_layout == FR_COL)
4045 {
4046 /* Find the frame at the cursor row. */
4047 while (fr->fr_next != NULL
4048 && frame2win(fr)->w_winrow + fr->fr_height
4049 <= curwin->w_winrow + curwin->w_wrow)
4050 fr = fr->fr_next;
4051 }
4052 if (nfr->fr_layout == FR_ROW && left)
4053 while (fr->fr_next != NULL)
4054 fr = fr->fr_next;
4055 nfr = fr;
4056 }
4057 }
4058end:
4059 if (foundfr != NULL)
4060 win_goto(foundfr->fr_win);
4061}
4062#endif
4063
4064/*
4065 * Make window "wp" the current window.
4066 */
4067 void
4068win_enter(wp, undo_sync)
4069 win_T *wp;
4070 int undo_sync;
4071{
4072 win_enter_ext(wp, undo_sync, FALSE);
4073}
4074
4075/*
4076 * Make window wp the current window.
4077 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4078 * been closed and isn't valid.
4079 */
4080 static void
4081win_enter_ext(wp, undo_sync, curwin_invalid)
4082 win_T *wp;
4083 int undo_sync;
4084 int curwin_invalid;
4085{
4086#ifdef FEAT_AUTOCMD
4087 int other_buffer = FALSE;
4088#endif
4089
4090 if (wp == curwin && !curwin_invalid) /* nothing to do */
4091 return;
4092
4093#ifdef FEAT_AUTOCMD
4094 if (!curwin_invalid)
4095 {
4096 /*
4097 * Be careful: If autocommands delete the window, return now.
4098 */
4099 if (wp->w_buffer != curbuf)
4100 {
4101 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4102 other_buffer = TRUE;
4103 if (!win_valid(wp))
4104 return;
4105 }
4106 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4107 if (!win_valid(wp))
4108 return;
4109# ifdef FEAT_EVAL
4110 /* autocmds may abort script processing */
4111 if (aborting())
4112 return;
4113# endif
4114 }
4115#endif
4116
4117 /* sync undo before leaving the current buffer */
4118 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004119 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 /* may have to copy the buffer options when 'cpo' contains 'S' */
4121 if (wp->w_buffer != curbuf)
4122 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4123 if (!curwin_invalid)
4124 {
4125 prevwin = curwin; /* remember for CTRL-W p */
4126 curwin->w_redr_status = TRUE;
4127 }
4128 curwin = wp;
4129 curbuf = wp->w_buffer;
4130 check_cursor();
4131#ifdef FEAT_VIRTUALEDIT
4132 if (!virtual_active())
4133 curwin->w_cursor.coladd = 0;
4134#endif
4135 changed_line_abv_curs(); /* assume cursor position needs updating */
4136
4137 if (curwin->w_localdir != NULL)
4138 {
4139 /* Window has a local directory: Save current directory as global
4140 * directory (unless that was done already) and change to the local
4141 * directory. */
4142 if (globaldir == NULL)
4143 {
4144 char_u cwd[MAXPATHL];
4145
4146 if (mch_dirname(cwd, MAXPATHL) == OK)
4147 globaldir = vim_strsave(cwd);
4148 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004149 if (mch_chdir((char *)curwin->w_localdir) == 0)
4150 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152 else if (globaldir != NULL)
4153 {
4154 /* Window doesn't have a local directory and we are not in the global
4155 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004156 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 vim_free(globaldir);
4158 globaldir = NULL;
4159 shorten_fnames(TRUE);
4160 }
4161
4162#ifdef FEAT_AUTOCMD
4163 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4164 if (other_buffer)
4165 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4166#endif
4167
4168#ifdef FEAT_TITLE
4169 maketitle();
4170#endif
4171 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004172 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 if (restart_edit)
4174 redraw_later(VALID); /* causes status line redraw */
4175
4176 /* set window height to desired minimal value */
4177 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4178 win_setheight((int)p_wh);
4179 else if (curwin->w_height == 0)
4180 win_setheight(1);
4181
4182#ifdef FEAT_VERTSPLIT
4183 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004184 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 win_setwidth((int)p_wiw);
4186#endif
4187
4188#ifdef FEAT_MOUSE
4189 setmouse(); /* in case jumped to/from help buffer */
4190#endif
4191
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004192 /* Change directories when the 'acd' option is set. */
4193 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194}
4195
4196#endif /* FEAT_WINDOWS */
4197
4198#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4199/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004200 * Jump to the first open window that contains buffer "buf", if one exists.
4201 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 */
4203 win_T *
4204buf_jump_open_win(buf)
4205 buf_T *buf;
4206{
4207# ifdef FEAT_WINDOWS
4208 win_T *wp;
4209
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004210 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 if (wp->w_buffer == buf)
4212 break;
4213 if (wp != NULL)
4214 win_enter(wp, FALSE);
4215 return wp;
4216# else
4217 if (curwin->w_buffer == buf)
4218 return curwin;
4219 return NULL;
4220# endif
4221}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004222
4223/*
4224 * Jump to the first open window in any tab page that contains buffer "buf",
4225 * if one exists.
4226 * Returns a pointer to the window found, otherwise NULL.
4227 */
4228 win_T *
4229buf_jump_open_tab(buf)
4230 buf_T *buf;
4231{
4232# ifdef FEAT_WINDOWS
4233 win_T *wp;
4234 tabpage_T *tp;
4235
4236 /* First try the current tab page. */
4237 wp = buf_jump_open_win(buf);
4238 if (wp != NULL)
4239 return wp;
4240
4241 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4242 if (tp != curtab)
4243 {
4244 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4245 if (wp->w_buffer == buf)
4246 break;
4247 if (wp != NULL)
4248 {
4249 goto_tabpage_win(tp, wp);
4250 if (curwin != wp)
4251 wp = NULL; /* something went wrong */
4252 break;
4253 }
4254 }
4255
4256 return wp;
4257# else
4258 if (curwin->w_buffer == buf)
4259 return curwin;
4260 return NULL;
4261# endif
4262}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263#endif
4264
4265/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004266 * Allocate a window structure and link it in the window list when "hidden" is
4267 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004270win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004271 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004272 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273{
4274 win_T *newwin;
4275
4276 /*
4277 * allocate window structure and linesizes arrays
4278 */
4279 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4280 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4281 {
4282 vim_free(newwin);
4283 newwin = NULL;
4284 }
4285
4286 if (newwin != NULL)
4287 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004288#ifdef FEAT_AUTOCMD
4289 /* Don't execute autocommands while the window is not properly
4290 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4291 * event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004292 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 /*
4295 * link the window in the window list
4296 */
4297#ifdef FEAT_WINDOWS
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004298 if (!hidden)
4299 win_append(after, newwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300#endif
4301#ifdef FEAT_VERTSPLIT
4302 newwin->w_wincol = 0;
4303 newwin->w_width = Columns;
4304#endif
4305
4306 /* position the display and the cursor at the top of the file. */
4307 newwin->w_topline = 1;
4308#ifdef FEAT_DIFF
4309 newwin->w_topfill = 0;
4310#endif
4311 newwin->w_botline = 2;
4312 newwin->w_cursor.lnum = 1;
4313#ifdef FEAT_SCROLLBIND
4314 newwin->w_scbind_pos = 1;
4315#endif
4316
4317 /* We won't calculate w_fraction until resizing the window */
4318 newwin->w_fraction = 0;
4319 newwin->w_prev_fraction_row = -1;
4320
4321#ifdef FEAT_GUI
4322 if (gui.in_use)
4323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4325 SBAR_LEFT, newwin);
4326 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4327 SBAR_RIGHT, newwin);
4328 }
4329#endif
4330#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004331 /* init w: variables */
4332 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333#endif
4334#ifdef FEAT_FOLDING
4335 foldInitWin(newwin);
4336#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004337#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004338 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004339#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004340#ifdef FEAT_SEARCH_EXTRA
4341 newwin->w_match_head = NULL;
4342 newwin->w_next_match_id = 4;
4343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345 return newwin;
4346}
4347
4348#if defined(FEAT_WINDOWS) || defined(PROTO)
4349
4350/*
4351 * remove window 'wp' from the window list and free the structure
4352 */
4353 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004354win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004356 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357{
4358 int i;
4359
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004360#ifdef FEAT_FOLDING
4361 clearFolding(wp);
4362#endif
4363
4364 /* reduce the reference count to the argument list. */
4365 alist_unlink(wp->w_alist);
4366
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004367#ifdef FEAT_AUTOCMD
4368 /* Don't execute autocommands while the window is halfway being deleted.
4369 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004370 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004371#endif
4372
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004373#ifdef FEAT_LUA
4374 lua_window_free(wp);
4375#endif
4376
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004377#ifdef FEAT_MZSCHEME
4378 mzscheme_window_free(wp);
4379#endif
4380
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381#ifdef FEAT_PERL
4382 perl_win_free(wp);
4383#endif
4384
4385#ifdef FEAT_PYTHON
4386 python_window_free(wp);
4387#endif
4388
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004389#ifdef FEAT_PYTHON3
4390 python3_window_free(wp);
4391#endif
4392
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393#ifdef FEAT_TCL
4394 tcl_window_free(wp);
4395#endif
4396
4397#ifdef FEAT_RUBY
4398 ruby_window_free(wp);
4399#endif
4400
4401 clear_winopt(&wp->w_onebuf_opt);
4402 clear_winopt(&wp->w_allbuf_opt);
4403
4404#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004405 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406#endif
4407
4408 if (prevwin == wp)
4409 prevwin = NULL;
4410 win_free_lsize(wp);
4411
4412 for (i = 0; i < wp->w_tagstacklen; ++i)
4413 vim_free(wp->w_tagstack[i].tagname);
4414
4415 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004416
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004418 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004420
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421#ifdef FEAT_JUMPLIST
4422 free_jumplist(wp);
4423#endif
4424
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004425#ifdef FEAT_QUICKFIX
4426 qf_free_all(wp);
4427#endif
4428
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429#ifdef FEAT_GUI
4430 if (gui.in_use)
4431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4433 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4434 }
4435#endif /* FEAT_GUI */
4436
Bram Moolenaar860cae12010-06-05 23:22:07 +02004437#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004438 reset_synblock(wp); /* free independent synblock */
Bram Moolenaar1a384422010-07-14 19:53:30 +02004439 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004440#endif
4441
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004442#ifdef FEAT_AUTOCMD
4443 if (wp != aucmd_win)
4444#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004445 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004447
4448#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004449 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451}
4452
4453/*
4454 * Append window "wp" in the window list after window "after".
4455 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004456 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457win_append(after, wp)
4458 win_T *after, *wp;
4459{
4460 win_T *before;
4461
4462 if (after == NULL) /* after NULL is in front of the first */
4463 before = firstwin;
4464 else
4465 before = after->w_next;
4466
4467 wp->w_next = before;
4468 wp->w_prev = after;
4469 if (after == NULL)
4470 firstwin = wp;
4471 else
4472 after->w_next = wp;
4473 if (before == NULL)
4474 lastwin = wp;
4475 else
4476 before->w_prev = wp;
4477}
4478
4479/*
4480 * Remove a window from the window list.
4481 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004482 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004483win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004485 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486{
4487 if (wp->w_prev != NULL)
4488 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004489 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004491 else
4492 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 if (wp->w_next != NULL)
4494 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004495 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004497 else
4498 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499}
4500
4501/*
4502 * Append frame "frp" in a frame list after frame "after".
4503 */
4504 static void
4505frame_append(after, frp)
4506 frame_T *after, *frp;
4507{
4508 frp->fr_next = after->fr_next;
4509 after->fr_next = frp;
4510 if (frp->fr_next != NULL)
4511 frp->fr_next->fr_prev = frp;
4512 frp->fr_prev = after;
4513}
4514
4515/*
4516 * Insert frame "frp" in a frame list before frame "before".
4517 */
4518 static void
4519frame_insert(before, frp)
4520 frame_T *before, *frp;
4521{
4522 frp->fr_next = before;
4523 frp->fr_prev = before->fr_prev;
4524 before->fr_prev = frp;
4525 if (frp->fr_prev != NULL)
4526 frp->fr_prev->fr_next = frp;
4527 else
4528 frp->fr_parent->fr_child = frp;
4529}
4530
4531/*
4532 * Remove a frame from a frame list.
4533 */
4534 static void
4535frame_remove(frp)
4536 frame_T *frp;
4537{
4538 if (frp->fr_prev != NULL)
4539 frp->fr_prev->fr_next = frp->fr_next;
4540 else
4541 frp->fr_parent->fr_child = frp->fr_next;
4542 if (frp->fr_next != NULL)
4543 frp->fr_next->fr_prev = frp->fr_prev;
4544}
4545
4546#endif /* FEAT_WINDOWS */
4547
4548/*
4549 * Allocate w_lines[] for window "wp".
4550 * Return FAIL for failure, OK for success.
4551 */
4552 int
4553win_alloc_lines(wp)
4554 win_T *wp;
4555{
4556 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004557 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 if (wp->w_lines == NULL)
4559 return FAIL;
4560 return OK;
4561}
4562
4563/*
4564 * free lsize arrays for a window
4565 */
4566 void
4567win_free_lsize(wp)
4568 win_T *wp;
4569{
4570 vim_free(wp->w_lines);
4571 wp->w_lines = NULL;
4572}
4573
4574/*
4575 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004576 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 */
4578 void
4579shell_new_rows()
4580{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004581 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582
4583 if (firstwin == NULL) /* not initialized yet */
4584 return;
4585#ifdef FEAT_WINDOWS
4586 if (h < frame_minheight(topframe, NULL))
4587 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004588
4589 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 * that doesn't result in the right height, forget about that option. */
4591 frame_new_height(topframe, h, FALSE, TRUE);
4592 if (topframe->fr_height != h)
4593 frame_new_height(topframe, h, FALSE, FALSE);
4594
4595 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4596#else
4597 if (h < 1)
4598 h = 1;
4599 win_new_height(firstwin, h);
4600#endif
4601 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004602#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004603 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004604#endif
4605
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606#if 0
4607 /* Disabled: don't want making the screen smaller make a window larger. */
4608 if (p_ea)
4609 win_equal(curwin, FALSE, 'v');
4610#endif
4611}
4612
4613#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4614/*
4615 * Called from win_new_shellsize() after Columns changed.
4616 */
4617 void
4618shell_new_columns()
4619{
4620 if (firstwin == NULL) /* not initialized yet */
4621 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004622
4623 /* First try setting the widths of windows with 'winfixwidth'. If that
4624 * doesn't result in the right width, forget about that option. */
4625 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4626 if (topframe->fr_width != Columns)
4627 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4628
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4630#if 0
4631 /* Disabled: don't want making the screen smaller make a window larger. */
4632 if (p_ea)
4633 win_equal(curwin, FALSE, 'h');
4634#endif
4635}
4636#endif
4637
4638#if defined(FEAT_CMDWIN) || defined(PROTO)
4639/*
4640 * Save the size of all windows in "gap".
4641 */
4642 void
4643win_size_save(gap)
4644 garray_T *gap;
4645
4646{
4647 win_T *wp;
4648
4649 ga_init2(gap, (int)sizeof(int), 1);
4650 if (ga_grow(gap, win_count() * 2) == OK)
4651 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4652 {
4653 ((int *)gap->ga_data)[gap->ga_len++] =
4654 wp->w_width + wp->w_vsep_width;
4655 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4656 }
4657}
4658
4659/*
4660 * Restore window sizes, but only if the number of windows is still the same.
4661 * Does not free the growarray.
4662 */
4663 void
4664win_size_restore(gap)
4665 garray_T *gap;
4666{
4667 win_T *wp;
4668 int i;
4669
4670 if (win_count() * 2 == gap->ga_len)
4671 {
4672 i = 0;
4673 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4674 {
4675 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4676 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4677 }
4678 /* recompute the window positions */
4679 (void)win_comp_pos();
4680 }
4681}
4682#endif /* FEAT_CMDWIN */
4683
4684#if defined(FEAT_WINDOWS) || defined(PROTO)
4685/*
4686 * Update the position for all windows, using the width and height of the
4687 * frames.
4688 * Returns the row just after the last window.
4689 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004690 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691win_comp_pos()
4692{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004693 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 int col = 0;
4695
4696 frame_comp_pos(topframe, &row, &col);
4697 return row;
4698}
4699
4700/*
4701 * Update the position of the windows in frame "topfrp", using the width and
4702 * height of the frames.
4703 * "*row" and "*col" are the top-left position of the frame. They are updated
4704 * to the bottom-right position plus one.
4705 */
4706 static void
4707frame_comp_pos(topfrp, row, col)
4708 frame_T *topfrp;
4709 int *row;
4710 int *col;
4711{
4712 win_T *wp;
4713 frame_T *frp;
4714#ifdef FEAT_VERTSPLIT
4715 int startcol;
4716 int startrow;
4717#endif
4718
4719 wp = topfrp->fr_win;
4720 if (wp != NULL)
4721 {
4722 if (wp->w_winrow != *row
4723#ifdef FEAT_VERTSPLIT
4724 || wp->w_wincol != *col
4725#endif
4726 )
4727 {
4728 /* position changed, redraw */
4729 wp->w_winrow = *row;
4730#ifdef FEAT_VERTSPLIT
4731 wp->w_wincol = *col;
4732#endif
4733 redraw_win_later(wp, NOT_VALID);
4734 wp->w_redr_status = TRUE;
4735 }
4736 *row += wp->w_height + wp->w_status_height;
4737#ifdef FEAT_VERTSPLIT
4738 *col += wp->w_width + wp->w_vsep_width;
4739#endif
4740 }
4741 else
4742 {
4743#ifdef FEAT_VERTSPLIT
4744 startrow = *row;
4745 startcol = *col;
4746#endif
4747 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4748 {
4749#ifdef FEAT_VERTSPLIT
4750 if (topfrp->fr_layout == FR_ROW)
4751 *row = startrow; /* all frames are at the same row */
4752 else
4753 *col = startcol; /* all frames are at the same col */
4754#endif
4755 frame_comp_pos(frp, row, col);
4756 }
4757 }
4758}
4759
4760#endif /* FEAT_WINDOWS */
4761
4762/*
4763 * Set current window height and take care of repositioning other windows to
4764 * fit around it.
4765 */
4766 void
4767win_setheight(height)
4768 int height;
4769{
4770 win_setheight_win(height, curwin);
4771}
4772
4773/*
4774 * Set the window height of window "win" and take care of repositioning other
4775 * windows to fit around it.
4776 */
4777 void
4778win_setheight_win(height, win)
4779 int height;
4780 win_T *win;
4781{
4782 int row;
4783
4784 if (win == curwin)
4785 {
4786 /* Always keep current window at least one line high, even when
4787 * 'winminheight' is zero. */
4788#ifdef FEAT_WINDOWS
4789 if (height < p_wmh)
4790 height = p_wmh;
4791#endif
4792 if (height == 0)
4793 height = 1;
4794 }
4795
4796#ifdef FEAT_WINDOWS
4797 frame_setheight(win->w_frame, height + win->w_status_height);
4798
4799 /* recompute the window positions */
4800 row = win_comp_pos();
4801#else
4802 if (height > topframe->fr_height)
4803 height = topframe->fr_height;
4804 win->w_height = height;
4805 row = height;
4806#endif
4807
4808 /*
4809 * If there is extra space created between the last window and the command
4810 * line, clear it.
4811 */
4812 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4813 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4814 cmdline_row = row;
4815 msg_row = row;
4816 msg_col = 0;
4817
4818 redraw_all_later(NOT_VALID);
4819}
4820
4821#if defined(FEAT_WINDOWS) || defined(PROTO)
4822
4823/*
4824 * Set the height of a frame to "height" and take care that all frames and
4825 * windows inside it are resized. Also resize frames on the left and right if
4826 * the are in the same FR_ROW frame.
4827 *
4828 * Strategy:
4829 * If the frame is part of a FR_COL frame, try fitting the frame in that
4830 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4831 * go to containing frames to resize them and make room.
4832 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4833 * Check for the minimal height of the FR_ROW frame.
4834 * At the top level we can also use change the command line height.
4835 */
4836 static void
4837frame_setheight(curfrp, height)
4838 frame_T *curfrp;
4839 int height;
4840{
4841 int room; /* total number of lines available */
4842 int take; /* number of lines taken from other windows */
4843 int room_cmdline; /* lines available from cmdline */
4844 int run;
4845 frame_T *frp;
4846 int h;
4847 int room_reserved;
4848
4849 /* If the height already is the desired value, nothing to do. */
4850 if (curfrp->fr_height == height)
4851 return;
4852
4853 if (curfrp->fr_parent == NULL)
4854 {
4855 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004856 if (height > ROWS_AVAIL)
4857 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 if (height > 0)
4859 frame_new_height(curfrp, height, FALSE, FALSE);
4860 }
4861 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4862 {
4863 /* Row of frames: Also need to resize frames left and right of this
4864 * one. First check for the minimal height of these. */
4865 h = frame_minheight(curfrp->fr_parent, NULL);
4866 if (height < h)
4867 height = h;
4868 frame_setheight(curfrp->fr_parent, height);
4869 }
4870 else
4871 {
4872 /*
4873 * Column of frames: try to change only frames in this column.
4874 */
4875#ifdef FEAT_VERTSPLIT
4876 /*
4877 * Do this twice:
4878 * 1: compute room available, if it's not enough try resizing the
4879 * containing frame.
4880 * 2: compute the room available and adjust the height to it.
4881 * Try not to reduce the height of a window with 'winfixheight' set.
4882 */
4883 for (run = 1; run <= 2; ++run)
4884#else
4885 for (;;)
4886#endif
4887 {
4888 room = 0;
4889 room_reserved = 0;
4890 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4891 frp = frp->fr_next)
4892 {
4893 if (frp != curfrp
4894 && frp->fr_win != NULL
4895 && frp->fr_win->w_p_wfh)
4896 room_reserved += frp->fr_height;
4897 room += frp->fr_height;
4898 if (frp != curfrp)
4899 room -= frame_minheight(frp, NULL);
4900 }
4901#ifdef FEAT_VERTSPLIT
4902 if (curfrp->fr_width != Columns)
4903 room_cmdline = 0;
4904 else
4905#endif
4906 {
4907 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4908 + lastwin->w_height + lastwin->w_status_height);
4909 if (room_cmdline < 0)
4910 room_cmdline = 0;
4911 }
4912
4913 if (height <= room + room_cmdline)
4914 break;
4915#ifdef FEAT_VERTSPLIT
4916 if (run == 2 || curfrp->fr_width == Columns)
4917#endif
4918 {
4919 if (height > room + room_cmdline)
4920 height = room + room_cmdline;
4921 break;
4922 }
4923#ifdef FEAT_VERTSPLIT
4924 frame_setheight(curfrp->fr_parent, height
4925 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4926#endif
4927 /*NOTREACHED*/
4928 }
4929
4930 /*
4931 * Compute the number of lines we will take from others frames (can be
4932 * negative!).
4933 */
4934 take = height - curfrp->fr_height;
4935
4936 /* If there is not enough room, also reduce the height of a window
4937 * with 'winfixheight' set. */
4938 if (height > room + room_cmdline - room_reserved)
4939 room_reserved = room + room_cmdline - height;
4940 /* If there is only a 'winfixheight' window and making the
4941 * window smaller, need to make the other window taller. */
4942 if (take < 0 && room - curfrp->fr_height < room_reserved)
4943 room_reserved = 0;
4944
4945 if (take > 0 && room_cmdline > 0)
4946 {
4947 /* use lines from cmdline first */
4948 if (take < room_cmdline)
4949 room_cmdline = take;
4950 take -= room_cmdline;
4951 topframe->fr_height += room_cmdline;
4952 }
4953
4954 /*
4955 * set the current frame to the new height
4956 */
4957 frame_new_height(curfrp, height, FALSE, FALSE);
4958
4959 /*
4960 * First take lines from the frames after the current frame. If
4961 * that is not enough, takes lines from frames above the current
4962 * frame.
4963 */
4964 for (run = 0; run < 2; ++run)
4965 {
4966 if (run == 0)
4967 frp = curfrp->fr_next; /* 1st run: start with next window */
4968 else
4969 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4970 while (frp != NULL && take != 0)
4971 {
4972 h = frame_minheight(frp, NULL);
4973 if (room_reserved > 0
4974 && frp->fr_win != NULL
4975 && frp->fr_win->w_p_wfh)
4976 {
4977 if (room_reserved >= frp->fr_height)
4978 room_reserved -= frp->fr_height;
4979 else
4980 {
4981 if (frp->fr_height - room_reserved > take)
4982 room_reserved = frp->fr_height - take;
4983 take -= frp->fr_height - room_reserved;
4984 frame_new_height(frp, room_reserved, FALSE, FALSE);
4985 room_reserved = 0;
4986 }
4987 }
4988 else
4989 {
4990 if (frp->fr_height - take < h)
4991 {
4992 take -= frp->fr_height - h;
4993 frame_new_height(frp, h, FALSE, FALSE);
4994 }
4995 else
4996 {
4997 frame_new_height(frp, frp->fr_height - take,
4998 FALSE, FALSE);
4999 take = 0;
5000 }
5001 }
5002 if (run == 0)
5003 frp = frp->fr_next;
5004 else
5005 frp = frp->fr_prev;
5006 }
5007 }
5008 }
5009}
5010
5011#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5012/*
5013 * Set current window width and take care of repositioning other windows to
5014 * fit around it.
5015 */
5016 void
5017win_setwidth(width)
5018 int width;
5019{
5020 win_setwidth_win(width, curwin);
5021}
5022
5023 void
5024win_setwidth_win(width, wp)
5025 int width;
5026 win_T *wp;
5027{
5028 /* Always keep current window at least one column wide, even when
5029 * 'winminwidth' is zero. */
5030 if (wp == curwin)
5031 {
5032 if (width < p_wmw)
5033 width = p_wmw;
5034 if (width == 0)
5035 width = 1;
5036 }
5037
5038 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5039
5040 /* recompute the window positions */
5041 (void)win_comp_pos();
5042
5043 redraw_all_later(NOT_VALID);
5044}
5045
5046/*
5047 * Set the width of a frame to "width" and take care that all frames and
5048 * windows inside it are resized. Also resize frames above and below if the
5049 * are in the same FR_ROW frame.
5050 *
5051 * Strategy is similar to frame_setheight().
5052 */
5053 static void
5054frame_setwidth(curfrp, width)
5055 frame_T *curfrp;
5056 int width;
5057{
5058 int room; /* total number of lines available */
5059 int take; /* number of lines taken from other windows */
5060 int run;
5061 frame_T *frp;
5062 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005063 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064
5065 /* If the width already is the desired value, nothing to do. */
5066 if (curfrp->fr_width == width)
5067 return;
5068
5069 if (curfrp->fr_parent == NULL)
5070 /* topframe: can't change width */
5071 return;
5072
5073 if (curfrp->fr_parent->fr_layout == FR_COL)
5074 {
5075 /* Column of frames: Also need to resize frames above and below of
5076 * this one. First check for the minimal width of these. */
5077 w = frame_minwidth(curfrp->fr_parent, NULL);
5078 if (width < w)
5079 width = w;
5080 frame_setwidth(curfrp->fr_parent, width);
5081 }
5082 else
5083 {
5084 /*
5085 * Row of frames: try to change only frames in this row.
5086 *
5087 * Do this twice:
5088 * 1: compute room available, if it's not enough try resizing the
5089 * containing frame.
5090 * 2: compute the room available and adjust the width to it.
5091 */
5092 for (run = 1; run <= 2; ++run)
5093 {
5094 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005095 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5097 frp = frp->fr_next)
5098 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005099 if (frp != curfrp
5100 && frp->fr_win != NULL
5101 && frp->fr_win->w_p_wfw)
5102 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 room += frp->fr_width;
5104 if (frp != curfrp)
5105 room -= frame_minwidth(frp, NULL);
5106 }
5107
5108 if (width <= room)
5109 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005110 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
5112 if (width > room)
5113 width = room;
5114 break;
5115 }
5116 frame_setwidth(curfrp->fr_parent, width
5117 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5118 }
5119
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 /*
5121 * Compute the number of lines we will take from others frames (can be
5122 * negative!).
5123 */
5124 take = width - curfrp->fr_width;
5125
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005126 /* If there is not enough room, also reduce the width of a window
5127 * with 'winfixwidth' set. */
5128 if (width > room - room_reserved)
5129 room_reserved = room - width;
5130 /* If there is only a 'winfixwidth' window and making the
5131 * window smaller, need to make the other window narrower. */
5132 if (take < 0 && room - curfrp->fr_width < room_reserved)
5133 room_reserved = 0;
5134
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 /*
5136 * set the current frame to the new width
5137 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005138 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139
5140 /*
5141 * First take lines from the frames right of the current frame. If
5142 * that is not enough, takes lines from frames left of the current
5143 * frame.
5144 */
5145 for (run = 0; run < 2; ++run)
5146 {
5147 if (run == 0)
5148 frp = curfrp->fr_next; /* 1st run: start with next window */
5149 else
5150 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5151 while (frp != NULL && take != 0)
5152 {
5153 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005154 if (room_reserved > 0
5155 && frp->fr_win != NULL
5156 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005158 if (room_reserved >= frp->fr_width)
5159 room_reserved -= frp->fr_width;
5160 else
5161 {
5162 if (frp->fr_width - room_reserved > take)
5163 room_reserved = frp->fr_width - take;
5164 take -= frp->fr_width - room_reserved;
5165 frame_new_width(frp, room_reserved, FALSE, FALSE);
5166 room_reserved = 0;
5167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 }
5169 else
5170 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005171 if (frp->fr_width - take < w)
5172 {
5173 take -= frp->fr_width - w;
5174 frame_new_width(frp, w, FALSE, FALSE);
5175 }
5176 else
5177 {
5178 frame_new_width(frp, frp->fr_width - take,
5179 FALSE, FALSE);
5180 take = 0;
5181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
5183 if (run == 0)
5184 frp = frp->fr_next;
5185 else
5186 frp = frp->fr_prev;
5187 }
5188 }
5189 }
5190}
5191#endif /* FEAT_VERTSPLIT */
5192
5193/*
5194 * Check 'winminheight' for a valid value.
5195 */
5196 void
5197win_setminheight()
5198{
5199 int room;
5200 int first = TRUE;
5201 win_T *wp;
5202
5203 /* loop until there is a 'winminheight' that is possible */
5204 while (p_wmh > 0)
5205 {
5206 /* TODO: handle vertical splits */
5207 room = -p_wh;
5208 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5209 room += wp->w_height - p_wmh;
5210 if (room >= 0)
5211 break;
5212 --p_wmh;
5213 if (first)
5214 {
5215 EMSG(_(e_noroom));
5216 first = FALSE;
5217 }
5218 }
5219}
5220
5221#ifdef FEAT_MOUSE
5222
5223/*
5224 * Status line of dragwin is dragged "offset" lines down (negative is up).
5225 */
5226 void
5227win_drag_status_line(dragwin, offset)
5228 win_T *dragwin;
5229 int offset;
5230{
5231 frame_T *curfr;
5232 frame_T *fr;
5233 int room;
5234 int row;
5235 int up; /* if TRUE, drag status line up, otherwise down */
5236 int n;
5237
5238 fr = dragwin->w_frame;
5239 curfr = fr;
5240 if (fr != topframe) /* more than one window */
5241 {
5242 fr = fr->fr_parent;
5243 /* When the parent frame is not a column of frames, its parent should
5244 * be. */
5245 if (fr->fr_layout != FR_COL)
5246 {
5247 curfr = fr;
5248 if (fr != topframe) /* only a row of windows, may drag statusline */
5249 fr = fr->fr_parent;
5250 }
5251 }
5252
5253 /* If this is the last frame in a column, may want to resize the parent
5254 * frame instead (go two up to skip a row of frames). */
5255 while (curfr != topframe && curfr->fr_next == NULL)
5256 {
5257 if (fr != topframe)
5258 fr = fr->fr_parent;
5259 curfr = fr;
5260 if (fr != topframe)
5261 fr = fr->fr_parent;
5262 }
5263
5264 if (offset < 0) /* drag up */
5265 {
5266 up = TRUE;
5267 offset = -offset;
5268 /* sum up the room of the current frame and above it */
5269 if (fr == curfr)
5270 {
5271 /* only one window */
5272 room = fr->fr_height - frame_minheight(fr, NULL);
5273 }
5274 else
5275 {
5276 room = 0;
5277 for (fr = fr->fr_child; ; fr = fr->fr_next)
5278 {
5279 room += fr->fr_height - frame_minheight(fr, NULL);
5280 if (fr == curfr)
5281 break;
5282 }
5283 }
5284 fr = curfr->fr_next; /* put fr at frame that grows */
5285 }
5286 else /* drag down */
5287 {
5288 up = FALSE;
5289 /*
5290 * Only dragging the last status line can reduce p_ch.
5291 */
5292 room = Rows - cmdline_row;
5293 if (curfr->fr_next == NULL)
5294 room -= 1;
5295 else
5296 room -= p_ch;
5297 if (room < 0)
5298 room = 0;
5299 /* sum up the room of frames below of the current one */
5300 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5301 room += fr->fr_height - frame_minheight(fr, NULL);
5302 fr = curfr; /* put fr at window that grows */
5303 }
5304
5305 if (room < offset) /* Not enough room */
5306 offset = room; /* Move as far as we can */
5307 if (offset <= 0)
5308 return;
5309
5310 /*
5311 * Grow frame fr by "offset" lines.
5312 * Doesn't happen when dragging the last status line up.
5313 */
5314 if (fr != NULL)
5315 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5316
5317 if (up)
5318 fr = curfr; /* current frame gets smaller */
5319 else
5320 fr = curfr->fr_next; /* next frame gets smaller */
5321
5322 /*
5323 * Now make the other frames smaller.
5324 */
5325 while (fr != NULL && offset > 0)
5326 {
5327 n = frame_minheight(fr, NULL);
5328 if (fr->fr_height - offset <= n)
5329 {
5330 offset -= fr->fr_height - n;
5331 frame_new_height(fr, n, !up, FALSE);
5332 }
5333 else
5334 {
5335 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5336 break;
5337 }
5338 if (up)
5339 fr = fr->fr_prev;
5340 else
5341 fr = fr->fr_next;
5342 }
5343 row = win_comp_pos();
5344 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5345 cmdline_row = row;
5346 p_ch = Rows - cmdline_row;
5347 if (p_ch < 1)
5348 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005349 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005350 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 showmode();
5352}
5353
5354#ifdef FEAT_VERTSPLIT
5355/*
5356 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5357 */
5358 void
5359win_drag_vsep_line(dragwin, offset)
5360 win_T *dragwin;
5361 int offset;
5362{
5363 frame_T *curfr;
5364 frame_T *fr;
5365 int room;
5366 int left; /* if TRUE, drag separator line left, otherwise right */
5367 int n;
5368
5369 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005370 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 return;
5372 curfr = fr;
5373 fr = fr->fr_parent;
5374 /* When the parent frame is not a row of frames, its parent should be. */
5375 if (fr->fr_layout != FR_ROW)
5376 {
5377 if (fr == topframe) /* only a column of windows (cannot happen?) */
5378 return;
5379 curfr = fr;
5380 fr = fr->fr_parent;
5381 }
5382
5383 /* If this is the last frame in a row, may want to resize a parent
5384 * frame instead. */
5385 while (curfr->fr_next == NULL)
5386 {
5387 if (fr == topframe)
5388 break;
5389 curfr = fr;
5390 fr = fr->fr_parent;
5391 if (fr != topframe)
5392 {
5393 curfr = fr;
5394 fr = fr->fr_parent;
5395 }
5396 }
5397
5398 if (offset < 0) /* drag left */
5399 {
5400 left = TRUE;
5401 offset = -offset;
5402 /* sum up the room of the current frame and left of it */
5403 room = 0;
5404 for (fr = fr->fr_child; ; fr = fr->fr_next)
5405 {
5406 room += fr->fr_width - frame_minwidth(fr, NULL);
5407 if (fr == curfr)
5408 break;
5409 }
5410 fr = curfr->fr_next; /* put fr at frame that grows */
5411 }
5412 else /* drag right */
5413 {
5414 left = FALSE;
5415 /* sum up the room of frames right of the current one */
5416 room = 0;
5417 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5418 room += fr->fr_width - frame_minwidth(fr, NULL);
5419 fr = curfr; /* put fr at window that grows */
5420 }
5421
5422 if (room < offset) /* Not enough room */
5423 offset = room; /* Move as far as we can */
5424 if (offset <= 0) /* No room at all, quit. */
5425 return;
5426
5427 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005428 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429
5430 /* shrink other frames: current and at the left or at the right */
5431 if (left)
5432 fr = curfr; /* current frame gets smaller */
5433 else
5434 fr = curfr->fr_next; /* next frame gets smaller */
5435
5436 while (fr != NULL && offset > 0)
5437 {
5438 n = frame_minwidth(fr, NULL);
5439 if (fr->fr_width - offset <= n)
5440 {
5441 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005442 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 }
5444 else
5445 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005446 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 break;
5448 }
5449 if (left)
5450 fr = fr->fr_prev;
5451 else
5452 fr = fr->fr_next;
5453 }
5454 (void)win_comp_pos();
5455 redraw_all_later(NOT_VALID);
5456}
5457#endif /* FEAT_VERTSPLIT */
5458#endif /* FEAT_MOUSE */
5459
5460#endif /* FEAT_WINDOWS */
5461
5462/*
5463 * Set the height of a window.
5464 * This takes care of the things inside the window, not what happens to the
5465 * window position, the frame or to other windows.
5466 */
5467 static void
5468win_new_height(wp, height)
5469 win_T *wp;
5470 int height;
5471{
5472 linenr_T lnum;
5473 int sline, line_size;
5474#define FRACTION_MULT 16384L
5475
5476 /* Don't want a negative height. Happens when splitting a tiny window.
5477 * Will equalize heights soon to fix it. */
5478 if (height < 0)
5479 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005480 if (wp->w_height == height)
5481 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
5483 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5484 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5485 + FRACTION_MULT / 2) / (long)wp->w_height;
5486
5487 wp->w_height = height;
5488 wp->w_skipcol = 0;
5489
5490 /* Don't change w_topline when height is zero. Don't set w_topline when
5491 * 'scrollbind' is set and this isn't the current window. */
5492 if (height > 0
5493#ifdef FEAT_SCROLLBIND
5494 && (!wp->w_p_scb || wp == curwin)
5495#endif
5496 )
5497 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005498 /*
5499 * Find a value for w_topline that shows the cursor at the same
5500 * relative position in the window as before (more or less).
5501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 lnum = wp->w_cursor.lnum;
5503 if (lnum < 1) /* can happen when starting up */
5504 lnum = 1;
5505 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5506 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5507 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005508
5509 if (sline >= 0)
5510 {
5511 /* Make sure the whole cursor line is visible, if possible. */
5512 int rows = plines_win(wp, lnum, FALSE);
5513
5514 if (sline > wp->w_height - rows)
5515 {
5516 sline = wp->w_height - rows;
5517 wp->w_wrow -= rows - line_size;
5518 }
5519 }
5520
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 if (sline < 0)
5522 {
5523 /*
5524 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005525 * Make cursor line the first line in the window. If not enough
5526 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 */
5528 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005529 if (wp->w_wrow >= wp->w_height
5530 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5531 {
5532 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5533 --wp->w_wrow;
5534 while (wp->w_wrow >= wp->w_height)
5535 {
5536 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5537 + win_col_off2(wp);
5538 --wp->w_wrow;
5539 }
5540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 }
5542 else
5543 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005544 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 {
5546#ifdef FEAT_FOLDING
5547 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5548 if (lnum == 1)
5549 {
5550 /* first line in buffer is folded */
5551 line_size = 1;
5552 --sline;
5553 break;
5554 }
5555#endif
5556 --lnum;
5557#ifdef FEAT_DIFF
5558 if (lnum == wp->w_topline)
5559 line_size = plines_win_nofill(wp, lnum, TRUE)
5560 + wp->w_topfill;
5561 else
5562#endif
5563 line_size = plines_win(wp, lnum, TRUE);
5564 sline -= line_size;
5565 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005566
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 if (sline < 0)
5568 {
5569 /*
5570 * Line we want at top would go off top of screen. Use next
5571 * line instead.
5572 */
5573#ifdef FEAT_FOLDING
5574 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5575#endif
5576 lnum++;
5577 wp->w_wrow -= line_size + sline;
5578 }
5579 else if (sline > 0)
5580 {
5581 /* First line of file reached, use that as topline. */
5582 lnum = 1;
5583 wp->w_wrow -= sline;
5584 }
5585 }
5586 set_topline(wp, lnum);
5587 }
5588
5589 if (wp == curwin)
5590 {
5591 if (p_so)
5592 update_topline();
5593 curs_columns(FALSE); /* validate w_wrow */
5594 }
5595 wp->w_prev_fraction_row = wp->w_wrow;
5596
5597 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005598 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599#ifdef FEAT_WINDOWS
5600 wp->w_redr_status = TRUE;
5601#endif
5602 invalidate_botline_win(wp);
5603}
5604
5605#ifdef FEAT_VERTSPLIT
5606/*
5607 * Set the width of a window.
5608 */
5609 static void
5610win_new_width(wp, width)
5611 win_T *wp;
5612 int width;
5613{
5614 wp->w_width = width;
5615 wp->w_lines_valid = 0;
5616 changed_line_abv_curs_win(wp);
5617 invalidate_botline_win(wp);
5618 if (wp == curwin)
5619 {
5620 update_topline();
5621 curs_columns(TRUE); /* validate w_wrow */
5622 }
5623 redraw_win_later(wp, NOT_VALID);
5624 wp->w_redr_status = TRUE;
5625}
5626#endif
5627
5628 void
5629win_comp_scroll(wp)
5630 win_T *wp;
5631{
5632 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5633 if (wp->w_p_scr == 0)
5634 wp->w_p_scr = 1;
5635}
5636
5637/*
5638 * command_height: called whenever p_ch has been changed
5639 */
5640 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005641command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
5643#ifdef FEAT_WINDOWS
5644 int h;
5645 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005646 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005648 /* Use the value of p_ch that we remembered. This is needed for when the
5649 * GUI starts up, we can't be sure in what order things happen. And when
5650 * p_ch was changed in another tab page. */
5651 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005652
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 /* Find bottom frame with width of screen. */
5654 frp = lastwin->w_frame;
5655# ifdef FEAT_VERTSPLIT
5656 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5657 frp = frp->fr_parent;
5658# endif
5659
5660 /* Avoid changing the height of a window with 'winfixheight' set. */
5661 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5662 && frp->fr_win->w_p_wfh)
5663 frp = frp->fr_prev;
5664
5665 if (starting != NO_SCREEN)
5666 {
5667 cmdline_row = Rows - p_ch;
5668
5669 if (p_ch > old_p_ch) /* p_ch got bigger */
5670 {
5671 while (p_ch > old_p_ch)
5672 {
5673 if (frp == NULL)
5674 {
5675 EMSG(_(e_noroom));
5676 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005677 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 cmdline_row = Rows - p_ch;
5679 break;
5680 }
5681 h = frp->fr_height - frame_minheight(frp, NULL);
5682 if (h > p_ch - old_p_ch)
5683 h = p_ch - old_p_ch;
5684 old_p_ch += h;
5685 frame_add_height(frp, -h);
5686 frp = frp->fr_prev;
5687 }
5688
5689 /* Recompute window positions. */
5690 (void)win_comp_pos();
5691
5692 /* clear the lines added to cmdline */
5693 if (full_screen)
5694 screen_fill((int)(cmdline_row), (int)Rows, 0,
5695 (int)Columns, ' ', ' ', 0);
5696 msg_row = cmdline_row;
5697 redraw_cmdline = TRUE;
5698 return;
5699 }
5700
5701 if (msg_row < cmdline_row)
5702 msg_row = cmdline_row;
5703 redraw_cmdline = TRUE;
5704 }
5705 frame_add_height(frp, (int)(old_p_ch - p_ch));
5706
5707 /* Recompute window positions. */
5708 if (frp != lastwin->w_frame)
5709 (void)win_comp_pos();
5710#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005712 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713#endif
5714}
5715
5716#if defined(FEAT_WINDOWS) || defined(PROTO)
5717/*
5718 * Resize frame "frp" to be "n" lines higher (negative for less high).
5719 * Also resize the frames it is contained in.
5720 */
5721 static void
5722frame_add_height(frp, n)
5723 frame_T *frp;
5724 int n;
5725{
5726 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5727 for (;;)
5728 {
5729 frp = frp->fr_parent;
5730 if (frp == NULL)
5731 break;
5732 frp->fr_height += n;
5733 }
5734}
5735
5736/*
5737 * Add or remove a status line for the bottom window(s), according to the
5738 * value of 'laststatus'.
5739 */
5740 void
5741last_status(morewin)
5742 int morewin; /* pretend there are two or more windows */
5743{
5744 /* Don't make a difference between horizontal or vertical split. */
5745 last_status_rec(topframe, (p_ls == 2
5746 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5747}
5748
5749 static void
5750last_status_rec(fr, statusline)
5751 frame_T *fr;
5752 int statusline;
5753{
5754 frame_T *fp;
5755 win_T *wp;
5756
5757 if (fr->fr_layout == FR_LEAF)
5758 {
5759 wp = fr->fr_win;
5760 if (wp->w_status_height != 0 && !statusline)
5761 {
5762 /* remove status line */
5763 win_new_height(wp, wp->w_height + 1);
5764 wp->w_status_height = 0;
5765 comp_col();
5766 }
5767 else if (wp->w_status_height == 0 && statusline)
5768 {
5769 /* Find a frame to take a line from. */
5770 fp = fr;
5771 while (fp->fr_height <= frame_minheight(fp, NULL))
5772 {
5773 if (fp == topframe)
5774 {
5775 EMSG(_(e_noroom));
5776 return;
5777 }
5778 /* In a column of frames: go to frame above. If already at
5779 * the top or in a row of frames: go to parent. */
5780 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5781 fp = fp->fr_prev;
5782 else
5783 fp = fp->fr_parent;
5784 }
5785 wp->w_status_height = 1;
5786 if (fp != fr)
5787 {
5788 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5789 frame_fix_height(wp);
5790 (void)win_comp_pos();
5791 }
5792 else
5793 win_new_height(wp, wp->w_height - 1);
5794 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005795 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 }
5797 }
5798#ifdef FEAT_VERTSPLIT
5799 else if (fr->fr_layout == FR_ROW)
5800 {
5801 /* vertically split windows, set status line for each one */
5802 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5803 last_status_rec(fp, statusline);
5804 }
5805#endif
5806 else
5807 {
5808 /* horizontally split window, set status line for last one */
5809 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5810 ;
5811 last_status_rec(fp, statusline);
5812 }
5813}
5814
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005815/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005816 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005817 */
5818 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005819tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005820{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005821#ifdef FEAT_GUI_TABLINE
5822 /* When the GUI has the tabline then this always returns zero. */
5823 if (gui_use_tabline())
5824 return 0;
5825#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005826 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005827 {
5828 case 0: return 0;
5829 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5830 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005831 return 1;
5832}
5833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834#endif /* FEAT_WINDOWS */
5835
5836#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5837/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005838 * Get the file name at the cursor.
5839 * If Visual mode is active, use the selected text if it's in one line.
5840 * Returns the name in allocated memory, NULL for failure.
5841 */
5842 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005843grab_file_name(count, file_lnum)
5844 long count;
5845 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005846{
5847# ifdef FEAT_VISUAL
5848 if (VIsual_active)
5849 {
5850 int len;
5851 char_u *ptr;
5852
5853 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5854 return NULL;
5855 return find_file_name_in_path(ptr, len,
5856 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5857 }
5858# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005859 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5860 file_lnum);
5861
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005862}
5863
5864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 * Return the file name under or after the cursor.
5866 *
5867 * The 'path' option is searched if the file name is not absolute.
5868 * The string returned has been alloc'ed and should be freed by the caller.
5869 * NULL is returned if the file name or file is not found.
5870 *
5871 * options:
5872 * FNAME_MESS give error messages
5873 * FNAME_EXP expand to path
5874 * FNAME_HYP check for hypertext link
5875 * FNAME_INCL apply "includeexpr"
5876 */
5877 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005878file_name_at_cursor(options, count, file_lnum)
5879 int options;
5880 long count;
5881 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882{
5883 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005884 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5885 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886}
5887
5888/*
5889 * Return the name of the file under or after ptr[col].
5890 * Otherwise like file_name_at_cursor().
5891 */
5892 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005893file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 char_u *line;
5895 int col;
5896 int options;
5897 long count;
5898 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005899 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 char_u *ptr;
5902 int len;
5903
5904 /*
5905 * search forward for what could be the start of a file name
5906 */
5907 ptr = line + col;
5908 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005909 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 if (*ptr == NUL) /* nothing found */
5911 {
5912 if (options & FNAME_MESS)
5913 EMSG(_("E446: No file name under cursor"));
5914 return NULL;
5915 }
5916
5917 /*
5918 * Search backward for first char of the file name.
5919 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5920 */
5921 while (ptr > line)
5922 {
5923#ifdef FEAT_MBYTE
5924 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5925 ptr -= len + 1;
5926 else
5927#endif
5928 if (vim_isfilec(ptr[-1])
5929 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5930 --ptr;
5931 else
5932 break;
5933 }
5934
5935 /*
5936 * Search forward for the last char of the file name.
5937 * Also allow "://" when ':' is not in 'isfname'.
5938 */
5939 len = 0;
5940 while (vim_isfilec(ptr[len])
5941 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5942#ifdef FEAT_MBYTE
5943 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005944 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 else
5946#endif
5947 ++len;
5948
5949 /*
5950 * If there is trailing punctuation, remove it.
5951 * But don't remove "..", could be a directory name.
5952 */
5953 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5954 && ptr[len - 2] != '.')
5955 --len;
5956
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005957 if (file_lnum != NULL)
5958 {
5959 char_u *p;
5960
5961 /* Get the number after the file name and a separator character */
5962 p = ptr + len;
5963 p = skipwhite(p);
5964 if (*p != NUL)
5965 {
5966 if (!isdigit(*p))
5967 ++p; /* skip the separator */
5968 p = skipwhite(p);
5969 if (isdigit(*p))
5970 *file_lnum = (int)getdigits(&p);
5971 }
5972 }
5973
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5975}
5976
5977# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5978static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5979
5980 static char_u *
5981eval_includeexpr(ptr, len)
5982 char_u *ptr;
5983 int len;
5984{
5985 char_u *res;
5986
5987 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005988 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005989 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 set_vim_var_string(VV_FNAME, NULL, 0);
5991 return res;
5992}
5993#endif
5994
5995/*
5996 * Return the name of the file ptr[len] in 'path'.
5997 * Otherwise like file_name_at_cursor().
5998 */
5999 char_u *
6000find_file_name_in_path(ptr, len, options, count, rel_fname)
6001 char_u *ptr;
6002 int len;
6003 int options;
6004 long count;
6005 char_u *rel_fname; /* file we are searching relative to */
6006{
6007 char_u *file_name;
6008 int c;
6009# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6010 char_u *tofree = NULL;
6011
6012 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6013 {
6014 tofree = eval_includeexpr(ptr, len);
6015 if (tofree != NULL)
6016 {
6017 ptr = tofree;
6018 len = (int)STRLEN(ptr);
6019 }
6020 }
6021# endif
6022
6023 if (options & FNAME_EXP)
6024 {
6025 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6026 TRUE, rel_fname);
6027
6028# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6029 /*
6030 * If the file could not be found in a normal way, try applying
6031 * 'includeexpr' (unless done already).
6032 */
6033 if (file_name == NULL
6034 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6035 {
6036 tofree = eval_includeexpr(ptr, len);
6037 if (tofree != NULL)
6038 {
6039 ptr = tofree;
6040 len = (int)STRLEN(ptr);
6041 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6042 TRUE, rel_fname);
6043 }
6044 }
6045# endif
6046 if (file_name == NULL && (options & FNAME_MESS))
6047 {
6048 c = ptr[len];
6049 ptr[len] = NUL;
6050 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6051 ptr[len] = c;
6052 }
6053
6054 /* Repeat finding the file "count" times. This matters when it
6055 * appears several times in the path. */
6056 while (file_name != NULL && --count > 0)
6057 {
6058 vim_free(file_name);
6059 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6060 }
6061 }
6062 else
6063 file_name = vim_strnsave(ptr, len);
6064
6065# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6066 vim_free(tofree);
6067# endif
6068
6069 return file_name;
6070}
6071#endif /* FEAT_SEARCHPATH */
6072
6073/*
6074 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6075 * Also check for ":\\", which MS Internet Explorer accepts, return
6076 * URL_BACKSLASH.
6077 */
6078 static int
6079path_is_url(p)
6080 char_u *p;
6081{
6082 if (STRNCMP(p, "://", (size_t)3) == 0)
6083 return URL_SLASH;
6084 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6085 return URL_BACKSLASH;
6086 return 0;
6087}
6088
6089/*
6090 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6091 * Return URL_BACKSLASH for "name:\\".
6092 * Return zero otherwise.
6093 */
6094 int
6095path_with_url(fname)
6096 char_u *fname;
6097{
6098 char_u *p;
6099
6100 for (p = fname; isalpha(*p); ++p)
6101 ;
6102 return path_is_url(p);
6103}
6104
6105/*
6106 * Return TRUE if "name" is a full (absolute) path name or URL.
6107 */
6108 int
6109vim_isAbsName(name)
6110 char_u *name;
6111{
6112 return (path_with_url(name) != 0 || mch_isFullName(name));
6113}
6114
6115/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006116 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 *
6118 * return FAIL for failure, OK otherwise
6119 */
6120 int
6121vim_FullName(fname, buf, len, force)
6122 char_u *fname, *buf;
6123 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006124 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125{
6126 int retval = OK;
6127 int url;
6128
6129 *buf = NUL;
6130 if (fname == NULL)
6131 return FAIL;
6132
6133 url = path_with_url(fname);
6134 if (!url)
6135 retval = mch_FullName(fname, buf, len, force);
6136 if (url || retval == FAIL)
6137 {
6138 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006139 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 }
6141#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6142 slash_adjust(buf);
6143#endif
6144 return retval;
6145}
6146
6147/*
6148 * Return the minimal number of rows that is needed on the screen to display
6149 * the current number of windows.
6150 */
6151 int
6152min_rows()
6153{
6154 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006155#ifdef FEAT_WINDOWS
6156 tabpage_T *tp;
6157 int n;
6158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159
6160 if (firstwin == NULL) /* not initialized yet */
6161 return MIN_LINES;
6162
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006164 total = 0;
6165 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6166 {
6167 n = frame_minheight(tp->tp_topframe, NULL);
6168 if (total < n)
6169 total = n;
6170 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006171 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006173 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006175 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 return total;
6177}
6178
6179/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006180 * Return TRUE if there is only one window (in the current tab page), not
6181 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006182 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 */
6184 int
6185only_one_window()
6186{
6187#ifdef FEAT_WINDOWS
6188 int count = 0;
6189 win_T *wp;
6190
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006191 /* If there is another tab page there always is another window. */
6192 if (first_tabpage->tp_next != NULL)
6193 return FALSE;
6194
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006196 if ((!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197# ifdef FEAT_QUICKFIX
6198 || wp->w_p_pvw
6199# endif
6200 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006201# ifdef FEAT_AUTOCMD
6202 && wp != aucmd_win
6203# endif
6204 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 ++count;
6206 return (count <= 1);
6207#else
6208 return TRUE;
6209#endif
6210}
6211
6212#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6213/*
6214 * Correct the cursor line number in other windows. Used after changing the
6215 * current buffer, and before applying autocommands.
6216 * When "do_curwin" is TRUE, also check current window.
6217 */
6218 void
6219check_lnums(do_curwin)
6220 int do_curwin;
6221{
6222 win_T *wp;
6223
6224#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006225 tabpage_T *tp;
6226
6227 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6229#else
6230 wp = curwin;
6231 if (do_curwin)
6232#endif
6233 {
6234 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6235 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6236 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6237 wp->w_topline = curbuf->b_ml.ml_line_count;
6238 }
6239}
6240#endif
6241
6242#if defined(FEAT_WINDOWS) || defined(PROTO)
6243
6244/*
6245 * A snapshot of the window sizes, to restore them after closing the help
6246 * window.
6247 * Only these fields are used:
6248 * fr_layout
6249 * fr_width
6250 * fr_height
6251 * fr_next
6252 * fr_child
6253 * fr_win (only valid for the old curwin, NULL otherwise)
6254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255
6256/*
6257 * Create a snapshot of the current frame sizes.
6258 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006259 void
6260make_snapshot(idx)
6261 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006263 clear_snapshot(curtab, idx);
6264 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265}
6266
6267 static void
6268make_snapshot_rec(fr, frp)
6269 frame_T *fr;
6270 frame_T **frp;
6271{
6272 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6273 if (*frp == NULL)
6274 return;
6275 (*frp)->fr_layout = fr->fr_layout;
6276# ifdef FEAT_VERTSPLIT
6277 (*frp)->fr_width = fr->fr_width;
6278# endif
6279 (*frp)->fr_height = fr->fr_height;
6280 if (fr->fr_next != NULL)
6281 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6282 if (fr->fr_child != NULL)
6283 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6284 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6285 (*frp)->fr_win = curwin;
6286}
6287
6288/*
6289 * Remove any existing snapshot.
6290 */
6291 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006292clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006293 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006294 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006296 clear_snapshot_rec(tp->tp_snapshot[idx]);
6297 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298}
6299
6300 static void
6301clear_snapshot_rec(fr)
6302 frame_T *fr;
6303{
6304 if (fr != NULL)
6305 {
6306 clear_snapshot_rec(fr->fr_next);
6307 clear_snapshot_rec(fr->fr_child);
6308 vim_free(fr);
6309 }
6310}
6311
6312/*
6313 * Restore a previously created snapshot, if there is any.
6314 * This is only done if the screen size didn't change and the window layout is
6315 * still the same.
6316 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006317 void
6318restore_snapshot(idx, close_curwin)
6319 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 int close_curwin; /* closing current window */
6321{
6322 win_T *wp;
6323
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006324 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006326 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006328 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6329 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006331 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 win_comp_pos();
6333 if (wp != NULL && close_curwin)
6334 win_goto(wp);
6335 redraw_all_later(CLEAR);
6336 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006337 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338}
6339
6340/*
6341 * Check if frames "sn" and "fr" have the same layout, same following frames
6342 * and same children.
6343 */
6344 static int
6345check_snapshot_rec(sn, fr)
6346 frame_T *sn;
6347 frame_T *fr;
6348{
6349 if (sn->fr_layout != fr->fr_layout
6350 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6351 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6352 || (sn->fr_next != NULL
6353 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6354 || (sn->fr_child != NULL
6355 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6356 return FAIL;
6357 return OK;
6358}
6359
6360/*
6361 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6362 * following frames and children.
6363 * Returns a pointer to the old current window, or NULL.
6364 */
6365 static win_T *
6366restore_snapshot_rec(sn, fr)
6367 frame_T *sn;
6368 frame_T *fr;
6369{
6370 win_T *wp = NULL;
6371 win_T *wp2;
6372
6373 fr->fr_height = sn->fr_height;
6374# ifdef FEAT_VERTSPLIT
6375 fr->fr_width = sn->fr_width;
6376# endif
6377 if (fr->fr_layout == FR_LEAF)
6378 {
6379 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6380# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006381 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382# endif
6383 wp = sn->fr_win;
6384 }
6385 if (sn->fr_next != NULL)
6386 {
6387 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6388 if (wp2 != NULL)
6389 wp = wp2;
6390 }
6391 if (sn->fr_child != NULL)
6392 {
6393 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6394 if (wp2 != NULL)
6395 wp = wp2;
6396 }
6397 return wp;
6398}
6399
6400#endif
6401
6402#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6403/*
6404 * Return TRUE if there is any vertically split window.
6405 */
6406 int
6407win_hasvertsplit()
6408{
6409 frame_T *fr;
6410
6411 if (topframe->fr_layout == FR_ROW)
6412 return TRUE;
6413
6414 if (topframe->fr_layout == FR_COL)
6415 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6416 if (fr->fr_layout == FR_ROW)
6417 return TRUE;
6418
6419 return FALSE;
6420}
6421#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006422
6423#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6424/*
6425 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006426 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006427 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6428 * If no particular ID is desired, -1 must be specified for 'id'.
6429 * Return ID of added match, -1 on failure.
6430 */
6431 int
6432match_add(wp, grp, pat, prio, id)
6433 win_T *wp;
6434 char_u *grp;
6435 char_u *pat;
6436 int prio;
6437 int id;
6438{
6439 matchitem_T *cur;
6440 matchitem_T *prev;
6441 matchitem_T *m;
6442 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006443 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006444
6445 if (*grp == NUL || *pat == NUL)
6446 return -1;
6447 if (id < -1 || id == 0)
6448 {
6449 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6450 return -1;
6451 }
6452 if (id != -1)
6453 {
6454 cur = wp->w_match_head;
6455 while (cur != NULL)
6456 {
6457 if (cur->id == id)
6458 {
6459 EMSGN("E801: ID already taken: %ld", id);
6460 return -1;
6461 }
6462 cur = cur->next;
6463 }
6464 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006465 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006466 {
6467 EMSG2(_(e_nogroup), grp);
6468 return -1;
6469 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006470 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006471 {
6472 EMSG2(_(e_invarg2), pat);
6473 return -1;
6474 }
6475
6476 /* Find available match ID. */
6477 while (id == -1)
6478 {
6479 cur = wp->w_match_head;
6480 while (cur != NULL && cur->id != wp->w_next_match_id)
6481 cur = cur->next;
6482 if (cur == NULL)
6483 id = wp->w_next_match_id;
6484 wp->w_next_match_id++;
6485 }
6486
6487 /* Build new match. */
6488 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6489 m->id = id;
6490 m->priority = prio;
6491 m->pattern = vim_strsave(pat);
6492 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006493 m->match.regprog = regprog;
6494 m->match.rmm_ic = FALSE;
6495 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006496
6497 /* Insert new match. The match list is in ascending order with regard to
6498 * the match priorities. */
6499 cur = wp->w_match_head;
6500 prev = cur;
6501 while (cur != NULL && prio >= cur->priority)
6502 {
6503 prev = cur;
6504 cur = cur->next;
6505 }
6506 if (cur == prev)
6507 wp->w_match_head = m;
6508 else
6509 prev->next = m;
6510 m->next = cur;
6511
6512 redraw_later(SOME_VALID);
6513 return id;
6514}
6515
6516/*
6517 * Delete match with ID 'id' in the match list of window 'wp'.
6518 * Print error messages if 'perr' is TRUE.
6519 */
6520 int
6521match_delete(wp, id, perr)
6522 win_T *wp;
6523 int id;
6524 int perr;
6525{
6526 matchitem_T *cur = wp->w_match_head;
6527 matchitem_T *prev = cur;
6528
6529 if (id < 1)
6530 {
6531 if (perr == TRUE)
6532 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6533 id);
6534 return -1;
6535 }
6536 while (cur != NULL && cur->id != id)
6537 {
6538 prev = cur;
6539 cur = cur->next;
6540 }
6541 if (cur == NULL)
6542 {
6543 if (perr == TRUE)
6544 EMSGN("E803: ID not found: %ld", id);
6545 return -1;
6546 }
6547 if (cur == prev)
6548 wp->w_match_head = cur->next;
6549 else
6550 prev->next = cur->next;
6551 vim_free(cur->match.regprog);
6552 vim_free(cur->pattern);
6553 vim_free(cur);
6554 redraw_later(SOME_VALID);
6555 return 0;
6556}
6557
6558/*
6559 * Delete all matches in the match list of window 'wp'.
6560 */
6561 void
6562clear_matches(wp)
6563 win_T *wp;
6564{
6565 matchitem_T *m;
6566
6567 while (wp->w_match_head != NULL)
6568 {
6569 m = wp->w_match_head->next;
6570 vim_free(wp->w_match_head->match.regprog);
6571 vim_free(wp->w_match_head->pattern);
6572 vim_free(wp->w_match_head);
6573 wp->w_match_head = m;
6574 }
6575 redraw_later(SOME_VALID);
6576}
6577
6578/*
6579 * Get match from ID 'id' in window 'wp'.
6580 * Return NULL if match not found.
6581 */
6582 matchitem_T *
6583get_match(wp, id)
6584 win_T *wp;
6585 int id;
6586{
6587 matchitem_T *cur = wp->w_match_head;
6588
6589 while (cur != NULL && cur->id != id)
6590 cur = cur->next;
6591 return cur;
6592}
6593#endif