blob: 08457683b89dc3cfa4ffea17deb21dc9ea18b4ae [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#include "vim.h"
11
Bram Moolenaar071d4272004-06-13 20:20:40 +000012static int path_is_url __ARGS((char_u *p));
13#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar884ae642009-02-22 01:37:59 +000014static void win_init __ARGS((win_T *newp, win_T *oldp, int flags));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000015static void win_init_some __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
17static void frame_setheight __ARGS((frame_T *curfrp, int height));
18#ifdef FEAT_VERTSPLIT
19static void frame_setwidth __ARGS((frame_T *curfrp, int width));
20#endif
21static void win_exchange __ARGS((long));
22static void win_rotate __ARGS((int, int));
23static void win_totop __ARGS((int size, int flags));
24static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000025static int last_window __ARGS((void));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000026static int one_window __ARGS((void));
Bram Moolenaarf740b292006-02-16 22:11:02 +000027static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
Bram Moolenaarf740b292006-02-16 22:11:02 +000028static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000029static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000030static win_T *frame2win __ARGS((frame_T *frp));
31static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
32static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
33static int frame_fixed_height __ARGS((frame_T *frp));
34#ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000035static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000036static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000037static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000038static void frame_add_vsep __ARGS((frame_T *frp));
39static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
40static void frame_fix_width __ARGS((win_T *wp));
41#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000042#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000043static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000044static void new_frame __ARGS((win_T *wp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000045#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000046static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000047static int leave_tabpage __ARGS((buf_T *new_curbuf));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000048static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000049static void frame_fix_height __ARGS((win_T *wp));
50static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
51static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
Bram Moolenaarf740b292006-02-16 22:11:02 +000052static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000053static void frame_append __ARGS((frame_T *after, frame_T *frp));
54static void frame_insert __ARGS((frame_T *before, frame_T *frp));
55static void frame_remove __ARGS((frame_T *frp));
56#ifdef FEAT_VERTSPLIT
57static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static void win_goto_ver __ARGS((int up, long count));
59static void win_goto_hor __ARGS((int left, long count));
60#endif
61static void frame_add_height __ARGS((frame_T *frp, int n));
62static void last_status_rec __ARGS((frame_T *fr, int statusline));
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000065static void clear_snapshot __ARGS((tabpage_T *tp, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static void clear_snapshot_rec __ARGS((frame_T *fr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000067static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
68static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
69
70#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000071
Bram Moolenaar746ebd32009-06-16 14:01:43 +000072static win_T *win_alloc __ARGS((win_T *after, int hidden));
Bram Moolenaar071d4272004-06-13 20:20:40 +000073static void win_new_height __ARGS((win_T *, int));
74
75#define URL_SLASH 1 /* path_is_url() has found "://" */
76#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
77
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000078#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Bram Moolenaar05159a02005-02-26 23:04:13 +000080#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000081# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000082#else
83# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000084#endif
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000087
88static char *m_onlyone = N_("Already only one window");
89
Bram Moolenaar071d4272004-06-13 20:20:40 +000090/*
91 * all CTRL-W window commands are handled here, called from normal_cmd().
92 */
93 void
94do_window(nchar, Prenum, xchar)
95 int nchar;
96 long Prenum;
97 int xchar; /* extra char from ":wincmd gx" or NUL */
98{
99 long Prenum1;
100 win_T *wp;
101#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
102 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000103 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105#ifdef FEAT_FIND_ID
106 int type = FIND_DEFINE;
107 int len;
108#endif
109 char_u cbuf[40];
110
111 if (Prenum == 0)
112 Prenum1 = 1;
113 else
114 Prenum1 = Prenum;
115
116#ifdef FEAT_CMDWIN
117# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
118#else
119# define CHECK_CMDWIN
120#endif
121
122 switch (nchar)
123 {
124/* split current window in two parts, horizontally */
125 case 'S':
126 case Ctrl_S:
127 case 's':
128 CHECK_CMDWIN
129#ifdef FEAT_VISUAL
130 reset_VIsual_and_resel(); /* stop Visual mode */
131#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000132#ifdef FEAT_QUICKFIX
133 /* When splitting the quickfix window open a new buffer in it,
134 * don't replicate the quickfix buffer. */
135 if (bt_quickfix(curbuf))
136 goto newwindow;
137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#ifdef FEAT_GUI
139 need_mouse_correct = TRUE;
140#endif
141 win_split((int)Prenum, 0);
142 break;
143
144#ifdef FEAT_VERTSPLIT
145/* split current window in two parts, vertically */
146 case Ctrl_V:
147 case 'v':
148 CHECK_CMDWIN
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000149# ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000151# endif
152# ifdef FEAT_QUICKFIX
153 /* When splitting the quickfix window open a new buffer in it,
154 * don't replicate the quickfix buffer. */
155 if (bt_quickfix(curbuf))
156 goto newwindow;
157# endif
158# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 win_split((int)Prenum, WSP_VERT);
162 break;
163#endif
164
165/* split current window and edit alternate file */
166 case Ctrl_HAT:
167 case '^':
168 CHECK_CMDWIN
169#ifdef FEAT_VISUAL
170 reset_VIsual_and_resel(); /* stop Visual mode */
171#endif
172 STRCPY(cbuf, "split #");
173 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000174 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
175 "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 do_cmdline_cmd(cbuf);
177 break;
178
179/* open new window */
180 case Ctrl_N:
181 case 'n':
182 CHECK_CMDWIN
183#ifdef FEAT_VISUAL
184 reset_VIsual_and_resel(); /* stop Visual mode */
185#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000186#ifdef FEAT_QUICKFIX
187newwindow:
188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000190 /* window height */
191 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 else
193 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000194#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
195 if (nchar == 'v' || nchar == Ctrl_V)
196 STRCAT(cbuf, "v");
197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 STRCAT(cbuf, "new");
199 do_cmdline_cmd(cbuf);
200 break;
201
202/* quit current window */
203 case Ctrl_Q:
204 case 'q':
205#ifdef FEAT_VISUAL
206 reset_VIsual_and_resel(); /* stop Visual mode */
207#endif
208 do_cmdline_cmd((char_u *)"quit");
209 break;
210
211/* close current window */
212 case Ctrl_C:
213 case 'c':
214#ifdef FEAT_VISUAL
215 reset_VIsual_and_resel(); /* stop Visual mode */
216#endif
217 do_cmdline_cmd((char_u *)"close");
218 break;
219
220#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
221/* close preview window */
222 case Ctrl_Z:
223 case 'z':
224 CHECK_CMDWIN
225#ifdef FEAT_VISUAL
226 reset_VIsual_and_resel(); /* stop Visual mode */
227#endif
228 do_cmdline_cmd((char_u *)"pclose");
229 break;
230
231/* cursor to preview window */
232 case 'P':
233 for (wp = firstwin; wp != NULL; wp = wp->w_next)
234 if (wp->w_p_pvw)
235 break;
236 if (wp == NULL)
237 EMSG(_("E441: There is no preview window"));
238 else
239 win_goto(wp);
240 break;
241#endif
242
243/* close all but current window */
244 case Ctrl_O:
245 case 'o':
246 CHECK_CMDWIN
247#ifdef FEAT_VISUAL
248 reset_VIsual_and_resel(); /* stop Visual mode */
249#endif
250 do_cmdline_cmd((char_u *)"only");
251 break;
252
253/* cursor to next window with wrap around */
254 case Ctrl_W:
255 case 'w':
256/* cursor to previous window with wrap around */
257 case 'W':
258 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000259 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 beep_flush();
261 else
262 {
263 if (Prenum) /* go to specified window */
264 {
265 for (wp = firstwin; --Prenum > 0; )
266 {
267 if (wp->w_next == NULL)
268 break;
269 else
270 wp = wp->w_next;
271 }
272 }
273 else
274 {
275 if (nchar == 'W') /* go to previous window */
276 {
277 wp = curwin->w_prev;
278 if (wp == NULL)
279 wp = lastwin; /* wrap around */
280 }
281 else /* go to next window */
282 {
283 wp = curwin->w_next;
284 if (wp == NULL)
285 wp = firstwin; /* wrap around */
286 }
287 }
288 win_goto(wp);
289 }
290 break;
291
292/* cursor to window below */
293 case 'j':
294 case K_DOWN:
295 case Ctrl_J:
296 CHECK_CMDWIN
297#ifdef FEAT_VERTSPLIT
298 win_goto_ver(FALSE, Prenum1);
299#else
300 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
301 wp = wp->w_next)
302 ;
303 win_goto(wp);
304#endif
305 break;
306
307/* cursor to window above */
308 case 'k':
309 case K_UP:
310 case Ctrl_K:
311 CHECK_CMDWIN
312#ifdef FEAT_VERTSPLIT
313 win_goto_ver(TRUE, Prenum1);
314#else
315 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
316 wp = wp->w_prev)
317 ;
318 win_goto(wp);
319#endif
320 break;
321
322#ifdef FEAT_VERTSPLIT
323/* cursor to left window */
324 case 'h':
325 case K_LEFT:
326 case Ctrl_H:
327 case K_BS:
328 CHECK_CMDWIN
329 win_goto_hor(TRUE, Prenum1);
330 break;
331
332/* cursor to right window */
333 case 'l':
334 case K_RIGHT:
335 case Ctrl_L:
336 CHECK_CMDWIN
337 win_goto_hor(FALSE, Prenum1);
338 break;
339#endif
340
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000341/* move window to new tab page */
342 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000343 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000344 MSG(_(m_onlyone));
345 else
346 {
347 tabpage_T *oldtab = curtab;
348 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000349
350 /* First create a new tab with the window, then go back to
351 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000352 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000353 if (win_new_tabpage((int)Prenum) == OK
354 && valid_tabpage(oldtab))
355 {
356 newtab = curtab;
357 goto_tabpage_tp(oldtab);
358 if (curwin == wp)
359 win_close(curwin, FALSE);
360 if (valid_tabpage(newtab))
361 goto_tabpage_tp(newtab);
362 }
363 }
364 break;
365
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366/* cursor to top-left window */
367 case 't':
368 case Ctrl_T:
369 win_goto(firstwin);
370 break;
371
372/* cursor to bottom-right window */
373 case 'b':
374 case Ctrl_B:
375 win_goto(lastwin);
376 break;
377
378/* cursor to last accessed (previous) window */
379 case 'p':
380 case Ctrl_P:
381 if (prevwin == NULL)
382 beep_flush();
383 else
384 win_goto(prevwin);
385 break;
386
387/* exchange current and next window */
388 case 'x':
389 case Ctrl_X:
390 CHECK_CMDWIN
391 win_exchange(Prenum);
392 break;
393
394/* rotate windows downwards */
395 case Ctrl_R:
396 case 'r':
397 CHECK_CMDWIN
398#ifdef FEAT_VISUAL
399 reset_VIsual_and_resel(); /* stop Visual mode */
400#endif
401 win_rotate(FALSE, (int)Prenum1); /* downwards */
402 break;
403
404/* rotate windows upwards */
405 case 'R':
406 CHECK_CMDWIN
407#ifdef FEAT_VISUAL
408 reset_VIsual_and_resel(); /* stop Visual mode */
409#endif
410 win_rotate(TRUE, (int)Prenum1); /* upwards */
411 break;
412
413/* move window to the very top/bottom/left/right */
414 case 'K':
415 case 'J':
416#ifdef FEAT_VERTSPLIT
417 case 'H':
418 case 'L':
419#endif
420 CHECK_CMDWIN
421 win_totop((int)Prenum,
422 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
423 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
424 break;
425
426/* make all windows the same height */
427 case '=':
428#ifdef FEAT_GUI
429 need_mouse_correct = TRUE;
430#endif
431 win_equal(NULL, FALSE, 'b');
432 break;
433
434/* increase current window height */
435 case '+':
436#ifdef FEAT_GUI
437 need_mouse_correct = TRUE;
438#endif
439 win_setheight(curwin->w_height + (int)Prenum1);
440 break;
441
442/* decrease current window height */
443 case '-':
444#ifdef FEAT_GUI
445 need_mouse_correct = TRUE;
446#endif
447 win_setheight(curwin->w_height - (int)Prenum1);
448 break;
449
450/* set current window height */
451 case Ctrl__:
452 case '_':
453#ifdef FEAT_GUI
454 need_mouse_correct = TRUE;
455#endif
456 win_setheight(Prenum ? (int)Prenum : 9999);
457 break;
458
459#ifdef FEAT_VERTSPLIT
460/* increase current window width */
461 case '>':
462#ifdef FEAT_GUI
463 need_mouse_correct = TRUE;
464#endif
465 win_setwidth(curwin->w_width + (int)Prenum1);
466 break;
467
468/* decrease current window width */
469 case '<':
470#ifdef FEAT_GUI
471 need_mouse_correct = TRUE;
472#endif
473 win_setwidth(curwin->w_width - (int)Prenum1);
474 break;
475
476/* set current window width */
477 case '|':
478#ifdef FEAT_GUI
479 need_mouse_correct = TRUE;
480#endif
481 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
482 break;
483#endif
484
485/* jump to tag and split window if tag exists (in preview window) */
486#if defined(FEAT_QUICKFIX)
487 case '}':
488 CHECK_CMDWIN
489 if (Prenum)
490 g_do_tagpreview = Prenum;
491 else
492 g_do_tagpreview = p_pvh;
493 /*FALLTHROUGH*/
494#endif
495 case ']':
496 case Ctrl_RSB:
497 CHECK_CMDWIN
498#ifdef FEAT_VISUAL
499 reset_VIsual_and_resel(); /* stop Visual mode */
500#endif
501 if (Prenum)
502 postponed_split = Prenum;
503 else
504 postponed_split = -1;
505
506 /* Execute the command right here, required when
507 * "wincmd ]" was used in a function. */
508 do_nv_ident(Ctrl_RSB, NUL);
509 break;
510
511#ifdef FEAT_SEARCHPATH
512/* edit file name under cursor in a new window */
513 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000514 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000516wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000518
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000519 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 if (ptr != NULL)
521 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000522# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000524# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 setpcmark();
526 if (win_split(0, 0) == OK)
527 {
528# ifdef FEAT_SCROLLBIND
529 curwin->w_p_scb = FALSE;
530# endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000531 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
532 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000533 if (nchar == 'F' && lnum >= 0)
534 {
535 curwin->w_cursor.lnum = lnum;
536 check_cursor_lnum();
537 beginline(BL_SOL | BL_FIX);
538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 }
540 vim_free(ptr);
541 }
542 break;
543#endif
544
545#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000546/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 * new window -- webb
548 */
549 case 'i': /* Go to any match */
550 case Ctrl_I:
551 type = FIND_ANY;
552 /* FALLTHROUGH */
553 case 'd': /* Go to definition, using 'define' */
554 case Ctrl_D:
555 CHECK_CMDWIN
556 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
557 break;
558 find_pattern_in_path(ptr, 0, len, TRUE,
559 Prenum == 0 ? TRUE : FALSE, type,
560 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
561 curwin->w_set_curswant = TRUE;
562 break;
563#endif
564
Bram Moolenaar05159a02005-02-26 23:04:13 +0000565 case K_KENTER:
566 case CAR:
567#if defined(FEAT_QUICKFIX)
568 /*
569 * In a quickfix window a <CR> jumps to the error under the
570 * cursor in a new window.
571 */
572 if (bt_quickfix(curbuf))
573 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000574 sprintf((char *)cbuf, "split +%ld%s",
575 (long)curwin->w_cursor.lnum,
576 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000577 do_cmdline_cmd(cbuf);
578 }
579#endif
580 break;
581
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583/* CTRL-W g extended commands */
584 case 'g':
585 case Ctrl_G:
586 CHECK_CMDWIN
587#ifdef USE_ON_FLY_SCROLL
588 dont_scroll = TRUE; /* disallow scrolling here */
589#endif
590 ++no_mapping;
591 ++allow_keys; /* no mapping for xchar, but allow key codes */
592 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000593 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 --no_mapping;
596 --allow_keys;
597#ifdef FEAT_CMDL_INFO
598 (void)add_to_showcmd(xchar);
599#endif
600 switch (xchar)
601 {
602#if defined(FEAT_QUICKFIX)
603 case '}':
604 xchar = Ctrl_RSB;
605 if (Prenum)
606 g_do_tagpreview = Prenum;
607 else
608 g_do_tagpreview = p_pvh;
609 /*FALLTHROUGH*/
610#endif
611 case ']':
612 case Ctrl_RSB:
613#ifdef FEAT_VISUAL
614 reset_VIsual_and_resel(); /* stop Visual mode */
615#endif
616 if (Prenum)
617 postponed_split = Prenum;
618 else
619 postponed_split = -1;
620
621 /* Execute the command right here, required when
622 * "wincmd g}" was used in a function. */
623 do_nv_ident('g', xchar);
624 break;
625
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000626#ifdef FEAT_SEARCHPATH
627 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000628 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100629 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000630 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000631 goto wingotofile;
632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 default:
634 beep_flush();
635 break;
636 }
637 break;
638
639 default: beep_flush();
640 break;
641 }
642}
643
644/*
645 * split the current window, implements CTRL-W s and :split
646 *
647 * "size" is the height or width for the new window, 0 to use half of current
648 * height or width.
649 *
650 * "flags":
651 * WSP_ROOM: require enough room for new window
652 * WSP_VERT: vertical split.
653 * WSP_TOP: open window at the top-left of the shell (help window).
654 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
655 * WSP_HELP: creating the help window, keep layout snapshot
656 *
657 * return FAIL for failure, OK otherwise
658 */
659 int
660win_split(size, flags)
661 int size;
662 int flags;
663{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000664 /* When the ":tab" modifier was used open a new tab page instead. */
665 if (may_open_tabpage() == OK)
666 return OK;
667
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 /* Add flags from ":vertical", ":topleft" and ":botright". */
669 flags |= cmdmod.split;
670 if ((flags & WSP_TOP) && (flags & WSP_BOT))
671 {
672 EMSG(_("E442: Can't split topleft and botright at the same time"));
673 return FAIL;
674 }
675
676 /* When creating the help window make a snapshot of the window layout.
677 * Otherwise clear the snapshot, it's now invalid. */
678 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000679 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000681 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682
683 return win_split_ins(size, flags, NULL, 0);
684}
685
686/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000687 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 * When "newwin" is not NULL: insert this window at the far
689 * top/left/right/bottom.
690 * return FAIL for failure, OK otherwise
691 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000692 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693win_split_ins(size, flags, newwin, dir)
694 int size;
695 int flags;
696 win_T *newwin;
697 int dir;
698{
699 win_T *wp = newwin;
700 win_T *oldwin;
701 int new_size = size;
702 int i;
703 int need_status = 0;
704 int do_equal = FALSE;
705 int needed;
706 int available;
707 int oldwin_height = 0;
708 int layout;
709 frame_T *frp, *curfrp;
710 int before;
711
712 if (flags & WSP_TOP)
713 oldwin = firstwin;
714 else if (flags & WSP_BOT)
715 oldwin = lastwin;
716 else
717 oldwin = curwin;
718
719 /* add a status line when p_ls == 1 and splitting the first window */
720 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
721 {
722 if (oldwin->w_height <= p_wmh && newwin == NULL)
723 {
724 EMSG(_(e_noroom));
725 return FAIL;
726 }
727 need_status = STATUS_HEIGHT;
728 }
729
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000730#ifdef FEAT_GUI
731 /* May be needed for the scrollbars that are going to change. */
732 if (gui.in_use)
733 out_flush();
734#endif
735
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#ifdef FEAT_VERTSPLIT
737 if (flags & WSP_VERT)
738 {
739 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
741 /*
742 * Check if we are able to split the current window and compute its
743 * width.
744 */
745 needed = p_wmw + 1;
746 if (flags & WSP_ROOM)
747 needed += p_wiw - p_wmw;
748 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
749 {
750 available = topframe->fr_width;
751 needed += frame_minwidth(topframe, NULL);
752 }
753 else
754 available = oldwin->w_width;
755 if (available < needed && newwin == NULL)
756 {
757 EMSG(_(e_noroom));
758 return FAIL;
759 }
760 if (new_size == 0)
761 new_size = oldwin->w_width / 2;
762 if (new_size > oldwin->w_width - p_wmw - 1)
763 new_size = oldwin->w_width - p_wmw - 1;
764 if (new_size < p_wmw)
765 new_size = p_wmw;
766
767 /* if it doesn't fit in the current window, need win_equal() */
768 if (oldwin->w_width - new_size - 1 < p_wmw)
769 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000770
771 /* We don't like to take lines for the new window from a
772 * 'winfixwidth' window. Take them from a window to the left or right
773 * instead, if possible. */
774 if (oldwin->w_p_wfw)
775 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000776
777 /* Only make all windows the same width if one of them (except oldwin)
778 * is wider than one of the split windows. */
779 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
780 && oldwin->w_frame->fr_parent != NULL)
781 {
782 frp = oldwin->w_frame->fr_parent->fr_child;
783 while (frp != NULL)
784 {
785 if (frp->fr_win != oldwin && frp->fr_win != NULL
786 && (frp->fr_win->w_width > new_size
787 || frp->fr_win->w_width > oldwin->w_width
788 - new_size - STATUS_HEIGHT))
789 {
790 do_equal = TRUE;
791 break;
792 }
793 frp = frp->fr_next;
794 }
795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 }
797 else
798#endif
799 {
800 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
802 /*
803 * Check if we are able to split the current window and compute its
804 * height.
805 */
806 needed = p_wmh + STATUS_HEIGHT + need_status;
807 if (flags & WSP_ROOM)
808 needed += p_wh - p_wmh;
809 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
810 {
811 available = topframe->fr_height;
812 needed += frame_minheight(topframe, NULL);
813 }
814 else
815 {
816 available = oldwin->w_height;
817 needed += p_wmh;
818 }
819 if (available < needed && newwin == NULL)
820 {
821 EMSG(_(e_noroom));
822 return FAIL;
823 }
824 oldwin_height = oldwin->w_height;
825 if (need_status)
826 {
827 oldwin->w_status_height = STATUS_HEIGHT;
828 oldwin_height -= STATUS_HEIGHT;
829 }
830 if (new_size == 0)
831 new_size = oldwin_height / 2;
832
833 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
834 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
835 if (new_size < p_wmh)
836 new_size = p_wmh;
837
838 /* if it doesn't fit in the current window, need win_equal() */
839 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
840 do_equal = TRUE;
841
842 /* We don't like to take lines for the new window from a
843 * 'winfixheight' window. Take them from a window above or below
844 * instead, if possible. */
845 if (oldwin->w_p_wfh)
846 {
847 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
848 oldwin);
849 oldwin_height = oldwin->w_height;
850 if (need_status)
851 oldwin_height -= STATUS_HEIGHT;
852 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000853
854 /* Only make all windows the same height if one of them (except oldwin)
855 * is higher than one of the split windows. */
856 if (!do_equal && p_ea && size == 0
857#ifdef FEAT_VERTSPLIT
858 && *p_ead != 'h'
859#endif
860 && oldwin->w_frame->fr_parent != NULL)
861 {
862 frp = oldwin->w_frame->fr_parent->fr_child;
863 while (frp != NULL)
864 {
865 if (frp->fr_win != oldwin && frp->fr_win != NULL
866 && (frp->fr_win->w_height > new_size
867 || frp->fr_win->w_height > oldwin_height - new_size
868 - STATUS_HEIGHT))
869 {
870 do_equal = TRUE;
871 break;
872 }
873 frp = frp->fr_next;
874 }
875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 }
877
878 /*
879 * allocate new window structure and link it in the window list
880 */
881 if ((flags & WSP_TOP) == 0
882 && ((flags & WSP_BOT)
883 || (flags & WSP_BELOW)
884 || (!(flags & WSP_ABOVE)
885 && (
886#ifdef FEAT_VERTSPLIT
887 (flags & WSP_VERT) ? p_spr :
888#endif
889 p_sb))))
890 {
891 /* new window below/right of current one */
892 if (newwin == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000893 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 else
895 win_append(oldwin, wp);
896 }
897 else
898 {
899 if (newwin == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000900 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 else
902 win_append(oldwin->w_prev, wp);
903 }
904
905 if (newwin == NULL)
906 {
907 if (wp == NULL)
908 return FAIL;
909
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000910 new_frame(wp);
911 if (wp->w_frame == NULL)
912 {
913 win_free(wp, NULL);
914 return FAIL;
915 }
916
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000917 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000918 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
920
921 /*
922 * Reorganise the tree of frames to insert the new window.
923 */
924 if (flags & (WSP_TOP | WSP_BOT))
925 {
926#ifdef FEAT_VERTSPLIT
927 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
928 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
929#else
930 if (topframe->fr_layout == FR_COL)
931#endif
932 {
933 curfrp = topframe->fr_child;
934 if (flags & WSP_BOT)
935 while (curfrp->fr_next != NULL)
936 curfrp = curfrp->fr_next;
937 }
938 else
939 curfrp = topframe;
940 before = (flags & WSP_TOP);
941 }
942 else
943 {
944 curfrp = oldwin->w_frame;
945 if (flags & WSP_BELOW)
946 before = FALSE;
947 else if (flags & WSP_ABOVE)
948 before = TRUE;
949 else
950#ifdef FEAT_VERTSPLIT
951 if (flags & WSP_VERT)
952 before = !p_spr;
953 else
954#endif
955 before = !p_sb;
956 }
957 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
958 {
959 /* Need to create a new frame in the tree to make a branch. */
960 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
961 *frp = *curfrp;
962 curfrp->fr_layout = layout;
963 frp->fr_parent = curfrp;
964 frp->fr_next = NULL;
965 frp->fr_prev = NULL;
966 curfrp->fr_child = frp;
967 curfrp->fr_win = NULL;
968 curfrp = frp;
969 if (frp->fr_win != NULL)
970 oldwin->w_frame = frp;
971 else
972 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
973 frp->fr_parent = curfrp;
974 }
975
976 if (newwin == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000977 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 else
979 frp = newwin->w_frame;
980 frp->fr_parent = curfrp->fr_parent;
981
982 /* Insert the new frame at the right place in the frame list. */
983 if (before)
984 frame_insert(curfrp, frp);
985 else
986 frame_append(curfrp, frp);
987
988#ifdef FEAT_VERTSPLIT
989 if (flags & WSP_VERT)
990 {
991 wp->w_p_scr = curwin->w_p_scr;
992 if (need_status)
993 {
994 --oldwin->w_height;
995 oldwin->w_status_height = need_status;
996 }
997 if (flags & (WSP_TOP | WSP_BOT))
998 {
999 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001000 wp->w_winrow = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 wp->w_height = curfrp->fr_height - (p_ls > 0);
1002 wp->w_status_height = (p_ls > 0);
1003 }
1004 else
1005 {
1006 /* height and row of new window is same as current window */
1007 wp->w_winrow = oldwin->w_winrow;
1008 wp->w_height = oldwin->w_height;
1009 wp->w_status_height = oldwin->w_status_height;
1010 }
1011 frp->fr_height = curfrp->fr_height;
1012
1013 /* "new_size" of the current window goes to the new window, use
1014 * one column for the vertical separator */
1015 wp->w_width = new_size;
1016 if (before)
1017 wp->w_vsep_width = 1;
1018 else
1019 {
1020 wp->w_vsep_width = oldwin->w_vsep_width;
1021 oldwin->w_vsep_width = 1;
1022 }
1023 if (flags & (WSP_TOP | WSP_BOT))
1024 {
1025 if (flags & WSP_BOT)
1026 frame_add_vsep(curfrp);
1027 /* Set width of neighbor frame */
1028 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001029 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1030 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
1032 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001033 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (before) /* new window left of current one */
1035 {
1036 wp->w_wincol = oldwin->w_wincol;
1037 oldwin->w_wincol += new_size + 1;
1038 }
1039 else /* new window right of current one */
1040 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1041 frame_fix_width(oldwin);
1042 frame_fix_width(wp);
1043 }
1044 else
1045#endif
1046 {
1047 /* width and column of new window is same as current window */
1048#ifdef FEAT_VERTSPLIT
1049 if (flags & (WSP_TOP | WSP_BOT))
1050 {
1051 wp->w_wincol = 0;
1052 wp->w_width = Columns;
1053 wp->w_vsep_width = 0;
1054 }
1055 else
1056 {
1057 wp->w_wincol = oldwin->w_wincol;
1058 wp->w_width = oldwin->w_width;
1059 wp->w_vsep_width = oldwin->w_vsep_width;
1060 }
1061 frp->fr_width = curfrp->fr_width;
1062#endif
1063
1064 /* "new_size" of the current window goes to the new window, use
1065 * one row for the status line */
1066 win_new_height(wp, new_size);
1067 if (flags & (WSP_TOP | WSP_BOT))
1068 frame_new_height(curfrp, curfrp->fr_height
1069 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1070 else
1071 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1072 if (before) /* new window above current one */
1073 {
1074 wp->w_winrow = oldwin->w_winrow;
1075 wp->w_status_height = STATUS_HEIGHT;
1076 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1077 }
1078 else /* new window below current one */
1079 {
1080 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1081 wp->w_status_height = oldwin->w_status_height;
1082 oldwin->w_status_height = STATUS_HEIGHT;
1083 }
1084#ifdef FEAT_VERTSPLIT
1085 if (flags & WSP_BOT)
1086 frame_add_statusline(curfrp);
1087#endif
1088 frame_fix_height(wp);
1089 frame_fix_height(oldwin);
1090 }
1091
1092 if (flags & (WSP_TOP | WSP_BOT))
1093 (void)win_comp_pos();
1094
1095 /*
1096 * Both windows need redrawing
1097 */
1098 redraw_win_later(wp, NOT_VALID);
1099 wp->w_redr_status = TRUE;
1100 redraw_win_later(oldwin, NOT_VALID);
1101 oldwin->w_redr_status = TRUE;
1102
1103 if (need_status)
1104 {
1105 msg_row = Rows - 1;
1106 msg_col = sc_col;
1107 msg_clr_eos_force(); /* Old command/ruler may still be there */
1108 comp_col();
1109 msg_row = Rows - 1;
1110 msg_col = 0; /* put position back at start of line */
1111 }
1112
1113 /*
1114 * make the new window the current window and redraw
1115 */
1116 if (do_equal || dir != 0)
1117 win_equal(wp, TRUE,
1118#ifdef FEAT_VERTSPLIT
1119 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1120 : dir == 'h' ? 'b' :
1121#endif
1122 'v');
1123
1124 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1125 * size was given. */
1126#ifdef FEAT_VERTSPLIT
1127 if (flags & WSP_VERT)
1128 {
1129 i = p_wiw;
1130 if (size != 0)
1131 p_wiw = size;
1132
1133# ifdef FEAT_GUI
1134 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1135 if (gui.in_use)
1136 gui_init_which_components(NULL);
1137# endif
1138 }
1139 else
1140#endif
1141 {
1142 i = p_wh;
1143 if (size != 0)
1144 p_wh = size;
1145 }
1146 win_enter(wp, FALSE);
1147#ifdef FEAT_VERTSPLIT
1148 if (flags & WSP_VERT)
1149 p_wiw = i;
1150 else
1151#endif
1152 p_wh = i;
1153
1154 return OK;
1155}
1156
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001157
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001158/*
1159 * Initialize window "newp" from window "oldp".
1160 * Used when splitting a window and when creating a new tab page.
1161 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001162 * WSP_NEWLOC may be specified in flags to prevent the location list from
1163 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001164 */
1165 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001166win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001167 win_T *newp;
1168 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001169 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001170{
1171 int i;
1172
1173 newp->w_buffer = oldp->w_buffer;
1174 oldp->w_buffer->b_nwindows++;
1175 newp->w_cursor = oldp->w_cursor;
1176 newp->w_valid = 0;
1177 newp->w_curswant = oldp->w_curswant;
1178 newp->w_set_curswant = oldp->w_set_curswant;
1179 newp->w_topline = oldp->w_topline;
1180#ifdef FEAT_DIFF
1181 newp->w_topfill = oldp->w_topfill;
1182#endif
1183 newp->w_leftcol = oldp->w_leftcol;
1184 newp->w_pcmark = oldp->w_pcmark;
1185 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1186 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001187 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001188 newp->w_fraction = oldp->w_fraction;
1189 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1190#ifdef FEAT_JUMPLIST
1191 copy_jumplist(oldp, newp);
1192#endif
1193#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001194 if (flags & WSP_NEWLOC)
1195 {
1196 /* Don't copy the location list. */
1197 newp->w_llist = NULL;
1198 newp->w_llist_ref = NULL;
1199 }
1200 else
1201 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001202#endif
1203 if (oldp->w_localdir != NULL)
1204 newp->w_localdir = vim_strsave(oldp->w_localdir);
1205
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001206 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001207 for (i = 0; i < oldp->w_tagstacklen; i++)
1208 {
1209 newp->w_tagstack[i] = oldp->w_tagstack[i];
1210 if (newp->w_tagstack[i].tagname != NULL)
1211 newp->w_tagstack[i].tagname =
1212 vim_strsave(newp->w_tagstack[i].tagname);
1213 }
1214 newp->w_tagstackidx = oldp->w_tagstackidx;
1215 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001216# ifdef FEAT_FOLDING
1217 copyFoldingState(oldp, newp);
1218# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001219
1220 win_init_some(newp, oldp);
1221}
1222
1223/*
1224 * Initialize window "newp" from window"old".
1225 * Only the essential things are copied.
1226 */
1227 static void
1228win_init_some(newp, oldp)
1229 win_T *newp;
1230 win_T *oldp;
1231{
1232 /* Use the same argument list. */
1233 newp->w_alist = oldp->w_alist;
1234 ++newp->w_alist->al_refcount;
1235 newp->w_arg_idx = oldp->w_arg_idx;
1236
1237 /* copy options from existing window */
1238 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001239}
1240
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241#endif /* FEAT_WINDOWS */
1242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243#if defined(FEAT_WINDOWS) || defined(PROTO)
1244/*
1245 * Check if "win" is a pointer to an existing window.
1246 */
1247 int
1248win_valid(win)
1249 win_T *win;
1250{
1251 win_T *wp;
1252
1253 if (win == NULL)
1254 return FALSE;
1255 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1256 if (wp == win)
1257 return TRUE;
1258 return FALSE;
1259}
1260
1261/*
1262 * Return the number of windows.
1263 */
1264 int
1265win_count()
1266{
1267 win_T *wp;
1268 int count = 0;
1269
1270 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1271 ++count;
1272 return count;
1273}
1274
1275/*
1276 * Make "count" windows on the screen.
1277 * Return actual number of windows on the screen.
1278 * Must be called when there is just one window, filling the whole screen
1279 * (excluding the command line).
1280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 int
1282make_windows(count, vertical)
1283 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001284 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285{
1286 int maxcount;
1287 int todo;
1288
1289#ifdef FEAT_VERTSPLIT
1290 if (vertical)
1291 {
1292 /* Each windows needs at least 'winminwidth' lines and a separator
1293 * column. */
1294 maxcount = (curwin->w_width + curwin->w_vsep_width
1295 - (p_wiw - p_wmw)) / (p_wmw + 1);
1296 }
1297 else
1298#endif
1299 {
1300 /* Each window needs at least 'winminheight' lines and a status line. */
1301 maxcount = (curwin->w_height + curwin->w_status_height
1302 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1303 }
1304
1305 if (maxcount < 2)
1306 maxcount = 2;
1307 if (count > maxcount)
1308 count = maxcount;
1309
1310 /*
1311 * add status line now, otherwise first window will be too big
1312 */
1313 if (count > 1)
1314 last_status(TRUE);
1315
1316#ifdef FEAT_AUTOCMD
1317 /*
1318 * Don't execute autocommands while creating the windows. Must do that
1319 * when putting the buffers in the windows.
1320 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001321 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322#endif
1323
1324 /* todo is number of windows left to create */
1325 for (todo = count - 1; todo > 0; --todo)
1326#ifdef FEAT_VERTSPLIT
1327 if (vertical)
1328 {
1329 if (win_split(curwin->w_width - (curwin->w_width - todo)
1330 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1331 break;
1332 }
1333 else
1334#endif
1335 {
1336 if (win_split(curwin->w_height - (curwin->w_height - todo
1337 * STATUS_HEIGHT) / (todo + 1)
1338 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1339 break;
1340 }
1341
1342#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001343 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344#endif
1345
1346 /* return actual number of windows */
1347 return (count - todo);
1348}
1349
1350/*
1351 * Exchange current and next window
1352 */
1353 static void
1354win_exchange(Prenum)
1355 long Prenum;
1356{
1357 frame_T *frp;
1358 frame_T *frp2;
1359 win_T *wp;
1360 win_T *wp2;
1361 int temp;
1362
1363 if (lastwin == firstwin) /* just one window */
1364 {
1365 beep_flush();
1366 return;
1367 }
1368
1369#ifdef FEAT_GUI
1370 need_mouse_correct = TRUE;
1371#endif
1372
1373 /*
1374 * find window to exchange with
1375 */
1376 if (Prenum)
1377 {
1378 frp = curwin->w_frame->fr_parent->fr_child;
1379 while (frp != NULL && --Prenum > 0)
1380 frp = frp->fr_next;
1381 }
1382 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1383 frp = curwin->w_frame->fr_next;
1384 else /* Swap last window in row/col with previous */
1385 frp = curwin->w_frame->fr_prev;
1386
1387 /* We can only exchange a window with another window, not with a frame
1388 * containing windows. */
1389 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1390 return;
1391 wp = frp->fr_win;
1392
1393/*
1394 * 1. remove curwin from the list. Remember after which window it was in wp2
1395 * 2. insert curwin before wp in the list
1396 * if wp != wp2
1397 * 3. remove wp from the list
1398 * 4. insert wp after wp2
1399 * 5. exchange the status line height and vsep width.
1400 */
1401 wp2 = curwin->w_prev;
1402 frp2 = curwin->w_frame->fr_prev;
1403 if (wp->w_prev != curwin)
1404 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001405 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 frame_remove(curwin->w_frame);
1407 win_append(wp->w_prev, curwin);
1408 frame_insert(frp, curwin->w_frame);
1409 }
1410 if (wp != wp2)
1411 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001412 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 frame_remove(wp->w_frame);
1414 win_append(wp2, wp);
1415 if (frp2 == NULL)
1416 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1417 else
1418 frame_append(frp2, wp->w_frame);
1419 }
1420 temp = curwin->w_status_height;
1421 curwin->w_status_height = wp->w_status_height;
1422 wp->w_status_height = temp;
1423#ifdef FEAT_VERTSPLIT
1424 temp = curwin->w_vsep_width;
1425 curwin->w_vsep_width = wp->w_vsep_width;
1426 wp->w_vsep_width = temp;
1427
1428 /* If the windows are not in the same frame, exchange the sizes to avoid
1429 * messing up the window layout. Otherwise fix the frame sizes. */
1430 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1431 {
1432 temp = curwin->w_height;
1433 curwin->w_height = wp->w_height;
1434 wp->w_height = temp;
1435 temp = curwin->w_width;
1436 curwin->w_width = wp->w_width;
1437 wp->w_width = temp;
1438 }
1439 else
1440 {
1441 frame_fix_height(curwin);
1442 frame_fix_height(wp);
1443 frame_fix_width(curwin);
1444 frame_fix_width(wp);
1445 }
1446#endif
1447
1448 (void)win_comp_pos(); /* recompute window positions */
1449
1450 win_enter(wp, TRUE);
1451 redraw_later(CLEAR);
1452}
1453
1454/*
1455 * rotate windows: if upwards TRUE the second window becomes the first one
1456 * if upwards FALSE the first window becomes the second one
1457 */
1458 static void
1459win_rotate(upwards, count)
1460 int upwards;
1461 int count;
1462{
1463 win_T *wp1;
1464 win_T *wp2;
1465 frame_T *frp;
1466 int n;
1467
1468 if (firstwin == lastwin) /* nothing to do */
1469 {
1470 beep_flush();
1471 return;
1472 }
1473
1474#ifdef FEAT_GUI
1475 need_mouse_correct = TRUE;
1476#endif
1477
1478#ifdef FEAT_VERTSPLIT
1479 /* Check if all frames in this row/col have one window. */
1480 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1481 frp = frp->fr_next)
1482 if (frp->fr_win == NULL)
1483 {
1484 EMSG(_("E443: Cannot rotate when another window is split"));
1485 return;
1486 }
1487#endif
1488
1489 while (count--)
1490 {
1491 if (upwards) /* first window becomes last window */
1492 {
1493 /* remove first window/frame from the list */
1494 frp = curwin->w_frame->fr_parent->fr_child;
1495 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001496 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 frame_remove(frp);
1498
1499 /* find last frame and append removed window/frame after it */
1500 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1501 ;
1502 win_append(frp->fr_win, wp1);
1503 frame_append(frp, wp1->w_frame);
1504
1505 wp2 = frp->fr_win; /* previously last window */
1506 }
1507 else /* last window becomes first window */
1508 {
1509 /* find last window/frame in the list and remove it */
1510 for (frp = curwin->w_frame; frp->fr_next != NULL;
1511 frp = frp->fr_next)
1512 ;
1513 wp1 = frp->fr_win;
1514 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001515 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 frame_remove(frp);
1517
1518 /* append the removed window/frame before the first in the list */
1519 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1520 frame_insert(frp->fr_parent->fr_child, frp);
1521 }
1522
1523 /* exchange status height and vsep width of old and new last window */
1524 n = wp2->w_status_height;
1525 wp2->w_status_height = wp1->w_status_height;
1526 wp1->w_status_height = n;
1527 frame_fix_height(wp1);
1528 frame_fix_height(wp2);
1529#ifdef FEAT_VERTSPLIT
1530 n = wp2->w_vsep_width;
1531 wp2->w_vsep_width = wp1->w_vsep_width;
1532 wp1->w_vsep_width = n;
1533 frame_fix_width(wp1);
1534 frame_fix_width(wp2);
1535#endif
1536
1537 /* recompute w_winrow and w_wincol for all windows */
1538 (void)win_comp_pos();
1539 }
1540
1541 redraw_later(CLEAR);
1542}
1543
1544/*
1545 * Move the current window to the very top/bottom/left/right of the screen.
1546 */
1547 static void
1548win_totop(size, flags)
1549 int size;
1550 int flags;
1551{
1552 int dir;
1553 int height = curwin->w_height;
1554
1555 if (lastwin == firstwin)
1556 {
1557 beep_flush();
1558 return;
1559 }
1560
1561 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001562 (void)winframe_remove(curwin, &dir, NULL);
1563 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 last_status(FALSE); /* may need to remove last status line */
1565 (void)win_comp_pos(); /* recompute window positions */
1566
1567 /* Split a window on the desired side and put the window there. */
1568 (void)win_split_ins(size, flags, curwin, dir);
1569 if (!(flags & WSP_VERT))
1570 {
1571 win_setheight(height);
1572 if (p_ea)
1573 win_equal(curwin, TRUE, 'v');
1574 }
1575
1576#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1577 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1578 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001579 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581}
1582
1583/*
1584 * Move window "win1" to below/right of "win2" and make "win1" the current
1585 * window. Only works within the same frame!
1586 */
1587 void
1588win_move_after(win1, win2)
1589 win_T *win1, *win2;
1590{
1591 int height;
1592
1593 /* check if the arguments are reasonable */
1594 if (win1 == win2)
1595 return;
1596
1597 /* check if there is something to do */
1598 if (win2->w_next != win1)
1599 {
1600 /* may need move the status line/vertical separator of the last window
1601 * */
1602 if (win1 == lastwin)
1603 {
1604 height = win1->w_prev->w_status_height;
1605 win1->w_prev->w_status_height = win1->w_status_height;
1606 win1->w_status_height = height;
1607#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001608 if (win1->w_prev->w_vsep_width == 1)
1609 {
1610 /* Remove the vertical separator from the last-but-one window,
1611 * add it to the last window. Adjust the frame widths. */
1612 win1->w_prev->w_vsep_width = 0;
1613 win1->w_prev->w_frame->fr_width -= 1;
1614 win1->w_vsep_width = 1;
1615 win1->w_frame->fr_width += 1;
1616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617#endif
1618 }
1619 else if (win2 == lastwin)
1620 {
1621 height = win1->w_status_height;
1622 win1->w_status_height = win2->w_status_height;
1623 win2->w_status_height = height;
1624#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001625 if (win1->w_vsep_width == 1)
1626 {
1627 /* Remove the vertical separator from win1, add it to the last
1628 * window, win2. Adjust the frame widths. */
1629 win2->w_vsep_width = 1;
1630 win2->w_frame->fr_width += 1;
1631 win1->w_vsep_width = 0;
1632 win1->w_frame->fr_width -= 1;
1633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634#endif
1635 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001636 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 frame_remove(win1->w_frame);
1638 win_append(win2, win1);
1639 frame_append(win2->w_frame, win1->w_frame);
1640
1641 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1642 redraw_later(NOT_VALID);
1643 }
1644 win_enter(win1, FALSE);
1645}
1646
1647/*
1648 * Make all windows the same height.
1649 * 'next_curwin' will soon be the current window, make sure it has enough
1650 * rows.
1651 */
1652 void
1653win_equal(next_curwin, current, dir)
1654 win_T *next_curwin; /* pointer to current window to be or NULL */
1655 int current; /* do only frame with current window */
1656 int dir; /* 'v' for vertically, 'h' for horizontally,
1657 'b' for both, 0 for using p_ead */
1658{
1659 if (dir == 0)
1660#ifdef FEAT_VERTSPLIT
1661 dir = *p_ead;
1662#else
1663 dir = 'b';
1664#endif
1665 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001666 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001667 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668}
1669
1670/*
1671 * Set a frame to a new position and height, spreading the available room
1672 * equally over contained frames.
1673 * The window "next_curwin" (if not NULL) should at least get the size from
1674 * 'winheight' and 'winwidth' if possible.
1675 */
1676 static void
1677win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1678 win_T *next_curwin; /* pointer to current window to be or NULL */
1679 int current; /* do only frame with current window */
1680 frame_T *topfr; /* frame to set size off */
1681 int dir; /* 'v', 'h' or 'b', see win_equal() */
1682 int col; /* horizontal position for frame */
1683 int row; /* vertical position for frame */
1684 int width; /* new width of frame */
1685 int height; /* new height of frame */
1686{
1687 int n, m;
1688 int extra_sep = 0;
1689 int wincount, totwincount = 0;
1690 frame_T *fr;
1691 int next_curwin_size = 0;
1692 int room = 0;
1693 int new_size;
1694 int has_next_curwin = 0;
1695 int hnc;
1696
1697 if (topfr->fr_layout == FR_LEAF)
1698 {
1699 /* Set the width/height of this frame.
1700 * Redraw when size or position changes */
1701 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1702#ifdef FEAT_VERTSPLIT
1703 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1704#endif
1705 )
1706 {
1707 topfr->fr_win->w_winrow = row;
1708 frame_new_height(topfr, height, FALSE, FALSE);
1709#ifdef FEAT_VERTSPLIT
1710 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001711 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712#endif
1713 redraw_all_later(CLEAR);
1714 }
1715 }
1716#ifdef FEAT_VERTSPLIT
1717 else if (topfr->fr_layout == FR_ROW)
1718 {
1719 topfr->fr_width = width;
1720 topfr->fr_height = height;
1721
1722 if (dir != 'v') /* equalize frame widths */
1723 {
1724 /* Compute the maximum number of windows horizontally in this
1725 * frame. */
1726 n = frame_minwidth(topfr, NOWIN);
1727 /* add one for the rightmost window, it doesn't have a separator */
1728 if (col + width == Columns)
1729 extra_sep = 1;
1730 else
1731 extra_sep = 0;
1732 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001733 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001735 /*
1736 * Compute width for "next_curwin" window and room available for
1737 * other windows.
1738 * "m" is the minimal width when counting p_wiw for "next_curwin".
1739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 m = frame_minwidth(topfr, next_curwin);
1741 room = width - m;
1742 if (room < 0)
1743 {
1744 next_curwin_size = p_wiw + room;
1745 room = 0;
1746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 else
1748 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001749 next_curwin_size = -1;
1750 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1751 {
1752 /* If 'winfixwidth' set keep the window width if
1753 * possible.
1754 * Watch out for this window being the next_curwin. */
1755 if (frame_fixed_width(fr))
1756 {
1757 n = frame_minwidth(fr, NOWIN);
1758 new_size = fr->fr_width;
1759 if (frame_has_win(fr, next_curwin))
1760 {
1761 room += p_wiw - p_wmw;
1762 next_curwin_size = 0;
1763 if (new_size < p_wiw)
1764 new_size = p_wiw;
1765 }
1766 else
1767 /* These windows don't use up room. */
1768 totwincount -= (n + (fr->fr_next == NULL
1769 ? extra_sep : 0)) / (p_wmw + 1);
1770 room -= new_size - n;
1771 if (room < 0)
1772 {
1773 new_size += room;
1774 room = 0;
1775 }
1776 fr->fr_newwidth = new_size;
1777 }
1778 }
1779 if (next_curwin_size == -1)
1780 {
1781 if (!has_next_curwin)
1782 next_curwin_size = 0;
1783 else if (totwincount > 1
1784 && (room + (totwincount - 2))
1785 / (totwincount - 1) > p_wiw)
1786 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001787 /* Can make all windows wider than 'winwidth', spread
1788 * the room equally. */
1789 next_curwin_size = (room + p_wiw
1790 + (totwincount - 1) * p_wmw
1791 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001792 room -= next_curwin_size - p_wiw;
1793 }
1794 else
1795 next_curwin_size = p_wiw;
1796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001798
1799 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 --totwincount; /* don't count curwin */
1801 }
1802
1803 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1804 {
1805 n = m = 0;
1806 wincount = 1;
1807 if (fr->fr_next == NULL)
1808 /* last frame gets all that remains (avoid roundoff error) */
1809 new_size = width;
1810 else if (dir == 'v')
1811 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001812 else if (frame_fixed_width(fr))
1813 {
1814 new_size = fr->fr_newwidth;
1815 wincount = 0; /* doesn't count as a sizeable window */
1816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 else
1818 {
1819 /* Compute the maximum number of windows horiz. in "fr". */
1820 n = frame_minwidth(fr, NOWIN);
1821 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1822 / (p_wmw + 1);
1823 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001824 if (has_next_curwin)
1825 hnc = frame_has_win(fr, next_curwin);
1826 else
1827 hnc = FALSE;
1828 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001830 if (totwincount == 0)
1831 new_size = room;
1832 else
1833 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001835 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
1837 next_curwin_size -= p_wiw - (m - n);
1838 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001839 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001841 else
1842 room -= new_size;
1843 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 }
1845
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001846 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 * window, unless equalizing all frames. */
1848 if (!current || dir != 'v' || topfr->fr_parent != NULL
1849 || (new_size != fr->fr_width)
1850 || frame_has_win(fr, next_curwin))
1851 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001852 new_size, height);
1853 col += new_size;
1854 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 totwincount -= wincount;
1856 }
1857 }
1858#endif
1859 else /* topfr->fr_layout == FR_COL */
1860 {
1861#ifdef FEAT_VERTSPLIT
1862 topfr->fr_width = width;
1863#endif
1864 topfr->fr_height = height;
1865
1866 if (dir != 'h') /* equalize frame heights */
1867 {
1868 /* Compute maximum number of windows vertically in this frame. */
1869 n = frame_minheight(topfr, NOWIN);
1870 /* add one for the bottom window if it doesn't have a statusline */
1871 if (row + height == cmdline_row && p_ls == 0)
1872 extra_sep = 1;
1873 else
1874 extra_sep = 0;
1875 totwincount = (n + extra_sep) / (p_wmh + 1);
1876 has_next_curwin = frame_has_win(topfr, next_curwin);
1877
1878 /*
1879 * Compute height for "next_curwin" window and room available for
1880 * other windows.
1881 * "m" is the minimal height when counting p_wh for "next_curwin".
1882 */
1883 m = frame_minheight(topfr, next_curwin);
1884 room = height - m;
1885 if (room < 0)
1886 {
1887 /* The room is less then 'winheight', use all space for the
1888 * current window. */
1889 next_curwin_size = p_wh + room;
1890 room = 0;
1891 }
1892 else
1893 {
1894 next_curwin_size = -1;
1895 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1896 {
1897 /* If 'winfixheight' set keep the window height if
1898 * possible.
1899 * Watch out for this window being the next_curwin. */
1900 if (frame_fixed_height(fr))
1901 {
1902 n = frame_minheight(fr, NOWIN);
1903 new_size = fr->fr_height;
1904 if (frame_has_win(fr, next_curwin))
1905 {
1906 room += p_wh - p_wmh;
1907 next_curwin_size = 0;
1908 if (new_size < p_wh)
1909 new_size = p_wh;
1910 }
1911 else
1912 /* These windows don't use up room. */
1913 totwincount -= (n + (fr->fr_next == NULL
1914 ? extra_sep : 0)) / (p_wmh + 1);
1915 room -= new_size - n;
1916 if (room < 0)
1917 {
1918 new_size += room;
1919 room = 0;
1920 }
1921 fr->fr_newheight = new_size;
1922 }
1923 }
1924 if (next_curwin_size == -1)
1925 {
1926 if (!has_next_curwin)
1927 next_curwin_size = 0;
1928 else if (totwincount > 1
1929 && (room + (totwincount - 2))
1930 / (totwincount - 1) > p_wh)
1931 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001932 /* can make all windows higher than 'winheight',
1933 * spread the room equally. */
1934 next_curwin_size = (room + p_wh
1935 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 + (totwincount - 1)) / totwincount;
1937 room -= next_curwin_size - p_wh;
1938 }
1939 else
1940 next_curwin_size = p_wh;
1941 }
1942 }
1943
1944 if (has_next_curwin)
1945 --totwincount; /* don't count curwin */
1946 }
1947
1948 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1949 {
1950 n = m = 0;
1951 wincount = 1;
1952 if (fr->fr_next == NULL)
1953 /* last frame gets all that remains (avoid roundoff error) */
1954 new_size = height;
1955 else if (dir == 'h')
1956 new_size = fr->fr_height;
1957 else if (frame_fixed_height(fr))
1958 {
1959 new_size = fr->fr_newheight;
1960 wincount = 0; /* doesn't count as a sizeable window */
1961 }
1962 else
1963 {
1964 /* Compute the maximum number of windows vert. in "fr". */
1965 n = frame_minheight(fr, NOWIN);
1966 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1967 / (p_wmh + 1);
1968 m = frame_minheight(fr, next_curwin);
1969 if (has_next_curwin)
1970 hnc = frame_has_win(fr, next_curwin);
1971 else
1972 hnc = FALSE;
1973 if (hnc) /* don't count next_curwin */
1974 --wincount;
1975 if (totwincount == 0)
1976 new_size = room;
1977 else
1978 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1979 / totwincount;
1980 if (hnc) /* add next_curwin size */
1981 {
1982 next_curwin_size -= p_wh - (m - n);
1983 new_size += next_curwin_size;
1984 room -= new_size - next_curwin_size;
1985 }
1986 else
1987 room -= new_size;
1988 new_size += n;
1989 }
1990 /* Skip frame that is full width when splitting or closing a
1991 * window, unless equalizing all frames. */
1992 if (!current || dir != 'h' || topfr->fr_parent != NULL
1993 || (new_size != fr->fr_height)
1994 || frame_has_win(fr, next_curwin))
1995 win_equal_rec(next_curwin, current, fr, dir, col, row,
1996 width, new_size);
1997 row += new_size;
1998 height -= new_size;
1999 totwincount -= wincount;
2000 }
2001 }
2002}
2003
2004/*
2005 * close all windows for buffer 'buf'
2006 */
2007 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002008close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002010 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002012 win_T *wp;
2013 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002014 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
2016 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002017
2018 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002020 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002022 win_close(wp, FALSE);
2023
2024 /* Start all over, autocommands may change the window layout. */
2025 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 }
2027 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002028 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002030
2031 /* Also check windows in other tab pages. */
2032 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2033 {
2034 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002035 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002036 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2037 if (wp->w_buffer == buf)
2038 {
2039 win_close_othertab(wp, FALSE, tp);
2040
2041 /* Start all over, the tab page may be closed and
2042 * autocommands may change the window layout. */
2043 nexttp = first_tabpage;
2044 break;
2045 }
2046 }
2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002049
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002050 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002051 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052}
2053
2054/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002055 * Return TRUE if the current window is the only window that exists (ignoring
2056 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002057 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002058 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002059 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002060last_window()
2061{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002062 return (one_window() && first_tabpage->tp_next == NULL);
2063}
2064
2065/*
2066 * Return TRUE if there is only one window other than "aucmd_win" in the
2067 * current tab page.
2068 */
2069 static int
2070one_window()
2071{
2072#ifdef FEAT_AUTOCMD
2073 win_T *wp;
2074 int seen_one = FALSE;
2075
2076 FOR_ALL_WINDOWS(wp)
2077 {
2078 if (wp != aucmd_win)
2079 {
2080 if (seen_one)
2081 return FALSE;
2082 seen_one = TRUE;
2083 }
2084 }
2085 return TRUE;
2086#else
2087 return firstwin == lastwin;
2088#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002089}
2090
2091/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002092 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 * If "free_buf" is TRUE related buffer may be unloaded.
2094 *
2095 * called by :quit, :close, :xit, :wq and findtag()
2096 */
2097 void
2098win_close(win, free_buf)
2099 win_T *win;
2100 int free_buf;
2101{
2102 win_T *wp;
2103#ifdef FEAT_AUTOCMD
2104 int other_buffer = FALSE;
2105#endif
2106 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 int dir;
2108 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002109 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002111 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
2113 EMSG(_("E444: Cannot close last window"));
2114 return;
2115 }
2116
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002117#ifdef FEAT_AUTOCMD
2118 if (win == aucmd_win)
2119 {
2120 EMSG(_("E813: Cannot close autocmd window"));
2121 return;
2122 }
2123 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2124 {
2125 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2126 return;
2127 }
2128#endif
2129
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002130 /*
2131 * When closing the last window in a tab page first go to another tab
2132 * page and then close the window and the tab page. This avoids that
2133 * curwin and curtab are not invalid while we are freeing memory, they may
2134 * be used in GUI events.
2135 */
2136 if (firstwin == lastwin)
2137 {
2138 goto_tabpage_tp(alt_tabpage());
2139 redraw_tabline = TRUE;
2140
2141 /* Safety check: Autocommands may have closed the window when jumping
2142 * to the other tab page. */
2143 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2144 {
2145 int h = tabline_height();
2146
2147 win_close_othertab(win, free_buf, prev_curtab);
2148 if (h != tabline_height())
2149 shell_new_rows();
2150 }
2151 return;
2152 }
2153
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 /* When closing the help window, try restoring a snapshot after closing
2155 * the window. Otherwise clear the snapshot, it's now invalid. */
2156 if (win->w_buffer->b_help)
2157 help_window = TRUE;
2158 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002159 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
2161#ifdef FEAT_AUTOCMD
2162 if (win == curwin)
2163 {
2164 /*
2165 * Guess which window is going to be the new current window.
2166 * This may change because of the autocommands (sigh).
2167 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002168 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169
2170 /*
2171 * Be careful: If autocommands delete the window, return now.
2172 */
2173 if (wp->w_buffer != curbuf)
2174 {
2175 other_buffer = TRUE;
2176 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002177 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 return;
2179 }
2180 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002181 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 return;
2183# ifdef FEAT_EVAL
2184 /* autocmds may abort script processing */
2185 if (aborting())
2186 return;
2187# endif
2188 }
2189#endif
2190
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002191#ifdef FEAT_GUI
2192 /* Avoid trouble with scrollbars that are going to be deleted in
2193 * win_free(). */
2194 if (gui.in_use)
2195 out_flush();
2196#endif
2197
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 /*
2199 * Close the link to the buffer.
2200 */
2201 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002202
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002204 * other window or moved to another tab page. */
2205 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 return;
2207
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002208 /* Free the memory used for the window. */
2209 wp = win_free_mem(win, &dir, NULL);
2210
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 /* Make sure curwin isn't invalid. It can cause severe trouble when
2212 * printing an error message. For win_equal() curbuf needs to be valid
2213 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002214 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 {
2216 curwin = wp;
2217#ifdef FEAT_QUICKFIX
2218 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2219 {
2220 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002221 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 * finding another window to go to.
2223 */
2224 for (;;)
2225 {
2226 if (wp->w_next == NULL)
2227 wp = firstwin;
2228 else
2229 wp = wp->w_next;
2230 if (wp == curwin)
2231 break;
2232 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2233 {
2234 curwin = wp;
2235 break;
2236 }
2237 }
2238 }
2239#endif
2240 curbuf = curwin->w_buffer;
2241 close_curwin = TRUE;
2242 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002243 if (p_ea
2244#ifdef FEAT_VERTSPLIT
2245 && (*p_ead == 'b' || *p_ead == dir)
2246#endif
2247 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 win_equal(curwin, TRUE,
2249#ifdef FEAT_VERTSPLIT
2250 dir
2251#else
2252 0
2253#endif
2254 );
2255 else
2256 win_comp_pos();
2257 if (close_curwin)
2258 {
2259 win_enter_ext(wp, FALSE, TRUE);
2260#ifdef FEAT_AUTOCMD
2261 if (other_buffer)
2262 /* careful: after this wp and win may be invalid! */
2263 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2264#endif
2265 }
2266
2267 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002268 * If last window has a status line now and we don't want one,
2269 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 */
2271 last_status(FALSE);
2272
2273 /* After closing the help window, try restoring the window layout from
2274 * before it was opened. */
2275 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002276 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
2278#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2279 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2280 if (gui.in_use && !win_hasvertsplit())
2281 gui_init_which_components(NULL);
2282#endif
2283
2284 redraw_all_later(NOT_VALID);
2285}
2286
2287/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002288 * Close window "win" in tab page "tp", which is not the current tab page.
2289 * This may be the last window ih that tab page and result in closing the tab,
2290 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002291 * Caller must check if buffer is hidden and whether the tabline needs to be
2292 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002293 */
2294 void
2295win_close_othertab(win, free_buf, tp)
2296 win_T *win;
2297 int free_buf;
2298 tabpage_T *tp;
2299{
2300 win_T *wp;
2301 int dir;
2302 tabpage_T *ptp = NULL;
2303
2304 /* Close the link to the buffer. */
2305 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2306
2307 /* Careful: Autocommands may have closed the tab page or made it the
2308 * current tab page. */
2309 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2310 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002311 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002312 return;
2313
2314 /* Autocommands may have closed the window already. */
2315 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2316 ;
2317 if (wp == NULL)
2318 return;
2319
2320 /* Free the memory used for the window. */
2321 wp = win_free_mem(win, &dir, tp);
2322
2323 /* When closing the last window in a tab page remove the tab page. */
2324 if (wp == NULL)
2325 {
2326 if (tp == first_tabpage)
2327 first_tabpage = tp->tp_next;
2328 else
2329 {
2330 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2331 ptp = ptp->tp_next)
2332 ;
2333 if (ptp == NULL)
2334 {
2335 EMSG2(_(e_intern2), "win_close_othertab()");
2336 return;
2337 }
2338 ptp->tp_next = tp->tp_next;
2339 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002340 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002341 }
2342}
2343
2344/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002345 * Free the memory used for a window.
2346 * Returns a pointer to the window that got the freed up space.
2347 */
2348 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002349win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002350 win_T *win;
2351 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002352 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002353{
2354 frame_T *frp;
2355 win_T *wp;
2356
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002357 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002358 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002359 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002360 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002361 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002362
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002363 /* When deleting the current window of another tab page select a new
2364 * current window. */
2365 if (tp != NULL && win == tp->tp_curwin)
2366 tp->tp_curwin = wp;
2367
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002368 return wp;
2369}
2370
2371#if defined(EXITFREE) || defined(PROTO)
2372 void
2373win_free_all()
2374{
2375 int dummy;
2376
Bram Moolenaarf740b292006-02-16 22:11:02 +00002377# ifdef FEAT_WINDOWS
2378 while (first_tabpage->tp_next != NULL)
2379 tabpage_close(TRUE);
2380# endif
2381
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002382# ifdef FEAT_AUTOCMD
2383 if (aucmd_win != NULL)
2384 {
2385 (void)win_free_mem(aucmd_win, &dummy, NULL);
2386 aucmd_win = NULL;
2387 }
2388# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002389
2390 while (firstwin != NULL)
2391 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002392}
2393#endif
2394
2395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 * Remove a window and its frame from the tree of frames.
2397 * Returns a pointer to the window that got the freed up space.
2398 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002399 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002400winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002402 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002403 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404{
2405 frame_T *frp, *frp2, *frp3;
2406 frame_T *frp_close = win->w_frame;
2407 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002410 * If there is only one window there is nothing to remove.
2411 */
2412 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2413 return NULL;
2414
2415 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 * Remove the window from its frame.
2417 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002418 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 wp = frame2win(frp2);
2420
2421 /* Remove this frame from the list of frames. */
2422 frame_remove(frp_close);
2423
2424#ifdef FEAT_VERTSPLIT
2425 if (frp_close->fr_parent->fr_layout == FR_COL)
2426 {
2427#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002428 /* When 'winfixheight' is set, try to find another frame in the column
2429 * (as close to the closed frame as possible) to distribute the height
2430 * to. */
2431 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2432 {
2433 frp = frp_close->fr_prev;
2434 frp3 = frp_close->fr_next;
2435 while (frp != NULL || frp3 != NULL)
2436 {
2437 if (frp != NULL)
2438 {
2439 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2440 {
2441 frp2 = frp;
2442 wp = frp->fr_win;
2443 break;
2444 }
2445 frp = frp->fr_prev;
2446 }
2447 if (frp3 != NULL)
2448 {
2449 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2450 {
2451 frp2 = frp3;
2452 wp = frp3->fr_win;
2453 break;
2454 }
2455 frp3 = frp3->fr_next;
2456 }
2457 }
2458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2460 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461#ifdef FEAT_VERTSPLIT
2462 *dirp = 'v';
2463 }
2464 else
2465 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002466 /* When 'winfixwidth' is set, try to find another frame in the column
2467 * (as close to the closed frame as possible) to distribute the width
2468 * to. */
2469 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2470 {
2471 frp = frp_close->fr_prev;
2472 frp3 = frp_close->fr_next;
2473 while (frp != NULL || frp3 != NULL)
2474 {
2475 if (frp != NULL)
2476 {
2477 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2478 {
2479 frp2 = frp;
2480 wp = frp->fr_win;
2481 break;
2482 }
2483 frp = frp->fr_prev;
2484 }
2485 if (frp3 != NULL)
2486 {
2487 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2488 {
2489 frp2 = frp3;
2490 wp = frp3->fr_win;
2491 break;
2492 }
2493 frp3 = frp3->fr_next;
2494 }
2495 }
2496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002498 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 *dirp = 'h';
2500 }
2501#endif
2502
2503 /* If rows/columns go to a window below/right its positions need to be
2504 * updated. Can only be done after the sizes have been updated. */
2505 if (frp2 == frp_close->fr_next)
2506 {
2507 int row = win->w_winrow;
2508 int col = W_WINCOL(win);
2509
2510 frame_comp_pos(frp2, &row, &col);
2511 }
2512
2513 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2514 {
2515 /* There is no other frame in this list, move its info to the parent
2516 * and remove it. */
2517 frp2->fr_parent->fr_layout = frp2->fr_layout;
2518 frp2->fr_parent->fr_child = frp2->fr_child;
2519 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2520 frp->fr_parent = frp2->fr_parent;
2521 frp2->fr_parent->fr_win = frp2->fr_win;
2522 if (frp2->fr_win != NULL)
2523 frp2->fr_win->w_frame = frp2->fr_parent;
2524 frp = frp2->fr_parent;
2525 vim_free(frp2);
2526
2527 frp2 = frp->fr_parent;
2528 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2529 {
2530 /* The frame above the parent has the same layout, have to merge
2531 * the frames into this list. */
2532 if (frp2->fr_child == frp)
2533 frp2->fr_child = frp->fr_child;
2534 frp->fr_child->fr_prev = frp->fr_prev;
2535 if (frp->fr_prev != NULL)
2536 frp->fr_prev->fr_next = frp->fr_child;
2537 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2538 {
2539 frp3->fr_parent = frp2;
2540 if (frp3->fr_next == NULL)
2541 {
2542 frp3->fr_next = frp->fr_next;
2543 if (frp->fr_next != NULL)
2544 frp->fr_next->fr_prev = frp3;
2545 break;
2546 }
2547 }
2548 vim_free(frp);
2549 }
2550 }
2551
2552 return wp;
2553}
2554
2555/*
2556 * Find out which frame is going to get the freed up space when "win" is
2557 * closed.
2558 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2559 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2560 * This makes opening a window and closing it immediately keep the same window
2561 * layout.
2562 */
2563 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002564win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002566 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567{
2568 frame_T *frp;
2569 int b;
2570
Bram Moolenaarf740b292006-02-16 22:11:02 +00002571 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002572 /* Last window in this tab page, will go to next tab page. */
2573 return alt_tabpage()->tp_curwin->w_frame;
2574
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 frp = win->w_frame;
2576#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002577 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 b = p_spr;
2579 else
2580#endif
2581 b = p_sb;
2582 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2583 return frp->fr_next;
2584 return frp->fr_prev;
2585}
2586
2587/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002588 * Return the tabpage that will be used if the current one is closed.
2589 */
2590 static tabpage_T *
2591alt_tabpage()
2592{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002593 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002594
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002595 /* Use the next tab page if possible. */
2596 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002597 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002598
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002599 /* Find the last but one tab page. */
2600 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2601 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002602 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002603}
2604
2605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 * Find the left-upper window in frame "frp".
2607 */
2608 static win_T *
2609frame2win(frp)
2610 frame_T *frp;
2611{
2612 while (frp->fr_win == NULL)
2613 frp = frp->fr_child;
2614 return frp->fr_win;
2615}
2616
2617/*
2618 * Return TRUE if frame "frp" contains window "wp".
2619 */
2620 static int
2621frame_has_win(frp, wp)
2622 frame_T *frp;
2623 win_T *wp;
2624{
2625 frame_T *p;
2626
2627 if (frp->fr_layout == FR_LEAF)
2628 return frp->fr_win == wp;
2629
2630 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2631 if (frame_has_win(p, wp))
2632 return TRUE;
2633 return FALSE;
2634}
2635
2636/*
2637 * Set a new height for a frame. Recursively sets the height for contained
2638 * frames and windows. Caller must take care of positions.
2639 */
2640 static void
2641frame_new_height(topfrp, height, topfirst, wfh)
2642 frame_T *topfrp;
2643 int height;
2644 int topfirst; /* resize topmost contained frame first */
2645 int wfh; /* obey 'winfixheight' when there is a choice;
2646 may cause the height not to be set */
2647{
2648 frame_T *frp;
2649 int extra_lines;
2650 int h;
2651
2652 if (topfrp->fr_win != NULL)
2653 {
2654 /* Simple case: just one window. */
2655 win_new_height(topfrp->fr_win,
2656 height - topfrp->fr_win->w_status_height);
2657 }
2658#ifdef FEAT_VERTSPLIT
2659 else if (topfrp->fr_layout == FR_ROW)
2660 {
2661 do
2662 {
2663 /* All frames in this row get the same new height. */
2664 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2665 {
2666 frame_new_height(frp, height, topfirst, wfh);
2667 if (frp->fr_height > height)
2668 {
2669 /* Could not fit the windows, make the whole row higher. */
2670 height = frp->fr_height;
2671 break;
2672 }
2673 }
2674 }
2675 while (frp != NULL);
2676 }
2677#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002678 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 {
2680 /* Complicated case: Resize a column of frames. Resize the bottom
2681 * frame first, frames above that when needed. */
2682
2683 frp = topfrp->fr_child;
2684 if (wfh)
2685 /* Advance past frames with one window with 'wfh' set. */
2686 while (frame_fixed_height(frp))
2687 {
2688 frp = frp->fr_next;
2689 if (frp == NULL)
2690 return; /* no frame without 'wfh', give up */
2691 }
2692 if (!topfirst)
2693 {
2694 /* Find the bottom frame of this column */
2695 while (frp->fr_next != NULL)
2696 frp = frp->fr_next;
2697 if (wfh)
2698 /* Advance back for frames with one window with 'wfh' set. */
2699 while (frame_fixed_height(frp))
2700 frp = frp->fr_prev;
2701 }
2702
2703 extra_lines = height - topfrp->fr_height;
2704 if (extra_lines < 0)
2705 {
2706 /* reduce height of contained frames, bottom or top frame first */
2707 while (frp != NULL)
2708 {
2709 h = frame_minheight(frp, NULL);
2710 if (frp->fr_height + extra_lines < h)
2711 {
2712 extra_lines += frp->fr_height - h;
2713 frame_new_height(frp, h, topfirst, wfh);
2714 }
2715 else
2716 {
2717 frame_new_height(frp, frp->fr_height + extra_lines,
2718 topfirst, wfh);
2719 break;
2720 }
2721 if (topfirst)
2722 {
2723 do
2724 frp = frp->fr_next;
2725 while (wfh && frp != NULL && frame_fixed_height(frp));
2726 }
2727 else
2728 {
2729 do
2730 frp = frp->fr_prev;
2731 while (wfh && frp != NULL && frame_fixed_height(frp));
2732 }
2733 /* Increase "height" if we could not reduce enough frames. */
2734 if (frp == NULL)
2735 height -= extra_lines;
2736 }
2737 }
2738 else if (extra_lines > 0)
2739 {
2740 /* increase height of bottom or top frame */
2741 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2742 }
2743 }
2744 topfrp->fr_height = height;
2745}
2746
2747/*
2748 * Return TRUE if height of frame "frp" should not be changed because of
2749 * the 'winfixheight' option.
2750 */
2751 static int
2752frame_fixed_height(frp)
2753 frame_T *frp;
2754{
2755 /* frame with one window: fixed height if 'winfixheight' set. */
2756 if (frp->fr_win != NULL)
2757 return frp->fr_win->w_p_wfh;
2758
2759 if (frp->fr_layout == FR_ROW)
2760 {
2761 /* The frame is fixed height if one of the frames in the row is fixed
2762 * height. */
2763 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2764 if (frame_fixed_height(frp))
2765 return TRUE;
2766 return FALSE;
2767 }
2768
2769 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2770 * frames in the row are fixed height. */
2771 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2772 if (!frame_fixed_height(frp))
2773 return FALSE;
2774 return TRUE;
2775}
2776
2777#ifdef FEAT_VERTSPLIT
2778/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002779 * Return TRUE if width of frame "frp" should not be changed because of
2780 * the 'winfixwidth' option.
2781 */
2782 static int
2783frame_fixed_width(frp)
2784 frame_T *frp;
2785{
2786 /* frame with one window: fixed width if 'winfixwidth' set. */
2787 if (frp->fr_win != NULL)
2788 return frp->fr_win->w_p_wfw;
2789
2790 if (frp->fr_layout == FR_COL)
2791 {
2792 /* The frame is fixed width if one of the frames in the row is fixed
2793 * width. */
2794 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2795 if (frame_fixed_width(frp))
2796 return TRUE;
2797 return FALSE;
2798 }
2799
2800 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2801 * frames in the row are fixed width. */
2802 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2803 if (!frame_fixed_width(frp))
2804 return FALSE;
2805 return TRUE;
2806}
2807
2808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 * Add a status line to windows at the bottom of "frp".
2810 * Note: Does not check if there is room!
2811 */
2812 static void
2813frame_add_statusline(frp)
2814 frame_T *frp;
2815{
2816 win_T *wp;
2817
2818 if (frp->fr_layout == FR_LEAF)
2819 {
2820 wp = frp->fr_win;
2821 if (wp->w_status_height == 0)
2822 {
2823 if (wp->w_height > 0) /* don't make it negative */
2824 --wp->w_height;
2825 wp->w_status_height = STATUS_HEIGHT;
2826 }
2827 }
2828 else if (frp->fr_layout == FR_ROW)
2829 {
2830 /* Handle all the frames in the row. */
2831 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2832 frame_add_statusline(frp);
2833 }
2834 else /* frp->fr_layout == FR_COL */
2835 {
2836 /* Only need to handle the last frame in the column. */
2837 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2838 ;
2839 frame_add_statusline(frp);
2840 }
2841}
2842
2843/*
2844 * Set width of a frame. Handles recursively going through contained frames.
2845 * May remove separator line for windows at the right side (for win_close()).
2846 */
2847 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002848frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 frame_T *topfrp;
2850 int width;
2851 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002852 int wfw; /* obey 'winfixwidth' when there is a choice;
2853 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854{
2855 frame_T *frp;
2856 int extra_cols;
2857 int w;
2858 win_T *wp;
2859
2860 if (topfrp->fr_layout == FR_LEAF)
2861 {
2862 /* Simple case: just one window. */
2863 wp = topfrp->fr_win;
2864 /* Find out if there are any windows right of this one. */
2865 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2866 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2867 break;
2868 if (frp->fr_parent == NULL)
2869 wp->w_vsep_width = 0;
2870 win_new_width(wp, width - wp->w_vsep_width);
2871 }
2872 else if (topfrp->fr_layout == FR_COL)
2873 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002874 do
2875 {
2876 /* All frames in this column get the same new width. */
2877 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2878 {
2879 frame_new_width(frp, width, leftfirst, wfw);
2880 if (frp->fr_width > width)
2881 {
2882 /* Could not fit the windows, make whole column wider. */
2883 width = frp->fr_width;
2884 break;
2885 }
2886 }
2887 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 }
2889 else /* fr_layout == FR_ROW */
2890 {
2891 /* Complicated case: Resize a row of frames. Resize the rightmost
2892 * frame first, frames left of it when needed. */
2893
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002895 if (wfw)
2896 /* Advance past frames with one window with 'wfw' set. */
2897 while (frame_fixed_width(frp))
2898 {
2899 frp = frp->fr_next;
2900 if (frp == NULL)
2901 return; /* no frame without 'wfw', give up */
2902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002904 {
2905 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 while (frp->fr_next != NULL)
2907 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002908 if (wfw)
2909 /* Advance back for frames with one window with 'wfw' set. */
2910 while (frame_fixed_width(frp))
2911 frp = frp->fr_prev;
2912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913
2914 extra_cols = width - topfrp->fr_width;
2915 if (extra_cols < 0)
2916 {
2917 /* reduce frame width, rightmost frame first */
2918 while (frp != NULL)
2919 {
2920 w = frame_minwidth(frp, NULL);
2921 if (frp->fr_width + extra_cols < w)
2922 {
2923 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002924 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 }
2926 else
2927 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002928 frame_new_width(frp, frp->fr_width + extra_cols,
2929 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 break;
2931 }
2932 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002933 {
2934 do
2935 frp = frp->fr_next;
2936 while (wfw && frp != NULL && frame_fixed_width(frp));
2937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002939 {
2940 do
2941 frp = frp->fr_prev;
2942 while (wfw && frp != NULL && frame_fixed_width(frp));
2943 }
2944 /* Increase "width" if we could not reduce enough frames. */
2945 if (frp == NULL)
2946 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
2948 }
2949 else if (extra_cols > 0)
2950 {
2951 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002952 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 }
2954 }
2955 topfrp->fr_width = width;
2956}
2957
2958/*
2959 * Add the vertical separator to windows at the right side of "frp".
2960 * Note: Does not check if there is room!
2961 */
2962 static void
2963frame_add_vsep(frp)
2964 frame_T *frp;
2965{
2966 win_T *wp;
2967
2968 if (frp->fr_layout == FR_LEAF)
2969 {
2970 wp = frp->fr_win;
2971 if (wp->w_vsep_width == 0)
2972 {
2973 if (wp->w_width > 0) /* don't make it negative */
2974 --wp->w_width;
2975 wp->w_vsep_width = 1;
2976 }
2977 }
2978 else if (frp->fr_layout == FR_COL)
2979 {
2980 /* Handle all the frames in the column. */
2981 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2982 frame_add_vsep(frp);
2983 }
2984 else /* frp->fr_layout == FR_ROW */
2985 {
2986 /* Only need to handle the last frame in the row. */
2987 frp = frp->fr_child;
2988 while (frp->fr_next != NULL)
2989 frp = frp->fr_next;
2990 frame_add_vsep(frp);
2991 }
2992}
2993
2994/*
2995 * Set frame width from the window it contains.
2996 */
2997 static void
2998frame_fix_width(wp)
2999 win_T *wp;
3000{
3001 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3002}
3003#endif
3004
3005/*
3006 * Set frame height from the window it contains.
3007 */
3008 static void
3009frame_fix_height(wp)
3010 win_T *wp;
3011{
3012 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3013}
3014
3015/*
3016 * Compute the minimal height for frame "topfrp".
3017 * Uses the 'winminheight' option.
3018 * When "next_curwin" isn't NULL, use p_wh for this window.
3019 * When "next_curwin" is NOWIN, don't use at least one line for the current
3020 * window.
3021 */
3022 static int
3023frame_minheight(topfrp, next_curwin)
3024 frame_T *topfrp;
3025 win_T *next_curwin;
3026{
3027 frame_T *frp;
3028 int m;
3029#ifdef FEAT_VERTSPLIT
3030 int n;
3031#endif
3032
3033 if (topfrp->fr_win != NULL)
3034 {
3035 if (topfrp->fr_win == next_curwin)
3036 m = p_wh + topfrp->fr_win->w_status_height;
3037 else
3038 {
3039 /* window: minimal height of the window plus status line */
3040 m = p_wmh + topfrp->fr_win->w_status_height;
3041 /* Current window is minimal one line high */
3042 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3043 ++m;
3044 }
3045 }
3046#ifdef FEAT_VERTSPLIT
3047 else if (topfrp->fr_layout == FR_ROW)
3048 {
3049 /* get the minimal height from each frame in this row */
3050 m = 0;
3051 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3052 {
3053 n = frame_minheight(frp, next_curwin);
3054 if (n > m)
3055 m = n;
3056 }
3057 }
3058#endif
3059 else
3060 {
3061 /* Add up the minimal heights for all frames in this column. */
3062 m = 0;
3063 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3064 m += frame_minheight(frp, next_curwin);
3065 }
3066
3067 return m;
3068}
3069
3070#ifdef FEAT_VERTSPLIT
3071/*
3072 * Compute the minimal width for frame "topfrp".
3073 * When "next_curwin" isn't NULL, use p_wiw for this window.
3074 * When "next_curwin" is NOWIN, don't use at least one column for the current
3075 * window.
3076 */
3077 static int
3078frame_minwidth(topfrp, next_curwin)
3079 frame_T *topfrp;
3080 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3081{
3082 frame_T *frp;
3083 int m, n;
3084
3085 if (topfrp->fr_win != NULL)
3086 {
3087 if (topfrp->fr_win == next_curwin)
3088 m = p_wiw + topfrp->fr_win->w_vsep_width;
3089 else
3090 {
3091 /* window: minimal width of the window plus separator column */
3092 m = p_wmw + topfrp->fr_win->w_vsep_width;
3093 /* Current window is minimal one column wide */
3094 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3095 ++m;
3096 }
3097 }
3098 else if (topfrp->fr_layout == FR_COL)
3099 {
3100 /* get the minimal width from each frame in this column */
3101 m = 0;
3102 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3103 {
3104 n = frame_minwidth(frp, next_curwin);
3105 if (n > m)
3106 m = n;
3107 }
3108 }
3109 else
3110 {
3111 /* Add up the minimal widths for all frames in this row. */
3112 m = 0;
3113 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3114 m += frame_minwidth(frp, next_curwin);
3115 }
3116
3117 return m;
3118}
3119#endif
3120
3121
3122/*
3123 * Try to close all windows except current one.
3124 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3125 * used and the buffer was modified.
3126 *
3127 * Used by ":bdel" and ":only".
3128 */
3129 void
3130close_others(message, forceit)
3131 int message;
3132 int forceit; /* always hide all other windows */
3133{
3134 win_T *wp;
3135 win_T *nextwp;
3136 int r;
3137
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003138 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 {
3140 if (message
3141#ifdef FEAT_AUTOCMD
3142 && !autocmd_busy
3143#endif
3144 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003145 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 return;
3147 }
3148
3149 /* Be very careful here: autocommands may change the window layout. */
3150 for (wp = firstwin; win_valid(wp); wp = nextwp)
3151 {
3152 nextwp = wp->w_next;
3153 if (wp != curwin) /* don't close current window */
3154 {
3155
3156 /* Check if it's allowed to abandon this window */
3157 r = can_abandon(wp->w_buffer, forceit);
3158#ifdef FEAT_AUTOCMD
3159 if (!win_valid(wp)) /* autocommands messed wp up */
3160 {
3161 nextwp = firstwin;
3162 continue;
3163 }
3164#endif
3165 if (!r)
3166 {
3167#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3168 if (message && (p_confirm || cmdmod.confirm) && p_write)
3169 {
3170 dialog_changed(wp->w_buffer, FALSE);
3171# ifdef FEAT_AUTOCMD
3172 if (!win_valid(wp)) /* autocommands messed wp up */
3173 {
3174 nextwp = firstwin;
3175 continue;
3176 }
3177# endif
3178 }
3179 if (bufIsChanged(wp->w_buffer))
3180#endif
3181 continue;
3182 }
3183 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3184 }
3185 }
3186
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003187 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 EMSG(_("E445: Other window contains changes"));
3189}
3190
3191#endif /* FEAT_WINDOWS */
3192
3193/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003194 * Init the current window "curwin".
3195 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 */
3197 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003198curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003200 win_init_empty(curwin);
3201}
3202
3203 void
3204win_init_empty(wp)
3205 win_T *wp;
3206{
3207 redraw_win_later(wp, NOT_VALID);
3208 wp->w_lines_valid = 0;
3209 wp->w_cursor.lnum = 1;
3210 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003212 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003214 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3215 wp->w_pcmark.col = 0;
3216 wp->w_prev_pcmark.lnum = 0;
3217 wp->w_prev_pcmark.col = 0;
3218 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003220 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003222 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003224 if (wp->w_p_rl)
3225 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003227 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228#endif
3229}
3230
3231/*
3232 * Allocate the first window and put an empty buffer in it.
3233 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003234 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003236 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237win_alloc_first()
3238{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003239 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003240 return FAIL;
3241
3242#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003243 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003244 if (first_tabpage == NULL)
3245 return FAIL;
3246 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003247 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003248#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003249
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003250 return OK;
3251}
3252
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003253#if defined(FEAT_AUTOCMD) || defined(PROTO)
3254/*
3255 * Init "aucmd_win". This can only be done after the first
3256 * window is fully initialized, thus it can't be in win_alloc_first().
3257 */
3258 void
3259win_alloc_aucmd_win()
3260{
3261 aucmd_win = win_alloc(NULL, TRUE);
3262 if (aucmd_win != NULL)
3263 {
3264 win_init_some(aucmd_win, curwin);
3265# ifdef FEAT_SCROLLBIND
3266 aucmd_win->w_p_scb = FALSE;
3267# endif
3268 new_frame(aucmd_win);
3269 }
3270}
3271#endif
3272
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003273/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003274 * Allocate the first window or the first window in a new tab page.
3275 * When "oldwin" is NULL create an empty buffer for it.
3276 * When "oldwin" is not NULL copy info from it to the new window (only with
3277 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003278 * Return FAIL when something goes wrong (out of memory).
3279 */
3280 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003281win_alloc_firstwin(oldwin)
3282 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003283{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003284 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003285 if (oldwin == NULL)
3286 {
3287 /* Very first window, need to create an empty buffer for it and
3288 * initialize from scratch. */
3289 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3290 if (curwin == NULL || curbuf == NULL)
3291 return FAIL;
3292 curwin->w_buffer = curbuf;
3293 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003295 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003297 curwin_init(); /* init current window */
3298 }
3299#ifdef FEAT_WINDOWS
3300 else
3301 {
3302 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003303 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003304
3305# ifdef FEAT_SCROLLBIND
3306 /* We don't want scroll-binding in the first window. */
3307 curwin->w_p_scb = FALSE;
3308# endif
3309 }
3310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003312 new_frame(curwin);
3313 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003314 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003315 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316#ifdef FEAT_VERTSPLIT
3317 topframe->fr_width = Columns;
3318#endif
3319 topframe->fr_height = Rows - p_ch;
3320 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003321
3322 return OK;
3323}
3324
3325/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003326 * Create a frame for window "wp".
3327 */
3328 static void
3329new_frame(win_T *wp)
3330{
3331 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3332
3333 wp->w_frame = frp;
3334 if (frp != NULL)
3335 {
3336 frp->fr_layout = FR_LEAF;
3337 frp->fr_win = wp;
3338 }
3339}
3340
3341/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003342 * Initialize the window and frame size to the maximum.
3343 */
3344 void
3345win_init_size()
3346{
3347 firstwin->w_height = ROWS_AVAIL;
3348 topframe->fr_height = ROWS_AVAIL;
3349#ifdef FEAT_VERTSPLIT
3350 firstwin->w_width = Columns;
3351 topframe->fr_width = Columns;
3352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353}
3354
3355#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003356
3357/*
3358 * Allocate a new tabpage_T and init the values.
3359 * Returns NULL when out of memory.
3360 */
3361 static tabpage_T *
3362alloc_tabpage()
3363{
3364 tabpage_T *tp;
3365
3366 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3367 if (tp != NULL)
3368 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003369# ifdef FEAT_GUI
3370 int i;
3371
3372 for (i = 0; i < 3; i++)
3373 tp->tp_prev_which_scrollbars[i] = -1;
3374# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003375# ifdef FEAT_DIFF
3376 tp->tp_diff_invalid = TRUE;
3377# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003378#ifdef FEAT_EVAL
3379 /* init t: variables */
3380 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3381#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003382 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003383 }
3384 return tp;
3385}
3386
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003387 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003388free_tabpage(tp)
3389 tabpage_T *tp;
3390{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003391 int idx;
3392
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003393# ifdef FEAT_DIFF
3394 diff_clear(tp);
3395# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003396 for (idx = 0; idx < SNAP_COUNT; ++idx)
3397 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003398#ifdef FEAT_EVAL
3399 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3400#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003401 vim_free(tp);
3402}
3403
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003404/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003405 * Create a new Tab page with one window.
3406 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003407 * When "after" is 0 put it just after the current Tab page.
3408 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003409 * Return FAIL or OK.
3410 */
3411 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003412win_new_tabpage(after)
3413 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003414{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003415 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003416 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003417 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003418
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003419 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003420 if (newtp == NULL)
3421 return FAIL;
3422
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003423 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003424 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003425 {
3426 vim_free(newtp);
3427 return FAIL;
3428 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003429 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003430
3431 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003432 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003433 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003434 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003435 if (after == 1)
3436 {
3437 /* New tab page becomes the first one. */
3438 newtp->tp_next = first_tabpage;
3439 first_tabpage = newtp;
3440 }
3441 else
3442 {
3443 if (after > 0)
3444 {
3445 /* Put new tab page before tab page "after". */
3446 n = 2;
3447 for (tp = first_tabpage; tp->tp_next != NULL
3448 && n < after; tp = tp->tp_next)
3449 ++n;
3450 }
3451 newtp->tp_next = tp->tp_next;
3452 tp->tp_next = newtp;
3453 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003454 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003455 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003456 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003457
3458 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003459 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003460
3461#if defined(FEAT_GUI)
3462 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3463 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003464 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003465#endif
3466
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003467 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003468#ifdef FEAT_AUTOCMD
3469 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3470 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3471#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003472 return OK;
3473 }
3474
3475 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003476 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003477 return FAIL;
3478}
3479
3480/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003481 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3482 * like with ":split".
3483 * Returns OK if a new tab page was created, FAIL otherwise.
3484 */
3485 int
3486may_open_tabpage()
3487{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003488 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003489
Bram Moolenaard326ce82007-03-11 14:48:29 +00003490 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003491 {
3492 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003493 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003494 return win_new_tabpage(n);
3495 }
3496 return FAIL;
3497}
3498
3499/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003500 * Create up to "maxcount" tabpages with empty windows.
3501 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003502 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003503 int
3504make_tabpages(maxcount)
3505 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003506{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003507 int count = maxcount;
3508 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003509
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003510 /* Limit to 'tabpagemax' tabs. */
3511 if (count > p_tpm)
3512 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003513
3514#ifdef FEAT_AUTOCMD
3515 /*
3516 * Don't execute autocommands while creating the tab pages. Must do that
3517 * when putting the buffers in the windows.
3518 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003519 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003520#endif
3521
3522 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003523 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003524 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003525
3526#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003527 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003528#endif
3529
3530 /* return actual number of tab pages */
3531 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003532}
3533
3534/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003535 * Return TRUE when "tpc" points to a valid tab page.
3536 */
3537 int
3538valid_tabpage(tpc)
3539 tabpage_T *tpc;
3540{
3541 tabpage_T *tp;
3542
3543 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3544 if (tp == tpc)
3545 return TRUE;
3546 return FALSE;
3547}
3548
3549/*
3550 * Find tab page "n" (first one is 1). Returns NULL when not found.
3551 */
3552 tabpage_T *
3553find_tabpage(n)
3554 int n;
3555{
3556 tabpage_T *tp;
3557 int i = 1;
3558
3559 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3560 ++i;
3561 return tp;
3562}
3563
3564/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003565 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003566 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003567 */
3568 int
3569tabpage_index(ftp)
3570 tabpage_T *ftp;
3571{
3572 int i = 1;
3573 tabpage_T *tp;
3574
3575 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3576 ++i;
3577 return i;
3578}
3579
3580/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003581 * Prepare for leaving the current tab page.
3582 * When autocomands change "curtab" we don't leave the tab page and return
3583 * FAIL.
3584 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003585 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003586 static int
3587leave_tabpage(new_curbuf)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003588 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003589 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003590{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003591 tabpage_T *tp = curtab;
3592
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003593#ifdef FEAT_VISUAL
3594 reset_VIsual_and_resel(); /* stop Visual mode */
3595#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003596#ifdef FEAT_AUTOCMD
3597 if (new_curbuf != curbuf)
3598 {
3599 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3600 if (curtab != tp)
3601 return FAIL;
3602 }
3603 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3604 if (curtab != tp)
3605 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003606 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003607 if (curtab != tp)
3608 return FAIL;
3609#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003610#if defined(FEAT_GUI)
3611 /* Remove the scrollbars. They may be added back later. */
3612 if (gui.in_use)
3613 gui_remove_scrollbars();
3614#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003615 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003616 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003617 tp->tp_firstwin = firstwin;
3618 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003619 tp->tp_old_Rows = Rows;
3620 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003621 firstwin = NULL;
3622 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003623 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003624}
3625
3626/*
3627 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003628 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003629 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003630 static void
3631enter_tabpage(tp, old_curbuf)
3632 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003633 buf_T *old_curbuf UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003634{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003635 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003636 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003637
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003638 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003639 firstwin = tp->tp_firstwin;
3640 lastwin = tp->tp_lastwin;
3641 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003642
3643 /* We would like doing the TabEnter event first, but we don't have a
3644 * valid current window yet, which may break some commands.
3645 * This triggers autocommands, thus may make "tp" invalid. */
3646 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3647 prevwin = next_prevwin;
3648
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003649#ifdef FEAT_AUTOCMD
3650 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003651
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003652 if (old_curbuf != curbuf)
3653 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3654#endif
3655
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003656 last_status(FALSE); /* status line may appear or disappear */
3657 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003658 must_redraw = CLEAR; /* need to redraw everything */
3659#ifdef FEAT_DIFF
3660 diff_need_scrollbind = TRUE;
3661#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003662
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003663 /* The tabpage line may have appeared or disappeared, may need to resize
3664 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003665 * frame sizes too. Use the stored value of p_ch, so that it can be
3666 * different for each tab page. */
3667 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003668 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3669#ifdef FEAT_GUI_TABLINE
3670 && !gui_use_tabline()
3671#endif
3672 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003673 shell_new_rows();
3674#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003675 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003676 shell_new_columns(); /* update window widths */
3677#endif
3678
3679#if defined(FEAT_GUI)
3680 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3681 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003682 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003683#endif
3684
3685 redraw_all_later(CLEAR);
3686}
3687
3688/*
3689 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003690 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003691 */
3692 void
3693goto_tabpage(n)
3694 int n;
3695{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003696 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003697 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003698 int i;
3699
Bram Moolenaard68071d2006-05-02 22:08:30 +00003700 if (text_locked())
3701 {
3702 /* Not allowed when editing the command line. */
3703#ifdef FEAT_CMDWIN
3704 if (cmdwin_type != 0)
3705 EMSG(_(e_cmdwin));
3706 else
3707#endif
3708 EMSG(_(e_secure));
3709 return;
3710 }
3711
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003712 /* If there is only one it can't work. */
3713 if (first_tabpage->tp_next == NULL)
3714 {
3715 if (n > 1)
3716 beep_flush();
3717 return;
3718 }
3719
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003720 if (n == 0)
3721 {
3722 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003723 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003724 tp = first_tabpage;
3725 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003726 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003727 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003728 else if (n < 0)
3729 {
3730 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3731 * this N times. */
3732 ttp = curtab;
3733 for (i = n; i < 0; ++i)
3734 {
3735 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3736 tp = tp->tp_next)
3737 ;
3738 ttp = tp;
3739 }
3740 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003741 else if (n == 9999)
3742 {
3743 /* Go to last tab page. */
3744 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3745 ;
3746 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003747 else
3748 {
3749 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003750 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003751 if (tp == NULL)
3752 {
3753 beep_flush();
3754 return;
3755 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003756 }
3757
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003758 goto_tabpage_tp(tp);
3759
3760#ifdef FEAT_GUI_TABLINE
3761 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003762 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003763#endif
3764}
3765
3766/*
3767 * Go to tabpage "tp".
3768 * Note: doesn't update the GUI tab.
3769 */
3770 void
3771goto_tabpage_tp(tp)
3772 tabpage_T *tp;
3773{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003774 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003775 {
3776 if (valid_tabpage(tp))
3777 enter_tabpage(tp, curbuf);
3778 else
3779 enter_tabpage(curtab, curbuf);
3780 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003781}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782
3783/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003784 * Enter window "wp" in tab page "tp".
3785 * Also updates the GUI tab.
3786 */
3787 void
3788goto_tabpage_win(tp, wp)
3789 tabpage_T *tp;
3790 win_T *wp;
3791{
3792 goto_tabpage_tp(tp);
3793 if (curtab == tp && win_valid(wp))
3794 {
3795 win_enter(wp, TRUE);
3796# ifdef FEAT_GUI_TABLINE
3797 if (gui_use_tabline())
3798 gui_mch_set_curtab(tabpage_index(curtab));
3799# endif
3800 }
3801}
3802
3803/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003804 * Move the current tab page to before tab page "nr".
3805 */
3806 void
3807tabpage_move(nr)
3808 int nr;
3809{
3810 int n = nr;
3811 tabpage_T *tp;
3812
3813 if (first_tabpage->tp_next == NULL)
3814 return;
3815
3816 /* Remove the current tab page from the list of tab pages. */
3817 if (curtab == first_tabpage)
3818 first_tabpage = curtab->tp_next;
3819 else
3820 {
3821 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3822 if (tp->tp_next == curtab)
3823 break;
3824 if (tp == NULL) /* "cannot happen" */
3825 return;
3826 tp->tp_next = curtab->tp_next;
3827 }
3828
3829 /* Re-insert it at the specified position. */
3830 if (n == 0)
3831 {
3832 curtab->tp_next = first_tabpage;
3833 first_tabpage = curtab;
3834 }
3835 else
3836 {
3837 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3838 --n;
3839 curtab->tp_next = tp->tp_next;
3840 tp->tp_next = curtab;
3841 }
3842
3843 /* Need to redraw the tabline. Tab page contents doesn't change. */
3844 redraw_tabline = TRUE;
3845}
3846
3847
3848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 * Go to another window.
3850 * When jumping to another buffer, stop Visual mode. Do this before
3851 * changing windows so we can yank the selection into the '*' register.
3852 * When jumping to another window on the same buffer, adjust its cursor
3853 * position to keep the same Visual area.
3854 */
3855 void
3856win_goto(wp)
3857 win_T *wp;
3858{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003859 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
3861 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003862 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 return;
3864 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003865#ifdef FEAT_AUTOCMD
3866 if (curbuf_locked())
3867 return;
3868#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003869
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870#ifdef FEAT_VISUAL
3871 if (wp->w_buffer != curbuf)
3872 reset_VIsual_and_resel();
3873 else if (VIsual_active)
3874 wp->w_cursor = curwin->w_cursor;
3875#endif
3876
3877#ifdef FEAT_GUI
3878 need_mouse_correct = TRUE;
3879#endif
3880 win_enter(wp, TRUE);
3881}
3882
3883#if defined(FEAT_PERL) || defined(PROTO)
3884/*
3885 * Find window number "winnr" (counting top to bottom).
3886 */
3887 win_T *
3888win_find_nr(winnr)
3889 int winnr;
3890{
3891 win_T *wp;
3892
3893# ifdef FEAT_WINDOWS
3894 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3895 if (--winnr == 0)
3896 break;
3897 return wp;
3898# else
3899 return curwin;
3900# endif
3901}
3902#endif
3903
3904#ifdef FEAT_VERTSPLIT
3905/*
3906 * Move to window above or below "count" times.
3907 */
3908 static void
3909win_goto_ver(up, count)
3910 int up; /* TRUE to go to win above */
3911 long count;
3912{
3913 frame_T *fr;
3914 frame_T *nfr;
3915 frame_T *foundfr;
3916
3917 foundfr = curwin->w_frame;
3918 while (count--)
3919 {
3920 /*
3921 * First go upwards in the tree of frames until we find a upwards or
3922 * downwards neighbor.
3923 */
3924 fr = foundfr;
3925 for (;;)
3926 {
3927 if (fr == topframe)
3928 goto end;
3929 if (up)
3930 nfr = fr->fr_prev;
3931 else
3932 nfr = fr->fr_next;
3933 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3934 break;
3935 fr = fr->fr_parent;
3936 }
3937
3938 /*
3939 * Now go downwards to find the bottom or top frame in it.
3940 */
3941 for (;;)
3942 {
3943 if (nfr->fr_layout == FR_LEAF)
3944 {
3945 foundfr = nfr;
3946 break;
3947 }
3948 fr = nfr->fr_child;
3949 if (nfr->fr_layout == FR_ROW)
3950 {
3951 /* Find the frame at the cursor row. */
3952 while (fr->fr_next != NULL
3953 && frame2win(fr)->w_wincol + fr->fr_width
3954 <= curwin->w_wincol + curwin->w_wcol)
3955 fr = fr->fr_next;
3956 }
3957 if (nfr->fr_layout == FR_COL && up)
3958 while (fr->fr_next != NULL)
3959 fr = fr->fr_next;
3960 nfr = fr;
3961 }
3962 }
3963end:
3964 if (foundfr != NULL)
3965 win_goto(foundfr->fr_win);
3966}
3967
3968/*
3969 * Move to left or right window.
3970 */
3971 static void
3972win_goto_hor(left, count)
3973 int left; /* TRUE to go to left win */
3974 long count;
3975{
3976 frame_T *fr;
3977 frame_T *nfr;
3978 frame_T *foundfr;
3979
3980 foundfr = curwin->w_frame;
3981 while (count--)
3982 {
3983 /*
3984 * First go upwards in the tree of frames until we find a left or
3985 * right neighbor.
3986 */
3987 fr = foundfr;
3988 for (;;)
3989 {
3990 if (fr == topframe)
3991 goto end;
3992 if (left)
3993 nfr = fr->fr_prev;
3994 else
3995 nfr = fr->fr_next;
3996 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3997 break;
3998 fr = fr->fr_parent;
3999 }
4000
4001 /*
4002 * Now go downwards to find the leftmost or rightmost frame in it.
4003 */
4004 for (;;)
4005 {
4006 if (nfr->fr_layout == FR_LEAF)
4007 {
4008 foundfr = nfr;
4009 break;
4010 }
4011 fr = nfr->fr_child;
4012 if (nfr->fr_layout == FR_COL)
4013 {
4014 /* Find the frame at the cursor row. */
4015 while (fr->fr_next != NULL
4016 && frame2win(fr)->w_winrow + fr->fr_height
4017 <= curwin->w_winrow + curwin->w_wrow)
4018 fr = fr->fr_next;
4019 }
4020 if (nfr->fr_layout == FR_ROW && left)
4021 while (fr->fr_next != NULL)
4022 fr = fr->fr_next;
4023 nfr = fr;
4024 }
4025 }
4026end:
4027 if (foundfr != NULL)
4028 win_goto(foundfr->fr_win);
4029}
4030#endif
4031
4032/*
4033 * Make window "wp" the current window.
4034 */
4035 void
4036win_enter(wp, undo_sync)
4037 win_T *wp;
4038 int undo_sync;
4039{
4040 win_enter_ext(wp, undo_sync, FALSE);
4041}
4042
4043/*
4044 * Make window wp the current window.
4045 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4046 * been closed and isn't valid.
4047 */
4048 static void
4049win_enter_ext(wp, undo_sync, curwin_invalid)
4050 win_T *wp;
4051 int undo_sync;
4052 int curwin_invalid;
4053{
4054#ifdef FEAT_AUTOCMD
4055 int other_buffer = FALSE;
4056#endif
4057
4058 if (wp == curwin && !curwin_invalid) /* nothing to do */
4059 return;
4060
4061#ifdef FEAT_AUTOCMD
4062 if (!curwin_invalid)
4063 {
4064 /*
4065 * Be careful: If autocommands delete the window, return now.
4066 */
4067 if (wp->w_buffer != curbuf)
4068 {
4069 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4070 other_buffer = TRUE;
4071 if (!win_valid(wp))
4072 return;
4073 }
4074 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4075 if (!win_valid(wp))
4076 return;
4077# ifdef FEAT_EVAL
4078 /* autocmds may abort script processing */
4079 if (aborting())
4080 return;
4081# endif
4082 }
4083#endif
4084
4085 /* sync undo before leaving the current buffer */
4086 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004087 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 /* may have to copy the buffer options when 'cpo' contains 'S' */
4089 if (wp->w_buffer != curbuf)
4090 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4091 if (!curwin_invalid)
4092 {
4093 prevwin = curwin; /* remember for CTRL-W p */
4094 curwin->w_redr_status = TRUE;
4095 }
4096 curwin = wp;
4097 curbuf = wp->w_buffer;
4098 check_cursor();
4099#ifdef FEAT_VIRTUALEDIT
4100 if (!virtual_active())
4101 curwin->w_cursor.coladd = 0;
4102#endif
4103 changed_line_abv_curs(); /* assume cursor position needs updating */
4104
4105 if (curwin->w_localdir != NULL)
4106 {
4107 /* Window has a local directory: Save current directory as global
4108 * directory (unless that was done already) and change to the local
4109 * directory. */
4110 if (globaldir == NULL)
4111 {
4112 char_u cwd[MAXPATHL];
4113
4114 if (mch_dirname(cwd, MAXPATHL) == OK)
4115 globaldir = vim_strsave(cwd);
4116 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004117 if (mch_chdir((char *)curwin->w_localdir) == 0)
4118 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
4120 else if (globaldir != NULL)
4121 {
4122 /* Window doesn't have a local directory and we are not in the global
4123 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004124 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 vim_free(globaldir);
4126 globaldir = NULL;
4127 shorten_fnames(TRUE);
4128 }
4129
4130#ifdef FEAT_AUTOCMD
4131 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4132 if (other_buffer)
4133 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4134#endif
4135
4136#ifdef FEAT_TITLE
4137 maketitle();
4138#endif
4139 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004140 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 if (restart_edit)
4142 redraw_later(VALID); /* causes status line redraw */
4143
4144 /* set window height to desired minimal value */
4145 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4146 win_setheight((int)p_wh);
4147 else if (curwin->w_height == 0)
4148 win_setheight(1);
4149
4150#ifdef FEAT_VERTSPLIT
4151 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004152 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 win_setwidth((int)p_wiw);
4154#endif
4155
4156#ifdef FEAT_MOUSE
4157 setmouse(); /* in case jumped to/from help buffer */
4158#endif
4159
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004160 /* Change directories when the 'acd' option is set. */
4161 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162}
4163
4164#endif /* FEAT_WINDOWS */
4165
4166#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4167/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004168 * Jump to the first open window that contains buffer "buf", if one exists.
4169 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 */
4171 win_T *
4172buf_jump_open_win(buf)
4173 buf_T *buf;
4174{
4175# ifdef FEAT_WINDOWS
4176 win_T *wp;
4177
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004178 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 if (wp->w_buffer == buf)
4180 break;
4181 if (wp != NULL)
4182 win_enter(wp, FALSE);
4183 return wp;
4184# else
4185 if (curwin->w_buffer == buf)
4186 return curwin;
4187 return NULL;
4188# endif
4189}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004190
4191/*
4192 * Jump to the first open window in any tab page that contains buffer "buf",
4193 * if one exists.
4194 * Returns a pointer to the window found, otherwise NULL.
4195 */
4196 win_T *
4197buf_jump_open_tab(buf)
4198 buf_T *buf;
4199{
4200# ifdef FEAT_WINDOWS
4201 win_T *wp;
4202 tabpage_T *tp;
4203
4204 /* First try the current tab page. */
4205 wp = buf_jump_open_win(buf);
4206 if (wp != NULL)
4207 return wp;
4208
4209 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4210 if (tp != curtab)
4211 {
4212 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4213 if (wp->w_buffer == buf)
4214 break;
4215 if (wp != NULL)
4216 {
4217 goto_tabpage_win(tp, wp);
4218 if (curwin != wp)
4219 wp = NULL; /* something went wrong */
4220 break;
4221 }
4222 }
4223
4224 return wp;
4225# else
4226 if (curwin->w_buffer == buf)
4227 return curwin;
4228 return NULL;
4229# endif
4230}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231#endif
4232
4233/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004234 * Allocate a window structure and link it in the window list when "hidden" is
4235 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004238win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004239 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004240 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241{
4242 win_T *newwin;
4243
4244 /*
4245 * allocate window structure and linesizes arrays
4246 */
4247 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4248 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4249 {
4250 vim_free(newwin);
4251 newwin = NULL;
4252 }
4253
4254 if (newwin != NULL)
4255 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004256#ifdef FEAT_AUTOCMD
4257 /* Don't execute autocommands while the window is not properly
4258 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4259 * event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004260 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 /*
4263 * link the window in the window list
4264 */
4265#ifdef FEAT_WINDOWS
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004266 if (!hidden)
4267 win_append(after, newwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268#endif
4269#ifdef FEAT_VERTSPLIT
4270 newwin->w_wincol = 0;
4271 newwin->w_width = Columns;
4272#endif
4273
4274 /* position the display and the cursor at the top of the file. */
4275 newwin->w_topline = 1;
4276#ifdef FEAT_DIFF
4277 newwin->w_topfill = 0;
4278#endif
4279 newwin->w_botline = 2;
4280 newwin->w_cursor.lnum = 1;
4281#ifdef FEAT_SCROLLBIND
4282 newwin->w_scbind_pos = 1;
4283#endif
4284
4285 /* We won't calculate w_fraction until resizing the window */
4286 newwin->w_fraction = 0;
4287 newwin->w_prev_fraction_row = -1;
4288
4289#ifdef FEAT_GUI
4290 if (gui.in_use)
4291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4293 SBAR_LEFT, newwin);
4294 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4295 SBAR_RIGHT, newwin);
4296 }
4297#endif
4298#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004299 /* init w: variables */
4300 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301#endif
4302#ifdef FEAT_FOLDING
4303 foldInitWin(newwin);
4304#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004305#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004306 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004307#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004308#ifdef FEAT_SEARCH_EXTRA
4309 newwin->w_match_head = NULL;
4310 newwin->w_next_match_id = 4;
4311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 }
4313 return newwin;
4314}
4315
4316#if defined(FEAT_WINDOWS) || defined(PROTO)
4317
4318/*
4319 * remove window 'wp' from the window list and free the structure
4320 */
4321 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004322win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004324 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325{
4326 int i;
4327
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004328#ifdef FEAT_FOLDING
4329 clearFolding(wp);
4330#endif
4331
4332 /* reduce the reference count to the argument list. */
4333 alist_unlink(wp->w_alist);
4334
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004335#ifdef FEAT_AUTOCMD
4336 /* Don't execute autocommands while the window is halfway being deleted.
4337 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004338 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004339#endif
4340
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004341#ifdef FEAT_MZSCHEME
4342 mzscheme_window_free(wp);
4343#endif
4344
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345#ifdef FEAT_PERL
4346 perl_win_free(wp);
4347#endif
4348
4349#ifdef FEAT_PYTHON
4350 python_window_free(wp);
4351#endif
4352
4353#ifdef FEAT_TCL
4354 tcl_window_free(wp);
4355#endif
4356
4357#ifdef FEAT_RUBY
4358 ruby_window_free(wp);
4359#endif
4360
4361 clear_winopt(&wp->w_onebuf_opt);
4362 clear_winopt(&wp->w_allbuf_opt);
4363
4364#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004365 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366#endif
4367
4368 if (prevwin == wp)
4369 prevwin = NULL;
4370 win_free_lsize(wp);
4371
4372 for (i = 0; i < wp->w_tagstacklen; ++i)
4373 vim_free(wp->w_tagstack[i].tagname);
4374
4375 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004376
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004378 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004380
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381#ifdef FEAT_JUMPLIST
4382 free_jumplist(wp);
4383#endif
4384
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004385#ifdef FEAT_QUICKFIX
4386 qf_free_all(wp);
4387#endif
4388
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389#ifdef FEAT_GUI
4390 if (gui.in_use)
4391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4393 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4394 }
4395#endif /* FEAT_GUI */
4396
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004397#ifdef FEAT_AUTOCMD
4398 if (wp != aucmd_win)
4399#endif
4400 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004402
4403#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004404 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406}
4407
4408/*
4409 * Append window "wp" in the window list after window "after".
4410 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004411 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412win_append(after, wp)
4413 win_T *after, *wp;
4414{
4415 win_T *before;
4416
4417 if (after == NULL) /* after NULL is in front of the first */
4418 before = firstwin;
4419 else
4420 before = after->w_next;
4421
4422 wp->w_next = before;
4423 wp->w_prev = after;
4424 if (after == NULL)
4425 firstwin = wp;
4426 else
4427 after->w_next = wp;
4428 if (before == NULL)
4429 lastwin = wp;
4430 else
4431 before->w_prev = wp;
4432}
4433
4434/*
4435 * Remove a window from the window list.
4436 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004437 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004438win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004440 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441{
4442 if (wp->w_prev != NULL)
4443 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004444 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004446 else
4447 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 if (wp->w_next != NULL)
4449 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004450 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004452 else
4453 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454}
4455
4456/*
4457 * Append frame "frp" in a frame list after frame "after".
4458 */
4459 static void
4460frame_append(after, frp)
4461 frame_T *after, *frp;
4462{
4463 frp->fr_next = after->fr_next;
4464 after->fr_next = frp;
4465 if (frp->fr_next != NULL)
4466 frp->fr_next->fr_prev = frp;
4467 frp->fr_prev = after;
4468}
4469
4470/*
4471 * Insert frame "frp" in a frame list before frame "before".
4472 */
4473 static void
4474frame_insert(before, frp)
4475 frame_T *before, *frp;
4476{
4477 frp->fr_next = before;
4478 frp->fr_prev = before->fr_prev;
4479 before->fr_prev = frp;
4480 if (frp->fr_prev != NULL)
4481 frp->fr_prev->fr_next = frp;
4482 else
4483 frp->fr_parent->fr_child = frp;
4484}
4485
4486/*
4487 * Remove a frame from a frame list.
4488 */
4489 static void
4490frame_remove(frp)
4491 frame_T *frp;
4492{
4493 if (frp->fr_prev != NULL)
4494 frp->fr_prev->fr_next = frp->fr_next;
4495 else
4496 frp->fr_parent->fr_child = frp->fr_next;
4497 if (frp->fr_next != NULL)
4498 frp->fr_next->fr_prev = frp->fr_prev;
4499}
4500
4501#endif /* FEAT_WINDOWS */
4502
4503/*
4504 * Allocate w_lines[] for window "wp".
4505 * Return FAIL for failure, OK for success.
4506 */
4507 int
4508win_alloc_lines(wp)
4509 win_T *wp;
4510{
4511 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004512 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 if (wp->w_lines == NULL)
4514 return FAIL;
4515 return OK;
4516}
4517
4518/*
4519 * free lsize arrays for a window
4520 */
4521 void
4522win_free_lsize(wp)
4523 win_T *wp;
4524{
4525 vim_free(wp->w_lines);
4526 wp->w_lines = NULL;
4527}
4528
4529/*
4530 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004531 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 */
4533 void
4534shell_new_rows()
4535{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004536 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
4538 if (firstwin == NULL) /* not initialized yet */
4539 return;
4540#ifdef FEAT_WINDOWS
4541 if (h < frame_minheight(topframe, NULL))
4542 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004543
4544 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 * that doesn't result in the right height, forget about that option. */
4546 frame_new_height(topframe, h, FALSE, TRUE);
4547 if (topframe->fr_height != h)
4548 frame_new_height(topframe, h, FALSE, FALSE);
4549
4550 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4551#else
4552 if (h < 1)
4553 h = 1;
4554 win_new_height(firstwin, h);
4555#endif
4556 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004557#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004558 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004559#endif
4560
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561#if 0
4562 /* Disabled: don't want making the screen smaller make a window larger. */
4563 if (p_ea)
4564 win_equal(curwin, FALSE, 'v');
4565#endif
4566}
4567
4568#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4569/*
4570 * Called from win_new_shellsize() after Columns changed.
4571 */
4572 void
4573shell_new_columns()
4574{
4575 if (firstwin == NULL) /* not initialized yet */
4576 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004577
4578 /* First try setting the widths of windows with 'winfixwidth'. If that
4579 * doesn't result in the right width, forget about that option. */
4580 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4581 if (topframe->fr_width != Columns)
4582 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4583
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4585#if 0
4586 /* Disabled: don't want making the screen smaller make a window larger. */
4587 if (p_ea)
4588 win_equal(curwin, FALSE, 'h');
4589#endif
4590}
4591#endif
4592
4593#if defined(FEAT_CMDWIN) || defined(PROTO)
4594/*
4595 * Save the size of all windows in "gap".
4596 */
4597 void
4598win_size_save(gap)
4599 garray_T *gap;
4600
4601{
4602 win_T *wp;
4603
4604 ga_init2(gap, (int)sizeof(int), 1);
4605 if (ga_grow(gap, win_count() * 2) == OK)
4606 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4607 {
4608 ((int *)gap->ga_data)[gap->ga_len++] =
4609 wp->w_width + wp->w_vsep_width;
4610 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4611 }
4612}
4613
4614/*
4615 * Restore window sizes, but only if the number of windows is still the same.
4616 * Does not free the growarray.
4617 */
4618 void
4619win_size_restore(gap)
4620 garray_T *gap;
4621{
4622 win_T *wp;
4623 int i;
4624
4625 if (win_count() * 2 == gap->ga_len)
4626 {
4627 i = 0;
4628 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4629 {
4630 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4631 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4632 }
4633 /* recompute the window positions */
4634 (void)win_comp_pos();
4635 }
4636}
4637#endif /* FEAT_CMDWIN */
4638
4639#if defined(FEAT_WINDOWS) || defined(PROTO)
4640/*
4641 * Update the position for all windows, using the width and height of the
4642 * frames.
4643 * Returns the row just after the last window.
4644 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004645 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646win_comp_pos()
4647{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004648 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 int col = 0;
4650
4651 frame_comp_pos(topframe, &row, &col);
4652 return row;
4653}
4654
4655/*
4656 * Update the position of the windows in frame "topfrp", using the width and
4657 * height of the frames.
4658 * "*row" and "*col" are the top-left position of the frame. They are updated
4659 * to the bottom-right position plus one.
4660 */
4661 static void
4662frame_comp_pos(topfrp, row, col)
4663 frame_T *topfrp;
4664 int *row;
4665 int *col;
4666{
4667 win_T *wp;
4668 frame_T *frp;
4669#ifdef FEAT_VERTSPLIT
4670 int startcol;
4671 int startrow;
4672#endif
4673
4674 wp = topfrp->fr_win;
4675 if (wp != NULL)
4676 {
4677 if (wp->w_winrow != *row
4678#ifdef FEAT_VERTSPLIT
4679 || wp->w_wincol != *col
4680#endif
4681 )
4682 {
4683 /* position changed, redraw */
4684 wp->w_winrow = *row;
4685#ifdef FEAT_VERTSPLIT
4686 wp->w_wincol = *col;
4687#endif
4688 redraw_win_later(wp, NOT_VALID);
4689 wp->w_redr_status = TRUE;
4690 }
4691 *row += wp->w_height + wp->w_status_height;
4692#ifdef FEAT_VERTSPLIT
4693 *col += wp->w_width + wp->w_vsep_width;
4694#endif
4695 }
4696 else
4697 {
4698#ifdef FEAT_VERTSPLIT
4699 startrow = *row;
4700 startcol = *col;
4701#endif
4702 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4703 {
4704#ifdef FEAT_VERTSPLIT
4705 if (topfrp->fr_layout == FR_ROW)
4706 *row = startrow; /* all frames are at the same row */
4707 else
4708 *col = startcol; /* all frames are at the same col */
4709#endif
4710 frame_comp_pos(frp, row, col);
4711 }
4712 }
4713}
4714
4715#endif /* FEAT_WINDOWS */
4716
4717/*
4718 * Set current window height and take care of repositioning other windows to
4719 * fit around it.
4720 */
4721 void
4722win_setheight(height)
4723 int height;
4724{
4725 win_setheight_win(height, curwin);
4726}
4727
4728/*
4729 * Set the window height of window "win" and take care of repositioning other
4730 * windows to fit around it.
4731 */
4732 void
4733win_setheight_win(height, win)
4734 int height;
4735 win_T *win;
4736{
4737 int row;
4738
4739 if (win == curwin)
4740 {
4741 /* Always keep current window at least one line high, even when
4742 * 'winminheight' is zero. */
4743#ifdef FEAT_WINDOWS
4744 if (height < p_wmh)
4745 height = p_wmh;
4746#endif
4747 if (height == 0)
4748 height = 1;
4749 }
4750
4751#ifdef FEAT_WINDOWS
4752 frame_setheight(win->w_frame, height + win->w_status_height);
4753
4754 /* recompute the window positions */
4755 row = win_comp_pos();
4756#else
4757 if (height > topframe->fr_height)
4758 height = topframe->fr_height;
4759 win->w_height = height;
4760 row = height;
4761#endif
4762
4763 /*
4764 * If there is extra space created between the last window and the command
4765 * line, clear it.
4766 */
4767 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4768 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4769 cmdline_row = row;
4770 msg_row = row;
4771 msg_col = 0;
4772
4773 redraw_all_later(NOT_VALID);
4774}
4775
4776#if defined(FEAT_WINDOWS) || defined(PROTO)
4777
4778/*
4779 * Set the height of a frame to "height" and take care that all frames and
4780 * windows inside it are resized. Also resize frames on the left and right if
4781 * the are in the same FR_ROW frame.
4782 *
4783 * Strategy:
4784 * If the frame is part of a FR_COL frame, try fitting the frame in that
4785 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4786 * go to containing frames to resize them and make room.
4787 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4788 * Check for the minimal height of the FR_ROW frame.
4789 * At the top level we can also use change the command line height.
4790 */
4791 static void
4792frame_setheight(curfrp, height)
4793 frame_T *curfrp;
4794 int height;
4795{
4796 int room; /* total number of lines available */
4797 int take; /* number of lines taken from other windows */
4798 int room_cmdline; /* lines available from cmdline */
4799 int run;
4800 frame_T *frp;
4801 int h;
4802 int room_reserved;
4803
4804 /* If the height already is the desired value, nothing to do. */
4805 if (curfrp->fr_height == height)
4806 return;
4807
4808 if (curfrp->fr_parent == NULL)
4809 {
4810 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004811 if (height > ROWS_AVAIL)
4812 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 if (height > 0)
4814 frame_new_height(curfrp, height, FALSE, FALSE);
4815 }
4816 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4817 {
4818 /* Row of frames: Also need to resize frames left and right of this
4819 * one. First check for the minimal height of these. */
4820 h = frame_minheight(curfrp->fr_parent, NULL);
4821 if (height < h)
4822 height = h;
4823 frame_setheight(curfrp->fr_parent, height);
4824 }
4825 else
4826 {
4827 /*
4828 * Column of frames: try to change only frames in this column.
4829 */
4830#ifdef FEAT_VERTSPLIT
4831 /*
4832 * Do this twice:
4833 * 1: compute room available, if it's not enough try resizing the
4834 * containing frame.
4835 * 2: compute the room available and adjust the height to it.
4836 * Try not to reduce the height of a window with 'winfixheight' set.
4837 */
4838 for (run = 1; run <= 2; ++run)
4839#else
4840 for (;;)
4841#endif
4842 {
4843 room = 0;
4844 room_reserved = 0;
4845 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4846 frp = frp->fr_next)
4847 {
4848 if (frp != curfrp
4849 && frp->fr_win != NULL
4850 && frp->fr_win->w_p_wfh)
4851 room_reserved += frp->fr_height;
4852 room += frp->fr_height;
4853 if (frp != curfrp)
4854 room -= frame_minheight(frp, NULL);
4855 }
4856#ifdef FEAT_VERTSPLIT
4857 if (curfrp->fr_width != Columns)
4858 room_cmdline = 0;
4859 else
4860#endif
4861 {
4862 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4863 + lastwin->w_height + lastwin->w_status_height);
4864 if (room_cmdline < 0)
4865 room_cmdline = 0;
4866 }
4867
4868 if (height <= room + room_cmdline)
4869 break;
4870#ifdef FEAT_VERTSPLIT
4871 if (run == 2 || curfrp->fr_width == Columns)
4872#endif
4873 {
4874 if (height > room + room_cmdline)
4875 height = room + room_cmdline;
4876 break;
4877 }
4878#ifdef FEAT_VERTSPLIT
4879 frame_setheight(curfrp->fr_parent, height
4880 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4881#endif
4882 /*NOTREACHED*/
4883 }
4884
4885 /*
4886 * Compute the number of lines we will take from others frames (can be
4887 * negative!).
4888 */
4889 take = height - curfrp->fr_height;
4890
4891 /* If there is not enough room, also reduce the height of a window
4892 * with 'winfixheight' set. */
4893 if (height > room + room_cmdline - room_reserved)
4894 room_reserved = room + room_cmdline - height;
4895 /* If there is only a 'winfixheight' window and making the
4896 * window smaller, need to make the other window taller. */
4897 if (take < 0 && room - curfrp->fr_height < room_reserved)
4898 room_reserved = 0;
4899
4900 if (take > 0 && room_cmdline > 0)
4901 {
4902 /* use lines from cmdline first */
4903 if (take < room_cmdline)
4904 room_cmdline = take;
4905 take -= room_cmdline;
4906 topframe->fr_height += room_cmdline;
4907 }
4908
4909 /*
4910 * set the current frame to the new height
4911 */
4912 frame_new_height(curfrp, height, FALSE, FALSE);
4913
4914 /*
4915 * First take lines from the frames after the current frame. If
4916 * that is not enough, takes lines from frames above the current
4917 * frame.
4918 */
4919 for (run = 0; run < 2; ++run)
4920 {
4921 if (run == 0)
4922 frp = curfrp->fr_next; /* 1st run: start with next window */
4923 else
4924 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4925 while (frp != NULL && take != 0)
4926 {
4927 h = frame_minheight(frp, NULL);
4928 if (room_reserved > 0
4929 && frp->fr_win != NULL
4930 && frp->fr_win->w_p_wfh)
4931 {
4932 if (room_reserved >= frp->fr_height)
4933 room_reserved -= frp->fr_height;
4934 else
4935 {
4936 if (frp->fr_height - room_reserved > take)
4937 room_reserved = frp->fr_height - take;
4938 take -= frp->fr_height - room_reserved;
4939 frame_new_height(frp, room_reserved, FALSE, FALSE);
4940 room_reserved = 0;
4941 }
4942 }
4943 else
4944 {
4945 if (frp->fr_height - take < h)
4946 {
4947 take -= frp->fr_height - h;
4948 frame_new_height(frp, h, FALSE, FALSE);
4949 }
4950 else
4951 {
4952 frame_new_height(frp, frp->fr_height - take,
4953 FALSE, FALSE);
4954 take = 0;
4955 }
4956 }
4957 if (run == 0)
4958 frp = frp->fr_next;
4959 else
4960 frp = frp->fr_prev;
4961 }
4962 }
4963 }
4964}
4965
4966#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4967/*
4968 * Set current window width and take care of repositioning other windows to
4969 * fit around it.
4970 */
4971 void
4972win_setwidth(width)
4973 int width;
4974{
4975 win_setwidth_win(width, curwin);
4976}
4977
4978 void
4979win_setwidth_win(width, wp)
4980 int width;
4981 win_T *wp;
4982{
4983 /* Always keep current window at least one column wide, even when
4984 * 'winminwidth' is zero. */
4985 if (wp == curwin)
4986 {
4987 if (width < p_wmw)
4988 width = p_wmw;
4989 if (width == 0)
4990 width = 1;
4991 }
4992
4993 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4994
4995 /* recompute the window positions */
4996 (void)win_comp_pos();
4997
4998 redraw_all_later(NOT_VALID);
4999}
5000
5001/*
5002 * Set the width of a frame to "width" and take care that all frames and
5003 * windows inside it are resized. Also resize frames above and below if the
5004 * are in the same FR_ROW frame.
5005 *
5006 * Strategy is similar to frame_setheight().
5007 */
5008 static void
5009frame_setwidth(curfrp, width)
5010 frame_T *curfrp;
5011 int width;
5012{
5013 int room; /* total number of lines available */
5014 int take; /* number of lines taken from other windows */
5015 int run;
5016 frame_T *frp;
5017 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005018 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019
5020 /* If the width already is the desired value, nothing to do. */
5021 if (curfrp->fr_width == width)
5022 return;
5023
5024 if (curfrp->fr_parent == NULL)
5025 /* topframe: can't change width */
5026 return;
5027
5028 if (curfrp->fr_parent->fr_layout == FR_COL)
5029 {
5030 /* Column of frames: Also need to resize frames above and below of
5031 * this one. First check for the minimal width of these. */
5032 w = frame_minwidth(curfrp->fr_parent, NULL);
5033 if (width < w)
5034 width = w;
5035 frame_setwidth(curfrp->fr_parent, width);
5036 }
5037 else
5038 {
5039 /*
5040 * Row of frames: try to change only frames in this row.
5041 *
5042 * Do this twice:
5043 * 1: compute room available, if it's not enough try resizing the
5044 * containing frame.
5045 * 2: compute the room available and adjust the width to it.
5046 */
5047 for (run = 1; run <= 2; ++run)
5048 {
5049 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005050 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5052 frp = frp->fr_next)
5053 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005054 if (frp != curfrp
5055 && frp->fr_win != NULL
5056 && frp->fr_win->w_p_wfw)
5057 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 room += frp->fr_width;
5059 if (frp != curfrp)
5060 room -= frame_minwidth(frp, NULL);
5061 }
5062
5063 if (width <= room)
5064 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005065 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
5067 if (width > room)
5068 width = room;
5069 break;
5070 }
5071 frame_setwidth(curfrp->fr_parent, width
5072 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5073 }
5074
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 /*
5076 * Compute the number of lines we will take from others frames (can be
5077 * negative!).
5078 */
5079 take = width - curfrp->fr_width;
5080
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005081 /* If there is not enough room, also reduce the width of a window
5082 * with 'winfixwidth' set. */
5083 if (width > room - room_reserved)
5084 room_reserved = room - width;
5085 /* If there is only a 'winfixwidth' window and making the
5086 * window smaller, need to make the other window narrower. */
5087 if (take < 0 && room - curfrp->fr_width < room_reserved)
5088 room_reserved = 0;
5089
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 /*
5091 * set the current frame to the new width
5092 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005093 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094
5095 /*
5096 * First take lines from the frames right of the current frame. If
5097 * that is not enough, takes lines from frames left of the current
5098 * frame.
5099 */
5100 for (run = 0; run < 2; ++run)
5101 {
5102 if (run == 0)
5103 frp = curfrp->fr_next; /* 1st run: start with next window */
5104 else
5105 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5106 while (frp != NULL && take != 0)
5107 {
5108 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005109 if (room_reserved > 0
5110 && frp->fr_win != NULL
5111 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005113 if (room_reserved >= frp->fr_width)
5114 room_reserved -= frp->fr_width;
5115 else
5116 {
5117 if (frp->fr_width - room_reserved > take)
5118 room_reserved = frp->fr_width - take;
5119 take -= frp->fr_width - room_reserved;
5120 frame_new_width(frp, room_reserved, FALSE, FALSE);
5121 room_reserved = 0;
5122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
5124 else
5125 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005126 if (frp->fr_width - take < w)
5127 {
5128 take -= frp->fr_width - w;
5129 frame_new_width(frp, w, FALSE, FALSE);
5130 }
5131 else
5132 {
5133 frame_new_width(frp, frp->fr_width - take,
5134 FALSE, FALSE);
5135 take = 0;
5136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 }
5138 if (run == 0)
5139 frp = frp->fr_next;
5140 else
5141 frp = frp->fr_prev;
5142 }
5143 }
5144 }
5145}
5146#endif /* FEAT_VERTSPLIT */
5147
5148/*
5149 * Check 'winminheight' for a valid value.
5150 */
5151 void
5152win_setminheight()
5153{
5154 int room;
5155 int first = TRUE;
5156 win_T *wp;
5157
5158 /* loop until there is a 'winminheight' that is possible */
5159 while (p_wmh > 0)
5160 {
5161 /* TODO: handle vertical splits */
5162 room = -p_wh;
5163 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5164 room += wp->w_height - p_wmh;
5165 if (room >= 0)
5166 break;
5167 --p_wmh;
5168 if (first)
5169 {
5170 EMSG(_(e_noroom));
5171 first = FALSE;
5172 }
5173 }
5174}
5175
5176#ifdef FEAT_MOUSE
5177
5178/*
5179 * Status line of dragwin is dragged "offset" lines down (negative is up).
5180 */
5181 void
5182win_drag_status_line(dragwin, offset)
5183 win_T *dragwin;
5184 int offset;
5185{
5186 frame_T *curfr;
5187 frame_T *fr;
5188 int room;
5189 int row;
5190 int up; /* if TRUE, drag status line up, otherwise down */
5191 int n;
5192
5193 fr = dragwin->w_frame;
5194 curfr = fr;
5195 if (fr != topframe) /* more than one window */
5196 {
5197 fr = fr->fr_parent;
5198 /* When the parent frame is not a column of frames, its parent should
5199 * be. */
5200 if (fr->fr_layout != FR_COL)
5201 {
5202 curfr = fr;
5203 if (fr != topframe) /* only a row of windows, may drag statusline */
5204 fr = fr->fr_parent;
5205 }
5206 }
5207
5208 /* If this is the last frame in a column, may want to resize the parent
5209 * frame instead (go two up to skip a row of frames). */
5210 while (curfr != topframe && curfr->fr_next == NULL)
5211 {
5212 if (fr != topframe)
5213 fr = fr->fr_parent;
5214 curfr = fr;
5215 if (fr != topframe)
5216 fr = fr->fr_parent;
5217 }
5218
5219 if (offset < 0) /* drag up */
5220 {
5221 up = TRUE;
5222 offset = -offset;
5223 /* sum up the room of the current frame and above it */
5224 if (fr == curfr)
5225 {
5226 /* only one window */
5227 room = fr->fr_height - frame_minheight(fr, NULL);
5228 }
5229 else
5230 {
5231 room = 0;
5232 for (fr = fr->fr_child; ; fr = fr->fr_next)
5233 {
5234 room += fr->fr_height - frame_minheight(fr, NULL);
5235 if (fr == curfr)
5236 break;
5237 }
5238 }
5239 fr = curfr->fr_next; /* put fr at frame that grows */
5240 }
5241 else /* drag down */
5242 {
5243 up = FALSE;
5244 /*
5245 * Only dragging the last status line can reduce p_ch.
5246 */
5247 room = Rows - cmdline_row;
5248 if (curfr->fr_next == NULL)
5249 room -= 1;
5250 else
5251 room -= p_ch;
5252 if (room < 0)
5253 room = 0;
5254 /* sum up the room of frames below of the current one */
5255 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5256 room += fr->fr_height - frame_minheight(fr, NULL);
5257 fr = curfr; /* put fr at window that grows */
5258 }
5259
5260 if (room < offset) /* Not enough room */
5261 offset = room; /* Move as far as we can */
5262 if (offset <= 0)
5263 return;
5264
5265 /*
5266 * Grow frame fr by "offset" lines.
5267 * Doesn't happen when dragging the last status line up.
5268 */
5269 if (fr != NULL)
5270 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5271
5272 if (up)
5273 fr = curfr; /* current frame gets smaller */
5274 else
5275 fr = curfr->fr_next; /* next frame gets smaller */
5276
5277 /*
5278 * Now make the other frames smaller.
5279 */
5280 while (fr != NULL && offset > 0)
5281 {
5282 n = frame_minheight(fr, NULL);
5283 if (fr->fr_height - offset <= n)
5284 {
5285 offset -= fr->fr_height - n;
5286 frame_new_height(fr, n, !up, FALSE);
5287 }
5288 else
5289 {
5290 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5291 break;
5292 }
5293 if (up)
5294 fr = fr->fr_prev;
5295 else
5296 fr = fr->fr_next;
5297 }
5298 row = win_comp_pos();
5299 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5300 cmdline_row = row;
5301 p_ch = Rows - cmdline_row;
5302 if (p_ch < 1)
5303 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005304 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005305 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 showmode();
5307}
5308
5309#ifdef FEAT_VERTSPLIT
5310/*
5311 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5312 */
5313 void
5314win_drag_vsep_line(dragwin, offset)
5315 win_T *dragwin;
5316 int offset;
5317{
5318 frame_T *curfr;
5319 frame_T *fr;
5320 int room;
5321 int left; /* if TRUE, drag separator line left, otherwise right */
5322 int n;
5323
5324 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005325 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 return;
5327 curfr = fr;
5328 fr = fr->fr_parent;
5329 /* When the parent frame is not a row of frames, its parent should be. */
5330 if (fr->fr_layout != FR_ROW)
5331 {
5332 if (fr == topframe) /* only a column of windows (cannot happen?) */
5333 return;
5334 curfr = fr;
5335 fr = fr->fr_parent;
5336 }
5337
5338 /* If this is the last frame in a row, may want to resize a parent
5339 * frame instead. */
5340 while (curfr->fr_next == NULL)
5341 {
5342 if (fr == topframe)
5343 break;
5344 curfr = fr;
5345 fr = fr->fr_parent;
5346 if (fr != topframe)
5347 {
5348 curfr = fr;
5349 fr = fr->fr_parent;
5350 }
5351 }
5352
5353 if (offset < 0) /* drag left */
5354 {
5355 left = TRUE;
5356 offset = -offset;
5357 /* sum up the room of the current frame and left of it */
5358 room = 0;
5359 for (fr = fr->fr_child; ; fr = fr->fr_next)
5360 {
5361 room += fr->fr_width - frame_minwidth(fr, NULL);
5362 if (fr == curfr)
5363 break;
5364 }
5365 fr = curfr->fr_next; /* put fr at frame that grows */
5366 }
5367 else /* drag right */
5368 {
5369 left = FALSE;
5370 /* sum up the room of frames right of the current one */
5371 room = 0;
5372 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5373 room += fr->fr_width - frame_minwidth(fr, NULL);
5374 fr = curfr; /* put fr at window that grows */
5375 }
5376
5377 if (room < offset) /* Not enough room */
5378 offset = room; /* Move as far as we can */
5379 if (offset <= 0) /* No room at all, quit. */
5380 return;
5381
5382 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005383 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384
5385 /* shrink other frames: current and at the left or at the right */
5386 if (left)
5387 fr = curfr; /* current frame gets smaller */
5388 else
5389 fr = curfr->fr_next; /* next frame gets smaller */
5390
5391 while (fr != NULL && offset > 0)
5392 {
5393 n = frame_minwidth(fr, NULL);
5394 if (fr->fr_width - offset <= n)
5395 {
5396 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005397 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 }
5399 else
5400 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005401 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 break;
5403 }
5404 if (left)
5405 fr = fr->fr_prev;
5406 else
5407 fr = fr->fr_next;
5408 }
5409 (void)win_comp_pos();
5410 redraw_all_later(NOT_VALID);
5411}
5412#endif /* FEAT_VERTSPLIT */
5413#endif /* FEAT_MOUSE */
5414
5415#endif /* FEAT_WINDOWS */
5416
5417/*
5418 * Set the height of a window.
5419 * This takes care of the things inside the window, not what happens to the
5420 * window position, the frame or to other windows.
5421 */
5422 static void
5423win_new_height(wp, height)
5424 win_T *wp;
5425 int height;
5426{
5427 linenr_T lnum;
5428 int sline, line_size;
5429#define FRACTION_MULT 16384L
5430
5431 /* Don't want a negative height. Happens when splitting a tiny window.
5432 * Will equalize heights soon to fix it. */
5433 if (height < 0)
5434 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005435 if (wp->w_height == height)
5436 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437
5438 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5439 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5440 + FRACTION_MULT / 2) / (long)wp->w_height;
5441
5442 wp->w_height = height;
5443 wp->w_skipcol = 0;
5444
5445 /* Don't change w_topline when height is zero. Don't set w_topline when
5446 * 'scrollbind' is set and this isn't the current window. */
5447 if (height > 0
5448#ifdef FEAT_SCROLLBIND
5449 && (!wp->w_p_scb || wp == curwin)
5450#endif
5451 )
5452 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005453 /*
5454 * Find a value for w_topline that shows the cursor at the same
5455 * relative position in the window as before (more or less).
5456 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 lnum = wp->w_cursor.lnum;
5458 if (lnum < 1) /* can happen when starting up */
5459 lnum = 1;
5460 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5461 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5462 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005463
5464 if (sline >= 0)
5465 {
5466 /* Make sure the whole cursor line is visible, if possible. */
5467 int rows = plines_win(wp, lnum, FALSE);
5468
5469 if (sline > wp->w_height - rows)
5470 {
5471 sline = wp->w_height - rows;
5472 wp->w_wrow -= rows - line_size;
5473 }
5474 }
5475
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 if (sline < 0)
5477 {
5478 /*
5479 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005480 * Make cursor line the first line in the window. If not enough
5481 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 */
5483 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005484 if (wp->w_wrow >= wp->w_height
5485 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5486 {
5487 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5488 --wp->w_wrow;
5489 while (wp->w_wrow >= wp->w_height)
5490 {
5491 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5492 + win_col_off2(wp);
5493 --wp->w_wrow;
5494 }
5495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
5497 else
5498 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005499 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 {
5501#ifdef FEAT_FOLDING
5502 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5503 if (lnum == 1)
5504 {
5505 /* first line in buffer is folded */
5506 line_size = 1;
5507 --sline;
5508 break;
5509 }
5510#endif
5511 --lnum;
5512#ifdef FEAT_DIFF
5513 if (lnum == wp->w_topline)
5514 line_size = plines_win_nofill(wp, lnum, TRUE)
5515 + wp->w_topfill;
5516 else
5517#endif
5518 line_size = plines_win(wp, lnum, TRUE);
5519 sline -= line_size;
5520 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005521
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 if (sline < 0)
5523 {
5524 /*
5525 * Line we want at top would go off top of screen. Use next
5526 * line instead.
5527 */
5528#ifdef FEAT_FOLDING
5529 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5530#endif
5531 lnum++;
5532 wp->w_wrow -= line_size + sline;
5533 }
5534 else if (sline > 0)
5535 {
5536 /* First line of file reached, use that as topline. */
5537 lnum = 1;
5538 wp->w_wrow -= sline;
5539 }
5540 }
5541 set_topline(wp, lnum);
5542 }
5543
5544 if (wp == curwin)
5545 {
5546 if (p_so)
5547 update_topline();
5548 curs_columns(FALSE); /* validate w_wrow */
5549 }
5550 wp->w_prev_fraction_row = wp->w_wrow;
5551
5552 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005553 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554#ifdef FEAT_WINDOWS
5555 wp->w_redr_status = TRUE;
5556#endif
5557 invalidate_botline_win(wp);
5558}
5559
5560#ifdef FEAT_VERTSPLIT
5561/*
5562 * Set the width of a window.
5563 */
5564 static void
5565win_new_width(wp, width)
5566 win_T *wp;
5567 int width;
5568{
5569 wp->w_width = width;
5570 wp->w_lines_valid = 0;
5571 changed_line_abv_curs_win(wp);
5572 invalidate_botline_win(wp);
5573 if (wp == curwin)
5574 {
5575 update_topline();
5576 curs_columns(TRUE); /* validate w_wrow */
5577 }
5578 redraw_win_later(wp, NOT_VALID);
5579 wp->w_redr_status = TRUE;
5580}
5581#endif
5582
5583 void
5584win_comp_scroll(wp)
5585 win_T *wp;
5586{
5587 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5588 if (wp->w_p_scr == 0)
5589 wp->w_p_scr = 1;
5590}
5591
5592/*
5593 * command_height: called whenever p_ch has been changed
5594 */
5595 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005596command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597{
5598#ifdef FEAT_WINDOWS
5599 int h;
5600 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005601 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005603 /* Use the value of p_ch that we remembered. This is needed for when the
5604 * GUI starts up, we can't be sure in what order things happen. And when
5605 * p_ch was changed in another tab page. */
5606 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005607
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 /* Find bottom frame with width of screen. */
5609 frp = lastwin->w_frame;
5610# ifdef FEAT_VERTSPLIT
5611 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5612 frp = frp->fr_parent;
5613# endif
5614
5615 /* Avoid changing the height of a window with 'winfixheight' set. */
5616 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5617 && frp->fr_win->w_p_wfh)
5618 frp = frp->fr_prev;
5619
5620 if (starting != NO_SCREEN)
5621 {
5622 cmdline_row = Rows - p_ch;
5623
5624 if (p_ch > old_p_ch) /* p_ch got bigger */
5625 {
5626 while (p_ch > old_p_ch)
5627 {
5628 if (frp == NULL)
5629 {
5630 EMSG(_(e_noroom));
5631 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005632 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 cmdline_row = Rows - p_ch;
5634 break;
5635 }
5636 h = frp->fr_height - frame_minheight(frp, NULL);
5637 if (h > p_ch - old_p_ch)
5638 h = p_ch - old_p_ch;
5639 old_p_ch += h;
5640 frame_add_height(frp, -h);
5641 frp = frp->fr_prev;
5642 }
5643
5644 /* Recompute window positions. */
5645 (void)win_comp_pos();
5646
5647 /* clear the lines added to cmdline */
5648 if (full_screen)
5649 screen_fill((int)(cmdline_row), (int)Rows, 0,
5650 (int)Columns, ' ', ' ', 0);
5651 msg_row = cmdline_row;
5652 redraw_cmdline = TRUE;
5653 return;
5654 }
5655
5656 if (msg_row < cmdline_row)
5657 msg_row = cmdline_row;
5658 redraw_cmdline = TRUE;
5659 }
5660 frame_add_height(frp, (int)(old_p_ch - p_ch));
5661
5662 /* Recompute window positions. */
5663 if (frp != lastwin->w_frame)
5664 (void)win_comp_pos();
5665#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005667 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668#endif
5669}
5670
5671#if defined(FEAT_WINDOWS) || defined(PROTO)
5672/*
5673 * Resize frame "frp" to be "n" lines higher (negative for less high).
5674 * Also resize the frames it is contained in.
5675 */
5676 static void
5677frame_add_height(frp, n)
5678 frame_T *frp;
5679 int n;
5680{
5681 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5682 for (;;)
5683 {
5684 frp = frp->fr_parent;
5685 if (frp == NULL)
5686 break;
5687 frp->fr_height += n;
5688 }
5689}
5690
5691/*
5692 * Add or remove a status line for the bottom window(s), according to the
5693 * value of 'laststatus'.
5694 */
5695 void
5696last_status(morewin)
5697 int morewin; /* pretend there are two or more windows */
5698{
5699 /* Don't make a difference between horizontal or vertical split. */
5700 last_status_rec(topframe, (p_ls == 2
5701 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5702}
5703
5704 static void
5705last_status_rec(fr, statusline)
5706 frame_T *fr;
5707 int statusline;
5708{
5709 frame_T *fp;
5710 win_T *wp;
5711
5712 if (fr->fr_layout == FR_LEAF)
5713 {
5714 wp = fr->fr_win;
5715 if (wp->w_status_height != 0 && !statusline)
5716 {
5717 /* remove status line */
5718 win_new_height(wp, wp->w_height + 1);
5719 wp->w_status_height = 0;
5720 comp_col();
5721 }
5722 else if (wp->w_status_height == 0 && statusline)
5723 {
5724 /* Find a frame to take a line from. */
5725 fp = fr;
5726 while (fp->fr_height <= frame_minheight(fp, NULL))
5727 {
5728 if (fp == topframe)
5729 {
5730 EMSG(_(e_noroom));
5731 return;
5732 }
5733 /* In a column of frames: go to frame above. If already at
5734 * the top or in a row of frames: go to parent. */
5735 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5736 fp = fp->fr_prev;
5737 else
5738 fp = fp->fr_parent;
5739 }
5740 wp->w_status_height = 1;
5741 if (fp != fr)
5742 {
5743 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5744 frame_fix_height(wp);
5745 (void)win_comp_pos();
5746 }
5747 else
5748 win_new_height(wp, wp->w_height - 1);
5749 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005750 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 }
5752 }
5753#ifdef FEAT_VERTSPLIT
5754 else if (fr->fr_layout == FR_ROW)
5755 {
5756 /* vertically split windows, set status line for each one */
5757 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5758 last_status_rec(fp, statusline);
5759 }
5760#endif
5761 else
5762 {
5763 /* horizontally split window, set status line for last one */
5764 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5765 ;
5766 last_status_rec(fp, statusline);
5767 }
5768}
5769
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005770/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005771 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005772 */
5773 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005774tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005775{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005776#ifdef FEAT_GUI_TABLINE
5777 /* When the GUI has the tabline then this always returns zero. */
5778 if (gui_use_tabline())
5779 return 0;
5780#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005781 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005782 {
5783 case 0: return 0;
5784 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5785 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005786 return 1;
5787}
5788
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789#endif /* FEAT_WINDOWS */
5790
5791#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5792/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005793 * Get the file name at the cursor.
5794 * If Visual mode is active, use the selected text if it's in one line.
5795 * Returns the name in allocated memory, NULL for failure.
5796 */
5797 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005798grab_file_name(count, file_lnum)
5799 long count;
5800 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005801{
5802# ifdef FEAT_VISUAL
5803 if (VIsual_active)
5804 {
5805 int len;
5806 char_u *ptr;
5807
5808 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5809 return NULL;
5810 return find_file_name_in_path(ptr, len,
5811 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5812 }
5813# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005814 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5815 file_lnum);
5816
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005817}
5818
5819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 * Return the file name under or after the cursor.
5821 *
5822 * The 'path' option is searched if the file name is not absolute.
5823 * The string returned has been alloc'ed and should be freed by the caller.
5824 * NULL is returned if the file name or file is not found.
5825 *
5826 * options:
5827 * FNAME_MESS give error messages
5828 * FNAME_EXP expand to path
5829 * FNAME_HYP check for hypertext link
5830 * FNAME_INCL apply "includeexpr"
5831 */
5832 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005833file_name_at_cursor(options, count, file_lnum)
5834 int options;
5835 long count;
5836 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837{
5838 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005839 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5840 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841}
5842
5843/*
5844 * Return the name of the file under or after ptr[col].
5845 * Otherwise like file_name_at_cursor().
5846 */
5847 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005848file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 char_u *line;
5850 int col;
5851 int options;
5852 long count;
5853 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005854 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855{
5856 char_u *ptr;
5857 int len;
5858
5859 /*
5860 * search forward for what could be the start of a file name
5861 */
5862 ptr = line + col;
5863 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005864 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 if (*ptr == NUL) /* nothing found */
5866 {
5867 if (options & FNAME_MESS)
5868 EMSG(_("E446: No file name under cursor"));
5869 return NULL;
5870 }
5871
5872 /*
5873 * Search backward for first char of the file name.
5874 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5875 */
5876 while (ptr > line)
5877 {
5878#ifdef FEAT_MBYTE
5879 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5880 ptr -= len + 1;
5881 else
5882#endif
5883 if (vim_isfilec(ptr[-1])
5884 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5885 --ptr;
5886 else
5887 break;
5888 }
5889
5890 /*
5891 * Search forward for the last char of the file name.
5892 * Also allow "://" when ':' is not in 'isfname'.
5893 */
5894 len = 0;
5895 while (vim_isfilec(ptr[len])
5896 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5897#ifdef FEAT_MBYTE
5898 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005899 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 else
5901#endif
5902 ++len;
5903
5904 /*
5905 * If there is trailing punctuation, remove it.
5906 * But don't remove "..", could be a directory name.
5907 */
5908 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5909 && ptr[len - 2] != '.')
5910 --len;
5911
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005912 if (file_lnum != NULL)
5913 {
5914 char_u *p;
5915
5916 /* Get the number after the file name and a separator character */
5917 p = ptr + len;
5918 p = skipwhite(p);
5919 if (*p != NUL)
5920 {
5921 if (!isdigit(*p))
5922 ++p; /* skip the separator */
5923 p = skipwhite(p);
5924 if (isdigit(*p))
5925 *file_lnum = (int)getdigits(&p);
5926 }
5927 }
5928
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5930}
5931
5932# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5933static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5934
5935 static char_u *
5936eval_includeexpr(ptr, len)
5937 char_u *ptr;
5938 int len;
5939{
5940 char_u *res;
5941
5942 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005943 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005944 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 set_vim_var_string(VV_FNAME, NULL, 0);
5946 return res;
5947}
5948#endif
5949
5950/*
5951 * Return the name of the file ptr[len] in 'path'.
5952 * Otherwise like file_name_at_cursor().
5953 */
5954 char_u *
5955find_file_name_in_path(ptr, len, options, count, rel_fname)
5956 char_u *ptr;
5957 int len;
5958 int options;
5959 long count;
5960 char_u *rel_fname; /* file we are searching relative to */
5961{
5962 char_u *file_name;
5963 int c;
5964# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5965 char_u *tofree = NULL;
5966
5967 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5968 {
5969 tofree = eval_includeexpr(ptr, len);
5970 if (tofree != NULL)
5971 {
5972 ptr = tofree;
5973 len = (int)STRLEN(ptr);
5974 }
5975 }
5976# endif
5977
5978 if (options & FNAME_EXP)
5979 {
5980 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5981 TRUE, rel_fname);
5982
5983# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5984 /*
5985 * If the file could not be found in a normal way, try applying
5986 * 'includeexpr' (unless done already).
5987 */
5988 if (file_name == NULL
5989 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5990 {
5991 tofree = eval_includeexpr(ptr, len);
5992 if (tofree != NULL)
5993 {
5994 ptr = tofree;
5995 len = (int)STRLEN(ptr);
5996 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5997 TRUE, rel_fname);
5998 }
5999 }
6000# endif
6001 if (file_name == NULL && (options & FNAME_MESS))
6002 {
6003 c = ptr[len];
6004 ptr[len] = NUL;
6005 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6006 ptr[len] = c;
6007 }
6008
6009 /* Repeat finding the file "count" times. This matters when it
6010 * appears several times in the path. */
6011 while (file_name != NULL && --count > 0)
6012 {
6013 vim_free(file_name);
6014 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6015 }
6016 }
6017 else
6018 file_name = vim_strnsave(ptr, len);
6019
6020# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6021 vim_free(tofree);
6022# endif
6023
6024 return file_name;
6025}
6026#endif /* FEAT_SEARCHPATH */
6027
6028/*
6029 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6030 * Also check for ":\\", which MS Internet Explorer accepts, return
6031 * URL_BACKSLASH.
6032 */
6033 static int
6034path_is_url(p)
6035 char_u *p;
6036{
6037 if (STRNCMP(p, "://", (size_t)3) == 0)
6038 return URL_SLASH;
6039 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6040 return URL_BACKSLASH;
6041 return 0;
6042}
6043
6044/*
6045 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6046 * Return URL_BACKSLASH for "name:\\".
6047 * Return zero otherwise.
6048 */
6049 int
6050path_with_url(fname)
6051 char_u *fname;
6052{
6053 char_u *p;
6054
6055 for (p = fname; isalpha(*p); ++p)
6056 ;
6057 return path_is_url(p);
6058}
6059
6060/*
6061 * Return TRUE if "name" is a full (absolute) path name or URL.
6062 */
6063 int
6064vim_isAbsName(name)
6065 char_u *name;
6066{
6067 return (path_with_url(name) != 0 || mch_isFullName(name));
6068}
6069
6070/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006071 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 *
6073 * return FAIL for failure, OK otherwise
6074 */
6075 int
6076vim_FullName(fname, buf, len, force)
6077 char_u *fname, *buf;
6078 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006079 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080{
6081 int retval = OK;
6082 int url;
6083
6084 *buf = NUL;
6085 if (fname == NULL)
6086 return FAIL;
6087
6088 url = path_with_url(fname);
6089 if (!url)
6090 retval = mch_FullName(fname, buf, len, force);
6091 if (url || retval == FAIL)
6092 {
6093 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006094 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 }
6096#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6097 slash_adjust(buf);
6098#endif
6099 return retval;
6100}
6101
6102/*
6103 * Return the minimal number of rows that is needed on the screen to display
6104 * the current number of windows.
6105 */
6106 int
6107min_rows()
6108{
6109 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006110#ifdef FEAT_WINDOWS
6111 tabpage_T *tp;
6112 int n;
6113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114
6115 if (firstwin == NULL) /* not initialized yet */
6116 return MIN_LINES;
6117
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006119 total = 0;
6120 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6121 {
6122 n = frame_minheight(tp->tp_topframe, NULL);
6123 if (total < n)
6124 total = n;
6125 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006126 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006128 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006130 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 return total;
6132}
6133
6134/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006135 * Return TRUE if there is only one window (in the current tab page), not
6136 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006137 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 */
6139 int
6140only_one_window()
6141{
6142#ifdef FEAT_WINDOWS
6143 int count = 0;
6144 win_T *wp;
6145
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006146 /* If there is another tab page there always is another window. */
6147 if (first_tabpage->tp_next != NULL)
6148 return FALSE;
6149
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006151 if ((!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152# ifdef FEAT_QUICKFIX
6153 || wp->w_p_pvw
6154# endif
6155 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006156# ifdef FEAT_AUTOCMD
6157 && wp != aucmd_win
6158# endif
6159 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 ++count;
6161 return (count <= 1);
6162#else
6163 return TRUE;
6164#endif
6165}
6166
6167#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6168/*
6169 * Correct the cursor line number in other windows. Used after changing the
6170 * current buffer, and before applying autocommands.
6171 * When "do_curwin" is TRUE, also check current window.
6172 */
6173 void
6174check_lnums(do_curwin)
6175 int do_curwin;
6176{
6177 win_T *wp;
6178
6179#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006180 tabpage_T *tp;
6181
6182 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6184#else
6185 wp = curwin;
6186 if (do_curwin)
6187#endif
6188 {
6189 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6190 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6191 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6192 wp->w_topline = curbuf->b_ml.ml_line_count;
6193 }
6194}
6195#endif
6196
6197#if defined(FEAT_WINDOWS) || defined(PROTO)
6198
6199/*
6200 * A snapshot of the window sizes, to restore them after closing the help
6201 * window.
6202 * Only these fields are used:
6203 * fr_layout
6204 * fr_width
6205 * fr_height
6206 * fr_next
6207 * fr_child
6208 * fr_win (only valid for the old curwin, NULL otherwise)
6209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210
6211/*
6212 * Create a snapshot of the current frame sizes.
6213 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006214 void
6215make_snapshot(idx)
6216 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006218 clear_snapshot(curtab, idx);
6219 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220}
6221
6222 static void
6223make_snapshot_rec(fr, frp)
6224 frame_T *fr;
6225 frame_T **frp;
6226{
6227 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6228 if (*frp == NULL)
6229 return;
6230 (*frp)->fr_layout = fr->fr_layout;
6231# ifdef FEAT_VERTSPLIT
6232 (*frp)->fr_width = fr->fr_width;
6233# endif
6234 (*frp)->fr_height = fr->fr_height;
6235 if (fr->fr_next != NULL)
6236 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6237 if (fr->fr_child != NULL)
6238 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6239 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6240 (*frp)->fr_win = curwin;
6241}
6242
6243/*
6244 * Remove any existing snapshot.
6245 */
6246 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006247clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006248 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006249 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006251 clear_snapshot_rec(tp->tp_snapshot[idx]);
6252 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253}
6254
6255 static void
6256clear_snapshot_rec(fr)
6257 frame_T *fr;
6258{
6259 if (fr != NULL)
6260 {
6261 clear_snapshot_rec(fr->fr_next);
6262 clear_snapshot_rec(fr->fr_child);
6263 vim_free(fr);
6264 }
6265}
6266
6267/*
6268 * Restore a previously created snapshot, if there is any.
6269 * This is only done if the screen size didn't change and the window layout is
6270 * still the same.
6271 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006272 void
6273restore_snapshot(idx, close_curwin)
6274 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 int close_curwin; /* closing current window */
6276{
6277 win_T *wp;
6278
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006279 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006281 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006283 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6284 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006286 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 win_comp_pos();
6288 if (wp != NULL && close_curwin)
6289 win_goto(wp);
6290 redraw_all_later(CLEAR);
6291 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006292 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293}
6294
6295/*
6296 * Check if frames "sn" and "fr" have the same layout, same following frames
6297 * and same children.
6298 */
6299 static int
6300check_snapshot_rec(sn, fr)
6301 frame_T *sn;
6302 frame_T *fr;
6303{
6304 if (sn->fr_layout != fr->fr_layout
6305 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6306 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6307 || (sn->fr_next != NULL
6308 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6309 || (sn->fr_child != NULL
6310 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6311 return FAIL;
6312 return OK;
6313}
6314
6315/*
6316 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6317 * following frames and children.
6318 * Returns a pointer to the old current window, or NULL.
6319 */
6320 static win_T *
6321restore_snapshot_rec(sn, fr)
6322 frame_T *sn;
6323 frame_T *fr;
6324{
6325 win_T *wp = NULL;
6326 win_T *wp2;
6327
6328 fr->fr_height = sn->fr_height;
6329# ifdef FEAT_VERTSPLIT
6330 fr->fr_width = sn->fr_width;
6331# endif
6332 if (fr->fr_layout == FR_LEAF)
6333 {
6334 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6335# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006336 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337# endif
6338 wp = sn->fr_win;
6339 }
6340 if (sn->fr_next != NULL)
6341 {
6342 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6343 if (wp2 != NULL)
6344 wp = wp2;
6345 }
6346 if (sn->fr_child != NULL)
6347 {
6348 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6349 if (wp2 != NULL)
6350 wp = wp2;
6351 }
6352 return wp;
6353}
6354
6355#endif
6356
6357#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6358/*
6359 * Return TRUE if there is any vertically split window.
6360 */
6361 int
6362win_hasvertsplit()
6363{
6364 frame_T *fr;
6365
6366 if (topframe->fr_layout == FR_ROW)
6367 return TRUE;
6368
6369 if (topframe->fr_layout == FR_COL)
6370 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6371 if (fr->fr_layout == FR_ROW)
6372 return TRUE;
6373
6374 return FALSE;
6375}
6376#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006377
6378#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6379/*
6380 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006381 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006382 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6383 * If no particular ID is desired, -1 must be specified for 'id'.
6384 * Return ID of added match, -1 on failure.
6385 */
6386 int
6387match_add(wp, grp, pat, prio, id)
6388 win_T *wp;
6389 char_u *grp;
6390 char_u *pat;
6391 int prio;
6392 int id;
6393{
6394 matchitem_T *cur;
6395 matchitem_T *prev;
6396 matchitem_T *m;
6397 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006398 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006399
6400 if (*grp == NUL || *pat == NUL)
6401 return -1;
6402 if (id < -1 || id == 0)
6403 {
6404 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6405 return -1;
6406 }
6407 if (id != -1)
6408 {
6409 cur = wp->w_match_head;
6410 while (cur != NULL)
6411 {
6412 if (cur->id == id)
6413 {
6414 EMSGN("E801: ID already taken: %ld", id);
6415 return -1;
6416 }
6417 cur = cur->next;
6418 }
6419 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006420 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006421 {
6422 EMSG2(_(e_nogroup), grp);
6423 return -1;
6424 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006425 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006426 {
6427 EMSG2(_(e_invarg2), pat);
6428 return -1;
6429 }
6430
6431 /* Find available match ID. */
6432 while (id == -1)
6433 {
6434 cur = wp->w_match_head;
6435 while (cur != NULL && cur->id != wp->w_next_match_id)
6436 cur = cur->next;
6437 if (cur == NULL)
6438 id = wp->w_next_match_id;
6439 wp->w_next_match_id++;
6440 }
6441
6442 /* Build new match. */
6443 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6444 m->id = id;
6445 m->priority = prio;
6446 m->pattern = vim_strsave(pat);
6447 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006448 m->match.regprog = regprog;
6449 m->match.rmm_ic = FALSE;
6450 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006451
6452 /* Insert new match. The match list is in ascending order with regard to
6453 * the match priorities. */
6454 cur = wp->w_match_head;
6455 prev = cur;
6456 while (cur != NULL && prio >= cur->priority)
6457 {
6458 prev = cur;
6459 cur = cur->next;
6460 }
6461 if (cur == prev)
6462 wp->w_match_head = m;
6463 else
6464 prev->next = m;
6465 m->next = cur;
6466
6467 redraw_later(SOME_VALID);
6468 return id;
6469}
6470
6471/*
6472 * Delete match with ID 'id' in the match list of window 'wp'.
6473 * Print error messages if 'perr' is TRUE.
6474 */
6475 int
6476match_delete(wp, id, perr)
6477 win_T *wp;
6478 int id;
6479 int perr;
6480{
6481 matchitem_T *cur = wp->w_match_head;
6482 matchitem_T *prev = cur;
6483
6484 if (id < 1)
6485 {
6486 if (perr == TRUE)
6487 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6488 id);
6489 return -1;
6490 }
6491 while (cur != NULL && cur->id != id)
6492 {
6493 prev = cur;
6494 cur = cur->next;
6495 }
6496 if (cur == NULL)
6497 {
6498 if (perr == TRUE)
6499 EMSGN("E803: ID not found: %ld", id);
6500 return -1;
6501 }
6502 if (cur == prev)
6503 wp->w_match_head = cur->next;
6504 else
6505 prev->next = cur->next;
6506 vim_free(cur->match.regprog);
6507 vim_free(cur->pattern);
6508 vim_free(cur);
6509 redraw_later(SOME_VALID);
6510 return 0;
6511}
6512
6513/*
6514 * Delete all matches in the match list of window 'wp'.
6515 */
6516 void
6517clear_matches(wp)
6518 win_T *wp;
6519{
6520 matchitem_T *m;
6521
6522 while (wp->w_match_head != NULL)
6523 {
6524 m = wp->w_match_head->next;
6525 vim_free(wp->w_match_head->match.regprog);
6526 vim_free(wp->w_match_head->pattern);
6527 vim_free(wp->w_match_head);
6528 wp->w_match_head = m;
6529 }
6530 redraw_later(SOME_VALID);
6531}
6532
6533/*
6534 * Get match from ID 'id' in window 'wp'.
6535 * Return NULL if match not found.
6536 */
6537 matchitem_T *
6538get_match(wp, id)
6539 win_T *wp;
6540 int id;
6541{
6542 matchitem_T *cur = wp->w_match_head;
6543
6544 while (cur != NULL && cur->id != id)
6545 cur = cur->next;
6546 return cur;
6547}
6548#endif