blob: aa4d26a0bb7cd988f357200ae9d23718b8da7259 [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
2087 /*
2088 * Close the link to the buffer.
2089 */
2090 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002091
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002093 * other window or moved to another tab page. */
2094 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 return;
2096
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002097 /* Free the memory used for the window. */
2098 wp = win_free_mem(win, &dir, NULL);
2099
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 /* Make sure curwin isn't invalid. It can cause severe trouble when
2101 * printing an error message. For win_equal() curbuf needs to be valid
2102 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002103 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
2105 curwin = wp;
2106#ifdef FEAT_QUICKFIX
2107 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2108 {
2109 /*
2110 * The cursor goes to the preview or the quickfix window, try
2111 * finding another window to go to.
2112 */
2113 for (;;)
2114 {
2115 if (wp->w_next == NULL)
2116 wp = firstwin;
2117 else
2118 wp = wp->w_next;
2119 if (wp == curwin)
2120 break;
2121 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2122 {
2123 curwin = wp;
2124 break;
2125 }
2126 }
2127 }
2128#endif
2129 curbuf = curwin->w_buffer;
2130 close_curwin = TRUE;
2131 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002132 if (p_ea
2133#ifdef FEAT_VERTSPLIT
2134 && (*p_ead == 'b' || *p_ead == dir)
2135#endif
2136 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 win_equal(curwin, TRUE,
2138#ifdef FEAT_VERTSPLIT
2139 dir
2140#else
2141 0
2142#endif
2143 );
2144 else
2145 win_comp_pos();
2146 if (close_curwin)
2147 {
2148 win_enter_ext(wp, FALSE, TRUE);
2149#ifdef FEAT_AUTOCMD
2150 if (other_buffer)
2151 /* careful: after this wp and win may be invalid! */
2152 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2153#endif
2154 }
2155
2156 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002157 * If last window has a status line now and we don't want one,
2158 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 */
2160 last_status(FALSE);
2161
2162 /* After closing the help window, try restoring the window layout from
2163 * before it was opened. */
2164 if (help_window)
2165 restore_snapshot(close_curwin);
2166
2167#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2168 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2169 if (gui.in_use && !win_hasvertsplit())
2170 gui_init_which_components(NULL);
2171#endif
2172
2173 redraw_all_later(NOT_VALID);
2174}
2175
2176/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002177 * Close window "win" in tab page "tp", which is not the current tab page.
2178 * This may be the last window ih that tab page and result in closing the tab,
2179 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002180 * Caller must check if buffer is hidden and whether the tabline needs to be
2181 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002182 */
2183 void
2184win_close_othertab(win, free_buf, tp)
2185 win_T *win;
2186 int free_buf;
2187 tabpage_T *tp;
2188{
2189 win_T *wp;
2190 int dir;
2191 tabpage_T *ptp = NULL;
2192
2193 /* Close the link to the buffer. */
2194 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2195
2196 /* Careful: Autocommands may have closed the tab page or made it the
2197 * current tab page. */
2198 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2199 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002200 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002201 return;
2202
2203 /* Autocommands may have closed the window already. */
2204 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2205 ;
2206 if (wp == NULL)
2207 return;
2208
2209 /* Free the memory used for the window. */
2210 wp = win_free_mem(win, &dir, tp);
2211
2212 /* When closing the last window in a tab page remove the tab page. */
2213 if (wp == NULL)
2214 {
2215 if (tp == first_tabpage)
2216 first_tabpage = tp->tp_next;
2217 else
2218 {
2219 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2220 ptp = ptp->tp_next)
2221 ;
2222 if (ptp == NULL)
2223 {
2224 EMSG2(_(e_intern2), "win_close_othertab()");
2225 return;
2226 }
2227 ptp->tp_next = tp->tp_next;
2228 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002229 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002230 }
2231}
2232
2233/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002234 * Free the memory used for a window.
2235 * Returns a pointer to the window that got the freed up space.
2236 */
2237 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002238win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002239 win_T *win;
2240 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002241 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002242{
2243 frame_T *frp;
2244 win_T *wp;
2245
Bram Moolenaarea408852005-06-25 22:49:46 +00002246#ifdef FEAT_FOLDING
2247 clearFolding(win);
2248#endif
2249
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002250 /* reduce the reference count to the argument list. */
2251 alist_unlink(win->w_alist);
2252
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002253 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002254 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002255 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002256 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002257 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002258
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002259 /* When deleting the current window of another tab page select a new
2260 * current window. */
2261 if (tp != NULL && win == tp->tp_curwin)
2262 tp->tp_curwin = wp;
2263
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002264 return wp;
2265}
2266
2267#if defined(EXITFREE) || defined(PROTO)
2268 void
2269win_free_all()
2270{
2271 int dummy;
2272
Bram Moolenaarf740b292006-02-16 22:11:02 +00002273# ifdef FEAT_WINDOWS
2274 while (first_tabpage->tp_next != NULL)
2275 tabpage_close(TRUE);
2276# endif
2277
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002278 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002279 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002280}
2281#endif
2282
2283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 * Remove a window and its frame from the tree of frames.
2285 * Returns a pointer to the window that got the freed up space.
2286 */
2287/*ARGSUSED*/
2288 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002289winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 win_T *win;
2291 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002292 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293{
2294 frame_T *frp, *frp2, *frp3;
2295 frame_T *frp_close = win->w_frame;
2296 win_T *wp;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002297 int old_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
2299 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002300 * If there is only one window there is nothing to remove.
2301 */
2302 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2303 return NULL;
2304
2305 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 * Remove the window from its frame.
2307 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002308 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 wp = frame2win(frp2);
2310
2311 /* Remove this frame from the list of frames. */
2312 frame_remove(frp_close);
2313
2314#ifdef FEAT_VERTSPLIT
2315 if (frp_close->fr_parent->fr_layout == FR_COL)
2316 {
2317#endif
2318 /* When 'winfixheight' is set, remember its old size and restore
2319 * it later (it's a simplistic solution...). Don't do this if the
2320 * window will occupy the full height of the screen. */
2321 if (frp2->fr_win != NULL
2322 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2323 && frp2->fr_win->w_p_wfh)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002324 old_size = frp2->fr_win->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2326 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002327 if (old_size != 0)
2328 win_setheight_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329#ifdef FEAT_VERTSPLIT
2330 *dirp = 'v';
2331 }
2332 else
2333 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002334 /* When 'winfixwidth' is set, remember its old size and restore
2335 * it later (it's a simplistic solution...). Don't do this if the
2336 * window will occupy the full width of the screen. */
2337 if (frp2->fr_win != NULL
2338 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2339 && frp2->fr_win->w_p_wfw)
2340 old_size = frp2->fr_win->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002342 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2343 if (old_size != 0)
2344 win_setwidth_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 *dirp = 'h';
2346 }
2347#endif
2348
2349 /* If rows/columns go to a window below/right its positions need to be
2350 * updated. Can only be done after the sizes have been updated. */
2351 if (frp2 == frp_close->fr_next)
2352 {
2353 int row = win->w_winrow;
2354 int col = W_WINCOL(win);
2355
2356 frame_comp_pos(frp2, &row, &col);
2357 }
2358
2359 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2360 {
2361 /* There is no other frame in this list, move its info to the parent
2362 * and remove it. */
2363 frp2->fr_parent->fr_layout = frp2->fr_layout;
2364 frp2->fr_parent->fr_child = frp2->fr_child;
2365 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2366 frp->fr_parent = frp2->fr_parent;
2367 frp2->fr_parent->fr_win = frp2->fr_win;
2368 if (frp2->fr_win != NULL)
2369 frp2->fr_win->w_frame = frp2->fr_parent;
2370 frp = frp2->fr_parent;
2371 vim_free(frp2);
2372
2373 frp2 = frp->fr_parent;
2374 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2375 {
2376 /* The frame above the parent has the same layout, have to merge
2377 * the frames into this list. */
2378 if (frp2->fr_child == frp)
2379 frp2->fr_child = frp->fr_child;
2380 frp->fr_child->fr_prev = frp->fr_prev;
2381 if (frp->fr_prev != NULL)
2382 frp->fr_prev->fr_next = frp->fr_child;
2383 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2384 {
2385 frp3->fr_parent = frp2;
2386 if (frp3->fr_next == NULL)
2387 {
2388 frp3->fr_next = frp->fr_next;
2389 if (frp->fr_next != NULL)
2390 frp->fr_next->fr_prev = frp3;
2391 break;
2392 }
2393 }
2394 vim_free(frp);
2395 }
2396 }
2397
2398 return wp;
2399}
2400
2401/*
2402 * Find out which frame is going to get the freed up space when "win" is
2403 * closed.
2404 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2405 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2406 * This makes opening a window and closing it immediately keep the same window
2407 * layout.
2408 */
2409 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002410win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002412 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
2414 frame_T *frp;
2415 int b;
2416
Bram Moolenaarf740b292006-02-16 22:11:02 +00002417 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002418 /* Last window in this tab page, will go to next tab page. */
2419 return alt_tabpage()->tp_curwin->w_frame;
2420
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 frp = win->w_frame;
2422#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002423 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 b = p_spr;
2425 else
2426#endif
2427 b = p_sb;
2428 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2429 return frp->fr_next;
2430 return frp->fr_prev;
2431}
2432
2433/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002434 * Return the tabpage that will be used if the current one is closed.
2435 */
2436 static tabpage_T *
2437alt_tabpage()
2438{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002439 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002440
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002441 /* Use the next tab page if possible. */
2442 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002443 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002444
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002445 /* Find the last but one tab page. */
2446 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2447 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002448 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002449}
2450
2451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 * Find the left-upper window in frame "frp".
2453 */
2454 static win_T *
2455frame2win(frp)
2456 frame_T *frp;
2457{
2458 while (frp->fr_win == NULL)
2459 frp = frp->fr_child;
2460 return frp->fr_win;
2461}
2462
2463/*
2464 * Return TRUE if frame "frp" contains window "wp".
2465 */
2466 static int
2467frame_has_win(frp, wp)
2468 frame_T *frp;
2469 win_T *wp;
2470{
2471 frame_T *p;
2472
2473 if (frp->fr_layout == FR_LEAF)
2474 return frp->fr_win == wp;
2475
2476 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2477 if (frame_has_win(p, wp))
2478 return TRUE;
2479 return FALSE;
2480}
2481
2482/*
2483 * Set a new height for a frame. Recursively sets the height for contained
2484 * frames and windows. Caller must take care of positions.
2485 */
2486 static void
2487frame_new_height(topfrp, height, topfirst, wfh)
2488 frame_T *topfrp;
2489 int height;
2490 int topfirst; /* resize topmost contained frame first */
2491 int wfh; /* obey 'winfixheight' when there is a choice;
2492 may cause the height not to be set */
2493{
2494 frame_T *frp;
2495 int extra_lines;
2496 int h;
2497
2498 if (topfrp->fr_win != NULL)
2499 {
2500 /* Simple case: just one window. */
2501 win_new_height(topfrp->fr_win,
2502 height - topfrp->fr_win->w_status_height);
2503 }
2504#ifdef FEAT_VERTSPLIT
2505 else if (topfrp->fr_layout == FR_ROW)
2506 {
2507 do
2508 {
2509 /* All frames in this row get the same new height. */
2510 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2511 {
2512 frame_new_height(frp, height, topfirst, wfh);
2513 if (frp->fr_height > height)
2514 {
2515 /* Could not fit the windows, make the whole row higher. */
2516 height = frp->fr_height;
2517 break;
2518 }
2519 }
2520 }
2521 while (frp != NULL);
2522 }
2523#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002524 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {
2526 /* Complicated case: Resize a column of frames. Resize the bottom
2527 * frame first, frames above that when needed. */
2528
2529 frp = topfrp->fr_child;
2530 if (wfh)
2531 /* Advance past frames with one window with 'wfh' set. */
2532 while (frame_fixed_height(frp))
2533 {
2534 frp = frp->fr_next;
2535 if (frp == NULL)
2536 return; /* no frame without 'wfh', give up */
2537 }
2538 if (!topfirst)
2539 {
2540 /* Find the bottom frame of this column */
2541 while (frp->fr_next != NULL)
2542 frp = frp->fr_next;
2543 if (wfh)
2544 /* Advance back for frames with one window with 'wfh' set. */
2545 while (frame_fixed_height(frp))
2546 frp = frp->fr_prev;
2547 }
2548
2549 extra_lines = height - topfrp->fr_height;
2550 if (extra_lines < 0)
2551 {
2552 /* reduce height of contained frames, bottom or top frame first */
2553 while (frp != NULL)
2554 {
2555 h = frame_minheight(frp, NULL);
2556 if (frp->fr_height + extra_lines < h)
2557 {
2558 extra_lines += frp->fr_height - h;
2559 frame_new_height(frp, h, topfirst, wfh);
2560 }
2561 else
2562 {
2563 frame_new_height(frp, frp->fr_height + extra_lines,
2564 topfirst, wfh);
2565 break;
2566 }
2567 if (topfirst)
2568 {
2569 do
2570 frp = frp->fr_next;
2571 while (wfh && frp != NULL && frame_fixed_height(frp));
2572 }
2573 else
2574 {
2575 do
2576 frp = frp->fr_prev;
2577 while (wfh && frp != NULL && frame_fixed_height(frp));
2578 }
2579 /* Increase "height" if we could not reduce enough frames. */
2580 if (frp == NULL)
2581 height -= extra_lines;
2582 }
2583 }
2584 else if (extra_lines > 0)
2585 {
2586 /* increase height of bottom or top frame */
2587 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2588 }
2589 }
2590 topfrp->fr_height = height;
2591}
2592
2593/*
2594 * Return TRUE if height of frame "frp" should not be changed because of
2595 * the 'winfixheight' option.
2596 */
2597 static int
2598frame_fixed_height(frp)
2599 frame_T *frp;
2600{
2601 /* frame with one window: fixed height if 'winfixheight' set. */
2602 if (frp->fr_win != NULL)
2603 return frp->fr_win->w_p_wfh;
2604
2605 if (frp->fr_layout == FR_ROW)
2606 {
2607 /* The frame is fixed height if one of the frames in the row is fixed
2608 * height. */
2609 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2610 if (frame_fixed_height(frp))
2611 return TRUE;
2612 return FALSE;
2613 }
2614
2615 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2616 * frames in the row are fixed height. */
2617 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2618 if (!frame_fixed_height(frp))
2619 return FALSE;
2620 return TRUE;
2621}
2622
2623#ifdef FEAT_VERTSPLIT
2624/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002625 * Return TRUE if width of frame "frp" should not be changed because of
2626 * the 'winfixwidth' option.
2627 */
2628 static int
2629frame_fixed_width(frp)
2630 frame_T *frp;
2631{
2632 /* frame with one window: fixed width if 'winfixwidth' set. */
2633 if (frp->fr_win != NULL)
2634 return frp->fr_win->w_p_wfw;
2635
2636 if (frp->fr_layout == FR_COL)
2637 {
2638 /* The frame is fixed width if one of the frames in the row is fixed
2639 * width. */
2640 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2641 if (frame_fixed_width(frp))
2642 return TRUE;
2643 return FALSE;
2644 }
2645
2646 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2647 * frames in the row are fixed width. */
2648 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2649 if (!frame_fixed_width(frp))
2650 return FALSE;
2651 return TRUE;
2652}
2653
2654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 * Add a status line to windows at the bottom of "frp".
2656 * Note: Does not check if there is room!
2657 */
2658 static void
2659frame_add_statusline(frp)
2660 frame_T *frp;
2661{
2662 win_T *wp;
2663
2664 if (frp->fr_layout == FR_LEAF)
2665 {
2666 wp = frp->fr_win;
2667 if (wp->w_status_height == 0)
2668 {
2669 if (wp->w_height > 0) /* don't make it negative */
2670 --wp->w_height;
2671 wp->w_status_height = STATUS_HEIGHT;
2672 }
2673 }
2674 else if (frp->fr_layout == FR_ROW)
2675 {
2676 /* Handle all the frames in the row. */
2677 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2678 frame_add_statusline(frp);
2679 }
2680 else /* frp->fr_layout == FR_COL */
2681 {
2682 /* Only need to handle the last frame in the column. */
2683 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2684 ;
2685 frame_add_statusline(frp);
2686 }
2687}
2688
2689/*
2690 * Set width of a frame. Handles recursively going through contained frames.
2691 * May remove separator line for windows at the right side (for win_close()).
2692 */
2693 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002694frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 frame_T *topfrp;
2696 int width;
2697 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002698 int wfw; /* obey 'winfixwidth' when there is a choice;
2699 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700{
2701 frame_T *frp;
2702 int extra_cols;
2703 int w;
2704 win_T *wp;
2705
2706 if (topfrp->fr_layout == FR_LEAF)
2707 {
2708 /* Simple case: just one window. */
2709 wp = topfrp->fr_win;
2710 /* Find out if there are any windows right of this one. */
2711 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2712 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2713 break;
2714 if (frp->fr_parent == NULL)
2715 wp->w_vsep_width = 0;
2716 win_new_width(wp, width - wp->w_vsep_width);
2717 }
2718 else if (topfrp->fr_layout == FR_COL)
2719 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002720 do
2721 {
2722 /* All frames in this column get the same new width. */
2723 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2724 {
2725 frame_new_width(frp, width, leftfirst, wfw);
2726 if (frp->fr_width > width)
2727 {
2728 /* Could not fit the windows, make whole column wider. */
2729 width = frp->fr_width;
2730 break;
2731 }
2732 }
2733 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 }
2735 else /* fr_layout == FR_ROW */
2736 {
2737 /* Complicated case: Resize a row of frames. Resize the rightmost
2738 * frame first, frames left of it when needed. */
2739
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002741 if (wfw)
2742 /* Advance past frames with one window with 'wfw' set. */
2743 while (frame_fixed_width(frp))
2744 {
2745 frp = frp->fr_next;
2746 if (frp == NULL)
2747 return; /* no frame without 'wfw', give up */
2748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002750 {
2751 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 while (frp->fr_next != NULL)
2753 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002754 if (wfw)
2755 /* Advance back for frames with one window with 'wfw' set. */
2756 while (frame_fixed_width(frp))
2757 frp = frp->fr_prev;
2758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
2760 extra_cols = width - topfrp->fr_width;
2761 if (extra_cols < 0)
2762 {
2763 /* reduce frame width, rightmost frame first */
2764 while (frp != NULL)
2765 {
2766 w = frame_minwidth(frp, NULL);
2767 if (frp->fr_width + extra_cols < w)
2768 {
2769 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002770 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 }
2772 else
2773 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002774 frame_new_width(frp, frp->fr_width + extra_cols,
2775 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 break;
2777 }
2778 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002779 {
2780 do
2781 frp = frp->fr_next;
2782 while (wfw && frp != NULL && frame_fixed_width(frp));
2783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002785 {
2786 do
2787 frp = frp->fr_prev;
2788 while (wfw && frp != NULL && frame_fixed_width(frp));
2789 }
2790 /* Increase "width" if we could not reduce enough frames. */
2791 if (frp == NULL)
2792 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
2794 }
2795 else if (extra_cols > 0)
2796 {
2797 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002798 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 }
2800 }
2801 topfrp->fr_width = width;
2802}
2803
2804/*
2805 * Add the vertical separator to windows at the right side of "frp".
2806 * Note: Does not check if there is room!
2807 */
2808 static void
2809frame_add_vsep(frp)
2810 frame_T *frp;
2811{
2812 win_T *wp;
2813
2814 if (frp->fr_layout == FR_LEAF)
2815 {
2816 wp = frp->fr_win;
2817 if (wp->w_vsep_width == 0)
2818 {
2819 if (wp->w_width > 0) /* don't make it negative */
2820 --wp->w_width;
2821 wp->w_vsep_width = 1;
2822 }
2823 }
2824 else if (frp->fr_layout == FR_COL)
2825 {
2826 /* Handle all the frames in the column. */
2827 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2828 frame_add_vsep(frp);
2829 }
2830 else /* frp->fr_layout == FR_ROW */
2831 {
2832 /* Only need to handle the last frame in the row. */
2833 frp = frp->fr_child;
2834 while (frp->fr_next != NULL)
2835 frp = frp->fr_next;
2836 frame_add_vsep(frp);
2837 }
2838}
2839
2840/*
2841 * Set frame width from the window it contains.
2842 */
2843 static void
2844frame_fix_width(wp)
2845 win_T *wp;
2846{
2847 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2848}
2849#endif
2850
2851/*
2852 * Set frame height from the window it contains.
2853 */
2854 static void
2855frame_fix_height(wp)
2856 win_T *wp;
2857{
2858 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2859}
2860
2861/*
2862 * Compute the minimal height for frame "topfrp".
2863 * Uses the 'winminheight' option.
2864 * When "next_curwin" isn't NULL, use p_wh for this window.
2865 * When "next_curwin" is NOWIN, don't use at least one line for the current
2866 * window.
2867 */
2868 static int
2869frame_minheight(topfrp, next_curwin)
2870 frame_T *topfrp;
2871 win_T *next_curwin;
2872{
2873 frame_T *frp;
2874 int m;
2875#ifdef FEAT_VERTSPLIT
2876 int n;
2877#endif
2878
2879 if (topfrp->fr_win != NULL)
2880 {
2881 if (topfrp->fr_win == next_curwin)
2882 m = p_wh + topfrp->fr_win->w_status_height;
2883 else
2884 {
2885 /* window: minimal height of the window plus status line */
2886 m = p_wmh + topfrp->fr_win->w_status_height;
2887 /* Current window is minimal one line high */
2888 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2889 ++m;
2890 }
2891 }
2892#ifdef FEAT_VERTSPLIT
2893 else if (topfrp->fr_layout == FR_ROW)
2894 {
2895 /* get the minimal height from each frame in this row */
2896 m = 0;
2897 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2898 {
2899 n = frame_minheight(frp, next_curwin);
2900 if (n > m)
2901 m = n;
2902 }
2903 }
2904#endif
2905 else
2906 {
2907 /* Add up the minimal heights for all frames in this column. */
2908 m = 0;
2909 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2910 m += frame_minheight(frp, next_curwin);
2911 }
2912
2913 return m;
2914}
2915
2916#ifdef FEAT_VERTSPLIT
2917/*
2918 * Compute the minimal width for frame "topfrp".
2919 * When "next_curwin" isn't NULL, use p_wiw for this window.
2920 * When "next_curwin" is NOWIN, don't use at least one column for the current
2921 * window.
2922 */
2923 static int
2924frame_minwidth(topfrp, next_curwin)
2925 frame_T *topfrp;
2926 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2927{
2928 frame_T *frp;
2929 int m, n;
2930
2931 if (topfrp->fr_win != NULL)
2932 {
2933 if (topfrp->fr_win == next_curwin)
2934 m = p_wiw + topfrp->fr_win->w_vsep_width;
2935 else
2936 {
2937 /* window: minimal width of the window plus separator column */
2938 m = p_wmw + topfrp->fr_win->w_vsep_width;
2939 /* Current window is minimal one column wide */
2940 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2941 ++m;
2942 }
2943 }
2944 else if (topfrp->fr_layout == FR_COL)
2945 {
2946 /* get the minimal width from each frame in this column */
2947 m = 0;
2948 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2949 {
2950 n = frame_minwidth(frp, next_curwin);
2951 if (n > m)
2952 m = n;
2953 }
2954 }
2955 else
2956 {
2957 /* Add up the minimal widths for all frames in this row. */
2958 m = 0;
2959 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2960 m += frame_minwidth(frp, next_curwin);
2961 }
2962
2963 return m;
2964}
2965#endif
2966
2967
2968/*
2969 * Try to close all windows except current one.
2970 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2971 * used and the buffer was modified.
2972 *
2973 * Used by ":bdel" and ":only".
2974 */
2975 void
2976close_others(message, forceit)
2977 int message;
2978 int forceit; /* always hide all other windows */
2979{
2980 win_T *wp;
2981 win_T *nextwp;
2982 int r;
2983
2984 if (lastwin == firstwin)
2985 {
2986 if (message
2987#ifdef FEAT_AUTOCMD
2988 && !autocmd_busy
2989#endif
2990 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00002991 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 return;
2993 }
2994
2995 /* Be very careful here: autocommands may change the window layout. */
2996 for (wp = firstwin; win_valid(wp); wp = nextwp)
2997 {
2998 nextwp = wp->w_next;
2999 if (wp != curwin) /* don't close current window */
3000 {
3001
3002 /* Check if it's allowed to abandon this window */
3003 r = can_abandon(wp->w_buffer, forceit);
3004#ifdef FEAT_AUTOCMD
3005 if (!win_valid(wp)) /* autocommands messed wp up */
3006 {
3007 nextwp = firstwin;
3008 continue;
3009 }
3010#endif
3011 if (!r)
3012 {
3013#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3014 if (message && (p_confirm || cmdmod.confirm) && p_write)
3015 {
3016 dialog_changed(wp->w_buffer, FALSE);
3017# ifdef FEAT_AUTOCMD
3018 if (!win_valid(wp)) /* autocommands messed wp up */
3019 {
3020 nextwp = firstwin;
3021 continue;
3022 }
3023# endif
3024 }
3025 if (bufIsChanged(wp->w_buffer))
3026#endif
3027 continue;
3028 }
3029 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3030 }
3031 }
3032
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003033 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 EMSG(_("E445: Other window contains changes"));
3035}
3036
3037#endif /* FEAT_WINDOWS */
3038
3039/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003040 * Init the current window "curwin".
3041 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 */
3043 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003044curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003046 redraw_win_later(curwin, NOT_VALID);
3047 curwin->w_lines_valid = 0;
3048 curwin->w_cursor.lnum = 1;
3049 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003051 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003053 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3054 curwin->w_pcmark.col = 0;
3055 curwin->w_prev_pcmark.lnum = 0;
3056 curwin->w_prev_pcmark.col = 0;
3057 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003059 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003061 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062#ifdef FEAT_FKMAP
3063 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003064 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003066 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067#endif
3068}
3069
3070/*
3071 * Allocate the first window and put an empty buffer in it.
3072 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003073 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003075 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076win_alloc_first()
3077{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003078 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003079 return FAIL;
3080
3081#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003082 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003083 if (first_tabpage == NULL)
3084 return FAIL;
3085 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003086 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003087#endif
3088 return OK;
3089}
3090
3091/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003092 * Allocate the first window or the first window in a new tab page.
3093 * When "oldwin" is NULL create an empty buffer for it.
3094 * When "oldwin" is not NULL copy info from it to the new window (only with
3095 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003096 * Return FAIL when something goes wrong (out of memory).
3097 */
3098 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003099win_alloc_firstwin(oldwin)
3100 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003101{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003103 if (oldwin == NULL)
3104 {
3105 /* Very first window, need to create an empty buffer for it and
3106 * initialize from scratch. */
3107 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3108 if (curwin == NULL || curbuf == NULL)
3109 return FAIL;
3110 curwin->w_buffer = curbuf;
3111 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003113 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003115 curwin_init(); /* init current window */
3116 }
3117#ifdef FEAT_WINDOWS
3118 else
3119 {
3120 /* First window in new tab page, initialize it from "oldwin". */
3121 win_init(curwin, oldwin);
3122
3123# ifdef FEAT_SCROLLBIND
3124 /* We don't want scroll-binding in the first window. */
3125 curwin->w_p_scb = FALSE;
3126# endif
3127 }
3128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129
3130 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3131 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003132 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 topframe->fr_layout = FR_LEAF;
3134#ifdef FEAT_VERTSPLIT
3135 topframe->fr_width = Columns;
3136#endif
3137 topframe->fr_height = Rows - p_ch;
3138 topframe->fr_win = curwin;
3139 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003140
3141 return OK;
3142}
3143
3144/*
3145 * Initialize the window and frame size to the maximum.
3146 */
3147 void
3148win_init_size()
3149{
3150 firstwin->w_height = ROWS_AVAIL;
3151 topframe->fr_height = ROWS_AVAIL;
3152#ifdef FEAT_VERTSPLIT
3153 firstwin->w_width = Columns;
3154 topframe->fr_width = Columns;
3155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156}
3157
3158#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003159
3160/*
3161 * Allocate a new tabpage_T and init the values.
3162 * Returns NULL when out of memory.
3163 */
3164 static tabpage_T *
3165alloc_tabpage()
3166{
3167 tabpage_T *tp;
3168
3169 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3170 if (tp != NULL)
3171 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003172# ifdef FEAT_GUI
3173 int i;
3174
3175 for (i = 0; i < 3; i++)
3176 tp->tp_prev_which_scrollbars[i] = -1;
3177# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003178# ifdef FEAT_DIFF
3179 tp->tp_diff_invalid = TRUE;
3180# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003181#ifdef FEAT_EVAL
3182 /* init t: variables */
3183 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3184#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003185 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003186 }
3187 return tp;
3188}
3189
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003190 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003191free_tabpage(tp)
3192 tabpage_T *tp;
3193{
3194# ifdef FEAT_DIFF
3195 diff_clear(tp);
3196# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003197 clear_snapshot(tp);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003198#ifdef FEAT_EVAL
3199 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3200#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003201 vim_free(tp);
3202}
3203
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003204/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003205 * Create a new Tab page with one window.
3206 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003207 * When "after" is 0 put it just after the current Tab page.
3208 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003209 * Return FAIL or OK.
3210 */
3211 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003212win_new_tabpage(after)
3213 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003214{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003215 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003216 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003217 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003218
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003219 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003220 if (newtp == NULL)
3221 return FAIL;
3222
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003223 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003224 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003225 {
3226 vim_free(newtp);
3227 return FAIL;
3228 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003229 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003230
3231 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003232 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003233 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003234 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003235 if (after == 1)
3236 {
3237 /* New tab page becomes the first one. */
3238 newtp->tp_next = first_tabpage;
3239 first_tabpage = newtp;
3240 }
3241 else
3242 {
3243 if (after > 0)
3244 {
3245 /* Put new tab page before tab page "after". */
3246 n = 2;
3247 for (tp = first_tabpage; tp->tp_next != NULL
3248 && n < after; tp = tp->tp_next)
3249 ++n;
3250 }
3251 newtp->tp_next = tp->tp_next;
3252 tp->tp_next = newtp;
3253 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003254 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003255 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003256 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003257
3258 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003259 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003260
3261#if defined(FEAT_GUI)
3262 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3263 * scrollbars. Have to update them anyway. */
3264 if (gui.in_use && starting == 0)
3265 {
3266 gui_init_which_components(NULL);
3267 gui_update_scrollbars(TRUE);
3268 }
3269 need_mouse_correct = TRUE;
3270#endif
3271
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003272 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003273#ifdef FEAT_AUTOCMD
3274 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3275 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3276#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003277 return OK;
3278 }
3279
3280 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003281 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003282 return FAIL;
3283}
3284
3285/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003286 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3287 * like with ":split".
3288 * Returns OK if a new tab page was created, FAIL otherwise.
3289 */
3290 int
3291may_open_tabpage()
3292{
3293 int n = cmdmod.tab;
3294
3295 if (cmdmod.tab != 0)
3296 {
3297 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3298 return win_new_tabpage(n);
3299 }
3300 return FAIL;
3301}
3302
3303/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003304 * Create up to "maxcount" tabpages with empty windows.
3305 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003306 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003307 int
3308make_tabpages(maxcount)
3309 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003310{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003311 int count = maxcount;
3312 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003313
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003314 /* Limit to 'tabpagemax' tabs. */
3315 if (count > p_tpm)
3316 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003317
3318#ifdef FEAT_AUTOCMD
3319 /*
3320 * Don't execute autocommands while creating the tab pages. Must do that
3321 * when putting the buffers in the windows.
3322 */
3323 ++autocmd_block;
3324#endif
3325
3326 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003327 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003328 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003329
3330#ifdef FEAT_AUTOCMD
3331 --autocmd_block;
3332#endif
3333
3334 /* return actual number of tab pages */
3335 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003336}
3337
3338/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003339 * Return TRUE when "tpc" points to a valid tab page.
3340 */
3341 int
3342valid_tabpage(tpc)
3343 tabpage_T *tpc;
3344{
3345 tabpage_T *tp;
3346
3347 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3348 if (tp == tpc)
3349 return TRUE;
3350 return FALSE;
3351}
3352
3353/*
3354 * Find tab page "n" (first one is 1). Returns NULL when not found.
3355 */
3356 tabpage_T *
3357find_tabpage(n)
3358 int n;
3359{
3360 tabpage_T *tp;
3361 int i = 1;
3362
3363 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3364 ++i;
3365 return tp;
3366}
3367
3368/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003369 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003370 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003371 */
3372 int
3373tabpage_index(ftp)
3374 tabpage_T *ftp;
3375{
3376 int i = 1;
3377 tabpage_T *tp;
3378
3379 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3380 ++i;
3381 return i;
3382}
3383
3384/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003385 * Prepare for leaving the current tab page.
3386 * When autocomands change "curtab" we don't leave the tab page and return
3387 * FAIL.
3388 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003389 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003390/*ARGSUSED*/
3391 static int
3392leave_tabpage(new_curbuf)
3393 buf_T *new_curbuf; /* what is going to be the new curbuf,
3394 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003395{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003396 tabpage_T *tp = curtab;
3397
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003398#ifdef FEAT_VISUAL
3399 reset_VIsual_and_resel(); /* stop Visual mode */
3400#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003401#ifdef FEAT_AUTOCMD
3402 if (new_curbuf != curbuf)
3403 {
3404 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3405 if (curtab != tp)
3406 return FAIL;
3407 }
3408 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3409 if (curtab != tp)
3410 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003411 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003412 if (curtab != tp)
3413 return FAIL;
3414#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003415#if defined(FEAT_GUI)
3416 /* Remove the scrollbars. They may be added back later. */
3417 if (gui.in_use)
3418 gui_remove_scrollbars();
3419#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003420 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003421 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003422 tp->tp_firstwin = firstwin;
3423 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003424 tp->tp_old_Rows = Rows;
3425 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003426 firstwin = NULL;
3427 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003428 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003429}
3430
3431/*
3432 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003433 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003434 */
3435/*ARGSUSED*/
3436 static void
3437enter_tabpage(tp, old_curbuf)
3438 tabpage_T *tp;
3439 buf_T *old_curbuf;
3440{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003441 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003442 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003443
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003444 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003445 firstwin = tp->tp_firstwin;
3446 lastwin = tp->tp_lastwin;
3447 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003448
3449 /* We would like doing the TabEnter event first, but we don't have a
3450 * valid current window yet, which may break some commands.
3451 * This triggers autocommands, thus may make "tp" invalid. */
3452 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3453 prevwin = next_prevwin;
3454
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003455#ifdef FEAT_AUTOCMD
3456 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003457
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003458 if (old_curbuf != curbuf)
3459 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3460#endif
3461
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003462 last_status(FALSE); /* status line may appear or disappear */
3463 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003464 must_redraw = CLEAR; /* need to redraw everything */
3465#ifdef FEAT_DIFF
3466 diff_need_scrollbind = TRUE;
3467#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003468
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003469 /* The tabpage line may have appeared or disappeared, may need to resize
3470 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003471 * frame sizes too. Use the stored value of p_ch, so that it can be
3472 * different for each tab page. */
3473 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003474 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3475#ifdef FEAT_GUI_TABLINE
3476 && !gui_use_tabline()
3477#endif
3478 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003479 shell_new_rows();
3480#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003481 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003482 shell_new_columns(); /* update window widths */
3483#endif
3484
3485#if defined(FEAT_GUI)
3486 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3487 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003488 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003489 {
3490 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003491 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003492 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003493 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003494#endif
3495
3496 redraw_all_later(CLEAR);
3497}
3498
3499/*
3500 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003501 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003502 */
3503 void
3504goto_tabpage(n)
3505 int n;
3506{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003507 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003508 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003509 int i;
3510
Bram Moolenaard68071d2006-05-02 22:08:30 +00003511 if (text_locked())
3512 {
3513 /* Not allowed when editing the command line. */
3514#ifdef FEAT_CMDWIN
3515 if (cmdwin_type != 0)
3516 EMSG(_(e_cmdwin));
3517 else
3518#endif
3519 EMSG(_(e_secure));
3520 return;
3521 }
3522
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003523 /* If there is only one it can't work. */
3524 if (first_tabpage->tp_next == NULL)
3525 {
3526 if (n > 1)
3527 beep_flush();
3528 return;
3529 }
3530
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003531 if (n == 0)
3532 {
3533 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003534 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003535 tp = first_tabpage;
3536 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003537 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003538 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003539 else if (n < 0)
3540 {
3541 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3542 * this N times. */
3543 ttp = curtab;
3544 for (i = n; i < 0; ++i)
3545 {
3546 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3547 tp = tp->tp_next)
3548 ;
3549 ttp = tp;
3550 }
3551 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003552 else if (n == 9999)
3553 {
3554 /* Go to last tab page. */
3555 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3556 ;
3557 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003558 else
3559 {
3560 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003561 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003562 if (tp == NULL)
3563 {
3564 beep_flush();
3565 return;
3566 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003567 }
3568
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003569 goto_tabpage_tp(tp);
3570
3571#ifdef FEAT_GUI_TABLINE
3572 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003573 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003574#endif
3575}
3576
3577/*
3578 * Go to tabpage "tp".
3579 * Note: doesn't update the GUI tab.
3580 */
3581 void
3582goto_tabpage_tp(tp)
3583 tabpage_T *tp;
3584{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003585 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003586 {
3587 if (valid_tabpage(tp))
3588 enter_tabpage(tp, curbuf);
3589 else
3590 enter_tabpage(curtab, curbuf);
3591 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003592}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593
3594/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003595 * Enter window "wp" in tab page "tp".
3596 * Also updates the GUI tab.
3597 */
3598 void
3599goto_tabpage_win(tp, wp)
3600 tabpage_T *tp;
3601 win_T *wp;
3602{
3603 goto_tabpage_tp(tp);
3604 if (curtab == tp && win_valid(wp))
3605 {
3606 win_enter(wp, TRUE);
3607# ifdef FEAT_GUI_TABLINE
3608 if (gui_use_tabline())
3609 gui_mch_set_curtab(tabpage_index(curtab));
3610# endif
3611 }
3612}
3613
3614/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003615 * Move the current tab page to before tab page "nr".
3616 */
3617 void
3618tabpage_move(nr)
3619 int nr;
3620{
3621 int n = nr;
3622 tabpage_T *tp;
3623
3624 if (first_tabpage->tp_next == NULL)
3625 return;
3626
3627 /* Remove the current tab page from the list of tab pages. */
3628 if (curtab == first_tabpage)
3629 first_tabpage = curtab->tp_next;
3630 else
3631 {
3632 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3633 if (tp->tp_next == curtab)
3634 break;
3635 if (tp == NULL) /* "cannot happen" */
3636 return;
3637 tp->tp_next = curtab->tp_next;
3638 }
3639
3640 /* Re-insert it at the specified position. */
3641 if (n == 0)
3642 {
3643 curtab->tp_next = first_tabpage;
3644 first_tabpage = curtab;
3645 }
3646 else
3647 {
3648 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3649 --n;
3650 curtab->tp_next = tp->tp_next;
3651 tp->tp_next = curtab;
3652 }
3653
3654 /* Need to redraw the tabline. Tab page contents doesn't change. */
3655 redraw_tabline = TRUE;
3656}
3657
3658
3659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 * Go to another window.
3661 * When jumping to another buffer, stop Visual mode. Do this before
3662 * changing windows so we can yank the selection into the '*' register.
3663 * When jumping to another window on the same buffer, adjust its cursor
3664 * position to keep the same Visual area.
3665 */
3666 void
3667win_goto(wp)
3668 win_T *wp;
3669{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003670 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
3672 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003673 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 return;
3675 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003676#ifdef FEAT_AUTOCMD
3677 if (curbuf_locked())
3678 return;
3679#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003680
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681#ifdef FEAT_VISUAL
3682 if (wp->w_buffer != curbuf)
3683 reset_VIsual_and_resel();
3684 else if (VIsual_active)
3685 wp->w_cursor = curwin->w_cursor;
3686#endif
3687
3688#ifdef FEAT_GUI
3689 need_mouse_correct = TRUE;
3690#endif
3691 win_enter(wp, TRUE);
3692}
3693
3694#if defined(FEAT_PERL) || defined(PROTO)
3695/*
3696 * Find window number "winnr" (counting top to bottom).
3697 */
3698 win_T *
3699win_find_nr(winnr)
3700 int winnr;
3701{
3702 win_T *wp;
3703
3704# ifdef FEAT_WINDOWS
3705 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3706 if (--winnr == 0)
3707 break;
3708 return wp;
3709# else
3710 return curwin;
3711# endif
3712}
3713#endif
3714
3715#ifdef FEAT_VERTSPLIT
3716/*
3717 * Move to window above or below "count" times.
3718 */
3719 static void
3720win_goto_ver(up, count)
3721 int up; /* TRUE to go to win above */
3722 long count;
3723{
3724 frame_T *fr;
3725 frame_T *nfr;
3726 frame_T *foundfr;
3727
3728 foundfr = curwin->w_frame;
3729 while (count--)
3730 {
3731 /*
3732 * First go upwards in the tree of frames until we find a upwards or
3733 * downwards neighbor.
3734 */
3735 fr = foundfr;
3736 for (;;)
3737 {
3738 if (fr == topframe)
3739 goto end;
3740 if (up)
3741 nfr = fr->fr_prev;
3742 else
3743 nfr = fr->fr_next;
3744 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3745 break;
3746 fr = fr->fr_parent;
3747 }
3748
3749 /*
3750 * Now go downwards to find the bottom or top frame in it.
3751 */
3752 for (;;)
3753 {
3754 if (nfr->fr_layout == FR_LEAF)
3755 {
3756 foundfr = nfr;
3757 break;
3758 }
3759 fr = nfr->fr_child;
3760 if (nfr->fr_layout == FR_ROW)
3761 {
3762 /* Find the frame at the cursor row. */
3763 while (fr->fr_next != NULL
3764 && frame2win(fr)->w_wincol + fr->fr_width
3765 <= curwin->w_wincol + curwin->w_wcol)
3766 fr = fr->fr_next;
3767 }
3768 if (nfr->fr_layout == FR_COL && up)
3769 while (fr->fr_next != NULL)
3770 fr = fr->fr_next;
3771 nfr = fr;
3772 }
3773 }
3774end:
3775 if (foundfr != NULL)
3776 win_goto(foundfr->fr_win);
3777}
3778
3779/*
3780 * Move to left or right window.
3781 */
3782 static void
3783win_goto_hor(left, count)
3784 int left; /* TRUE to go to left win */
3785 long count;
3786{
3787 frame_T *fr;
3788 frame_T *nfr;
3789 frame_T *foundfr;
3790
3791 foundfr = curwin->w_frame;
3792 while (count--)
3793 {
3794 /*
3795 * First go upwards in the tree of frames until we find a left or
3796 * right neighbor.
3797 */
3798 fr = foundfr;
3799 for (;;)
3800 {
3801 if (fr == topframe)
3802 goto end;
3803 if (left)
3804 nfr = fr->fr_prev;
3805 else
3806 nfr = fr->fr_next;
3807 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3808 break;
3809 fr = fr->fr_parent;
3810 }
3811
3812 /*
3813 * Now go downwards to find the leftmost or rightmost frame in it.
3814 */
3815 for (;;)
3816 {
3817 if (nfr->fr_layout == FR_LEAF)
3818 {
3819 foundfr = nfr;
3820 break;
3821 }
3822 fr = nfr->fr_child;
3823 if (nfr->fr_layout == FR_COL)
3824 {
3825 /* Find the frame at the cursor row. */
3826 while (fr->fr_next != NULL
3827 && frame2win(fr)->w_winrow + fr->fr_height
3828 <= curwin->w_winrow + curwin->w_wrow)
3829 fr = fr->fr_next;
3830 }
3831 if (nfr->fr_layout == FR_ROW && left)
3832 while (fr->fr_next != NULL)
3833 fr = fr->fr_next;
3834 nfr = fr;
3835 }
3836 }
3837end:
3838 if (foundfr != NULL)
3839 win_goto(foundfr->fr_win);
3840}
3841#endif
3842
3843/*
3844 * Make window "wp" the current window.
3845 */
3846 void
3847win_enter(wp, undo_sync)
3848 win_T *wp;
3849 int undo_sync;
3850{
3851 win_enter_ext(wp, undo_sync, FALSE);
3852}
3853
3854/*
3855 * Make window wp the current window.
3856 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3857 * been closed and isn't valid.
3858 */
3859 static void
3860win_enter_ext(wp, undo_sync, curwin_invalid)
3861 win_T *wp;
3862 int undo_sync;
3863 int curwin_invalid;
3864{
3865#ifdef FEAT_AUTOCMD
3866 int other_buffer = FALSE;
3867#endif
3868
3869 if (wp == curwin && !curwin_invalid) /* nothing to do */
3870 return;
3871
3872#ifdef FEAT_AUTOCMD
3873 if (!curwin_invalid)
3874 {
3875 /*
3876 * Be careful: If autocommands delete the window, return now.
3877 */
3878 if (wp->w_buffer != curbuf)
3879 {
3880 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3881 other_buffer = TRUE;
3882 if (!win_valid(wp))
3883 return;
3884 }
3885 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3886 if (!win_valid(wp))
3887 return;
3888# ifdef FEAT_EVAL
3889 /* autocmds may abort script processing */
3890 if (aborting())
3891 return;
3892# endif
3893 }
3894#endif
3895
3896 /* sync undo before leaving the current buffer */
3897 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003898 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 /* may have to copy the buffer options when 'cpo' contains 'S' */
3900 if (wp->w_buffer != curbuf)
3901 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3902 if (!curwin_invalid)
3903 {
3904 prevwin = curwin; /* remember for CTRL-W p */
3905 curwin->w_redr_status = TRUE;
3906 }
3907 curwin = wp;
3908 curbuf = wp->w_buffer;
3909 check_cursor();
3910#ifdef FEAT_VIRTUALEDIT
3911 if (!virtual_active())
3912 curwin->w_cursor.coladd = 0;
3913#endif
3914 changed_line_abv_curs(); /* assume cursor position needs updating */
3915
3916 if (curwin->w_localdir != NULL)
3917 {
3918 /* Window has a local directory: Save current directory as global
3919 * directory (unless that was done already) and change to the local
3920 * directory. */
3921 if (globaldir == NULL)
3922 {
3923 char_u cwd[MAXPATHL];
3924
3925 if (mch_dirname(cwd, MAXPATHL) == OK)
3926 globaldir = vim_strsave(cwd);
3927 }
3928 mch_chdir((char *)curwin->w_localdir);
3929 shorten_fnames(TRUE);
3930 }
3931 else if (globaldir != NULL)
3932 {
3933 /* Window doesn't have a local directory and we are not in the global
3934 * directory: Change to the global directory. */
3935 mch_chdir((char *)globaldir);
3936 vim_free(globaldir);
3937 globaldir = NULL;
3938 shorten_fnames(TRUE);
3939 }
3940
3941#ifdef FEAT_AUTOCMD
3942 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3943 if (other_buffer)
3944 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3945#endif
3946
3947#ifdef FEAT_TITLE
3948 maketitle();
3949#endif
3950 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003951 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 if (restart_edit)
3953 redraw_later(VALID); /* causes status line redraw */
3954
3955 /* set window height to desired minimal value */
3956 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3957 win_setheight((int)p_wh);
3958 else if (curwin->w_height == 0)
3959 win_setheight(1);
3960
3961#ifdef FEAT_VERTSPLIT
3962 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003963 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 win_setwidth((int)p_wiw);
3965#endif
3966
3967#ifdef FEAT_MOUSE
3968 setmouse(); /* in case jumped to/from help buffer */
3969#endif
3970
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003971 /* Change directories when the 'acd' option is set. */
3972 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973}
3974
3975#endif /* FEAT_WINDOWS */
3976
3977#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3978/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003979 * Jump to the first open window that contains buffer "buf", if one exists.
3980 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 */
3982 win_T *
3983buf_jump_open_win(buf)
3984 buf_T *buf;
3985{
3986# ifdef FEAT_WINDOWS
3987 win_T *wp;
3988
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003989 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 if (wp->w_buffer == buf)
3991 break;
3992 if (wp != NULL)
3993 win_enter(wp, FALSE);
3994 return wp;
3995# else
3996 if (curwin->w_buffer == buf)
3997 return curwin;
3998 return NULL;
3999# endif
4000}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004001
4002/*
4003 * Jump to the first open window in any tab page that contains buffer "buf",
4004 * if one exists.
4005 * Returns a pointer to the window found, otherwise NULL.
4006 */
4007 win_T *
4008buf_jump_open_tab(buf)
4009 buf_T *buf;
4010{
4011# ifdef FEAT_WINDOWS
4012 win_T *wp;
4013 tabpage_T *tp;
4014
4015 /* First try the current tab page. */
4016 wp = buf_jump_open_win(buf);
4017 if (wp != NULL)
4018 return wp;
4019
4020 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4021 if (tp != curtab)
4022 {
4023 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4024 if (wp->w_buffer == buf)
4025 break;
4026 if (wp != NULL)
4027 {
4028 goto_tabpage_win(tp, wp);
4029 if (curwin != wp)
4030 wp = NULL; /* something went wrong */
4031 break;
4032 }
4033 }
4034
4035 return wp;
4036# else
4037 if (curwin->w_buffer == buf)
4038 return curwin;
4039 return NULL;
4040# endif
4041}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042#endif
4043
4044/*
4045 * allocate a window structure and link it in the window list
4046 */
4047/*ARGSUSED*/
4048 static win_T *
4049win_alloc(after)
4050 win_T *after;
4051{
4052 win_T *newwin;
4053
4054 /*
4055 * allocate window structure and linesizes arrays
4056 */
4057 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4058 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4059 {
4060 vim_free(newwin);
4061 newwin = NULL;
4062 }
4063
4064 if (newwin != NULL)
4065 {
4066 /*
4067 * link the window in the window list
4068 */
4069#ifdef FEAT_WINDOWS
4070 win_append(after, newwin);
4071#endif
4072#ifdef FEAT_VERTSPLIT
4073 newwin->w_wincol = 0;
4074 newwin->w_width = Columns;
4075#endif
4076
4077 /* position the display and the cursor at the top of the file. */
4078 newwin->w_topline = 1;
4079#ifdef FEAT_DIFF
4080 newwin->w_topfill = 0;
4081#endif
4082 newwin->w_botline = 2;
4083 newwin->w_cursor.lnum = 1;
4084#ifdef FEAT_SCROLLBIND
4085 newwin->w_scbind_pos = 1;
4086#endif
4087
4088 /* We won't calculate w_fraction until resizing the window */
4089 newwin->w_fraction = 0;
4090 newwin->w_prev_fraction_row = -1;
4091
4092#ifdef FEAT_GUI
4093 if (gui.in_use)
4094 {
4095 out_flush();
4096 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4097 SBAR_LEFT, newwin);
4098 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4099 SBAR_RIGHT, newwin);
4100 }
4101#endif
4102#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004103 /* init w: variables */
4104 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105#endif
4106#ifdef FEAT_FOLDING
4107 foldInitWin(newwin);
4108#endif
4109 }
4110 return newwin;
4111}
4112
4113#if defined(FEAT_WINDOWS) || defined(PROTO)
4114
4115/*
4116 * remove window 'wp' from the window list and free the structure
4117 */
4118 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004119win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004121 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122{
4123 int i;
4124
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004125#ifdef FEAT_MZSCHEME
4126 mzscheme_window_free(wp);
4127#endif
4128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129#ifdef FEAT_PERL
4130 perl_win_free(wp);
4131#endif
4132
4133#ifdef FEAT_PYTHON
4134 python_window_free(wp);
4135#endif
4136
4137#ifdef FEAT_TCL
4138 tcl_window_free(wp);
4139#endif
4140
4141#ifdef FEAT_RUBY
4142 ruby_window_free(wp);
4143#endif
4144
4145 clear_winopt(&wp->w_onebuf_opt);
4146 clear_winopt(&wp->w_allbuf_opt);
4147
4148#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004149 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150#endif
4151
4152 if (prevwin == wp)
4153 prevwin = NULL;
4154 win_free_lsize(wp);
4155
4156 for (i = 0; i < wp->w_tagstacklen; ++i)
4157 vim_free(wp->w_tagstack[i].tagname);
4158
4159 vim_free(wp->w_localdir);
4160#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004161 vim_free(wp->w_match[0].regprog);
4162 vim_free(wp->w_match[1].regprog);
4163 vim_free(wp->w_match[2].regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164#endif
4165#ifdef FEAT_JUMPLIST
4166 free_jumplist(wp);
4167#endif
4168
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004169#ifdef FEAT_QUICKFIX
4170 qf_free_all(wp);
4171#endif
4172
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173#ifdef FEAT_GUI
4174 if (gui.in_use)
4175 {
4176 out_flush();
4177 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4178 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4179 }
4180#endif /* FEAT_GUI */
4181
Bram Moolenaarf740b292006-02-16 22:11:02 +00004182 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 vim_free(wp);
4184}
4185
4186/*
4187 * Append window "wp" in the window list after window "after".
4188 */
4189 static void
4190win_append(after, wp)
4191 win_T *after, *wp;
4192{
4193 win_T *before;
4194
4195 if (after == NULL) /* after NULL is in front of the first */
4196 before = firstwin;
4197 else
4198 before = after->w_next;
4199
4200 wp->w_next = before;
4201 wp->w_prev = after;
4202 if (after == NULL)
4203 firstwin = wp;
4204 else
4205 after->w_next = wp;
4206 if (before == NULL)
4207 lastwin = wp;
4208 else
4209 before->w_prev = wp;
4210}
4211
4212/*
4213 * Remove a window from the window list.
4214 */
4215 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004216win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004218 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219{
4220 if (wp->w_prev != NULL)
4221 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004222 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004224 else
4225 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 if (wp->w_next != NULL)
4227 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004228 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004230 else
4231 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232}
4233
4234/*
4235 * Append frame "frp" in a frame list after frame "after".
4236 */
4237 static void
4238frame_append(after, frp)
4239 frame_T *after, *frp;
4240{
4241 frp->fr_next = after->fr_next;
4242 after->fr_next = frp;
4243 if (frp->fr_next != NULL)
4244 frp->fr_next->fr_prev = frp;
4245 frp->fr_prev = after;
4246}
4247
4248/*
4249 * Insert frame "frp" in a frame list before frame "before".
4250 */
4251 static void
4252frame_insert(before, frp)
4253 frame_T *before, *frp;
4254{
4255 frp->fr_next = before;
4256 frp->fr_prev = before->fr_prev;
4257 before->fr_prev = frp;
4258 if (frp->fr_prev != NULL)
4259 frp->fr_prev->fr_next = frp;
4260 else
4261 frp->fr_parent->fr_child = frp;
4262}
4263
4264/*
4265 * Remove a frame from a frame list.
4266 */
4267 static void
4268frame_remove(frp)
4269 frame_T *frp;
4270{
4271 if (frp->fr_prev != NULL)
4272 frp->fr_prev->fr_next = frp->fr_next;
4273 else
4274 frp->fr_parent->fr_child = frp->fr_next;
4275 if (frp->fr_next != NULL)
4276 frp->fr_next->fr_prev = frp->fr_prev;
4277}
4278
4279#endif /* FEAT_WINDOWS */
4280
4281/*
4282 * Allocate w_lines[] for window "wp".
4283 * Return FAIL for failure, OK for success.
4284 */
4285 int
4286win_alloc_lines(wp)
4287 win_T *wp;
4288{
4289 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004290 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 if (wp->w_lines == NULL)
4292 return FAIL;
4293 return OK;
4294}
4295
4296/*
4297 * free lsize arrays for a window
4298 */
4299 void
4300win_free_lsize(wp)
4301 win_T *wp;
4302{
4303 vim_free(wp->w_lines);
4304 wp->w_lines = NULL;
4305}
4306
4307/*
4308 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004309 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 */
4311 void
4312shell_new_rows()
4313{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004314 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315
4316 if (firstwin == NULL) /* not initialized yet */
4317 return;
4318#ifdef FEAT_WINDOWS
4319 if (h < frame_minheight(topframe, NULL))
4320 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004321
4322 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 * that doesn't result in the right height, forget about that option. */
4324 frame_new_height(topframe, h, FALSE, TRUE);
4325 if (topframe->fr_height != h)
4326 frame_new_height(topframe, h, FALSE, FALSE);
4327
4328 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4329#else
4330 if (h < 1)
4331 h = 1;
4332 win_new_height(firstwin, h);
4333#endif
4334 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004335#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004336 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004337#endif
4338
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339#if 0
4340 /* Disabled: don't want making the screen smaller make a window larger. */
4341 if (p_ea)
4342 win_equal(curwin, FALSE, 'v');
4343#endif
4344}
4345
4346#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4347/*
4348 * Called from win_new_shellsize() after Columns changed.
4349 */
4350 void
4351shell_new_columns()
4352{
4353 if (firstwin == NULL) /* not initialized yet */
4354 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004355
4356 /* First try setting the widths of windows with 'winfixwidth'. If that
4357 * doesn't result in the right width, forget about that option. */
4358 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4359 if (topframe->fr_width != Columns)
4360 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4361
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4363#if 0
4364 /* Disabled: don't want making the screen smaller make a window larger. */
4365 if (p_ea)
4366 win_equal(curwin, FALSE, 'h');
4367#endif
4368}
4369#endif
4370
4371#if defined(FEAT_CMDWIN) || defined(PROTO)
4372/*
4373 * Save the size of all windows in "gap".
4374 */
4375 void
4376win_size_save(gap)
4377 garray_T *gap;
4378
4379{
4380 win_T *wp;
4381
4382 ga_init2(gap, (int)sizeof(int), 1);
4383 if (ga_grow(gap, win_count() * 2) == OK)
4384 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4385 {
4386 ((int *)gap->ga_data)[gap->ga_len++] =
4387 wp->w_width + wp->w_vsep_width;
4388 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4389 }
4390}
4391
4392/*
4393 * Restore window sizes, but only if the number of windows is still the same.
4394 * Does not free the growarray.
4395 */
4396 void
4397win_size_restore(gap)
4398 garray_T *gap;
4399{
4400 win_T *wp;
4401 int i;
4402
4403 if (win_count() * 2 == gap->ga_len)
4404 {
4405 i = 0;
4406 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4407 {
4408 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4409 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4410 }
4411 /* recompute the window positions */
4412 (void)win_comp_pos();
4413 }
4414}
4415#endif /* FEAT_CMDWIN */
4416
4417#if defined(FEAT_WINDOWS) || defined(PROTO)
4418/*
4419 * Update the position for all windows, using the width and height of the
4420 * frames.
4421 * Returns the row just after the last window.
4422 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004423 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424win_comp_pos()
4425{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004426 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 int col = 0;
4428
4429 frame_comp_pos(topframe, &row, &col);
4430 return row;
4431}
4432
4433/*
4434 * Update the position of the windows in frame "topfrp", using the width and
4435 * height of the frames.
4436 * "*row" and "*col" are the top-left position of the frame. They are updated
4437 * to the bottom-right position plus one.
4438 */
4439 static void
4440frame_comp_pos(topfrp, row, col)
4441 frame_T *topfrp;
4442 int *row;
4443 int *col;
4444{
4445 win_T *wp;
4446 frame_T *frp;
4447#ifdef FEAT_VERTSPLIT
4448 int startcol;
4449 int startrow;
4450#endif
4451
4452 wp = topfrp->fr_win;
4453 if (wp != NULL)
4454 {
4455 if (wp->w_winrow != *row
4456#ifdef FEAT_VERTSPLIT
4457 || wp->w_wincol != *col
4458#endif
4459 )
4460 {
4461 /* position changed, redraw */
4462 wp->w_winrow = *row;
4463#ifdef FEAT_VERTSPLIT
4464 wp->w_wincol = *col;
4465#endif
4466 redraw_win_later(wp, NOT_VALID);
4467 wp->w_redr_status = TRUE;
4468 }
4469 *row += wp->w_height + wp->w_status_height;
4470#ifdef FEAT_VERTSPLIT
4471 *col += wp->w_width + wp->w_vsep_width;
4472#endif
4473 }
4474 else
4475 {
4476#ifdef FEAT_VERTSPLIT
4477 startrow = *row;
4478 startcol = *col;
4479#endif
4480 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4481 {
4482#ifdef FEAT_VERTSPLIT
4483 if (topfrp->fr_layout == FR_ROW)
4484 *row = startrow; /* all frames are at the same row */
4485 else
4486 *col = startcol; /* all frames are at the same col */
4487#endif
4488 frame_comp_pos(frp, row, col);
4489 }
4490 }
4491}
4492
4493#endif /* FEAT_WINDOWS */
4494
4495/*
4496 * Set current window height and take care of repositioning other windows to
4497 * fit around it.
4498 */
4499 void
4500win_setheight(height)
4501 int height;
4502{
4503 win_setheight_win(height, curwin);
4504}
4505
4506/*
4507 * Set the window height of window "win" and take care of repositioning other
4508 * windows to fit around it.
4509 */
4510 void
4511win_setheight_win(height, win)
4512 int height;
4513 win_T *win;
4514{
4515 int row;
4516
4517 if (win == curwin)
4518 {
4519 /* Always keep current window at least one line high, even when
4520 * 'winminheight' is zero. */
4521#ifdef FEAT_WINDOWS
4522 if (height < p_wmh)
4523 height = p_wmh;
4524#endif
4525 if (height == 0)
4526 height = 1;
4527 }
4528
4529#ifdef FEAT_WINDOWS
4530 frame_setheight(win->w_frame, height + win->w_status_height);
4531
4532 /* recompute the window positions */
4533 row = win_comp_pos();
4534#else
4535 if (height > topframe->fr_height)
4536 height = topframe->fr_height;
4537 win->w_height = height;
4538 row = height;
4539#endif
4540
4541 /*
4542 * If there is extra space created between the last window and the command
4543 * line, clear it.
4544 */
4545 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4546 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4547 cmdline_row = row;
4548 msg_row = row;
4549 msg_col = 0;
4550
4551 redraw_all_later(NOT_VALID);
4552}
4553
4554#if defined(FEAT_WINDOWS) || defined(PROTO)
4555
4556/*
4557 * Set the height of a frame to "height" and take care that all frames and
4558 * windows inside it are resized. Also resize frames on the left and right if
4559 * the are in the same FR_ROW frame.
4560 *
4561 * Strategy:
4562 * If the frame is part of a FR_COL frame, try fitting the frame in that
4563 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4564 * go to containing frames to resize them and make room.
4565 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4566 * Check for the minimal height of the FR_ROW frame.
4567 * At the top level we can also use change the command line height.
4568 */
4569 static void
4570frame_setheight(curfrp, height)
4571 frame_T *curfrp;
4572 int height;
4573{
4574 int room; /* total number of lines available */
4575 int take; /* number of lines taken from other windows */
4576 int room_cmdline; /* lines available from cmdline */
4577 int run;
4578 frame_T *frp;
4579 int h;
4580 int room_reserved;
4581
4582 /* If the height already is the desired value, nothing to do. */
4583 if (curfrp->fr_height == height)
4584 return;
4585
4586 if (curfrp->fr_parent == NULL)
4587 {
4588 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004589 if (height > ROWS_AVAIL)
4590 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 if (height > 0)
4592 frame_new_height(curfrp, height, FALSE, FALSE);
4593 }
4594 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4595 {
4596 /* Row of frames: Also need to resize frames left and right of this
4597 * one. First check for the minimal height of these. */
4598 h = frame_minheight(curfrp->fr_parent, NULL);
4599 if (height < h)
4600 height = h;
4601 frame_setheight(curfrp->fr_parent, height);
4602 }
4603 else
4604 {
4605 /*
4606 * Column of frames: try to change only frames in this column.
4607 */
4608#ifdef FEAT_VERTSPLIT
4609 /*
4610 * Do this twice:
4611 * 1: compute room available, if it's not enough try resizing the
4612 * containing frame.
4613 * 2: compute the room available and adjust the height to it.
4614 * Try not to reduce the height of a window with 'winfixheight' set.
4615 */
4616 for (run = 1; run <= 2; ++run)
4617#else
4618 for (;;)
4619#endif
4620 {
4621 room = 0;
4622 room_reserved = 0;
4623 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4624 frp = frp->fr_next)
4625 {
4626 if (frp != curfrp
4627 && frp->fr_win != NULL
4628 && frp->fr_win->w_p_wfh)
4629 room_reserved += frp->fr_height;
4630 room += frp->fr_height;
4631 if (frp != curfrp)
4632 room -= frame_minheight(frp, NULL);
4633 }
4634#ifdef FEAT_VERTSPLIT
4635 if (curfrp->fr_width != Columns)
4636 room_cmdline = 0;
4637 else
4638#endif
4639 {
4640 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4641 + lastwin->w_height + lastwin->w_status_height);
4642 if (room_cmdline < 0)
4643 room_cmdline = 0;
4644 }
4645
4646 if (height <= room + room_cmdline)
4647 break;
4648#ifdef FEAT_VERTSPLIT
4649 if (run == 2 || curfrp->fr_width == Columns)
4650#endif
4651 {
4652 if (height > room + room_cmdline)
4653 height = room + room_cmdline;
4654 break;
4655 }
4656#ifdef FEAT_VERTSPLIT
4657 frame_setheight(curfrp->fr_parent, height
4658 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4659#endif
4660 /*NOTREACHED*/
4661 }
4662
4663 /*
4664 * Compute the number of lines we will take from others frames (can be
4665 * negative!).
4666 */
4667 take = height - curfrp->fr_height;
4668
4669 /* If there is not enough room, also reduce the height of a window
4670 * with 'winfixheight' set. */
4671 if (height > room + room_cmdline - room_reserved)
4672 room_reserved = room + room_cmdline - height;
4673 /* If there is only a 'winfixheight' window and making the
4674 * window smaller, need to make the other window taller. */
4675 if (take < 0 && room - curfrp->fr_height < room_reserved)
4676 room_reserved = 0;
4677
4678 if (take > 0 && room_cmdline > 0)
4679 {
4680 /* use lines from cmdline first */
4681 if (take < room_cmdline)
4682 room_cmdline = take;
4683 take -= room_cmdline;
4684 topframe->fr_height += room_cmdline;
4685 }
4686
4687 /*
4688 * set the current frame to the new height
4689 */
4690 frame_new_height(curfrp, height, FALSE, FALSE);
4691
4692 /*
4693 * First take lines from the frames after the current frame. If
4694 * that is not enough, takes lines from frames above the current
4695 * frame.
4696 */
4697 for (run = 0; run < 2; ++run)
4698 {
4699 if (run == 0)
4700 frp = curfrp->fr_next; /* 1st run: start with next window */
4701 else
4702 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4703 while (frp != NULL && take != 0)
4704 {
4705 h = frame_minheight(frp, NULL);
4706 if (room_reserved > 0
4707 && frp->fr_win != NULL
4708 && frp->fr_win->w_p_wfh)
4709 {
4710 if (room_reserved >= frp->fr_height)
4711 room_reserved -= frp->fr_height;
4712 else
4713 {
4714 if (frp->fr_height - room_reserved > take)
4715 room_reserved = frp->fr_height - take;
4716 take -= frp->fr_height - room_reserved;
4717 frame_new_height(frp, room_reserved, FALSE, FALSE);
4718 room_reserved = 0;
4719 }
4720 }
4721 else
4722 {
4723 if (frp->fr_height - take < h)
4724 {
4725 take -= frp->fr_height - h;
4726 frame_new_height(frp, h, FALSE, FALSE);
4727 }
4728 else
4729 {
4730 frame_new_height(frp, frp->fr_height - take,
4731 FALSE, FALSE);
4732 take = 0;
4733 }
4734 }
4735 if (run == 0)
4736 frp = frp->fr_next;
4737 else
4738 frp = frp->fr_prev;
4739 }
4740 }
4741 }
4742}
4743
4744#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4745/*
4746 * Set current window width and take care of repositioning other windows to
4747 * fit around it.
4748 */
4749 void
4750win_setwidth(width)
4751 int width;
4752{
4753 win_setwidth_win(width, curwin);
4754}
4755
4756 void
4757win_setwidth_win(width, wp)
4758 int width;
4759 win_T *wp;
4760{
4761 /* Always keep current window at least one column wide, even when
4762 * 'winminwidth' is zero. */
4763 if (wp == curwin)
4764 {
4765 if (width < p_wmw)
4766 width = p_wmw;
4767 if (width == 0)
4768 width = 1;
4769 }
4770
4771 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4772
4773 /* recompute the window positions */
4774 (void)win_comp_pos();
4775
4776 redraw_all_later(NOT_VALID);
4777}
4778
4779/*
4780 * Set the width of a frame to "width" and take care that all frames and
4781 * windows inside it are resized. Also resize frames above and below if the
4782 * are in the same FR_ROW frame.
4783 *
4784 * Strategy is similar to frame_setheight().
4785 */
4786 static void
4787frame_setwidth(curfrp, width)
4788 frame_T *curfrp;
4789 int width;
4790{
4791 int room; /* total number of lines available */
4792 int take; /* number of lines taken from other windows */
4793 int run;
4794 frame_T *frp;
4795 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004796 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797
4798 /* If the width already is the desired value, nothing to do. */
4799 if (curfrp->fr_width == width)
4800 return;
4801
4802 if (curfrp->fr_parent == NULL)
4803 /* topframe: can't change width */
4804 return;
4805
4806 if (curfrp->fr_parent->fr_layout == FR_COL)
4807 {
4808 /* Column of frames: Also need to resize frames above and below of
4809 * this one. First check for the minimal width of these. */
4810 w = frame_minwidth(curfrp->fr_parent, NULL);
4811 if (width < w)
4812 width = w;
4813 frame_setwidth(curfrp->fr_parent, width);
4814 }
4815 else
4816 {
4817 /*
4818 * Row of frames: try to change only frames in this row.
4819 *
4820 * Do this twice:
4821 * 1: compute room available, if it's not enough try resizing the
4822 * containing frame.
4823 * 2: compute the room available and adjust the width to it.
4824 */
4825 for (run = 1; run <= 2; ++run)
4826 {
4827 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004828 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4830 frp = frp->fr_next)
4831 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004832 if (frp != curfrp
4833 && frp->fr_win != NULL
4834 && frp->fr_win->w_p_wfw)
4835 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 room += frp->fr_width;
4837 if (frp != curfrp)
4838 room -= frame_minwidth(frp, NULL);
4839 }
4840
4841 if (width <= room)
4842 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004843 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 {
4845 if (width > room)
4846 width = room;
4847 break;
4848 }
4849 frame_setwidth(curfrp->fr_parent, width
4850 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4851 }
4852
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 /*
4854 * Compute the number of lines we will take from others frames (can be
4855 * negative!).
4856 */
4857 take = width - curfrp->fr_width;
4858
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004859 /* If there is not enough room, also reduce the width of a window
4860 * with 'winfixwidth' set. */
4861 if (width > room - room_reserved)
4862 room_reserved = room - width;
4863 /* If there is only a 'winfixwidth' window and making the
4864 * window smaller, need to make the other window narrower. */
4865 if (take < 0 && room - curfrp->fr_width < room_reserved)
4866 room_reserved = 0;
4867
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 /*
4869 * set the current frame to the new width
4870 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004871 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872
4873 /*
4874 * First take lines from the frames right of the current frame. If
4875 * that is not enough, takes lines from frames left of the current
4876 * frame.
4877 */
4878 for (run = 0; run < 2; ++run)
4879 {
4880 if (run == 0)
4881 frp = curfrp->fr_next; /* 1st run: start with next window */
4882 else
4883 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4884 while (frp != NULL && take != 0)
4885 {
4886 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004887 if (room_reserved > 0
4888 && frp->fr_win != NULL
4889 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004891 if (room_reserved >= frp->fr_width)
4892 room_reserved -= frp->fr_width;
4893 else
4894 {
4895 if (frp->fr_width - room_reserved > take)
4896 room_reserved = frp->fr_width - take;
4897 take -= frp->fr_width - room_reserved;
4898 frame_new_width(frp, room_reserved, FALSE, FALSE);
4899 room_reserved = 0;
4900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
4902 else
4903 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004904 if (frp->fr_width - take < w)
4905 {
4906 take -= frp->fr_width - w;
4907 frame_new_width(frp, w, FALSE, FALSE);
4908 }
4909 else
4910 {
4911 frame_new_width(frp, frp->fr_width - take,
4912 FALSE, FALSE);
4913 take = 0;
4914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 }
4916 if (run == 0)
4917 frp = frp->fr_next;
4918 else
4919 frp = frp->fr_prev;
4920 }
4921 }
4922 }
4923}
4924#endif /* FEAT_VERTSPLIT */
4925
4926/*
4927 * Check 'winminheight' for a valid value.
4928 */
4929 void
4930win_setminheight()
4931{
4932 int room;
4933 int first = TRUE;
4934 win_T *wp;
4935
4936 /* loop until there is a 'winminheight' that is possible */
4937 while (p_wmh > 0)
4938 {
4939 /* TODO: handle vertical splits */
4940 room = -p_wh;
4941 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4942 room += wp->w_height - p_wmh;
4943 if (room >= 0)
4944 break;
4945 --p_wmh;
4946 if (first)
4947 {
4948 EMSG(_(e_noroom));
4949 first = FALSE;
4950 }
4951 }
4952}
4953
4954#ifdef FEAT_MOUSE
4955
4956/*
4957 * Status line of dragwin is dragged "offset" lines down (negative is up).
4958 */
4959 void
4960win_drag_status_line(dragwin, offset)
4961 win_T *dragwin;
4962 int offset;
4963{
4964 frame_T *curfr;
4965 frame_T *fr;
4966 int room;
4967 int row;
4968 int up; /* if TRUE, drag status line up, otherwise down */
4969 int n;
4970
4971 fr = dragwin->w_frame;
4972 curfr = fr;
4973 if (fr != topframe) /* more than one window */
4974 {
4975 fr = fr->fr_parent;
4976 /* When the parent frame is not a column of frames, its parent should
4977 * be. */
4978 if (fr->fr_layout != FR_COL)
4979 {
4980 curfr = fr;
4981 if (fr != topframe) /* only a row of windows, may drag statusline */
4982 fr = fr->fr_parent;
4983 }
4984 }
4985
4986 /* If this is the last frame in a column, may want to resize the parent
4987 * frame instead (go two up to skip a row of frames). */
4988 while (curfr != topframe && curfr->fr_next == NULL)
4989 {
4990 if (fr != topframe)
4991 fr = fr->fr_parent;
4992 curfr = fr;
4993 if (fr != topframe)
4994 fr = fr->fr_parent;
4995 }
4996
4997 if (offset < 0) /* drag up */
4998 {
4999 up = TRUE;
5000 offset = -offset;
5001 /* sum up the room of the current frame and above it */
5002 if (fr == curfr)
5003 {
5004 /* only one window */
5005 room = fr->fr_height - frame_minheight(fr, NULL);
5006 }
5007 else
5008 {
5009 room = 0;
5010 for (fr = fr->fr_child; ; fr = fr->fr_next)
5011 {
5012 room += fr->fr_height - frame_minheight(fr, NULL);
5013 if (fr == curfr)
5014 break;
5015 }
5016 }
5017 fr = curfr->fr_next; /* put fr at frame that grows */
5018 }
5019 else /* drag down */
5020 {
5021 up = FALSE;
5022 /*
5023 * Only dragging the last status line can reduce p_ch.
5024 */
5025 room = Rows - cmdline_row;
5026 if (curfr->fr_next == NULL)
5027 room -= 1;
5028 else
5029 room -= p_ch;
5030 if (room < 0)
5031 room = 0;
5032 /* sum up the room of frames below of the current one */
5033 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5034 room += fr->fr_height - frame_minheight(fr, NULL);
5035 fr = curfr; /* put fr at window that grows */
5036 }
5037
5038 if (room < offset) /* Not enough room */
5039 offset = room; /* Move as far as we can */
5040 if (offset <= 0)
5041 return;
5042
5043 /*
5044 * Grow frame fr by "offset" lines.
5045 * Doesn't happen when dragging the last status line up.
5046 */
5047 if (fr != NULL)
5048 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5049
5050 if (up)
5051 fr = curfr; /* current frame gets smaller */
5052 else
5053 fr = curfr->fr_next; /* next frame gets smaller */
5054
5055 /*
5056 * Now make the other frames smaller.
5057 */
5058 while (fr != NULL && offset > 0)
5059 {
5060 n = frame_minheight(fr, NULL);
5061 if (fr->fr_height - offset <= n)
5062 {
5063 offset -= fr->fr_height - n;
5064 frame_new_height(fr, n, !up, FALSE);
5065 }
5066 else
5067 {
5068 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5069 break;
5070 }
5071 if (up)
5072 fr = fr->fr_prev;
5073 else
5074 fr = fr->fr_next;
5075 }
5076 row = win_comp_pos();
5077 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5078 cmdline_row = row;
5079 p_ch = Rows - cmdline_row;
5080 if (p_ch < 1)
5081 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005082 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005083 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 showmode();
5085}
5086
5087#ifdef FEAT_VERTSPLIT
5088/*
5089 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5090 */
5091 void
5092win_drag_vsep_line(dragwin, offset)
5093 win_T *dragwin;
5094 int offset;
5095{
5096 frame_T *curfr;
5097 frame_T *fr;
5098 int room;
5099 int left; /* if TRUE, drag separator line left, otherwise right */
5100 int n;
5101
5102 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005103 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 return;
5105 curfr = fr;
5106 fr = fr->fr_parent;
5107 /* When the parent frame is not a row of frames, its parent should be. */
5108 if (fr->fr_layout != FR_ROW)
5109 {
5110 if (fr == topframe) /* only a column of windows (cannot happen?) */
5111 return;
5112 curfr = fr;
5113 fr = fr->fr_parent;
5114 }
5115
5116 /* If this is the last frame in a row, may want to resize a parent
5117 * frame instead. */
5118 while (curfr->fr_next == NULL)
5119 {
5120 if (fr == topframe)
5121 break;
5122 curfr = fr;
5123 fr = fr->fr_parent;
5124 if (fr != topframe)
5125 {
5126 curfr = fr;
5127 fr = fr->fr_parent;
5128 }
5129 }
5130
5131 if (offset < 0) /* drag left */
5132 {
5133 left = TRUE;
5134 offset = -offset;
5135 /* sum up the room of the current frame and left of it */
5136 room = 0;
5137 for (fr = fr->fr_child; ; fr = fr->fr_next)
5138 {
5139 room += fr->fr_width - frame_minwidth(fr, NULL);
5140 if (fr == curfr)
5141 break;
5142 }
5143 fr = curfr->fr_next; /* put fr at frame that grows */
5144 }
5145 else /* drag right */
5146 {
5147 left = FALSE;
5148 /* sum up the room of frames right of the current one */
5149 room = 0;
5150 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5151 room += fr->fr_width - frame_minwidth(fr, NULL);
5152 fr = curfr; /* put fr at window that grows */
5153 }
5154
5155 if (room < offset) /* Not enough room */
5156 offset = room; /* Move as far as we can */
5157 if (offset <= 0) /* No room at all, quit. */
5158 return;
5159
5160 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005161 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162
5163 /* shrink other frames: current and at the left or at the right */
5164 if (left)
5165 fr = curfr; /* current frame gets smaller */
5166 else
5167 fr = curfr->fr_next; /* next frame gets smaller */
5168
5169 while (fr != NULL && offset > 0)
5170 {
5171 n = frame_minwidth(fr, NULL);
5172 if (fr->fr_width - offset <= n)
5173 {
5174 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005175 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
5177 else
5178 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005179 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 break;
5181 }
5182 if (left)
5183 fr = fr->fr_prev;
5184 else
5185 fr = fr->fr_next;
5186 }
5187 (void)win_comp_pos();
5188 redraw_all_later(NOT_VALID);
5189}
5190#endif /* FEAT_VERTSPLIT */
5191#endif /* FEAT_MOUSE */
5192
5193#endif /* FEAT_WINDOWS */
5194
5195/*
5196 * Set the height of a window.
5197 * This takes care of the things inside the window, not what happens to the
5198 * window position, the frame or to other windows.
5199 */
5200 static void
5201win_new_height(wp, height)
5202 win_T *wp;
5203 int height;
5204{
5205 linenr_T lnum;
5206 int sline, line_size;
5207#define FRACTION_MULT 16384L
5208
5209 /* Don't want a negative height. Happens when splitting a tiny window.
5210 * Will equalize heights soon to fix it. */
5211 if (height < 0)
5212 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005213 if (wp->w_height == height)
5214 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215
5216 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5217 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5218 + FRACTION_MULT / 2) / (long)wp->w_height;
5219
5220 wp->w_height = height;
5221 wp->w_skipcol = 0;
5222
5223 /* Don't change w_topline when height is zero. Don't set w_topline when
5224 * 'scrollbind' is set and this isn't the current window. */
5225 if (height > 0
5226#ifdef FEAT_SCROLLBIND
5227 && (!wp->w_p_scb || wp == curwin)
5228#endif
5229 )
5230 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005231 /*
5232 * Find a value for w_topline that shows the cursor at the same
5233 * relative position in the window as before (more or less).
5234 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 lnum = wp->w_cursor.lnum;
5236 if (lnum < 1) /* can happen when starting up */
5237 lnum = 1;
5238 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5239 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5240 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005241
5242 if (sline >= 0)
5243 {
5244 /* Make sure the whole cursor line is visible, if possible. */
5245 int rows = plines_win(wp, lnum, FALSE);
5246
5247 if (sline > wp->w_height - rows)
5248 {
5249 sline = wp->w_height - rows;
5250 wp->w_wrow -= rows - line_size;
5251 }
5252 }
5253
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 if (sline < 0)
5255 {
5256 /*
5257 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005258 * Make cursor line the first line in the window. If not enough
5259 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 */
5261 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005262 if (wp->w_wrow >= wp->w_height
5263 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5264 {
5265 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5266 --wp->w_wrow;
5267 while (wp->w_wrow >= wp->w_height)
5268 {
5269 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5270 + win_col_off2(wp);
5271 --wp->w_wrow;
5272 }
5273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 }
5275 else
5276 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005277 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 {
5279#ifdef FEAT_FOLDING
5280 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5281 if (lnum == 1)
5282 {
5283 /* first line in buffer is folded */
5284 line_size = 1;
5285 --sline;
5286 break;
5287 }
5288#endif
5289 --lnum;
5290#ifdef FEAT_DIFF
5291 if (lnum == wp->w_topline)
5292 line_size = plines_win_nofill(wp, lnum, TRUE)
5293 + wp->w_topfill;
5294 else
5295#endif
5296 line_size = plines_win(wp, lnum, TRUE);
5297 sline -= line_size;
5298 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005299
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (sline < 0)
5301 {
5302 /*
5303 * Line we want at top would go off top of screen. Use next
5304 * line instead.
5305 */
5306#ifdef FEAT_FOLDING
5307 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5308#endif
5309 lnum++;
5310 wp->w_wrow -= line_size + sline;
5311 }
5312 else if (sline > 0)
5313 {
5314 /* First line of file reached, use that as topline. */
5315 lnum = 1;
5316 wp->w_wrow -= sline;
5317 }
5318 }
5319 set_topline(wp, lnum);
5320 }
5321
5322 if (wp == curwin)
5323 {
5324 if (p_so)
5325 update_topline();
5326 curs_columns(FALSE); /* validate w_wrow */
5327 }
5328 wp->w_prev_fraction_row = wp->w_wrow;
5329
5330 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005331 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332#ifdef FEAT_WINDOWS
5333 wp->w_redr_status = TRUE;
5334#endif
5335 invalidate_botline_win(wp);
5336}
5337
5338#ifdef FEAT_VERTSPLIT
5339/*
5340 * Set the width of a window.
5341 */
5342 static void
5343win_new_width(wp, width)
5344 win_T *wp;
5345 int width;
5346{
5347 wp->w_width = width;
5348 wp->w_lines_valid = 0;
5349 changed_line_abv_curs_win(wp);
5350 invalidate_botline_win(wp);
5351 if (wp == curwin)
5352 {
5353 update_topline();
5354 curs_columns(TRUE); /* validate w_wrow */
5355 }
5356 redraw_win_later(wp, NOT_VALID);
5357 wp->w_redr_status = TRUE;
5358}
5359#endif
5360
5361 void
5362win_comp_scroll(wp)
5363 win_T *wp;
5364{
5365 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5366 if (wp->w_p_scr == 0)
5367 wp->w_p_scr = 1;
5368}
5369
5370/*
5371 * command_height: called whenever p_ch has been changed
5372 */
5373 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005374command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375{
5376#ifdef FEAT_WINDOWS
5377 int h;
5378 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005379 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005381 /* Use the value of p_ch that we remembered. This is needed for when the
5382 * GUI starts up, we can't be sure in what order things happen. And when
5383 * p_ch was changed in another tab page. */
5384 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005385
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 /* Find bottom frame with width of screen. */
5387 frp = lastwin->w_frame;
5388# ifdef FEAT_VERTSPLIT
5389 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5390 frp = frp->fr_parent;
5391# endif
5392
5393 /* Avoid changing the height of a window with 'winfixheight' set. */
5394 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5395 && frp->fr_win->w_p_wfh)
5396 frp = frp->fr_prev;
5397
5398 if (starting != NO_SCREEN)
5399 {
5400 cmdline_row = Rows - p_ch;
5401
5402 if (p_ch > old_p_ch) /* p_ch got bigger */
5403 {
5404 while (p_ch > old_p_ch)
5405 {
5406 if (frp == NULL)
5407 {
5408 EMSG(_(e_noroom));
5409 p_ch = old_p_ch;
5410 cmdline_row = Rows - p_ch;
5411 break;
5412 }
5413 h = frp->fr_height - frame_minheight(frp, NULL);
5414 if (h > p_ch - old_p_ch)
5415 h = p_ch - old_p_ch;
5416 old_p_ch += h;
5417 frame_add_height(frp, -h);
5418 frp = frp->fr_prev;
5419 }
5420
5421 /* Recompute window positions. */
5422 (void)win_comp_pos();
5423
5424 /* clear the lines added to cmdline */
5425 if (full_screen)
5426 screen_fill((int)(cmdline_row), (int)Rows, 0,
5427 (int)Columns, ' ', ' ', 0);
5428 msg_row = cmdline_row;
5429 redraw_cmdline = TRUE;
5430 return;
5431 }
5432
5433 if (msg_row < cmdline_row)
5434 msg_row = cmdline_row;
5435 redraw_cmdline = TRUE;
5436 }
5437 frame_add_height(frp, (int)(old_p_ch - p_ch));
5438
5439 /* Recompute window positions. */
5440 if (frp != lastwin->w_frame)
5441 (void)win_comp_pos();
5442#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005444 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445#endif
5446}
5447
5448#if defined(FEAT_WINDOWS) || defined(PROTO)
5449/*
5450 * Resize frame "frp" to be "n" lines higher (negative for less high).
5451 * Also resize the frames it is contained in.
5452 */
5453 static void
5454frame_add_height(frp, n)
5455 frame_T *frp;
5456 int n;
5457{
5458 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5459 for (;;)
5460 {
5461 frp = frp->fr_parent;
5462 if (frp == NULL)
5463 break;
5464 frp->fr_height += n;
5465 }
5466}
5467
5468/*
5469 * Add or remove a status line for the bottom window(s), according to the
5470 * value of 'laststatus'.
5471 */
5472 void
5473last_status(morewin)
5474 int morewin; /* pretend there are two or more windows */
5475{
5476 /* Don't make a difference between horizontal or vertical split. */
5477 last_status_rec(topframe, (p_ls == 2
5478 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5479}
5480
5481 static void
5482last_status_rec(fr, statusline)
5483 frame_T *fr;
5484 int statusline;
5485{
5486 frame_T *fp;
5487 win_T *wp;
5488
5489 if (fr->fr_layout == FR_LEAF)
5490 {
5491 wp = fr->fr_win;
5492 if (wp->w_status_height != 0 && !statusline)
5493 {
5494 /* remove status line */
5495 win_new_height(wp, wp->w_height + 1);
5496 wp->w_status_height = 0;
5497 comp_col();
5498 }
5499 else if (wp->w_status_height == 0 && statusline)
5500 {
5501 /* Find a frame to take a line from. */
5502 fp = fr;
5503 while (fp->fr_height <= frame_minheight(fp, NULL))
5504 {
5505 if (fp == topframe)
5506 {
5507 EMSG(_(e_noroom));
5508 return;
5509 }
5510 /* In a column of frames: go to frame above. If already at
5511 * the top or in a row of frames: go to parent. */
5512 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5513 fp = fp->fr_prev;
5514 else
5515 fp = fp->fr_parent;
5516 }
5517 wp->w_status_height = 1;
5518 if (fp != fr)
5519 {
5520 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5521 frame_fix_height(wp);
5522 (void)win_comp_pos();
5523 }
5524 else
5525 win_new_height(wp, wp->w_height - 1);
5526 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005527 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 }
5529 }
5530#ifdef FEAT_VERTSPLIT
5531 else if (fr->fr_layout == FR_ROW)
5532 {
5533 /* vertically split windows, set status line for each one */
5534 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5535 last_status_rec(fp, statusline);
5536 }
5537#endif
5538 else
5539 {
5540 /* horizontally split window, set status line for last one */
5541 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5542 ;
5543 last_status_rec(fp, statusline);
5544 }
5545}
5546
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005547/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005548 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005549 */
5550 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005551tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005552{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005553#ifdef FEAT_GUI_TABLINE
5554 /* When the GUI has the tabline then this always returns zero. */
5555 if (gui_use_tabline())
5556 return 0;
5557#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005558 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005559 {
5560 case 0: return 0;
5561 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5562 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005563 return 1;
5564}
5565
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566#endif /* FEAT_WINDOWS */
5567
5568#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5569/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005570 * Get the file name at the cursor.
5571 * If Visual mode is active, use the selected text if it's in one line.
5572 * Returns the name in allocated memory, NULL for failure.
5573 */
5574 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005575grab_file_name(count, file_lnum)
5576 long count;
5577 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005578{
5579# ifdef FEAT_VISUAL
5580 if (VIsual_active)
5581 {
5582 int len;
5583 char_u *ptr;
5584
5585 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5586 return NULL;
5587 return find_file_name_in_path(ptr, len,
5588 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5589 }
5590# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005591 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5592 file_lnum);
5593
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005594}
5595
5596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 * Return the file name under or after the cursor.
5598 *
5599 * The 'path' option is searched if the file name is not absolute.
5600 * The string returned has been alloc'ed and should be freed by the caller.
5601 * NULL is returned if the file name or file is not found.
5602 *
5603 * options:
5604 * FNAME_MESS give error messages
5605 * FNAME_EXP expand to path
5606 * FNAME_HYP check for hypertext link
5607 * FNAME_INCL apply "includeexpr"
5608 */
5609 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005610file_name_at_cursor(options, count, file_lnum)
5611 int options;
5612 long count;
5613 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614{
5615 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005616 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5617 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618}
5619
5620/*
5621 * Return the name of the file under or after ptr[col].
5622 * Otherwise like file_name_at_cursor().
5623 */
5624 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005625file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 char_u *line;
5627 int col;
5628 int options;
5629 long count;
5630 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005631 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632{
5633 char_u *ptr;
5634 int len;
5635
5636 /*
5637 * search forward for what could be the start of a file name
5638 */
5639 ptr = line + col;
5640 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005641 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 if (*ptr == NUL) /* nothing found */
5643 {
5644 if (options & FNAME_MESS)
5645 EMSG(_("E446: No file name under cursor"));
5646 return NULL;
5647 }
5648
5649 /*
5650 * Search backward for first char of the file name.
5651 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5652 */
5653 while (ptr > line)
5654 {
5655#ifdef FEAT_MBYTE
5656 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5657 ptr -= len + 1;
5658 else
5659#endif
5660 if (vim_isfilec(ptr[-1])
5661 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5662 --ptr;
5663 else
5664 break;
5665 }
5666
5667 /*
5668 * Search forward for the last char of the file name.
5669 * Also allow "://" when ':' is not in 'isfname'.
5670 */
5671 len = 0;
5672 while (vim_isfilec(ptr[len])
5673 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5674#ifdef FEAT_MBYTE
5675 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005676 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 else
5678#endif
5679 ++len;
5680
5681 /*
5682 * If there is trailing punctuation, remove it.
5683 * But don't remove "..", could be a directory name.
5684 */
5685 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5686 && ptr[len - 2] != '.')
5687 --len;
5688
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005689 if (file_lnum != NULL)
5690 {
5691 char_u *p;
5692
5693 /* Get the number after the file name and a separator character */
5694 p = ptr + len;
5695 p = skipwhite(p);
5696 if (*p != NUL)
5697 {
5698 if (!isdigit(*p))
5699 ++p; /* skip the separator */
5700 p = skipwhite(p);
5701 if (isdigit(*p))
5702 *file_lnum = (int)getdigits(&p);
5703 }
5704 }
5705
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5707}
5708
5709# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5710static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5711
5712 static char_u *
5713eval_includeexpr(ptr, len)
5714 char_u *ptr;
5715 int len;
5716{
5717 char_u *res;
5718
5719 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005720 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005721 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 set_vim_var_string(VV_FNAME, NULL, 0);
5723 return res;
5724}
5725#endif
5726
5727/*
5728 * Return the name of the file ptr[len] in 'path'.
5729 * Otherwise like file_name_at_cursor().
5730 */
5731 char_u *
5732find_file_name_in_path(ptr, len, options, count, rel_fname)
5733 char_u *ptr;
5734 int len;
5735 int options;
5736 long count;
5737 char_u *rel_fname; /* file we are searching relative to */
5738{
5739 char_u *file_name;
5740 int c;
5741# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5742 char_u *tofree = NULL;
5743
5744 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5745 {
5746 tofree = eval_includeexpr(ptr, len);
5747 if (tofree != NULL)
5748 {
5749 ptr = tofree;
5750 len = (int)STRLEN(ptr);
5751 }
5752 }
5753# endif
5754
5755 if (options & FNAME_EXP)
5756 {
5757 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5758 TRUE, rel_fname);
5759
5760# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5761 /*
5762 * If the file could not be found in a normal way, try applying
5763 * 'includeexpr' (unless done already).
5764 */
5765 if (file_name == NULL
5766 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5767 {
5768 tofree = eval_includeexpr(ptr, len);
5769 if (tofree != NULL)
5770 {
5771 ptr = tofree;
5772 len = (int)STRLEN(ptr);
5773 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5774 TRUE, rel_fname);
5775 }
5776 }
5777# endif
5778 if (file_name == NULL && (options & FNAME_MESS))
5779 {
5780 c = ptr[len];
5781 ptr[len] = NUL;
5782 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5783 ptr[len] = c;
5784 }
5785
5786 /* Repeat finding the file "count" times. This matters when it
5787 * appears several times in the path. */
5788 while (file_name != NULL && --count > 0)
5789 {
5790 vim_free(file_name);
5791 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5792 }
5793 }
5794 else
5795 file_name = vim_strnsave(ptr, len);
5796
5797# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5798 vim_free(tofree);
5799# endif
5800
5801 return file_name;
5802}
5803#endif /* FEAT_SEARCHPATH */
5804
5805/*
5806 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5807 * Also check for ":\\", which MS Internet Explorer accepts, return
5808 * URL_BACKSLASH.
5809 */
5810 static int
5811path_is_url(p)
5812 char_u *p;
5813{
5814 if (STRNCMP(p, "://", (size_t)3) == 0)
5815 return URL_SLASH;
5816 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5817 return URL_BACKSLASH;
5818 return 0;
5819}
5820
5821/*
5822 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5823 * Return URL_BACKSLASH for "name:\\".
5824 * Return zero otherwise.
5825 */
5826 int
5827path_with_url(fname)
5828 char_u *fname;
5829{
5830 char_u *p;
5831
5832 for (p = fname; isalpha(*p); ++p)
5833 ;
5834 return path_is_url(p);
5835}
5836
5837/*
5838 * Return TRUE if "name" is a full (absolute) path name or URL.
5839 */
5840 int
5841vim_isAbsName(name)
5842 char_u *name;
5843{
5844 return (path_with_url(name) != 0 || mch_isFullName(name));
5845}
5846
5847/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005848 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 *
5850 * return FAIL for failure, OK otherwise
5851 */
5852 int
5853vim_FullName(fname, buf, len, force)
5854 char_u *fname, *buf;
5855 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005856 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857{
5858 int retval = OK;
5859 int url;
5860
5861 *buf = NUL;
5862 if (fname == NULL)
5863 return FAIL;
5864
5865 url = path_with_url(fname);
5866 if (!url)
5867 retval = mch_FullName(fname, buf, len, force);
5868 if (url || retval == FAIL)
5869 {
5870 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005871 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 }
5873#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5874 slash_adjust(buf);
5875#endif
5876 return retval;
5877}
5878
5879/*
5880 * Return the minimal number of rows that is needed on the screen to display
5881 * the current number of windows.
5882 */
5883 int
5884min_rows()
5885{
5886 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005887#ifdef FEAT_WINDOWS
5888 tabpage_T *tp;
5889 int n;
5890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891
5892 if (firstwin == NULL) /* not initialized yet */
5893 return MIN_LINES;
5894
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005896 total = 0;
5897 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5898 {
5899 n = frame_minheight(tp->tp_topframe, NULL);
5900 if (total < n)
5901 total = n;
5902 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005903 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005905 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005907 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 return total;
5909}
5910
5911/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005912 * Return TRUE if there is only one window (in the current tab page), not
5913 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 */
5915 int
5916only_one_window()
5917{
5918#ifdef FEAT_WINDOWS
5919 int count = 0;
5920 win_T *wp;
5921
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005922 /* If there is another tab page there always is another window. */
5923 if (first_tabpage->tp_next != NULL)
5924 return FALSE;
5925
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005927 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928# ifdef FEAT_QUICKFIX
5929 || wp->w_p_pvw
5930# endif
5931 ) || wp == curwin)
5932 ++count;
5933 return (count <= 1);
5934#else
5935 return TRUE;
5936#endif
5937}
5938
5939#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5940/*
5941 * Correct the cursor line number in other windows. Used after changing the
5942 * current buffer, and before applying autocommands.
5943 * When "do_curwin" is TRUE, also check current window.
5944 */
5945 void
5946check_lnums(do_curwin)
5947 int do_curwin;
5948{
5949 win_T *wp;
5950
5951#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005952 tabpage_T *tp;
5953
5954 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5956#else
5957 wp = curwin;
5958 if (do_curwin)
5959#endif
5960 {
5961 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5962 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5963 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5964 wp->w_topline = curbuf->b_ml.ml_line_count;
5965 }
5966}
5967#endif
5968
5969#if defined(FEAT_WINDOWS) || defined(PROTO)
5970
5971/*
5972 * A snapshot of the window sizes, to restore them after closing the help
5973 * window.
5974 * Only these fields are used:
5975 * fr_layout
5976 * fr_width
5977 * fr_height
5978 * fr_next
5979 * fr_child
5980 * fr_win (only valid for the old curwin, NULL otherwise)
5981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982
5983/*
5984 * Create a snapshot of the current frame sizes.
5985 */
5986 static void
5987make_snapshot()
5988{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005989 clear_snapshot(curtab);
5990 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991}
5992
5993 static void
5994make_snapshot_rec(fr, frp)
5995 frame_T *fr;
5996 frame_T **frp;
5997{
5998 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5999 if (*frp == NULL)
6000 return;
6001 (*frp)->fr_layout = fr->fr_layout;
6002# ifdef FEAT_VERTSPLIT
6003 (*frp)->fr_width = fr->fr_width;
6004# endif
6005 (*frp)->fr_height = fr->fr_height;
6006 if (fr->fr_next != NULL)
6007 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6008 if (fr->fr_child != NULL)
6009 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6010 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6011 (*frp)->fr_win = curwin;
6012}
6013
6014/*
6015 * Remove any existing snapshot.
6016 */
6017 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006018clear_snapshot(tp)
6019 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006021 clear_snapshot_rec(tp->tp_snapshot);
6022 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023}
6024
6025 static void
6026clear_snapshot_rec(fr)
6027 frame_T *fr;
6028{
6029 if (fr != NULL)
6030 {
6031 clear_snapshot_rec(fr->fr_next);
6032 clear_snapshot_rec(fr->fr_child);
6033 vim_free(fr);
6034 }
6035}
6036
6037/*
6038 * Restore a previously created snapshot, if there is any.
6039 * This is only done if the screen size didn't change and the window layout is
6040 * still the same.
6041 */
6042 static void
6043restore_snapshot(close_curwin)
6044 int close_curwin; /* closing current window */
6045{
6046 win_T *wp;
6047
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006048 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006050 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006052 && curtab->tp_snapshot->fr_height == topframe->fr_height
6053 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006055 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 win_comp_pos();
6057 if (wp != NULL && close_curwin)
6058 win_goto(wp);
6059 redraw_all_later(CLEAR);
6060 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006061 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062}
6063
6064/*
6065 * Check if frames "sn" and "fr" have the same layout, same following frames
6066 * and same children.
6067 */
6068 static int
6069check_snapshot_rec(sn, fr)
6070 frame_T *sn;
6071 frame_T *fr;
6072{
6073 if (sn->fr_layout != fr->fr_layout
6074 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6075 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6076 || (sn->fr_next != NULL
6077 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6078 || (sn->fr_child != NULL
6079 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6080 return FAIL;
6081 return OK;
6082}
6083
6084/*
6085 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6086 * following frames and children.
6087 * Returns a pointer to the old current window, or NULL.
6088 */
6089 static win_T *
6090restore_snapshot_rec(sn, fr)
6091 frame_T *sn;
6092 frame_T *fr;
6093{
6094 win_T *wp = NULL;
6095 win_T *wp2;
6096
6097 fr->fr_height = sn->fr_height;
6098# ifdef FEAT_VERTSPLIT
6099 fr->fr_width = sn->fr_width;
6100# endif
6101 if (fr->fr_layout == FR_LEAF)
6102 {
6103 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6104# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006105 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106# endif
6107 wp = sn->fr_win;
6108 }
6109 if (sn->fr_next != NULL)
6110 {
6111 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6112 if (wp2 != NULL)
6113 wp = wp2;
6114 }
6115 if (sn->fr_child != NULL)
6116 {
6117 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6118 if (wp2 != NULL)
6119 wp = wp2;
6120 }
6121 return wp;
6122}
6123
6124#endif
6125
6126#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6127/*
6128 * Return TRUE if there is any vertically split window.
6129 */
6130 int
6131win_hasvertsplit()
6132{
6133 frame_T *fr;
6134
6135 if (topframe->fr_layout == FR_ROW)
6136 return TRUE;
6137
6138 if (topframe->fr_layout == FR_COL)
6139 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6140 if (fr->fr_layout == FR_ROW)
6141 return TRUE;
6142
6143 return FALSE;
6144}
6145#endif