blob: 28b9660b84faf974d8220345efb279f8d4fb1f69 [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;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000343
344 /* First create a new tab with the window, then go back to
345 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000346 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000347 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
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001544 if (win1->w_prev->w_vsep_width == 1)
1545 {
1546 /* Remove the vertical separator from the last-but-one window,
1547 * add it to the last window. Adjust the frame widths. */
1548 win1->w_prev->w_vsep_width = 0;
1549 win1->w_prev->w_frame->fr_width -= 1;
1550 win1->w_vsep_width = 1;
1551 win1->w_frame->fr_width += 1;
1552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553#endif
1554 }
1555 else if (win2 == lastwin)
1556 {
1557 height = win1->w_status_height;
1558 win1->w_status_height = win2->w_status_height;
1559 win2->w_status_height = height;
1560#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001561 if (win1->w_vsep_width == 1)
1562 {
1563 /* Remove the vertical separator from win1, add it to the last
1564 * window, win2. Adjust the frame widths. */
1565 win2->w_vsep_width = 1;
1566 win2->w_frame->fr_width += 1;
1567 win1->w_vsep_width = 0;
1568 win1->w_frame->fr_width -= 1;
1569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570#endif
1571 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001572 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 frame_remove(win1->w_frame);
1574 win_append(win2, win1);
1575 frame_append(win2->w_frame, win1->w_frame);
1576
1577 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1578 redraw_later(NOT_VALID);
1579 }
1580 win_enter(win1, FALSE);
1581}
1582
1583/*
1584 * Make all windows the same height.
1585 * 'next_curwin' will soon be the current window, make sure it has enough
1586 * rows.
1587 */
1588 void
1589win_equal(next_curwin, current, dir)
1590 win_T *next_curwin; /* pointer to current window to be or NULL */
1591 int current; /* do only frame with current window */
1592 int dir; /* 'v' for vertically, 'h' for horizontally,
1593 'b' for both, 0 for using p_ead */
1594{
1595 if (dir == 0)
1596#ifdef FEAT_VERTSPLIT
1597 dir = *p_ead;
1598#else
1599 dir = 'b';
1600#endif
1601 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001602 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001603 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604}
1605
1606/*
1607 * Set a frame to a new position and height, spreading the available room
1608 * equally over contained frames.
1609 * The window "next_curwin" (if not NULL) should at least get the size from
1610 * 'winheight' and 'winwidth' if possible.
1611 */
1612 static void
1613win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1614 win_T *next_curwin; /* pointer to current window to be or NULL */
1615 int current; /* do only frame with current window */
1616 frame_T *topfr; /* frame to set size off */
1617 int dir; /* 'v', 'h' or 'b', see win_equal() */
1618 int col; /* horizontal position for frame */
1619 int row; /* vertical position for frame */
1620 int width; /* new width of frame */
1621 int height; /* new height of frame */
1622{
1623 int n, m;
1624 int extra_sep = 0;
1625 int wincount, totwincount = 0;
1626 frame_T *fr;
1627 int next_curwin_size = 0;
1628 int room = 0;
1629 int new_size;
1630 int has_next_curwin = 0;
1631 int hnc;
1632
1633 if (topfr->fr_layout == FR_LEAF)
1634 {
1635 /* Set the width/height of this frame.
1636 * Redraw when size or position changes */
1637 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1638#ifdef FEAT_VERTSPLIT
1639 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1640#endif
1641 )
1642 {
1643 topfr->fr_win->w_winrow = row;
1644 frame_new_height(topfr, height, FALSE, FALSE);
1645#ifdef FEAT_VERTSPLIT
1646 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001647 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648#endif
1649 redraw_all_later(CLEAR);
1650 }
1651 }
1652#ifdef FEAT_VERTSPLIT
1653 else if (topfr->fr_layout == FR_ROW)
1654 {
1655 topfr->fr_width = width;
1656 topfr->fr_height = height;
1657
1658 if (dir != 'v') /* equalize frame widths */
1659 {
1660 /* Compute the maximum number of windows horizontally in this
1661 * frame. */
1662 n = frame_minwidth(topfr, NOWIN);
1663 /* add one for the rightmost window, it doesn't have a separator */
1664 if (col + width == Columns)
1665 extra_sep = 1;
1666 else
1667 extra_sep = 0;
1668 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001669 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001671 /*
1672 * Compute width for "next_curwin" window and room available for
1673 * other windows.
1674 * "m" is the minimal width when counting p_wiw for "next_curwin".
1675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 m = frame_minwidth(topfr, next_curwin);
1677 room = width - m;
1678 if (room < 0)
1679 {
1680 next_curwin_size = p_wiw + room;
1681 room = 0;
1682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 else
1684 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001685 next_curwin_size = -1;
1686 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1687 {
1688 /* If 'winfixwidth' set keep the window width if
1689 * possible.
1690 * Watch out for this window being the next_curwin. */
1691 if (frame_fixed_width(fr))
1692 {
1693 n = frame_minwidth(fr, NOWIN);
1694 new_size = fr->fr_width;
1695 if (frame_has_win(fr, next_curwin))
1696 {
1697 room += p_wiw - p_wmw;
1698 next_curwin_size = 0;
1699 if (new_size < p_wiw)
1700 new_size = p_wiw;
1701 }
1702 else
1703 /* These windows don't use up room. */
1704 totwincount -= (n + (fr->fr_next == NULL
1705 ? extra_sep : 0)) / (p_wmw + 1);
1706 room -= new_size - n;
1707 if (room < 0)
1708 {
1709 new_size += room;
1710 room = 0;
1711 }
1712 fr->fr_newwidth = new_size;
1713 }
1714 }
1715 if (next_curwin_size == -1)
1716 {
1717 if (!has_next_curwin)
1718 next_curwin_size = 0;
1719 else if (totwincount > 1
1720 && (room + (totwincount - 2))
1721 / (totwincount - 1) > p_wiw)
1722 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001723 /* Can make all windows wider than 'winwidth', spread
1724 * the room equally. */
1725 next_curwin_size = (room + p_wiw
1726 + (totwincount - 1) * p_wmw
1727 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001728 room -= next_curwin_size - p_wiw;
1729 }
1730 else
1731 next_curwin_size = p_wiw;
1732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001734
1735 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 --totwincount; /* don't count curwin */
1737 }
1738
1739 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1740 {
1741 n = m = 0;
1742 wincount = 1;
1743 if (fr->fr_next == NULL)
1744 /* last frame gets all that remains (avoid roundoff error) */
1745 new_size = width;
1746 else if (dir == 'v')
1747 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001748 else if (frame_fixed_width(fr))
1749 {
1750 new_size = fr->fr_newwidth;
1751 wincount = 0; /* doesn't count as a sizeable window */
1752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 else
1754 {
1755 /* Compute the maximum number of windows horiz. in "fr". */
1756 n = frame_minwidth(fr, NOWIN);
1757 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1758 / (p_wmw + 1);
1759 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001760 if (has_next_curwin)
1761 hnc = frame_has_win(fr, next_curwin);
1762 else
1763 hnc = FALSE;
1764 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001766 if (totwincount == 0)
1767 new_size = room;
1768 else
1769 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001771 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 {
1773 next_curwin_size -= p_wiw - (m - n);
1774 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001775 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001777 else
1778 room -= new_size;
1779 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 }
1781
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001782 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 * window, unless equalizing all frames. */
1784 if (!current || dir != 'v' || topfr->fr_parent != NULL
1785 || (new_size != fr->fr_width)
1786 || frame_has_win(fr, next_curwin))
1787 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001788 new_size, height);
1789 col += new_size;
1790 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 totwincount -= wincount;
1792 }
1793 }
1794#endif
1795 else /* topfr->fr_layout == FR_COL */
1796 {
1797#ifdef FEAT_VERTSPLIT
1798 topfr->fr_width = width;
1799#endif
1800 topfr->fr_height = height;
1801
1802 if (dir != 'h') /* equalize frame heights */
1803 {
1804 /* Compute maximum number of windows vertically in this frame. */
1805 n = frame_minheight(topfr, NOWIN);
1806 /* add one for the bottom window if it doesn't have a statusline */
1807 if (row + height == cmdline_row && p_ls == 0)
1808 extra_sep = 1;
1809 else
1810 extra_sep = 0;
1811 totwincount = (n + extra_sep) / (p_wmh + 1);
1812 has_next_curwin = frame_has_win(topfr, next_curwin);
1813
1814 /*
1815 * Compute height for "next_curwin" window and room available for
1816 * other windows.
1817 * "m" is the minimal height when counting p_wh for "next_curwin".
1818 */
1819 m = frame_minheight(topfr, next_curwin);
1820 room = height - m;
1821 if (room < 0)
1822 {
1823 /* The room is less then 'winheight', use all space for the
1824 * current window. */
1825 next_curwin_size = p_wh + room;
1826 room = 0;
1827 }
1828 else
1829 {
1830 next_curwin_size = -1;
1831 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1832 {
1833 /* If 'winfixheight' set keep the window height if
1834 * possible.
1835 * Watch out for this window being the next_curwin. */
1836 if (frame_fixed_height(fr))
1837 {
1838 n = frame_minheight(fr, NOWIN);
1839 new_size = fr->fr_height;
1840 if (frame_has_win(fr, next_curwin))
1841 {
1842 room += p_wh - p_wmh;
1843 next_curwin_size = 0;
1844 if (new_size < p_wh)
1845 new_size = p_wh;
1846 }
1847 else
1848 /* These windows don't use up room. */
1849 totwincount -= (n + (fr->fr_next == NULL
1850 ? extra_sep : 0)) / (p_wmh + 1);
1851 room -= new_size - n;
1852 if (room < 0)
1853 {
1854 new_size += room;
1855 room = 0;
1856 }
1857 fr->fr_newheight = new_size;
1858 }
1859 }
1860 if (next_curwin_size == -1)
1861 {
1862 if (!has_next_curwin)
1863 next_curwin_size = 0;
1864 else if (totwincount > 1
1865 && (room + (totwincount - 2))
1866 / (totwincount - 1) > p_wh)
1867 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001868 /* can make all windows higher than 'winheight',
1869 * spread the room equally. */
1870 next_curwin_size = (room + p_wh
1871 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 + (totwincount - 1)) / totwincount;
1873 room -= next_curwin_size - p_wh;
1874 }
1875 else
1876 next_curwin_size = p_wh;
1877 }
1878 }
1879
1880 if (has_next_curwin)
1881 --totwincount; /* don't count curwin */
1882 }
1883
1884 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1885 {
1886 n = m = 0;
1887 wincount = 1;
1888 if (fr->fr_next == NULL)
1889 /* last frame gets all that remains (avoid roundoff error) */
1890 new_size = height;
1891 else if (dir == 'h')
1892 new_size = fr->fr_height;
1893 else if (frame_fixed_height(fr))
1894 {
1895 new_size = fr->fr_newheight;
1896 wincount = 0; /* doesn't count as a sizeable window */
1897 }
1898 else
1899 {
1900 /* Compute the maximum number of windows vert. in "fr". */
1901 n = frame_minheight(fr, NOWIN);
1902 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1903 / (p_wmh + 1);
1904 m = frame_minheight(fr, next_curwin);
1905 if (has_next_curwin)
1906 hnc = frame_has_win(fr, next_curwin);
1907 else
1908 hnc = FALSE;
1909 if (hnc) /* don't count next_curwin */
1910 --wincount;
1911 if (totwincount == 0)
1912 new_size = room;
1913 else
1914 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1915 / totwincount;
1916 if (hnc) /* add next_curwin size */
1917 {
1918 next_curwin_size -= p_wh - (m - n);
1919 new_size += next_curwin_size;
1920 room -= new_size - next_curwin_size;
1921 }
1922 else
1923 room -= new_size;
1924 new_size += n;
1925 }
1926 /* Skip frame that is full width when splitting or closing a
1927 * window, unless equalizing all frames. */
1928 if (!current || dir != 'h' || topfr->fr_parent != NULL
1929 || (new_size != fr->fr_height)
1930 || frame_has_win(fr, next_curwin))
1931 win_equal_rec(next_curwin, current, fr, dir, col, row,
1932 width, new_size);
1933 row += new_size;
1934 height -= new_size;
1935 totwincount -= wincount;
1936 }
1937 }
1938}
1939
1940/*
1941 * close all windows for buffer 'buf'
1942 */
1943 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00001944close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001946 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947{
Bram Moolenaarf740b292006-02-16 22:11:02 +00001948 win_T *wp;
1949 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001950 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951
1952 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001953
1954 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001956 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001958 win_close(wp, FALSE);
1959
1960 /* Start all over, autocommands may change the window layout. */
1961 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 }
1963 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00001964 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001966
1967 /* Also check windows in other tab pages. */
1968 for (tp = first_tabpage; tp != NULL; tp = nexttp)
1969 {
1970 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001971 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001972 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
1973 if (wp->w_buffer == buf)
1974 {
1975 win_close_othertab(wp, FALSE, tp);
1976
1977 /* Start all over, the tab page may be closed and
1978 * autocommands may change the window layout. */
1979 nexttp = first_tabpage;
1980 break;
1981 }
1982 }
1983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001985
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001986 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00001987 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988}
1989
1990/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001991 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001992 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001993 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001994 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001995last_window()
1996{
1997 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
1998}
1999
2000/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002001 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 * If "free_buf" is TRUE related buffer may be unloaded.
2003 *
2004 * called by :quit, :close, :xit, :wq and findtag()
2005 */
2006 void
2007win_close(win, free_buf)
2008 win_T *win;
2009 int free_buf;
2010{
2011 win_T *wp;
2012#ifdef FEAT_AUTOCMD
2013 int other_buffer = FALSE;
2014#endif
2015 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 int dir;
2017 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002018 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002020 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
2022 EMSG(_("E444: Cannot close last window"));
2023 return;
2024 }
2025
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002026 /*
2027 * When closing the last window in a tab page first go to another tab
2028 * page and then close the window and the tab page. This avoids that
2029 * curwin and curtab are not invalid while we are freeing memory, they may
2030 * be used in GUI events.
2031 */
2032 if (firstwin == lastwin)
2033 {
2034 goto_tabpage_tp(alt_tabpage());
2035 redraw_tabline = TRUE;
2036
2037 /* Safety check: Autocommands may have closed the window when jumping
2038 * to the other tab page. */
2039 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2040 {
2041 int h = tabline_height();
2042
2043 win_close_othertab(win, free_buf, prev_curtab);
2044 if (h != tabline_height())
2045 shell_new_rows();
2046 }
2047 return;
2048 }
2049
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 /* When closing the help window, try restoring a snapshot after closing
2051 * the window. Otherwise clear the snapshot, it's now invalid. */
2052 if (win->w_buffer->b_help)
2053 help_window = TRUE;
2054 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002055 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
2057#ifdef FEAT_AUTOCMD
2058 if (win == curwin)
2059 {
2060 /*
2061 * Guess which window is going to be the new current window.
2062 * This may change because of the autocommands (sigh).
2063 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002064 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
2066 /*
2067 * Be careful: If autocommands delete the window, return now.
2068 */
2069 if (wp->w_buffer != curbuf)
2070 {
2071 other_buffer = TRUE;
2072 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002073 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 return;
2075 }
2076 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002077 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 return;
2079# ifdef FEAT_EVAL
2080 /* autocmds may abort script processing */
2081 if (aborting())
2082 return;
2083# endif
2084 }
2085#endif
2086
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002087#ifdef FEAT_GUI
2088 /* Avoid trouble with scrollbars that are going to be deleted in
2089 * win_free(). */
2090 if (gui.in_use)
2091 out_flush();
2092#endif
2093
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 /*
2095 * Close the link to the buffer.
2096 */
2097 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002098
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002100 * other window or moved to another tab page. */
2101 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 return;
2103
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002104 /* Free the memory used for the window. */
2105 wp = win_free_mem(win, &dir, NULL);
2106
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 /* Make sure curwin isn't invalid. It can cause severe trouble when
2108 * printing an error message. For win_equal() curbuf needs to be valid
2109 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002110 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 {
2112 curwin = wp;
2113#ifdef FEAT_QUICKFIX
2114 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2115 {
2116 /*
2117 * The cursor goes to the preview or the quickfix window, try
2118 * finding another window to go to.
2119 */
2120 for (;;)
2121 {
2122 if (wp->w_next == NULL)
2123 wp = firstwin;
2124 else
2125 wp = wp->w_next;
2126 if (wp == curwin)
2127 break;
2128 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2129 {
2130 curwin = wp;
2131 break;
2132 }
2133 }
2134 }
2135#endif
2136 curbuf = curwin->w_buffer;
2137 close_curwin = TRUE;
2138 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002139 if (p_ea
2140#ifdef FEAT_VERTSPLIT
2141 && (*p_ead == 'b' || *p_ead == dir)
2142#endif
2143 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 win_equal(curwin, TRUE,
2145#ifdef FEAT_VERTSPLIT
2146 dir
2147#else
2148 0
2149#endif
2150 );
2151 else
2152 win_comp_pos();
2153 if (close_curwin)
2154 {
2155 win_enter_ext(wp, FALSE, TRUE);
2156#ifdef FEAT_AUTOCMD
2157 if (other_buffer)
2158 /* careful: after this wp and win may be invalid! */
2159 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2160#endif
2161 }
2162
2163 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002164 * If last window has a status line now and we don't want one,
2165 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 */
2167 last_status(FALSE);
2168
2169 /* After closing the help window, try restoring the window layout from
2170 * before it was opened. */
2171 if (help_window)
2172 restore_snapshot(close_curwin);
2173
2174#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2175 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2176 if (gui.in_use && !win_hasvertsplit())
2177 gui_init_which_components(NULL);
2178#endif
2179
2180 redraw_all_later(NOT_VALID);
2181}
2182
2183/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002184 * Close window "win" in tab page "tp", which is not the current tab page.
2185 * This may be the last window ih that tab page and result in closing the tab,
2186 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002187 * Caller must check if buffer is hidden and whether the tabline needs to be
2188 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002189 */
2190 void
2191win_close_othertab(win, free_buf, tp)
2192 win_T *win;
2193 int free_buf;
2194 tabpage_T *tp;
2195{
2196 win_T *wp;
2197 int dir;
2198 tabpage_T *ptp = NULL;
2199
2200 /* Close the link to the buffer. */
2201 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2202
2203 /* Careful: Autocommands may have closed the tab page or made it the
2204 * current tab page. */
2205 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2206 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002207 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002208 return;
2209
2210 /* Autocommands may have closed the window already. */
2211 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2212 ;
2213 if (wp == NULL)
2214 return;
2215
2216 /* Free the memory used for the window. */
2217 wp = win_free_mem(win, &dir, tp);
2218
2219 /* When closing the last window in a tab page remove the tab page. */
2220 if (wp == NULL)
2221 {
2222 if (tp == first_tabpage)
2223 first_tabpage = tp->tp_next;
2224 else
2225 {
2226 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2227 ptp = ptp->tp_next)
2228 ;
2229 if (ptp == NULL)
2230 {
2231 EMSG2(_(e_intern2), "win_close_othertab()");
2232 return;
2233 }
2234 ptp->tp_next = tp->tp_next;
2235 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002236 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002237 }
2238}
2239
2240/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002241 * Free the memory used for a window.
2242 * Returns a pointer to the window that got the freed up space.
2243 */
2244 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002245win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002246 win_T *win;
2247 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002248 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002249{
2250 frame_T *frp;
2251 win_T *wp;
2252
Bram Moolenaarea408852005-06-25 22:49:46 +00002253#ifdef FEAT_FOLDING
2254 clearFolding(win);
2255#endif
2256
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002257 /* reduce the reference count to the argument list. */
2258 alist_unlink(win->w_alist);
2259
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002260 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002261 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002262 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002263 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002264 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002265
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002266 /* When deleting the current window of another tab page select a new
2267 * current window. */
2268 if (tp != NULL && win == tp->tp_curwin)
2269 tp->tp_curwin = wp;
2270
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002271 return wp;
2272}
2273
2274#if defined(EXITFREE) || defined(PROTO)
2275 void
2276win_free_all()
2277{
2278 int dummy;
2279
Bram Moolenaarf740b292006-02-16 22:11:02 +00002280# ifdef FEAT_WINDOWS
2281 while (first_tabpage->tp_next != NULL)
2282 tabpage_close(TRUE);
2283# endif
2284
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002285 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002286 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002287}
2288#endif
2289
2290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 * Remove a window and its frame from the tree of frames.
2292 * Returns a pointer to the window that got the freed up space.
2293 */
2294/*ARGSUSED*/
2295 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002296winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 win_T *win;
2298 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002299 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300{
2301 frame_T *frp, *frp2, *frp3;
2302 frame_T *frp_close = win->w_frame;
2303 win_T *wp;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002304 int old_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305
2306 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002307 * If there is only one window there is nothing to remove.
2308 */
2309 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2310 return NULL;
2311
2312 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 * Remove the window from its frame.
2314 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002315 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 wp = frame2win(frp2);
2317
2318 /* Remove this frame from the list of frames. */
2319 frame_remove(frp_close);
2320
2321#ifdef FEAT_VERTSPLIT
2322 if (frp_close->fr_parent->fr_layout == FR_COL)
2323 {
2324#endif
2325 /* When 'winfixheight' is set, remember its old size and restore
2326 * it later (it's a simplistic solution...). Don't do this if the
2327 * window will occupy the full height of the screen. */
2328 if (frp2->fr_win != NULL
2329 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2330 && frp2->fr_win->w_p_wfh)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002331 old_size = frp2->fr_win->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2333 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002334 if (old_size != 0)
2335 win_setheight_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336#ifdef FEAT_VERTSPLIT
2337 *dirp = 'v';
2338 }
2339 else
2340 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002341 /* When 'winfixwidth' is set, remember its old size and restore
2342 * it later (it's a simplistic solution...). Don't do this if the
2343 * window will occupy the full width of the screen. */
2344 if (frp2->fr_win != NULL
2345 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2346 && frp2->fr_win->w_p_wfw)
2347 old_size = frp2->fr_win->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002349 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2350 if (old_size != 0)
2351 win_setwidth_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 *dirp = 'h';
2353 }
2354#endif
2355
2356 /* If rows/columns go to a window below/right its positions need to be
2357 * updated. Can only be done after the sizes have been updated. */
2358 if (frp2 == frp_close->fr_next)
2359 {
2360 int row = win->w_winrow;
2361 int col = W_WINCOL(win);
2362
2363 frame_comp_pos(frp2, &row, &col);
2364 }
2365
2366 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2367 {
2368 /* There is no other frame in this list, move its info to the parent
2369 * and remove it. */
2370 frp2->fr_parent->fr_layout = frp2->fr_layout;
2371 frp2->fr_parent->fr_child = frp2->fr_child;
2372 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2373 frp->fr_parent = frp2->fr_parent;
2374 frp2->fr_parent->fr_win = frp2->fr_win;
2375 if (frp2->fr_win != NULL)
2376 frp2->fr_win->w_frame = frp2->fr_parent;
2377 frp = frp2->fr_parent;
2378 vim_free(frp2);
2379
2380 frp2 = frp->fr_parent;
2381 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2382 {
2383 /* The frame above the parent has the same layout, have to merge
2384 * the frames into this list. */
2385 if (frp2->fr_child == frp)
2386 frp2->fr_child = frp->fr_child;
2387 frp->fr_child->fr_prev = frp->fr_prev;
2388 if (frp->fr_prev != NULL)
2389 frp->fr_prev->fr_next = frp->fr_child;
2390 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2391 {
2392 frp3->fr_parent = frp2;
2393 if (frp3->fr_next == NULL)
2394 {
2395 frp3->fr_next = frp->fr_next;
2396 if (frp->fr_next != NULL)
2397 frp->fr_next->fr_prev = frp3;
2398 break;
2399 }
2400 }
2401 vim_free(frp);
2402 }
2403 }
2404
2405 return wp;
2406}
2407
2408/*
2409 * Find out which frame is going to get the freed up space when "win" is
2410 * closed.
2411 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2412 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2413 * This makes opening a window and closing it immediately keep the same window
2414 * layout.
2415 */
2416 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002417win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002419 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
2421 frame_T *frp;
2422 int b;
2423
Bram Moolenaarf740b292006-02-16 22:11:02 +00002424 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002425 /* Last window in this tab page, will go to next tab page. */
2426 return alt_tabpage()->tp_curwin->w_frame;
2427
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 frp = win->w_frame;
2429#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002430 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 b = p_spr;
2432 else
2433#endif
2434 b = p_sb;
2435 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2436 return frp->fr_next;
2437 return frp->fr_prev;
2438}
2439
2440/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002441 * Return the tabpage that will be used if the current one is closed.
2442 */
2443 static tabpage_T *
2444alt_tabpage()
2445{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002446 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002447
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002448 /* Use the next tab page if possible. */
2449 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002450 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002451
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002452 /* Find the last but one tab page. */
2453 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2454 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002455 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002456}
2457
2458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 * Find the left-upper window in frame "frp".
2460 */
2461 static win_T *
2462frame2win(frp)
2463 frame_T *frp;
2464{
2465 while (frp->fr_win == NULL)
2466 frp = frp->fr_child;
2467 return frp->fr_win;
2468}
2469
2470/*
2471 * Return TRUE if frame "frp" contains window "wp".
2472 */
2473 static int
2474frame_has_win(frp, wp)
2475 frame_T *frp;
2476 win_T *wp;
2477{
2478 frame_T *p;
2479
2480 if (frp->fr_layout == FR_LEAF)
2481 return frp->fr_win == wp;
2482
2483 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2484 if (frame_has_win(p, wp))
2485 return TRUE;
2486 return FALSE;
2487}
2488
2489/*
2490 * Set a new height for a frame. Recursively sets the height for contained
2491 * frames and windows. Caller must take care of positions.
2492 */
2493 static void
2494frame_new_height(topfrp, height, topfirst, wfh)
2495 frame_T *topfrp;
2496 int height;
2497 int topfirst; /* resize topmost contained frame first */
2498 int wfh; /* obey 'winfixheight' when there is a choice;
2499 may cause the height not to be set */
2500{
2501 frame_T *frp;
2502 int extra_lines;
2503 int h;
2504
2505 if (topfrp->fr_win != NULL)
2506 {
2507 /* Simple case: just one window. */
2508 win_new_height(topfrp->fr_win,
2509 height - topfrp->fr_win->w_status_height);
2510 }
2511#ifdef FEAT_VERTSPLIT
2512 else if (topfrp->fr_layout == FR_ROW)
2513 {
2514 do
2515 {
2516 /* All frames in this row get the same new height. */
2517 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2518 {
2519 frame_new_height(frp, height, topfirst, wfh);
2520 if (frp->fr_height > height)
2521 {
2522 /* Could not fit the windows, make the whole row higher. */
2523 height = frp->fr_height;
2524 break;
2525 }
2526 }
2527 }
2528 while (frp != NULL);
2529 }
2530#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002531 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
2533 /* Complicated case: Resize a column of frames. Resize the bottom
2534 * frame first, frames above that when needed. */
2535
2536 frp = topfrp->fr_child;
2537 if (wfh)
2538 /* Advance past frames with one window with 'wfh' set. */
2539 while (frame_fixed_height(frp))
2540 {
2541 frp = frp->fr_next;
2542 if (frp == NULL)
2543 return; /* no frame without 'wfh', give up */
2544 }
2545 if (!topfirst)
2546 {
2547 /* Find the bottom frame of this column */
2548 while (frp->fr_next != NULL)
2549 frp = frp->fr_next;
2550 if (wfh)
2551 /* Advance back for frames with one window with 'wfh' set. */
2552 while (frame_fixed_height(frp))
2553 frp = frp->fr_prev;
2554 }
2555
2556 extra_lines = height - topfrp->fr_height;
2557 if (extra_lines < 0)
2558 {
2559 /* reduce height of contained frames, bottom or top frame first */
2560 while (frp != NULL)
2561 {
2562 h = frame_minheight(frp, NULL);
2563 if (frp->fr_height + extra_lines < h)
2564 {
2565 extra_lines += frp->fr_height - h;
2566 frame_new_height(frp, h, topfirst, wfh);
2567 }
2568 else
2569 {
2570 frame_new_height(frp, frp->fr_height + extra_lines,
2571 topfirst, wfh);
2572 break;
2573 }
2574 if (topfirst)
2575 {
2576 do
2577 frp = frp->fr_next;
2578 while (wfh && frp != NULL && frame_fixed_height(frp));
2579 }
2580 else
2581 {
2582 do
2583 frp = frp->fr_prev;
2584 while (wfh && frp != NULL && frame_fixed_height(frp));
2585 }
2586 /* Increase "height" if we could not reduce enough frames. */
2587 if (frp == NULL)
2588 height -= extra_lines;
2589 }
2590 }
2591 else if (extra_lines > 0)
2592 {
2593 /* increase height of bottom or top frame */
2594 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2595 }
2596 }
2597 topfrp->fr_height = height;
2598}
2599
2600/*
2601 * Return TRUE if height of frame "frp" should not be changed because of
2602 * the 'winfixheight' option.
2603 */
2604 static int
2605frame_fixed_height(frp)
2606 frame_T *frp;
2607{
2608 /* frame with one window: fixed height if 'winfixheight' set. */
2609 if (frp->fr_win != NULL)
2610 return frp->fr_win->w_p_wfh;
2611
2612 if (frp->fr_layout == FR_ROW)
2613 {
2614 /* The frame is fixed height if one of the frames in the row is fixed
2615 * height. */
2616 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2617 if (frame_fixed_height(frp))
2618 return TRUE;
2619 return FALSE;
2620 }
2621
2622 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2623 * frames in the row are fixed height. */
2624 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2625 if (!frame_fixed_height(frp))
2626 return FALSE;
2627 return TRUE;
2628}
2629
2630#ifdef FEAT_VERTSPLIT
2631/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002632 * Return TRUE if width of frame "frp" should not be changed because of
2633 * the 'winfixwidth' option.
2634 */
2635 static int
2636frame_fixed_width(frp)
2637 frame_T *frp;
2638{
2639 /* frame with one window: fixed width if 'winfixwidth' set. */
2640 if (frp->fr_win != NULL)
2641 return frp->fr_win->w_p_wfw;
2642
2643 if (frp->fr_layout == FR_COL)
2644 {
2645 /* The frame is fixed width if one of the frames in the row is fixed
2646 * width. */
2647 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2648 if (frame_fixed_width(frp))
2649 return TRUE;
2650 return FALSE;
2651 }
2652
2653 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2654 * frames in the row are fixed width. */
2655 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2656 if (!frame_fixed_width(frp))
2657 return FALSE;
2658 return TRUE;
2659}
2660
2661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 * Add a status line to windows at the bottom of "frp".
2663 * Note: Does not check if there is room!
2664 */
2665 static void
2666frame_add_statusline(frp)
2667 frame_T *frp;
2668{
2669 win_T *wp;
2670
2671 if (frp->fr_layout == FR_LEAF)
2672 {
2673 wp = frp->fr_win;
2674 if (wp->w_status_height == 0)
2675 {
2676 if (wp->w_height > 0) /* don't make it negative */
2677 --wp->w_height;
2678 wp->w_status_height = STATUS_HEIGHT;
2679 }
2680 }
2681 else if (frp->fr_layout == FR_ROW)
2682 {
2683 /* Handle all the frames in the row. */
2684 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2685 frame_add_statusline(frp);
2686 }
2687 else /* frp->fr_layout == FR_COL */
2688 {
2689 /* Only need to handle the last frame in the column. */
2690 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2691 ;
2692 frame_add_statusline(frp);
2693 }
2694}
2695
2696/*
2697 * Set width of a frame. Handles recursively going through contained frames.
2698 * May remove separator line for windows at the right side (for win_close()).
2699 */
2700 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002701frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 frame_T *topfrp;
2703 int width;
2704 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002705 int wfw; /* obey 'winfixwidth' when there is a choice;
2706 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707{
2708 frame_T *frp;
2709 int extra_cols;
2710 int w;
2711 win_T *wp;
2712
2713 if (topfrp->fr_layout == FR_LEAF)
2714 {
2715 /* Simple case: just one window. */
2716 wp = topfrp->fr_win;
2717 /* Find out if there are any windows right of this one. */
2718 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2719 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2720 break;
2721 if (frp->fr_parent == NULL)
2722 wp->w_vsep_width = 0;
2723 win_new_width(wp, width - wp->w_vsep_width);
2724 }
2725 else if (topfrp->fr_layout == FR_COL)
2726 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002727 do
2728 {
2729 /* All frames in this column get the same new width. */
2730 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2731 {
2732 frame_new_width(frp, width, leftfirst, wfw);
2733 if (frp->fr_width > width)
2734 {
2735 /* Could not fit the windows, make whole column wider. */
2736 width = frp->fr_width;
2737 break;
2738 }
2739 }
2740 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 }
2742 else /* fr_layout == FR_ROW */
2743 {
2744 /* Complicated case: Resize a row of frames. Resize the rightmost
2745 * frame first, frames left of it when needed. */
2746
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002748 if (wfw)
2749 /* Advance past frames with one window with 'wfw' set. */
2750 while (frame_fixed_width(frp))
2751 {
2752 frp = frp->fr_next;
2753 if (frp == NULL)
2754 return; /* no frame without 'wfw', give up */
2755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002757 {
2758 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 while (frp->fr_next != NULL)
2760 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002761 if (wfw)
2762 /* Advance back for frames with one window with 'wfw' set. */
2763 while (frame_fixed_width(frp))
2764 frp = frp->fr_prev;
2765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766
2767 extra_cols = width - topfrp->fr_width;
2768 if (extra_cols < 0)
2769 {
2770 /* reduce frame width, rightmost frame first */
2771 while (frp != NULL)
2772 {
2773 w = frame_minwidth(frp, NULL);
2774 if (frp->fr_width + extra_cols < w)
2775 {
2776 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002777 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
2779 else
2780 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002781 frame_new_width(frp, frp->fr_width + extra_cols,
2782 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 break;
2784 }
2785 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002786 {
2787 do
2788 frp = frp->fr_next;
2789 while (wfw && frp != NULL && frame_fixed_width(frp));
2790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002792 {
2793 do
2794 frp = frp->fr_prev;
2795 while (wfw && frp != NULL && frame_fixed_width(frp));
2796 }
2797 /* Increase "width" if we could not reduce enough frames. */
2798 if (frp == NULL)
2799 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
2801 }
2802 else if (extra_cols > 0)
2803 {
2804 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002805 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 }
2807 }
2808 topfrp->fr_width = width;
2809}
2810
2811/*
2812 * Add the vertical separator to windows at the right side of "frp".
2813 * Note: Does not check if there is room!
2814 */
2815 static void
2816frame_add_vsep(frp)
2817 frame_T *frp;
2818{
2819 win_T *wp;
2820
2821 if (frp->fr_layout == FR_LEAF)
2822 {
2823 wp = frp->fr_win;
2824 if (wp->w_vsep_width == 0)
2825 {
2826 if (wp->w_width > 0) /* don't make it negative */
2827 --wp->w_width;
2828 wp->w_vsep_width = 1;
2829 }
2830 }
2831 else if (frp->fr_layout == FR_COL)
2832 {
2833 /* Handle all the frames in the column. */
2834 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2835 frame_add_vsep(frp);
2836 }
2837 else /* frp->fr_layout == FR_ROW */
2838 {
2839 /* Only need to handle the last frame in the row. */
2840 frp = frp->fr_child;
2841 while (frp->fr_next != NULL)
2842 frp = frp->fr_next;
2843 frame_add_vsep(frp);
2844 }
2845}
2846
2847/*
2848 * Set frame width from the window it contains.
2849 */
2850 static void
2851frame_fix_width(wp)
2852 win_T *wp;
2853{
2854 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2855}
2856#endif
2857
2858/*
2859 * Set frame height from the window it contains.
2860 */
2861 static void
2862frame_fix_height(wp)
2863 win_T *wp;
2864{
2865 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2866}
2867
2868/*
2869 * Compute the minimal height for frame "topfrp".
2870 * Uses the 'winminheight' option.
2871 * When "next_curwin" isn't NULL, use p_wh for this window.
2872 * When "next_curwin" is NOWIN, don't use at least one line for the current
2873 * window.
2874 */
2875 static int
2876frame_minheight(topfrp, next_curwin)
2877 frame_T *topfrp;
2878 win_T *next_curwin;
2879{
2880 frame_T *frp;
2881 int m;
2882#ifdef FEAT_VERTSPLIT
2883 int n;
2884#endif
2885
2886 if (topfrp->fr_win != NULL)
2887 {
2888 if (topfrp->fr_win == next_curwin)
2889 m = p_wh + topfrp->fr_win->w_status_height;
2890 else
2891 {
2892 /* window: minimal height of the window plus status line */
2893 m = p_wmh + topfrp->fr_win->w_status_height;
2894 /* Current window is minimal one line high */
2895 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2896 ++m;
2897 }
2898 }
2899#ifdef FEAT_VERTSPLIT
2900 else if (topfrp->fr_layout == FR_ROW)
2901 {
2902 /* get the minimal height from each frame in this row */
2903 m = 0;
2904 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2905 {
2906 n = frame_minheight(frp, next_curwin);
2907 if (n > m)
2908 m = n;
2909 }
2910 }
2911#endif
2912 else
2913 {
2914 /* Add up the minimal heights for all frames in this column. */
2915 m = 0;
2916 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2917 m += frame_minheight(frp, next_curwin);
2918 }
2919
2920 return m;
2921}
2922
2923#ifdef FEAT_VERTSPLIT
2924/*
2925 * Compute the minimal width for frame "topfrp".
2926 * When "next_curwin" isn't NULL, use p_wiw for this window.
2927 * When "next_curwin" is NOWIN, don't use at least one column for the current
2928 * window.
2929 */
2930 static int
2931frame_minwidth(topfrp, next_curwin)
2932 frame_T *topfrp;
2933 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2934{
2935 frame_T *frp;
2936 int m, n;
2937
2938 if (topfrp->fr_win != NULL)
2939 {
2940 if (topfrp->fr_win == next_curwin)
2941 m = p_wiw + topfrp->fr_win->w_vsep_width;
2942 else
2943 {
2944 /* window: minimal width of the window plus separator column */
2945 m = p_wmw + topfrp->fr_win->w_vsep_width;
2946 /* Current window is minimal one column wide */
2947 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2948 ++m;
2949 }
2950 }
2951 else if (topfrp->fr_layout == FR_COL)
2952 {
2953 /* get the minimal width from each frame in this column */
2954 m = 0;
2955 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2956 {
2957 n = frame_minwidth(frp, next_curwin);
2958 if (n > m)
2959 m = n;
2960 }
2961 }
2962 else
2963 {
2964 /* Add up the minimal widths for all frames in this row. */
2965 m = 0;
2966 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2967 m += frame_minwidth(frp, next_curwin);
2968 }
2969
2970 return m;
2971}
2972#endif
2973
2974
2975/*
2976 * Try to close all windows except current one.
2977 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2978 * used and the buffer was modified.
2979 *
2980 * Used by ":bdel" and ":only".
2981 */
2982 void
2983close_others(message, forceit)
2984 int message;
2985 int forceit; /* always hide all other windows */
2986{
2987 win_T *wp;
2988 win_T *nextwp;
2989 int r;
2990
2991 if (lastwin == firstwin)
2992 {
2993 if (message
2994#ifdef FEAT_AUTOCMD
2995 && !autocmd_busy
2996#endif
2997 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00002998 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 return;
3000 }
3001
3002 /* Be very careful here: autocommands may change the window layout. */
3003 for (wp = firstwin; win_valid(wp); wp = nextwp)
3004 {
3005 nextwp = wp->w_next;
3006 if (wp != curwin) /* don't close current window */
3007 {
3008
3009 /* Check if it's allowed to abandon this window */
3010 r = can_abandon(wp->w_buffer, forceit);
3011#ifdef FEAT_AUTOCMD
3012 if (!win_valid(wp)) /* autocommands messed wp up */
3013 {
3014 nextwp = firstwin;
3015 continue;
3016 }
3017#endif
3018 if (!r)
3019 {
3020#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3021 if (message && (p_confirm || cmdmod.confirm) && p_write)
3022 {
3023 dialog_changed(wp->w_buffer, FALSE);
3024# ifdef FEAT_AUTOCMD
3025 if (!win_valid(wp)) /* autocommands messed wp up */
3026 {
3027 nextwp = firstwin;
3028 continue;
3029 }
3030# endif
3031 }
3032 if (bufIsChanged(wp->w_buffer))
3033#endif
3034 continue;
3035 }
3036 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3037 }
3038 }
3039
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003040 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 EMSG(_("E445: Other window contains changes"));
3042}
3043
3044#endif /* FEAT_WINDOWS */
3045
3046/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003047 * Init the current window "curwin".
3048 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 */
3050 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003051curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003053 redraw_win_later(curwin, NOT_VALID);
3054 curwin->w_lines_valid = 0;
3055 curwin->w_cursor.lnum = 1;
3056 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003058 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003060 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3061 curwin->w_pcmark.col = 0;
3062 curwin->w_prev_pcmark.lnum = 0;
3063 curwin->w_prev_pcmark.col = 0;
3064 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003066 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003068 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069#ifdef FEAT_FKMAP
3070 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003071 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003073 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074#endif
3075}
3076
3077/*
3078 * Allocate the first window and put an empty buffer in it.
3079 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003080 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003082 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083win_alloc_first()
3084{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003085 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003086 return FAIL;
3087
3088#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003089 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003090 if (first_tabpage == NULL)
3091 return FAIL;
3092 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003093 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003094#endif
3095 return OK;
3096}
3097
3098/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003099 * Allocate the first window or the first window in a new tab page.
3100 * When "oldwin" is NULL create an empty buffer for it.
3101 * When "oldwin" is not NULL copy info from it to the new window (only with
3102 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003103 * Return FAIL when something goes wrong (out of memory).
3104 */
3105 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003106win_alloc_firstwin(oldwin)
3107 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003108{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003110 if (oldwin == NULL)
3111 {
3112 /* Very first window, need to create an empty buffer for it and
3113 * initialize from scratch. */
3114 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3115 if (curwin == NULL || curbuf == NULL)
3116 return FAIL;
3117 curwin->w_buffer = curbuf;
3118 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003120 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003122 curwin_init(); /* init current window */
3123 }
3124#ifdef FEAT_WINDOWS
3125 else
3126 {
3127 /* First window in new tab page, initialize it from "oldwin". */
3128 win_init(curwin, oldwin);
3129
3130# ifdef FEAT_SCROLLBIND
3131 /* We don't want scroll-binding in the first window. */
3132 curwin->w_p_scb = FALSE;
3133# endif
3134 }
3135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136
3137 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3138 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003139 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 topframe->fr_layout = FR_LEAF;
3141#ifdef FEAT_VERTSPLIT
3142 topframe->fr_width = Columns;
3143#endif
3144 topframe->fr_height = Rows - p_ch;
3145 topframe->fr_win = curwin;
3146 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003147
3148 return OK;
3149}
3150
3151/*
3152 * Initialize the window and frame size to the maximum.
3153 */
3154 void
3155win_init_size()
3156{
3157 firstwin->w_height = ROWS_AVAIL;
3158 topframe->fr_height = ROWS_AVAIL;
3159#ifdef FEAT_VERTSPLIT
3160 firstwin->w_width = Columns;
3161 topframe->fr_width = Columns;
3162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163}
3164
3165#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003166
3167/*
3168 * Allocate a new tabpage_T and init the values.
3169 * Returns NULL when out of memory.
3170 */
3171 static tabpage_T *
3172alloc_tabpage()
3173{
3174 tabpage_T *tp;
3175
3176 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3177 if (tp != NULL)
3178 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003179# ifdef FEAT_GUI
3180 int i;
3181
3182 for (i = 0; i < 3; i++)
3183 tp->tp_prev_which_scrollbars[i] = -1;
3184# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003185# ifdef FEAT_DIFF
3186 tp->tp_diff_invalid = TRUE;
3187# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003188#ifdef FEAT_EVAL
3189 /* init t: variables */
3190 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3191#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003192 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003193 }
3194 return tp;
3195}
3196
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003197 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003198free_tabpage(tp)
3199 tabpage_T *tp;
3200{
3201# ifdef FEAT_DIFF
3202 diff_clear(tp);
3203# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003204 clear_snapshot(tp);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003205#ifdef FEAT_EVAL
3206 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3207#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003208 vim_free(tp);
3209}
3210
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003211/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003212 * Create a new Tab page with one window.
3213 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003214 * When "after" is 0 put it just after the current Tab page.
3215 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003216 * Return FAIL or OK.
3217 */
3218 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003219win_new_tabpage(after)
3220 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003221{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003222 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003223 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003224 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003225
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003226 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003227 if (newtp == NULL)
3228 return FAIL;
3229
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003230 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003231 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003232 {
3233 vim_free(newtp);
3234 return FAIL;
3235 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003236 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003237
3238 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003239 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003240 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003241 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003242 if (after == 1)
3243 {
3244 /* New tab page becomes the first one. */
3245 newtp->tp_next = first_tabpage;
3246 first_tabpage = newtp;
3247 }
3248 else
3249 {
3250 if (after > 0)
3251 {
3252 /* Put new tab page before tab page "after". */
3253 n = 2;
3254 for (tp = first_tabpage; tp->tp_next != NULL
3255 && n < after; tp = tp->tp_next)
3256 ++n;
3257 }
3258 newtp->tp_next = tp->tp_next;
3259 tp->tp_next = newtp;
3260 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003261 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003262 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003263 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003264
3265 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003266 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003267
3268#if defined(FEAT_GUI)
3269 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3270 * scrollbars. Have to update them anyway. */
3271 if (gui.in_use && starting == 0)
3272 {
3273 gui_init_which_components(NULL);
3274 gui_update_scrollbars(TRUE);
3275 }
3276 need_mouse_correct = TRUE;
3277#endif
3278
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003279 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003280#ifdef FEAT_AUTOCMD
3281 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3282 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3283#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003284 return OK;
3285 }
3286
3287 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003288 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003289 return FAIL;
3290}
3291
3292/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003293 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3294 * like with ":split".
3295 * Returns OK if a new tab page was created, FAIL otherwise.
3296 */
3297 int
3298may_open_tabpage()
3299{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003300 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003301
Bram Moolenaard326ce82007-03-11 14:48:29 +00003302 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003303 {
3304 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003305 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003306 return win_new_tabpage(n);
3307 }
3308 return FAIL;
3309}
3310
3311/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003312 * Create up to "maxcount" tabpages with empty windows.
3313 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003314 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003315 int
3316make_tabpages(maxcount)
3317 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003318{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003319 int count = maxcount;
3320 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003321
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003322 /* Limit to 'tabpagemax' tabs. */
3323 if (count > p_tpm)
3324 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003325
3326#ifdef FEAT_AUTOCMD
3327 /*
3328 * Don't execute autocommands while creating the tab pages. Must do that
3329 * when putting the buffers in the windows.
3330 */
3331 ++autocmd_block;
3332#endif
3333
3334 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003335 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003336 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003337
3338#ifdef FEAT_AUTOCMD
3339 --autocmd_block;
3340#endif
3341
3342 /* return actual number of tab pages */
3343 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003344}
3345
3346/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003347 * Return TRUE when "tpc" points to a valid tab page.
3348 */
3349 int
3350valid_tabpage(tpc)
3351 tabpage_T *tpc;
3352{
3353 tabpage_T *tp;
3354
3355 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3356 if (tp == tpc)
3357 return TRUE;
3358 return FALSE;
3359}
3360
3361/*
3362 * Find tab page "n" (first one is 1). Returns NULL when not found.
3363 */
3364 tabpage_T *
3365find_tabpage(n)
3366 int n;
3367{
3368 tabpage_T *tp;
3369 int i = 1;
3370
3371 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3372 ++i;
3373 return tp;
3374}
3375
3376/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003377 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003378 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003379 */
3380 int
3381tabpage_index(ftp)
3382 tabpage_T *ftp;
3383{
3384 int i = 1;
3385 tabpage_T *tp;
3386
3387 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3388 ++i;
3389 return i;
3390}
3391
3392/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003393 * Prepare for leaving the current tab page.
3394 * When autocomands change "curtab" we don't leave the tab page and return
3395 * FAIL.
3396 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003397 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003398/*ARGSUSED*/
3399 static int
3400leave_tabpage(new_curbuf)
3401 buf_T *new_curbuf; /* what is going to be the new curbuf,
3402 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003403{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003404 tabpage_T *tp = curtab;
3405
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003406#ifdef FEAT_VISUAL
3407 reset_VIsual_and_resel(); /* stop Visual mode */
3408#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003409#ifdef FEAT_AUTOCMD
3410 if (new_curbuf != curbuf)
3411 {
3412 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3413 if (curtab != tp)
3414 return FAIL;
3415 }
3416 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3417 if (curtab != tp)
3418 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003419 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003420 if (curtab != tp)
3421 return FAIL;
3422#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003423#if defined(FEAT_GUI)
3424 /* Remove the scrollbars. They may be added back later. */
3425 if (gui.in_use)
3426 gui_remove_scrollbars();
3427#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003428 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003429 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003430 tp->tp_firstwin = firstwin;
3431 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003432 tp->tp_old_Rows = Rows;
3433 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003434 firstwin = NULL;
3435 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003436 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003437}
3438
3439/*
3440 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003441 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003442 */
3443/*ARGSUSED*/
3444 static void
3445enter_tabpage(tp, old_curbuf)
3446 tabpage_T *tp;
3447 buf_T *old_curbuf;
3448{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003449 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003450 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003451
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003452 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003453 firstwin = tp->tp_firstwin;
3454 lastwin = tp->tp_lastwin;
3455 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003456
3457 /* We would like doing the TabEnter event first, but we don't have a
3458 * valid current window yet, which may break some commands.
3459 * This triggers autocommands, thus may make "tp" invalid. */
3460 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3461 prevwin = next_prevwin;
3462
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003463#ifdef FEAT_AUTOCMD
3464 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003465
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003466 if (old_curbuf != curbuf)
3467 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3468#endif
3469
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003470 last_status(FALSE); /* status line may appear or disappear */
3471 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003472 must_redraw = CLEAR; /* need to redraw everything */
3473#ifdef FEAT_DIFF
3474 diff_need_scrollbind = TRUE;
3475#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003476
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003477 /* The tabpage line may have appeared or disappeared, may need to resize
3478 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003479 * frame sizes too. Use the stored value of p_ch, so that it can be
3480 * different for each tab page. */
3481 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003482 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3483#ifdef FEAT_GUI_TABLINE
3484 && !gui_use_tabline()
3485#endif
3486 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003487 shell_new_rows();
3488#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003489 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003490 shell_new_columns(); /* update window widths */
3491#endif
3492
3493#if defined(FEAT_GUI)
3494 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3495 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003496 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003497 {
3498 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003499 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003500 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003501 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003502#endif
3503
3504 redraw_all_later(CLEAR);
3505}
3506
3507/*
3508 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003509 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003510 */
3511 void
3512goto_tabpage(n)
3513 int n;
3514{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003515 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003516 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003517 int i;
3518
Bram Moolenaard68071d2006-05-02 22:08:30 +00003519 if (text_locked())
3520 {
3521 /* Not allowed when editing the command line. */
3522#ifdef FEAT_CMDWIN
3523 if (cmdwin_type != 0)
3524 EMSG(_(e_cmdwin));
3525 else
3526#endif
3527 EMSG(_(e_secure));
3528 return;
3529 }
3530
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003531 /* If there is only one it can't work. */
3532 if (first_tabpage->tp_next == NULL)
3533 {
3534 if (n > 1)
3535 beep_flush();
3536 return;
3537 }
3538
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003539 if (n == 0)
3540 {
3541 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003542 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003543 tp = first_tabpage;
3544 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003545 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003546 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003547 else if (n < 0)
3548 {
3549 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3550 * this N times. */
3551 ttp = curtab;
3552 for (i = n; i < 0; ++i)
3553 {
3554 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3555 tp = tp->tp_next)
3556 ;
3557 ttp = tp;
3558 }
3559 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003560 else if (n == 9999)
3561 {
3562 /* Go to last tab page. */
3563 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3564 ;
3565 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003566 else
3567 {
3568 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003569 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003570 if (tp == NULL)
3571 {
3572 beep_flush();
3573 return;
3574 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003575 }
3576
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003577 goto_tabpage_tp(tp);
3578
3579#ifdef FEAT_GUI_TABLINE
3580 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003581 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003582#endif
3583}
3584
3585/*
3586 * Go to tabpage "tp".
3587 * Note: doesn't update the GUI tab.
3588 */
3589 void
3590goto_tabpage_tp(tp)
3591 tabpage_T *tp;
3592{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003593 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003594 {
3595 if (valid_tabpage(tp))
3596 enter_tabpage(tp, curbuf);
3597 else
3598 enter_tabpage(curtab, curbuf);
3599 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003600}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601
3602/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003603 * Enter window "wp" in tab page "tp".
3604 * Also updates the GUI tab.
3605 */
3606 void
3607goto_tabpage_win(tp, wp)
3608 tabpage_T *tp;
3609 win_T *wp;
3610{
3611 goto_tabpage_tp(tp);
3612 if (curtab == tp && win_valid(wp))
3613 {
3614 win_enter(wp, TRUE);
3615# ifdef FEAT_GUI_TABLINE
3616 if (gui_use_tabline())
3617 gui_mch_set_curtab(tabpage_index(curtab));
3618# endif
3619 }
3620}
3621
3622/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003623 * Move the current tab page to before tab page "nr".
3624 */
3625 void
3626tabpage_move(nr)
3627 int nr;
3628{
3629 int n = nr;
3630 tabpage_T *tp;
3631
3632 if (first_tabpage->tp_next == NULL)
3633 return;
3634
3635 /* Remove the current tab page from the list of tab pages. */
3636 if (curtab == first_tabpage)
3637 first_tabpage = curtab->tp_next;
3638 else
3639 {
3640 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3641 if (tp->tp_next == curtab)
3642 break;
3643 if (tp == NULL) /* "cannot happen" */
3644 return;
3645 tp->tp_next = curtab->tp_next;
3646 }
3647
3648 /* Re-insert it at the specified position. */
3649 if (n == 0)
3650 {
3651 curtab->tp_next = first_tabpage;
3652 first_tabpage = curtab;
3653 }
3654 else
3655 {
3656 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3657 --n;
3658 curtab->tp_next = tp->tp_next;
3659 tp->tp_next = curtab;
3660 }
3661
3662 /* Need to redraw the tabline. Tab page contents doesn't change. */
3663 redraw_tabline = TRUE;
3664}
3665
3666
3667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 * Go to another window.
3669 * When jumping to another buffer, stop Visual mode. Do this before
3670 * changing windows so we can yank the selection into the '*' register.
3671 * When jumping to another window on the same buffer, adjust its cursor
3672 * position to keep the same Visual area.
3673 */
3674 void
3675win_goto(wp)
3676 win_T *wp;
3677{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003678 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 {
3680 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003681 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 return;
3683 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003684#ifdef FEAT_AUTOCMD
3685 if (curbuf_locked())
3686 return;
3687#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689#ifdef FEAT_VISUAL
3690 if (wp->w_buffer != curbuf)
3691 reset_VIsual_and_resel();
3692 else if (VIsual_active)
3693 wp->w_cursor = curwin->w_cursor;
3694#endif
3695
3696#ifdef FEAT_GUI
3697 need_mouse_correct = TRUE;
3698#endif
3699 win_enter(wp, TRUE);
3700}
3701
3702#if defined(FEAT_PERL) || defined(PROTO)
3703/*
3704 * Find window number "winnr" (counting top to bottom).
3705 */
3706 win_T *
3707win_find_nr(winnr)
3708 int winnr;
3709{
3710 win_T *wp;
3711
3712# ifdef FEAT_WINDOWS
3713 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3714 if (--winnr == 0)
3715 break;
3716 return wp;
3717# else
3718 return curwin;
3719# endif
3720}
3721#endif
3722
3723#ifdef FEAT_VERTSPLIT
3724/*
3725 * Move to window above or below "count" times.
3726 */
3727 static void
3728win_goto_ver(up, count)
3729 int up; /* TRUE to go to win above */
3730 long count;
3731{
3732 frame_T *fr;
3733 frame_T *nfr;
3734 frame_T *foundfr;
3735
3736 foundfr = curwin->w_frame;
3737 while (count--)
3738 {
3739 /*
3740 * First go upwards in the tree of frames until we find a upwards or
3741 * downwards neighbor.
3742 */
3743 fr = foundfr;
3744 for (;;)
3745 {
3746 if (fr == topframe)
3747 goto end;
3748 if (up)
3749 nfr = fr->fr_prev;
3750 else
3751 nfr = fr->fr_next;
3752 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3753 break;
3754 fr = fr->fr_parent;
3755 }
3756
3757 /*
3758 * Now go downwards to find the bottom or top frame in it.
3759 */
3760 for (;;)
3761 {
3762 if (nfr->fr_layout == FR_LEAF)
3763 {
3764 foundfr = nfr;
3765 break;
3766 }
3767 fr = nfr->fr_child;
3768 if (nfr->fr_layout == FR_ROW)
3769 {
3770 /* Find the frame at the cursor row. */
3771 while (fr->fr_next != NULL
3772 && frame2win(fr)->w_wincol + fr->fr_width
3773 <= curwin->w_wincol + curwin->w_wcol)
3774 fr = fr->fr_next;
3775 }
3776 if (nfr->fr_layout == FR_COL && up)
3777 while (fr->fr_next != NULL)
3778 fr = fr->fr_next;
3779 nfr = fr;
3780 }
3781 }
3782end:
3783 if (foundfr != NULL)
3784 win_goto(foundfr->fr_win);
3785}
3786
3787/*
3788 * Move to left or right window.
3789 */
3790 static void
3791win_goto_hor(left, count)
3792 int left; /* TRUE to go to left win */
3793 long count;
3794{
3795 frame_T *fr;
3796 frame_T *nfr;
3797 frame_T *foundfr;
3798
3799 foundfr = curwin->w_frame;
3800 while (count--)
3801 {
3802 /*
3803 * First go upwards in the tree of frames until we find a left or
3804 * right neighbor.
3805 */
3806 fr = foundfr;
3807 for (;;)
3808 {
3809 if (fr == topframe)
3810 goto end;
3811 if (left)
3812 nfr = fr->fr_prev;
3813 else
3814 nfr = fr->fr_next;
3815 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3816 break;
3817 fr = fr->fr_parent;
3818 }
3819
3820 /*
3821 * Now go downwards to find the leftmost or rightmost frame in it.
3822 */
3823 for (;;)
3824 {
3825 if (nfr->fr_layout == FR_LEAF)
3826 {
3827 foundfr = nfr;
3828 break;
3829 }
3830 fr = nfr->fr_child;
3831 if (nfr->fr_layout == FR_COL)
3832 {
3833 /* Find the frame at the cursor row. */
3834 while (fr->fr_next != NULL
3835 && frame2win(fr)->w_winrow + fr->fr_height
3836 <= curwin->w_winrow + curwin->w_wrow)
3837 fr = fr->fr_next;
3838 }
3839 if (nfr->fr_layout == FR_ROW && left)
3840 while (fr->fr_next != NULL)
3841 fr = fr->fr_next;
3842 nfr = fr;
3843 }
3844 }
3845end:
3846 if (foundfr != NULL)
3847 win_goto(foundfr->fr_win);
3848}
3849#endif
3850
3851/*
3852 * Make window "wp" the current window.
3853 */
3854 void
3855win_enter(wp, undo_sync)
3856 win_T *wp;
3857 int undo_sync;
3858{
3859 win_enter_ext(wp, undo_sync, FALSE);
3860}
3861
3862/*
3863 * Make window wp the current window.
3864 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3865 * been closed and isn't valid.
3866 */
3867 static void
3868win_enter_ext(wp, undo_sync, curwin_invalid)
3869 win_T *wp;
3870 int undo_sync;
3871 int curwin_invalid;
3872{
3873#ifdef FEAT_AUTOCMD
3874 int other_buffer = FALSE;
3875#endif
3876
3877 if (wp == curwin && !curwin_invalid) /* nothing to do */
3878 return;
3879
3880#ifdef FEAT_AUTOCMD
3881 if (!curwin_invalid)
3882 {
3883 /*
3884 * Be careful: If autocommands delete the window, return now.
3885 */
3886 if (wp->w_buffer != curbuf)
3887 {
3888 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3889 other_buffer = TRUE;
3890 if (!win_valid(wp))
3891 return;
3892 }
3893 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3894 if (!win_valid(wp))
3895 return;
3896# ifdef FEAT_EVAL
3897 /* autocmds may abort script processing */
3898 if (aborting())
3899 return;
3900# endif
3901 }
3902#endif
3903
3904 /* sync undo before leaving the current buffer */
3905 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003906 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 /* may have to copy the buffer options when 'cpo' contains 'S' */
3908 if (wp->w_buffer != curbuf)
3909 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3910 if (!curwin_invalid)
3911 {
3912 prevwin = curwin; /* remember for CTRL-W p */
3913 curwin->w_redr_status = TRUE;
3914 }
3915 curwin = wp;
3916 curbuf = wp->w_buffer;
3917 check_cursor();
3918#ifdef FEAT_VIRTUALEDIT
3919 if (!virtual_active())
3920 curwin->w_cursor.coladd = 0;
3921#endif
3922 changed_line_abv_curs(); /* assume cursor position needs updating */
3923
3924 if (curwin->w_localdir != NULL)
3925 {
3926 /* Window has a local directory: Save current directory as global
3927 * directory (unless that was done already) and change to the local
3928 * directory. */
3929 if (globaldir == NULL)
3930 {
3931 char_u cwd[MAXPATHL];
3932
3933 if (mch_dirname(cwd, MAXPATHL) == OK)
3934 globaldir = vim_strsave(cwd);
3935 }
3936 mch_chdir((char *)curwin->w_localdir);
3937 shorten_fnames(TRUE);
3938 }
3939 else if (globaldir != NULL)
3940 {
3941 /* Window doesn't have a local directory and we are not in the global
3942 * directory: Change to the global directory. */
3943 mch_chdir((char *)globaldir);
3944 vim_free(globaldir);
3945 globaldir = NULL;
3946 shorten_fnames(TRUE);
3947 }
3948
3949#ifdef FEAT_AUTOCMD
3950 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3951 if (other_buffer)
3952 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3953#endif
3954
3955#ifdef FEAT_TITLE
3956 maketitle();
3957#endif
3958 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003959 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 if (restart_edit)
3961 redraw_later(VALID); /* causes status line redraw */
3962
3963 /* set window height to desired minimal value */
3964 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3965 win_setheight((int)p_wh);
3966 else if (curwin->w_height == 0)
3967 win_setheight(1);
3968
3969#ifdef FEAT_VERTSPLIT
3970 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003971 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 win_setwidth((int)p_wiw);
3973#endif
3974
3975#ifdef FEAT_MOUSE
3976 setmouse(); /* in case jumped to/from help buffer */
3977#endif
3978
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003979 /* Change directories when the 'acd' option is set. */
3980 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981}
3982
3983#endif /* FEAT_WINDOWS */
3984
3985#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3986/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003987 * Jump to the first open window that contains buffer "buf", if one exists.
3988 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 */
3990 win_T *
3991buf_jump_open_win(buf)
3992 buf_T *buf;
3993{
3994# ifdef FEAT_WINDOWS
3995 win_T *wp;
3996
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003997 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (wp->w_buffer == buf)
3999 break;
4000 if (wp != NULL)
4001 win_enter(wp, FALSE);
4002 return wp;
4003# else
4004 if (curwin->w_buffer == buf)
4005 return curwin;
4006 return NULL;
4007# endif
4008}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004009
4010/*
4011 * Jump to the first open window in any tab page that contains buffer "buf",
4012 * if one exists.
4013 * Returns a pointer to the window found, otherwise NULL.
4014 */
4015 win_T *
4016buf_jump_open_tab(buf)
4017 buf_T *buf;
4018{
4019# ifdef FEAT_WINDOWS
4020 win_T *wp;
4021 tabpage_T *tp;
4022
4023 /* First try the current tab page. */
4024 wp = buf_jump_open_win(buf);
4025 if (wp != NULL)
4026 return wp;
4027
4028 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4029 if (tp != curtab)
4030 {
4031 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4032 if (wp->w_buffer == buf)
4033 break;
4034 if (wp != NULL)
4035 {
4036 goto_tabpage_win(tp, wp);
4037 if (curwin != wp)
4038 wp = NULL; /* something went wrong */
4039 break;
4040 }
4041 }
4042
4043 return wp;
4044# else
4045 if (curwin->w_buffer == buf)
4046 return curwin;
4047 return NULL;
4048# endif
4049}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050#endif
4051
4052/*
4053 * allocate a window structure and link it in the window list
4054 */
4055/*ARGSUSED*/
4056 static win_T *
4057win_alloc(after)
4058 win_T *after;
4059{
4060 win_T *newwin;
4061
4062 /*
4063 * allocate window structure and linesizes arrays
4064 */
4065 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4066 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4067 {
4068 vim_free(newwin);
4069 newwin = NULL;
4070 }
4071
4072 if (newwin != NULL)
4073 {
4074 /*
4075 * link the window in the window list
4076 */
4077#ifdef FEAT_WINDOWS
4078 win_append(after, newwin);
4079#endif
4080#ifdef FEAT_VERTSPLIT
4081 newwin->w_wincol = 0;
4082 newwin->w_width = Columns;
4083#endif
4084
4085 /* position the display and the cursor at the top of the file. */
4086 newwin->w_topline = 1;
4087#ifdef FEAT_DIFF
4088 newwin->w_topfill = 0;
4089#endif
4090 newwin->w_botline = 2;
4091 newwin->w_cursor.lnum = 1;
4092#ifdef FEAT_SCROLLBIND
4093 newwin->w_scbind_pos = 1;
4094#endif
4095
4096 /* We won't calculate w_fraction until resizing the window */
4097 newwin->w_fraction = 0;
4098 newwin->w_prev_fraction_row = -1;
4099
4100#ifdef FEAT_GUI
4101 if (gui.in_use)
4102 {
4103 out_flush();
4104 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4105 SBAR_LEFT, newwin);
4106 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4107 SBAR_RIGHT, newwin);
4108 }
4109#endif
4110#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004111 /* init w: variables */
4112 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113#endif
4114#ifdef FEAT_FOLDING
4115 foldInitWin(newwin);
4116#endif
4117 }
4118 return newwin;
4119}
4120
4121#if defined(FEAT_WINDOWS) || defined(PROTO)
4122
4123/*
4124 * remove window 'wp' from the window list and free the structure
4125 */
4126 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004127win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004129 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130{
4131 int i;
4132
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004133#ifdef FEAT_MZSCHEME
4134 mzscheme_window_free(wp);
4135#endif
4136
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137#ifdef FEAT_PERL
4138 perl_win_free(wp);
4139#endif
4140
4141#ifdef FEAT_PYTHON
4142 python_window_free(wp);
4143#endif
4144
4145#ifdef FEAT_TCL
4146 tcl_window_free(wp);
4147#endif
4148
4149#ifdef FEAT_RUBY
4150 ruby_window_free(wp);
4151#endif
4152
4153 clear_winopt(&wp->w_onebuf_opt);
4154 clear_winopt(&wp->w_allbuf_opt);
4155
4156#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004157 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158#endif
4159
4160 if (prevwin == wp)
4161 prevwin = NULL;
4162 win_free_lsize(wp);
4163
4164 for (i = 0; i < wp->w_tagstacklen; ++i)
4165 vim_free(wp->w_tagstack[i].tagname);
4166
4167 vim_free(wp->w_localdir);
4168#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004169 vim_free(wp->w_match[0].regprog);
4170 vim_free(wp->w_match[1].regprog);
4171 vim_free(wp->w_match[2].regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172#endif
4173#ifdef FEAT_JUMPLIST
4174 free_jumplist(wp);
4175#endif
4176
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004177#ifdef FEAT_QUICKFIX
4178 qf_free_all(wp);
4179#endif
4180
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181#ifdef FEAT_GUI
4182 if (gui.in_use)
4183 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4185 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4186 }
4187#endif /* FEAT_GUI */
4188
Bram Moolenaarf740b292006-02-16 22:11:02 +00004189 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 vim_free(wp);
4191}
4192
4193/*
4194 * Append window "wp" in the window list after window "after".
4195 */
4196 static void
4197win_append(after, wp)
4198 win_T *after, *wp;
4199{
4200 win_T *before;
4201
4202 if (after == NULL) /* after NULL is in front of the first */
4203 before = firstwin;
4204 else
4205 before = after->w_next;
4206
4207 wp->w_next = before;
4208 wp->w_prev = after;
4209 if (after == NULL)
4210 firstwin = wp;
4211 else
4212 after->w_next = wp;
4213 if (before == NULL)
4214 lastwin = wp;
4215 else
4216 before->w_prev = wp;
4217}
4218
4219/*
4220 * Remove a window from the window list.
4221 */
4222 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004223win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004225 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226{
4227 if (wp->w_prev != NULL)
4228 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004229 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004231 else
4232 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 if (wp->w_next != NULL)
4234 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004235 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004237 else
4238 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239}
4240
4241/*
4242 * Append frame "frp" in a frame list after frame "after".
4243 */
4244 static void
4245frame_append(after, frp)
4246 frame_T *after, *frp;
4247{
4248 frp->fr_next = after->fr_next;
4249 after->fr_next = frp;
4250 if (frp->fr_next != NULL)
4251 frp->fr_next->fr_prev = frp;
4252 frp->fr_prev = after;
4253}
4254
4255/*
4256 * Insert frame "frp" in a frame list before frame "before".
4257 */
4258 static void
4259frame_insert(before, frp)
4260 frame_T *before, *frp;
4261{
4262 frp->fr_next = before;
4263 frp->fr_prev = before->fr_prev;
4264 before->fr_prev = frp;
4265 if (frp->fr_prev != NULL)
4266 frp->fr_prev->fr_next = frp;
4267 else
4268 frp->fr_parent->fr_child = frp;
4269}
4270
4271/*
4272 * Remove a frame from a frame list.
4273 */
4274 static void
4275frame_remove(frp)
4276 frame_T *frp;
4277{
4278 if (frp->fr_prev != NULL)
4279 frp->fr_prev->fr_next = frp->fr_next;
4280 else
4281 frp->fr_parent->fr_child = frp->fr_next;
4282 if (frp->fr_next != NULL)
4283 frp->fr_next->fr_prev = frp->fr_prev;
4284}
4285
4286#endif /* FEAT_WINDOWS */
4287
4288/*
4289 * Allocate w_lines[] for window "wp".
4290 * Return FAIL for failure, OK for success.
4291 */
4292 int
4293win_alloc_lines(wp)
4294 win_T *wp;
4295{
4296 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004297 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 if (wp->w_lines == NULL)
4299 return FAIL;
4300 return OK;
4301}
4302
4303/*
4304 * free lsize arrays for a window
4305 */
4306 void
4307win_free_lsize(wp)
4308 win_T *wp;
4309{
4310 vim_free(wp->w_lines);
4311 wp->w_lines = NULL;
4312}
4313
4314/*
4315 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004316 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 */
4318 void
4319shell_new_rows()
4320{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004321 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 if (firstwin == NULL) /* not initialized yet */
4324 return;
4325#ifdef FEAT_WINDOWS
4326 if (h < frame_minheight(topframe, NULL))
4327 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004328
4329 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 * that doesn't result in the right height, forget about that option. */
4331 frame_new_height(topframe, h, FALSE, TRUE);
4332 if (topframe->fr_height != h)
4333 frame_new_height(topframe, h, FALSE, FALSE);
4334
4335 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4336#else
4337 if (h < 1)
4338 h = 1;
4339 win_new_height(firstwin, h);
4340#endif
4341 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004342#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004343 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004344#endif
4345
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346#if 0
4347 /* Disabled: don't want making the screen smaller make a window larger. */
4348 if (p_ea)
4349 win_equal(curwin, FALSE, 'v');
4350#endif
4351}
4352
4353#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4354/*
4355 * Called from win_new_shellsize() after Columns changed.
4356 */
4357 void
4358shell_new_columns()
4359{
4360 if (firstwin == NULL) /* not initialized yet */
4361 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004362
4363 /* First try setting the widths of windows with 'winfixwidth'. If that
4364 * doesn't result in the right width, forget about that option. */
4365 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4366 if (topframe->fr_width != Columns)
4367 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4368
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4370#if 0
4371 /* Disabled: don't want making the screen smaller make a window larger. */
4372 if (p_ea)
4373 win_equal(curwin, FALSE, 'h');
4374#endif
4375}
4376#endif
4377
4378#if defined(FEAT_CMDWIN) || defined(PROTO)
4379/*
4380 * Save the size of all windows in "gap".
4381 */
4382 void
4383win_size_save(gap)
4384 garray_T *gap;
4385
4386{
4387 win_T *wp;
4388
4389 ga_init2(gap, (int)sizeof(int), 1);
4390 if (ga_grow(gap, win_count() * 2) == OK)
4391 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4392 {
4393 ((int *)gap->ga_data)[gap->ga_len++] =
4394 wp->w_width + wp->w_vsep_width;
4395 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4396 }
4397}
4398
4399/*
4400 * Restore window sizes, but only if the number of windows is still the same.
4401 * Does not free the growarray.
4402 */
4403 void
4404win_size_restore(gap)
4405 garray_T *gap;
4406{
4407 win_T *wp;
4408 int i;
4409
4410 if (win_count() * 2 == gap->ga_len)
4411 {
4412 i = 0;
4413 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4414 {
4415 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4416 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4417 }
4418 /* recompute the window positions */
4419 (void)win_comp_pos();
4420 }
4421}
4422#endif /* FEAT_CMDWIN */
4423
4424#if defined(FEAT_WINDOWS) || defined(PROTO)
4425/*
4426 * Update the position for all windows, using the width and height of the
4427 * frames.
4428 * Returns the row just after the last window.
4429 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004430 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431win_comp_pos()
4432{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004433 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 int col = 0;
4435
4436 frame_comp_pos(topframe, &row, &col);
4437 return row;
4438}
4439
4440/*
4441 * Update the position of the windows in frame "topfrp", using the width and
4442 * height of the frames.
4443 * "*row" and "*col" are the top-left position of the frame. They are updated
4444 * to the bottom-right position plus one.
4445 */
4446 static void
4447frame_comp_pos(topfrp, row, col)
4448 frame_T *topfrp;
4449 int *row;
4450 int *col;
4451{
4452 win_T *wp;
4453 frame_T *frp;
4454#ifdef FEAT_VERTSPLIT
4455 int startcol;
4456 int startrow;
4457#endif
4458
4459 wp = topfrp->fr_win;
4460 if (wp != NULL)
4461 {
4462 if (wp->w_winrow != *row
4463#ifdef FEAT_VERTSPLIT
4464 || wp->w_wincol != *col
4465#endif
4466 )
4467 {
4468 /* position changed, redraw */
4469 wp->w_winrow = *row;
4470#ifdef FEAT_VERTSPLIT
4471 wp->w_wincol = *col;
4472#endif
4473 redraw_win_later(wp, NOT_VALID);
4474 wp->w_redr_status = TRUE;
4475 }
4476 *row += wp->w_height + wp->w_status_height;
4477#ifdef FEAT_VERTSPLIT
4478 *col += wp->w_width + wp->w_vsep_width;
4479#endif
4480 }
4481 else
4482 {
4483#ifdef FEAT_VERTSPLIT
4484 startrow = *row;
4485 startcol = *col;
4486#endif
4487 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4488 {
4489#ifdef FEAT_VERTSPLIT
4490 if (topfrp->fr_layout == FR_ROW)
4491 *row = startrow; /* all frames are at the same row */
4492 else
4493 *col = startcol; /* all frames are at the same col */
4494#endif
4495 frame_comp_pos(frp, row, col);
4496 }
4497 }
4498}
4499
4500#endif /* FEAT_WINDOWS */
4501
4502/*
4503 * Set current window height and take care of repositioning other windows to
4504 * fit around it.
4505 */
4506 void
4507win_setheight(height)
4508 int height;
4509{
4510 win_setheight_win(height, curwin);
4511}
4512
4513/*
4514 * Set the window height of window "win" and take care of repositioning other
4515 * windows to fit around it.
4516 */
4517 void
4518win_setheight_win(height, win)
4519 int height;
4520 win_T *win;
4521{
4522 int row;
4523
4524 if (win == curwin)
4525 {
4526 /* Always keep current window at least one line high, even when
4527 * 'winminheight' is zero. */
4528#ifdef FEAT_WINDOWS
4529 if (height < p_wmh)
4530 height = p_wmh;
4531#endif
4532 if (height == 0)
4533 height = 1;
4534 }
4535
4536#ifdef FEAT_WINDOWS
4537 frame_setheight(win->w_frame, height + win->w_status_height);
4538
4539 /* recompute the window positions */
4540 row = win_comp_pos();
4541#else
4542 if (height > topframe->fr_height)
4543 height = topframe->fr_height;
4544 win->w_height = height;
4545 row = height;
4546#endif
4547
4548 /*
4549 * If there is extra space created between the last window and the command
4550 * line, clear it.
4551 */
4552 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4553 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4554 cmdline_row = row;
4555 msg_row = row;
4556 msg_col = 0;
4557
4558 redraw_all_later(NOT_VALID);
4559}
4560
4561#if defined(FEAT_WINDOWS) || defined(PROTO)
4562
4563/*
4564 * Set the height of a frame to "height" and take care that all frames and
4565 * windows inside it are resized. Also resize frames on the left and right if
4566 * the are in the same FR_ROW frame.
4567 *
4568 * Strategy:
4569 * If the frame is part of a FR_COL frame, try fitting the frame in that
4570 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4571 * go to containing frames to resize them and make room.
4572 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4573 * Check for the minimal height of the FR_ROW frame.
4574 * At the top level we can also use change the command line height.
4575 */
4576 static void
4577frame_setheight(curfrp, height)
4578 frame_T *curfrp;
4579 int height;
4580{
4581 int room; /* total number of lines available */
4582 int take; /* number of lines taken from other windows */
4583 int room_cmdline; /* lines available from cmdline */
4584 int run;
4585 frame_T *frp;
4586 int h;
4587 int room_reserved;
4588
4589 /* If the height already is the desired value, nothing to do. */
4590 if (curfrp->fr_height == height)
4591 return;
4592
4593 if (curfrp->fr_parent == NULL)
4594 {
4595 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004596 if (height > ROWS_AVAIL)
4597 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 if (height > 0)
4599 frame_new_height(curfrp, height, FALSE, FALSE);
4600 }
4601 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4602 {
4603 /* Row of frames: Also need to resize frames left and right of this
4604 * one. First check for the minimal height of these. */
4605 h = frame_minheight(curfrp->fr_parent, NULL);
4606 if (height < h)
4607 height = h;
4608 frame_setheight(curfrp->fr_parent, height);
4609 }
4610 else
4611 {
4612 /*
4613 * Column of frames: try to change only frames in this column.
4614 */
4615#ifdef FEAT_VERTSPLIT
4616 /*
4617 * Do this twice:
4618 * 1: compute room available, if it's not enough try resizing the
4619 * containing frame.
4620 * 2: compute the room available and adjust the height to it.
4621 * Try not to reduce the height of a window with 'winfixheight' set.
4622 */
4623 for (run = 1; run <= 2; ++run)
4624#else
4625 for (;;)
4626#endif
4627 {
4628 room = 0;
4629 room_reserved = 0;
4630 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4631 frp = frp->fr_next)
4632 {
4633 if (frp != curfrp
4634 && frp->fr_win != NULL
4635 && frp->fr_win->w_p_wfh)
4636 room_reserved += frp->fr_height;
4637 room += frp->fr_height;
4638 if (frp != curfrp)
4639 room -= frame_minheight(frp, NULL);
4640 }
4641#ifdef FEAT_VERTSPLIT
4642 if (curfrp->fr_width != Columns)
4643 room_cmdline = 0;
4644 else
4645#endif
4646 {
4647 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4648 + lastwin->w_height + lastwin->w_status_height);
4649 if (room_cmdline < 0)
4650 room_cmdline = 0;
4651 }
4652
4653 if (height <= room + room_cmdline)
4654 break;
4655#ifdef FEAT_VERTSPLIT
4656 if (run == 2 || curfrp->fr_width == Columns)
4657#endif
4658 {
4659 if (height > room + room_cmdline)
4660 height = room + room_cmdline;
4661 break;
4662 }
4663#ifdef FEAT_VERTSPLIT
4664 frame_setheight(curfrp->fr_parent, height
4665 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4666#endif
4667 /*NOTREACHED*/
4668 }
4669
4670 /*
4671 * Compute the number of lines we will take from others frames (can be
4672 * negative!).
4673 */
4674 take = height - curfrp->fr_height;
4675
4676 /* If there is not enough room, also reduce the height of a window
4677 * with 'winfixheight' set. */
4678 if (height > room + room_cmdline - room_reserved)
4679 room_reserved = room + room_cmdline - height;
4680 /* If there is only a 'winfixheight' window and making the
4681 * window smaller, need to make the other window taller. */
4682 if (take < 0 && room - curfrp->fr_height < room_reserved)
4683 room_reserved = 0;
4684
4685 if (take > 0 && room_cmdline > 0)
4686 {
4687 /* use lines from cmdline first */
4688 if (take < room_cmdline)
4689 room_cmdline = take;
4690 take -= room_cmdline;
4691 topframe->fr_height += room_cmdline;
4692 }
4693
4694 /*
4695 * set the current frame to the new height
4696 */
4697 frame_new_height(curfrp, height, FALSE, FALSE);
4698
4699 /*
4700 * First take lines from the frames after the current frame. If
4701 * that is not enough, takes lines from frames above the current
4702 * frame.
4703 */
4704 for (run = 0; run < 2; ++run)
4705 {
4706 if (run == 0)
4707 frp = curfrp->fr_next; /* 1st run: start with next window */
4708 else
4709 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4710 while (frp != NULL && take != 0)
4711 {
4712 h = frame_minheight(frp, NULL);
4713 if (room_reserved > 0
4714 && frp->fr_win != NULL
4715 && frp->fr_win->w_p_wfh)
4716 {
4717 if (room_reserved >= frp->fr_height)
4718 room_reserved -= frp->fr_height;
4719 else
4720 {
4721 if (frp->fr_height - room_reserved > take)
4722 room_reserved = frp->fr_height - take;
4723 take -= frp->fr_height - room_reserved;
4724 frame_new_height(frp, room_reserved, FALSE, FALSE);
4725 room_reserved = 0;
4726 }
4727 }
4728 else
4729 {
4730 if (frp->fr_height - take < h)
4731 {
4732 take -= frp->fr_height - h;
4733 frame_new_height(frp, h, FALSE, FALSE);
4734 }
4735 else
4736 {
4737 frame_new_height(frp, frp->fr_height - take,
4738 FALSE, FALSE);
4739 take = 0;
4740 }
4741 }
4742 if (run == 0)
4743 frp = frp->fr_next;
4744 else
4745 frp = frp->fr_prev;
4746 }
4747 }
4748 }
4749}
4750
4751#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4752/*
4753 * Set current window width and take care of repositioning other windows to
4754 * fit around it.
4755 */
4756 void
4757win_setwidth(width)
4758 int width;
4759{
4760 win_setwidth_win(width, curwin);
4761}
4762
4763 void
4764win_setwidth_win(width, wp)
4765 int width;
4766 win_T *wp;
4767{
4768 /* Always keep current window at least one column wide, even when
4769 * 'winminwidth' is zero. */
4770 if (wp == curwin)
4771 {
4772 if (width < p_wmw)
4773 width = p_wmw;
4774 if (width == 0)
4775 width = 1;
4776 }
4777
4778 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4779
4780 /* recompute the window positions */
4781 (void)win_comp_pos();
4782
4783 redraw_all_later(NOT_VALID);
4784}
4785
4786/*
4787 * Set the width of a frame to "width" and take care that all frames and
4788 * windows inside it are resized. Also resize frames above and below if the
4789 * are in the same FR_ROW frame.
4790 *
4791 * Strategy is similar to frame_setheight().
4792 */
4793 static void
4794frame_setwidth(curfrp, width)
4795 frame_T *curfrp;
4796 int width;
4797{
4798 int room; /* total number of lines available */
4799 int take; /* number of lines taken from other windows */
4800 int run;
4801 frame_T *frp;
4802 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004803 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804
4805 /* If the width already is the desired value, nothing to do. */
4806 if (curfrp->fr_width == width)
4807 return;
4808
4809 if (curfrp->fr_parent == NULL)
4810 /* topframe: can't change width */
4811 return;
4812
4813 if (curfrp->fr_parent->fr_layout == FR_COL)
4814 {
4815 /* Column of frames: Also need to resize frames above and below of
4816 * this one. First check for the minimal width of these. */
4817 w = frame_minwidth(curfrp->fr_parent, NULL);
4818 if (width < w)
4819 width = w;
4820 frame_setwidth(curfrp->fr_parent, width);
4821 }
4822 else
4823 {
4824 /*
4825 * Row of frames: try to change only frames in this row.
4826 *
4827 * Do this twice:
4828 * 1: compute room available, if it's not enough try resizing the
4829 * containing frame.
4830 * 2: compute the room available and adjust the width to it.
4831 */
4832 for (run = 1; run <= 2; ++run)
4833 {
4834 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004835 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4837 frp = frp->fr_next)
4838 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004839 if (frp != curfrp
4840 && frp->fr_win != NULL
4841 && frp->fr_win->w_p_wfw)
4842 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 room += frp->fr_width;
4844 if (frp != curfrp)
4845 room -= frame_minwidth(frp, NULL);
4846 }
4847
4848 if (width <= room)
4849 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004850 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 {
4852 if (width > room)
4853 width = room;
4854 break;
4855 }
4856 frame_setwidth(curfrp->fr_parent, width
4857 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4858 }
4859
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 /*
4861 * Compute the number of lines we will take from others frames (can be
4862 * negative!).
4863 */
4864 take = width - curfrp->fr_width;
4865
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004866 /* If there is not enough room, also reduce the width of a window
4867 * with 'winfixwidth' set. */
4868 if (width > room - room_reserved)
4869 room_reserved = room - width;
4870 /* If there is only a 'winfixwidth' window and making the
4871 * window smaller, need to make the other window narrower. */
4872 if (take < 0 && room - curfrp->fr_width < room_reserved)
4873 room_reserved = 0;
4874
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 /*
4876 * set the current frame to the new width
4877 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004878 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880 /*
4881 * First take lines from the frames right of the current frame. If
4882 * that is not enough, takes lines from frames left of the current
4883 * frame.
4884 */
4885 for (run = 0; run < 2; ++run)
4886 {
4887 if (run == 0)
4888 frp = curfrp->fr_next; /* 1st run: start with next window */
4889 else
4890 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4891 while (frp != NULL && take != 0)
4892 {
4893 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004894 if (room_reserved > 0
4895 && frp->fr_win != NULL
4896 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004898 if (room_reserved >= frp->fr_width)
4899 room_reserved -= frp->fr_width;
4900 else
4901 {
4902 if (frp->fr_width - room_reserved > take)
4903 room_reserved = frp->fr_width - take;
4904 take -= frp->fr_width - room_reserved;
4905 frame_new_width(frp, room_reserved, FALSE, FALSE);
4906 room_reserved = 0;
4907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 }
4909 else
4910 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004911 if (frp->fr_width - take < w)
4912 {
4913 take -= frp->fr_width - w;
4914 frame_new_width(frp, w, FALSE, FALSE);
4915 }
4916 else
4917 {
4918 frame_new_width(frp, frp->fr_width - take,
4919 FALSE, FALSE);
4920 take = 0;
4921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923 if (run == 0)
4924 frp = frp->fr_next;
4925 else
4926 frp = frp->fr_prev;
4927 }
4928 }
4929 }
4930}
4931#endif /* FEAT_VERTSPLIT */
4932
4933/*
4934 * Check 'winminheight' for a valid value.
4935 */
4936 void
4937win_setminheight()
4938{
4939 int room;
4940 int first = TRUE;
4941 win_T *wp;
4942
4943 /* loop until there is a 'winminheight' that is possible */
4944 while (p_wmh > 0)
4945 {
4946 /* TODO: handle vertical splits */
4947 room = -p_wh;
4948 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4949 room += wp->w_height - p_wmh;
4950 if (room >= 0)
4951 break;
4952 --p_wmh;
4953 if (first)
4954 {
4955 EMSG(_(e_noroom));
4956 first = FALSE;
4957 }
4958 }
4959}
4960
4961#ifdef FEAT_MOUSE
4962
4963/*
4964 * Status line of dragwin is dragged "offset" lines down (negative is up).
4965 */
4966 void
4967win_drag_status_line(dragwin, offset)
4968 win_T *dragwin;
4969 int offset;
4970{
4971 frame_T *curfr;
4972 frame_T *fr;
4973 int room;
4974 int row;
4975 int up; /* if TRUE, drag status line up, otherwise down */
4976 int n;
4977
4978 fr = dragwin->w_frame;
4979 curfr = fr;
4980 if (fr != topframe) /* more than one window */
4981 {
4982 fr = fr->fr_parent;
4983 /* When the parent frame is not a column of frames, its parent should
4984 * be. */
4985 if (fr->fr_layout != FR_COL)
4986 {
4987 curfr = fr;
4988 if (fr != topframe) /* only a row of windows, may drag statusline */
4989 fr = fr->fr_parent;
4990 }
4991 }
4992
4993 /* If this is the last frame in a column, may want to resize the parent
4994 * frame instead (go two up to skip a row of frames). */
4995 while (curfr != topframe && curfr->fr_next == NULL)
4996 {
4997 if (fr != topframe)
4998 fr = fr->fr_parent;
4999 curfr = fr;
5000 if (fr != topframe)
5001 fr = fr->fr_parent;
5002 }
5003
5004 if (offset < 0) /* drag up */
5005 {
5006 up = TRUE;
5007 offset = -offset;
5008 /* sum up the room of the current frame and above it */
5009 if (fr == curfr)
5010 {
5011 /* only one window */
5012 room = fr->fr_height - frame_minheight(fr, NULL);
5013 }
5014 else
5015 {
5016 room = 0;
5017 for (fr = fr->fr_child; ; fr = fr->fr_next)
5018 {
5019 room += fr->fr_height - frame_minheight(fr, NULL);
5020 if (fr == curfr)
5021 break;
5022 }
5023 }
5024 fr = curfr->fr_next; /* put fr at frame that grows */
5025 }
5026 else /* drag down */
5027 {
5028 up = FALSE;
5029 /*
5030 * Only dragging the last status line can reduce p_ch.
5031 */
5032 room = Rows - cmdline_row;
5033 if (curfr->fr_next == NULL)
5034 room -= 1;
5035 else
5036 room -= p_ch;
5037 if (room < 0)
5038 room = 0;
5039 /* sum up the room of frames below of the current one */
5040 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5041 room += fr->fr_height - frame_minheight(fr, NULL);
5042 fr = curfr; /* put fr at window that grows */
5043 }
5044
5045 if (room < offset) /* Not enough room */
5046 offset = room; /* Move as far as we can */
5047 if (offset <= 0)
5048 return;
5049
5050 /*
5051 * Grow frame fr by "offset" lines.
5052 * Doesn't happen when dragging the last status line up.
5053 */
5054 if (fr != NULL)
5055 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5056
5057 if (up)
5058 fr = curfr; /* current frame gets smaller */
5059 else
5060 fr = curfr->fr_next; /* next frame gets smaller */
5061
5062 /*
5063 * Now make the other frames smaller.
5064 */
5065 while (fr != NULL && offset > 0)
5066 {
5067 n = frame_minheight(fr, NULL);
5068 if (fr->fr_height - offset <= n)
5069 {
5070 offset -= fr->fr_height - n;
5071 frame_new_height(fr, n, !up, FALSE);
5072 }
5073 else
5074 {
5075 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5076 break;
5077 }
5078 if (up)
5079 fr = fr->fr_prev;
5080 else
5081 fr = fr->fr_next;
5082 }
5083 row = win_comp_pos();
5084 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5085 cmdline_row = row;
5086 p_ch = Rows - cmdline_row;
5087 if (p_ch < 1)
5088 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005089 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005090 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 showmode();
5092}
5093
5094#ifdef FEAT_VERTSPLIT
5095/*
5096 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5097 */
5098 void
5099win_drag_vsep_line(dragwin, offset)
5100 win_T *dragwin;
5101 int offset;
5102{
5103 frame_T *curfr;
5104 frame_T *fr;
5105 int room;
5106 int left; /* if TRUE, drag separator line left, otherwise right */
5107 int n;
5108
5109 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005110 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 return;
5112 curfr = fr;
5113 fr = fr->fr_parent;
5114 /* When the parent frame is not a row of frames, its parent should be. */
5115 if (fr->fr_layout != FR_ROW)
5116 {
5117 if (fr == topframe) /* only a column of windows (cannot happen?) */
5118 return;
5119 curfr = fr;
5120 fr = fr->fr_parent;
5121 }
5122
5123 /* If this is the last frame in a row, may want to resize a parent
5124 * frame instead. */
5125 while (curfr->fr_next == NULL)
5126 {
5127 if (fr == topframe)
5128 break;
5129 curfr = fr;
5130 fr = fr->fr_parent;
5131 if (fr != topframe)
5132 {
5133 curfr = fr;
5134 fr = fr->fr_parent;
5135 }
5136 }
5137
5138 if (offset < 0) /* drag left */
5139 {
5140 left = TRUE;
5141 offset = -offset;
5142 /* sum up the room of the current frame and left of it */
5143 room = 0;
5144 for (fr = fr->fr_child; ; fr = fr->fr_next)
5145 {
5146 room += fr->fr_width - frame_minwidth(fr, NULL);
5147 if (fr == curfr)
5148 break;
5149 }
5150 fr = curfr->fr_next; /* put fr at frame that grows */
5151 }
5152 else /* drag right */
5153 {
5154 left = FALSE;
5155 /* sum up the room of frames right of the current one */
5156 room = 0;
5157 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5158 room += fr->fr_width - frame_minwidth(fr, NULL);
5159 fr = curfr; /* put fr at window that grows */
5160 }
5161
5162 if (room < offset) /* Not enough room */
5163 offset = room; /* Move as far as we can */
5164 if (offset <= 0) /* No room at all, quit. */
5165 return;
5166
5167 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005168 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169
5170 /* shrink other frames: current and at the left or at the right */
5171 if (left)
5172 fr = curfr; /* current frame gets smaller */
5173 else
5174 fr = curfr->fr_next; /* next frame gets smaller */
5175
5176 while (fr != NULL && offset > 0)
5177 {
5178 n = frame_minwidth(fr, NULL);
5179 if (fr->fr_width - offset <= n)
5180 {
5181 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005182 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 }
5184 else
5185 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005186 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 break;
5188 }
5189 if (left)
5190 fr = fr->fr_prev;
5191 else
5192 fr = fr->fr_next;
5193 }
5194 (void)win_comp_pos();
5195 redraw_all_later(NOT_VALID);
5196}
5197#endif /* FEAT_VERTSPLIT */
5198#endif /* FEAT_MOUSE */
5199
5200#endif /* FEAT_WINDOWS */
5201
5202/*
5203 * Set the height of a window.
5204 * This takes care of the things inside the window, not what happens to the
5205 * window position, the frame or to other windows.
5206 */
5207 static void
5208win_new_height(wp, height)
5209 win_T *wp;
5210 int height;
5211{
5212 linenr_T lnum;
5213 int sline, line_size;
5214#define FRACTION_MULT 16384L
5215
5216 /* Don't want a negative height. Happens when splitting a tiny window.
5217 * Will equalize heights soon to fix it. */
5218 if (height < 0)
5219 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005220 if (wp->w_height == height)
5221 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222
5223 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5224 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5225 + FRACTION_MULT / 2) / (long)wp->w_height;
5226
5227 wp->w_height = height;
5228 wp->w_skipcol = 0;
5229
5230 /* Don't change w_topline when height is zero. Don't set w_topline when
5231 * 'scrollbind' is set and this isn't the current window. */
5232 if (height > 0
5233#ifdef FEAT_SCROLLBIND
5234 && (!wp->w_p_scb || wp == curwin)
5235#endif
5236 )
5237 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005238 /*
5239 * Find a value for w_topline that shows the cursor at the same
5240 * relative position in the window as before (more or less).
5241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 lnum = wp->w_cursor.lnum;
5243 if (lnum < 1) /* can happen when starting up */
5244 lnum = 1;
5245 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5246 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5247 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005248
5249 if (sline >= 0)
5250 {
5251 /* Make sure the whole cursor line is visible, if possible. */
5252 int rows = plines_win(wp, lnum, FALSE);
5253
5254 if (sline > wp->w_height - rows)
5255 {
5256 sline = wp->w_height - rows;
5257 wp->w_wrow -= rows - line_size;
5258 }
5259 }
5260
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 if (sline < 0)
5262 {
5263 /*
5264 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005265 * Make cursor line the first line in the window. If not enough
5266 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 */
5268 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005269 if (wp->w_wrow >= wp->w_height
5270 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5271 {
5272 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5273 --wp->w_wrow;
5274 while (wp->w_wrow >= wp->w_height)
5275 {
5276 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5277 + win_col_off2(wp);
5278 --wp->w_wrow;
5279 }
5280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 }
5282 else
5283 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005284 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 {
5286#ifdef FEAT_FOLDING
5287 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5288 if (lnum == 1)
5289 {
5290 /* first line in buffer is folded */
5291 line_size = 1;
5292 --sline;
5293 break;
5294 }
5295#endif
5296 --lnum;
5297#ifdef FEAT_DIFF
5298 if (lnum == wp->w_topline)
5299 line_size = plines_win_nofill(wp, lnum, TRUE)
5300 + wp->w_topfill;
5301 else
5302#endif
5303 line_size = plines_win(wp, lnum, TRUE);
5304 sline -= line_size;
5305 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005306
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 if (sline < 0)
5308 {
5309 /*
5310 * Line we want at top would go off top of screen. Use next
5311 * line instead.
5312 */
5313#ifdef FEAT_FOLDING
5314 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5315#endif
5316 lnum++;
5317 wp->w_wrow -= line_size + sline;
5318 }
5319 else if (sline > 0)
5320 {
5321 /* First line of file reached, use that as topline. */
5322 lnum = 1;
5323 wp->w_wrow -= sline;
5324 }
5325 }
5326 set_topline(wp, lnum);
5327 }
5328
5329 if (wp == curwin)
5330 {
5331 if (p_so)
5332 update_topline();
5333 curs_columns(FALSE); /* validate w_wrow */
5334 }
5335 wp->w_prev_fraction_row = wp->w_wrow;
5336
5337 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005338 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339#ifdef FEAT_WINDOWS
5340 wp->w_redr_status = TRUE;
5341#endif
5342 invalidate_botline_win(wp);
5343}
5344
5345#ifdef FEAT_VERTSPLIT
5346/*
5347 * Set the width of a window.
5348 */
5349 static void
5350win_new_width(wp, width)
5351 win_T *wp;
5352 int width;
5353{
5354 wp->w_width = width;
5355 wp->w_lines_valid = 0;
5356 changed_line_abv_curs_win(wp);
5357 invalidate_botline_win(wp);
5358 if (wp == curwin)
5359 {
5360 update_topline();
5361 curs_columns(TRUE); /* validate w_wrow */
5362 }
5363 redraw_win_later(wp, NOT_VALID);
5364 wp->w_redr_status = TRUE;
5365}
5366#endif
5367
5368 void
5369win_comp_scroll(wp)
5370 win_T *wp;
5371{
5372 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5373 if (wp->w_p_scr == 0)
5374 wp->w_p_scr = 1;
5375}
5376
5377/*
5378 * command_height: called whenever p_ch has been changed
5379 */
5380 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005381command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382{
5383#ifdef FEAT_WINDOWS
5384 int h;
5385 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005386 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005388 /* Use the value of p_ch that we remembered. This is needed for when the
5389 * GUI starts up, we can't be sure in what order things happen. And when
5390 * p_ch was changed in another tab page. */
5391 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005392
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 /* Find bottom frame with width of screen. */
5394 frp = lastwin->w_frame;
5395# ifdef FEAT_VERTSPLIT
5396 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5397 frp = frp->fr_parent;
5398# endif
5399
5400 /* Avoid changing the height of a window with 'winfixheight' set. */
5401 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5402 && frp->fr_win->w_p_wfh)
5403 frp = frp->fr_prev;
5404
5405 if (starting != NO_SCREEN)
5406 {
5407 cmdline_row = Rows - p_ch;
5408
5409 if (p_ch > old_p_ch) /* p_ch got bigger */
5410 {
5411 while (p_ch > old_p_ch)
5412 {
5413 if (frp == NULL)
5414 {
5415 EMSG(_(e_noroom));
5416 p_ch = old_p_ch;
5417 cmdline_row = Rows - p_ch;
5418 break;
5419 }
5420 h = frp->fr_height - frame_minheight(frp, NULL);
5421 if (h > p_ch - old_p_ch)
5422 h = p_ch - old_p_ch;
5423 old_p_ch += h;
5424 frame_add_height(frp, -h);
5425 frp = frp->fr_prev;
5426 }
5427
5428 /* Recompute window positions. */
5429 (void)win_comp_pos();
5430
5431 /* clear the lines added to cmdline */
5432 if (full_screen)
5433 screen_fill((int)(cmdline_row), (int)Rows, 0,
5434 (int)Columns, ' ', ' ', 0);
5435 msg_row = cmdline_row;
5436 redraw_cmdline = TRUE;
5437 return;
5438 }
5439
5440 if (msg_row < cmdline_row)
5441 msg_row = cmdline_row;
5442 redraw_cmdline = TRUE;
5443 }
5444 frame_add_height(frp, (int)(old_p_ch - p_ch));
5445
5446 /* Recompute window positions. */
5447 if (frp != lastwin->w_frame)
5448 (void)win_comp_pos();
5449#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005451 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452#endif
5453}
5454
5455#if defined(FEAT_WINDOWS) || defined(PROTO)
5456/*
5457 * Resize frame "frp" to be "n" lines higher (negative for less high).
5458 * Also resize the frames it is contained in.
5459 */
5460 static void
5461frame_add_height(frp, n)
5462 frame_T *frp;
5463 int n;
5464{
5465 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5466 for (;;)
5467 {
5468 frp = frp->fr_parent;
5469 if (frp == NULL)
5470 break;
5471 frp->fr_height += n;
5472 }
5473}
5474
5475/*
5476 * Add or remove a status line for the bottom window(s), according to the
5477 * value of 'laststatus'.
5478 */
5479 void
5480last_status(morewin)
5481 int morewin; /* pretend there are two or more windows */
5482{
5483 /* Don't make a difference between horizontal or vertical split. */
5484 last_status_rec(topframe, (p_ls == 2
5485 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5486}
5487
5488 static void
5489last_status_rec(fr, statusline)
5490 frame_T *fr;
5491 int statusline;
5492{
5493 frame_T *fp;
5494 win_T *wp;
5495
5496 if (fr->fr_layout == FR_LEAF)
5497 {
5498 wp = fr->fr_win;
5499 if (wp->w_status_height != 0 && !statusline)
5500 {
5501 /* remove status line */
5502 win_new_height(wp, wp->w_height + 1);
5503 wp->w_status_height = 0;
5504 comp_col();
5505 }
5506 else if (wp->w_status_height == 0 && statusline)
5507 {
5508 /* Find a frame to take a line from. */
5509 fp = fr;
5510 while (fp->fr_height <= frame_minheight(fp, NULL))
5511 {
5512 if (fp == topframe)
5513 {
5514 EMSG(_(e_noroom));
5515 return;
5516 }
5517 /* In a column of frames: go to frame above. If already at
5518 * the top or in a row of frames: go to parent. */
5519 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5520 fp = fp->fr_prev;
5521 else
5522 fp = fp->fr_parent;
5523 }
5524 wp->w_status_height = 1;
5525 if (fp != fr)
5526 {
5527 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5528 frame_fix_height(wp);
5529 (void)win_comp_pos();
5530 }
5531 else
5532 win_new_height(wp, wp->w_height - 1);
5533 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005534 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 }
5536 }
5537#ifdef FEAT_VERTSPLIT
5538 else if (fr->fr_layout == FR_ROW)
5539 {
5540 /* vertically split windows, set status line for each one */
5541 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5542 last_status_rec(fp, statusline);
5543 }
5544#endif
5545 else
5546 {
5547 /* horizontally split window, set status line for last one */
5548 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5549 ;
5550 last_status_rec(fp, statusline);
5551 }
5552}
5553
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005554/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005555 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005556 */
5557 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005558tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005559{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005560#ifdef FEAT_GUI_TABLINE
5561 /* When the GUI has the tabline then this always returns zero. */
5562 if (gui_use_tabline())
5563 return 0;
5564#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005565 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005566 {
5567 case 0: return 0;
5568 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5569 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005570 return 1;
5571}
5572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573#endif /* FEAT_WINDOWS */
5574
5575#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5576/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005577 * Get the file name at the cursor.
5578 * If Visual mode is active, use the selected text if it's in one line.
5579 * Returns the name in allocated memory, NULL for failure.
5580 */
5581 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005582grab_file_name(count, file_lnum)
5583 long count;
5584 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005585{
5586# ifdef FEAT_VISUAL
5587 if (VIsual_active)
5588 {
5589 int len;
5590 char_u *ptr;
5591
5592 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5593 return NULL;
5594 return find_file_name_in_path(ptr, len,
5595 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5596 }
5597# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005598 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5599 file_lnum);
5600
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005601}
5602
5603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 * Return the file name under or after the cursor.
5605 *
5606 * The 'path' option is searched if the file name is not absolute.
5607 * The string returned has been alloc'ed and should be freed by the caller.
5608 * NULL is returned if the file name or file is not found.
5609 *
5610 * options:
5611 * FNAME_MESS give error messages
5612 * FNAME_EXP expand to path
5613 * FNAME_HYP check for hypertext link
5614 * FNAME_INCL apply "includeexpr"
5615 */
5616 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005617file_name_at_cursor(options, count, file_lnum)
5618 int options;
5619 long count;
5620 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621{
5622 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005623 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5624 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625}
5626
5627/*
5628 * Return the name of the file under or after ptr[col].
5629 * Otherwise like file_name_at_cursor().
5630 */
5631 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005632file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 char_u *line;
5634 int col;
5635 int options;
5636 long count;
5637 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005638 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
5640 char_u *ptr;
5641 int len;
5642
5643 /*
5644 * search forward for what could be the start of a file name
5645 */
5646 ptr = line + col;
5647 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005648 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 if (*ptr == NUL) /* nothing found */
5650 {
5651 if (options & FNAME_MESS)
5652 EMSG(_("E446: No file name under cursor"));
5653 return NULL;
5654 }
5655
5656 /*
5657 * Search backward for first char of the file name.
5658 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5659 */
5660 while (ptr > line)
5661 {
5662#ifdef FEAT_MBYTE
5663 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5664 ptr -= len + 1;
5665 else
5666#endif
5667 if (vim_isfilec(ptr[-1])
5668 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5669 --ptr;
5670 else
5671 break;
5672 }
5673
5674 /*
5675 * Search forward for the last char of the file name.
5676 * Also allow "://" when ':' is not in 'isfname'.
5677 */
5678 len = 0;
5679 while (vim_isfilec(ptr[len])
5680 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5681#ifdef FEAT_MBYTE
5682 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005683 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 else
5685#endif
5686 ++len;
5687
5688 /*
5689 * If there is trailing punctuation, remove it.
5690 * But don't remove "..", could be a directory name.
5691 */
5692 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5693 && ptr[len - 2] != '.')
5694 --len;
5695
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005696 if (file_lnum != NULL)
5697 {
5698 char_u *p;
5699
5700 /* Get the number after the file name and a separator character */
5701 p = ptr + len;
5702 p = skipwhite(p);
5703 if (*p != NUL)
5704 {
5705 if (!isdigit(*p))
5706 ++p; /* skip the separator */
5707 p = skipwhite(p);
5708 if (isdigit(*p))
5709 *file_lnum = (int)getdigits(&p);
5710 }
5711 }
5712
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5714}
5715
5716# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5717static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5718
5719 static char_u *
5720eval_includeexpr(ptr, len)
5721 char_u *ptr;
5722 int len;
5723{
5724 char_u *res;
5725
5726 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005727 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005728 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 set_vim_var_string(VV_FNAME, NULL, 0);
5730 return res;
5731}
5732#endif
5733
5734/*
5735 * Return the name of the file ptr[len] in 'path'.
5736 * Otherwise like file_name_at_cursor().
5737 */
5738 char_u *
5739find_file_name_in_path(ptr, len, options, count, rel_fname)
5740 char_u *ptr;
5741 int len;
5742 int options;
5743 long count;
5744 char_u *rel_fname; /* file we are searching relative to */
5745{
5746 char_u *file_name;
5747 int c;
5748# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5749 char_u *tofree = NULL;
5750
5751 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5752 {
5753 tofree = eval_includeexpr(ptr, len);
5754 if (tofree != NULL)
5755 {
5756 ptr = tofree;
5757 len = (int)STRLEN(ptr);
5758 }
5759 }
5760# endif
5761
5762 if (options & FNAME_EXP)
5763 {
5764 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5765 TRUE, rel_fname);
5766
5767# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5768 /*
5769 * If the file could not be found in a normal way, try applying
5770 * 'includeexpr' (unless done already).
5771 */
5772 if (file_name == NULL
5773 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5774 {
5775 tofree = eval_includeexpr(ptr, len);
5776 if (tofree != NULL)
5777 {
5778 ptr = tofree;
5779 len = (int)STRLEN(ptr);
5780 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5781 TRUE, rel_fname);
5782 }
5783 }
5784# endif
5785 if (file_name == NULL && (options & FNAME_MESS))
5786 {
5787 c = ptr[len];
5788 ptr[len] = NUL;
5789 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5790 ptr[len] = c;
5791 }
5792
5793 /* Repeat finding the file "count" times. This matters when it
5794 * appears several times in the path. */
5795 while (file_name != NULL && --count > 0)
5796 {
5797 vim_free(file_name);
5798 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5799 }
5800 }
5801 else
5802 file_name = vim_strnsave(ptr, len);
5803
5804# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5805 vim_free(tofree);
5806# endif
5807
5808 return file_name;
5809}
5810#endif /* FEAT_SEARCHPATH */
5811
5812/*
5813 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5814 * Also check for ":\\", which MS Internet Explorer accepts, return
5815 * URL_BACKSLASH.
5816 */
5817 static int
5818path_is_url(p)
5819 char_u *p;
5820{
5821 if (STRNCMP(p, "://", (size_t)3) == 0)
5822 return URL_SLASH;
5823 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5824 return URL_BACKSLASH;
5825 return 0;
5826}
5827
5828/*
5829 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5830 * Return URL_BACKSLASH for "name:\\".
5831 * Return zero otherwise.
5832 */
5833 int
5834path_with_url(fname)
5835 char_u *fname;
5836{
5837 char_u *p;
5838
5839 for (p = fname; isalpha(*p); ++p)
5840 ;
5841 return path_is_url(p);
5842}
5843
5844/*
5845 * Return TRUE if "name" is a full (absolute) path name or URL.
5846 */
5847 int
5848vim_isAbsName(name)
5849 char_u *name;
5850{
5851 return (path_with_url(name) != 0 || mch_isFullName(name));
5852}
5853
5854/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005855 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 *
5857 * return FAIL for failure, OK otherwise
5858 */
5859 int
5860vim_FullName(fname, buf, len, force)
5861 char_u *fname, *buf;
5862 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005863 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864{
5865 int retval = OK;
5866 int url;
5867
5868 *buf = NUL;
5869 if (fname == NULL)
5870 return FAIL;
5871
5872 url = path_with_url(fname);
5873 if (!url)
5874 retval = mch_FullName(fname, buf, len, force);
5875 if (url || retval == FAIL)
5876 {
5877 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005878 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 }
5880#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5881 slash_adjust(buf);
5882#endif
5883 return retval;
5884}
5885
5886/*
5887 * Return the minimal number of rows that is needed on the screen to display
5888 * the current number of windows.
5889 */
5890 int
5891min_rows()
5892{
5893 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005894#ifdef FEAT_WINDOWS
5895 tabpage_T *tp;
5896 int n;
5897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898
5899 if (firstwin == NULL) /* not initialized yet */
5900 return MIN_LINES;
5901
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005903 total = 0;
5904 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5905 {
5906 n = frame_minheight(tp->tp_topframe, NULL);
5907 if (total < n)
5908 total = n;
5909 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005910 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005912 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005914 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 return total;
5916}
5917
5918/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005919 * Return TRUE if there is only one window (in the current tab page), not
5920 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 */
5922 int
5923only_one_window()
5924{
5925#ifdef FEAT_WINDOWS
5926 int count = 0;
5927 win_T *wp;
5928
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005929 /* If there is another tab page there always is another window. */
5930 if (first_tabpage->tp_next != NULL)
5931 return FALSE;
5932
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005934 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935# ifdef FEAT_QUICKFIX
5936 || wp->w_p_pvw
5937# endif
5938 ) || wp == curwin)
5939 ++count;
5940 return (count <= 1);
5941#else
5942 return TRUE;
5943#endif
5944}
5945
5946#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5947/*
5948 * Correct the cursor line number in other windows. Used after changing the
5949 * current buffer, and before applying autocommands.
5950 * When "do_curwin" is TRUE, also check current window.
5951 */
5952 void
5953check_lnums(do_curwin)
5954 int do_curwin;
5955{
5956 win_T *wp;
5957
5958#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005959 tabpage_T *tp;
5960
5961 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5963#else
5964 wp = curwin;
5965 if (do_curwin)
5966#endif
5967 {
5968 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5969 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5970 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5971 wp->w_topline = curbuf->b_ml.ml_line_count;
5972 }
5973}
5974#endif
5975
5976#if defined(FEAT_WINDOWS) || defined(PROTO)
5977
5978/*
5979 * A snapshot of the window sizes, to restore them after closing the help
5980 * window.
5981 * Only these fields are used:
5982 * fr_layout
5983 * fr_width
5984 * fr_height
5985 * fr_next
5986 * fr_child
5987 * fr_win (only valid for the old curwin, NULL otherwise)
5988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989
5990/*
5991 * Create a snapshot of the current frame sizes.
5992 */
5993 static void
5994make_snapshot()
5995{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005996 clear_snapshot(curtab);
5997 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998}
5999
6000 static void
6001make_snapshot_rec(fr, frp)
6002 frame_T *fr;
6003 frame_T **frp;
6004{
6005 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6006 if (*frp == NULL)
6007 return;
6008 (*frp)->fr_layout = fr->fr_layout;
6009# ifdef FEAT_VERTSPLIT
6010 (*frp)->fr_width = fr->fr_width;
6011# endif
6012 (*frp)->fr_height = fr->fr_height;
6013 if (fr->fr_next != NULL)
6014 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6015 if (fr->fr_child != NULL)
6016 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6017 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6018 (*frp)->fr_win = curwin;
6019}
6020
6021/*
6022 * Remove any existing snapshot.
6023 */
6024 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006025clear_snapshot(tp)
6026 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006028 clear_snapshot_rec(tp->tp_snapshot);
6029 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030}
6031
6032 static void
6033clear_snapshot_rec(fr)
6034 frame_T *fr;
6035{
6036 if (fr != NULL)
6037 {
6038 clear_snapshot_rec(fr->fr_next);
6039 clear_snapshot_rec(fr->fr_child);
6040 vim_free(fr);
6041 }
6042}
6043
6044/*
6045 * Restore a previously created snapshot, if there is any.
6046 * This is only done if the screen size didn't change and the window layout is
6047 * still the same.
6048 */
6049 static void
6050restore_snapshot(close_curwin)
6051 int close_curwin; /* closing current window */
6052{
6053 win_T *wp;
6054
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006055 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006057 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006059 && curtab->tp_snapshot->fr_height == topframe->fr_height
6060 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006062 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 win_comp_pos();
6064 if (wp != NULL && close_curwin)
6065 win_goto(wp);
6066 redraw_all_later(CLEAR);
6067 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006068 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069}
6070
6071/*
6072 * Check if frames "sn" and "fr" have the same layout, same following frames
6073 * and same children.
6074 */
6075 static int
6076check_snapshot_rec(sn, fr)
6077 frame_T *sn;
6078 frame_T *fr;
6079{
6080 if (sn->fr_layout != fr->fr_layout
6081 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6082 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6083 || (sn->fr_next != NULL
6084 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6085 || (sn->fr_child != NULL
6086 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6087 return FAIL;
6088 return OK;
6089}
6090
6091/*
6092 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6093 * following frames and children.
6094 * Returns a pointer to the old current window, or NULL.
6095 */
6096 static win_T *
6097restore_snapshot_rec(sn, fr)
6098 frame_T *sn;
6099 frame_T *fr;
6100{
6101 win_T *wp = NULL;
6102 win_T *wp2;
6103
6104 fr->fr_height = sn->fr_height;
6105# ifdef FEAT_VERTSPLIT
6106 fr->fr_width = sn->fr_width;
6107# endif
6108 if (fr->fr_layout == FR_LEAF)
6109 {
6110 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6111# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006112 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113# endif
6114 wp = sn->fr_win;
6115 }
6116 if (sn->fr_next != NULL)
6117 {
6118 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6119 if (wp2 != NULL)
6120 wp = wp2;
6121 }
6122 if (sn->fr_child != NULL)
6123 {
6124 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6125 if (wp2 != NULL)
6126 wp = wp2;
6127 }
6128 return wp;
6129}
6130
6131#endif
6132
6133#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6134/*
6135 * Return TRUE if there is any vertically split window.
6136 */
6137 int
6138win_hasvertsplit()
6139{
6140 frame_T *fr;
6141
6142 if (topframe->fr_layout == FR_ROW)
6143 return TRUE;
6144
6145 if (topframe->fr_layout == FR_COL)
6146 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6147 if (fr->fr_layout == FR_ROW)
6148 return TRUE;
6149
6150 return FALSE;
6151}
6152#endif