blob: 7a2b1012fd38622b69bf134376b157c105152a3d [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 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200528 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000529 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
530 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000531 if (nchar == 'F' && lnum >= 0)
532 {
533 curwin->w_cursor.lnum = lnum;
534 check_cursor_lnum();
535 beginline(BL_SOL | BL_FIX);
536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 }
538 vim_free(ptr);
539 }
540 break;
541#endif
542
543#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000544/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 * new window -- webb
546 */
547 case 'i': /* Go to any match */
548 case Ctrl_I:
549 type = FIND_ANY;
550 /* FALLTHROUGH */
551 case 'd': /* Go to definition, using 'define' */
552 case Ctrl_D:
553 CHECK_CMDWIN
554 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
555 break;
556 find_pattern_in_path(ptr, 0, len, TRUE,
557 Prenum == 0 ? TRUE : FALSE, type,
558 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
559 curwin->w_set_curswant = TRUE;
560 break;
561#endif
562
Bram Moolenaar05159a02005-02-26 23:04:13 +0000563 case K_KENTER:
564 case CAR:
565#if defined(FEAT_QUICKFIX)
566 /*
567 * In a quickfix window a <CR> jumps to the error under the
568 * cursor in a new window.
569 */
570 if (bt_quickfix(curbuf))
571 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000572 sprintf((char *)cbuf, "split +%ld%s",
573 (long)curwin->w_cursor.lnum,
574 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000575 do_cmdline_cmd(cbuf);
576 }
577#endif
578 break;
579
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581/* CTRL-W g extended commands */
582 case 'g':
583 case Ctrl_G:
584 CHECK_CMDWIN
585#ifdef USE_ON_FLY_SCROLL
586 dont_scroll = TRUE; /* disallow scrolling here */
587#endif
588 ++no_mapping;
589 ++allow_keys; /* no mapping for xchar, but allow key codes */
590 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000591 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 --no_mapping;
594 --allow_keys;
595#ifdef FEAT_CMDL_INFO
596 (void)add_to_showcmd(xchar);
597#endif
598 switch (xchar)
599 {
600#if defined(FEAT_QUICKFIX)
601 case '}':
602 xchar = Ctrl_RSB;
603 if (Prenum)
604 g_do_tagpreview = Prenum;
605 else
606 g_do_tagpreview = p_pvh;
607 /*FALLTHROUGH*/
608#endif
609 case ']':
610 case Ctrl_RSB:
611#ifdef FEAT_VISUAL
612 reset_VIsual_and_resel(); /* stop Visual mode */
613#endif
614 if (Prenum)
615 postponed_split = Prenum;
616 else
617 postponed_split = -1;
618
619 /* Execute the command right here, required when
620 * "wincmd g}" was used in a function. */
621 do_nv_ident('g', xchar);
622 break;
623
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000624#ifdef FEAT_SEARCHPATH
625 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000626 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100627 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000628 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000629 goto wingotofile;
630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 default:
632 beep_flush();
633 break;
634 }
635 break;
636
637 default: beep_flush();
638 break;
639 }
640}
641
642/*
643 * split the current window, implements CTRL-W s and :split
644 *
645 * "size" is the height or width for the new window, 0 to use half of current
646 * height or width.
647 *
648 * "flags":
649 * WSP_ROOM: require enough room for new window
650 * WSP_VERT: vertical split.
651 * WSP_TOP: open window at the top-left of the shell (help window).
652 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
653 * WSP_HELP: creating the help window, keep layout snapshot
654 *
655 * return FAIL for failure, OK otherwise
656 */
657 int
658win_split(size, flags)
659 int size;
660 int flags;
661{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000662 /* When the ":tab" modifier was used open a new tab page instead. */
663 if (may_open_tabpage() == OK)
664 return OK;
665
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 /* Add flags from ":vertical", ":topleft" and ":botright". */
667 flags |= cmdmod.split;
668 if ((flags & WSP_TOP) && (flags & WSP_BOT))
669 {
670 EMSG(_("E442: Can't split topleft and botright at the same time"));
671 return FAIL;
672 }
673
674 /* When creating the help window make a snapshot of the window layout.
675 * Otherwise clear the snapshot, it's now invalid. */
676 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000677 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000679 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680
681 return win_split_ins(size, flags, NULL, 0);
682}
683
684/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000685 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 * When "newwin" is not NULL: insert this window at the far
687 * top/left/right/bottom.
688 * return FAIL for failure, OK otherwise
689 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000690 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691win_split_ins(size, flags, newwin, dir)
692 int size;
693 int flags;
694 win_T *newwin;
695 int dir;
696{
697 win_T *wp = newwin;
698 win_T *oldwin;
699 int new_size = size;
700 int i;
701 int need_status = 0;
702 int do_equal = FALSE;
703 int needed;
704 int available;
705 int oldwin_height = 0;
706 int layout;
707 frame_T *frp, *curfrp;
708 int before;
709
710 if (flags & WSP_TOP)
711 oldwin = firstwin;
712 else if (flags & WSP_BOT)
713 oldwin = lastwin;
714 else
715 oldwin = curwin;
716
717 /* add a status line when p_ls == 1 and splitting the first window */
718 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
719 {
720 if (oldwin->w_height <= p_wmh && newwin == NULL)
721 {
722 EMSG(_(e_noroom));
723 return FAIL;
724 }
725 need_status = STATUS_HEIGHT;
726 }
727
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000728#ifdef FEAT_GUI
729 /* May be needed for the scrollbars that are going to change. */
730 if (gui.in_use)
731 out_flush();
732#endif
733
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734#ifdef FEAT_VERTSPLIT
735 if (flags & WSP_VERT)
736 {
737 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
739 /*
740 * Check if we are able to split the current window and compute its
741 * width.
742 */
743 needed = p_wmw + 1;
744 if (flags & WSP_ROOM)
745 needed += p_wiw - p_wmw;
746 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
747 {
748 available = topframe->fr_width;
749 needed += frame_minwidth(topframe, NULL);
750 }
751 else
752 available = oldwin->w_width;
753 if (available < needed && newwin == NULL)
754 {
755 EMSG(_(e_noroom));
756 return FAIL;
757 }
758 if (new_size == 0)
759 new_size = oldwin->w_width / 2;
760 if (new_size > oldwin->w_width - p_wmw - 1)
761 new_size = oldwin->w_width - p_wmw - 1;
762 if (new_size < p_wmw)
763 new_size = p_wmw;
764
765 /* if it doesn't fit in the current window, need win_equal() */
766 if (oldwin->w_width - new_size - 1 < p_wmw)
767 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000768
769 /* We don't like to take lines for the new window from a
770 * 'winfixwidth' window. Take them from a window to the left or right
771 * instead, if possible. */
772 if (oldwin->w_p_wfw)
773 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000774
775 /* Only make all windows the same width if one of them (except oldwin)
776 * is wider than one of the split windows. */
777 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
778 && oldwin->w_frame->fr_parent != NULL)
779 {
780 frp = oldwin->w_frame->fr_parent->fr_child;
781 while (frp != NULL)
782 {
783 if (frp->fr_win != oldwin && frp->fr_win != NULL
784 && (frp->fr_win->w_width > new_size
785 || frp->fr_win->w_width > oldwin->w_width
786 - new_size - STATUS_HEIGHT))
787 {
788 do_equal = TRUE;
789 break;
790 }
791 frp = frp->fr_next;
792 }
793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 }
795 else
796#endif
797 {
798 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
800 /*
801 * Check if we are able to split the current window and compute its
802 * height.
803 */
804 needed = p_wmh + STATUS_HEIGHT + need_status;
805 if (flags & WSP_ROOM)
806 needed += p_wh - p_wmh;
807 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
808 {
809 available = topframe->fr_height;
810 needed += frame_minheight(topframe, NULL);
811 }
812 else
813 {
814 available = oldwin->w_height;
815 needed += p_wmh;
816 }
817 if (available < needed && newwin == NULL)
818 {
819 EMSG(_(e_noroom));
820 return FAIL;
821 }
822 oldwin_height = oldwin->w_height;
823 if (need_status)
824 {
825 oldwin->w_status_height = STATUS_HEIGHT;
826 oldwin_height -= STATUS_HEIGHT;
827 }
828 if (new_size == 0)
829 new_size = oldwin_height / 2;
830
831 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
832 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
833 if (new_size < p_wmh)
834 new_size = p_wmh;
835
836 /* if it doesn't fit in the current window, need win_equal() */
837 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
838 do_equal = TRUE;
839
840 /* We don't like to take lines for the new window from a
841 * 'winfixheight' window. Take them from a window above or below
842 * instead, if possible. */
843 if (oldwin->w_p_wfh)
844 {
845 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
846 oldwin);
847 oldwin_height = oldwin->w_height;
848 if (need_status)
849 oldwin_height -= STATUS_HEIGHT;
850 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000851
852 /* Only make all windows the same height if one of them (except oldwin)
853 * is higher than one of the split windows. */
854 if (!do_equal && p_ea && size == 0
855#ifdef FEAT_VERTSPLIT
856 && *p_ead != 'h'
857#endif
858 && oldwin->w_frame->fr_parent != NULL)
859 {
860 frp = oldwin->w_frame->fr_parent->fr_child;
861 while (frp != NULL)
862 {
863 if (frp->fr_win != oldwin && frp->fr_win != NULL
864 && (frp->fr_win->w_height > new_size
865 || frp->fr_win->w_height > oldwin_height - new_size
866 - STATUS_HEIGHT))
867 {
868 do_equal = TRUE;
869 break;
870 }
871 frp = frp->fr_next;
872 }
873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 }
875
876 /*
877 * allocate new window structure and link it in the window list
878 */
879 if ((flags & WSP_TOP) == 0
880 && ((flags & WSP_BOT)
881 || (flags & WSP_BELOW)
882 || (!(flags & WSP_ABOVE)
883 && (
884#ifdef FEAT_VERTSPLIT
885 (flags & WSP_VERT) ? p_spr :
886#endif
887 p_sb))))
888 {
889 /* new window below/right of current one */
890 if (newwin == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000891 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 else
893 win_append(oldwin, wp);
894 }
895 else
896 {
897 if (newwin == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000898 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 else
900 win_append(oldwin->w_prev, wp);
901 }
902
903 if (newwin == NULL)
904 {
905 if (wp == NULL)
906 return FAIL;
907
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000908 new_frame(wp);
909 if (wp->w_frame == NULL)
910 {
911 win_free(wp, NULL);
912 return FAIL;
913 }
914
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000915 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000916 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
918
919 /*
920 * Reorganise the tree of frames to insert the new window.
921 */
922 if (flags & (WSP_TOP | WSP_BOT))
923 {
924#ifdef FEAT_VERTSPLIT
925 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
926 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
927#else
928 if (topframe->fr_layout == FR_COL)
929#endif
930 {
931 curfrp = topframe->fr_child;
932 if (flags & WSP_BOT)
933 while (curfrp->fr_next != NULL)
934 curfrp = curfrp->fr_next;
935 }
936 else
937 curfrp = topframe;
938 before = (flags & WSP_TOP);
939 }
940 else
941 {
942 curfrp = oldwin->w_frame;
943 if (flags & WSP_BELOW)
944 before = FALSE;
945 else if (flags & WSP_ABOVE)
946 before = TRUE;
947 else
948#ifdef FEAT_VERTSPLIT
949 if (flags & WSP_VERT)
950 before = !p_spr;
951 else
952#endif
953 before = !p_sb;
954 }
955 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
956 {
957 /* Need to create a new frame in the tree to make a branch. */
958 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
959 *frp = *curfrp;
960 curfrp->fr_layout = layout;
961 frp->fr_parent = curfrp;
962 frp->fr_next = NULL;
963 frp->fr_prev = NULL;
964 curfrp->fr_child = frp;
965 curfrp->fr_win = NULL;
966 curfrp = frp;
967 if (frp->fr_win != NULL)
968 oldwin->w_frame = frp;
969 else
970 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
971 frp->fr_parent = curfrp;
972 }
973
974 if (newwin == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000975 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 else
977 frp = newwin->w_frame;
978 frp->fr_parent = curfrp->fr_parent;
979
980 /* Insert the new frame at the right place in the frame list. */
981 if (before)
982 frame_insert(curfrp, frp);
983 else
984 frame_append(curfrp, frp);
985
986#ifdef FEAT_VERTSPLIT
987 if (flags & WSP_VERT)
988 {
989 wp->w_p_scr = curwin->w_p_scr;
990 if (need_status)
991 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +0100992 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 oldwin->w_status_height = need_status;
994 }
995 if (flags & (WSP_TOP | WSP_BOT))
996 {
997 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000998 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +0100999 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 wp->w_status_height = (p_ls > 0);
1001 }
1002 else
1003 {
1004 /* height and row of new window is same as current window */
1005 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001006 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 wp->w_status_height = oldwin->w_status_height;
1008 }
1009 frp->fr_height = curfrp->fr_height;
1010
1011 /* "new_size" of the current window goes to the new window, use
1012 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001013 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 if (before)
1015 wp->w_vsep_width = 1;
1016 else
1017 {
1018 wp->w_vsep_width = oldwin->w_vsep_width;
1019 oldwin->w_vsep_width = 1;
1020 }
1021 if (flags & (WSP_TOP | WSP_BOT))
1022 {
1023 if (flags & WSP_BOT)
1024 frame_add_vsep(curfrp);
1025 /* Set width of neighbor frame */
1026 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001027 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1028 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 }
1030 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001031 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 if (before) /* new window left of current one */
1033 {
1034 wp->w_wincol = oldwin->w_wincol;
1035 oldwin->w_wincol += new_size + 1;
1036 }
1037 else /* new window right of current one */
1038 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1039 frame_fix_width(oldwin);
1040 frame_fix_width(wp);
1041 }
1042 else
1043#endif
1044 {
1045 /* width and column of new window is same as current window */
1046#ifdef FEAT_VERTSPLIT
1047 if (flags & (WSP_TOP | WSP_BOT))
1048 {
1049 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001050 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 wp->w_vsep_width = 0;
1052 }
1053 else
1054 {
1055 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001056 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 wp->w_vsep_width = oldwin->w_vsep_width;
1058 }
1059 frp->fr_width = curfrp->fr_width;
1060#endif
1061
1062 /* "new_size" of the current window goes to the new window, use
1063 * one row for the status line */
1064 win_new_height(wp, new_size);
1065 if (flags & (WSP_TOP | WSP_BOT))
1066 frame_new_height(curfrp, curfrp->fr_height
1067 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1068 else
1069 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1070 if (before) /* new window above current one */
1071 {
1072 wp->w_winrow = oldwin->w_winrow;
1073 wp->w_status_height = STATUS_HEIGHT;
1074 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1075 }
1076 else /* new window below current one */
1077 {
1078 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1079 wp->w_status_height = oldwin->w_status_height;
1080 oldwin->w_status_height = STATUS_HEIGHT;
1081 }
1082#ifdef FEAT_VERTSPLIT
1083 if (flags & WSP_BOT)
1084 frame_add_statusline(curfrp);
1085#endif
1086 frame_fix_height(wp);
1087 frame_fix_height(oldwin);
1088 }
1089
1090 if (flags & (WSP_TOP | WSP_BOT))
1091 (void)win_comp_pos();
1092
1093 /*
1094 * Both windows need redrawing
1095 */
1096 redraw_win_later(wp, NOT_VALID);
1097 wp->w_redr_status = TRUE;
1098 redraw_win_later(oldwin, NOT_VALID);
1099 oldwin->w_redr_status = TRUE;
1100
1101 if (need_status)
1102 {
1103 msg_row = Rows - 1;
1104 msg_col = sc_col;
1105 msg_clr_eos_force(); /* Old command/ruler may still be there */
1106 comp_col();
1107 msg_row = Rows - 1;
1108 msg_col = 0; /* put position back at start of line */
1109 }
1110
1111 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001112 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 */
1114 if (do_equal || dir != 0)
1115 win_equal(wp, TRUE,
1116#ifdef FEAT_VERTSPLIT
1117 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1118 : dir == 'h' ? 'b' :
1119#endif
1120 'v');
1121
1122 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1123 * size was given. */
1124#ifdef FEAT_VERTSPLIT
1125 if (flags & WSP_VERT)
1126 {
1127 i = p_wiw;
1128 if (size != 0)
1129 p_wiw = size;
1130
1131# ifdef FEAT_GUI
1132 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1133 if (gui.in_use)
1134 gui_init_which_components(NULL);
1135# endif
1136 }
1137 else
1138#endif
1139 {
1140 i = p_wh;
1141 if (size != 0)
1142 p_wh = size;
1143 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001144
1145 /*
1146 * make the new window the current window
1147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 win_enter(wp, FALSE);
1149#ifdef FEAT_VERTSPLIT
1150 if (flags & WSP_VERT)
1151 p_wiw = i;
1152 else
1153#endif
1154 p_wh = i;
1155
1156 return OK;
1157}
1158
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001159
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001160/*
1161 * Initialize window "newp" from window "oldp".
1162 * Used when splitting a window and when creating a new tab page.
1163 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001164 * WSP_NEWLOC may be specified in flags to prevent the location list from
1165 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001166 */
1167 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001168win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001169 win_T *newp;
1170 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001171 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001172{
1173 int i;
1174
1175 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001176#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001177 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001178#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001179 oldp->w_buffer->b_nwindows++;
1180 newp->w_cursor = oldp->w_cursor;
1181 newp->w_valid = 0;
1182 newp->w_curswant = oldp->w_curswant;
1183 newp->w_set_curswant = oldp->w_set_curswant;
1184 newp->w_topline = oldp->w_topline;
1185#ifdef FEAT_DIFF
1186 newp->w_topfill = oldp->w_topfill;
1187#endif
1188 newp->w_leftcol = oldp->w_leftcol;
1189 newp->w_pcmark = oldp->w_pcmark;
1190 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1191 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001192 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001193 newp->w_fraction = oldp->w_fraction;
1194 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1195#ifdef FEAT_JUMPLIST
1196 copy_jumplist(oldp, newp);
1197#endif
1198#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001199 if (flags & WSP_NEWLOC)
1200 {
1201 /* Don't copy the location list. */
1202 newp->w_llist = NULL;
1203 newp->w_llist_ref = NULL;
1204 }
1205 else
1206 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001207#endif
1208 if (oldp->w_localdir != NULL)
1209 newp->w_localdir = vim_strsave(oldp->w_localdir);
1210
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001211 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001212 for (i = 0; i < oldp->w_tagstacklen; i++)
1213 {
1214 newp->w_tagstack[i] = oldp->w_tagstack[i];
1215 if (newp->w_tagstack[i].tagname != NULL)
1216 newp->w_tagstack[i].tagname =
1217 vim_strsave(newp->w_tagstack[i].tagname);
1218 }
1219 newp->w_tagstackidx = oldp->w_tagstackidx;
1220 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001221# ifdef FEAT_FOLDING
1222 copyFoldingState(oldp, newp);
1223# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001224
1225 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001226
1227# ifdef FEAT_SYN_HL
1228 check_colorcolumn(newp);
1229# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001230}
1231
1232/*
1233 * Initialize window "newp" from window"old".
1234 * Only the essential things are copied.
1235 */
1236 static void
1237win_init_some(newp, oldp)
1238 win_T *newp;
1239 win_T *oldp;
1240{
1241 /* Use the same argument list. */
1242 newp->w_alist = oldp->w_alist;
1243 ++newp->w_alist->al_refcount;
1244 newp->w_arg_idx = oldp->w_arg_idx;
1245
1246 /* copy options from existing window */
1247 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001248}
1249
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250#endif /* FEAT_WINDOWS */
1251
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252#if defined(FEAT_WINDOWS) || defined(PROTO)
1253/*
1254 * Check if "win" is a pointer to an existing window.
1255 */
1256 int
1257win_valid(win)
1258 win_T *win;
1259{
1260 win_T *wp;
1261
1262 if (win == NULL)
1263 return FALSE;
1264 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1265 if (wp == win)
1266 return TRUE;
1267 return FALSE;
1268}
1269
1270/*
1271 * Return the number of windows.
1272 */
1273 int
1274win_count()
1275{
1276 win_T *wp;
1277 int count = 0;
1278
1279 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1280 ++count;
1281 return count;
1282}
1283
1284/*
1285 * Make "count" windows on the screen.
1286 * Return actual number of windows on the screen.
1287 * Must be called when there is just one window, filling the whole screen
1288 * (excluding the command line).
1289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 int
1291make_windows(count, vertical)
1292 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001293 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
1295 int maxcount;
1296 int todo;
1297
1298#ifdef FEAT_VERTSPLIT
1299 if (vertical)
1300 {
1301 /* Each windows needs at least 'winminwidth' lines and a separator
1302 * column. */
1303 maxcount = (curwin->w_width + curwin->w_vsep_width
1304 - (p_wiw - p_wmw)) / (p_wmw + 1);
1305 }
1306 else
1307#endif
1308 {
1309 /* Each window needs at least 'winminheight' lines and a status line. */
1310 maxcount = (curwin->w_height + curwin->w_status_height
1311 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1312 }
1313
1314 if (maxcount < 2)
1315 maxcount = 2;
1316 if (count > maxcount)
1317 count = maxcount;
1318
1319 /*
1320 * add status line now, otherwise first window will be too big
1321 */
1322 if (count > 1)
1323 last_status(TRUE);
1324
1325#ifdef FEAT_AUTOCMD
1326 /*
1327 * Don't execute autocommands while creating the windows. Must do that
1328 * when putting the buffers in the windows.
1329 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001330 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331#endif
1332
1333 /* todo is number of windows left to create */
1334 for (todo = count - 1; todo > 0; --todo)
1335#ifdef FEAT_VERTSPLIT
1336 if (vertical)
1337 {
1338 if (win_split(curwin->w_width - (curwin->w_width - todo)
1339 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1340 break;
1341 }
1342 else
1343#endif
1344 {
1345 if (win_split(curwin->w_height - (curwin->w_height - todo
1346 * STATUS_HEIGHT) / (todo + 1)
1347 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1348 break;
1349 }
1350
1351#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001352 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353#endif
1354
1355 /* return actual number of windows */
1356 return (count - todo);
1357}
1358
1359/*
1360 * Exchange current and next window
1361 */
1362 static void
1363win_exchange(Prenum)
1364 long Prenum;
1365{
1366 frame_T *frp;
1367 frame_T *frp2;
1368 win_T *wp;
1369 win_T *wp2;
1370 int temp;
1371
1372 if (lastwin == firstwin) /* just one window */
1373 {
1374 beep_flush();
1375 return;
1376 }
1377
1378#ifdef FEAT_GUI
1379 need_mouse_correct = TRUE;
1380#endif
1381
1382 /*
1383 * find window to exchange with
1384 */
1385 if (Prenum)
1386 {
1387 frp = curwin->w_frame->fr_parent->fr_child;
1388 while (frp != NULL && --Prenum > 0)
1389 frp = frp->fr_next;
1390 }
1391 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1392 frp = curwin->w_frame->fr_next;
1393 else /* Swap last window in row/col with previous */
1394 frp = curwin->w_frame->fr_prev;
1395
1396 /* We can only exchange a window with another window, not with a frame
1397 * containing windows. */
1398 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1399 return;
1400 wp = frp->fr_win;
1401
1402/*
1403 * 1. remove curwin from the list. Remember after which window it was in wp2
1404 * 2. insert curwin before wp in the list
1405 * if wp != wp2
1406 * 3. remove wp from the list
1407 * 4. insert wp after wp2
1408 * 5. exchange the status line height and vsep width.
1409 */
1410 wp2 = curwin->w_prev;
1411 frp2 = curwin->w_frame->fr_prev;
1412 if (wp->w_prev != curwin)
1413 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001414 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 frame_remove(curwin->w_frame);
1416 win_append(wp->w_prev, curwin);
1417 frame_insert(frp, curwin->w_frame);
1418 }
1419 if (wp != wp2)
1420 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001421 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 frame_remove(wp->w_frame);
1423 win_append(wp2, wp);
1424 if (frp2 == NULL)
1425 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1426 else
1427 frame_append(frp2, wp->w_frame);
1428 }
1429 temp = curwin->w_status_height;
1430 curwin->w_status_height = wp->w_status_height;
1431 wp->w_status_height = temp;
1432#ifdef FEAT_VERTSPLIT
1433 temp = curwin->w_vsep_width;
1434 curwin->w_vsep_width = wp->w_vsep_width;
1435 wp->w_vsep_width = temp;
1436
1437 /* If the windows are not in the same frame, exchange the sizes to avoid
1438 * messing up the window layout. Otherwise fix the frame sizes. */
1439 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1440 {
1441 temp = curwin->w_height;
1442 curwin->w_height = wp->w_height;
1443 wp->w_height = temp;
1444 temp = curwin->w_width;
1445 curwin->w_width = wp->w_width;
1446 wp->w_width = temp;
1447 }
1448 else
1449 {
1450 frame_fix_height(curwin);
1451 frame_fix_height(wp);
1452 frame_fix_width(curwin);
1453 frame_fix_width(wp);
1454 }
1455#endif
1456
1457 (void)win_comp_pos(); /* recompute window positions */
1458
1459 win_enter(wp, TRUE);
1460 redraw_later(CLEAR);
1461}
1462
1463/*
1464 * rotate windows: if upwards TRUE the second window becomes the first one
1465 * if upwards FALSE the first window becomes the second one
1466 */
1467 static void
1468win_rotate(upwards, count)
1469 int upwards;
1470 int count;
1471{
1472 win_T *wp1;
1473 win_T *wp2;
1474 frame_T *frp;
1475 int n;
1476
1477 if (firstwin == lastwin) /* nothing to do */
1478 {
1479 beep_flush();
1480 return;
1481 }
1482
1483#ifdef FEAT_GUI
1484 need_mouse_correct = TRUE;
1485#endif
1486
1487#ifdef FEAT_VERTSPLIT
1488 /* Check if all frames in this row/col have one window. */
1489 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1490 frp = frp->fr_next)
1491 if (frp->fr_win == NULL)
1492 {
1493 EMSG(_("E443: Cannot rotate when another window is split"));
1494 return;
1495 }
1496#endif
1497
1498 while (count--)
1499 {
1500 if (upwards) /* first window becomes last window */
1501 {
1502 /* remove first window/frame from the list */
1503 frp = curwin->w_frame->fr_parent->fr_child;
1504 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001505 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 frame_remove(frp);
1507
1508 /* find last frame and append removed window/frame after it */
1509 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1510 ;
1511 win_append(frp->fr_win, wp1);
1512 frame_append(frp, wp1->w_frame);
1513
1514 wp2 = frp->fr_win; /* previously last window */
1515 }
1516 else /* last window becomes first window */
1517 {
1518 /* find last window/frame in the list and remove it */
1519 for (frp = curwin->w_frame; frp->fr_next != NULL;
1520 frp = frp->fr_next)
1521 ;
1522 wp1 = frp->fr_win;
1523 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001524 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 frame_remove(frp);
1526
1527 /* append the removed window/frame before the first in the list */
1528 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1529 frame_insert(frp->fr_parent->fr_child, frp);
1530 }
1531
1532 /* exchange status height and vsep width of old and new last window */
1533 n = wp2->w_status_height;
1534 wp2->w_status_height = wp1->w_status_height;
1535 wp1->w_status_height = n;
1536 frame_fix_height(wp1);
1537 frame_fix_height(wp2);
1538#ifdef FEAT_VERTSPLIT
1539 n = wp2->w_vsep_width;
1540 wp2->w_vsep_width = wp1->w_vsep_width;
1541 wp1->w_vsep_width = n;
1542 frame_fix_width(wp1);
1543 frame_fix_width(wp2);
1544#endif
1545
1546 /* recompute w_winrow and w_wincol for all windows */
1547 (void)win_comp_pos();
1548 }
1549
1550 redraw_later(CLEAR);
1551}
1552
1553/*
1554 * Move the current window to the very top/bottom/left/right of the screen.
1555 */
1556 static void
1557win_totop(size, flags)
1558 int size;
1559 int flags;
1560{
1561 int dir;
1562 int height = curwin->w_height;
1563
1564 if (lastwin == firstwin)
1565 {
1566 beep_flush();
1567 return;
1568 }
1569
1570 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001571 (void)winframe_remove(curwin, &dir, NULL);
1572 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 last_status(FALSE); /* may need to remove last status line */
1574 (void)win_comp_pos(); /* recompute window positions */
1575
1576 /* Split a window on the desired side and put the window there. */
1577 (void)win_split_ins(size, flags, curwin, dir);
1578 if (!(flags & WSP_VERT))
1579 {
1580 win_setheight(height);
1581 if (p_ea)
1582 win_equal(curwin, TRUE, 'v');
1583 }
1584
1585#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1586 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1587 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001588 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590}
1591
1592/*
1593 * Move window "win1" to below/right of "win2" and make "win1" the current
1594 * window. Only works within the same frame!
1595 */
1596 void
1597win_move_after(win1, win2)
1598 win_T *win1, *win2;
1599{
1600 int height;
1601
1602 /* check if the arguments are reasonable */
1603 if (win1 == win2)
1604 return;
1605
1606 /* check if there is something to do */
1607 if (win2->w_next != win1)
1608 {
1609 /* may need move the status line/vertical separator of the last window
1610 * */
1611 if (win1 == lastwin)
1612 {
1613 height = win1->w_prev->w_status_height;
1614 win1->w_prev->w_status_height = win1->w_status_height;
1615 win1->w_status_height = height;
1616#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001617 if (win1->w_prev->w_vsep_width == 1)
1618 {
1619 /* Remove the vertical separator from the last-but-one window,
1620 * add it to the last window. Adjust the frame widths. */
1621 win1->w_prev->w_vsep_width = 0;
1622 win1->w_prev->w_frame->fr_width -= 1;
1623 win1->w_vsep_width = 1;
1624 win1->w_frame->fr_width += 1;
1625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626#endif
1627 }
1628 else if (win2 == lastwin)
1629 {
1630 height = win1->w_status_height;
1631 win1->w_status_height = win2->w_status_height;
1632 win2->w_status_height = height;
1633#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001634 if (win1->w_vsep_width == 1)
1635 {
1636 /* Remove the vertical separator from win1, add it to the last
1637 * window, win2. Adjust the frame widths. */
1638 win2->w_vsep_width = 1;
1639 win2->w_frame->fr_width += 1;
1640 win1->w_vsep_width = 0;
1641 win1->w_frame->fr_width -= 1;
1642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643#endif
1644 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001645 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 frame_remove(win1->w_frame);
1647 win_append(win2, win1);
1648 frame_append(win2->w_frame, win1->w_frame);
1649
1650 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1651 redraw_later(NOT_VALID);
1652 }
1653 win_enter(win1, FALSE);
1654}
1655
1656/*
1657 * Make all windows the same height.
1658 * 'next_curwin' will soon be the current window, make sure it has enough
1659 * rows.
1660 */
1661 void
1662win_equal(next_curwin, current, dir)
1663 win_T *next_curwin; /* pointer to current window to be or NULL */
1664 int current; /* do only frame with current window */
1665 int dir; /* 'v' for vertically, 'h' for horizontally,
1666 'b' for both, 0 for using p_ead */
1667{
1668 if (dir == 0)
1669#ifdef FEAT_VERTSPLIT
1670 dir = *p_ead;
1671#else
1672 dir = 'b';
1673#endif
1674 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001675 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001676 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677}
1678
1679/*
1680 * Set a frame to a new position and height, spreading the available room
1681 * equally over contained frames.
1682 * The window "next_curwin" (if not NULL) should at least get the size from
1683 * 'winheight' and 'winwidth' if possible.
1684 */
1685 static void
1686win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1687 win_T *next_curwin; /* pointer to current window to be or NULL */
1688 int current; /* do only frame with current window */
1689 frame_T *topfr; /* frame to set size off */
1690 int dir; /* 'v', 'h' or 'b', see win_equal() */
1691 int col; /* horizontal position for frame */
1692 int row; /* vertical position for frame */
1693 int width; /* new width of frame */
1694 int height; /* new height of frame */
1695{
1696 int n, m;
1697 int extra_sep = 0;
1698 int wincount, totwincount = 0;
1699 frame_T *fr;
1700 int next_curwin_size = 0;
1701 int room = 0;
1702 int new_size;
1703 int has_next_curwin = 0;
1704 int hnc;
1705
1706 if (topfr->fr_layout == FR_LEAF)
1707 {
1708 /* Set the width/height of this frame.
1709 * Redraw when size or position changes */
1710 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1711#ifdef FEAT_VERTSPLIT
1712 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1713#endif
1714 )
1715 {
1716 topfr->fr_win->w_winrow = row;
1717 frame_new_height(topfr, height, FALSE, FALSE);
1718#ifdef FEAT_VERTSPLIT
1719 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001720 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721#endif
1722 redraw_all_later(CLEAR);
1723 }
1724 }
1725#ifdef FEAT_VERTSPLIT
1726 else if (topfr->fr_layout == FR_ROW)
1727 {
1728 topfr->fr_width = width;
1729 topfr->fr_height = height;
1730
1731 if (dir != 'v') /* equalize frame widths */
1732 {
1733 /* Compute the maximum number of windows horizontally in this
1734 * frame. */
1735 n = frame_minwidth(topfr, NOWIN);
1736 /* add one for the rightmost window, it doesn't have a separator */
1737 if (col + width == Columns)
1738 extra_sep = 1;
1739 else
1740 extra_sep = 0;
1741 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001742 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001744 /*
1745 * Compute width for "next_curwin" window and room available for
1746 * other windows.
1747 * "m" is the minimal width when counting p_wiw for "next_curwin".
1748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 m = frame_minwidth(topfr, next_curwin);
1750 room = width - m;
1751 if (room < 0)
1752 {
1753 next_curwin_size = p_wiw + room;
1754 room = 0;
1755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 else
1757 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001758 next_curwin_size = -1;
1759 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1760 {
1761 /* If 'winfixwidth' set keep the window width if
1762 * possible.
1763 * Watch out for this window being the next_curwin. */
1764 if (frame_fixed_width(fr))
1765 {
1766 n = frame_minwidth(fr, NOWIN);
1767 new_size = fr->fr_width;
1768 if (frame_has_win(fr, next_curwin))
1769 {
1770 room += p_wiw - p_wmw;
1771 next_curwin_size = 0;
1772 if (new_size < p_wiw)
1773 new_size = p_wiw;
1774 }
1775 else
1776 /* These windows don't use up room. */
1777 totwincount -= (n + (fr->fr_next == NULL
1778 ? extra_sep : 0)) / (p_wmw + 1);
1779 room -= new_size - n;
1780 if (room < 0)
1781 {
1782 new_size += room;
1783 room = 0;
1784 }
1785 fr->fr_newwidth = new_size;
1786 }
1787 }
1788 if (next_curwin_size == -1)
1789 {
1790 if (!has_next_curwin)
1791 next_curwin_size = 0;
1792 else if (totwincount > 1
1793 && (room + (totwincount - 2))
1794 / (totwincount - 1) > p_wiw)
1795 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001796 /* Can make all windows wider than 'winwidth', spread
1797 * the room equally. */
1798 next_curwin_size = (room + p_wiw
1799 + (totwincount - 1) * p_wmw
1800 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001801 room -= next_curwin_size - p_wiw;
1802 }
1803 else
1804 next_curwin_size = p_wiw;
1805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001807
1808 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 --totwincount; /* don't count curwin */
1810 }
1811
1812 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1813 {
1814 n = m = 0;
1815 wincount = 1;
1816 if (fr->fr_next == NULL)
1817 /* last frame gets all that remains (avoid roundoff error) */
1818 new_size = width;
1819 else if (dir == 'v')
1820 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001821 else if (frame_fixed_width(fr))
1822 {
1823 new_size = fr->fr_newwidth;
1824 wincount = 0; /* doesn't count as a sizeable window */
1825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 else
1827 {
1828 /* Compute the maximum number of windows horiz. in "fr". */
1829 n = frame_minwidth(fr, NOWIN);
1830 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1831 / (p_wmw + 1);
1832 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001833 if (has_next_curwin)
1834 hnc = frame_has_win(fr, next_curwin);
1835 else
1836 hnc = FALSE;
1837 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001839 if (totwincount == 0)
1840 new_size = room;
1841 else
1842 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001844 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 {
1846 next_curwin_size -= p_wiw - (m - n);
1847 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001848 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001850 else
1851 room -= new_size;
1852 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
1854
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001855 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 * window, unless equalizing all frames. */
1857 if (!current || dir != 'v' || topfr->fr_parent != NULL
1858 || (new_size != fr->fr_width)
1859 || frame_has_win(fr, next_curwin))
1860 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001861 new_size, height);
1862 col += new_size;
1863 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 totwincount -= wincount;
1865 }
1866 }
1867#endif
1868 else /* topfr->fr_layout == FR_COL */
1869 {
1870#ifdef FEAT_VERTSPLIT
1871 topfr->fr_width = width;
1872#endif
1873 topfr->fr_height = height;
1874
1875 if (dir != 'h') /* equalize frame heights */
1876 {
1877 /* Compute maximum number of windows vertically in this frame. */
1878 n = frame_minheight(topfr, NOWIN);
1879 /* add one for the bottom window if it doesn't have a statusline */
1880 if (row + height == cmdline_row && p_ls == 0)
1881 extra_sep = 1;
1882 else
1883 extra_sep = 0;
1884 totwincount = (n + extra_sep) / (p_wmh + 1);
1885 has_next_curwin = frame_has_win(topfr, next_curwin);
1886
1887 /*
1888 * Compute height for "next_curwin" window and room available for
1889 * other windows.
1890 * "m" is the minimal height when counting p_wh for "next_curwin".
1891 */
1892 m = frame_minheight(topfr, next_curwin);
1893 room = height - m;
1894 if (room < 0)
1895 {
1896 /* The room is less then 'winheight', use all space for the
1897 * current window. */
1898 next_curwin_size = p_wh + room;
1899 room = 0;
1900 }
1901 else
1902 {
1903 next_curwin_size = -1;
1904 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1905 {
1906 /* If 'winfixheight' set keep the window height if
1907 * possible.
1908 * Watch out for this window being the next_curwin. */
1909 if (frame_fixed_height(fr))
1910 {
1911 n = frame_minheight(fr, NOWIN);
1912 new_size = fr->fr_height;
1913 if (frame_has_win(fr, next_curwin))
1914 {
1915 room += p_wh - p_wmh;
1916 next_curwin_size = 0;
1917 if (new_size < p_wh)
1918 new_size = p_wh;
1919 }
1920 else
1921 /* These windows don't use up room. */
1922 totwincount -= (n + (fr->fr_next == NULL
1923 ? extra_sep : 0)) / (p_wmh + 1);
1924 room -= new_size - n;
1925 if (room < 0)
1926 {
1927 new_size += room;
1928 room = 0;
1929 }
1930 fr->fr_newheight = new_size;
1931 }
1932 }
1933 if (next_curwin_size == -1)
1934 {
1935 if (!has_next_curwin)
1936 next_curwin_size = 0;
1937 else if (totwincount > 1
1938 && (room + (totwincount - 2))
1939 / (totwincount - 1) > p_wh)
1940 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001941 /* can make all windows higher than 'winheight',
1942 * spread the room equally. */
1943 next_curwin_size = (room + p_wh
1944 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 + (totwincount - 1)) / totwincount;
1946 room -= next_curwin_size - p_wh;
1947 }
1948 else
1949 next_curwin_size = p_wh;
1950 }
1951 }
1952
1953 if (has_next_curwin)
1954 --totwincount; /* don't count curwin */
1955 }
1956
1957 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1958 {
1959 n = m = 0;
1960 wincount = 1;
1961 if (fr->fr_next == NULL)
1962 /* last frame gets all that remains (avoid roundoff error) */
1963 new_size = height;
1964 else if (dir == 'h')
1965 new_size = fr->fr_height;
1966 else if (frame_fixed_height(fr))
1967 {
1968 new_size = fr->fr_newheight;
1969 wincount = 0; /* doesn't count as a sizeable window */
1970 }
1971 else
1972 {
1973 /* Compute the maximum number of windows vert. in "fr". */
1974 n = frame_minheight(fr, NOWIN);
1975 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1976 / (p_wmh + 1);
1977 m = frame_minheight(fr, next_curwin);
1978 if (has_next_curwin)
1979 hnc = frame_has_win(fr, next_curwin);
1980 else
1981 hnc = FALSE;
1982 if (hnc) /* don't count next_curwin */
1983 --wincount;
1984 if (totwincount == 0)
1985 new_size = room;
1986 else
1987 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1988 / totwincount;
1989 if (hnc) /* add next_curwin size */
1990 {
1991 next_curwin_size -= p_wh - (m - n);
1992 new_size += next_curwin_size;
1993 room -= new_size - next_curwin_size;
1994 }
1995 else
1996 room -= new_size;
1997 new_size += n;
1998 }
1999 /* Skip frame that is full width when splitting or closing a
2000 * window, unless equalizing all frames. */
2001 if (!current || dir != 'h' || topfr->fr_parent != NULL
2002 || (new_size != fr->fr_height)
2003 || frame_has_win(fr, next_curwin))
2004 win_equal_rec(next_curwin, current, fr, dir, col, row,
2005 width, new_size);
2006 row += new_size;
2007 height -= new_size;
2008 totwincount -= wincount;
2009 }
2010 }
2011}
2012
2013/*
2014 * close all windows for buffer 'buf'
2015 */
2016 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002017close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002019 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002021 win_T *wp;
2022 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002023 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
2025 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002026
2027 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002029 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002031 win_close(wp, FALSE);
2032
2033 /* Start all over, autocommands may change the window layout. */
2034 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 }
2036 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002037 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002039
2040 /* Also check windows in other tab pages. */
2041 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2042 {
2043 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002044 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002045 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2046 if (wp->w_buffer == buf)
2047 {
2048 win_close_othertab(wp, FALSE, tp);
2049
2050 /* Start all over, the tab page may be closed and
2051 * autocommands may change the window layout. */
2052 nexttp = first_tabpage;
2053 break;
2054 }
2055 }
2056
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002058
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002059 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002060 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061}
2062
2063/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002064 * Return TRUE if the current window is the only window that exists (ignoring
2065 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002066 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002067 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002068 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002069last_window()
2070{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002071 return (one_window() && first_tabpage->tp_next == NULL);
2072}
2073
2074/*
2075 * Return TRUE if there is only one window other than "aucmd_win" in the
2076 * current tab page.
2077 */
2078 static int
2079one_window()
2080{
2081#ifdef FEAT_AUTOCMD
2082 win_T *wp;
2083 int seen_one = FALSE;
2084
2085 FOR_ALL_WINDOWS(wp)
2086 {
2087 if (wp != aucmd_win)
2088 {
2089 if (seen_one)
2090 return FALSE;
2091 seen_one = TRUE;
2092 }
2093 }
2094 return TRUE;
2095#else
2096 return firstwin == lastwin;
2097#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002098}
2099
2100/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002101 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 * If "free_buf" is TRUE related buffer may be unloaded.
2103 *
2104 * called by :quit, :close, :xit, :wq and findtag()
2105 */
2106 void
2107win_close(win, free_buf)
2108 win_T *win;
2109 int free_buf;
2110{
2111 win_T *wp;
2112#ifdef FEAT_AUTOCMD
2113 int other_buffer = FALSE;
2114#endif
2115 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 int dir;
2117 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002118 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002120 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
2122 EMSG(_("E444: Cannot close last window"));
2123 return;
2124 }
2125
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002126#ifdef FEAT_AUTOCMD
2127 if (win == aucmd_win)
2128 {
2129 EMSG(_("E813: Cannot close autocmd window"));
2130 return;
2131 }
2132 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2133 {
2134 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2135 return;
2136 }
2137#endif
2138
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002139 /*
2140 * When closing the last window in a tab page first go to another tab
2141 * page and then close the window and the tab page. This avoids that
2142 * curwin and curtab are not invalid while we are freeing memory, they may
2143 * be used in GUI events.
2144 */
2145 if (firstwin == lastwin)
2146 {
2147 goto_tabpage_tp(alt_tabpage());
2148 redraw_tabline = TRUE;
2149
2150 /* Safety check: Autocommands may have closed the window when jumping
2151 * to the other tab page. */
2152 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2153 {
2154 int h = tabline_height();
2155
2156 win_close_othertab(win, free_buf, prev_curtab);
2157 if (h != tabline_height())
2158 shell_new_rows();
2159 }
2160 return;
2161 }
2162
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 /* When closing the help window, try restoring a snapshot after closing
2164 * the window. Otherwise clear the snapshot, it's now invalid. */
2165 if (win->w_buffer->b_help)
2166 help_window = TRUE;
2167 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002168 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169
2170#ifdef FEAT_AUTOCMD
2171 if (win == curwin)
2172 {
2173 /*
2174 * Guess which window is going to be the new current window.
2175 * This may change because of the autocommands (sigh).
2176 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002177 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
2179 /*
2180 * Be careful: If autocommands delete the window, return now.
2181 */
2182 if (wp->w_buffer != curbuf)
2183 {
2184 other_buffer = TRUE;
2185 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002186 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 return;
2188 }
2189 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002190 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 return;
2192# ifdef FEAT_EVAL
2193 /* autocmds may abort script processing */
2194 if (aborting())
2195 return;
2196# endif
2197 }
2198#endif
2199
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002200#ifdef FEAT_GUI
2201 /* Avoid trouble with scrollbars that are going to be deleted in
2202 * win_free(). */
2203 if (gui.in_use)
2204 out_flush();
2205#endif
2206
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 /*
2208 * Close the link to the buffer.
2209 */
2210 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002211
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002213 * other window or moved to another tab page. */
2214 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 return;
2216
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002217 /* Free the memory used for the window. */
2218 wp = win_free_mem(win, &dir, NULL);
2219
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 /* Make sure curwin isn't invalid. It can cause severe trouble when
2221 * printing an error message. For win_equal() curbuf needs to be valid
2222 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002223 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 {
2225 curwin = wp;
2226#ifdef FEAT_QUICKFIX
2227 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2228 {
2229 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002230 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 * finding another window to go to.
2232 */
2233 for (;;)
2234 {
2235 if (wp->w_next == NULL)
2236 wp = firstwin;
2237 else
2238 wp = wp->w_next;
2239 if (wp == curwin)
2240 break;
2241 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2242 {
2243 curwin = wp;
2244 break;
2245 }
2246 }
2247 }
2248#endif
2249 curbuf = curwin->w_buffer;
2250 close_curwin = TRUE;
2251 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002252 if (p_ea
2253#ifdef FEAT_VERTSPLIT
2254 && (*p_ead == 'b' || *p_ead == dir)
2255#endif
2256 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 win_equal(curwin, TRUE,
2258#ifdef FEAT_VERTSPLIT
2259 dir
2260#else
2261 0
2262#endif
2263 );
2264 else
2265 win_comp_pos();
2266 if (close_curwin)
2267 {
2268 win_enter_ext(wp, FALSE, TRUE);
2269#ifdef FEAT_AUTOCMD
2270 if (other_buffer)
2271 /* careful: after this wp and win may be invalid! */
2272 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2273#endif
2274 }
2275
2276 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002277 * If last window has a status line now and we don't want one,
2278 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 */
2280 last_status(FALSE);
2281
2282 /* After closing the help window, try restoring the window layout from
2283 * before it was opened. */
2284 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002285 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
2287#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2288 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2289 if (gui.in_use && !win_hasvertsplit())
2290 gui_init_which_components(NULL);
2291#endif
2292
2293 redraw_all_later(NOT_VALID);
2294}
2295
2296/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002297 * Close window "win" in tab page "tp", which is not the current tab page.
2298 * This may be the last window ih that tab page and result in closing the tab,
2299 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002300 * Caller must check if buffer is hidden and whether the tabline needs to be
2301 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002302 */
2303 void
2304win_close_othertab(win, free_buf, tp)
2305 win_T *win;
2306 int free_buf;
2307 tabpage_T *tp;
2308{
2309 win_T *wp;
2310 int dir;
2311 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002312 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002313
2314 /* Close the link to the buffer. */
2315 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2316
2317 /* Careful: Autocommands may have closed the tab page or made it the
2318 * current tab page. */
2319 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2320 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002321 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002322 return;
2323
2324 /* Autocommands may have closed the window already. */
2325 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2326 ;
2327 if (wp == NULL)
2328 return;
2329
Bram Moolenaarf740b292006-02-16 22:11:02 +00002330 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002331 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002332 {
2333 if (tp == first_tabpage)
2334 first_tabpage = tp->tp_next;
2335 else
2336 {
2337 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2338 ptp = ptp->tp_next)
2339 ;
2340 if (ptp == NULL)
2341 {
2342 EMSG2(_(e_intern2), "win_close_othertab()");
2343 return;
2344 }
2345 ptp->tp_next = tp->tp_next;
2346 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002347 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002348 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002349
2350 /* Free the memory used for the window. */
2351 win_free_mem(win, &dir, tp);
2352
2353 if (free_tp)
2354 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002355}
2356
2357/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002358 * Free the memory used for a window.
2359 * Returns a pointer to the window that got the freed up space.
2360 */
2361 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002362win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002363 win_T *win;
2364 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002365 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002366{
2367 frame_T *frp;
2368 win_T *wp;
2369
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002370 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002371 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002372 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002373 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002374 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002375
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002376 /* When deleting the current window of another tab page select a new
2377 * current window. */
2378 if (tp != NULL && win == tp->tp_curwin)
2379 tp->tp_curwin = wp;
2380
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002381 return wp;
2382}
2383
2384#if defined(EXITFREE) || defined(PROTO)
2385 void
2386win_free_all()
2387{
2388 int dummy;
2389
Bram Moolenaarf740b292006-02-16 22:11:02 +00002390# ifdef FEAT_WINDOWS
2391 while (first_tabpage->tp_next != NULL)
2392 tabpage_close(TRUE);
2393# endif
2394
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002395# ifdef FEAT_AUTOCMD
2396 if (aucmd_win != NULL)
2397 {
2398 (void)win_free_mem(aucmd_win, &dummy, NULL);
2399 aucmd_win = NULL;
2400 }
2401# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002402
2403 while (firstwin != NULL)
2404 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002405}
2406#endif
2407
2408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 * Remove a window and its frame from the tree of frames.
2410 * Returns a pointer to the window that got the freed up space.
2411 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002412 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002413winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002415 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002416 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417{
2418 frame_T *frp, *frp2, *frp3;
2419 frame_T *frp_close = win->w_frame;
2420 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
2422 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002423 * If there is only one window there is nothing to remove.
2424 */
2425 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2426 return NULL;
2427
2428 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 * Remove the window from its frame.
2430 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002431 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 wp = frame2win(frp2);
2433
2434 /* Remove this frame from the list of frames. */
2435 frame_remove(frp_close);
2436
2437#ifdef FEAT_VERTSPLIT
2438 if (frp_close->fr_parent->fr_layout == FR_COL)
2439 {
2440#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002441 /* When 'winfixheight' is set, try to find another frame in the column
2442 * (as close to the closed frame as possible) to distribute the height
2443 * to. */
2444 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2445 {
2446 frp = frp_close->fr_prev;
2447 frp3 = frp_close->fr_next;
2448 while (frp != NULL || frp3 != NULL)
2449 {
2450 if (frp != NULL)
2451 {
2452 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2453 {
2454 frp2 = frp;
2455 wp = frp->fr_win;
2456 break;
2457 }
2458 frp = frp->fr_prev;
2459 }
2460 if (frp3 != NULL)
2461 {
2462 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2463 {
2464 frp2 = frp3;
2465 wp = frp3->fr_win;
2466 break;
2467 }
2468 frp3 = frp3->fr_next;
2469 }
2470 }
2471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2473 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474#ifdef FEAT_VERTSPLIT
2475 *dirp = 'v';
2476 }
2477 else
2478 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002479 /* When 'winfixwidth' is set, try to find another frame in the column
2480 * (as close to the closed frame as possible) to distribute the width
2481 * to. */
2482 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2483 {
2484 frp = frp_close->fr_prev;
2485 frp3 = frp_close->fr_next;
2486 while (frp != NULL || frp3 != NULL)
2487 {
2488 if (frp != NULL)
2489 {
2490 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2491 {
2492 frp2 = frp;
2493 wp = frp->fr_win;
2494 break;
2495 }
2496 frp = frp->fr_prev;
2497 }
2498 if (frp3 != NULL)
2499 {
2500 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2501 {
2502 frp2 = frp3;
2503 wp = frp3->fr_win;
2504 break;
2505 }
2506 frp3 = frp3->fr_next;
2507 }
2508 }
2509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002511 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 *dirp = 'h';
2513 }
2514#endif
2515
2516 /* If rows/columns go to a window below/right its positions need to be
2517 * updated. Can only be done after the sizes have been updated. */
2518 if (frp2 == frp_close->fr_next)
2519 {
2520 int row = win->w_winrow;
2521 int col = W_WINCOL(win);
2522
2523 frame_comp_pos(frp2, &row, &col);
2524 }
2525
2526 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2527 {
2528 /* There is no other frame in this list, move its info to the parent
2529 * and remove it. */
2530 frp2->fr_parent->fr_layout = frp2->fr_layout;
2531 frp2->fr_parent->fr_child = frp2->fr_child;
2532 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2533 frp->fr_parent = frp2->fr_parent;
2534 frp2->fr_parent->fr_win = frp2->fr_win;
2535 if (frp2->fr_win != NULL)
2536 frp2->fr_win->w_frame = frp2->fr_parent;
2537 frp = frp2->fr_parent;
2538 vim_free(frp2);
2539
2540 frp2 = frp->fr_parent;
2541 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2542 {
2543 /* The frame above the parent has the same layout, have to merge
2544 * the frames into this list. */
2545 if (frp2->fr_child == frp)
2546 frp2->fr_child = frp->fr_child;
2547 frp->fr_child->fr_prev = frp->fr_prev;
2548 if (frp->fr_prev != NULL)
2549 frp->fr_prev->fr_next = frp->fr_child;
2550 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2551 {
2552 frp3->fr_parent = frp2;
2553 if (frp3->fr_next == NULL)
2554 {
2555 frp3->fr_next = frp->fr_next;
2556 if (frp->fr_next != NULL)
2557 frp->fr_next->fr_prev = frp3;
2558 break;
2559 }
2560 }
2561 vim_free(frp);
2562 }
2563 }
2564
2565 return wp;
2566}
2567
2568/*
2569 * Find out which frame is going to get the freed up space when "win" is
2570 * closed.
2571 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2572 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2573 * This makes opening a window and closing it immediately keep the same window
2574 * layout.
2575 */
2576 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002577win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002579 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
2581 frame_T *frp;
2582 int b;
2583
Bram Moolenaarf740b292006-02-16 22:11:02 +00002584 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002585 /* Last window in this tab page, will go to next tab page. */
2586 return alt_tabpage()->tp_curwin->w_frame;
2587
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 frp = win->w_frame;
2589#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002590 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 b = p_spr;
2592 else
2593#endif
2594 b = p_sb;
2595 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2596 return frp->fr_next;
2597 return frp->fr_prev;
2598}
2599
2600/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002601 * Return the tabpage that will be used if the current one is closed.
2602 */
2603 static tabpage_T *
2604alt_tabpage()
2605{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002606 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002607
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002608 /* Use the next tab page if possible. */
2609 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002610 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002611
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002612 /* Find the last but one tab page. */
2613 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2614 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002615 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002616}
2617
2618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 * Find the left-upper window in frame "frp".
2620 */
2621 static win_T *
2622frame2win(frp)
2623 frame_T *frp;
2624{
2625 while (frp->fr_win == NULL)
2626 frp = frp->fr_child;
2627 return frp->fr_win;
2628}
2629
2630/*
2631 * Return TRUE if frame "frp" contains window "wp".
2632 */
2633 static int
2634frame_has_win(frp, wp)
2635 frame_T *frp;
2636 win_T *wp;
2637{
2638 frame_T *p;
2639
2640 if (frp->fr_layout == FR_LEAF)
2641 return frp->fr_win == wp;
2642
2643 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2644 if (frame_has_win(p, wp))
2645 return TRUE;
2646 return FALSE;
2647}
2648
2649/*
2650 * Set a new height for a frame. Recursively sets the height for contained
2651 * frames and windows. Caller must take care of positions.
2652 */
2653 static void
2654frame_new_height(topfrp, height, topfirst, wfh)
2655 frame_T *topfrp;
2656 int height;
2657 int topfirst; /* resize topmost contained frame first */
2658 int wfh; /* obey 'winfixheight' when there is a choice;
2659 may cause the height not to be set */
2660{
2661 frame_T *frp;
2662 int extra_lines;
2663 int h;
2664
2665 if (topfrp->fr_win != NULL)
2666 {
2667 /* Simple case: just one window. */
2668 win_new_height(topfrp->fr_win,
2669 height - topfrp->fr_win->w_status_height);
2670 }
2671#ifdef FEAT_VERTSPLIT
2672 else if (topfrp->fr_layout == FR_ROW)
2673 {
2674 do
2675 {
2676 /* All frames in this row get the same new height. */
2677 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2678 {
2679 frame_new_height(frp, height, topfirst, wfh);
2680 if (frp->fr_height > height)
2681 {
2682 /* Could not fit the windows, make the whole row higher. */
2683 height = frp->fr_height;
2684 break;
2685 }
2686 }
2687 }
2688 while (frp != NULL);
2689 }
2690#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002691 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 {
2693 /* Complicated case: Resize a column of frames. Resize the bottom
2694 * frame first, frames above that when needed. */
2695
2696 frp = topfrp->fr_child;
2697 if (wfh)
2698 /* Advance past frames with one window with 'wfh' set. */
2699 while (frame_fixed_height(frp))
2700 {
2701 frp = frp->fr_next;
2702 if (frp == NULL)
2703 return; /* no frame without 'wfh', give up */
2704 }
2705 if (!topfirst)
2706 {
2707 /* Find the bottom frame of this column */
2708 while (frp->fr_next != NULL)
2709 frp = frp->fr_next;
2710 if (wfh)
2711 /* Advance back for frames with one window with 'wfh' set. */
2712 while (frame_fixed_height(frp))
2713 frp = frp->fr_prev;
2714 }
2715
2716 extra_lines = height - topfrp->fr_height;
2717 if (extra_lines < 0)
2718 {
2719 /* reduce height of contained frames, bottom or top frame first */
2720 while (frp != NULL)
2721 {
2722 h = frame_minheight(frp, NULL);
2723 if (frp->fr_height + extra_lines < h)
2724 {
2725 extra_lines += frp->fr_height - h;
2726 frame_new_height(frp, h, topfirst, wfh);
2727 }
2728 else
2729 {
2730 frame_new_height(frp, frp->fr_height + extra_lines,
2731 topfirst, wfh);
2732 break;
2733 }
2734 if (topfirst)
2735 {
2736 do
2737 frp = frp->fr_next;
2738 while (wfh && frp != NULL && frame_fixed_height(frp));
2739 }
2740 else
2741 {
2742 do
2743 frp = frp->fr_prev;
2744 while (wfh && frp != NULL && frame_fixed_height(frp));
2745 }
2746 /* Increase "height" if we could not reduce enough frames. */
2747 if (frp == NULL)
2748 height -= extra_lines;
2749 }
2750 }
2751 else if (extra_lines > 0)
2752 {
2753 /* increase height of bottom or top frame */
2754 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2755 }
2756 }
2757 topfrp->fr_height = height;
2758}
2759
2760/*
2761 * Return TRUE if height of frame "frp" should not be changed because of
2762 * the 'winfixheight' option.
2763 */
2764 static int
2765frame_fixed_height(frp)
2766 frame_T *frp;
2767{
2768 /* frame with one window: fixed height if 'winfixheight' set. */
2769 if (frp->fr_win != NULL)
2770 return frp->fr_win->w_p_wfh;
2771
2772 if (frp->fr_layout == FR_ROW)
2773 {
2774 /* The frame is fixed height if one of the frames in the row is fixed
2775 * height. */
2776 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2777 if (frame_fixed_height(frp))
2778 return TRUE;
2779 return FALSE;
2780 }
2781
2782 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2783 * frames in the row are fixed height. */
2784 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2785 if (!frame_fixed_height(frp))
2786 return FALSE;
2787 return TRUE;
2788}
2789
2790#ifdef FEAT_VERTSPLIT
2791/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002792 * Return TRUE if width of frame "frp" should not be changed because of
2793 * the 'winfixwidth' option.
2794 */
2795 static int
2796frame_fixed_width(frp)
2797 frame_T *frp;
2798{
2799 /* frame with one window: fixed width if 'winfixwidth' set. */
2800 if (frp->fr_win != NULL)
2801 return frp->fr_win->w_p_wfw;
2802
2803 if (frp->fr_layout == FR_COL)
2804 {
2805 /* The frame is fixed width if one of the frames in the row is fixed
2806 * width. */
2807 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2808 if (frame_fixed_width(frp))
2809 return TRUE;
2810 return FALSE;
2811 }
2812
2813 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2814 * frames in the row are fixed width. */
2815 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2816 if (!frame_fixed_width(frp))
2817 return FALSE;
2818 return TRUE;
2819}
2820
2821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 * Add a status line to windows at the bottom of "frp".
2823 * Note: Does not check if there is room!
2824 */
2825 static void
2826frame_add_statusline(frp)
2827 frame_T *frp;
2828{
2829 win_T *wp;
2830
2831 if (frp->fr_layout == FR_LEAF)
2832 {
2833 wp = frp->fr_win;
2834 if (wp->w_status_height == 0)
2835 {
2836 if (wp->w_height > 0) /* don't make it negative */
2837 --wp->w_height;
2838 wp->w_status_height = STATUS_HEIGHT;
2839 }
2840 }
2841 else if (frp->fr_layout == FR_ROW)
2842 {
2843 /* Handle all the frames in the row. */
2844 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2845 frame_add_statusline(frp);
2846 }
2847 else /* frp->fr_layout == FR_COL */
2848 {
2849 /* Only need to handle the last frame in the column. */
2850 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2851 ;
2852 frame_add_statusline(frp);
2853 }
2854}
2855
2856/*
2857 * Set width of a frame. Handles recursively going through contained frames.
2858 * May remove separator line for windows at the right side (for win_close()).
2859 */
2860 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002861frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 frame_T *topfrp;
2863 int width;
2864 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002865 int wfw; /* obey 'winfixwidth' when there is a choice;
2866 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867{
2868 frame_T *frp;
2869 int extra_cols;
2870 int w;
2871 win_T *wp;
2872
2873 if (topfrp->fr_layout == FR_LEAF)
2874 {
2875 /* Simple case: just one window. */
2876 wp = topfrp->fr_win;
2877 /* Find out if there are any windows right of this one. */
2878 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2879 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2880 break;
2881 if (frp->fr_parent == NULL)
2882 wp->w_vsep_width = 0;
2883 win_new_width(wp, width - wp->w_vsep_width);
2884 }
2885 else if (topfrp->fr_layout == FR_COL)
2886 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002887 do
2888 {
2889 /* All frames in this column get the same new width. */
2890 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2891 {
2892 frame_new_width(frp, width, leftfirst, wfw);
2893 if (frp->fr_width > width)
2894 {
2895 /* Could not fit the windows, make whole column wider. */
2896 width = frp->fr_width;
2897 break;
2898 }
2899 }
2900 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
2902 else /* fr_layout == FR_ROW */
2903 {
2904 /* Complicated case: Resize a row of frames. Resize the rightmost
2905 * frame first, frames left of it when needed. */
2906
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002908 if (wfw)
2909 /* Advance past frames with one window with 'wfw' set. */
2910 while (frame_fixed_width(frp))
2911 {
2912 frp = frp->fr_next;
2913 if (frp == NULL)
2914 return; /* no frame without 'wfw', give up */
2915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002917 {
2918 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 while (frp->fr_next != NULL)
2920 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002921 if (wfw)
2922 /* Advance back for frames with one window with 'wfw' set. */
2923 while (frame_fixed_width(frp))
2924 frp = frp->fr_prev;
2925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
2927 extra_cols = width - topfrp->fr_width;
2928 if (extra_cols < 0)
2929 {
2930 /* reduce frame width, rightmost frame first */
2931 while (frp != NULL)
2932 {
2933 w = frame_minwidth(frp, NULL);
2934 if (frp->fr_width + extra_cols < w)
2935 {
2936 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002937 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 }
2939 else
2940 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002941 frame_new_width(frp, frp->fr_width + extra_cols,
2942 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 break;
2944 }
2945 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002946 {
2947 do
2948 frp = frp->fr_next;
2949 while (wfw && frp != NULL && frame_fixed_width(frp));
2950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002952 {
2953 do
2954 frp = frp->fr_prev;
2955 while (wfw && frp != NULL && frame_fixed_width(frp));
2956 }
2957 /* Increase "width" if we could not reduce enough frames. */
2958 if (frp == NULL)
2959 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 }
2961 }
2962 else if (extra_cols > 0)
2963 {
2964 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002965 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 }
2967 }
2968 topfrp->fr_width = width;
2969}
2970
2971/*
2972 * Add the vertical separator to windows at the right side of "frp".
2973 * Note: Does not check if there is room!
2974 */
2975 static void
2976frame_add_vsep(frp)
2977 frame_T *frp;
2978{
2979 win_T *wp;
2980
2981 if (frp->fr_layout == FR_LEAF)
2982 {
2983 wp = frp->fr_win;
2984 if (wp->w_vsep_width == 0)
2985 {
2986 if (wp->w_width > 0) /* don't make it negative */
2987 --wp->w_width;
2988 wp->w_vsep_width = 1;
2989 }
2990 }
2991 else if (frp->fr_layout == FR_COL)
2992 {
2993 /* Handle all the frames in the column. */
2994 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2995 frame_add_vsep(frp);
2996 }
2997 else /* frp->fr_layout == FR_ROW */
2998 {
2999 /* Only need to handle the last frame in the row. */
3000 frp = frp->fr_child;
3001 while (frp->fr_next != NULL)
3002 frp = frp->fr_next;
3003 frame_add_vsep(frp);
3004 }
3005}
3006
3007/*
3008 * Set frame width from the window it contains.
3009 */
3010 static void
3011frame_fix_width(wp)
3012 win_T *wp;
3013{
3014 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3015}
3016#endif
3017
3018/*
3019 * Set frame height from the window it contains.
3020 */
3021 static void
3022frame_fix_height(wp)
3023 win_T *wp;
3024{
3025 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3026}
3027
3028/*
3029 * Compute the minimal height for frame "topfrp".
3030 * Uses the 'winminheight' option.
3031 * When "next_curwin" isn't NULL, use p_wh for this window.
3032 * When "next_curwin" is NOWIN, don't use at least one line for the current
3033 * window.
3034 */
3035 static int
3036frame_minheight(topfrp, next_curwin)
3037 frame_T *topfrp;
3038 win_T *next_curwin;
3039{
3040 frame_T *frp;
3041 int m;
3042#ifdef FEAT_VERTSPLIT
3043 int n;
3044#endif
3045
3046 if (topfrp->fr_win != NULL)
3047 {
3048 if (topfrp->fr_win == next_curwin)
3049 m = p_wh + topfrp->fr_win->w_status_height;
3050 else
3051 {
3052 /* window: minimal height of the window plus status line */
3053 m = p_wmh + topfrp->fr_win->w_status_height;
3054 /* Current window is minimal one line high */
3055 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3056 ++m;
3057 }
3058 }
3059#ifdef FEAT_VERTSPLIT
3060 else if (topfrp->fr_layout == FR_ROW)
3061 {
3062 /* get the minimal height from each frame in this row */
3063 m = 0;
3064 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3065 {
3066 n = frame_minheight(frp, next_curwin);
3067 if (n > m)
3068 m = n;
3069 }
3070 }
3071#endif
3072 else
3073 {
3074 /* Add up the minimal heights for all frames in this column. */
3075 m = 0;
3076 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3077 m += frame_minheight(frp, next_curwin);
3078 }
3079
3080 return m;
3081}
3082
3083#ifdef FEAT_VERTSPLIT
3084/*
3085 * Compute the minimal width for frame "topfrp".
3086 * When "next_curwin" isn't NULL, use p_wiw for this window.
3087 * When "next_curwin" is NOWIN, don't use at least one column for the current
3088 * window.
3089 */
3090 static int
3091frame_minwidth(topfrp, next_curwin)
3092 frame_T *topfrp;
3093 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3094{
3095 frame_T *frp;
3096 int m, n;
3097
3098 if (topfrp->fr_win != NULL)
3099 {
3100 if (topfrp->fr_win == next_curwin)
3101 m = p_wiw + topfrp->fr_win->w_vsep_width;
3102 else
3103 {
3104 /* window: minimal width of the window plus separator column */
3105 m = p_wmw + topfrp->fr_win->w_vsep_width;
3106 /* Current window is minimal one column wide */
3107 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3108 ++m;
3109 }
3110 }
3111 else if (topfrp->fr_layout == FR_COL)
3112 {
3113 /* get the minimal width from each frame in this column */
3114 m = 0;
3115 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3116 {
3117 n = frame_minwidth(frp, next_curwin);
3118 if (n > m)
3119 m = n;
3120 }
3121 }
3122 else
3123 {
3124 /* Add up the minimal widths for all frames in this row. */
3125 m = 0;
3126 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3127 m += frame_minwidth(frp, next_curwin);
3128 }
3129
3130 return m;
3131}
3132#endif
3133
3134
3135/*
3136 * Try to close all windows except current one.
3137 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3138 * used and the buffer was modified.
3139 *
3140 * Used by ":bdel" and ":only".
3141 */
3142 void
3143close_others(message, forceit)
3144 int message;
3145 int forceit; /* always hide all other windows */
3146{
3147 win_T *wp;
3148 win_T *nextwp;
3149 int r;
3150
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003151 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 {
3153 if (message
3154#ifdef FEAT_AUTOCMD
3155 && !autocmd_busy
3156#endif
3157 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003158 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 return;
3160 }
3161
3162 /* Be very careful here: autocommands may change the window layout. */
3163 for (wp = firstwin; win_valid(wp); wp = nextwp)
3164 {
3165 nextwp = wp->w_next;
3166 if (wp != curwin) /* don't close current window */
3167 {
3168
3169 /* Check if it's allowed to abandon this window */
3170 r = can_abandon(wp->w_buffer, forceit);
3171#ifdef FEAT_AUTOCMD
3172 if (!win_valid(wp)) /* autocommands messed wp up */
3173 {
3174 nextwp = firstwin;
3175 continue;
3176 }
3177#endif
3178 if (!r)
3179 {
3180#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3181 if (message && (p_confirm || cmdmod.confirm) && p_write)
3182 {
3183 dialog_changed(wp->w_buffer, FALSE);
3184# ifdef FEAT_AUTOCMD
3185 if (!win_valid(wp)) /* autocommands messed wp up */
3186 {
3187 nextwp = firstwin;
3188 continue;
3189 }
3190# endif
3191 }
3192 if (bufIsChanged(wp->w_buffer))
3193#endif
3194 continue;
3195 }
3196 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3197 }
3198 }
3199
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003200 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 EMSG(_("E445: Other window contains changes"));
3202}
3203
3204#endif /* FEAT_WINDOWS */
3205
3206/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003207 * Init the current window "curwin".
3208 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 */
3210 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003211curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003213 win_init_empty(curwin);
3214}
3215
3216 void
3217win_init_empty(wp)
3218 win_T *wp;
3219{
3220 redraw_win_later(wp, NOT_VALID);
3221 wp->w_lines_valid = 0;
3222 wp->w_cursor.lnum = 1;
3223 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003225 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003227 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3228 wp->w_pcmark.col = 0;
3229 wp->w_prev_pcmark.lnum = 0;
3230 wp->w_prev_pcmark.col = 0;
3231 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003233 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003235 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003237 if (wp->w_p_rl)
3238 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003240 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241#endif
3242}
3243
3244/*
3245 * Allocate the first window and put an empty buffer in it.
3246 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003247 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003249 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250win_alloc_first()
3251{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003252 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003253 return FAIL;
3254
3255#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003256 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003257 if (first_tabpage == NULL)
3258 return FAIL;
3259 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003260 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003261#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003262
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003263 return OK;
3264}
3265
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003266#if defined(FEAT_AUTOCMD) || defined(PROTO)
3267/*
3268 * Init "aucmd_win". This can only be done after the first
3269 * window is fully initialized, thus it can't be in win_alloc_first().
3270 */
3271 void
3272win_alloc_aucmd_win()
3273{
3274 aucmd_win = win_alloc(NULL, TRUE);
3275 if (aucmd_win != NULL)
3276 {
3277 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003278 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003279 new_frame(aucmd_win);
3280 }
3281}
3282#endif
3283
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003284/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003285 * Allocate the first window or the first window in a new tab page.
3286 * When "oldwin" is NULL create an empty buffer for it.
3287 * When "oldwin" is not NULL copy info from it to the new window (only with
3288 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003289 * Return FAIL when something goes wrong (out of memory).
3290 */
3291 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003292win_alloc_firstwin(oldwin)
3293 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003294{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003295 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003296 if (oldwin == NULL)
3297 {
3298 /* Very first window, need to create an empty buffer for it and
3299 * initialize from scratch. */
3300 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3301 if (curwin == NULL || curbuf == NULL)
3302 return FAIL;
3303 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003304#ifdef FEAT_SYN_HL
3305 curwin->w_s = &(curbuf->b_s);
3306#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003307 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003309 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003311 curwin_init(); /* init current window */
3312 }
3313#ifdef FEAT_WINDOWS
3314 else
3315 {
3316 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003317 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003318
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003319 /* We don't want cursor- and scroll-binding in the first window. */
3320 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003321 }
3322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003324 new_frame(curwin);
3325 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003326 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003327 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328#ifdef FEAT_VERTSPLIT
3329 topframe->fr_width = Columns;
3330#endif
3331 topframe->fr_height = Rows - p_ch;
3332 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003333
3334 return OK;
3335}
3336
3337/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003338 * Create a frame for window "wp".
3339 */
3340 static void
3341new_frame(win_T *wp)
3342{
3343 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3344
3345 wp->w_frame = frp;
3346 if (frp != NULL)
3347 {
3348 frp->fr_layout = FR_LEAF;
3349 frp->fr_win = wp;
3350 }
3351}
3352
3353/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003354 * Initialize the window and frame size to the maximum.
3355 */
3356 void
3357win_init_size()
3358{
3359 firstwin->w_height = ROWS_AVAIL;
3360 topframe->fr_height = ROWS_AVAIL;
3361#ifdef FEAT_VERTSPLIT
3362 firstwin->w_width = Columns;
3363 topframe->fr_width = Columns;
3364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365}
3366
3367#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003368
3369/*
3370 * Allocate a new tabpage_T and init the values.
3371 * Returns NULL when out of memory.
3372 */
3373 static tabpage_T *
3374alloc_tabpage()
3375{
3376 tabpage_T *tp;
3377
3378 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3379 if (tp != NULL)
3380 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003381# ifdef FEAT_GUI
3382 int i;
3383
3384 for (i = 0; i < 3; i++)
3385 tp->tp_prev_which_scrollbars[i] = -1;
3386# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003387# ifdef FEAT_DIFF
3388 tp->tp_diff_invalid = TRUE;
3389# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003390#ifdef FEAT_EVAL
3391 /* init t: variables */
3392 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3393#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003394 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003395 }
3396 return tp;
3397}
3398
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003399 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003400free_tabpage(tp)
3401 tabpage_T *tp;
3402{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003403 int idx;
3404
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003405# ifdef FEAT_DIFF
3406 diff_clear(tp);
3407# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003408 for (idx = 0; idx < SNAP_COUNT; ++idx)
3409 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003410#ifdef FEAT_EVAL
3411 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3412#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003413 vim_free(tp);
3414}
3415
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003416/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003417 * Create a new Tab page with one window.
3418 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003419 * When "after" is 0 put it just after the current Tab page.
3420 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003421 * Return FAIL or OK.
3422 */
3423 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003424win_new_tabpage(after)
3425 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003426{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003427 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003428 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003429 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003430
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003431 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003432 if (newtp == NULL)
3433 return FAIL;
3434
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003435 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003436 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003437 {
3438 vim_free(newtp);
3439 return FAIL;
3440 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003441 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003442
3443 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003444 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003445 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003446 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003447 if (after == 1)
3448 {
3449 /* New tab page becomes the first one. */
3450 newtp->tp_next = first_tabpage;
3451 first_tabpage = newtp;
3452 }
3453 else
3454 {
3455 if (after > 0)
3456 {
3457 /* Put new tab page before tab page "after". */
3458 n = 2;
3459 for (tp = first_tabpage; tp->tp_next != NULL
3460 && n < after; tp = tp->tp_next)
3461 ++n;
3462 }
3463 newtp->tp_next = tp->tp_next;
3464 tp->tp_next = newtp;
3465 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003466 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003467 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003468 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003469
3470 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003471 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003472
3473#if defined(FEAT_GUI)
3474 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3475 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003476 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003477#endif
3478
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003479 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003480#ifdef FEAT_AUTOCMD
3481 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3482 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3483#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003484 return OK;
3485 }
3486
3487 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003488 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003489 return FAIL;
3490}
3491
3492/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003493 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3494 * like with ":split".
3495 * Returns OK if a new tab page was created, FAIL otherwise.
3496 */
3497 int
3498may_open_tabpage()
3499{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003500 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003501
Bram Moolenaard326ce82007-03-11 14:48:29 +00003502 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003503 {
3504 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003505 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003506 return win_new_tabpage(n);
3507 }
3508 return FAIL;
3509}
3510
3511/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003512 * Create up to "maxcount" tabpages with empty windows.
3513 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003514 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003515 int
3516make_tabpages(maxcount)
3517 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003518{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003519 int count = maxcount;
3520 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003521
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003522 /* Limit to 'tabpagemax' tabs. */
3523 if (count > p_tpm)
3524 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003525
3526#ifdef FEAT_AUTOCMD
3527 /*
3528 * Don't execute autocommands while creating the tab pages. Must do that
3529 * when putting the buffers in the windows.
3530 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003531 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003532#endif
3533
3534 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003535 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003536 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003537
3538#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003539 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003540#endif
3541
3542 /* return actual number of tab pages */
3543 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003544}
3545
3546/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003547 * Return TRUE when "tpc" points to a valid tab page.
3548 */
3549 int
3550valid_tabpage(tpc)
3551 tabpage_T *tpc;
3552{
3553 tabpage_T *tp;
3554
3555 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3556 if (tp == tpc)
3557 return TRUE;
3558 return FALSE;
3559}
3560
3561/*
3562 * Find tab page "n" (first one is 1). Returns NULL when not found.
3563 */
3564 tabpage_T *
3565find_tabpage(n)
3566 int n;
3567{
3568 tabpage_T *tp;
3569 int i = 1;
3570
3571 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3572 ++i;
3573 return tp;
3574}
3575
3576/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003577 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003578 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003579 */
3580 int
3581tabpage_index(ftp)
3582 tabpage_T *ftp;
3583{
3584 int i = 1;
3585 tabpage_T *tp;
3586
3587 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3588 ++i;
3589 return i;
3590}
3591
3592/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003593 * Prepare for leaving the current tab page.
3594 * When autocomands change "curtab" we don't leave the tab page and return
3595 * FAIL.
3596 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003597 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003598 static int
3599leave_tabpage(new_curbuf)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003600 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003601 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003602{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003603 tabpage_T *tp = curtab;
3604
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003605#ifdef FEAT_VISUAL
3606 reset_VIsual_and_resel(); /* stop Visual mode */
3607#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003608#ifdef FEAT_AUTOCMD
3609 if (new_curbuf != curbuf)
3610 {
3611 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3612 if (curtab != tp)
3613 return FAIL;
3614 }
3615 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3616 if (curtab != tp)
3617 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003618 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003619 if (curtab != tp)
3620 return FAIL;
3621#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003622#if defined(FEAT_GUI)
3623 /* Remove the scrollbars. They may be added back later. */
3624 if (gui.in_use)
3625 gui_remove_scrollbars();
3626#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003627 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003628 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003629 tp->tp_firstwin = firstwin;
3630 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003631 tp->tp_old_Rows = Rows;
3632 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003633 firstwin = NULL;
3634 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003635 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003636}
3637
3638/*
3639 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003640 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003641 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003642 static void
3643enter_tabpage(tp, old_curbuf)
3644 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003645 buf_T *old_curbuf UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003646{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003647 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003648 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003649
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003650 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003651 firstwin = tp->tp_firstwin;
3652 lastwin = tp->tp_lastwin;
3653 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003654
3655 /* We would like doing the TabEnter event first, but we don't have a
3656 * valid current window yet, which may break some commands.
3657 * This triggers autocommands, thus may make "tp" invalid. */
3658 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3659 prevwin = next_prevwin;
3660
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003661#ifdef FEAT_AUTOCMD
3662 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003663
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003664 if (old_curbuf != curbuf)
3665 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3666#endif
3667
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003668 last_status(FALSE); /* status line may appear or disappear */
3669 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003670 must_redraw = CLEAR; /* need to redraw everything */
3671#ifdef FEAT_DIFF
3672 diff_need_scrollbind = TRUE;
3673#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003674
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003675 /* The tabpage line may have appeared or disappeared, may need to resize
3676 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003677 * frame sizes too. Use the stored value of p_ch, so that it can be
3678 * different for each tab page. */
3679 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003680 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3681#ifdef FEAT_GUI_TABLINE
3682 && !gui_use_tabline()
3683#endif
3684 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003685 shell_new_rows();
3686#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003687 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003688 shell_new_columns(); /* update window widths */
3689#endif
3690
3691#if defined(FEAT_GUI)
3692 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3693 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003694 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003695#endif
3696
3697 redraw_all_later(CLEAR);
3698}
3699
3700/*
3701 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003702 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003703 */
3704 void
3705goto_tabpage(n)
3706 int n;
3707{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003708 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003709 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003710 int i;
3711
Bram Moolenaard68071d2006-05-02 22:08:30 +00003712 if (text_locked())
3713 {
3714 /* Not allowed when editing the command line. */
3715#ifdef FEAT_CMDWIN
3716 if (cmdwin_type != 0)
3717 EMSG(_(e_cmdwin));
3718 else
3719#endif
3720 EMSG(_(e_secure));
3721 return;
3722 }
3723
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003724 /* If there is only one it can't work. */
3725 if (first_tabpage->tp_next == NULL)
3726 {
3727 if (n > 1)
3728 beep_flush();
3729 return;
3730 }
3731
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003732 if (n == 0)
3733 {
3734 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003735 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003736 tp = first_tabpage;
3737 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003738 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003739 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003740 else if (n < 0)
3741 {
3742 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3743 * this N times. */
3744 ttp = curtab;
3745 for (i = n; i < 0; ++i)
3746 {
3747 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3748 tp = tp->tp_next)
3749 ;
3750 ttp = tp;
3751 }
3752 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003753 else if (n == 9999)
3754 {
3755 /* Go to last tab page. */
3756 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3757 ;
3758 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003759 else
3760 {
3761 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003762 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003763 if (tp == NULL)
3764 {
3765 beep_flush();
3766 return;
3767 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003768 }
3769
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003770 goto_tabpage_tp(tp);
3771
3772#ifdef FEAT_GUI_TABLINE
3773 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003774 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003775#endif
3776}
3777
3778/*
3779 * Go to tabpage "tp".
3780 * Note: doesn't update the GUI tab.
3781 */
3782 void
3783goto_tabpage_tp(tp)
3784 tabpage_T *tp;
3785{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003786 /* Don't repeat a message in another tab page. */
3787 set_keep_msg(NULL, 0);
3788
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003789 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003790 {
3791 if (valid_tabpage(tp))
3792 enter_tabpage(tp, curbuf);
3793 else
3794 enter_tabpage(curtab, curbuf);
3795 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003796}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797
3798/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003799 * Enter window "wp" in tab page "tp".
3800 * Also updates the GUI tab.
3801 */
3802 void
3803goto_tabpage_win(tp, wp)
3804 tabpage_T *tp;
3805 win_T *wp;
3806{
3807 goto_tabpage_tp(tp);
3808 if (curtab == tp && win_valid(wp))
3809 {
3810 win_enter(wp, TRUE);
3811# ifdef FEAT_GUI_TABLINE
3812 if (gui_use_tabline())
3813 gui_mch_set_curtab(tabpage_index(curtab));
3814# endif
3815 }
3816}
3817
3818/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003819 * Move the current tab page to before tab page "nr".
3820 */
3821 void
3822tabpage_move(nr)
3823 int nr;
3824{
3825 int n = nr;
3826 tabpage_T *tp;
3827
3828 if (first_tabpage->tp_next == NULL)
3829 return;
3830
3831 /* Remove the current tab page from the list of tab pages. */
3832 if (curtab == first_tabpage)
3833 first_tabpage = curtab->tp_next;
3834 else
3835 {
3836 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3837 if (tp->tp_next == curtab)
3838 break;
3839 if (tp == NULL) /* "cannot happen" */
3840 return;
3841 tp->tp_next = curtab->tp_next;
3842 }
3843
3844 /* Re-insert it at the specified position. */
3845 if (n == 0)
3846 {
3847 curtab->tp_next = first_tabpage;
3848 first_tabpage = curtab;
3849 }
3850 else
3851 {
3852 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3853 --n;
3854 curtab->tp_next = tp->tp_next;
3855 tp->tp_next = curtab;
3856 }
3857
3858 /* Need to redraw the tabline. Tab page contents doesn't change. */
3859 redraw_tabline = TRUE;
3860}
3861
3862
3863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 * Go to another window.
3865 * When jumping to another buffer, stop Visual mode. Do this before
3866 * changing windows so we can yank the selection into the '*' register.
3867 * When jumping to another window on the same buffer, adjust its cursor
3868 * position to keep the same Visual area.
3869 */
3870 void
3871win_goto(wp)
3872 win_T *wp;
3873{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02003874#ifdef FEAT_CONCEAL
3875 win_T *owp = curwin;
3876#endif
3877
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003878 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 {
3880 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003881 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 return;
3883 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003884#ifdef FEAT_AUTOCMD
3885 if (curbuf_locked())
3886 return;
3887#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889#ifdef FEAT_VISUAL
3890 if (wp->w_buffer != curbuf)
3891 reset_VIsual_and_resel();
3892 else if (VIsual_active)
3893 wp->w_cursor = curwin->w_cursor;
3894#endif
3895
3896#ifdef FEAT_GUI
3897 need_mouse_correct = TRUE;
3898#endif
3899 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02003900
3901#ifdef FEAT_CONCEAL
3902 /* Conceal cursor line in previous window, unconceal in current window. */
3903 if (win_valid(owp))
3904 update_single_line(owp, owp->w_cursor.lnum);
3905 update_single_line(curwin, curwin->w_cursor.lnum);
3906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907}
3908
3909#if defined(FEAT_PERL) || defined(PROTO)
3910/*
3911 * Find window number "winnr" (counting top to bottom).
3912 */
3913 win_T *
3914win_find_nr(winnr)
3915 int winnr;
3916{
3917 win_T *wp;
3918
3919# ifdef FEAT_WINDOWS
3920 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3921 if (--winnr == 0)
3922 break;
3923 return wp;
3924# else
3925 return curwin;
3926# endif
3927}
3928#endif
3929
3930#ifdef FEAT_VERTSPLIT
3931/*
3932 * Move to window above or below "count" times.
3933 */
3934 static void
3935win_goto_ver(up, count)
3936 int up; /* TRUE to go to win above */
3937 long count;
3938{
3939 frame_T *fr;
3940 frame_T *nfr;
3941 frame_T *foundfr;
3942
3943 foundfr = curwin->w_frame;
3944 while (count--)
3945 {
3946 /*
3947 * First go upwards in the tree of frames until we find a upwards or
3948 * downwards neighbor.
3949 */
3950 fr = foundfr;
3951 for (;;)
3952 {
3953 if (fr == topframe)
3954 goto end;
3955 if (up)
3956 nfr = fr->fr_prev;
3957 else
3958 nfr = fr->fr_next;
3959 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3960 break;
3961 fr = fr->fr_parent;
3962 }
3963
3964 /*
3965 * Now go downwards to find the bottom or top frame in it.
3966 */
3967 for (;;)
3968 {
3969 if (nfr->fr_layout == FR_LEAF)
3970 {
3971 foundfr = nfr;
3972 break;
3973 }
3974 fr = nfr->fr_child;
3975 if (nfr->fr_layout == FR_ROW)
3976 {
3977 /* Find the frame at the cursor row. */
3978 while (fr->fr_next != NULL
3979 && frame2win(fr)->w_wincol + fr->fr_width
3980 <= curwin->w_wincol + curwin->w_wcol)
3981 fr = fr->fr_next;
3982 }
3983 if (nfr->fr_layout == FR_COL && up)
3984 while (fr->fr_next != NULL)
3985 fr = fr->fr_next;
3986 nfr = fr;
3987 }
3988 }
3989end:
3990 if (foundfr != NULL)
3991 win_goto(foundfr->fr_win);
3992}
3993
3994/*
3995 * Move to left or right window.
3996 */
3997 static void
3998win_goto_hor(left, count)
3999 int left; /* TRUE to go to left win */
4000 long count;
4001{
4002 frame_T *fr;
4003 frame_T *nfr;
4004 frame_T *foundfr;
4005
4006 foundfr = curwin->w_frame;
4007 while (count--)
4008 {
4009 /*
4010 * First go upwards in the tree of frames until we find a left or
4011 * right neighbor.
4012 */
4013 fr = foundfr;
4014 for (;;)
4015 {
4016 if (fr == topframe)
4017 goto end;
4018 if (left)
4019 nfr = fr->fr_prev;
4020 else
4021 nfr = fr->fr_next;
4022 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4023 break;
4024 fr = fr->fr_parent;
4025 }
4026
4027 /*
4028 * Now go downwards to find the leftmost or rightmost frame in it.
4029 */
4030 for (;;)
4031 {
4032 if (nfr->fr_layout == FR_LEAF)
4033 {
4034 foundfr = nfr;
4035 break;
4036 }
4037 fr = nfr->fr_child;
4038 if (nfr->fr_layout == FR_COL)
4039 {
4040 /* Find the frame at the cursor row. */
4041 while (fr->fr_next != NULL
4042 && frame2win(fr)->w_winrow + fr->fr_height
4043 <= curwin->w_winrow + curwin->w_wrow)
4044 fr = fr->fr_next;
4045 }
4046 if (nfr->fr_layout == FR_ROW && left)
4047 while (fr->fr_next != NULL)
4048 fr = fr->fr_next;
4049 nfr = fr;
4050 }
4051 }
4052end:
4053 if (foundfr != NULL)
4054 win_goto(foundfr->fr_win);
4055}
4056#endif
4057
4058/*
4059 * Make window "wp" the current window.
4060 */
4061 void
4062win_enter(wp, undo_sync)
4063 win_T *wp;
4064 int undo_sync;
4065{
4066 win_enter_ext(wp, undo_sync, FALSE);
4067}
4068
4069/*
4070 * Make window wp the current window.
4071 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4072 * been closed and isn't valid.
4073 */
4074 static void
4075win_enter_ext(wp, undo_sync, curwin_invalid)
4076 win_T *wp;
4077 int undo_sync;
4078 int curwin_invalid;
4079{
4080#ifdef FEAT_AUTOCMD
4081 int other_buffer = FALSE;
4082#endif
4083
4084 if (wp == curwin && !curwin_invalid) /* nothing to do */
4085 return;
4086
4087#ifdef FEAT_AUTOCMD
4088 if (!curwin_invalid)
4089 {
4090 /*
4091 * Be careful: If autocommands delete the window, return now.
4092 */
4093 if (wp->w_buffer != curbuf)
4094 {
4095 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4096 other_buffer = TRUE;
4097 if (!win_valid(wp))
4098 return;
4099 }
4100 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4101 if (!win_valid(wp))
4102 return;
4103# ifdef FEAT_EVAL
4104 /* autocmds may abort script processing */
4105 if (aborting())
4106 return;
4107# endif
4108 }
4109#endif
4110
4111 /* sync undo before leaving the current buffer */
4112 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004113 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 /* may have to copy the buffer options when 'cpo' contains 'S' */
4115 if (wp->w_buffer != curbuf)
4116 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4117 if (!curwin_invalid)
4118 {
4119 prevwin = curwin; /* remember for CTRL-W p */
4120 curwin->w_redr_status = TRUE;
4121 }
4122 curwin = wp;
4123 curbuf = wp->w_buffer;
4124 check_cursor();
4125#ifdef FEAT_VIRTUALEDIT
4126 if (!virtual_active())
4127 curwin->w_cursor.coladd = 0;
4128#endif
4129 changed_line_abv_curs(); /* assume cursor position needs updating */
4130
4131 if (curwin->w_localdir != NULL)
4132 {
4133 /* Window has a local directory: Save current directory as global
4134 * directory (unless that was done already) and change to the local
4135 * directory. */
4136 if (globaldir == NULL)
4137 {
4138 char_u cwd[MAXPATHL];
4139
4140 if (mch_dirname(cwd, MAXPATHL) == OK)
4141 globaldir = vim_strsave(cwd);
4142 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004143 if (mch_chdir((char *)curwin->w_localdir) == 0)
4144 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 }
4146 else if (globaldir != NULL)
4147 {
4148 /* Window doesn't have a local directory and we are not in the global
4149 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004150 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 vim_free(globaldir);
4152 globaldir = NULL;
4153 shorten_fnames(TRUE);
4154 }
4155
4156#ifdef FEAT_AUTOCMD
4157 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4158 if (other_buffer)
4159 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4160#endif
4161
4162#ifdef FEAT_TITLE
4163 maketitle();
4164#endif
4165 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004166 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 if (restart_edit)
4168 redraw_later(VALID); /* causes status line redraw */
4169
4170 /* set window height to desired minimal value */
4171 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4172 win_setheight((int)p_wh);
4173 else if (curwin->w_height == 0)
4174 win_setheight(1);
4175
4176#ifdef FEAT_VERTSPLIT
4177 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004178 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 win_setwidth((int)p_wiw);
4180#endif
4181
4182#ifdef FEAT_MOUSE
4183 setmouse(); /* in case jumped to/from help buffer */
4184#endif
4185
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004186 /* Change directories when the 'acd' option is set. */
4187 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188}
4189
4190#endif /* FEAT_WINDOWS */
4191
4192#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4193/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004194 * Jump to the first open window that contains buffer "buf", if one exists.
4195 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 */
4197 win_T *
4198buf_jump_open_win(buf)
4199 buf_T *buf;
4200{
4201# ifdef FEAT_WINDOWS
4202 win_T *wp;
4203
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004204 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 if (wp->w_buffer == buf)
4206 break;
4207 if (wp != NULL)
4208 win_enter(wp, FALSE);
4209 return wp;
4210# else
4211 if (curwin->w_buffer == buf)
4212 return curwin;
4213 return NULL;
4214# endif
4215}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004216
4217/*
4218 * Jump to the first open window in any tab page that contains buffer "buf",
4219 * if one exists.
4220 * Returns a pointer to the window found, otherwise NULL.
4221 */
4222 win_T *
4223buf_jump_open_tab(buf)
4224 buf_T *buf;
4225{
4226# ifdef FEAT_WINDOWS
4227 win_T *wp;
4228 tabpage_T *tp;
4229
4230 /* First try the current tab page. */
4231 wp = buf_jump_open_win(buf);
4232 if (wp != NULL)
4233 return wp;
4234
4235 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4236 if (tp != curtab)
4237 {
4238 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4239 if (wp->w_buffer == buf)
4240 break;
4241 if (wp != NULL)
4242 {
4243 goto_tabpage_win(tp, wp);
4244 if (curwin != wp)
4245 wp = NULL; /* something went wrong */
4246 break;
4247 }
4248 }
4249
4250 return wp;
4251# else
4252 if (curwin->w_buffer == buf)
4253 return curwin;
4254 return NULL;
4255# endif
4256}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257#endif
4258
4259/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004260 * Allocate a window structure and link it in the window list when "hidden" is
4261 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004264win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004265 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004266 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267{
4268 win_T *newwin;
4269
4270 /*
4271 * allocate window structure and linesizes arrays
4272 */
4273 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4274 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4275 {
4276 vim_free(newwin);
4277 newwin = NULL;
4278 }
4279
4280 if (newwin != NULL)
4281 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004282#ifdef FEAT_AUTOCMD
4283 /* Don't execute autocommands while the window is not properly
4284 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4285 * event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004286 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 /*
4289 * link the window in the window list
4290 */
4291#ifdef FEAT_WINDOWS
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004292 if (!hidden)
4293 win_append(after, newwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294#endif
4295#ifdef FEAT_VERTSPLIT
4296 newwin->w_wincol = 0;
4297 newwin->w_width = Columns;
4298#endif
4299
4300 /* position the display and the cursor at the top of the file. */
4301 newwin->w_topline = 1;
4302#ifdef FEAT_DIFF
4303 newwin->w_topfill = 0;
4304#endif
4305 newwin->w_botline = 2;
4306 newwin->w_cursor.lnum = 1;
4307#ifdef FEAT_SCROLLBIND
4308 newwin->w_scbind_pos = 1;
4309#endif
4310
4311 /* We won't calculate w_fraction until resizing the window */
4312 newwin->w_fraction = 0;
4313 newwin->w_prev_fraction_row = -1;
4314
4315#ifdef FEAT_GUI
4316 if (gui.in_use)
4317 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4319 SBAR_LEFT, newwin);
4320 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4321 SBAR_RIGHT, newwin);
4322 }
4323#endif
4324#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004325 /* init w: variables */
4326 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327#endif
4328#ifdef FEAT_FOLDING
4329 foldInitWin(newwin);
4330#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004331#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004332 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004333#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004334#ifdef FEAT_SEARCH_EXTRA
4335 newwin->w_match_head = NULL;
4336 newwin->w_next_match_id = 4;
4337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 }
4339 return newwin;
4340}
4341
4342#if defined(FEAT_WINDOWS) || defined(PROTO)
4343
4344/*
4345 * remove window 'wp' from the window list and free the structure
4346 */
4347 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004348win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004350 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351{
4352 int i;
4353
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004354#ifdef FEAT_FOLDING
4355 clearFolding(wp);
4356#endif
4357
4358 /* reduce the reference count to the argument list. */
4359 alist_unlink(wp->w_alist);
4360
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004361#ifdef FEAT_AUTOCMD
4362 /* Don't execute autocommands while the window is halfway being deleted.
4363 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004364 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004365#endif
4366
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004367#ifdef FEAT_LUA
4368 lua_window_free(wp);
4369#endif
4370
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004371#ifdef FEAT_MZSCHEME
4372 mzscheme_window_free(wp);
4373#endif
4374
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375#ifdef FEAT_PERL
4376 perl_win_free(wp);
4377#endif
4378
4379#ifdef FEAT_PYTHON
4380 python_window_free(wp);
4381#endif
4382
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004383#ifdef FEAT_PYTHON3
4384 python3_window_free(wp);
4385#endif
4386
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387#ifdef FEAT_TCL
4388 tcl_window_free(wp);
4389#endif
4390
4391#ifdef FEAT_RUBY
4392 ruby_window_free(wp);
4393#endif
4394
4395 clear_winopt(&wp->w_onebuf_opt);
4396 clear_winopt(&wp->w_allbuf_opt);
4397
4398#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004399 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400#endif
4401
4402 if (prevwin == wp)
4403 prevwin = NULL;
4404 win_free_lsize(wp);
4405
4406 for (i = 0; i < wp->w_tagstacklen; ++i)
4407 vim_free(wp->w_tagstack[i].tagname);
4408
4409 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004410
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004412 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004414
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415#ifdef FEAT_JUMPLIST
4416 free_jumplist(wp);
4417#endif
4418
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004419#ifdef FEAT_QUICKFIX
4420 qf_free_all(wp);
4421#endif
4422
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423#ifdef FEAT_GUI
4424 if (gui.in_use)
4425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4427 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4428 }
4429#endif /* FEAT_GUI */
4430
Bram Moolenaar860cae12010-06-05 23:22:07 +02004431#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004432 reset_synblock(wp); /* free independent synblock */
Bram Moolenaar1a384422010-07-14 19:53:30 +02004433 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004434#endif
4435
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004436#ifdef FEAT_AUTOCMD
4437 if (wp != aucmd_win)
4438#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004439 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004441
4442#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004443 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445}
4446
4447/*
4448 * Append window "wp" in the window list after window "after".
4449 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004450 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451win_append(after, wp)
4452 win_T *after, *wp;
4453{
4454 win_T *before;
4455
4456 if (after == NULL) /* after NULL is in front of the first */
4457 before = firstwin;
4458 else
4459 before = after->w_next;
4460
4461 wp->w_next = before;
4462 wp->w_prev = after;
4463 if (after == NULL)
4464 firstwin = wp;
4465 else
4466 after->w_next = wp;
4467 if (before == NULL)
4468 lastwin = wp;
4469 else
4470 before->w_prev = wp;
4471}
4472
4473/*
4474 * Remove a window from the window list.
4475 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004476 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004477win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004479 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480{
4481 if (wp->w_prev != NULL)
4482 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004483 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004485 else
4486 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 if (wp->w_next != NULL)
4488 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004489 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004491 else
4492 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493}
4494
4495/*
4496 * Append frame "frp" in a frame list after frame "after".
4497 */
4498 static void
4499frame_append(after, frp)
4500 frame_T *after, *frp;
4501{
4502 frp->fr_next = after->fr_next;
4503 after->fr_next = frp;
4504 if (frp->fr_next != NULL)
4505 frp->fr_next->fr_prev = frp;
4506 frp->fr_prev = after;
4507}
4508
4509/*
4510 * Insert frame "frp" in a frame list before frame "before".
4511 */
4512 static void
4513frame_insert(before, frp)
4514 frame_T *before, *frp;
4515{
4516 frp->fr_next = before;
4517 frp->fr_prev = before->fr_prev;
4518 before->fr_prev = frp;
4519 if (frp->fr_prev != NULL)
4520 frp->fr_prev->fr_next = frp;
4521 else
4522 frp->fr_parent->fr_child = frp;
4523}
4524
4525/*
4526 * Remove a frame from a frame list.
4527 */
4528 static void
4529frame_remove(frp)
4530 frame_T *frp;
4531{
4532 if (frp->fr_prev != NULL)
4533 frp->fr_prev->fr_next = frp->fr_next;
4534 else
4535 frp->fr_parent->fr_child = frp->fr_next;
4536 if (frp->fr_next != NULL)
4537 frp->fr_next->fr_prev = frp->fr_prev;
4538}
4539
4540#endif /* FEAT_WINDOWS */
4541
4542/*
4543 * Allocate w_lines[] for window "wp".
4544 * Return FAIL for failure, OK for success.
4545 */
4546 int
4547win_alloc_lines(wp)
4548 win_T *wp;
4549{
4550 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004551 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 if (wp->w_lines == NULL)
4553 return FAIL;
4554 return OK;
4555}
4556
4557/*
4558 * free lsize arrays for a window
4559 */
4560 void
4561win_free_lsize(wp)
4562 win_T *wp;
4563{
4564 vim_free(wp->w_lines);
4565 wp->w_lines = NULL;
4566}
4567
4568/*
4569 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004570 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 */
4572 void
4573shell_new_rows()
4574{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004575 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576
4577 if (firstwin == NULL) /* not initialized yet */
4578 return;
4579#ifdef FEAT_WINDOWS
4580 if (h < frame_minheight(topframe, NULL))
4581 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004582
4583 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 * that doesn't result in the right height, forget about that option. */
4585 frame_new_height(topframe, h, FALSE, TRUE);
4586 if (topframe->fr_height != h)
4587 frame_new_height(topframe, h, FALSE, FALSE);
4588
4589 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4590#else
4591 if (h < 1)
4592 h = 1;
4593 win_new_height(firstwin, h);
4594#endif
4595 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004596#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004597 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004598#endif
4599
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600#if 0
4601 /* Disabled: don't want making the screen smaller make a window larger. */
4602 if (p_ea)
4603 win_equal(curwin, FALSE, 'v');
4604#endif
4605}
4606
4607#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4608/*
4609 * Called from win_new_shellsize() after Columns changed.
4610 */
4611 void
4612shell_new_columns()
4613{
4614 if (firstwin == NULL) /* not initialized yet */
4615 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004616
4617 /* First try setting the widths of windows with 'winfixwidth'. If that
4618 * doesn't result in the right width, forget about that option. */
4619 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4620 if (topframe->fr_width != Columns)
4621 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4622
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4624#if 0
4625 /* Disabled: don't want making the screen smaller make a window larger. */
4626 if (p_ea)
4627 win_equal(curwin, FALSE, 'h');
4628#endif
4629}
4630#endif
4631
4632#if defined(FEAT_CMDWIN) || defined(PROTO)
4633/*
4634 * Save the size of all windows in "gap".
4635 */
4636 void
4637win_size_save(gap)
4638 garray_T *gap;
4639
4640{
4641 win_T *wp;
4642
4643 ga_init2(gap, (int)sizeof(int), 1);
4644 if (ga_grow(gap, win_count() * 2) == OK)
4645 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4646 {
4647 ((int *)gap->ga_data)[gap->ga_len++] =
4648 wp->w_width + wp->w_vsep_width;
4649 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4650 }
4651}
4652
4653/*
4654 * Restore window sizes, but only if the number of windows is still the same.
4655 * Does not free the growarray.
4656 */
4657 void
4658win_size_restore(gap)
4659 garray_T *gap;
4660{
4661 win_T *wp;
4662 int i;
4663
4664 if (win_count() * 2 == gap->ga_len)
4665 {
4666 i = 0;
4667 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4668 {
4669 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4670 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4671 }
4672 /* recompute the window positions */
4673 (void)win_comp_pos();
4674 }
4675}
4676#endif /* FEAT_CMDWIN */
4677
4678#if defined(FEAT_WINDOWS) || defined(PROTO)
4679/*
4680 * Update the position for all windows, using the width and height of the
4681 * frames.
4682 * Returns the row just after the last window.
4683 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004684 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685win_comp_pos()
4686{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004687 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 int col = 0;
4689
4690 frame_comp_pos(topframe, &row, &col);
4691 return row;
4692}
4693
4694/*
4695 * Update the position of the windows in frame "topfrp", using the width and
4696 * height of the frames.
4697 * "*row" and "*col" are the top-left position of the frame. They are updated
4698 * to the bottom-right position plus one.
4699 */
4700 static void
4701frame_comp_pos(topfrp, row, col)
4702 frame_T *topfrp;
4703 int *row;
4704 int *col;
4705{
4706 win_T *wp;
4707 frame_T *frp;
4708#ifdef FEAT_VERTSPLIT
4709 int startcol;
4710 int startrow;
4711#endif
4712
4713 wp = topfrp->fr_win;
4714 if (wp != NULL)
4715 {
4716 if (wp->w_winrow != *row
4717#ifdef FEAT_VERTSPLIT
4718 || wp->w_wincol != *col
4719#endif
4720 )
4721 {
4722 /* position changed, redraw */
4723 wp->w_winrow = *row;
4724#ifdef FEAT_VERTSPLIT
4725 wp->w_wincol = *col;
4726#endif
4727 redraw_win_later(wp, NOT_VALID);
4728 wp->w_redr_status = TRUE;
4729 }
4730 *row += wp->w_height + wp->w_status_height;
4731#ifdef FEAT_VERTSPLIT
4732 *col += wp->w_width + wp->w_vsep_width;
4733#endif
4734 }
4735 else
4736 {
4737#ifdef FEAT_VERTSPLIT
4738 startrow = *row;
4739 startcol = *col;
4740#endif
4741 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4742 {
4743#ifdef FEAT_VERTSPLIT
4744 if (topfrp->fr_layout == FR_ROW)
4745 *row = startrow; /* all frames are at the same row */
4746 else
4747 *col = startcol; /* all frames are at the same col */
4748#endif
4749 frame_comp_pos(frp, row, col);
4750 }
4751 }
4752}
4753
4754#endif /* FEAT_WINDOWS */
4755
4756/*
4757 * Set current window height and take care of repositioning other windows to
4758 * fit around it.
4759 */
4760 void
4761win_setheight(height)
4762 int height;
4763{
4764 win_setheight_win(height, curwin);
4765}
4766
4767/*
4768 * Set the window height of window "win" and take care of repositioning other
4769 * windows to fit around it.
4770 */
4771 void
4772win_setheight_win(height, win)
4773 int height;
4774 win_T *win;
4775{
4776 int row;
4777
4778 if (win == curwin)
4779 {
4780 /* Always keep current window at least one line high, even when
4781 * 'winminheight' is zero. */
4782#ifdef FEAT_WINDOWS
4783 if (height < p_wmh)
4784 height = p_wmh;
4785#endif
4786 if (height == 0)
4787 height = 1;
4788 }
4789
4790#ifdef FEAT_WINDOWS
4791 frame_setheight(win->w_frame, height + win->w_status_height);
4792
4793 /* recompute the window positions */
4794 row = win_comp_pos();
4795#else
4796 if (height > topframe->fr_height)
4797 height = topframe->fr_height;
4798 win->w_height = height;
4799 row = height;
4800#endif
4801
4802 /*
4803 * If there is extra space created between the last window and the command
4804 * line, clear it.
4805 */
4806 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4807 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4808 cmdline_row = row;
4809 msg_row = row;
4810 msg_col = 0;
4811
4812 redraw_all_later(NOT_VALID);
4813}
4814
4815#if defined(FEAT_WINDOWS) || defined(PROTO)
4816
4817/*
4818 * Set the height of a frame to "height" and take care that all frames and
4819 * windows inside it are resized. Also resize frames on the left and right if
4820 * the are in the same FR_ROW frame.
4821 *
4822 * Strategy:
4823 * If the frame is part of a FR_COL frame, try fitting the frame in that
4824 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4825 * go to containing frames to resize them and make room.
4826 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4827 * Check for the minimal height of the FR_ROW frame.
4828 * At the top level we can also use change the command line height.
4829 */
4830 static void
4831frame_setheight(curfrp, height)
4832 frame_T *curfrp;
4833 int height;
4834{
4835 int room; /* total number of lines available */
4836 int take; /* number of lines taken from other windows */
4837 int room_cmdline; /* lines available from cmdline */
4838 int run;
4839 frame_T *frp;
4840 int h;
4841 int room_reserved;
4842
4843 /* If the height already is the desired value, nothing to do. */
4844 if (curfrp->fr_height == height)
4845 return;
4846
4847 if (curfrp->fr_parent == NULL)
4848 {
4849 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004850 if (height > ROWS_AVAIL)
4851 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 if (height > 0)
4853 frame_new_height(curfrp, height, FALSE, FALSE);
4854 }
4855 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4856 {
4857 /* Row of frames: Also need to resize frames left and right of this
4858 * one. First check for the minimal height of these. */
4859 h = frame_minheight(curfrp->fr_parent, NULL);
4860 if (height < h)
4861 height = h;
4862 frame_setheight(curfrp->fr_parent, height);
4863 }
4864 else
4865 {
4866 /*
4867 * Column of frames: try to change only frames in this column.
4868 */
4869#ifdef FEAT_VERTSPLIT
4870 /*
4871 * Do this twice:
4872 * 1: compute room available, if it's not enough try resizing the
4873 * containing frame.
4874 * 2: compute the room available and adjust the height to it.
4875 * Try not to reduce the height of a window with 'winfixheight' set.
4876 */
4877 for (run = 1; run <= 2; ++run)
4878#else
4879 for (;;)
4880#endif
4881 {
4882 room = 0;
4883 room_reserved = 0;
4884 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4885 frp = frp->fr_next)
4886 {
4887 if (frp != curfrp
4888 && frp->fr_win != NULL
4889 && frp->fr_win->w_p_wfh)
4890 room_reserved += frp->fr_height;
4891 room += frp->fr_height;
4892 if (frp != curfrp)
4893 room -= frame_minheight(frp, NULL);
4894 }
4895#ifdef FEAT_VERTSPLIT
4896 if (curfrp->fr_width != Columns)
4897 room_cmdline = 0;
4898 else
4899#endif
4900 {
4901 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4902 + lastwin->w_height + lastwin->w_status_height);
4903 if (room_cmdline < 0)
4904 room_cmdline = 0;
4905 }
4906
4907 if (height <= room + room_cmdline)
4908 break;
4909#ifdef FEAT_VERTSPLIT
4910 if (run == 2 || curfrp->fr_width == Columns)
4911#endif
4912 {
4913 if (height > room + room_cmdline)
4914 height = room + room_cmdline;
4915 break;
4916 }
4917#ifdef FEAT_VERTSPLIT
4918 frame_setheight(curfrp->fr_parent, height
4919 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4920#endif
4921 /*NOTREACHED*/
4922 }
4923
4924 /*
4925 * Compute the number of lines we will take from others frames (can be
4926 * negative!).
4927 */
4928 take = height - curfrp->fr_height;
4929
4930 /* If there is not enough room, also reduce the height of a window
4931 * with 'winfixheight' set. */
4932 if (height > room + room_cmdline - room_reserved)
4933 room_reserved = room + room_cmdline - height;
4934 /* If there is only a 'winfixheight' window and making the
4935 * window smaller, need to make the other window taller. */
4936 if (take < 0 && room - curfrp->fr_height < room_reserved)
4937 room_reserved = 0;
4938
4939 if (take > 0 && room_cmdline > 0)
4940 {
4941 /* use lines from cmdline first */
4942 if (take < room_cmdline)
4943 room_cmdline = take;
4944 take -= room_cmdline;
4945 topframe->fr_height += room_cmdline;
4946 }
4947
4948 /*
4949 * set the current frame to the new height
4950 */
4951 frame_new_height(curfrp, height, FALSE, FALSE);
4952
4953 /*
4954 * First take lines from the frames after the current frame. If
4955 * that is not enough, takes lines from frames above the current
4956 * frame.
4957 */
4958 for (run = 0; run < 2; ++run)
4959 {
4960 if (run == 0)
4961 frp = curfrp->fr_next; /* 1st run: start with next window */
4962 else
4963 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4964 while (frp != NULL && take != 0)
4965 {
4966 h = frame_minheight(frp, NULL);
4967 if (room_reserved > 0
4968 && frp->fr_win != NULL
4969 && frp->fr_win->w_p_wfh)
4970 {
4971 if (room_reserved >= frp->fr_height)
4972 room_reserved -= frp->fr_height;
4973 else
4974 {
4975 if (frp->fr_height - room_reserved > take)
4976 room_reserved = frp->fr_height - take;
4977 take -= frp->fr_height - room_reserved;
4978 frame_new_height(frp, room_reserved, FALSE, FALSE);
4979 room_reserved = 0;
4980 }
4981 }
4982 else
4983 {
4984 if (frp->fr_height - take < h)
4985 {
4986 take -= frp->fr_height - h;
4987 frame_new_height(frp, h, FALSE, FALSE);
4988 }
4989 else
4990 {
4991 frame_new_height(frp, frp->fr_height - take,
4992 FALSE, FALSE);
4993 take = 0;
4994 }
4995 }
4996 if (run == 0)
4997 frp = frp->fr_next;
4998 else
4999 frp = frp->fr_prev;
5000 }
5001 }
5002 }
5003}
5004
5005#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5006/*
5007 * Set current window width and take care of repositioning other windows to
5008 * fit around it.
5009 */
5010 void
5011win_setwidth(width)
5012 int width;
5013{
5014 win_setwidth_win(width, curwin);
5015}
5016
5017 void
5018win_setwidth_win(width, wp)
5019 int width;
5020 win_T *wp;
5021{
5022 /* Always keep current window at least one column wide, even when
5023 * 'winminwidth' is zero. */
5024 if (wp == curwin)
5025 {
5026 if (width < p_wmw)
5027 width = p_wmw;
5028 if (width == 0)
5029 width = 1;
5030 }
5031
5032 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5033
5034 /* recompute the window positions */
5035 (void)win_comp_pos();
5036
5037 redraw_all_later(NOT_VALID);
5038}
5039
5040/*
5041 * Set the width of a frame to "width" and take care that all frames and
5042 * windows inside it are resized. Also resize frames above and below if the
5043 * are in the same FR_ROW frame.
5044 *
5045 * Strategy is similar to frame_setheight().
5046 */
5047 static void
5048frame_setwidth(curfrp, width)
5049 frame_T *curfrp;
5050 int width;
5051{
5052 int room; /* total number of lines available */
5053 int take; /* number of lines taken from other windows */
5054 int run;
5055 frame_T *frp;
5056 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005057 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058
5059 /* If the width already is the desired value, nothing to do. */
5060 if (curfrp->fr_width == width)
5061 return;
5062
5063 if (curfrp->fr_parent == NULL)
5064 /* topframe: can't change width */
5065 return;
5066
5067 if (curfrp->fr_parent->fr_layout == FR_COL)
5068 {
5069 /* Column of frames: Also need to resize frames above and below of
5070 * this one. First check for the minimal width of these. */
5071 w = frame_minwidth(curfrp->fr_parent, NULL);
5072 if (width < w)
5073 width = w;
5074 frame_setwidth(curfrp->fr_parent, width);
5075 }
5076 else
5077 {
5078 /*
5079 * Row of frames: try to change only frames in this row.
5080 *
5081 * Do this twice:
5082 * 1: compute room available, if it's not enough try resizing the
5083 * containing frame.
5084 * 2: compute the room available and adjust the width to it.
5085 */
5086 for (run = 1; run <= 2; ++run)
5087 {
5088 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005089 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5091 frp = frp->fr_next)
5092 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005093 if (frp != curfrp
5094 && frp->fr_win != NULL
5095 && frp->fr_win->w_p_wfw)
5096 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 room += frp->fr_width;
5098 if (frp != curfrp)
5099 room -= frame_minwidth(frp, NULL);
5100 }
5101
5102 if (width <= room)
5103 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005104 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 {
5106 if (width > room)
5107 width = room;
5108 break;
5109 }
5110 frame_setwidth(curfrp->fr_parent, width
5111 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5112 }
5113
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 /*
5115 * Compute the number of lines we will take from others frames (can be
5116 * negative!).
5117 */
5118 take = width - curfrp->fr_width;
5119
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005120 /* If there is not enough room, also reduce the width of a window
5121 * with 'winfixwidth' set. */
5122 if (width > room - room_reserved)
5123 room_reserved = room - width;
5124 /* If there is only a 'winfixwidth' window and making the
5125 * window smaller, need to make the other window narrower. */
5126 if (take < 0 && room - curfrp->fr_width < room_reserved)
5127 room_reserved = 0;
5128
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 /*
5130 * set the current frame to the new width
5131 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005132 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
5134 /*
5135 * First take lines from the frames right of the current frame. If
5136 * that is not enough, takes lines from frames left of the current
5137 * frame.
5138 */
5139 for (run = 0; run < 2; ++run)
5140 {
5141 if (run == 0)
5142 frp = curfrp->fr_next; /* 1st run: start with next window */
5143 else
5144 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5145 while (frp != NULL && take != 0)
5146 {
5147 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005148 if (room_reserved > 0
5149 && frp->fr_win != NULL
5150 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005152 if (room_reserved >= frp->fr_width)
5153 room_reserved -= frp->fr_width;
5154 else
5155 {
5156 if (frp->fr_width - room_reserved > take)
5157 room_reserved = frp->fr_width - take;
5158 take -= frp->fr_width - room_reserved;
5159 frame_new_width(frp, room_reserved, FALSE, FALSE);
5160 room_reserved = 0;
5161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
5163 else
5164 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005165 if (frp->fr_width - take < w)
5166 {
5167 take -= frp->fr_width - w;
5168 frame_new_width(frp, w, FALSE, FALSE);
5169 }
5170 else
5171 {
5172 frame_new_width(frp, frp->fr_width - take,
5173 FALSE, FALSE);
5174 take = 0;
5175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
5177 if (run == 0)
5178 frp = frp->fr_next;
5179 else
5180 frp = frp->fr_prev;
5181 }
5182 }
5183 }
5184}
5185#endif /* FEAT_VERTSPLIT */
5186
5187/*
5188 * Check 'winminheight' for a valid value.
5189 */
5190 void
5191win_setminheight()
5192{
5193 int room;
5194 int first = TRUE;
5195 win_T *wp;
5196
5197 /* loop until there is a 'winminheight' that is possible */
5198 while (p_wmh > 0)
5199 {
5200 /* TODO: handle vertical splits */
5201 room = -p_wh;
5202 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5203 room += wp->w_height - p_wmh;
5204 if (room >= 0)
5205 break;
5206 --p_wmh;
5207 if (first)
5208 {
5209 EMSG(_(e_noroom));
5210 first = FALSE;
5211 }
5212 }
5213}
5214
5215#ifdef FEAT_MOUSE
5216
5217/*
5218 * Status line of dragwin is dragged "offset" lines down (negative is up).
5219 */
5220 void
5221win_drag_status_line(dragwin, offset)
5222 win_T *dragwin;
5223 int offset;
5224{
5225 frame_T *curfr;
5226 frame_T *fr;
5227 int room;
5228 int row;
5229 int up; /* if TRUE, drag status line up, otherwise down */
5230 int n;
5231
5232 fr = dragwin->w_frame;
5233 curfr = fr;
5234 if (fr != topframe) /* more than one window */
5235 {
5236 fr = fr->fr_parent;
5237 /* When the parent frame is not a column of frames, its parent should
5238 * be. */
5239 if (fr->fr_layout != FR_COL)
5240 {
5241 curfr = fr;
5242 if (fr != topframe) /* only a row of windows, may drag statusline */
5243 fr = fr->fr_parent;
5244 }
5245 }
5246
5247 /* If this is the last frame in a column, may want to resize the parent
5248 * frame instead (go two up to skip a row of frames). */
5249 while (curfr != topframe && curfr->fr_next == NULL)
5250 {
5251 if (fr != topframe)
5252 fr = fr->fr_parent;
5253 curfr = fr;
5254 if (fr != topframe)
5255 fr = fr->fr_parent;
5256 }
5257
5258 if (offset < 0) /* drag up */
5259 {
5260 up = TRUE;
5261 offset = -offset;
5262 /* sum up the room of the current frame and above it */
5263 if (fr == curfr)
5264 {
5265 /* only one window */
5266 room = fr->fr_height - frame_minheight(fr, NULL);
5267 }
5268 else
5269 {
5270 room = 0;
5271 for (fr = fr->fr_child; ; fr = fr->fr_next)
5272 {
5273 room += fr->fr_height - frame_minheight(fr, NULL);
5274 if (fr == curfr)
5275 break;
5276 }
5277 }
5278 fr = curfr->fr_next; /* put fr at frame that grows */
5279 }
5280 else /* drag down */
5281 {
5282 up = FALSE;
5283 /*
5284 * Only dragging the last status line can reduce p_ch.
5285 */
5286 room = Rows - cmdline_row;
5287 if (curfr->fr_next == NULL)
5288 room -= 1;
5289 else
5290 room -= p_ch;
5291 if (room < 0)
5292 room = 0;
5293 /* sum up the room of frames below of the current one */
5294 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5295 room += fr->fr_height - frame_minheight(fr, NULL);
5296 fr = curfr; /* put fr at window that grows */
5297 }
5298
5299 if (room < offset) /* Not enough room */
5300 offset = room; /* Move as far as we can */
5301 if (offset <= 0)
5302 return;
5303
5304 /*
5305 * Grow frame fr by "offset" lines.
5306 * Doesn't happen when dragging the last status line up.
5307 */
5308 if (fr != NULL)
5309 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5310
5311 if (up)
5312 fr = curfr; /* current frame gets smaller */
5313 else
5314 fr = curfr->fr_next; /* next frame gets smaller */
5315
5316 /*
5317 * Now make the other frames smaller.
5318 */
5319 while (fr != NULL && offset > 0)
5320 {
5321 n = frame_minheight(fr, NULL);
5322 if (fr->fr_height - offset <= n)
5323 {
5324 offset -= fr->fr_height - n;
5325 frame_new_height(fr, n, !up, FALSE);
5326 }
5327 else
5328 {
5329 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5330 break;
5331 }
5332 if (up)
5333 fr = fr->fr_prev;
5334 else
5335 fr = fr->fr_next;
5336 }
5337 row = win_comp_pos();
5338 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5339 cmdline_row = row;
5340 p_ch = Rows - cmdline_row;
5341 if (p_ch < 1)
5342 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005343 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005344 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 showmode();
5346}
5347
5348#ifdef FEAT_VERTSPLIT
5349/*
5350 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5351 */
5352 void
5353win_drag_vsep_line(dragwin, offset)
5354 win_T *dragwin;
5355 int offset;
5356{
5357 frame_T *curfr;
5358 frame_T *fr;
5359 int room;
5360 int left; /* if TRUE, drag separator line left, otherwise right */
5361 int n;
5362
5363 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005364 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 return;
5366 curfr = fr;
5367 fr = fr->fr_parent;
5368 /* When the parent frame is not a row of frames, its parent should be. */
5369 if (fr->fr_layout != FR_ROW)
5370 {
5371 if (fr == topframe) /* only a column of windows (cannot happen?) */
5372 return;
5373 curfr = fr;
5374 fr = fr->fr_parent;
5375 }
5376
5377 /* If this is the last frame in a row, may want to resize a parent
5378 * frame instead. */
5379 while (curfr->fr_next == NULL)
5380 {
5381 if (fr == topframe)
5382 break;
5383 curfr = fr;
5384 fr = fr->fr_parent;
5385 if (fr != topframe)
5386 {
5387 curfr = fr;
5388 fr = fr->fr_parent;
5389 }
5390 }
5391
5392 if (offset < 0) /* drag left */
5393 {
5394 left = TRUE;
5395 offset = -offset;
5396 /* sum up the room of the current frame and left of it */
5397 room = 0;
5398 for (fr = fr->fr_child; ; fr = fr->fr_next)
5399 {
5400 room += fr->fr_width - frame_minwidth(fr, NULL);
5401 if (fr == curfr)
5402 break;
5403 }
5404 fr = curfr->fr_next; /* put fr at frame that grows */
5405 }
5406 else /* drag right */
5407 {
5408 left = FALSE;
5409 /* sum up the room of frames right of the current one */
5410 room = 0;
5411 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5412 room += fr->fr_width - frame_minwidth(fr, NULL);
5413 fr = curfr; /* put fr at window that grows */
5414 }
5415
5416 if (room < offset) /* Not enough room */
5417 offset = room; /* Move as far as we can */
5418 if (offset <= 0) /* No room at all, quit. */
5419 return;
5420
5421 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005422 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423
5424 /* shrink other frames: current and at the left or at the right */
5425 if (left)
5426 fr = curfr; /* current frame gets smaller */
5427 else
5428 fr = curfr->fr_next; /* next frame gets smaller */
5429
5430 while (fr != NULL && offset > 0)
5431 {
5432 n = frame_minwidth(fr, NULL);
5433 if (fr->fr_width - offset <= n)
5434 {
5435 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005436 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 }
5438 else
5439 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005440 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 break;
5442 }
5443 if (left)
5444 fr = fr->fr_prev;
5445 else
5446 fr = fr->fr_next;
5447 }
5448 (void)win_comp_pos();
5449 redraw_all_later(NOT_VALID);
5450}
5451#endif /* FEAT_VERTSPLIT */
5452#endif /* FEAT_MOUSE */
5453
5454#endif /* FEAT_WINDOWS */
5455
5456/*
5457 * Set the height of a window.
5458 * This takes care of the things inside the window, not what happens to the
5459 * window position, the frame or to other windows.
5460 */
5461 static void
5462win_new_height(wp, height)
5463 win_T *wp;
5464 int height;
5465{
5466 linenr_T lnum;
5467 int sline, line_size;
5468#define FRACTION_MULT 16384L
5469
5470 /* Don't want a negative height. Happens when splitting a tiny window.
5471 * Will equalize heights soon to fix it. */
5472 if (height < 0)
5473 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005474 if (wp->w_height == height)
5475 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
5477 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5478 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5479 + FRACTION_MULT / 2) / (long)wp->w_height;
5480
5481 wp->w_height = height;
5482 wp->w_skipcol = 0;
5483
5484 /* Don't change w_topline when height is zero. Don't set w_topline when
5485 * 'scrollbind' is set and this isn't the current window. */
5486 if (height > 0
5487#ifdef FEAT_SCROLLBIND
5488 && (!wp->w_p_scb || wp == curwin)
5489#endif
5490 )
5491 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005492 /*
5493 * Find a value for w_topline that shows the cursor at the same
5494 * relative position in the window as before (more or less).
5495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 lnum = wp->w_cursor.lnum;
5497 if (lnum < 1) /* can happen when starting up */
5498 lnum = 1;
5499 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5500 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5501 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005502
5503 if (sline >= 0)
5504 {
5505 /* Make sure the whole cursor line is visible, if possible. */
5506 int rows = plines_win(wp, lnum, FALSE);
5507
5508 if (sline > wp->w_height - rows)
5509 {
5510 sline = wp->w_height - rows;
5511 wp->w_wrow -= rows - line_size;
5512 }
5513 }
5514
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 if (sline < 0)
5516 {
5517 /*
5518 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005519 * Make cursor line the first line in the window. If not enough
5520 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 */
5522 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005523 if (wp->w_wrow >= wp->w_height
5524 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5525 {
5526 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5527 --wp->w_wrow;
5528 while (wp->w_wrow >= wp->w_height)
5529 {
5530 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5531 + win_col_off2(wp);
5532 --wp->w_wrow;
5533 }
5534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 }
5536 else
5537 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005538 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {
5540#ifdef FEAT_FOLDING
5541 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5542 if (lnum == 1)
5543 {
5544 /* first line in buffer is folded */
5545 line_size = 1;
5546 --sline;
5547 break;
5548 }
5549#endif
5550 --lnum;
5551#ifdef FEAT_DIFF
5552 if (lnum == wp->w_topline)
5553 line_size = plines_win_nofill(wp, lnum, TRUE)
5554 + wp->w_topfill;
5555 else
5556#endif
5557 line_size = plines_win(wp, lnum, TRUE);
5558 sline -= line_size;
5559 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005560
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 if (sline < 0)
5562 {
5563 /*
5564 * Line we want at top would go off top of screen. Use next
5565 * line instead.
5566 */
5567#ifdef FEAT_FOLDING
5568 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5569#endif
5570 lnum++;
5571 wp->w_wrow -= line_size + sline;
5572 }
5573 else if (sline > 0)
5574 {
5575 /* First line of file reached, use that as topline. */
5576 lnum = 1;
5577 wp->w_wrow -= sline;
5578 }
5579 }
5580 set_topline(wp, lnum);
5581 }
5582
5583 if (wp == curwin)
5584 {
5585 if (p_so)
5586 update_topline();
5587 curs_columns(FALSE); /* validate w_wrow */
5588 }
5589 wp->w_prev_fraction_row = wp->w_wrow;
5590
5591 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005592 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593#ifdef FEAT_WINDOWS
5594 wp->w_redr_status = TRUE;
5595#endif
5596 invalidate_botline_win(wp);
5597}
5598
5599#ifdef FEAT_VERTSPLIT
5600/*
5601 * Set the width of a window.
5602 */
5603 static void
5604win_new_width(wp, width)
5605 win_T *wp;
5606 int width;
5607{
5608 wp->w_width = width;
5609 wp->w_lines_valid = 0;
5610 changed_line_abv_curs_win(wp);
5611 invalidate_botline_win(wp);
5612 if (wp == curwin)
5613 {
5614 update_topline();
5615 curs_columns(TRUE); /* validate w_wrow */
5616 }
5617 redraw_win_later(wp, NOT_VALID);
5618 wp->w_redr_status = TRUE;
5619}
5620#endif
5621
5622 void
5623win_comp_scroll(wp)
5624 win_T *wp;
5625{
5626 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5627 if (wp->w_p_scr == 0)
5628 wp->w_p_scr = 1;
5629}
5630
5631/*
5632 * command_height: called whenever p_ch has been changed
5633 */
5634 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005635command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
5637#ifdef FEAT_WINDOWS
5638 int h;
5639 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005640 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005642 /* Use the value of p_ch that we remembered. This is needed for when the
5643 * GUI starts up, we can't be sure in what order things happen. And when
5644 * p_ch was changed in another tab page. */
5645 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005646
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 /* Find bottom frame with width of screen. */
5648 frp = lastwin->w_frame;
5649# ifdef FEAT_VERTSPLIT
5650 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5651 frp = frp->fr_parent;
5652# endif
5653
5654 /* Avoid changing the height of a window with 'winfixheight' set. */
5655 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5656 && frp->fr_win->w_p_wfh)
5657 frp = frp->fr_prev;
5658
5659 if (starting != NO_SCREEN)
5660 {
5661 cmdline_row = Rows - p_ch;
5662
5663 if (p_ch > old_p_ch) /* p_ch got bigger */
5664 {
5665 while (p_ch > old_p_ch)
5666 {
5667 if (frp == NULL)
5668 {
5669 EMSG(_(e_noroom));
5670 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005671 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 cmdline_row = Rows - p_ch;
5673 break;
5674 }
5675 h = frp->fr_height - frame_minheight(frp, NULL);
5676 if (h > p_ch - old_p_ch)
5677 h = p_ch - old_p_ch;
5678 old_p_ch += h;
5679 frame_add_height(frp, -h);
5680 frp = frp->fr_prev;
5681 }
5682
5683 /* Recompute window positions. */
5684 (void)win_comp_pos();
5685
5686 /* clear the lines added to cmdline */
5687 if (full_screen)
5688 screen_fill((int)(cmdline_row), (int)Rows, 0,
5689 (int)Columns, ' ', ' ', 0);
5690 msg_row = cmdline_row;
5691 redraw_cmdline = TRUE;
5692 return;
5693 }
5694
5695 if (msg_row < cmdline_row)
5696 msg_row = cmdline_row;
5697 redraw_cmdline = TRUE;
5698 }
5699 frame_add_height(frp, (int)(old_p_ch - p_ch));
5700
5701 /* Recompute window positions. */
5702 if (frp != lastwin->w_frame)
5703 (void)win_comp_pos();
5704#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005706 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707#endif
5708}
5709
5710#if defined(FEAT_WINDOWS) || defined(PROTO)
5711/*
5712 * Resize frame "frp" to be "n" lines higher (negative for less high).
5713 * Also resize the frames it is contained in.
5714 */
5715 static void
5716frame_add_height(frp, n)
5717 frame_T *frp;
5718 int n;
5719{
5720 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5721 for (;;)
5722 {
5723 frp = frp->fr_parent;
5724 if (frp == NULL)
5725 break;
5726 frp->fr_height += n;
5727 }
5728}
5729
5730/*
5731 * Add or remove a status line for the bottom window(s), according to the
5732 * value of 'laststatus'.
5733 */
5734 void
5735last_status(morewin)
5736 int morewin; /* pretend there are two or more windows */
5737{
5738 /* Don't make a difference between horizontal or vertical split. */
5739 last_status_rec(topframe, (p_ls == 2
5740 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5741}
5742
5743 static void
5744last_status_rec(fr, statusline)
5745 frame_T *fr;
5746 int statusline;
5747{
5748 frame_T *fp;
5749 win_T *wp;
5750
5751 if (fr->fr_layout == FR_LEAF)
5752 {
5753 wp = fr->fr_win;
5754 if (wp->w_status_height != 0 && !statusline)
5755 {
5756 /* remove status line */
5757 win_new_height(wp, wp->w_height + 1);
5758 wp->w_status_height = 0;
5759 comp_col();
5760 }
5761 else if (wp->w_status_height == 0 && statusline)
5762 {
5763 /* Find a frame to take a line from. */
5764 fp = fr;
5765 while (fp->fr_height <= frame_minheight(fp, NULL))
5766 {
5767 if (fp == topframe)
5768 {
5769 EMSG(_(e_noroom));
5770 return;
5771 }
5772 /* In a column of frames: go to frame above. If already at
5773 * the top or in a row of frames: go to parent. */
5774 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5775 fp = fp->fr_prev;
5776 else
5777 fp = fp->fr_parent;
5778 }
5779 wp->w_status_height = 1;
5780 if (fp != fr)
5781 {
5782 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5783 frame_fix_height(wp);
5784 (void)win_comp_pos();
5785 }
5786 else
5787 win_new_height(wp, wp->w_height - 1);
5788 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005789 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 }
5791 }
5792#ifdef FEAT_VERTSPLIT
5793 else if (fr->fr_layout == FR_ROW)
5794 {
5795 /* vertically split windows, set status line for each one */
5796 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5797 last_status_rec(fp, statusline);
5798 }
5799#endif
5800 else
5801 {
5802 /* horizontally split window, set status line for last one */
5803 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5804 ;
5805 last_status_rec(fp, statusline);
5806 }
5807}
5808
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005809/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005810 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005811 */
5812 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005813tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005814{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005815#ifdef FEAT_GUI_TABLINE
5816 /* When the GUI has the tabline then this always returns zero. */
5817 if (gui_use_tabline())
5818 return 0;
5819#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005820 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005821 {
5822 case 0: return 0;
5823 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5824 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005825 return 1;
5826}
5827
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828#endif /* FEAT_WINDOWS */
5829
5830#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5831/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005832 * Get the file name at the cursor.
5833 * If Visual mode is active, use the selected text if it's in one line.
5834 * Returns the name in allocated memory, NULL for failure.
5835 */
5836 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005837grab_file_name(count, file_lnum)
5838 long count;
5839 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005840{
5841# ifdef FEAT_VISUAL
5842 if (VIsual_active)
5843 {
5844 int len;
5845 char_u *ptr;
5846
5847 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5848 return NULL;
5849 return find_file_name_in_path(ptr, len,
5850 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5851 }
5852# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005853 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5854 file_lnum);
5855
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005856}
5857
5858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 * Return the file name under or after the cursor.
5860 *
5861 * The 'path' option is searched if the file name is not absolute.
5862 * The string returned has been alloc'ed and should be freed by the caller.
5863 * NULL is returned if the file name or file is not found.
5864 *
5865 * options:
5866 * FNAME_MESS give error messages
5867 * FNAME_EXP expand to path
5868 * FNAME_HYP check for hypertext link
5869 * FNAME_INCL apply "includeexpr"
5870 */
5871 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005872file_name_at_cursor(options, count, file_lnum)
5873 int options;
5874 long count;
5875 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876{
5877 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005878 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5879 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880}
5881
5882/*
5883 * Return the name of the file under or after ptr[col].
5884 * Otherwise like file_name_at_cursor().
5885 */
5886 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005887file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 char_u *line;
5889 int col;
5890 int options;
5891 long count;
5892 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005893 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894{
5895 char_u *ptr;
5896 int len;
5897
5898 /*
5899 * search forward for what could be the start of a file name
5900 */
5901 ptr = line + col;
5902 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005903 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 if (*ptr == NUL) /* nothing found */
5905 {
5906 if (options & FNAME_MESS)
5907 EMSG(_("E446: No file name under cursor"));
5908 return NULL;
5909 }
5910
5911 /*
5912 * Search backward for first char of the file name.
5913 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5914 */
5915 while (ptr > line)
5916 {
5917#ifdef FEAT_MBYTE
5918 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5919 ptr -= len + 1;
5920 else
5921#endif
5922 if (vim_isfilec(ptr[-1])
5923 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5924 --ptr;
5925 else
5926 break;
5927 }
5928
5929 /*
5930 * Search forward for the last char of the file name.
5931 * Also allow "://" when ':' is not in 'isfname'.
5932 */
5933 len = 0;
5934 while (vim_isfilec(ptr[len])
5935 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5936#ifdef FEAT_MBYTE
5937 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005938 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 else
5940#endif
5941 ++len;
5942
5943 /*
5944 * If there is trailing punctuation, remove it.
5945 * But don't remove "..", could be a directory name.
5946 */
5947 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5948 && ptr[len - 2] != '.')
5949 --len;
5950
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005951 if (file_lnum != NULL)
5952 {
5953 char_u *p;
5954
5955 /* Get the number after the file name and a separator character */
5956 p = ptr + len;
5957 p = skipwhite(p);
5958 if (*p != NUL)
5959 {
5960 if (!isdigit(*p))
5961 ++p; /* skip the separator */
5962 p = skipwhite(p);
5963 if (isdigit(*p))
5964 *file_lnum = (int)getdigits(&p);
5965 }
5966 }
5967
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5969}
5970
5971# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5972static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5973
5974 static char_u *
5975eval_includeexpr(ptr, len)
5976 char_u *ptr;
5977 int len;
5978{
5979 char_u *res;
5980
5981 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005982 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005983 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 set_vim_var_string(VV_FNAME, NULL, 0);
5985 return res;
5986}
5987#endif
5988
5989/*
5990 * Return the name of the file ptr[len] in 'path'.
5991 * Otherwise like file_name_at_cursor().
5992 */
5993 char_u *
5994find_file_name_in_path(ptr, len, options, count, rel_fname)
5995 char_u *ptr;
5996 int len;
5997 int options;
5998 long count;
5999 char_u *rel_fname; /* file we are searching relative to */
6000{
6001 char_u *file_name;
6002 int c;
6003# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6004 char_u *tofree = NULL;
6005
6006 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6007 {
6008 tofree = eval_includeexpr(ptr, len);
6009 if (tofree != NULL)
6010 {
6011 ptr = tofree;
6012 len = (int)STRLEN(ptr);
6013 }
6014 }
6015# endif
6016
6017 if (options & FNAME_EXP)
6018 {
6019 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6020 TRUE, rel_fname);
6021
6022# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6023 /*
6024 * If the file could not be found in a normal way, try applying
6025 * 'includeexpr' (unless done already).
6026 */
6027 if (file_name == NULL
6028 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6029 {
6030 tofree = eval_includeexpr(ptr, len);
6031 if (tofree != NULL)
6032 {
6033 ptr = tofree;
6034 len = (int)STRLEN(ptr);
6035 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6036 TRUE, rel_fname);
6037 }
6038 }
6039# endif
6040 if (file_name == NULL && (options & FNAME_MESS))
6041 {
6042 c = ptr[len];
6043 ptr[len] = NUL;
6044 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6045 ptr[len] = c;
6046 }
6047
6048 /* Repeat finding the file "count" times. This matters when it
6049 * appears several times in the path. */
6050 while (file_name != NULL && --count > 0)
6051 {
6052 vim_free(file_name);
6053 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6054 }
6055 }
6056 else
6057 file_name = vim_strnsave(ptr, len);
6058
6059# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6060 vim_free(tofree);
6061# endif
6062
6063 return file_name;
6064}
6065#endif /* FEAT_SEARCHPATH */
6066
6067/*
6068 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6069 * Also check for ":\\", which MS Internet Explorer accepts, return
6070 * URL_BACKSLASH.
6071 */
6072 static int
6073path_is_url(p)
6074 char_u *p;
6075{
6076 if (STRNCMP(p, "://", (size_t)3) == 0)
6077 return URL_SLASH;
6078 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6079 return URL_BACKSLASH;
6080 return 0;
6081}
6082
6083/*
6084 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6085 * Return URL_BACKSLASH for "name:\\".
6086 * Return zero otherwise.
6087 */
6088 int
6089path_with_url(fname)
6090 char_u *fname;
6091{
6092 char_u *p;
6093
6094 for (p = fname; isalpha(*p); ++p)
6095 ;
6096 return path_is_url(p);
6097}
6098
6099/*
6100 * Return TRUE if "name" is a full (absolute) path name or URL.
6101 */
6102 int
6103vim_isAbsName(name)
6104 char_u *name;
6105{
6106 return (path_with_url(name) != 0 || mch_isFullName(name));
6107}
6108
6109/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006110 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 *
6112 * return FAIL for failure, OK otherwise
6113 */
6114 int
6115vim_FullName(fname, buf, len, force)
6116 char_u *fname, *buf;
6117 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006118 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119{
6120 int retval = OK;
6121 int url;
6122
6123 *buf = NUL;
6124 if (fname == NULL)
6125 return FAIL;
6126
6127 url = path_with_url(fname);
6128 if (!url)
6129 retval = mch_FullName(fname, buf, len, force);
6130 if (url || retval == FAIL)
6131 {
6132 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006133 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 }
6135#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6136 slash_adjust(buf);
6137#endif
6138 return retval;
6139}
6140
6141/*
6142 * Return the minimal number of rows that is needed on the screen to display
6143 * the current number of windows.
6144 */
6145 int
6146min_rows()
6147{
6148 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006149#ifdef FEAT_WINDOWS
6150 tabpage_T *tp;
6151 int n;
6152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153
6154 if (firstwin == NULL) /* not initialized yet */
6155 return MIN_LINES;
6156
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006158 total = 0;
6159 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6160 {
6161 n = frame_minheight(tp->tp_topframe, NULL);
6162 if (total < n)
6163 total = n;
6164 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006165 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006167 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006169 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 return total;
6171}
6172
6173/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006174 * Return TRUE if there is only one window (in the current tab page), not
6175 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006176 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 */
6178 int
6179only_one_window()
6180{
6181#ifdef FEAT_WINDOWS
6182 int count = 0;
6183 win_T *wp;
6184
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006185 /* If there is another tab page there always is another window. */
6186 if (first_tabpage->tp_next != NULL)
6187 return FALSE;
6188
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006190 if ((!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191# ifdef FEAT_QUICKFIX
6192 || wp->w_p_pvw
6193# endif
6194 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006195# ifdef FEAT_AUTOCMD
6196 && wp != aucmd_win
6197# endif
6198 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 ++count;
6200 return (count <= 1);
6201#else
6202 return TRUE;
6203#endif
6204}
6205
6206#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6207/*
6208 * Correct the cursor line number in other windows. Used after changing the
6209 * current buffer, and before applying autocommands.
6210 * When "do_curwin" is TRUE, also check current window.
6211 */
6212 void
6213check_lnums(do_curwin)
6214 int do_curwin;
6215{
6216 win_T *wp;
6217
6218#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006219 tabpage_T *tp;
6220
6221 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6223#else
6224 wp = curwin;
6225 if (do_curwin)
6226#endif
6227 {
6228 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6229 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6230 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6231 wp->w_topline = curbuf->b_ml.ml_line_count;
6232 }
6233}
6234#endif
6235
6236#if defined(FEAT_WINDOWS) || defined(PROTO)
6237
6238/*
6239 * A snapshot of the window sizes, to restore them after closing the help
6240 * window.
6241 * Only these fields are used:
6242 * fr_layout
6243 * fr_width
6244 * fr_height
6245 * fr_next
6246 * fr_child
6247 * fr_win (only valid for the old curwin, NULL otherwise)
6248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249
6250/*
6251 * Create a snapshot of the current frame sizes.
6252 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006253 void
6254make_snapshot(idx)
6255 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006257 clear_snapshot(curtab, idx);
6258 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259}
6260
6261 static void
6262make_snapshot_rec(fr, frp)
6263 frame_T *fr;
6264 frame_T **frp;
6265{
6266 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6267 if (*frp == NULL)
6268 return;
6269 (*frp)->fr_layout = fr->fr_layout;
6270# ifdef FEAT_VERTSPLIT
6271 (*frp)->fr_width = fr->fr_width;
6272# endif
6273 (*frp)->fr_height = fr->fr_height;
6274 if (fr->fr_next != NULL)
6275 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6276 if (fr->fr_child != NULL)
6277 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6278 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6279 (*frp)->fr_win = curwin;
6280}
6281
6282/*
6283 * Remove any existing snapshot.
6284 */
6285 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006286clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006287 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006288 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006290 clear_snapshot_rec(tp->tp_snapshot[idx]);
6291 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292}
6293
6294 static void
6295clear_snapshot_rec(fr)
6296 frame_T *fr;
6297{
6298 if (fr != NULL)
6299 {
6300 clear_snapshot_rec(fr->fr_next);
6301 clear_snapshot_rec(fr->fr_child);
6302 vim_free(fr);
6303 }
6304}
6305
6306/*
6307 * Restore a previously created snapshot, if there is any.
6308 * This is only done if the screen size didn't change and the window layout is
6309 * still the same.
6310 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006311 void
6312restore_snapshot(idx, close_curwin)
6313 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 int close_curwin; /* closing current window */
6315{
6316 win_T *wp;
6317
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006318 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006320 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006322 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6323 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006325 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 win_comp_pos();
6327 if (wp != NULL && close_curwin)
6328 win_goto(wp);
6329 redraw_all_later(CLEAR);
6330 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006331 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332}
6333
6334/*
6335 * Check if frames "sn" and "fr" have the same layout, same following frames
6336 * and same children.
6337 */
6338 static int
6339check_snapshot_rec(sn, fr)
6340 frame_T *sn;
6341 frame_T *fr;
6342{
6343 if (sn->fr_layout != fr->fr_layout
6344 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6345 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6346 || (sn->fr_next != NULL
6347 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6348 || (sn->fr_child != NULL
6349 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6350 return FAIL;
6351 return OK;
6352}
6353
6354/*
6355 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6356 * following frames and children.
6357 * Returns a pointer to the old current window, or NULL.
6358 */
6359 static win_T *
6360restore_snapshot_rec(sn, fr)
6361 frame_T *sn;
6362 frame_T *fr;
6363{
6364 win_T *wp = NULL;
6365 win_T *wp2;
6366
6367 fr->fr_height = sn->fr_height;
6368# ifdef FEAT_VERTSPLIT
6369 fr->fr_width = sn->fr_width;
6370# endif
6371 if (fr->fr_layout == FR_LEAF)
6372 {
6373 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6374# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006375 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376# endif
6377 wp = sn->fr_win;
6378 }
6379 if (sn->fr_next != NULL)
6380 {
6381 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6382 if (wp2 != NULL)
6383 wp = wp2;
6384 }
6385 if (sn->fr_child != NULL)
6386 {
6387 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6388 if (wp2 != NULL)
6389 wp = wp2;
6390 }
6391 return wp;
6392}
6393
6394#endif
6395
6396#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6397/*
6398 * Return TRUE if there is any vertically split window.
6399 */
6400 int
6401win_hasvertsplit()
6402{
6403 frame_T *fr;
6404
6405 if (topframe->fr_layout == FR_ROW)
6406 return TRUE;
6407
6408 if (topframe->fr_layout == FR_COL)
6409 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6410 if (fr->fr_layout == FR_ROW)
6411 return TRUE;
6412
6413 return FALSE;
6414}
6415#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006416
6417#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6418/*
6419 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006420 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006421 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6422 * If no particular ID is desired, -1 must be specified for 'id'.
6423 * Return ID of added match, -1 on failure.
6424 */
6425 int
6426match_add(wp, grp, pat, prio, id)
6427 win_T *wp;
6428 char_u *grp;
6429 char_u *pat;
6430 int prio;
6431 int id;
6432{
6433 matchitem_T *cur;
6434 matchitem_T *prev;
6435 matchitem_T *m;
6436 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006437 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006438
6439 if (*grp == NUL || *pat == NUL)
6440 return -1;
6441 if (id < -1 || id == 0)
6442 {
6443 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6444 return -1;
6445 }
6446 if (id != -1)
6447 {
6448 cur = wp->w_match_head;
6449 while (cur != NULL)
6450 {
6451 if (cur->id == id)
6452 {
6453 EMSGN("E801: ID already taken: %ld", id);
6454 return -1;
6455 }
6456 cur = cur->next;
6457 }
6458 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006459 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006460 {
6461 EMSG2(_(e_nogroup), grp);
6462 return -1;
6463 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006464 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006465 {
6466 EMSG2(_(e_invarg2), pat);
6467 return -1;
6468 }
6469
6470 /* Find available match ID. */
6471 while (id == -1)
6472 {
6473 cur = wp->w_match_head;
6474 while (cur != NULL && cur->id != wp->w_next_match_id)
6475 cur = cur->next;
6476 if (cur == NULL)
6477 id = wp->w_next_match_id;
6478 wp->w_next_match_id++;
6479 }
6480
6481 /* Build new match. */
6482 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6483 m->id = id;
6484 m->priority = prio;
6485 m->pattern = vim_strsave(pat);
6486 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006487 m->match.regprog = regprog;
6488 m->match.rmm_ic = FALSE;
6489 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006490
6491 /* Insert new match. The match list is in ascending order with regard to
6492 * the match priorities. */
6493 cur = wp->w_match_head;
6494 prev = cur;
6495 while (cur != NULL && prio >= cur->priority)
6496 {
6497 prev = cur;
6498 cur = cur->next;
6499 }
6500 if (cur == prev)
6501 wp->w_match_head = m;
6502 else
6503 prev->next = m;
6504 m->next = cur;
6505
6506 redraw_later(SOME_VALID);
6507 return id;
6508}
6509
6510/*
6511 * Delete match with ID 'id' in the match list of window 'wp'.
6512 * Print error messages if 'perr' is TRUE.
6513 */
6514 int
6515match_delete(wp, id, perr)
6516 win_T *wp;
6517 int id;
6518 int perr;
6519{
6520 matchitem_T *cur = wp->w_match_head;
6521 matchitem_T *prev = cur;
6522
6523 if (id < 1)
6524 {
6525 if (perr == TRUE)
6526 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6527 id);
6528 return -1;
6529 }
6530 while (cur != NULL && cur->id != id)
6531 {
6532 prev = cur;
6533 cur = cur->next;
6534 }
6535 if (cur == NULL)
6536 {
6537 if (perr == TRUE)
6538 EMSGN("E803: ID not found: %ld", id);
6539 return -1;
6540 }
6541 if (cur == prev)
6542 wp->w_match_head = cur->next;
6543 else
6544 prev->next = cur->next;
6545 vim_free(cur->match.regprog);
6546 vim_free(cur->pattern);
6547 vim_free(cur);
6548 redraw_later(SOME_VALID);
6549 return 0;
6550}
6551
6552/*
6553 * Delete all matches in the match list of window 'wp'.
6554 */
6555 void
6556clear_matches(wp)
6557 win_T *wp;
6558{
6559 matchitem_T *m;
6560
6561 while (wp->w_match_head != NULL)
6562 {
6563 m = wp->w_match_head->next;
6564 vim_free(wp->w_match_head->match.regprog);
6565 vim_free(wp->w_match_head->pattern);
6566 vim_free(wp->w_match_head);
6567 wp->w_match_head = m;
6568 }
6569 redraw_later(SOME_VALID);
6570}
6571
6572/*
6573 * Get match from ID 'id' in window 'wp'.
6574 * Return NULL if match not found.
6575 */
6576 matchitem_T *
6577get_match(wp, id)
6578 win_T *wp;
6579 int id;
6580{
6581 matchitem_T *cur = wp->w_match_head;
6582
6583 while (cur != NULL && cur->id != id)
6584 cur = cur->next;
6585 return cur;
6586}
6587#endif