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