blob: 09c638e0ce0e2222c7e2c1f90673034ea1d1b283 [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
12#ifdef HAVE_FCNTL_H
13# include <fcntl.h> /* for chdir() */
14#endif
15
16static int path_is_url __ARGS((char_u *p));
17#if defined(FEAT_WINDOWS) || defined(PROTO)
18static int win_split_ins __ARGS((int size, int flags, win_T *newwin, int dir));
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000019static void win_init __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
21static void frame_setheight __ARGS((frame_T *curfrp, int height));
22#ifdef FEAT_VERTSPLIT
23static void frame_setwidth __ARGS((frame_T *curfrp, int width));
24#endif
25static void win_exchange __ARGS((long));
26static void win_rotate __ARGS((int, int));
27static void win_totop __ARGS((int size, int flags));
28static 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 +000029static int last_window __ARGS((void));
Bram Moolenaarf740b292006-02-16 22:11:02 +000030static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
31static win_T *winframe_remove __ARGS((win_T *win, int *dirp, tabpage_T *tp));
32static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000033static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000034static win_T *frame2win __ARGS((frame_T *frp));
35static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
36static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
37static int frame_fixed_height __ARGS((frame_T *frp));
38#ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000039static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000040static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000041static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000042static void frame_add_vsep __ARGS((frame_T *frp));
43static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
44static void frame_fix_width __ARGS((win_T *wp));
45#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000046#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000047static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000048#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000049static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000050static int leave_tabpage __ARGS((buf_T *new_curbuf));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000051static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000052static void frame_fix_height __ARGS((win_T *wp));
53static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
54static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
Bram Moolenaarf740b292006-02-16 22:11:02 +000055static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000056static void win_append __ARGS((win_T *, win_T *));
Bram Moolenaarf740b292006-02-16 22:11:02 +000057static void win_remove __ARGS((win_T *, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static void frame_append __ARGS((frame_T *after, frame_T *frp));
59static void frame_insert __ARGS((frame_T *before, frame_T *frp));
60static void frame_remove __ARGS((frame_T *frp));
61#ifdef FEAT_VERTSPLIT
62static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000063static void win_goto_ver __ARGS((int up, long count));
64static void win_goto_hor __ARGS((int left, long count));
65#endif
66static void frame_add_height __ARGS((frame_T *frp, int n));
67static void last_status_rec __ARGS((frame_T *fr, int statusline));
68
69static void make_snapshot __ARGS((void));
70static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000071static void clear_snapshot __ARGS((tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000072static void clear_snapshot_rec __ARGS((frame_T *fr));
73static void restore_snapshot __ARGS((int close_curwin));
74static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
75static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
76
77#endif /* FEAT_WINDOWS */
78static win_T *win_alloc __ARGS((win_T *after));
79static void win_new_height __ARGS((win_T *, int));
80
81#define URL_SLASH 1 /* path_is_url() has found "://" */
82#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
83
84#define NOWIN (win_T *)-1 /* non-exisiting window */
85
Bram Moolenaar05159a02005-02-26 23:04:13 +000086#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000087# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000088#else
89# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000090#endif
91
Bram Moolenaar071d4272004-06-13 20:20:40 +000092#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000093
94static char *m_onlyone = N_("Already only one window");
95
Bram Moolenaar071d4272004-06-13 20:20:40 +000096/*
97 * all CTRL-W window commands are handled here, called from normal_cmd().
98 */
99 void
100do_window(nchar, Prenum, xchar)
101 int nchar;
102 long Prenum;
103 int xchar; /* extra char from ":wincmd gx" or NUL */
104{
105 long Prenum1;
106 win_T *wp;
107#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
108 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000109 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110#endif
111#ifdef FEAT_FIND_ID
112 int type = FIND_DEFINE;
113 int len;
114#endif
115 char_u cbuf[40];
116
117 if (Prenum == 0)
118 Prenum1 = 1;
119 else
120 Prenum1 = Prenum;
121
122#ifdef FEAT_CMDWIN
123# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
124#else
125# define CHECK_CMDWIN
126#endif
127
128 switch (nchar)
129 {
130/* split current window in two parts, horizontally */
131 case 'S':
132 case Ctrl_S:
133 case 's':
134 CHECK_CMDWIN
135#ifdef FEAT_VISUAL
136 reset_VIsual_and_resel(); /* stop Visual mode */
137#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000138#ifdef FEAT_QUICKFIX
139 /* When splitting the quickfix window open a new buffer in it,
140 * don't replicate the quickfix buffer. */
141 if (bt_quickfix(curbuf))
142 goto newwindow;
143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#ifdef FEAT_GUI
145 need_mouse_correct = TRUE;
146#endif
147 win_split((int)Prenum, 0);
148 break;
149
150#ifdef FEAT_VERTSPLIT
151/* split current window in two parts, vertically */
152 case Ctrl_V:
153 case 'v':
154 CHECK_CMDWIN
155#ifdef FEAT_VISUAL
156 reset_VIsual_and_resel(); /* stop Visual mode */
157#endif
158#ifdef FEAT_GUI
159 need_mouse_correct = TRUE;
160#endif
161 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)
174 sprintf((char *)cbuf + 7, "%ld", Prenum);
175 do_cmdline_cmd(cbuf);
176 break;
177
178/* open new window */
179 case Ctrl_N:
180 case 'n':
181 CHECK_CMDWIN
182#ifdef FEAT_VISUAL
183 reset_VIsual_and_resel(); /* stop Visual mode */
184#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000185#ifdef FEAT_QUICKFIX
186newwindow:
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 if (Prenum)
189 sprintf((char *)cbuf, "%ld", Prenum); /* window height */
190 else
191 cbuf[0] = NUL;
192 STRCAT(cbuf, "new");
193 do_cmdline_cmd(cbuf);
194 break;
195
196/* quit current window */
197 case Ctrl_Q:
198 case 'q':
199#ifdef FEAT_VISUAL
200 reset_VIsual_and_resel(); /* stop Visual mode */
201#endif
202 do_cmdline_cmd((char_u *)"quit");
203 break;
204
205/* close current window */
206 case Ctrl_C:
207 case 'c':
208#ifdef FEAT_VISUAL
209 reset_VIsual_and_resel(); /* stop Visual mode */
210#endif
211 do_cmdline_cmd((char_u *)"close");
212 break;
213
214#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
215/* close preview window */
216 case Ctrl_Z:
217 case 'z':
218 CHECK_CMDWIN
219#ifdef FEAT_VISUAL
220 reset_VIsual_and_resel(); /* stop Visual mode */
221#endif
222 do_cmdline_cmd((char_u *)"pclose");
223 break;
224
225/* cursor to preview window */
226 case 'P':
227 for (wp = firstwin; wp != NULL; wp = wp->w_next)
228 if (wp->w_p_pvw)
229 break;
230 if (wp == NULL)
231 EMSG(_("E441: There is no preview window"));
232 else
233 win_goto(wp);
234 break;
235#endif
236
237/* close all but current window */
238 case Ctrl_O:
239 case 'o':
240 CHECK_CMDWIN
241#ifdef FEAT_VISUAL
242 reset_VIsual_and_resel(); /* stop Visual mode */
243#endif
244 do_cmdline_cmd((char_u *)"only");
245 break;
246
247/* cursor to next window with wrap around */
248 case Ctrl_W:
249 case 'w':
250/* cursor to previous window with wrap around */
251 case 'W':
252 CHECK_CMDWIN
253 if (lastwin == firstwin && Prenum != 1) /* just one window */
254 beep_flush();
255 else
256 {
257 if (Prenum) /* go to specified window */
258 {
259 for (wp = firstwin; --Prenum > 0; )
260 {
261 if (wp->w_next == NULL)
262 break;
263 else
264 wp = wp->w_next;
265 }
266 }
267 else
268 {
269 if (nchar == 'W') /* go to previous window */
270 {
271 wp = curwin->w_prev;
272 if (wp == NULL)
273 wp = lastwin; /* wrap around */
274 }
275 else /* go to next window */
276 {
277 wp = curwin->w_next;
278 if (wp == NULL)
279 wp = firstwin; /* wrap around */
280 }
281 }
282 win_goto(wp);
283 }
284 break;
285
286/* cursor to window below */
287 case 'j':
288 case K_DOWN:
289 case Ctrl_J:
290 CHECK_CMDWIN
291#ifdef FEAT_VERTSPLIT
292 win_goto_ver(FALSE, Prenum1);
293#else
294 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
295 wp = wp->w_next)
296 ;
297 win_goto(wp);
298#endif
299 break;
300
301/* cursor to window above */
302 case 'k':
303 case K_UP:
304 case Ctrl_K:
305 CHECK_CMDWIN
306#ifdef FEAT_VERTSPLIT
307 win_goto_ver(TRUE, Prenum1);
308#else
309 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
310 wp = wp->w_prev)
311 ;
312 win_goto(wp);
313#endif
314 break;
315
316#ifdef FEAT_VERTSPLIT
317/* cursor to left window */
318 case 'h':
319 case K_LEFT:
320 case Ctrl_H:
321 case K_BS:
322 CHECK_CMDWIN
323 win_goto_hor(TRUE, Prenum1);
324 break;
325
326/* cursor to right window */
327 case 'l':
328 case K_RIGHT:
329 case Ctrl_L:
330 CHECK_CMDWIN
331 win_goto_hor(FALSE, Prenum1);
332 break;
333#endif
334
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000335/* move window to new tab page */
336 case 'T':
337 if (firstwin == lastwin)
338 MSG(_(m_onlyone));
339 else
340 {
341 tabpage_T *oldtab = curtab;
342 tabpage_T *newtab;
343 win_T *wp = curwin;
344
345 /* First create a new tab with the window, then go back to
346 * the old tab and close the window there. */
347 if (win_new_tabpage((int)Prenum) == OK
348 && valid_tabpage(oldtab))
349 {
350 newtab = curtab;
351 goto_tabpage_tp(oldtab);
352 if (curwin == wp)
353 win_close(curwin, FALSE);
354 if (valid_tabpage(newtab))
355 goto_tabpage_tp(newtab);
356 }
357 }
358 break;
359
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360/* cursor to top-left window */
361 case 't':
362 case Ctrl_T:
363 win_goto(firstwin);
364 break;
365
366/* cursor to bottom-right window */
367 case 'b':
368 case Ctrl_B:
369 win_goto(lastwin);
370 break;
371
372/* cursor to last accessed (previous) window */
373 case 'p':
374 case Ctrl_P:
375 if (prevwin == NULL)
376 beep_flush();
377 else
378 win_goto(prevwin);
379 break;
380
381/* exchange current and next window */
382 case 'x':
383 case Ctrl_X:
384 CHECK_CMDWIN
385 win_exchange(Prenum);
386 break;
387
388/* rotate windows downwards */
389 case Ctrl_R:
390 case 'r':
391 CHECK_CMDWIN
392#ifdef FEAT_VISUAL
393 reset_VIsual_and_resel(); /* stop Visual mode */
394#endif
395 win_rotate(FALSE, (int)Prenum1); /* downwards */
396 break;
397
398/* rotate windows upwards */
399 case 'R':
400 CHECK_CMDWIN
401#ifdef FEAT_VISUAL
402 reset_VIsual_and_resel(); /* stop Visual mode */
403#endif
404 win_rotate(TRUE, (int)Prenum1); /* upwards */
405 break;
406
407/* move window to the very top/bottom/left/right */
408 case 'K':
409 case 'J':
410#ifdef FEAT_VERTSPLIT
411 case 'H':
412 case 'L':
413#endif
414 CHECK_CMDWIN
415 win_totop((int)Prenum,
416 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
417 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
418 break;
419
420/* make all windows the same height */
421 case '=':
422#ifdef FEAT_GUI
423 need_mouse_correct = TRUE;
424#endif
425 win_equal(NULL, FALSE, 'b');
426 break;
427
428/* increase current window height */
429 case '+':
430#ifdef FEAT_GUI
431 need_mouse_correct = TRUE;
432#endif
433 win_setheight(curwin->w_height + (int)Prenum1);
434 break;
435
436/* decrease current window height */
437 case '-':
438#ifdef FEAT_GUI
439 need_mouse_correct = TRUE;
440#endif
441 win_setheight(curwin->w_height - (int)Prenum1);
442 break;
443
444/* set current window height */
445 case Ctrl__:
446 case '_':
447#ifdef FEAT_GUI
448 need_mouse_correct = TRUE;
449#endif
450 win_setheight(Prenum ? (int)Prenum : 9999);
451 break;
452
453#ifdef FEAT_VERTSPLIT
454/* increase current window width */
455 case '>':
456#ifdef FEAT_GUI
457 need_mouse_correct = TRUE;
458#endif
459 win_setwidth(curwin->w_width + (int)Prenum1);
460 break;
461
462/* decrease current window width */
463 case '<':
464#ifdef FEAT_GUI
465 need_mouse_correct = TRUE;
466#endif
467 win_setwidth(curwin->w_width - (int)Prenum1);
468 break;
469
470/* set current window width */
471 case '|':
472#ifdef FEAT_GUI
473 need_mouse_correct = TRUE;
474#endif
475 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
476 break;
477#endif
478
479/* jump to tag and split window if tag exists (in preview window) */
480#if defined(FEAT_QUICKFIX)
481 case '}':
482 CHECK_CMDWIN
483 if (Prenum)
484 g_do_tagpreview = Prenum;
485 else
486 g_do_tagpreview = p_pvh;
487 /*FALLTHROUGH*/
488#endif
489 case ']':
490 case Ctrl_RSB:
491 CHECK_CMDWIN
492#ifdef FEAT_VISUAL
493 reset_VIsual_and_resel(); /* stop Visual mode */
494#endif
495 if (Prenum)
496 postponed_split = Prenum;
497 else
498 postponed_split = -1;
499
500 /* Execute the command right here, required when
501 * "wincmd ]" was used in a function. */
502 do_nv_ident(Ctrl_RSB, NUL);
503 break;
504
505#ifdef FEAT_SEARCHPATH
506/* edit file name under cursor in a new window */
507 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000508 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000510wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000512
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000513 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (ptr != NULL)
515 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000516# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 setpcmark();
520 if (win_split(0, 0) == OK)
521 {
522# ifdef FEAT_SCROLLBIND
523 curwin->w_p_scb = FALSE;
524# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000525 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE);
526 if (nchar == 'F' && lnum >= 0)
527 {
528 curwin->w_cursor.lnum = lnum;
529 check_cursor_lnum();
530 beginline(BL_SOL | BL_FIX);
531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 }
533 vim_free(ptr);
534 }
535 break;
536#endif
537
538#ifdef FEAT_FIND_ID
539/* Go to the first occurence of the identifier under cursor along path in a
540 * new window -- webb
541 */
542 case 'i': /* Go to any match */
543 case Ctrl_I:
544 type = FIND_ANY;
545 /* FALLTHROUGH */
546 case 'd': /* Go to definition, using 'define' */
547 case Ctrl_D:
548 CHECK_CMDWIN
549 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
550 break;
551 find_pattern_in_path(ptr, 0, len, TRUE,
552 Prenum == 0 ? TRUE : FALSE, type,
553 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
554 curwin->w_set_curswant = TRUE;
555 break;
556#endif
557
Bram Moolenaar05159a02005-02-26 23:04:13 +0000558 case K_KENTER:
559 case CAR:
560#if defined(FEAT_QUICKFIX)
561 /*
562 * In a quickfix window a <CR> jumps to the error under the
563 * cursor in a new window.
564 */
565 if (bt_quickfix(curbuf))
566 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000567 sprintf((char *)cbuf, "split +%ld%s",
568 (long)curwin->w_cursor.lnum,
569 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000570 do_cmdline_cmd(cbuf);
571 }
572#endif
573 break;
574
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576/* CTRL-W g extended commands */
577 case 'g':
578 case Ctrl_G:
579 CHECK_CMDWIN
580#ifdef USE_ON_FLY_SCROLL
581 dont_scroll = TRUE; /* disallow scrolling here */
582#endif
583 ++no_mapping;
584 ++allow_keys; /* no mapping for xchar, but allow key codes */
585 if (xchar == NUL)
586 xchar = safe_vgetc();
587#ifdef FEAT_LANGMAP
588 LANGMAP_ADJUST(xchar, TRUE);
589#endif
590 --no_mapping;
591 --allow_keys;
592#ifdef FEAT_CMDL_INFO
593 (void)add_to_showcmd(xchar);
594#endif
595 switch (xchar)
596 {
597#if defined(FEAT_QUICKFIX)
598 case '}':
599 xchar = Ctrl_RSB;
600 if (Prenum)
601 g_do_tagpreview = Prenum;
602 else
603 g_do_tagpreview = p_pvh;
604 /*FALLTHROUGH*/
605#endif
606 case ']':
607 case Ctrl_RSB:
608#ifdef FEAT_VISUAL
609 reset_VIsual_and_resel(); /* stop Visual mode */
610#endif
611 if (Prenum)
612 postponed_split = Prenum;
613 else
614 postponed_split = -1;
615
616 /* Execute the command right here, required when
617 * "wincmd g}" was used in a function. */
618 do_nv_ident('g', xchar);
619 break;
620
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000621#ifdef FEAT_SEARCHPATH
622 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000623 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000624 cmdmod.tab = TRUE;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000625 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000626 goto wingotofile;
627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 default:
629 beep_flush();
630 break;
631 }
632 break;
633
634 default: beep_flush();
635 break;
636 }
637}
638
639/*
640 * split the current window, implements CTRL-W s and :split
641 *
642 * "size" is the height or width for the new window, 0 to use half of current
643 * height or width.
644 *
645 * "flags":
646 * WSP_ROOM: require enough room for new window
647 * WSP_VERT: vertical split.
648 * WSP_TOP: open window at the top-left of the shell (help window).
649 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
650 * WSP_HELP: creating the help window, keep layout snapshot
651 *
652 * return FAIL for failure, OK otherwise
653 */
654 int
655win_split(size, flags)
656 int size;
657 int flags;
658{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000659 /* When the ":tab" modifier was used open a new tab page instead. */
660 if (may_open_tabpage() == OK)
661 return OK;
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 /* Add flags from ":vertical", ":topleft" and ":botright". */
664 flags |= cmdmod.split;
665 if ((flags & WSP_TOP) && (flags & WSP_BOT))
666 {
667 EMSG(_("E442: Can't split topleft and botright at the same time"));
668 return FAIL;
669 }
670
671 /* When creating the help window make a snapshot of the window layout.
672 * Otherwise clear the snapshot, it's now invalid. */
673 if (flags & WSP_HELP)
674 make_snapshot();
675 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000676 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678 return win_split_ins(size, flags, NULL, 0);
679}
680
681/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000682 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 * When "newwin" is not NULL: insert this window at the far
684 * top/left/right/bottom.
685 * return FAIL for failure, OK otherwise
686 */
687 static int
688win_split_ins(size, flags, newwin, dir)
689 int size;
690 int flags;
691 win_T *newwin;
692 int dir;
693{
694 win_T *wp = newwin;
695 win_T *oldwin;
696 int new_size = size;
697 int i;
698 int need_status = 0;
699 int do_equal = FALSE;
700 int needed;
701 int available;
702 int oldwin_height = 0;
703 int layout;
704 frame_T *frp, *curfrp;
705 int before;
706
707 if (flags & WSP_TOP)
708 oldwin = firstwin;
709 else if (flags & WSP_BOT)
710 oldwin = lastwin;
711 else
712 oldwin = curwin;
713
714 /* add a status line when p_ls == 1 and splitting the first window */
715 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
716 {
717 if (oldwin->w_height <= p_wmh && newwin == NULL)
718 {
719 EMSG(_(e_noroom));
720 return FAIL;
721 }
722 need_status = STATUS_HEIGHT;
723 }
724
725#ifdef FEAT_VERTSPLIT
726 if (flags & WSP_VERT)
727 {
728 layout = FR_ROW;
729 do_equal = (p_ea && new_size == 0 && *p_ead != 'v');
730
731 /*
732 * Check if we are able to split the current window and compute its
733 * width.
734 */
735 needed = p_wmw + 1;
736 if (flags & WSP_ROOM)
737 needed += p_wiw - p_wmw;
738 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
739 {
740 available = topframe->fr_width;
741 needed += frame_minwidth(topframe, NULL);
742 }
743 else
744 available = oldwin->w_width;
745 if (available < needed && newwin == NULL)
746 {
747 EMSG(_(e_noroom));
748 return FAIL;
749 }
750 if (new_size == 0)
751 new_size = oldwin->w_width / 2;
752 if (new_size > oldwin->w_width - p_wmw - 1)
753 new_size = oldwin->w_width - p_wmw - 1;
754 if (new_size < p_wmw)
755 new_size = p_wmw;
756
757 /* if it doesn't fit in the current window, need win_equal() */
758 if (oldwin->w_width - new_size - 1 < p_wmw)
759 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000760
761 /* We don't like to take lines for the new window from a
762 * 'winfixwidth' window. Take them from a window to the left or right
763 * instead, if possible. */
764 if (oldwin->w_p_wfw)
765 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 }
767 else
768#endif
769 {
770 layout = FR_COL;
771 do_equal = (p_ea && new_size == 0
772#ifdef FEAT_VERTSPLIT
773 && *p_ead != 'h'
774#endif
775 );
776
777 /*
778 * Check if we are able to split the current window and compute its
779 * height.
780 */
781 needed = p_wmh + STATUS_HEIGHT + need_status;
782 if (flags & WSP_ROOM)
783 needed += p_wh - p_wmh;
784 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
785 {
786 available = topframe->fr_height;
787 needed += frame_minheight(topframe, NULL);
788 }
789 else
790 {
791 available = oldwin->w_height;
792 needed += p_wmh;
793 }
794 if (available < needed && newwin == NULL)
795 {
796 EMSG(_(e_noroom));
797 return FAIL;
798 }
799 oldwin_height = oldwin->w_height;
800 if (need_status)
801 {
802 oldwin->w_status_height = STATUS_HEIGHT;
803 oldwin_height -= STATUS_HEIGHT;
804 }
805 if (new_size == 0)
806 new_size = oldwin_height / 2;
807
808 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
809 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
810 if (new_size < p_wmh)
811 new_size = p_wmh;
812
813 /* if it doesn't fit in the current window, need win_equal() */
814 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
815 do_equal = TRUE;
816
817 /* We don't like to take lines for the new window from a
818 * 'winfixheight' window. Take them from a window above or below
819 * instead, if possible. */
820 if (oldwin->w_p_wfh)
821 {
822 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
823 oldwin);
824 oldwin_height = oldwin->w_height;
825 if (need_status)
826 oldwin_height -= STATUS_HEIGHT;
827 }
828 }
829
830 /*
831 * allocate new window structure and link it in the window list
832 */
833 if ((flags & WSP_TOP) == 0
834 && ((flags & WSP_BOT)
835 || (flags & WSP_BELOW)
836 || (!(flags & WSP_ABOVE)
837 && (
838#ifdef FEAT_VERTSPLIT
839 (flags & WSP_VERT) ? p_spr :
840#endif
841 p_sb))))
842 {
843 /* new window below/right of current one */
844 if (newwin == NULL)
845 wp = win_alloc(oldwin);
846 else
847 win_append(oldwin, wp);
848 }
849 else
850 {
851 if (newwin == NULL)
852 wp = win_alloc(oldwin->w_prev);
853 else
854 win_append(oldwin->w_prev, wp);
855 }
856
857 if (newwin == NULL)
858 {
859 if (wp == NULL)
860 return FAIL;
861
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000862 /* make the contents of the new window the same as the current one */
863 win_init(wp, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
865
866 /*
867 * Reorganise the tree of frames to insert the new window.
868 */
869 if (flags & (WSP_TOP | WSP_BOT))
870 {
871#ifdef FEAT_VERTSPLIT
872 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
873 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
874#else
875 if (topframe->fr_layout == FR_COL)
876#endif
877 {
878 curfrp = topframe->fr_child;
879 if (flags & WSP_BOT)
880 while (curfrp->fr_next != NULL)
881 curfrp = curfrp->fr_next;
882 }
883 else
884 curfrp = topframe;
885 before = (flags & WSP_TOP);
886 }
887 else
888 {
889 curfrp = oldwin->w_frame;
890 if (flags & WSP_BELOW)
891 before = FALSE;
892 else if (flags & WSP_ABOVE)
893 before = TRUE;
894 else
895#ifdef FEAT_VERTSPLIT
896 if (flags & WSP_VERT)
897 before = !p_spr;
898 else
899#endif
900 before = !p_sb;
901 }
902 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
903 {
904 /* Need to create a new frame in the tree to make a branch. */
905 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
906 *frp = *curfrp;
907 curfrp->fr_layout = layout;
908 frp->fr_parent = curfrp;
909 frp->fr_next = NULL;
910 frp->fr_prev = NULL;
911 curfrp->fr_child = frp;
912 curfrp->fr_win = NULL;
913 curfrp = frp;
914 if (frp->fr_win != NULL)
915 oldwin->w_frame = frp;
916 else
917 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
918 frp->fr_parent = curfrp;
919 }
920
921 if (newwin == NULL)
922 {
923 /* Create a frame for the new window. */
924 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
925 frp->fr_layout = FR_LEAF;
926 frp->fr_win = wp;
927 wp->w_frame = frp;
928 }
929 else
930 frp = newwin->w_frame;
931 frp->fr_parent = curfrp->fr_parent;
932
933 /* Insert the new frame at the right place in the frame list. */
934 if (before)
935 frame_insert(curfrp, frp);
936 else
937 frame_append(curfrp, frp);
938
939#ifdef FEAT_VERTSPLIT
940 if (flags & WSP_VERT)
941 {
942 wp->w_p_scr = curwin->w_p_scr;
943 if (need_status)
944 {
945 --oldwin->w_height;
946 oldwin->w_status_height = need_status;
947 }
948 if (flags & (WSP_TOP | WSP_BOT))
949 {
950 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000951 wp->w_winrow = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 wp->w_height = curfrp->fr_height - (p_ls > 0);
953 wp->w_status_height = (p_ls > 0);
954 }
955 else
956 {
957 /* height and row of new window is same as current window */
958 wp->w_winrow = oldwin->w_winrow;
959 wp->w_height = oldwin->w_height;
960 wp->w_status_height = oldwin->w_status_height;
961 }
962 frp->fr_height = curfrp->fr_height;
963
964 /* "new_size" of the current window goes to the new window, use
965 * one column for the vertical separator */
966 wp->w_width = new_size;
967 if (before)
968 wp->w_vsep_width = 1;
969 else
970 {
971 wp->w_vsep_width = oldwin->w_vsep_width;
972 oldwin->w_vsep_width = 1;
973 }
974 if (flags & (WSP_TOP | WSP_BOT))
975 {
976 if (flags & WSP_BOT)
977 frame_add_vsep(curfrp);
978 /* Set width of neighbor frame */
979 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000980 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
981 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 }
983 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000984 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (before) /* new window left of current one */
986 {
987 wp->w_wincol = oldwin->w_wincol;
988 oldwin->w_wincol += new_size + 1;
989 }
990 else /* new window right of current one */
991 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
992 frame_fix_width(oldwin);
993 frame_fix_width(wp);
994 }
995 else
996#endif
997 {
998 /* width and column of new window is same as current window */
999#ifdef FEAT_VERTSPLIT
1000 if (flags & (WSP_TOP | WSP_BOT))
1001 {
1002 wp->w_wincol = 0;
1003 wp->w_width = Columns;
1004 wp->w_vsep_width = 0;
1005 }
1006 else
1007 {
1008 wp->w_wincol = oldwin->w_wincol;
1009 wp->w_width = oldwin->w_width;
1010 wp->w_vsep_width = oldwin->w_vsep_width;
1011 }
1012 frp->fr_width = curfrp->fr_width;
1013#endif
1014
1015 /* "new_size" of the current window goes to the new window, use
1016 * one row for the status line */
1017 win_new_height(wp, new_size);
1018 if (flags & (WSP_TOP | WSP_BOT))
1019 frame_new_height(curfrp, curfrp->fr_height
1020 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1021 else
1022 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1023 if (before) /* new window above current one */
1024 {
1025 wp->w_winrow = oldwin->w_winrow;
1026 wp->w_status_height = STATUS_HEIGHT;
1027 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1028 }
1029 else /* new window below current one */
1030 {
1031 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1032 wp->w_status_height = oldwin->w_status_height;
1033 oldwin->w_status_height = STATUS_HEIGHT;
1034 }
1035#ifdef FEAT_VERTSPLIT
1036 if (flags & WSP_BOT)
1037 frame_add_statusline(curfrp);
1038#endif
1039 frame_fix_height(wp);
1040 frame_fix_height(oldwin);
1041 }
1042
1043 if (flags & (WSP_TOP | WSP_BOT))
1044 (void)win_comp_pos();
1045
1046 /*
1047 * Both windows need redrawing
1048 */
1049 redraw_win_later(wp, NOT_VALID);
1050 wp->w_redr_status = TRUE;
1051 redraw_win_later(oldwin, NOT_VALID);
1052 oldwin->w_redr_status = TRUE;
1053
1054 if (need_status)
1055 {
1056 msg_row = Rows - 1;
1057 msg_col = sc_col;
1058 msg_clr_eos_force(); /* Old command/ruler may still be there */
1059 comp_col();
1060 msg_row = Rows - 1;
1061 msg_col = 0; /* put position back at start of line */
1062 }
1063
1064 /*
1065 * make the new window the current window and redraw
1066 */
1067 if (do_equal || dir != 0)
1068 win_equal(wp, TRUE,
1069#ifdef FEAT_VERTSPLIT
1070 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1071 : dir == 'h' ? 'b' :
1072#endif
1073 'v');
1074
1075 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1076 * size was given. */
1077#ifdef FEAT_VERTSPLIT
1078 if (flags & WSP_VERT)
1079 {
1080 i = p_wiw;
1081 if (size != 0)
1082 p_wiw = size;
1083
1084# ifdef FEAT_GUI
1085 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1086 if (gui.in_use)
1087 gui_init_which_components(NULL);
1088# endif
1089 }
1090 else
1091#endif
1092 {
1093 i = p_wh;
1094 if (size != 0)
1095 p_wh = size;
1096 }
1097 win_enter(wp, FALSE);
1098#ifdef FEAT_VERTSPLIT
1099 if (flags & WSP_VERT)
1100 p_wiw = i;
1101 else
1102#endif
1103 p_wh = i;
1104
1105 return OK;
1106}
1107
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001108/*
1109 * Initialize window "newp" from window "oldp".
1110 * Used when splitting a window and when creating a new tab page.
1111 * The windows will both edit the same buffer.
1112 */
1113 static void
1114win_init(newp, oldp)
1115 win_T *newp;
1116 win_T *oldp;
1117{
1118 int i;
1119
1120 newp->w_buffer = oldp->w_buffer;
1121 oldp->w_buffer->b_nwindows++;
1122 newp->w_cursor = oldp->w_cursor;
1123 newp->w_valid = 0;
1124 newp->w_curswant = oldp->w_curswant;
1125 newp->w_set_curswant = oldp->w_set_curswant;
1126 newp->w_topline = oldp->w_topline;
1127#ifdef FEAT_DIFF
1128 newp->w_topfill = oldp->w_topfill;
1129#endif
1130 newp->w_leftcol = oldp->w_leftcol;
1131 newp->w_pcmark = oldp->w_pcmark;
1132 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1133 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001134 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001135 newp->w_fraction = oldp->w_fraction;
1136 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1137#ifdef FEAT_JUMPLIST
1138 copy_jumplist(oldp, newp);
1139#endif
1140#ifdef FEAT_QUICKFIX
1141 copy_loclist(oldp, newp);
1142#endif
1143 if (oldp->w_localdir != NULL)
1144 newp->w_localdir = vim_strsave(oldp->w_localdir);
1145
1146 /* Use the same argument list. */
1147 newp->w_alist = oldp->w_alist;
1148 ++newp->w_alist->al_refcount;
1149 newp->w_arg_idx = oldp->w_arg_idx;
1150
1151 /*
1152 * copy tagstack and options from existing window
1153 */
1154 for (i = 0; i < oldp->w_tagstacklen; i++)
1155 {
1156 newp->w_tagstack[i] = oldp->w_tagstack[i];
1157 if (newp->w_tagstack[i].tagname != NULL)
1158 newp->w_tagstack[i].tagname =
1159 vim_strsave(newp->w_tagstack[i].tagname);
1160 }
1161 newp->w_tagstackidx = oldp->w_tagstackidx;
1162 newp->w_tagstacklen = oldp->w_tagstacklen;
1163 win_copy_options(oldp, newp);
1164# ifdef FEAT_FOLDING
1165 copyFoldingState(oldp, newp);
1166# endif
1167}
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169#endif /* FEAT_WINDOWS */
1170
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171#if defined(FEAT_WINDOWS) || defined(PROTO)
1172/*
1173 * Check if "win" is a pointer to an existing window.
1174 */
1175 int
1176win_valid(win)
1177 win_T *win;
1178{
1179 win_T *wp;
1180
1181 if (win == NULL)
1182 return FALSE;
1183 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1184 if (wp == win)
1185 return TRUE;
1186 return FALSE;
1187}
1188
1189/*
1190 * Return the number of windows.
1191 */
1192 int
1193win_count()
1194{
1195 win_T *wp;
1196 int count = 0;
1197
1198 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1199 ++count;
1200 return count;
1201}
1202
1203/*
1204 * Make "count" windows on the screen.
1205 * Return actual number of windows on the screen.
1206 * Must be called when there is just one window, filling the whole screen
1207 * (excluding the command line).
1208 */
1209/*ARGSUSED*/
1210 int
1211make_windows(count, vertical)
1212 int count;
1213 int vertical; /* split windows vertically if TRUE */
1214{
1215 int maxcount;
1216 int todo;
1217
1218#ifdef FEAT_VERTSPLIT
1219 if (vertical)
1220 {
1221 /* Each windows needs at least 'winminwidth' lines and a separator
1222 * column. */
1223 maxcount = (curwin->w_width + curwin->w_vsep_width
1224 - (p_wiw - p_wmw)) / (p_wmw + 1);
1225 }
1226 else
1227#endif
1228 {
1229 /* Each window needs at least 'winminheight' lines and a status line. */
1230 maxcount = (curwin->w_height + curwin->w_status_height
1231 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1232 }
1233
1234 if (maxcount < 2)
1235 maxcount = 2;
1236 if (count > maxcount)
1237 count = maxcount;
1238
1239 /*
1240 * add status line now, otherwise first window will be too big
1241 */
1242 if (count > 1)
1243 last_status(TRUE);
1244
1245#ifdef FEAT_AUTOCMD
1246 /*
1247 * Don't execute autocommands while creating the windows. Must do that
1248 * when putting the buffers in the windows.
1249 */
1250 ++autocmd_block;
1251#endif
1252
1253 /* todo is number of windows left to create */
1254 for (todo = count - 1; todo > 0; --todo)
1255#ifdef FEAT_VERTSPLIT
1256 if (vertical)
1257 {
1258 if (win_split(curwin->w_width - (curwin->w_width - todo)
1259 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1260 break;
1261 }
1262 else
1263#endif
1264 {
1265 if (win_split(curwin->w_height - (curwin->w_height - todo
1266 * STATUS_HEIGHT) / (todo + 1)
1267 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1268 break;
1269 }
1270
1271#ifdef FEAT_AUTOCMD
1272 --autocmd_block;
1273#endif
1274
1275 /* return actual number of windows */
1276 return (count - todo);
1277}
1278
1279/*
1280 * Exchange current and next window
1281 */
1282 static void
1283win_exchange(Prenum)
1284 long Prenum;
1285{
1286 frame_T *frp;
1287 frame_T *frp2;
1288 win_T *wp;
1289 win_T *wp2;
1290 int temp;
1291
1292 if (lastwin == firstwin) /* just one window */
1293 {
1294 beep_flush();
1295 return;
1296 }
1297
1298#ifdef FEAT_GUI
1299 need_mouse_correct = TRUE;
1300#endif
1301
1302 /*
1303 * find window to exchange with
1304 */
1305 if (Prenum)
1306 {
1307 frp = curwin->w_frame->fr_parent->fr_child;
1308 while (frp != NULL && --Prenum > 0)
1309 frp = frp->fr_next;
1310 }
1311 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1312 frp = curwin->w_frame->fr_next;
1313 else /* Swap last window in row/col with previous */
1314 frp = curwin->w_frame->fr_prev;
1315
1316 /* We can only exchange a window with another window, not with a frame
1317 * containing windows. */
1318 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1319 return;
1320 wp = frp->fr_win;
1321
1322/*
1323 * 1. remove curwin from the list. Remember after which window it was in wp2
1324 * 2. insert curwin before wp in the list
1325 * if wp != wp2
1326 * 3. remove wp from the list
1327 * 4. insert wp after wp2
1328 * 5. exchange the status line height and vsep width.
1329 */
1330 wp2 = curwin->w_prev;
1331 frp2 = curwin->w_frame->fr_prev;
1332 if (wp->w_prev != curwin)
1333 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001334 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 frame_remove(curwin->w_frame);
1336 win_append(wp->w_prev, curwin);
1337 frame_insert(frp, curwin->w_frame);
1338 }
1339 if (wp != wp2)
1340 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001341 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 frame_remove(wp->w_frame);
1343 win_append(wp2, wp);
1344 if (frp2 == NULL)
1345 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1346 else
1347 frame_append(frp2, wp->w_frame);
1348 }
1349 temp = curwin->w_status_height;
1350 curwin->w_status_height = wp->w_status_height;
1351 wp->w_status_height = temp;
1352#ifdef FEAT_VERTSPLIT
1353 temp = curwin->w_vsep_width;
1354 curwin->w_vsep_width = wp->w_vsep_width;
1355 wp->w_vsep_width = temp;
1356
1357 /* If the windows are not in the same frame, exchange the sizes to avoid
1358 * messing up the window layout. Otherwise fix the frame sizes. */
1359 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1360 {
1361 temp = curwin->w_height;
1362 curwin->w_height = wp->w_height;
1363 wp->w_height = temp;
1364 temp = curwin->w_width;
1365 curwin->w_width = wp->w_width;
1366 wp->w_width = temp;
1367 }
1368 else
1369 {
1370 frame_fix_height(curwin);
1371 frame_fix_height(wp);
1372 frame_fix_width(curwin);
1373 frame_fix_width(wp);
1374 }
1375#endif
1376
1377 (void)win_comp_pos(); /* recompute window positions */
1378
1379 win_enter(wp, TRUE);
1380 redraw_later(CLEAR);
1381}
1382
1383/*
1384 * rotate windows: if upwards TRUE the second window becomes the first one
1385 * if upwards FALSE the first window becomes the second one
1386 */
1387 static void
1388win_rotate(upwards, count)
1389 int upwards;
1390 int count;
1391{
1392 win_T *wp1;
1393 win_T *wp2;
1394 frame_T *frp;
1395 int n;
1396
1397 if (firstwin == lastwin) /* nothing to do */
1398 {
1399 beep_flush();
1400 return;
1401 }
1402
1403#ifdef FEAT_GUI
1404 need_mouse_correct = TRUE;
1405#endif
1406
1407#ifdef FEAT_VERTSPLIT
1408 /* Check if all frames in this row/col have one window. */
1409 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1410 frp = frp->fr_next)
1411 if (frp->fr_win == NULL)
1412 {
1413 EMSG(_("E443: Cannot rotate when another window is split"));
1414 return;
1415 }
1416#endif
1417
1418 while (count--)
1419 {
1420 if (upwards) /* first window becomes last window */
1421 {
1422 /* remove first window/frame from the list */
1423 frp = curwin->w_frame->fr_parent->fr_child;
1424 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001425 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 frame_remove(frp);
1427
1428 /* find last frame and append removed window/frame after it */
1429 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1430 ;
1431 win_append(frp->fr_win, wp1);
1432 frame_append(frp, wp1->w_frame);
1433
1434 wp2 = frp->fr_win; /* previously last window */
1435 }
1436 else /* last window becomes first window */
1437 {
1438 /* find last window/frame in the list and remove it */
1439 for (frp = curwin->w_frame; frp->fr_next != NULL;
1440 frp = frp->fr_next)
1441 ;
1442 wp1 = frp->fr_win;
1443 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001444 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 frame_remove(frp);
1446
1447 /* append the removed window/frame before the first in the list */
1448 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1449 frame_insert(frp->fr_parent->fr_child, frp);
1450 }
1451
1452 /* exchange status height and vsep width of old and new last window */
1453 n = wp2->w_status_height;
1454 wp2->w_status_height = wp1->w_status_height;
1455 wp1->w_status_height = n;
1456 frame_fix_height(wp1);
1457 frame_fix_height(wp2);
1458#ifdef FEAT_VERTSPLIT
1459 n = wp2->w_vsep_width;
1460 wp2->w_vsep_width = wp1->w_vsep_width;
1461 wp1->w_vsep_width = n;
1462 frame_fix_width(wp1);
1463 frame_fix_width(wp2);
1464#endif
1465
1466 /* recompute w_winrow and w_wincol for all windows */
1467 (void)win_comp_pos();
1468 }
1469
1470 redraw_later(CLEAR);
1471}
1472
1473/*
1474 * Move the current window to the very top/bottom/left/right of the screen.
1475 */
1476 static void
1477win_totop(size, flags)
1478 int size;
1479 int flags;
1480{
1481 int dir;
1482 int height = curwin->w_height;
1483
1484 if (lastwin == firstwin)
1485 {
1486 beep_flush();
1487 return;
1488 }
1489
1490 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001491 (void)winframe_remove(curwin, &dir, NULL);
1492 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 last_status(FALSE); /* may need to remove last status line */
1494 (void)win_comp_pos(); /* recompute window positions */
1495
1496 /* Split a window on the desired side and put the window there. */
1497 (void)win_split_ins(size, flags, curwin, dir);
1498 if (!(flags & WSP_VERT))
1499 {
1500 win_setheight(height);
1501 if (p_ea)
1502 win_equal(curwin, TRUE, 'v');
1503 }
1504
1505#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1506 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1507 * scrollbars. Have to update them anyway. */
1508 if (gui.in_use)
1509 {
1510 out_flush();
1511 gui_init_which_components(NULL);
1512 gui_update_scrollbars(TRUE);
1513 }
1514 need_mouse_correct = TRUE;
1515#endif
1516
1517}
1518
1519/*
1520 * Move window "win1" to below/right of "win2" and make "win1" the current
1521 * window. Only works within the same frame!
1522 */
1523 void
1524win_move_after(win1, win2)
1525 win_T *win1, *win2;
1526{
1527 int height;
1528
1529 /* check if the arguments are reasonable */
1530 if (win1 == win2)
1531 return;
1532
1533 /* check if there is something to do */
1534 if (win2->w_next != win1)
1535 {
1536 /* may need move the status line/vertical separator of the last window
1537 * */
1538 if (win1 == lastwin)
1539 {
1540 height = win1->w_prev->w_status_height;
1541 win1->w_prev->w_status_height = win1->w_status_height;
1542 win1->w_status_height = height;
1543#ifdef FEAT_VERTSPLIT
1544 win1->w_prev->w_vsep_width = 0;
1545 win1->w_vsep_width = 1;
1546#endif
1547 }
1548 else if (win2 == lastwin)
1549 {
1550 height = win1->w_status_height;
1551 win1->w_status_height = win2->w_status_height;
1552 win2->w_status_height = height;
1553#ifdef FEAT_VERTSPLIT
1554 win2->w_vsep_width = 1;
1555 win1->w_vsep_width = 0;
1556#endif
1557 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001558 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 frame_remove(win1->w_frame);
1560 win_append(win2, win1);
1561 frame_append(win2->w_frame, win1->w_frame);
1562
1563 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1564 redraw_later(NOT_VALID);
1565 }
1566 win_enter(win1, FALSE);
1567}
1568
1569/*
1570 * Make all windows the same height.
1571 * 'next_curwin' will soon be the current window, make sure it has enough
1572 * rows.
1573 */
1574 void
1575win_equal(next_curwin, current, dir)
1576 win_T *next_curwin; /* pointer to current window to be or NULL */
1577 int current; /* do only frame with current window */
1578 int dir; /* 'v' for vertically, 'h' for horizontally,
1579 'b' for both, 0 for using p_ead */
1580{
1581 if (dir == 0)
1582#ifdef FEAT_VERTSPLIT
1583 dir = *p_ead;
1584#else
1585 dir = 'b';
1586#endif
1587 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001588 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001589 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590}
1591
1592/*
1593 * Set a frame to a new position and height, spreading the available room
1594 * equally over contained frames.
1595 * The window "next_curwin" (if not NULL) should at least get the size from
1596 * 'winheight' and 'winwidth' if possible.
1597 */
1598 static void
1599win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1600 win_T *next_curwin; /* pointer to current window to be or NULL */
1601 int current; /* do only frame with current window */
1602 frame_T *topfr; /* frame to set size off */
1603 int dir; /* 'v', 'h' or 'b', see win_equal() */
1604 int col; /* horizontal position for frame */
1605 int row; /* vertical position for frame */
1606 int width; /* new width of frame */
1607 int height; /* new height of frame */
1608{
1609 int n, m;
1610 int extra_sep = 0;
1611 int wincount, totwincount = 0;
1612 frame_T *fr;
1613 int next_curwin_size = 0;
1614 int room = 0;
1615 int new_size;
1616 int has_next_curwin = 0;
1617 int hnc;
1618
1619 if (topfr->fr_layout == FR_LEAF)
1620 {
1621 /* Set the width/height of this frame.
1622 * Redraw when size or position changes */
1623 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1624#ifdef FEAT_VERTSPLIT
1625 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1626#endif
1627 )
1628 {
1629 topfr->fr_win->w_winrow = row;
1630 frame_new_height(topfr, height, FALSE, FALSE);
1631#ifdef FEAT_VERTSPLIT
1632 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001633 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634#endif
1635 redraw_all_later(CLEAR);
1636 }
1637 }
1638#ifdef FEAT_VERTSPLIT
1639 else if (topfr->fr_layout == FR_ROW)
1640 {
1641 topfr->fr_width = width;
1642 topfr->fr_height = height;
1643
1644 if (dir != 'v') /* equalize frame widths */
1645 {
1646 /* Compute the maximum number of windows horizontally in this
1647 * frame. */
1648 n = frame_minwidth(topfr, NOWIN);
1649 /* add one for the rightmost window, it doesn't have a separator */
1650 if (col + width == Columns)
1651 extra_sep = 1;
1652 else
1653 extra_sep = 0;
1654 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001655 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001657 /*
1658 * Compute width for "next_curwin" window and room available for
1659 * other windows.
1660 * "m" is the minimal width when counting p_wiw for "next_curwin".
1661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 m = frame_minwidth(topfr, next_curwin);
1663 room = width - m;
1664 if (room < 0)
1665 {
1666 next_curwin_size = p_wiw + room;
1667 room = 0;
1668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 else
1670 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001671 next_curwin_size = -1;
1672 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1673 {
1674 /* If 'winfixwidth' set keep the window width if
1675 * possible.
1676 * Watch out for this window being the next_curwin. */
1677 if (frame_fixed_width(fr))
1678 {
1679 n = frame_minwidth(fr, NOWIN);
1680 new_size = fr->fr_width;
1681 if (frame_has_win(fr, next_curwin))
1682 {
1683 room += p_wiw - p_wmw;
1684 next_curwin_size = 0;
1685 if (new_size < p_wiw)
1686 new_size = p_wiw;
1687 }
1688 else
1689 /* These windows don't use up room. */
1690 totwincount -= (n + (fr->fr_next == NULL
1691 ? extra_sep : 0)) / (p_wmw + 1);
1692 room -= new_size - n;
1693 if (room < 0)
1694 {
1695 new_size += room;
1696 room = 0;
1697 }
1698 fr->fr_newwidth = new_size;
1699 }
1700 }
1701 if (next_curwin_size == -1)
1702 {
1703 if (!has_next_curwin)
1704 next_curwin_size = 0;
1705 else if (totwincount > 1
1706 && (room + (totwincount - 2))
1707 / (totwincount - 1) > p_wiw)
1708 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001709 /* Can make all windows wider than 'winwidth', spread
1710 * the room equally. */
1711 next_curwin_size = (room + p_wiw
1712 + (totwincount - 1) * p_wmw
1713 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001714 room -= next_curwin_size - p_wiw;
1715 }
1716 else
1717 next_curwin_size = p_wiw;
1718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001720
1721 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 --totwincount; /* don't count curwin */
1723 }
1724
1725 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1726 {
1727 n = m = 0;
1728 wincount = 1;
1729 if (fr->fr_next == NULL)
1730 /* last frame gets all that remains (avoid roundoff error) */
1731 new_size = width;
1732 else if (dir == 'v')
1733 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001734 else if (frame_fixed_width(fr))
1735 {
1736 new_size = fr->fr_newwidth;
1737 wincount = 0; /* doesn't count as a sizeable window */
1738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 else
1740 {
1741 /* Compute the maximum number of windows horiz. in "fr". */
1742 n = frame_minwidth(fr, NOWIN);
1743 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1744 / (p_wmw + 1);
1745 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001746 if (has_next_curwin)
1747 hnc = frame_has_win(fr, next_curwin);
1748 else
1749 hnc = FALSE;
1750 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001752 if (totwincount == 0)
1753 new_size = room;
1754 else
1755 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001757 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 {
1759 next_curwin_size -= p_wiw - (m - n);
1760 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001761 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001763 else
1764 room -= new_size;
1765 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 }
1767
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001768 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 * window, unless equalizing all frames. */
1770 if (!current || dir != 'v' || topfr->fr_parent != NULL
1771 || (new_size != fr->fr_width)
1772 || frame_has_win(fr, next_curwin))
1773 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001774 new_size, height);
1775 col += new_size;
1776 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 totwincount -= wincount;
1778 }
1779 }
1780#endif
1781 else /* topfr->fr_layout == FR_COL */
1782 {
1783#ifdef FEAT_VERTSPLIT
1784 topfr->fr_width = width;
1785#endif
1786 topfr->fr_height = height;
1787
1788 if (dir != 'h') /* equalize frame heights */
1789 {
1790 /* Compute maximum number of windows vertically in this frame. */
1791 n = frame_minheight(topfr, NOWIN);
1792 /* add one for the bottom window if it doesn't have a statusline */
1793 if (row + height == cmdline_row && p_ls == 0)
1794 extra_sep = 1;
1795 else
1796 extra_sep = 0;
1797 totwincount = (n + extra_sep) / (p_wmh + 1);
1798 has_next_curwin = frame_has_win(topfr, next_curwin);
1799
1800 /*
1801 * Compute height for "next_curwin" window and room available for
1802 * other windows.
1803 * "m" is the minimal height when counting p_wh for "next_curwin".
1804 */
1805 m = frame_minheight(topfr, next_curwin);
1806 room = height - m;
1807 if (room < 0)
1808 {
1809 /* The room is less then 'winheight', use all space for the
1810 * current window. */
1811 next_curwin_size = p_wh + room;
1812 room = 0;
1813 }
1814 else
1815 {
1816 next_curwin_size = -1;
1817 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1818 {
1819 /* If 'winfixheight' set keep the window height if
1820 * possible.
1821 * Watch out for this window being the next_curwin. */
1822 if (frame_fixed_height(fr))
1823 {
1824 n = frame_minheight(fr, NOWIN);
1825 new_size = fr->fr_height;
1826 if (frame_has_win(fr, next_curwin))
1827 {
1828 room += p_wh - p_wmh;
1829 next_curwin_size = 0;
1830 if (new_size < p_wh)
1831 new_size = p_wh;
1832 }
1833 else
1834 /* These windows don't use up room. */
1835 totwincount -= (n + (fr->fr_next == NULL
1836 ? extra_sep : 0)) / (p_wmh + 1);
1837 room -= new_size - n;
1838 if (room < 0)
1839 {
1840 new_size += room;
1841 room = 0;
1842 }
1843 fr->fr_newheight = new_size;
1844 }
1845 }
1846 if (next_curwin_size == -1)
1847 {
1848 if (!has_next_curwin)
1849 next_curwin_size = 0;
1850 else if (totwincount > 1
1851 && (room + (totwincount - 2))
1852 / (totwincount - 1) > p_wh)
1853 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001854 /* can make all windows higher than 'winheight',
1855 * spread the room equally. */
1856 next_curwin_size = (room + p_wh
1857 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 + (totwincount - 1)) / totwincount;
1859 room -= next_curwin_size - p_wh;
1860 }
1861 else
1862 next_curwin_size = p_wh;
1863 }
1864 }
1865
1866 if (has_next_curwin)
1867 --totwincount; /* don't count curwin */
1868 }
1869
1870 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1871 {
1872 n = m = 0;
1873 wincount = 1;
1874 if (fr->fr_next == NULL)
1875 /* last frame gets all that remains (avoid roundoff error) */
1876 new_size = height;
1877 else if (dir == 'h')
1878 new_size = fr->fr_height;
1879 else if (frame_fixed_height(fr))
1880 {
1881 new_size = fr->fr_newheight;
1882 wincount = 0; /* doesn't count as a sizeable window */
1883 }
1884 else
1885 {
1886 /* Compute the maximum number of windows vert. in "fr". */
1887 n = frame_minheight(fr, NOWIN);
1888 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1889 / (p_wmh + 1);
1890 m = frame_minheight(fr, next_curwin);
1891 if (has_next_curwin)
1892 hnc = frame_has_win(fr, next_curwin);
1893 else
1894 hnc = FALSE;
1895 if (hnc) /* don't count next_curwin */
1896 --wincount;
1897 if (totwincount == 0)
1898 new_size = room;
1899 else
1900 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1901 / totwincount;
1902 if (hnc) /* add next_curwin size */
1903 {
1904 next_curwin_size -= p_wh - (m - n);
1905 new_size += next_curwin_size;
1906 room -= new_size - next_curwin_size;
1907 }
1908 else
1909 room -= new_size;
1910 new_size += n;
1911 }
1912 /* Skip frame that is full width when splitting or closing a
1913 * window, unless equalizing all frames. */
1914 if (!current || dir != 'h' || topfr->fr_parent != NULL
1915 || (new_size != fr->fr_height)
1916 || frame_has_win(fr, next_curwin))
1917 win_equal_rec(next_curwin, current, fr, dir, col, row,
1918 width, new_size);
1919 row += new_size;
1920 height -= new_size;
1921 totwincount -= wincount;
1922 }
1923 }
1924}
1925
1926/*
1927 * close all windows for buffer 'buf'
1928 */
1929 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00001930close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001932 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933{
Bram Moolenaarf740b292006-02-16 22:11:02 +00001934 win_T *wp;
1935 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001936 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937
1938 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001939
1940 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001942 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001944 win_close(wp, FALSE);
1945
1946 /* Start all over, autocommands may change the window layout. */
1947 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
1949 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00001950 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001952
1953 /* Also check windows in other tab pages. */
1954 for (tp = first_tabpage; tp != NULL; tp = nexttp)
1955 {
1956 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001957 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001958 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
1959 if (wp->w_buffer == buf)
1960 {
1961 win_close_othertab(wp, FALSE, tp);
1962
1963 /* Start all over, the tab page may be closed and
1964 * autocommands may change the window layout. */
1965 nexttp = first_tabpage;
1966 break;
1967 }
1968 }
1969
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001971
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001972 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00001973 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974}
1975
1976/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001977 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001978 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001979 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001980 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001981last_window()
1982{
1983 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
1984}
1985
1986/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001987 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 * If "free_buf" is TRUE related buffer may be unloaded.
1989 *
1990 * called by :quit, :close, :xit, :wq and findtag()
1991 */
1992 void
1993win_close(win, free_buf)
1994 win_T *win;
1995 int free_buf;
1996{
1997 win_T *wp;
1998#ifdef FEAT_AUTOCMD
1999 int other_buffer = FALSE;
2000#endif
2001 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 int dir;
2003 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002004 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002006 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 EMSG(_("E444: Cannot close last window"));
2009 return;
2010 }
2011
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002012 /*
2013 * When closing the last window in a tab page first go to another tab
2014 * page and then close the window and the tab page. This avoids that
2015 * curwin and curtab are not invalid while we are freeing memory, they may
2016 * be used in GUI events.
2017 */
2018 if (firstwin == lastwin)
2019 {
2020 goto_tabpage_tp(alt_tabpage());
2021 redraw_tabline = TRUE;
2022
2023 /* Safety check: Autocommands may have closed the window when jumping
2024 * to the other tab page. */
2025 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2026 {
2027 int h = tabline_height();
2028
2029 win_close_othertab(win, free_buf, prev_curtab);
2030 if (h != tabline_height())
2031 shell_new_rows();
2032 }
2033 return;
2034 }
2035
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 /* When closing the help window, try restoring a snapshot after closing
2037 * the window. Otherwise clear the snapshot, it's now invalid. */
2038 if (win->w_buffer->b_help)
2039 help_window = TRUE;
2040 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002041 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042
2043#ifdef FEAT_AUTOCMD
2044 if (win == curwin)
2045 {
2046 /*
2047 * Guess which window is going to be the new current window.
2048 * This may change because of the autocommands (sigh).
2049 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002050 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
2052 /*
2053 * Be careful: If autocommands delete the window, return now.
2054 */
2055 if (wp->w_buffer != curbuf)
2056 {
2057 other_buffer = TRUE;
2058 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002059 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 return;
2061 }
2062 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002063 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 return;
2065# ifdef FEAT_EVAL
2066 /* autocmds may abort script processing */
2067 if (aborting())
2068 return;
2069# endif
2070 }
2071#endif
2072
2073 /*
2074 * Close the link to the buffer.
2075 */
2076 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002077
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002079 * other window or moved to another tab page. */
2080 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 return;
2082
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002083 /* Free the memory used for the window. */
2084 wp = win_free_mem(win, &dir, NULL);
2085
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 /* Make sure curwin isn't invalid. It can cause severe trouble when
2087 * printing an error message. For win_equal() curbuf needs to be valid
2088 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002089 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
2091 curwin = wp;
2092#ifdef FEAT_QUICKFIX
2093 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2094 {
2095 /*
2096 * The cursor goes to the preview or the quickfix window, try
2097 * finding another window to go to.
2098 */
2099 for (;;)
2100 {
2101 if (wp->w_next == NULL)
2102 wp = firstwin;
2103 else
2104 wp = wp->w_next;
2105 if (wp == curwin)
2106 break;
2107 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2108 {
2109 curwin = wp;
2110 break;
2111 }
2112 }
2113 }
2114#endif
2115 curbuf = curwin->w_buffer;
2116 close_curwin = TRUE;
2117 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002118 if (p_ea
2119#ifdef FEAT_VERTSPLIT
2120 && (*p_ead == 'b' || *p_ead == dir)
2121#endif
2122 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 win_equal(curwin, TRUE,
2124#ifdef FEAT_VERTSPLIT
2125 dir
2126#else
2127 0
2128#endif
2129 );
2130 else
2131 win_comp_pos();
2132 if (close_curwin)
2133 {
2134 win_enter_ext(wp, FALSE, TRUE);
2135#ifdef FEAT_AUTOCMD
2136 if (other_buffer)
2137 /* careful: after this wp and win may be invalid! */
2138 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2139#endif
2140 }
2141
2142 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002143 * If last window has a status line now and we don't want one,
2144 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 */
2146 last_status(FALSE);
2147
2148 /* After closing the help window, try restoring the window layout from
2149 * before it was opened. */
2150 if (help_window)
2151 restore_snapshot(close_curwin);
2152
2153#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2154 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2155 if (gui.in_use && !win_hasvertsplit())
2156 gui_init_which_components(NULL);
2157#endif
2158
2159 redraw_all_later(NOT_VALID);
2160}
2161
2162/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002163 * Close window "win" in tab page "tp", which is not the current tab page.
2164 * This may be the last window ih that tab page and result in closing the tab,
2165 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002166 * Caller must check if buffer is hidden and whether the tabline needs to be
2167 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002168 */
2169 void
2170win_close_othertab(win, free_buf, tp)
2171 win_T *win;
2172 int free_buf;
2173 tabpage_T *tp;
2174{
2175 win_T *wp;
2176 int dir;
2177 tabpage_T *ptp = NULL;
2178
2179 /* Close the link to the buffer. */
2180 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2181
2182 /* Careful: Autocommands may have closed the tab page or made it the
2183 * current tab page. */
2184 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2185 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002186 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002187 return;
2188
2189 /* Autocommands may have closed the window already. */
2190 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2191 ;
2192 if (wp == NULL)
2193 return;
2194
2195 /* Free the memory used for the window. */
2196 wp = win_free_mem(win, &dir, tp);
2197
2198 /* When closing the last window in a tab page remove the tab page. */
2199 if (wp == NULL)
2200 {
2201 if (tp == first_tabpage)
2202 first_tabpage = tp->tp_next;
2203 else
2204 {
2205 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2206 ptp = ptp->tp_next)
2207 ;
2208 if (ptp == NULL)
2209 {
2210 EMSG2(_(e_intern2), "win_close_othertab()");
2211 return;
2212 }
2213 ptp->tp_next = tp->tp_next;
2214 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002215 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002216 }
2217}
2218
2219/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002220 * Free the memory used for a window.
2221 * Returns a pointer to the window that got the freed up space.
2222 */
2223 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002224win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002225 win_T *win;
2226 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002227 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002228{
2229 frame_T *frp;
2230 win_T *wp;
2231
Bram Moolenaarea408852005-06-25 22:49:46 +00002232#ifdef FEAT_FOLDING
2233 clearFolding(win);
2234#endif
2235
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002236 /* reduce the reference count to the argument list. */
2237 alist_unlink(win->w_alist);
2238
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002239 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002240 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002241 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002242 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002243 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002244
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002245 /* When deleting the current window of another tab page select a new
2246 * current window. */
2247 if (tp != NULL && win == tp->tp_curwin)
2248 tp->tp_curwin = wp;
2249
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002250 return wp;
2251}
2252
2253#if defined(EXITFREE) || defined(PROTO)
2254 void
2255win_free_all()
2256{
2257 int dummy;
2258
Bram Moolenaarf740b292006-02-16 22:11:02 +00002259# ifdef FEAT_WINDOWS
2260 while (first_tabpage->tp_next != NULL)
2261 tabpage_close(TRUE);
2262# endif
2263
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002264 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002265 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002266}
2267#endif
2268
2269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 * Remove a window and its frame from the tree of frames.
2271 * Returns a pointer to the window that got the freed up space.
2272 */
2273/*ARGSUSED*/
2274 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002275winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 win_T *win;
2277 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002278 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
2280 frame_T *frp, *frp2, *frp3;
2281 frame_T *frp_close = win->w_frame;
2282 win_T *wp;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002283 int old_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284
2285 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002286 * If there is only one window there is nothing to remove.
2287 */
2288 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2289 return NULL;
2290
2291 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 * Remove the window from its frame.
2293 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002294 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 wp = frame2win(frp2);
2296
2297 /* Remove this frame from the list of frames. */
2298 frame_remove(frp_close);
2299
2300#ifdef FEAT_VERTSPLIT
2301 if (frp_close->fr_parent->fr_layout == FR_COL)
2302 {
2303#endif
2304 /* When 'winfixheight' is set, remember its old size and restore
2305 * it later (it's a simplistic solution...). Don't do this if the
2306 * window will occupy the full height of the screen. */
2307 if (frp2->fr_win != NULL
2308 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2309 && frp2->fr_win->w_p_wfh)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002310 old_size = frp2->fr_win->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2312 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002313 if (old_size != 0)
2314 win_setheight_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315#ifdef FEAT_VERTSPLIT
2316 *dirp = 'v';
2317 }
2318 else
2319 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002320 /* When 'winfixwidth' is set, remember its old size and restore
2321 * it later (it's a simplistic solution...). Don't do this if the
2322 * window will occupy the full width of the screen. */
2323 if (frp2->fr_win != NULL
2324 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2325 && frp2->fr_win->w_p_wfw)
2326 old_size = frp2->fr_win->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002328 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2329 if (old_size != 0)
2330 win_setwidth_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 *dirp = 'h';
2332 }
2333#endif
2334
2335 /* If rows/columns go to a window below/right its positions need to be
2336 * updated. Can only be done after the sizes have been updated. */
2337 if (frp2 == frp_close->fr_next)
2338 {
2339 int row = win->w_winrow;
2340 int col = W_WINCOL(win);
2341
2342 frame_comp_pos(frp2, &row, &col);
2343 }
2344
2345 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2346 {
2347 /* There is no other frame in this list, move its info to the parent
2348 * and remove it. */
2349 frp2->fr_parent->fr_layout = frp2->fr_layout;
2350 frp2->fr_parent->fr_child = frp2->fr_child;
2351 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2352 frp->fr_parent = frp2->fr_parent;
2353 frp2->fr_parent->fr_win = frp2->fr_win;
2354 if (frp2->fr_win != NULL)
2355 frp2->fr_win->w_frame = frp2->fr_parent;
2356 frp = frp2->fr_parent;
2357 vim_free(frp2);
2358
2359 frp2 = frp->fr_parent;
2360 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2361 {
2362 /* The frame above the parent has the same layout, have to merge
2363 * the frames into this list. */
2364 if (frp2->fr_child == frp)
2365 frp2->fr_child = frp->fr_child;
2366 frp->fr_child->fr_prev = frp->fr_prev;
2367 if (frp->fr_prev != NULL)
2368 frp->fr_prev->fr_next = frp->fr_child;
2369 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2370 {
2371 frp3->fr_parent = frp2;
2372 if (frp3->fr_next == NULL)
2373 {
2374 frp3->fr_next = frp->fr_next;
2375 if (frp->fr_next != NULL)
2376 frp->fr_next->fr_prev = frp3;
2377 break;
2378 }
2379 }
2380 vim_free(frp);
2381 }
2382 }
2383
2384 return wp;
2385}
2386
2387/*
2388 * Find out which frame is going to get the freed up space when "win" is
2389 * closed.
2390 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2391 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2392 * This makes opening a window and closing it immediately keep the same window
2393 * layout.
2394 */
2395 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002396win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002398 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
2400 frame_T *frp;
2401 int b;
2402
Bram Moolenaarf740b292006-02-16 22:11:02 +00002403 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002404 /* Last window in this tab page, will go to next tab page. */
2405 return alt_tabpage()->tp_curwin->w_frame;
2406
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 frp = win->w_frame;
2408#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002409 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 b = p_spr;
2411 else
2412#endif
2413 b = p_sb;
2414 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2415 return frp->fr_next;
2416 return frp->fr_prev;
2417}
2418
2419/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002420 * Return the tabpage that will be used if the current one is closed.
2421 */
2422 static tabpage_T *
2423alt_tabpage()
2424{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002425 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002426
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002427 /* Use the next tab page if possible. */
2428 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002429 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002430
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002431 /* Find the last but one tab page. */
2432 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2433 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002434 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002435}
2436
2437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 * Find the left-upper window in frame "frp".
2439 */
2440 static win_T *
2441frame2win(frp)
2442 frame_T *frp;
2443{
2444 while (frp->fr_win == NULL)
2445 frp = frp->fr_child;
2446 return frp->fr_win;
2447}
2448
2449/*
2450 * Return TRUE if frame "frp" contains window "wp".
2451 */
2452 static int
2453frame_has_win(frp, wp)
2454 frame_T *frp;
2455 win_T *wp;
2456{
2457 frame_T *p;
2458
2459 if (frp->fr_layout == FR_LEAF)
2460 return frp->fr_win == wp;
2461
2462 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2463 if (frame_has_win(p, wp))
2464 return TRUE;
2465 return FALSE;
2466}
2467
2468/*
2469 * Set a new height for a frame. Recursively sets the height for contained
2470 * frames and windows. Caller must take care of positions.
2471 */
2472 static void
2473frame_new_height(topfrp, height, topfirst, wfh)
2474 frame_T *topfrp;
2475 int height;
2476 int topfirst; /* resize topmost contained frame first */
2477 int wfh; /* obey 'winfixheight' when there is a choice;
2478 may cause the height not to be set */
2479{
2480 frame_T *frp;
2481 int extra_lines;
2482 int h;
2483
2484 if (topfrp->fr_win != NULL)
2485 {
2486 /* Simple case: just one window. */
2487 win_new_height(topfrp->fr_win,
2488 height - topfrp->fr_win->w_status_height);
2489 }
2490#ifdef FEAT_VERTSPLIT
2491 else if (topfrp->fr_layout == FR_ROW)
2492 {
2493 do
2494 {
2495 /* All frames in this row get the same new height. */
2496 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2497 {
2498 frame_new_height(frp, height, topfirst, wfh);
2499 if (frp->fr_height > height)
2500 {
2501 /* Could not fit the windows, make the whole row higher. */
2502 height = frp->fr_height;
2503 break;
2504 }
2505 }
2506 }
2507 while (frp != NULL);
2508 }
2509#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002510 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
2512 /* Complicated case: Resize a column of frames. Resize the bottom
2513 * frame first, frames above that when needed. */
2514
2515 frp = topfrp->fr_child;
2516 if (wfh)
2517 /* Advance past frames with one window with 'wfh' set. */
2518 while (frame_fixed_height(frp))
2519 {
2520 frp = frp->fr_next;
2521 if (frp == NULL)
2522 return; /* no frame without 'wfh', give up */
2523 }
2524 if (!topfirst)
2525 {
2526 /* Find the bottom frame of this column */
2527 while (frp->fr_next != NULL)
2528 frp = frp->fr_next;
2529 if (wfh)
2530 /* Advance back for frames with one window with 'wfh' set. */
2531 while (frame_fixed_height(frp))
2532 frp = frp->fr_prev;
2533 }
2534
2535 extra_lines = height - topfrp->fr_height;
2536 if (extra_lines < 0)
2537 {
2538 /* reduce height of contained frames, bottom or top frame first */
2539 while (frp != NULL)
2540 {
2541 h = frame_minheight(frp, NULL);
2542 if (frp->fr_height + extra_lines < h)
2543 {
2544 extra_lines += frp->fr_height - h;
2545 frame_new_height(frp, h, topfirst, wfh);
2546 }
2547 else
2548 {
2549 frame_new_height(frp, frp->fr_height + extra_lines,
2550 topfirst, wfh);
2551 break;
2552 }
2553 if (topfirst)
2554 {
2555 do
2556 frp = frp->fr_next;
2557 while (wfh && frp != NULL && frame_fixed_height(frp));
2558 }
2559 else
2560 {
2561 do
2562 frp = frp->fr_prev;
2563 while (wfh && frp != NULL && frame_fixed_height(frp));
2564 }
2565 /* Increase "height" if we could not reduce enough frames. */
2566 if (frp == NULL)
2567 height -= extra_lines;
2568 }
2569 }
2570 else if (extra_lines > 0)
2571 {
2572 /* increase height of bottom or top frame */
2573 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2574 }
2575 }
2576 topfrp->fr_height = height;
2577}
2578
2579/*
2580 * Return TRUE if height of frame "frp" should not be changed because of
2581 * the 'winfixheight' option.
2582 */
2583 static int
2584frame_fixed_height(frp)
2585 frame_T *frp;
2586{
2587 /* frame with one window: fixed height if 'winfixheight' set. */
2588 if (frp->fr_win != NULL)
2589 return frp->fr_win->w_p_wfh;
2590
2591 if (frp->fr_layout == FR_ROW)
2592 {
2593 /* The frame is fixed height if one of the frames in the row is fixed
2594 * height. */
2595 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2596 if (frame_fixed_height(frp))
2597 return TRUE;
2598 return FALSE;
2599 }
2600
2601 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2602 * frames in the row are fixed height. */
2603 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2604 if (!frame_fixed_height(frp))
2605 return FALSE;
2606 return TRUE;
2607}
2608
2609#ifdef FEAT_VERTSPLIT
2610/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002611 * Return TRUE if width of frame "frp" should not be changed because of
2612 * the 'winfixwidth' option.
2613 */
2614 static int
2615frame_fixed_width(frp)
2616 frame_T *frp;
2617{
2618 /* frame with one window: fixed width if 'winfixwidth' set. */
2619 if (frp->fr_win != NULL)
2620 return frp->fr_win->w_p_wfw;
2621
2622 if (frp->fr_layout == FR_COL)
2623 {
2624 /* The frame is fixed width if one of the frames in the row is fixed
2625 * width. */
2626 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2627 if (frame_fixed_width(frp))
2628 return TRUE;
2629 return FALSE;
2630 }
2631
2632 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2633 * frames in the row are fixed width. */
2634 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2635 if (!frame_fixed_width(frp))
2636 return FALSE;
2637 return TRUE;
2638}
2639
2640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 * Add a status line to windows at the bottom of "frp".
2642 * Note: Does not check if there is room!
2643 */
2644 static void
2645frame_add_statusline(frp)
2646 frame_T *frp;
2647{
2648 win_T *wp;
2649
2650 if (frp->fr_layout == FR_LEAF)
2651 {
2652 wp = frp->fr_win;
2653 if (wp->w_status_height == 0)
2654 {
2655 if (wp->w_height > 0) /* don't make it negative */
2656 --wp->w_height;
2657 wp->w_status_height = STATUS_HEIGHT;
2658 }
2659 }
2660 else if (frp->fr_layout == FR_ROW)
2661 {
2662 /* Handle all the frames in the row. */
2663 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2664 frame_add_statusline(frp);
2665 }
2666 else /* frp->fr_layout == FR_COL */
2667 {
2668 /* Only need to handle the last frame in the column. */
2669 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2670 ;
2671 frame_add_statusline(frp);
2672 }
2673}
2674
2675/*
2676 * Set width of a frame. Handles recursively going through contained frames.
2677 * May remove separator line for windows at the right side (for win_close()).
2678 */
2679 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002680frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 frame_T *topfrp;
2682 int width;
2683 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002684 int wfw; /* obey 'winfixwidth' when there is a choice;
2685 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
2687 frame_T *frp;
2688 int extra_cols;
2689 int w;
2690 win_T *wp;
2691
2692 if (topfrp->fr_layout == FR_LEAF)
2693 {
2694 /* Simple case: just one window. */
2695 wp = topfrp->fr_win;
2696 /* Find out if there are any windows right of this one. */
2697 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2698 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2699 break;
2700 if (frp->fr_parent == NULL)
2701 wp->w_vsep_width = 0;
2702 win_new_width(wp, width - wp->w_vsep_width);
2703 }
2704 else if (topfrp->fr_layout == FR_COL)
2705 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002706 do
2707 {
2708 /* All frames in this column get the same new width. */
2709 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2710 {
2711 frame_new_width(frp, width, leftfirst, wfw);
2712 if (frp->fr_width > width)
2713 {
2714 /* Could not fit the windows, make whole column wider. */
2715 width = frp->fr_width;
2716 break;
2717 }
2718 }
2719 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 }
2721 else /* fr_layout == FR_ROW */
2722 {
2723 /* Complicated case: Resize a row of frames. Resize the rightmost
2724 * frame first, frames left of it when needed. */
2725
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002727 if (wfw)
2728 /* Advance past frames with one window with 'wfw' set. */
2729 while (frame_fixed_width(frp))
2730 {
2731 frp = frp->fr_next;
2732 if (frp == NULL)
2733 return; /* no frame without 'wfw', give up */
2734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002736 {
2737 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 while (frp->fr_next != NULL)
2739 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002740 if (wfw)
2741 /* Advance back for frames with one window with 'wfw' set. */
2742 while (frame_fixed_width(frp))
2743 frp = frp->fr_prev;
2744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
2746 extra_cols = width - topfrp->fr_width;
2747 if (extra_cols < 0)
2748 {
2749 /* reduce frame width, rightmost frame first */
2750 while (frp != NULL)
2751 {
2752 w = frame_minwidth(frp, NULL);
2753 if (frp->fr_width + extra_cols < w)
2754 {
2755 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002756 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758 else
2759 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002760 frame_new_width(frp, frp->fr_width + extra_cols,
2761 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 break;
2763 }
2764 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002765 {
2766 do
2767 frp = frp->fr_next;
2768 while (wfw && frp != NULL && frame_fixed_width(frp));
2769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002771 {
2772 do
2773 frp = frp->fr_prev;
2774 while (wfw && frp != NULL && frame_fixed_width(frp));
2775 }
2776 /* Increase "width" if we could not reduce enough frames. */
2777 if (frp == NULL)
2778 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
2780 }
2781 else if (extra_cols > 0)
2782 {
2783 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002784 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
2786 }
2787 topfrp->fr_width = width;
2788}
2789
2790/*
2791 * Add the vertical separator to windows at the right side of "frp".
2792 * Note: Does not check if there is room!
2793 */
2794 static void
2795frame_add_vsep(frp)
2796 frame_T *frp;
2797{
2798 win_T *wp;
2799
2800 if (frp->fr_layout == FR_LEAF)
2801 {
2802 wp = frp->fr_win;
2803 if (wp->w_vsep_width == 0)
2804 {
2805 if (wp->w_width > 0) /* don't make it negative */
2806 --wp->w_width;
2807 wp->w_vsep_width = 1;
2808 }
2809 }
2810 else if (frp->fr_layout == FR_COL)
2811 {
2812 /* Handle all the frames in the column. */
2813 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2814 frame_add_vsep(frp);
2815 }
2816 else /* frp->fr_layout == FR_ROW */
2817 {
2818 /* Only need to handle the last frame in the row. */
2819 frp = frp->fr_child;
2820 while (frp->fr_next != NULL)
2821 frp = frp->fr_next;
2822 frame_add_vsep(frp);
2823 }
2824}
2825
2826/*
2827 * Set frame width from the window it contains.
2828 */
2829 static void
2830frame_fix_width(wp)
2831 win_T *wp;
2832{
2833 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2834}
2835#endif
2836
2837/*
2838 * Set frame height from the window it contains.
2839 */
2840 static void
2841frame_fix_height(wp)
2842 win_T *wp;
2843{
2844 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2845}
2846
2847/*
2848 * Compute the minimal height for frame "topfrp".
2849 * Uses the 'winminheight' option.
2850 * When "next_curwin" isn't NULL, use p_wh for this window.
2851 * When "next_curwin" is NOWIN, don't use at least one line for the current
2852 * window.
2853 */
2854 static int
2855frame_minheight(topfrp, next_curwin)
2856 frame_T *topfrp;
2857 win_T *next_curwin;
2858{
2859 frame_T *frp;
2860 int m;
2861#ifdef FEAT_VERTSPLIT
2862 int n;
2863#endif
2864
2865 if (topfrp->fr_win != NULL)
2866 {
2867 if (topfrp->fr_win == next_curwin)
2868 m = p_wh + topfrp->fr_win->w_status_height;
2869 else
2870 {
2871 /* window: minimal height of the window plus status line */
2872 m = p_wmh + topfrp->fr_win->w_status_height;
2873 /* Current window is minimal one line high */
2874 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2875 ++m;
2876 }
2877 }
2878#ifdef FEAT_VERTSPLIT
2879 else if (topfrp->fr_layout == FR_ROW)
2880 {
2881 /* get the minimal height from each frame in this row */
2882 m = 0;
2883 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2884 {
2885 n = frame_minheight(frp, next_curwin);
2886 if (n > m)
2887 m = n;
2888 }
2889 }
2890#endif
2891 else
2892 {
2893 /* Add up the minimal heights for all frames in this column. */
2894 m = 0;
2895 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2896 m += frame_minheight(frp, next_curwin);
2897 }
2898
2899 return m;
2900}
2901
2902#ifdef FEAT_VERTSPLIT
2903/*
2904 * Compute the minimal width for frame "topfrp".
2905 * When "next_curwin" isn't NULL, use p_wiw for this window.
2906 * When "next_curwin" is NOWIN, don't use at least one column for the current
2907 * window.
2908 */
2909 static int
2910frame_minwidth(topfrp, next_curwin)
2911 frame_T *topfrp;
2912 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2913{
2914 frame_T *frp;
2915 int m, n;
2916
2917 if (topfrp->fr_win != NULL)
2918 {
2919 if (topfrp->fr_win == next_curwin)
2920 m = p_wiw + topfrp->fr_win->w_vsep_width;
2921 else
2922 {
2923 /* window: minimal width of the window plus separator column */
2924 m = p_wmw + topfrp->fr_win->w_vsep_width;
2925 /* Current window is minimal one column wide */
2926 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2927 ++m;
2928 }
2929 }
2930 else if (topfrp->fr_layout == FR_COL)
2931 {
2932 /* get the minimal width from each frame in this column */
2933 m = 0;
2934 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2935 {
2936 n = frame_minwidth(frp, next_curwin);
2937 if (n > m)
2938 m = n;
2939 }
2940 }
2941 else
2942 {
2943 /* Add up the minimal widths for all frames in this row. */
2944 m = 0;
2945 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2946 m += frame_minwidth(frp, next_curwin);
2947 }
2948
2949 return m;
2950}
2951#endif
2952
2953
2954/*
2955 * Try to close all windows except current one.
2956 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2957 * used and the buffer was modified.
2958 *
2959 * Used by ":bdel" and ":only".
2960 */
2961 void
2962close_others(message, forceit)
2963 int message;
2964 int forceit; /* always hide all other windows */
2965{
2966 win_T *wp;
2967 win_T *nextwp;
2968 int r;
2969
2970 if (lastwin == firstwin)
2971 {
2972 if (message
2973#ifdef FEAT_AUTOCMD
2974 && !autocmd_busy
2975#endif
2976 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00002977 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 return;
2979 }
2980
2981 /* Be very careful here: autocommands may change the window layout. */
2982 for (wp = firstwin; win_valid(wp); wp = nextwp)
2983 {
2984 nextwp = wp->w_next;
2985 if (wp != curwin) /* don't close current window */
2986 {
2987
2988 /* Check if it's allowed to abandon this window */
2989 r = can_abandon(wp->w_buffer, forceit);
2990#ifdef FEAT_AUTOCMD
2991 if (!win_valid(wp)) /* autocommands messed wp up */
2992 {
2993 nextwp = firstwin;
2994 continue;
2995 }
2996#endif
2997 if (!r)
2998 {
2999#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3000 if (message && (p_confirm || cmdmod.confirm) && p_write)
3001 {
3002 dialog_changed(wp->w_buffer, FALSE);
3003# ifdef FEAT_AUTOCMD
3004 if (!win_valid(wp)) /* autocommands messed wp up */
3005 {
3006 nextwp = firstwin;
3007 continue;
3008 }
3009# endif
3010 }
3011 if (bufIsChanged(wp->w_buffer))
3012#endif
3013 continue;
3014 }
3015 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3016 }
3017 }
3018
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003019 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 EMSG(_("E445: Other window contains changes"));
3021}
3022
3023#endif /* FEAT_WINDOWS */
3024
3025/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003026 * Init the current window "curwin".
3027 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 */
3029 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003030curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003032 redraw_win_later(curwin, NOT_VALID);
3033 curwin->w_lines_valid = 0;
3034 curwin->w_cursor.lnum = 1;
3035 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003037 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003039 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3040 curwin->w_pcmark.col = 0;
3041 curwin->w_prev_pcmark.lnum = 0;
3042 curwin->w_prev_pcmark.col = 0;
3043 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003045 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003047 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048#ifdef FEAT_FKMAP
3049 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003050 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003052 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053#endif
3054}
3055
3056/*
3057 * Allocate the first window and put an empty buffer in it.
3058 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003059 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003061 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062win_alloc_first()
3063{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003064 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003065 return FAIL;
3066
3067#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003068 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003069 if (first_tabpage == NULL)
3070 return FAIL;
3071 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003072 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003073#endif
3074 return OK;
3075}
3076
3077/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003078 * Allocate the first window or the first window in a new tab page.
3079 * When "oldwin" is NULL create an empty buffer for it.
3080 * When "oldwin" is not NULL copy info from it to the new window (only with
3081 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003082 * Return FAIL when something goes wrong (out of memory).
3083 */
3084 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003085win_alloc_firstwin(oldwin)
3086 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003087{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003089 if (oldwin == NULL)
3090 {
3091 /* Very first window, need to create an empty buffer for it and
3092 * initialize from scratch. */
3093 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3094 if (curwin == NULL || curbuf == NULL)
3095 return FAIL;
3096 curwin->w_buffer = curbuf;
3097 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003099 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003101 curwin_init(); /* init current window */
3102 }
3103#ifdef FEAT_WINDOWS
3104 else
3105 {
3106 /* First window in new tab page, initialize it from "oldwin". */
3107 win_init(curwin, oldwin);
3108
3109# ifdef FEAT_SCROLLBIND
3110 /* We don't want scroll-binding in the first window. */
3111 curwin->w_p_scb = FALSE;
3112# endif
3113 }
3114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115
3116 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3117 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003118 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 topframe->fr_layout = FR_LEAF;
3120#ifdef FEAT_VERTSPLIT
3121 topframe->fr_width = Columns;
3122#endif
3123 topframe->fr_height = Rows - p_ch;
3124 topframe->fr_win = curwin;
3125 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003126
3127 return OK;
3128}
3129
3130/*
3131 * Initialize the window and frame size to the maximum.
3132 */
3133 void
3134win_init_size()
3135{
3136 firstwin->w_height = ROWS_AVAIL;
3137 topframe->fr_height = ROWS_AVAIL;
3138#ifdef FEAT_VERTSPLIT
3139 firstwin->w_width = Columns;
3140 topframe->fr_width = Columns;
3141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142}
3143
3144#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003145
3146/*
3147 * Allocate a new tabpage_T and init the values.
3148 * Returns NULL when out of memory.
3149 */
3150 static tabpage_T *
3151alloc_tabpage()
3152{
3153 tabpage_T *tp;
3154
3155 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3156 if (tp != NULL)
3157 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003158# ifdef FEAT_GUI
3159 int i;
3160
3161 for (i = 0; i < 3; i++)
3162 tp->tp_prev_which_scrollbars[i] = -1;
3163# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003164# ifdef FEAT_DIFF
3165 tp->tp_diff_invalid = TRUE;
3166# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003167#ifdef FEAT_EVAL
3168 /* init t: variables */
3169 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3170#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003171 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003172 }
3173 return tp;
3174}
3175
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003176 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003177free_tabpage(tp)
3178 tabpage_T *tp;
3179{
3180# ifdef FEAT_DIFF
3181 diff_clear(tp);
3182# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003183 clear_snapshot(tp);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003184#ifdef FEAT_EVAL
3185 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3186#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003187 vim_free(tp);
3188}
3189
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003190/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003191 * Create a new Tab page with one window.
3192 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003193 * When "after" is 0 put it just after the current Tab page.
3194 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003195 * Return FAIL or OK.
3196 */
3197 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003198win_new_tabpage(after)
3199 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003200{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003201 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003202 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003203 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003204
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003205 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003206 if (newtp == NULL)
3207 return FAIL;
3208
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003209 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003210 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003211 {
3212 vim_free(newtp);
3213 return FAIL;
3214 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003215 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003216
3217 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003218 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003219 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003220 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003221 if (after == 1)
3222 {
3223 /* New tab page becomes the first one. */
3224 newtp->tp_next = first_tabpage;
3225 first_tabpage = newtp;
3226 }
3227 else
3228 {
3229 if (after > 0)
3230 {
3231 /* Put new tab page before tab page "after". */
3232 n = 2;
3233 for (tp = first_tabpage; tp->tp_next != NULL
3234 && n < after; tp = tp->tp_next)
3235 ++n;
3236 }
3237 newtp->tp_next = tp->tp_next;
3238 tp->tp_next = newtp;
3239 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003240 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003241 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003242 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003243
3244 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003245 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003246
3247#if defined(FEAT_GUI)
3248 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3249 * scrollbars. Have to update them anyway. */
3250 if (gui.in_use && starting == 0)
3251 {
3252 gui_init_which_components(NULL);
3253 gui_update_scrollbars(TRUE);
3254 }
3255 need_mouse_correct = TRUE;
3256#endif
3257
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003258 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003259#ifdef FEAT_AUTOCMD
3260 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3261 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3262#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003263 return OK;
3264 }
3265
3266 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003267 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003268 return FAIL;
3269}
3270
3271/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003272 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3273 * like with ":split".
3274 * Returns OK if a new tab page was created, FAIL otherwise.
3275 */
3276 int
3277may_open_tabpage()
3278{
3279 int n = cmdmod.tab;
3280
3281 if (cmdmod.tab != 0)
3282 {
3283 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3284 return win_new_tabpage(n);
3285 }
3286 return FAIL;
3287}
3288
3289/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003290 * Create up to "maxcount" tabpages with empty windows.
3291 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003292 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003293 int
3294make_tabpages(maxcount)
3295 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003296{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003297 int count = maxcount;
3298 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003299
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003300 /* Limit to 'tabpagemax' tabs. */
3301 if (count > p_tpm)
3302 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003303
3304#ifdef FEAT_AUTOCMD
3305 /*
3306 * Don't execute autocommands while creating the tab pages. Must do that
3307 * when putting the buffers in the windows.
3308 */
3309 ++autocmd_block;
3310#endif
3311
3312 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003313 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003314 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003315
3316#ifdef FEAT_AUTOCMD
3317 --autocmd_block;
3318#endif
3319
3320 /* return actual number of tab pages */
3321 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003322}
3323
3324/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003325 * Return TRUE when "tpc" points to a valid tab page.
3326 */
3327 int
3328valid_tabpage(tpc)
3329 tabpage_T *tpc;
3330{
3331 tabpage_T *tp;
3332
3333 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3334 if (tp == tpc)
3335 return TRUE;
3336 return FALSE;
3337}
3338
3339/*
3340 * Find tab page "n" (first one is 1). Returns NULL when not found.
3341 */
3342 tabpage_T *
3343find_tabpage(n)
3344 int n;
3345{
3346 tabpage_T *tp;
3347 int i = 1;
3348
3349 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3350 ++i;
3351 return tp;
3352}
3353
3354/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003355 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003356 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003357 */
3358 int
3359tabpage_index(ftp)
3360 tabpage_T *ftp;
3361{
3362 int i = 1;
3363 tabpage_T *tp;
3364
3365 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3366 ++i;
3367 return i;
3368}
3369
3370/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003371 * Prepare for leaving the current tab page.
3372 * When autocomands change "curtab" we don't leave the tab page and return
3373 * FAIL.
3374 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003375 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003376/*ARGSUSED*/
3377 static int
3378leave_tabpage(new_curbuf)
3379 buf_T *new_curbuf; /* what is going to be the new curbuf,
3380 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003381{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003382 tabpage_T *tp = curtab;
3383
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003384#ifdef FEAT_VISUAL
3385 reset_VIsual_and_resel(); /* stop Visual mode */
3386#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003387#ifdef FEAT_AUTOCMD
3388 if (new_curbuf != curbuf)
3389 {
3390 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3391 if (curtab != tp)
3392 return FAIL;
3393 }
3394 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3395 if (curtab != tp)
3396 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003397 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003398 if (curtab != tp)
3399 return FAIL;
3400#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003401#if defined(FEAT_GUI)
3402 /* Remove the scrollbars. They may be added back later. */
3403 if (gui.in_use)
3404 gui_remove_scrollbars();
3405#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003406 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003407 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003408 tp->tp_firstwin = firstwin;
3409 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003410 tp->tp_old_Rows = Rows;
3411 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003412 firstwin = NULL;
3413 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003414 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003415}
3416
3417/*
3418 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003419 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003420 */
3421/*ARGSUSED*/
3422 static void
3423enter_tabpage(tp, old_curbuf)
3424 tabpage_T *tp;
3425 buf_T *old_curbuf;
3426{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003427 int old_off = tp->tp_firstwin->w_winrow;
3428
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003429 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003430 firstwin = tp->tp_firstwin;
3431 lastwin = tp->tp_lastwin;
3432 topframe = tp->tp_topframe;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003433#ifdef FEAT_AUTOCMD
3434 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3435#endif
3436
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003437 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003438 prevwin = tp->tp_prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003439
3440#ifdef FEAT_AUTOCMD
3441 if (old_curbuf != curbuf)
3442 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3443#endif
3444
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003445 last_status(FALSE); /* status line may appear or disappear */
3446 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003447 must_redraw = CLEAR; /* need to redraw everything */
3448#ifdef FEAT_DIFF
3449 diff_need_scrollbind = TRUE;
3450#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003451
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003452 /* The tabpage line may have appeared or disappeared, may need to resize
3453 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003454 * frame sizes too. Use the stored value of p_ch, so that it can be
3455 * different for each tab page. */
3456 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003457 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3458#ifdef FEAT_GUI_TABLINE
3459 && !gui_use_tabline()
3460#endif
3461 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003462 shell_new_rows();
3463#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003464 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003465 shell_new_columns(); /* update window widths */
3466#endif
3467
3468#if defined(FEAT_GUI)
3469 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3470 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003471 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003472 {
3473 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003474 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003475 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003476 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003477#endif
3478
3479 redraw_all_later(CLEAR);
3480}
3481
3482/*
3483 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003484 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003485 */
3486 void
3487goto_tabpage(n)
3488 int n;
3489{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003490 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003491 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003492 int i;
3493
Bram Moolenaard68071d2006-05-02 22:08:30 +00003494 if (text_locked())
3495 {
3496 /* Not allowed when editing the command line. */
3497#ifdef FEAT_CMDWIN
3498 if (cmdwin_type != 0)
3499 EMSG(_(e_cmdwin));
3500 else
3501#endif
3502 EMSG(_(e_secure));
3503 return;
3504 }
3505
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003506 /* If there is only one it can't work. */
3507 if (first_tabpage->tp_next == NULL)
3508 {
3509 if (n > 1)
3510 beep_flush();
3511 return;
3512 }
3513
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003514 if (n == 0)
3515 {
3516 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003517 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003518 tp = first_tabpage;
3519 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003520 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003521 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003522 else if (n < 0)
3523 {
3524 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3525 * this N times. */
3526 ttp = curtab;
3527 for (i = n; i < 0; ++i)
3528 {
3529 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3530 tp = tp->tp_next)
3531 ;
3532 ttp = tp;
3533 }
3534 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003535 else if (n == 9999)
3536 {
3537 /* Go to last tab page. */
3538 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3539 ;
3540 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003541 else
3542 {
3543 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003544 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003545 if (tp == NULL)
3546 {
3547 beep_flush();
3548 return;
3549 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003550 }
3551
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003552 goto_tabpage_tp(tp);
3553
3554#ifdef FEAT_GUI_TABLINE
3555 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003556 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003557#endif
3558}
3559
3560/*
3561 * Go to tabpage "tp".
3562 * Note: doesn't update the GUI tab.
3563 */
3564 void
3565goto_tabpage_tp(tp)
3566 tabpage_T *tp;
3567{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003568 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003569 {
3570 if (valid_tabpage(tp))
3571 enter_tabpage(tp, curbuf);
3572 else
3573 enter_tabpage(curtab, curbuf);
3574 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003575}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
3577/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003578 * Enter window "wp" in tab page "tp".
3579 * Also updates the GUI tab.
3580 */
3581 void
3582goto_tabpage_win(tp, wp)
3583 tabpage_T *tp;
3584 win_T *wp;
3585{
3586 goto_tabpage_tp(tp);
3587 if (curtab == tp && win_valid(wp))
3588 {
3589 win_enter(wp, TRUE);
3590# ifdef FEAT_GUI_TABLINE
3591 if (gui_use_tabline())
3592 gui_mch_set_curtab(tabpage_index(curtab));
3593# endif
3594 }
3595}
3596
3597/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003598 * Move the current tab page to before tab page "nr".
3599 */
3600 void
3601tabpage_move(nr)
3602 int nr;
3603{
3604 int n = nr;
3605 tabpage_T *tp;
3606
3607 if (first_tabpage->tp_next == NULL)
3608 return;
3609
3610 /* Remove the current tab page from the list of tab pages. */
3611 if (curtab == first_tabpage)
3612 first_tabpage = curtab->tp_next;
3613 else
3614 {
3615 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3616 if (tp->tp_next == curtab)
3617 break;
3618 if (tp == NULL) /* "cannot happen" */
3619 return;
3620 tp->tp_next = curtab->tp_next;
3621 }
3622
3623 /* Re-insert it at the specified position. */
3624 if (n == 0)
3625 {
3626 curtab->tp_next = first_tabpage;
3627 first_tabpage = curtab;
3628 }
3629 else
3630 {
3631 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3632 --n;
3633 curtab->tp_next = tp->tp_next;
3634 tp->tp_next = curtab;
3635 }
3636
3637 /* Need to redraw the tabline. Tab page contents doesn't change. */
3638 redraw_tabline = TRUE;
3639}
3640
3641
3642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 * Go to another window.
3644 * When jumping to another buffer, stop Visual mode. Do this before
3645 * changing windows so we can yank the selection into the '*' register.
3646 * When jumping to another window on the same buffer, adjust its cursor
3647 * position to keep the same Visual area.
3648 */
3649 void
3650win_goto(wp)
3651 win_T *wp;
3652{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003653 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 {
3655 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003656 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 return;
3658 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003659#ifdef FEAT_AUTOCMD
3660 if (curbuf_locked())
3661 return;
3662#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003663
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664#ifdef FEAT_VISUAL
3665 if (wp->w_buffer != curbuf)
3666 reset_VIsual_and_resel();
3667 else if (VIsual_active)
3668 wp->w_cursor = curwin->w_cursor;
3669#endif
3670
3671#ifdef FEAT_GUI
3672 need_mouse_correct = TRUE;
3673#endif
3674 win_enter(wp, TRUE);
3675}
3676
3677#if defined(FEAT_PERL) || defined(PROTO)
3678/*
3679 * Find window number "winnr" (counting top to bottom).
3680 */
3681 win_T *
3682win_find_nr(winnr)
3683 int winnr;
3684{
3685 win_T *wp;
3686
3687# ifdef FEAT_WINDOWS
3688 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3689 if (--winnr == 0)
3690 break;
3691 return wp;
3692# else
3693 return curwin;
3694# endif
3695}
3696#endif
3697
3698#ifdef FEAT_VERTSPLIT
3699/*
3700 * Move to window above or below "count" times.
3701 */
3702 static void
3703win_goto_ver(up, count)
3704 int up; /* TRUE to go to win above */
3705 long count;
3706{
3707 frame_T *fr;
3708 frame_T *nfr;
3709 frame_T *foundfr;
3710
3711 foundfr = curwin->w_frame;
3712 while (count--)
3713 {
3714 /*
3715 * First go upwards in the tree of frames until we find a upwards or
3716 * downwards neighbor.
3717 */
3718 fr = foundfr;
3719 for (;;)
3720 {
3721 if (fr == topframe)
3722 goto end;
3723 if (up)
3724 nfr = fr->fr_prev;
3725 else
3726 nfr = fr->fr_next;
3727 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3728 break;
3729 fr = fr->fr_parent;
3730 }
3731
3732 /*
3733 * Now go downwards to find the bottom or top frame in it.
3734 */
3735 for (;;)
3736 {
3737 if (nfr->fr_layout == FR_LEAF)
3738 {
3739 foundfr = nfr;
3740 break;
3741 }
3742 fr = nfr->fr_child;
3743 if (nfr->fr_layout == FR_ROW)
3744 {
3745 /* Find the frame at the cursor row. */
3746 while (fr->fr_next != NULL
3747 && frame2win(fr)->w_wincol + fr->fr_width
3748 <= curwin->w_wincol + curwin->w_wcol)
3749 fr = fr->fr_next;
3750 }
3751 if (nfr->fr_layout == FR_COL && up)
3752 while (fr->fr_next != NULL)
3753 fr = fr->fr_next;
3754 nfr = fr;
3755 }
3756 }
3757end:
3758 if (foundfr != NULL)
3759 win_goto(foundfr->fr_win);
3760}
3761
3762/*
3763 * Move to left or right window.
3764 */
3765 static void
3766win_goto_hor(left, count)
3767 int left; /* TRUE to go to left win */
3768 long count;
3769{
3770 frame_T *fr;
3771 frame_T *nfr;
3772 frame_T *foundfr;
3773
3774 foundfr = curwin->w_frame;
3775 while (count--)
3776 {
3777 /*
3778 * First go upwards in the tree of frames until we find a left or
3779 * right neighbor.
3780 */
3781 fr = foundfr;
3782 for (;;)
3783 {
3784 if (fr == topframe)
3785 goto end;
3786 if (left)
3787 nfr = fr->fr_prev;
3788 else
3789 nfr = fr->fr_next;
3790 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3791 break;
3792 fr = fr->fr_parent;
3793 }
3794
3795 /*
3796 * Now go downwards to find the leftmost or rightmost frame in it.
3797 */
3798 for (;;)
3799 {
3800 if (nfr->fr_layout == FR_LEAF)
3801 {
3802 foundfr = nfr;
3803 break;
3804 }
3805 fr = nfr->fr_child;
3806 if (nfr->fr_layout == FR_COL)
3807 {
3808 /* Find the frame at the cursor row. */
3809 while (fr->fr_next != NULL
3810 && frame2win(fr)->w_winrow + fr->fr_height
3811 <= curwin->w_winrow + curwin->w_wrow)
3812 fr = fr->fr_next;
3813 }
3814 if (nfr->fr_layout == FR_ROW && left)
3815 while (fr->fr_next != NULL)
3816 fr = fr->fr_next;
3817 nfr = fr;
3818 }
3819 }
3820end:
3821 if (foundfr != NULL)
3822 win_goto(foundfr->fr_win);
3823}
3824#endif
3825
3826/*
3827 * Make window "wp" the current window.
3828 */
3829 void
3830win_enter(wp, undo_sync)
3831 win_T *wp;
3832 int undo_sync;
3833{
3834 win_enter_ext(wp, undo_sync, FALSE);
3835}
3836
3837/*
3838 * Make window wp the current window.
3839 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3840 * been closed and isn't valid.
3841 */
3842 static void
3843win_enter_ext(wp, undo_sync, curwin_invalid)
3844 win_T *wp;
3845 int undo_sync;
3846 int curwin_invalid;
3847{
3848#ifdef FEAT_AUTOCMD
3849 int other_buffer = FALSE;
3850#endif
3851
3852 if (wp == curwin && !curwin_invalid) /* nothing to do */
3853 return;
3854
3855#ifdef FEAT_AUTOCMD
3856 if (!curwin_invalid)
3857 {
3858 /*
3859 * Be careful: If autocommands delete the window, return now.
3860 */
3861 if (wp->w_buffer != curbuf)
3862 {
3863 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3864 other_buffer = TRUE;
3865 if (!win_valid(wp))
3866 return;
3867 }
3868 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3869 if (!win_valid(wp))
3870 return;
3871# ifdef FEAT_EVAL
3872 /* autocmds may abort script processing */
3873 if (aborting())
3874 return;
3875# endif
3876 }
3877#endif
3878
3879 /* sync undo before leaving the current buffer */
3880 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003881 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 /* may have to copy the buffer options when 'cpo' contains 'S' */
3883 if (wp->w_buffer != curbuf)
3884 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3885 if (!curwin_invalid)
3886 {
3887 prevwin = curwin; /* remember for CTRL-W p */
3888 curwin->w_redr_status = TRUE;
3889 }
3890 curwin = wp;
3891 curbuf = wp->w_buffer;
3892 check_cursor();
3893#ifdef FEAT_VIRTUALEDIT
3894 if (!virtual_active())
3895 curwin->w_cursor.coladd = 0;
3896#endif
3897 changed_line_abv_curs(); /* assume cursor position needs updating */
3898
3899 if (curwin->w_localdir != NULL)
3900 {
3901 /* Window has a local directory: Save current directory as global
3902 * directory (unless that was done already) and change to the local
3903 * directory. */
3904 if (globaldir == NULL)
3905 {
3906 char_u cwd[MAXPATHL];
3907
3908 if (mch_dirname(cwd, MAXPATHL) == OK)
3909 globaldir = vim_strsave(cwd);
3910 }
3911 mch_chdir((char *)curwin->w_localdir);
3912 shorten_fnames(TRUE);
3913 }
3914 else if (globaldir != NULL)
3915 {
3916 /* Window doesn't have a local directory and we are not in the global
3917 * directory: Change to the global directory. */
3918 mch_chdir((char *)globaldir);
3919 vim_free(globaldir);
3920 globaldir = NULL;
3921 shorten_fnames(TRUE);
3922 }
3923
3924#ifdef FEAT_AUTOCMD
3925 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3926 if (other_buffer)
3927 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3928#endif
3929
3930#ifdef FEAT_TITLE
3931 maketitle();
3932#endif
3933 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003934 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 if (restart_edit)
3936 redraw_later(VALID); /* causes status line redraw */
3937
3938 /* set window height to desired minimal value */
3939 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3940 win_setheight((int)p_wh);
3941 else if (curwin->w_height == 0)
3942 win_setheight(1);
3943
3944#ifdef FEAT_VERTSPLIT
3945 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003946 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 win_setwidth((int)p_wiw);
3948#endif
3949
3950#ifdef FEAT_MOUSE
3951 setmouse(); /* in case jumped to/from help buffer */
3952#endif
3953
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003954#ifdef FEAT_AUTOCHDIR
3955 /* Change directories when the 'acd' option is set on and after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 * switching windows. */
3957 if (p_acd && curbuf->b_ffname != NULL
3958 && vim_chdirfile(curbuf->b_ffname) == OK)
3959 shorten_fnames(TRUE);
3960#endif
3961}
3962
3963#endif /* FEAT_WINDOWS */
3964
3965#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3966/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003967 * Jump to the first open window that contains buffer "buf", if one exists.
3968 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 */
3970 win_T *
3971buf_jump_open_win(buf)
3972 buf_T *buf;
3973{
3974# ifdef FEAT_WINDOWS
3975 win_T *wp;
3976
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003977 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 if (wp->w_buffer == buf)
3979 break;
3980 if (wp != NULL)
3981 win_enter(wp, FALSE);
3982 return wp;
3983# else
3984 if (curwin->w_buffer == buf)
3985 return curwin;
3986 return NULL;
3987# endif
3988}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003989
3990/*
3991 * Jump to the first open window in any tab page that contains buffer "buf",
3992 * if one exists.
3993 * Returns a pointer to the window found, otherwise NULL.
3994 */
3995 win_T *
3996buf_jump_open_tab(buf)
3997 buf_T *buf;
3998{
3999# ifdef FEAT_WINDOWS
4000 win_T *wp;
4001 tabpage_T *tp;
4002
4003 /* First try the current tab page. */
4004 wp = buf_jump_open_win(buf);
4005 if (wp != NULL)
4006 return wp;
4007
4008 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4009 if (tp != curtab)
4010 {
4011 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4012 if (wp->w_buffer == buf)
4013 break;
4014 if (wp != NULL)
4015 {
4016 goto_tabpage_win(tp, wp);
4017 if (curwin != wp)
4018 wp = NULL; /* something went wrong */
4019 break;
4020 }
4021 }
4022
4023 return wp;
4024# else
4025 if (curwin->w_buffer == buf)
4026 return curwin;
4027 return NULL;
4028# endif
4029}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030#endif
4031
4032/*
4033 * allocate a window structure and link it in the window list
4034 */
4035/*ARGSUSED*/
4036 static win_T *
4037win_alloc(after)
4038 win_T *after;
4039{
4040 win_T *newwin;
4041
4042 /*
4043 * allocate window structure and linesizes arrays
4044 */
4045 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4046 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4047 {
4048 vim_free(newwin);
4049 newwin = NULL;
4050 }
4051
4052 if (newwin != NULL)
4053 {
4054 /*
4055 * link the window in the window list
4056 */
4057#ifdef FEAT_WINDOWS
4058 win_append(after, newwin);
4059#endif
4060#ifdef FEAT_VERTSPLIT
4061 newwin->w_wincol = 0;
4062 newwin->w_width = Columns;
4063#endif
4064
4065 /* position the display and the cursor at the top of the file. */
4066 newwin->w_topline = 1;
4067#ifdef FEAT_DIFF
4068 newwin->w_topfill = 0;
4069#endif
4070 newwin->w_botline = 2;
4071 newwin->w_cursor.lnum = 1;
4072#ifdef FEAT_SCROLLBIND
4073 newwin->w_scbind_pos = 1;
4074#endif
4075
4076 /* We won't calculate w_fraction until resizing the window */
4077 newwin->w_fraction = 0;
4078 newwin->w_prev_fraction_row = -1;
4079
4080#ifdef FEAT_GUI
4081 if (gui.in_use)
4082 {
4083 out_flush();
4084 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4085 SBAR_LEFT, newwin);
4086 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4087 SBAR_RIGHT, newwin);
4088 }
4089#endif
4090#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004091 /* init w: variables */
4092 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093#endif
4094#ifdef FEAT_FOLDING
4095 foldInitWin(newwin);
4096#endif
4097 }
4098 return newwin;
4099}
4100
4101#if defined(FEAT_WINDOWS) || defined(PROTO)
4102
4103/*
4104 * remove window 'wp' from the window list and free the structure
4105 */
4106 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004107win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004109 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110{
4111 int i;
4112
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004113#ifdef FEAT_MZSCHEME
4114 mzscheme_window_free(wp);
4115#endif
4116
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117#ifdef FEAT_PERL
4118 perl_win_free(wp);
4119#endif
4120
4121#ifdef FEAT_PYTHON
4122 python_window_free(wp);
4123#endif
4124
4125#ifdef FEAT_TCL
4126 tcl_window_free(wp);
4127#endif
4128
4129#ifdef FEAT_RUBY
4130 ruby_window_free(wp);
4131#endif
4132
4133 clear_winopt(&wp->w_onebuf_opt);
4134 clear_winopt(&wp->w_allbuf_opt);
4135
4136#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004137 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138#endif
4139
4140 if (prevwin == wp)
4141 prevwin = NULL;
4142 win_free_lsize(wp);
4143
4144 for (i = 0; i < wp->w_tagstacklen; ++i)
4145 vim_free(wp->w_tagstack[i].tagname);
4146
4147 vim_free(wp->w_localdir);
4148#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004149 vim_free(wp->w_match[0].regprog);
4150 vim_free(wp->w_match[1].regprog);
4151 vim_free(wp->w_match[2].regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152#endif
4153#ifdef FEAT_JUMPLIST
4154 free_jumplist(wp);
4155#endif
4156
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004157#ifdef FEAT_QUICKFIX
4158 qf_free_all(wp);
4159#endif
4160
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161#ifdef FEAT_GUI
4162 if (gui.in_use)
4163 {
4164 out_flush();
4165 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4166 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4167 }
4168#endif /* FEAT_GUI */
4169
Bram Moolenaarf740b292006-02-16 22:11:02 +00004170 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 vim_free(wp);
4172}
4173
4174/*
4175 * Append window "wp" in the window list after window "after".
4176 */
4177 static void
4178win_append(after, wp)
4179 win_T *after, *wp;
4180{
4181 win_T *before;
4182
4183 if (after == NULL) /* after NULL is in front of the first */
4184 before = firstwin;
4185 else
4186 before = after->w_next;
4187
4188 wp->w_next = before;
4189 wp->w_prev = after;
4190 if (after == NULL)
4191 firstwin = wp;
4192 else
4193 after->w_next = wp;
4194 if (before == NULL)
4195 lastwin = wp;
4196 else
4197 before->w_prev = wp;
4198}
4199
4200/*
4201 * Remove a window from the window list.
4202 */
4203 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004204win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004206 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207{
4208 if (wp->w_prev != NULL)
4209 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004210 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004212 else
4213 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 if (wp->w_next != NULL)
4215 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004216 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004218 else
4219 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220}
4221
4222/*
4223 * Append frame "frp" in a frame list after frame "after".
4224 */
4225 static void
4226frame_append(after, frp)
4227 frame_T *after, *frp;
4228{
4229 frp->fr_next = after->fr_next;
4230 after->fr_next = frp;
4231 if (frp->fr_next != NULL)
4232 frp->fr_next->fr_prev = frp;
4233 frp->fr_prev = after;
4234}
4235
4236/*
4237 * Insert frame "frp" in a frame list before frame "before".
4238 */
4239 static void
4240frame_insert(before, frp)
4241 frame_T *before, *frp;
4242{
4243 frp->fr_next = before;
4244 frp->fr_prev = before->fr_prev;
4245 before->fr_prev = frp;
4246 if (frp->fr_prev != NULL)
4247 frp->fr_prev->fr_next = frp;
4248 else
4249 frp->fr_parent->fr_child = frp;
4250}
4251
4252/*
4253 * Remove a frame from a frame list.
4254 */
4255 static void
4256frame_remove(frp)
4257 frame_T *frp;
4258{
4259 if (frp->fr_prev != NULL)
4260 frp->fr_prev->fr_next = frp->fr_next;
4261 else
4262 frp->fr_parent->fr_child = frp->fr_next;
4263 if (frp->fr_next != NULL)
4264 frp->fr_next->fr_prev = frp->fr_prev;
4265}
4266
4267#endif /* FEAT_WINDOWS */
4268
4269/*
4270 * Allocate w_lines[] for window "wp".
4271 * Return FAIL for failure, OK for success.
4272 */
4273 int
4274win_alloc_lines(wp)
4275 win_T *wp;
4276{
4277 wp->w_lines_valid = 0;
4278 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
4279 if (wp->w_lines == NULL)
4280 return FAIL;
4281 return OK;
4282}
4283
4284/*
4285 * free lsize arrays for a window
4286 */
4287 void
4288win_free_lsize(wp)
4289 win_T *wp;
4290{
4291 vim_free(wp->w_lines);
4292 wp->w_lines = NULL;
4293}
4294
4295/*
4296 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004297 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 */
4299 void
4300shell_new_rows()
4301{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004302 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
4304 if (firstwin == NULL) /* not initialized yet */
4305 return;
4306#ifdef FEAT_WINDOWS
4307 if (h < frame_minheight(topframe, NULL))
4308 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004309
4310 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 * that doesn't result in the right height, forget about that option. */
4312 frame_new_height(topframe, h, FALSE, TRUE);
4313 if (topframe->fr_height != h)
4314 frame_new_height(topframe, h, FALSE, FALSE);
4315
4316 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4317#else
4318 if (h < 1)
4319 h = 1;
4320 win_new_height(firstwin, h);
4321#endif
4322 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004323#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004324 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004325#endif
4326
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327#if 0
4328 /* Disabled: don't want making the screen smaller make a window larger. */
4329 if (p_ea)
4330 win_equal(curwin, FALSE, 'v');
4331#endif
4332}
4333
4334#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4335/*
4336 * Called from win_new_shellsize() after Columns changed.
4337 */
4338 void
4339shell_new_columns()
4340{
4341 if (firstwin == NULL) /* not initialized yet */
4342 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004343
4344 /* First try setting the widths of windows with 'winfixwidth'. If that
4345 * doesn't result in the right width, forget about that option. */
4346 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4347 if (topframe->fr_width != Columns)
4348 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4349
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4351#if 0
4352 /* Disabled: don't want making the screen smaller make a window larger. */
4353 if (p_ea)
4354 win_equal(curwin, FALSE, 'h');
4355#endif
4356}
4357#endif
4358
4359#if defined(FEAT_CMDWIN) || defined(PROTO)
4360/*
4361 * Save the size of all windows in "gap".
4362 */
4363 void
4364win_size_save(gap)
4365 garray_T *gap;
4366
4367{
4368 win_T *wp;
4369
4370 ga_init2(gap, (int)sizeof(int), 1);
4371 if (ga_grow(gap, win_count() * 2) == OK)
4372 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4373 {
4374 ((int *)gap->ga_data)[gap->ga_len++] =
4375 wp->w_width + wp->w_vsep_width;
4376 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4377 }
4378}
4379
4380/*
4381 * Restore window sizes, but only if the number of windows is still the same.
4382 * Does not free the growarray.
4383 */
4384 void
4385win_size_restore(gap)
4386 garray_T *gap;
4387{
4388 win_T *wp;
4389 int i;
4390
4391 if (win_count() * 2 == gap->ga_len)
4392 {
4393 i = 0;
4394 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4395 {
4396 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4397 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4398 }
4399 /* recompute the window positions */
4400 (void)win_comp_pos();
4401 }
4402}
4403#endif /* FEAT_CMDWIN */
4404
4405#if defined(FEAT_WINDOWS) || defined(PROTO)
4406/*
4407 * Update the position for all windows, using the width and height of the
4408 * frames.
4409 * Returns the row just after the last window.
4410 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004411 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412win_comp_pos()
4413{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004414 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 int col = 0;
4416
4417 frame_comp_pos(topframe, &row, &col);
4418 return row;
4419}
4420
4421/*
4422 * Update the position of the windows in frame "topfrp", using the width and
4423 * height of the frames.
4424 * "*row" and "*col" are the top-left position of the frame. They are updated
4425 * to the bottom-right position plus one.
4426 */
4427 static void
4428frame_comp_pos(topfrp, row, col)
4429 frame_T *topfrp;
4430 int *row;
4431 int *col;
4432{
4433 win_T *wp;
4434 frame_T *frp;
4435#ifdef FEAT_VERTSPLIT
4436 int startcol;
4437 int startrow;
4438#endif
4439
4440 wp = topfrp->fr_win;
4441 if (wp != NULL)
4442 {
4443 if (wp->w_winrow != *row
4444#ifdef FEAT_VERTSPLIT
4445 || wp->w_wincol != *col
4446#endif
4447 )
4448 {
4449 /* position changed, redraw */
4450 wp->w_winrow = *row;
4451#ifdef FEAT_VERTSPLIT
4452 wp->w_wincol = *col;
4453#endif
4454 redraw_win_later(wp, NOT_VALID);
4455 wp->w_redr_status = TRUE;
4456 }
4457 *row += wp->w_height + wp->w_status_height;
4458#ifdef FEAT_VERTSPLIT
4459 *col += wp->w_width + wp->w_vsep_width;
4460#endif
4461 }
4462 else
4463 {
4464#ifdef FEAT_VERTSPLIT
4465 startrow = *row;
4466 startcol = *col;
4467#endif
4468 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4469 {
4470#ifdef FEAT_VERTSPLIT
4471 if (topfrp->fr_layout == FR_ROW)
4472 *row = startrow; /* all frames are at the same row */
4473 else
4474 *col = startcol; /* all frames are at the same col */
4475#endif
4476 frame_comp_pos(frp, row, col);
4477 }
4478 }
4479}
4480
4481#endif /* FEAT_WINDOWS */
4482
4483/*
4484 * Set current window height and take care of repositioning other windows to
4485 * fit around it.
4486 */
4487 void
4488win_setheight(height)
4489 int height;
4490{
4491 win_setheight_win(height, curwin);
4492}
4493
4494/*
4495 * Set the window height of window "win" and take care of repositioning other
4496 * windows to fit around it.
4497 */
4498 void
4499win_setheight_win(height, win)
4500 int height;
4501 win_T *win;
4502{
4503 int row;
4504
4505 if (win == curwin)
4506 {
4507 /* Always keep current window at least one line high, even when
4508 * 'winminheight' is zero. */
4509#ifdef FEAT_WINDOWS
4510 if (height < p_wmh)
4511 height = p_wmh;
4512#endif
4513 if (height == 0)
4514 height = 1;
4515 }
4516
4517#ifdef FEAT_WINDOWS
4518 frame_setheight(win->w_frame, height + win->w_status_height);
4519
4520 /* recompute the window positions */
4521 row = win_comp_pos();
4522#else
4523 if (height > topframe->fr_height)
4524 height = topframe->fr_height;
4525 win->w_height = height;
4526 row = height;
4527#endif
4528
4529 /*
4530 * If there is extra space created between the last window and the command
4531 * line, clear it.
4532 */
4533 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4534 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4535 cmdline_row = row;
4536 msg_row = row;
4537 msg_col = 0;
4538
4539 redraw_all_later(NOT_VALID);
4540}
4541
4542#if defined(FEAT_WINDOWS) || defined(PROTO)
4543
4544/*
4545 * Set the height of a frame to "height" and take care that all frames and
4546 * windows inside it are resized. Also resize frames on the left and right if
4547 * the are in the same FR_ROW frame.
4548 *
4549 * Strategy:
4550 * If the frame is part of a FR_COL frame, try fitting the frame in that
4551 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4552 * go to containing frames to resize them and make room.
4553 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4554 * Check for the minimal height of the FR_ROW frame.
4555 * At the top level we can also use change the command line height.
4556 */
4557 static void
4558frame_setheight(curfrp, height)
4559 frame_T *curfrp;
4560 int height;
4561{
4562 int room; /* total number of lines available */
4563 int take; /* number of lines taken from other windows */
4564 int room_cmdline; /* lines available from cmdline */
4565 int run;
4566 frame_T *frp;
4567 int h;
4568 int room_reserved;
4569
4570 /* If the height already is the desired value, nothing to do. */
4571 if (curfrp->fr_height == height)
4572 return;
4573
4574 if (curfrp->fr_parent == NULL)
4575 {
4576 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004577 if (height > ROWS_AVAIL)
4578 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 if (height > 0)
4580 frame_new_height(curfrp, height, FALSE, FALSE);
4581 }
4582 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4583 {
4584 /* Row of frames: Also need to resize frames left and right of this
4585 * one. First check for the minimal height of these. */
4586 h = frame_minheight(curfrp->fr_parent, NULL);
4587 if (height < h)
4588 height = h;
4589 frame_setheight(curfrp->fr_parent, height);
4590 }
4591 else
4592 {
4593 /*
4594 * Column of frames: try to change only frames in this column.
4595 */
4596#ifdef FEAT_VERTSPLIT
4597 /*
4598 * Do this twice:
4599 * 1: compute room available, if it's not enough try resizing the
4600 * containing frame.
4601 * 2: compute the room available and adjust the height to it.
4602 * Try not to reduce the height of a window with 'winfixheight' set.
4603 */
4604 for (run = 1; run <= 2; ++run)
4605#else
4606 for (;;)
4607#endif
4608 {
4609 room = 0;
4610 room_reserved = 0;
4611 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4612 frp = frp->fr_next)
4613 {
4614 if (frp != curfrp
4615 && frp->fr_win != NULL
4616 && frp->fr_win->w_p_wfh)
4617 room_reserved += frp->fr_height;
4618 room += frp->fr_height;
4619 if (frp != curfrp)
4620 room -= frame_minheight(frp, NULL);
4621 }
4622#ifdef FEAT_VERTSPLIT
4623 if (curfrp->fr_width != Columns)
4624 room_cmdline = 0;
4625 else
4626#endif
4627 {
4628 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4629 + lastwin->w_height + lastwin->w_status_height);
4630 if (room_cmdline < 0)
4631 room_cmdline = 0;
4632 }
4633
4634 if (height <= room + room_cmdline)
4635 break;
4636#ifdef FEAT_VERTSPLIT
4637 if (run == 2 || curfrp->fr_width == Columns)
4638#endif
4639 {
4640 if (height > room + room_cmdline)
4641 height = room + room_cmdline;
4642 break;
4643 }
4644#ifdef FEAT_VERTSPLIT
4645 frame_setheight(curfrp->fr_parent, height
4646 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4647#endif
4648 /*NOTREACHED*/
4649 }
4650
4651 /*
4652 * Compute the number of lines we will take from others frames (can be
4653 * negative!).
4654 */
4655 take = height - curfrp->fr_height;
4656
4657 /* If there is not enough room, also reduce the height of a window
4658 * with 'winfixheight' set. */
4659 if (height > room + room_cmdline - room_reserved)
4660 room_reserved = room + room_cmdline - height;
4661 /* If there is only a 'winfixheight' window and making the
4662 * window smaller, need to make the other window taller. */
4663 if (take < 0 && room - curfrp->fr_height < room_reserved)
4664 room_reserved = 0;
4665
4666 if (take > 0 && room_cmdline > 0)
4667 {
4668 /* use lines from cmdline first */
4669 if (take < room_cmdline)
4670 room_cmdline = take;
4671 take -= room_cmdline;
4672 topframe->fr_height += room_cmdline;
4673 }
4674
4675 /*
4676 * set the current frame to the new height
4677 */
4678 frame_new_height(curfrp, height, FALSE, FALSE);
4679
4680 /*
4681 * First take lines from the frames after the current frame. If
4682 * that is not enough, takes lines from frames above the current
4683 * frame.
4684 */
4685 for (run = 0; run < 2; ++run)
4686 {
4687 if (run == 0)
4688 frp = curfrp->fr_next; /* 1st run: start with next window */
4689 else
4690 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4691 while (frp != NULL && take != 0)
4692 {
4693 h = frame_minheight(frp, NULL);
4694 if (room_reserved > 0
4695 && frp->fr_win != NULL
4696 && frp->fr_win->w_p_wfh)
4697 {
4698 if (room_reserved >= frp->fr_height)
4699 room_reserved -= frp->fr_height;
4700 else
4701 {
4702 if (frp->fr_height - room_reserved > take)
4703 room_reserved = frp->fr_height - take;
4704 take -= frp->fr_height - room_reserved;
4705 frame_new_height(frp, room_reserved, FALSE, FALSE);
4706 room_reserved = 0;
4707 }
4708 }
4709 else
4710 {
4711 if (frp->fr_height - take < h)
4712 {
4713 take -= frp->fr_height - h;
4714 frame_new_height(frp, h, FALSE, FALSE);
4715 }
4716 else
4717 {
4718 frame_new_height(frp, frp->fr_height - take,
4719 FALSE, FALSE);
4720 take = 0;
4721 }
4722 }
4723 if (run == 0)
4724 frp = frp->fr_next;
4725 else
4726 frp = frp->fr_prev;
4727 }
4728 }
4729 }
4730}
4731
4732#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4733/*
4734 * Set current window width and take care of repositioning other windows to
4735 * fit around it.
4736 */
4737 void
4738win_setwidth(width)
4739 int width;
4740{
4741 win_setwidth_win(width, curwin);
4742}
4743
4744 void
4745win_setwidth_win(width, wp)
4746 int width;
4747 win_T *wp;
4748{
4749 /* Always keep current window at least one column wide, even when
4750 * 'winminwidth' is zero. */
4751 if (wp == curwin)
4752 {
4753 if (width < p_wmw)
4754 width = p_wmw;
4755 if (width == 0)
4756 width = 1;
4757 }
4758
4759 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4760
4761 /* recompute the window positions */
4762 (void)win_comp_pos();
4763
4764 redraw_all_later(NOT_VALID);
4765}
4766
4767/*
4768 * Set the width of a frame to "width" and take care that all frames and
4769 * windows inside it are resized. Also resize frames above and below if the
4770 * are in the same FR_ROW frame.
4771 *
4772 * Strategy is similar to frame_setheight().
4773 */
4774 static void
4775frame_setwidth(curfrp, width)
4776 frame_T *curfrp;
4777 int width;
4778{
4779 int room; /* total number of lines available */
4780 int take; /* number of lines taken from other windows */
4781 int run;
4782 frame_T *frp;
4783 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004784 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
4786 /* If the width already is the desired value, nothing to do. */
4787 if (curfrp->fr_width == width)
4788 return;
4789
4790 if (curfrp->fr_parent == NULL)
4791 /* topframe: can't change width */
4792 return;
4793
4794 if (curfrp->fr_parent->fr_layout == FR_COL)
4795 {
4796 /* Column of frames: Also need to resize frames above and below of
4797 * this one. First check for the minimal width of these. */
4798 w = frame_minwidth(curfrp->fr_parent, NULL);
4799 if (width < w)
4800 width = w;
4801 frame_setwidth(curfrp->fr_parent, width);
4802 }
4803 else
4804 {
4805 /*
4806 * Row of frames: try to change only frames in this row.
4807 *
4808 * Do this twice:
4809 * 1: compute room available, if it's not enough try resizing the
4810 * containing frame.
4811 * 2: compute the room available and adjust the width to it.
4812 */
4813 for (run = 1; run <= 2; ++run)
4814 {
4815 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004816 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4818 frp = frp->fr_next)
4819 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004820 if (frp != curfrp
4821 && frp->fr_win != NULL
4822 && frp->fr_win->w_p_wfw)
4823 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 room += frp->fr_width;
4825 if (frp != curfrp)
4826 room -= frame_minwidth(frp, NULL);
4827 }
4828
4829 if (width <= room)
4830 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004831 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 {
4833 if (width > room)
4834 width = room;
4835 break;
4836 }
4837 frame_setwidth(curfrp->fr_parent, width
4838 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4839 }
4840
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 /*
4842 * Compute the number of lines we will take from others frames (can be
4843 * negative!).
4844 */
4845 take = width - curfrp->fr_width;
4846
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004847 /* If there is not enough room, also reduce the width of a window
4848 * with 'winfixwidth' set. */
4849 if (width > room - room_reserved)
4850 room_reserved = room - width;
4851 /* If there is only a 'winfixwidth' window and making the
4852 * window smaller, need to make the other window narrower. */
4853 if (take < 0 && room - curfrp->fr_width < room_reserved)
4854 room_reserved = 0;
4855
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 /*
4857 * set the current frame to the new width
4858 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004859 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860
4861 /*
4862 * First take lines from the frames right of the current frame. If
4863 * that is not enough, takes lines from frames left of the current
4864 * frame.
4865 */
4866 for (run = 0; run < 2; ++run)
4867 {
4868 if (run == 0)
4869 frp = curfrp->fr_next; /* 1st run: start with next window */
4870 else
4871 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4872 while (frp != NULL && take != 0)
4873 {
4874 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004875 if (room_reserved > 0
4876 && frp->fr_win != NULL
4877 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004879 if (room_reserved >= frp->fr_width)
4880 room_reserved -= frp->fr_width;
4881 else
4882 {
4883 if (frp->fr_width - room_reserved > take)
4884 room_reserved = frp->fr_width - take;
4885 take -= frp->fr_width - room_reserved;
4886 frame_new_width(frp, room_reserved, FALSE, FALSE);
4887 room_reserved = 0;
4888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 }
4890 else
4891 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004892 if (frp->fr_width - take < w)
4893 {
4894 take -= frp->fr_width - w;
4895 frame_new_width(frp, w, FALSE, FALSE);
4896 }
4897 else
4898 {
4899 frame_new_width(frp, frp->fr_width - take,
4900 FALSE, FALSE);
4901 take = 0;
4902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
4904 if (run == 0)
4905 frp = frp->fr_next;
4906 else
4907 frp = frp->fr_prev;
4908 }
4909 }
4910 }
4911}
4912#endif /* FEAT_VERTSPLIT */
4913
4914/*
4915 * Check 'winminheight' for a valid value.
4916 */
4917 void
4918win_setminheight()
4919{
4920 int room;
4921 int first = TRUE;
4922 win_T *wp;
4923
4924 /* loop until there is a 'winminheight' that is possible */
4925 while (p_wmh > 0)
4926 {
4927 /* TODO: handle vertical splits */
4928 room = -p_wh;
4929 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4930 room += wp->w_height - p_wmh;
4931 if (room >= 0)
4932 break;
4933 --p_wmh;
4934 if (first)
4935 {
4936 EMSG(_(e_noroom));
4937 first = FALSE;
4938 }
4939 }
4940}
4941
4942#ifdef FEAT_MOUSE
4943
4944/*
4945 * Status line of dragwin is dragged "offset" lines down (negative is up).
4946 */
4947 void
4948win_drag_status_line(dragwin, offset)
4949 win_T *dragwin;
4950 int offset;
4951{
4952 frame_T *curfr;
4953 frame_T *fr;
4954 int room;
4955 int row;
4956 int up; /* if TRUE, drag status line up, otherwise down */
4957 int n;
4958
4959 fr = dragwin->w_frame;
4960 curfr = fr;
4961 if (fr != topframe) /* more than one window */
4962 {
4963 fr = fr->fr_parent;
4964 /* When the parent frame is not a column of frames, its parent should
4965 * be. */
4966 if (fr->fr_layout != FR_COL)
4967 {
4968 curfr = fr;
4969 if (fr != topframe) /* only a row of windows, may drag statusline */
4970 fr = fr->fr_parent;
4971 }
4972 }
4973
4974 /* If this is the last frame in a column, may want to resize the parent
4975 * frame instead (go two up to skip a row of frames). */
4976 while (curfr != topframe && curfr->fr_next == NULL)
4977 {
4978 if (fr != topframe)
4979 fr = fr->fr_parent;
4980 curfr = fr;
4981 if (fr != topframe)
4982 fr = fr->fr_parent;
4983 }
4984
4985 if (offset < 0) /* drag up */
4986 {
4987 up = TRUE;
4988 offset = -offset;
4989 /* sum up the room of the current frame and above it */
4990 if (fr == curfr)
4991 {
4992 /* only one window */
4993 room = fr->fr_height - frame_minheight(fr, NULL);
4994 }
4995 else
4996 {
4997 room = 0;
4998 for (fr = fr->fr_child; ; fr = fr->fr_next)
4999 {
5000 room += fr->fr_height - frame_minheight(fr, NULL);
5001 if (fr == curfr)
5002 break;
5003 }
5004 }
5005 fr = curfr->fr_next; /* put fr at frame that grows */
5006 }
5007 else /* drag down */
5008 {
5009 up = FALSE;
5010 /*
5011 * Only dragging the last status line can reduce p_ch.
5012 */
5013 room = Rows - cmdline_row;
5014 if (curfr->fr_next == NULL)
5015 room -= 1;
5016 else
5017 room -= p_ch;
5018 if (room < 0)
5019 room = 0;
5020 /* sum up the room of frames below of the current one */
5021 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5022 room += fr->fr_height - frame_minheight(fr, NULL);
5023 fr = curfr; /* put fr at window that grows */
5024 }
5025
5026 if (room < offset) /* Not enough room */
5027 offset = room; /* Move as far as we can */
5028 if (offset <= 0)
5029 return;
5030
5031 /*
5032 * Grow frame fr by "offset" lines.
5033 * Doesn't happen when dragging the last status line up.
5034 */
5035 if (fr != NULL)
5036 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5037
5038 if (up)
5039 fr = curfr; /* current frame gets smaller */
5040 else
5041 fr = curfr->fr_next; /* next frame gets smaller */
5042
5043 /*
5044 * Now make the other frames smaller.
5045 */
5046 while (fr != NULL && offset > 0)
5047 {
5048 n = frame_minheight(fr, NULL);
5049 if (fr->fr_height - offset <= n)
5050 {
5051 offset -= fr->fr_height - n;
5052 frame_new_height(fr, n, !up, FALSE);
5053 }
5054 else
5055 {
5056 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5057 break;
5058 }
5059 if (up)
5060 fr = fr->fr_prev;
5061 else
5062 fr = fr->fr_next;
5063 }
5064 row = win_comp_pos();
5065 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5066 cmdline_row = row;
5067 p_ch = Rows - cmdline_row;
5068 if (p_ch < 1)
5069 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005070 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005071 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 showmode();
5073}
5074
5075#ifdef FEAT_VERTSPLIT
5076/*
5077 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5078 */
5079 void
5080win_drag_vsep_line(dragwin, offset)
5081 win_T *dragwin;
5082 int offset;
5083{
5084 frame_T *curfr;
5085 frame_T *fr;
5086 int room;
5087 int left; /* if TRUE, drag separator line left, otherwise right */
5088 int n;
5089
5090 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005091 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 return;
5093 curfr = fr;
5094 fr = fr->fr_parent;
5095 /* When the parent frame is not a row of frames, its parent should be. */
5096 if (fr->fr_layout != FR_ROW)
5097 {
5098 if (fr == topframe) /* only a column of windows (cannot happen?) */
5099 return;
5100 curfr = fr;
5101 fr = fr->fr_parent;
5102 }
5103
5104 /* If this is the last frame in a row, may want to resize a parent
5105 * frame instead. */
5106 while (curfr->fr_next == NULL)
5107 {
5108 if (fr == topframe)
5109 break;
5110 curfr = fr;
5111 fr = fr->fr_parent;
5112 if (fr != topframe)
5113 {
5114 curfr = fr;
5115 fr = fr->fr_parent;
5116 }
5117 }
5118
5119 if (offset < 0) /* drag left */
5120 {
5121 left = TRUE;
5122 offset = -offset;
5123 /* sum up the room of the current frame and left of it */
5124 room = 0;
5125 for (fr = fr->fr_child; ; fr = fr->fr_next)
5126 {
5127 room += fr->fr_width - frame_minwidth(fr, NULL);
5128 if (fr == curfr)
5129 break;
5130 }
5131 fr = curfr->fr_next; /* put fr at frame that grows */
5132 }
5133 else /* drag right */
5134 {
5135 left = FALSE;
5136 /* sum up the room of frames right of the current one */
5137 room = 0;
5138 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5139 room += fr->fr_width - frame_minwidth(fr, NULL);
5140 fr = curfr; /* put fr at window that grows */
5141 }
5142
5143 if (room < offset) /* Not enough room */
5144 offset = room; /* Move as far as we can */
5145 if (offset <= 0) /* No room at all, quit. */
5146 return;
5147
5148 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005149 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150
5151 /* shrink other frames: current and at the left or at the right */
5152 if (left)
5153 fr = curfr; /* current frame gets smaller */
5154 else
5155 fr = curfr->fr_next; /* next frame gets smaller */
5156
5157 while (fr != NULL && offset > 0)
5158 {
5159 n = frame_minwidth(fr, NULL);
5160 if (fr->fr_width - offset <= n)
5161 {
5162 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005163 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165 else
5166 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005167 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 break;
5169 }
5170 if (left)
5171 fr = fr->fr_prev;
5172 else
5173 fr = fr->fr_next;
5174 }
5175 (void)win_comp_pos();
5176 redraw_all_later(NOT_VALID);
5177}
5178#endif /* FEAT_VERTSPLIT */
5179#endif /* FEAT_MOUSE */
5180
5181#endif /* FEAT_WINDOWS */
5182
5183/*
5184 * Set the height of a window.
5185 * This takes care of the things inside the window, not what happens to the
5186 * window position, the frame or to other windows.
5187 */
5188 static void
5189win_new_height(wp, height)
5190 win_T *wp;
5191 int height;
5192{
5193 linenr_T lnum;
Bram Moolenaar34114692005-01-02 11:28:13 +00005194 linenr_T bot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 int sline, line_size;
Bram Moolenaar34114692005-01-02 11:28:13 +00005196 int space;
5197 int did_below = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198#define FRACTION_MULT 16384L
5199
5200 /* Don't want a negative height. Happens when splitting a tiny window.
5201 * Will equalize heights soon to fix it. */
5202 if (height < 0)
5203 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005204 if (wp->w_height == height)
5205 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206
5207 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5208 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5209 + FRACTION_MULT / 2) / (long)wp->w_height;
5210
5211 wp->w_height = height;
5212 wp->w_skipcol = 0;
5213
5214 /* Don't change w_topline when height is zero. Don't set w_topline when
5215 * 'scrollbind' is set and this isn't the current window. */
5216 if (height > 0
5217#ifdef FEAT_SCROLLBIND
5218 && (!wp->w_p_scb || wp == curwin)
5219#endif
5220 )
5221 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005222 /*
5223 * Find a value for w_topline that shows the cursor at the same
5224 * relative position in the window as before (more or less).
5225 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 lnum = wp->w_cursor.lnum;
5227 if (lnum < 1) /* can happen when starting up */
5228 lnum = 1;
5229 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5230 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5231 sline = wp->w_wrow - line_size;
5232 if (sline < 0)
5233 {
5234 /*
5235 * Cursor line would go off top of screen if w_wrow was this high.
5236 */
5237 wp->w_wrow = line_size;
5238 }
5239 else
5240 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005241 space = height;
5242 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005244 space -= line_size;
5245 if (space > 0 && sline <= 0 && !did_below)
5246 {
5247 /* Try to use "~" lines below the text to avoid that text
5248 * is above the window while there are empty lines.
5249 * Subtract the rows below the cursor from "space" and
5250 * give the rest to "sline". */
5251 did_below = TRUE;
5252 bot = wp->w_cursor.lnum;
5253 while (space > 0)
5254 {
5255 if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
5256 space = 0;
5257 else
5258 {
5259#ifdef FEAT_FOLDING
5260 hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
5261#endif
5262 if (bot >= wp->w_buffer->b_ml.ml_line_count)
5263 break;
5264 ++bot;
5265 space -= plines_win(wp, bot, TRUE);
5266 }
5267 }
5268 if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
5269 sline += space;
5270 }
5271 if (sline <= 0)
5272 break;
5273
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274#ifdef FEAT_FOLDING
5275 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5276 if (lnum == 1)
5277 {
5278 /* first line in buffer is folded */
5279 line_size = 1;
5280 --sline;
5281 break;
5282 }
5283#endif
5284 --lnum;
5285#ifdef FEAT_DIFF
5286 if (lnum == wp->w_topline)
5287 line_size = plines_win_nofill(wp, lnum, TRUE)
5288 + wp->w_topfill;
5289 else
5290#endif
5291 line_size = plines_win(wp, lnum, TRUE);
5292 sline -= line_size;
5293 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005294
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 if (sline < 0)
5296 {
5297 /*
5298 * Line we want at top would go off top of screen. Use next
5299 * line instead.
5300 */
5301#ifdef FEAT_FOLDING
5302 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5303#endif
5304 lnum++;
5305 wp->w_wrow -= line_size + sline;
5306 }
5307 else if (sline > 0)
5308 {
5309 /* First line of file reached, use that as topline. */
5310 lnum = 1;
5311 wp->w_wrow -= sline;
5312 }
5313 }
5314 set_topline(wp, lnum);
5315 }
5316
5317 if (wp == curwin)
5318 {
5319 if (p_so)
5320 update_topline();
5321 curs_columns(FALSE); /* validate w_wrow */
5322 }
5323 wp->w_prev_fraction_row = wp->w_wrow;
5324
5325 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005326 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327#ifdef FEAT_WINDOWS
5328 wp->w_redr_status = TRUE;
5329#endif
5330 invalidate_botline_win(wp);
5331}
5332
5333#ifdef FEAT_VERTSPLIT
5334/*
5335 * Set the width of a window.
5336 */
5337 static void
5338win_new_width(wp, width)
5339 win_T *wp;
5340 int width;
5341{
5342 wp->w_width = width;
5343 wp->w_lines_valid = 0;
5344 changed_line_abv_curs_win(wp);
5345 invalidate_botline_win(wp);
5346 if (wp == curwin)
5347 {
5348 update_topline();
5349 curs_columns(TRUE); /* validate w_wrow */
5350 }
5351 redraw_win_later(wp, NOT_VALID);
5352 wp->w_redr_status = TRUE;
5353}
5354#endif
5355
5356 void
5357win_comp_scroll(wp)
5358 win_T *wp;
5359{
5360 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5361 if (wp->w_p_scr == 0)
5362 wp->w_p_scr = 1;
5363}
5364
5365/*
5366 * command_height: called whenever p_ch has been changed
5367 */
5368 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005369command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370{
5371#ifdef FEAT_WINDOWS
5372 int h;
5373 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005374 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005376 /* Use the value of p_ch that we remembered. This is needed for when the
5377 * GUI starts up, we can't be sure in what order things happen. And when
5378 * p_ch was changed in another tab page. */
5379 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005380
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 /* Find bottom frame with width of screen. */
5382 frp = lastwin->w_frame;
5383# ifdef FEAT_VERTSPLIT
5384 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5385 frp = frp->fr_parent;
5386# endif
5387
5388 /* Avoid changing the height of a window with 'winfixheight' set. */
5389 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5390 && frp->fr_win->w_p_wfh)
5391 frp = frp->fr_prev;
5392
5393 if (starting != NO_SCREEN)
5394 {
5395 cmdline_row = Rows - p_ch;
5396
5397 if (p_ch > old_p_ch) /* p_ch got bigger */
5398 {
5399 while (p_ch > old_p_ch)
5400 {
5401 if (frp == NULL)
5402 {
5403 EMSG(_(e_noroom));
5404 p_ch = old_p_ch;
5405 cmdline_row = Rows - p_ch;
5406 break;
5407 }
5408 h = frp->fr_height - frame_minheight(frp, NULL);
5409 if (h > p_ch - old_p_ch)
5410 h = p_ch - old_p_ch;
5411 old_p_ch += h;
5412 frame_add_height(frp, -h);
5413 frp = frp->fr_prev;
5414 }
5415
5416 /* Recompute window positions. */
5417 (void)win_comp_pos();
5418
5419 /* clear the lines added to cmdline */
5420 if (full_screen)
5421 screen_fill((int)(cmdline_row), (int)Rows, 0,
5422 (int)Columns, ' ', ' ', 0);
5423 msg_row = cmdline_row;
5424 redraw_cmdline = TRUE;
5425 return;
5426 }
5427
5428 if (msg_row < cmdline_row)
5429 msg_row = cmdline_row;
5430 redraw_cmdline = TRUE;
5431 }
5432 frame_add_height(frp, (int)(old_p_ch - p_ch));
5433
5434 /* Recompute window positions. */
5435 if (frp != lastwin->w_frame)
5436 (void)win_comp_pos();
5437#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005439 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440#endif
5441}
5442
5443#if defined(FEAT_WINDOWS) || defined(PROTO)
5444/*
5445 * Resize frame "frp" to be "n" lines higher (negative for less high).
5446 * Also resize the frames it is contained in.
5447 */
5448 static void
5449frame_add_height(frp, n)
5450 frame_T *frp;
5451 int n;
5452{
5453 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5454 for (;;)
5455 {
5456 frp = frp->fr_parent;
5457 if (frp == NULL)
5458 break;
5459 frp->fr_height += n;
5460 }
5461}
5462
5463/*
5464 * Add or remove a status line for the bottom window(s), according to the
5465 * value of 'laststatus'.
5466 */
5467 void
5468last_status(morewin)
5469 int morewin; /* pretend there are two or more windows */
5470{
5471 /* Don't make a difference between horizontal or vertical split. */
5472 last_status_rec(topframe, (p_ls == 2
5473 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5474}
5475
5476 static void
5477last_status_rec(fr, statusline)
5478 frame_T *fr;
5479 int statusline;
5480{
5481 frame_T *fp;
5482 win_T *wp;
5483
5484 if (fr->fr_layout == FR_LEAF)
5485 {
5486 wp = fr->fr_win;
5487 if (wp->w_status_height != 0 && !statusline)
5488 {
5489 /* remove status line */
5490 win_new_height(wp, wp->w_height + 1);
5491 wp->w_status_height = 0;
5492 comp_col();
5493 }
5494 else if (wp->w_status_height == 0 && statusline)
5495 {
5496 /* Find a frame to take a line from. */
5497 fp = fr;
5498 while (fp->fr_height <= frame_minheight(fp, NULL))
5499 {
5500 if (fp == topframe)
5501 {
5502 EMSG(_(e_noroom));
5503 return;
5504 }
5505 /* In a column of frames: go to frame above. If already at
5506 * the top or in a row of frames: go to parent. */
5507 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5508 fp = fp->fr_prev;
5509 else
5510 fp = fp->fr_parent;
5511 }
5512 wp->w_status_height = 1;
5513 if (fp != fr)
5514 {
5515 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5516 frame_fix_height(wp);
5517 (void)win_comp_pos();
5518 }
5519 else
5520 win_new_height(wp, wp->w_height - 1);
5521 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005522 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 }
5524 }
5525#ifdef FEAT_VERTSPLIT
5526 else if (fr->fr_layout == FR_ROW)
5527 {
5528 /* vertically split windows, set status line for each one */
5529 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5530 last_status_rec(fp, statusline);
5531 }
5532#endif
5533 else
5534 {
5535 /* horizontally split window, set status line for last one */
5536 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5537 ;
5538 last_status_rec(fp, statusline);
5539 }
5540}
5541
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005542/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005543 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005544 */
5545 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005546tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005547{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005548#ifdef FEAT_GUI_TABLINE
5549 /* When the GUI has the tabline then this always returns zero. */
5550 if (gui_use_tabline())
5551 return 0;
5552#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005553 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005554 {
5555 case 0: return 0;
5556 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5557 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005558 return 1;
5559}
5560
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561#endif /* FEAT_WINDOWS */
5562
5563#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5564/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005565 * Get the file name at the cursor.
5566 * If Visual mode is active, use the selected text if it's in one line.
5567 * Returns the name in allocated memory, NULL for failure.
5568 */
5569 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005570grab_file_name(count, file_lnum)
5571 long count;
5572 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005573{
5574# ifdef FEAT_VISUAL
5575 if (VIsual_active)
5576 {
5577 int len;
5578 char_u *ptr;
5579
5580 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5581 return NULL;
5582 return find_file_name_in_path(ptr, len,
5583 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5584 }
5585# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005586 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5587 file_lnum);
5588
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005589}
5590
5591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 * Return the file name under or after the cursor.
5593 *
5594 * The 'path' option is searched if the file name is not absolute.
5595 * The string returned has been alloc'ed and should be freed by the caller.
5596 * NULL is returned if the file name or file is not found.
5597 *
5598 * options:
5599 * FNAME_MESS give error messages
5600 * FNAME_EXP expand to path
5601 * FNAME_HYP check for hypertext link
5602 * FNAME_INCL apply "includeexpr"
5603 */
5604 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005605file_name_at_cursor(options, count, file_lnum)
5606 int options;
5607 long count;
5608 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609{
5610 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005611 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5612 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613}
5614
5615/*
5616 * Return the name of the file under or after ptr[col].
5617 * Otherwise like file_name_at_cursor().
5618 */
5619 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005620file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 char_u *line;
5622 int col;
5623 int options;
5624 long count;
5625 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005626 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 char_u *ptr;
5629 int len;
5630
5631 /*
5632 * search forward for what could be the start of a file name
5633 */
5634 ptr = line + col;
5635 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005636 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 if (*ptr == NUL) /* nothing found */
5638 {
5639 if (options & FNAME_MESS)
5640 EMSG(_("E446: No file name under cursor"));
5641 return NULL;
5642 }
5643
5644 /*
5645 * Search backward for first char of the file name.
5646 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5647 */
5648 while (ptr > line)
5649 {
5650#ifdef FEAT_MBYTE
5651 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5652 ptr -= len + 1;
5653 else
5654#endif
5655 if (vim_isfilec(ptr[-1])
5656 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5657 --ptr;
5658 else
5659 break;
5660 }
5661
5662 /*
5663 * Search forward for the last char of the file name.
5664 * Also allow "://" when ':' is not in 'isfname'.
5665 */
5666 len = 0;
5667 while (vim_isfilec(ptr[len])
5668 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5669#ifdef FEAT_MBYTE
5670 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005671 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 else
5673#endif
5674 ++len;
5675
5676 /*
5677 * If there is trailing punctuation, remove it.
5678 * But don't remove "..", could be a directory name.
5679 */
5680 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5681 && ptr[len - 2] != '.')
5682 --len;
5683
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005684 if (file_lnum != NULL)
5685 {
5686 char_u *p;
5687
5688 /* Get the number after the file name and a separator character */
5689 p = ptr + len;
5690 p = skipwhite(p);
5691 if (*p != NUL)
5692 {
5693 if (!isdigit(*p))
5694 ++p; /* skip the separator */
5695 p = skipwhite(p);
5696 if (isdigit(*p))
5697 *file_lnum = (int)getdigits(&p);
5698 }
5699 }
5700
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5702}
5703
5704# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5705static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5706
5707 static char_u *
5708eval_includeexpr(ptr, len)
5709 char_u *ptr;
5710 int len;
5711{
5712 char_u *res;
5713
5714 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005715 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005716 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 set_vim_var_string(VV_FNAME, NULL, 0);
5718 return res;
5719}
5720#endif
5721
5722/*
5723 * Return the name of the file ptr[len] in 'path'.
5724 * Otherwise like file_name_at_cursor().
5725 */
5726 char_u *
5727find_file_name_in_path(ptr, len, options, count, rel_fname)
5728 char_u *ptr;
5729 int len;
5730 int options;
5731 long count;
5732 char_u *rel_fname; /* file we are searching relative to */
5733{
5734 char_u *file_name;
5735 int c;
5736# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5737 char_u *tofree = NULL;
5738
5739 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5740 {
5741 tofree = eval_includeexpr(ptr, len);
5742 if (tofree != NULL)
5743 {
5744 ptr = tofree;
5745 len = (int)STRLEN(ptr);
5746 }
5747 }
5748# endif
5749
5750 if (options & FNAME_EXP)
5751 {
5752 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5753 TRUE, rel_fname);
5754
5755# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5756 /*
5757 * If the file could not be found in a normal way, try applying
5758 * 'includeexpr' (unless done already).
5759 */
5760 if (file_name == NULL
5761 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5762 {
5763 tofree = eval_includeexpr(ptr, len);
5764 if (tofree != NULL)
5765 {
5766 ptr = tofree;
5767 len = (int)STRLEN(ptr);
5768 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5769 TRUE, rel_fname);
5770 }
5771 }
5772# endif
5773 if (file_name == NULL && (options & FNAME_MESS))
5774 {
5775 c = ptr[len];
5776 ptr[len] = NUL;
5777 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5778 ptr[len] = c;
5779 }
5780
5781 /* Repeat finding the file "count" times. This matters when it
5782 * appears several times in the path. */
5783 while (file_name != NULL && --count > 0)
5784 {
5785 vim_free(file_name);
5786 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5787 }
5788 }
5789 else
5790 file_name = vim_strnsave(ptr, len);
5791
5792# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5793 vim_free(tofree);
5794# endif
5795
5796 return file_name;
5797}
5798#endif /* FEAT_SEARCHPATH */
5799
5800/*
5801 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5802 * Also check for ":\\", which MS Internet Explorer accepts, return
5803 * URL_BACKSLASH.
5804 */
5805 static int
5806path_is_url(p)
5807 char_u *p;
5808{
5809 if (STRNCMP(p, "://", (size_t)3) == 0)
5810 return URL_SLASH;
5811 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5812 return URL_BACKSLASH;
5813 return 0;
5814}
5815
5816/*
5817 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5818 * Return URL_BACKSLASH for "name:\\".
5819 * Return zero otherwise.
5820 */
5821 int
5822path_with_url(fname)
5823 char_u *fname;
5824{
5825 char_u *p;
5826
5827 for (p = fname; isalpha(*p); ++p)
5828 ;
5829 return path_is_url(p);
5830}
5831
5832/*
5833 * Return TRUE if "name" is a full (absolute) path name or URL.
5834 */
5835 int
5836vim_isAbsName(name)
5837 char_u *name;
5838{
5839 return (path_with_url(name) != 0 || mch_isFullName(name));
5840}
5841
5842/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005843 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 *
5845 * return FAIL for failure, OK otherwise
5846 */
5847 int
5848vim_FullName(fname, buf, len, force)
5849 char_u *fname, *buf;
5850 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005851 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852{
5853 int retval = OK;
5854 int url;
5855
5856 *buf = NUL;
5857 if (fname == NULL)
5858 return FAIL;
5859
5860 url = path_with_url(fname);
5861 if (!url)
5862 retval = mch_FullName(fname, buf, len, force);
5863 if (url || retval == FAIL)
5864 {
5865 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005866 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 }
5868#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5869 slash_adjust(buf);
5870#endif
5871 return retval;
5872}
5873
5874/*
5875 * Return the minimal number of rows that is needed on the screen to display
5876 * the current number of windows.
5877 */
5878 int
5879min_rows()
5880{
5881 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005882#ifdef FEAT_WINDOWS
5883 tabpage_T *tp;
5884 int n;
5885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886
5887 if (firstwin == NULL) /* not initialized yet */
5888 return MIN_LINES;
5889
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005891 total = 0;
5892 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5893 {
5894 n = frame_minheight(tp->tp_topframe, NULL);
5895 if (total < n)
5896 total = n;
5897 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005898 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005900 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005902 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 return total;
5904}
5905
5906/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005907 * Return TRUE if there is only one window (in the current tab page), not
5908 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 */
5910 int
5911only_one_window()
5912{
5913#ifdef FEAT_WINDOWS
5914 int count = 0;
5915 win_T *wp;
5916
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005917 /* If there is another tab page there always is another window. */
5918 if (first_tabpage->tp_next != NULL)
5919 return FALSE;
5920
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005922 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923# ifdef FEAT_QUICKFIX
5924 || wp->w_p_pvw
5925# endif
5926 ) || wp == curwin)
5927 ++count;
5928 return (count <= 1);
5929#else
5930 return TRUE;
5931#endif
5932}
5933
5934#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5935/*
5936 * Correct the cursor line number in other windows. Used after changing the
5937 * current buffer, and before applying autocommands.
5938 * When "do_curwin" is TRUE, also check current window.
5939 */
5940 void
5941check_lnums(do_curwin)
5942 int do_curwin;
5943{
5944 win_T *wp;
5945
5946#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005947 tabpage_T *tp;
5948
5949 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5951#else
5952 wp = curwin;
5953 if (do_curwin)
5954#endif
5955 {
5956 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5957 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5958 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5959 wp->w_topline = curbuf->b_ml.ml_line_count;
5960 }
5961}
5962#endif
5963
5964#if defined(FEAT_WINDOWS) || defined(PROTO)
5965
5966/*
5967 * A snapshot of the window sizes, to restore them after closing the help
5968 * window.
5969 * Only these fields are used:
5970 * fr_layout
5971 * fr_width
5972 * fr_height
5973 * fr_next
5974 * fr_child
5975 * fr_win (only valid for the old curwin, NULL otherwise)
5976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977
5978/*
5979 * Create a snapshot of the current frame sizes.
5980 */
5981 static void
5982make_snapshot()
5983{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005984 clear_snapshot(curtab);
5985 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986}
5987
5988 static void
5989make_snapshot_rec(fr, frp)
5990 frame_T *fr;
5991 frame_T **frp;
5992{
5993 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5994 if (*frp == NULL)
5995 return;
5996 (*frp)->fr_layout = fr->fr_layout;
5997# ifdef FEAT_VERTSPLIT
5998 (*frp)->fr_width = fr->fr_width;
5999# endif
6000 (*frp)->fr_height = fr->fr_height;
6001 if (fr->fr_next != NULL)
6002 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6003 if (fr->fr_child != NULL)
6004 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6005 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6006 (*frp)->fr_win = curwin;
6007}
6008
6009/*
6010 * Remove any existing snapshot.
6011 */
6012 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006013clear_snapshot(tp)
6014 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006016 clear_snapshot_rec(tp->tp_snapshot);
6017 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018}
6019
6020 static void
6021clear_snapshot_rec(fr)
6022 frame_T *fr;
6023{
6024 if (fr != NULL)
6025 {
6026 clear_snapshot_rec(fr->fr_next);
6027 clear_snapshot_rec(fr->fr_child);
6028 vim_free(fr);
6029 }
6030}
6031
6032/*
6033 * Restore a previously created snapshot, if there is any.
6034 * This is only done if the screen size didn't change and the window layout is
6035 * still the same.
6036 */
6037 static void
6038restore_snapshot(close_curwin)
6039 int close_curwin; /* closing current window */
6040{
6041 win_T *wp;
6042
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006043 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006045 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006047 && curtab->tp_snapshot->fr_height == topframe->fr_height
6048 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006050 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 win_comp_pos();
6052 if (wp != NULL && close_curwin)
6053 win_goto(wp);
6054 redraw_all_later(CLEAR);
6055 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006056 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057}
6058
6059/*
6060 * Check if frames "sn" and "fr" have the same layout, same following frames
6061 * and same children.
6062 */
6063 static int
6064check_snapshot_rec(sn, fr)
6065 frame_T *sn;
6066 frame_T *fr;
6067{
6068 if (sn->fr_layout != fr->fr_layout
6069 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6070 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6071 || (sn->fr_next != NULL
6072 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6073 || (sn->fr_child != NULL
6074 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6075 return FAIL;
6076 return OK;
6077}
6078
6079/*
6080 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6081 * following frames and children.
6082 * Returns a pointer to the old current window, or NULL.
6083 */
6084 static win_T *
6085restore_snapshot_rec(sn, fr)
6086 frame_T *sn;
6087 frame_T *fr;
6088{
6089 win_T *wp = NULL;
6090 win_T *wp2;
6091
6092 fr->fr_height = sn->fr_height;
6093# ifdef FEAT_VERTSPLIT
6094 fr->fr_width = sn->fr_width;
6095# endif
6096 if (fr->fr_layout == FR_LEAF)
6097 {
6098 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6099# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006100 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101# endif
6102 wp = sn->fr_win;
6103 }
6104 if (sn->fr_next != NULL)
6105 {
6106 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6107 if (wp2 != NULL)
6108 wp = wp2;
6109 }
6110 if (sn->fr_child != NULL)
6111 {
6112 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6113 if (wp2 != NULL)
6114 wp = wp2;
6115 }
6116 return wp;
6117}
6118
6119#endif
6120
6121#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6122/*
6123 * Return TRUE if there is any vertically split window.
6124 */
6125 int
6126win_hasvertsplit()
6127{
6128 frame_T *fr;
6129
6130 if (topframe->fr_layout == FR_ROW)
6131 return TRUE;
6132
6133 if (topframe->fr_layout == FR_COL)
6134 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6135 if (fr->fr_layout == FR_ROW)
6136 return TRUE;
6137
6138 return FALSE;
6139}
6140#endif