blob: 0300b559004d962e2f0cf226f470eadc351fa55e [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#include "vim.h"
11
12#ifdef HAVE_FCNTL_H
13# include <fcntl.h> /* for chdir() */
14#endif
15
16static int path_is_url __ARGS((char_u *p));
17#if defined(FEAT_WINDOWS) || defined(PROTO)
18static int win_split_ins __ARGS((int size, int flags, win_T *newwin, int dir));
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000019static void win_init __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
21static void frame_setheight __ARGS((frame_T *curfrp, int height));
22#ifdef FEAT_VERTSPLIT
23static void frame_setwidth __ARGS((frame_T *curfrp, int width));
24#endif
25static void win_exchange __ARGS((long));
26static void win_rotate __ARGS((int, int));
27static void win_totop __ARGS((int size, int flags));
28static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000029static int last_window __ARGS((void));
Bram Moolenaarf740b292006-02-16 22:11:02 +000030static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
31static win_T *winframe_remove __ARGS((win_T *win, int *dirp, tabpage_T *tp));
32static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000033static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000034static win_T *frame2win __ARGS((frame_T *frp));
35static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
36static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
37static int frame_fixed_height __ARGS((frame_T *frp));
38#ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000039static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000040static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000041static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000042static void frame_add_vsep __ARGS((frame_T *frp));
43static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
44static void frame_fix_width __ARGS((win_T *wp));
45#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000046#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000047static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000048#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000049static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000050static int leave_tabpage __ARGS((buf_T *new_curbuf));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000051static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000052static void frame_fix_height __ARGS((win_T *wp));
53static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
54static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
Bram Moolenaarf740b292006-02-16 22:11:02 +000055static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000056static void win_append __ARGS((win_T *, win_T *));
Bram Moolenaarf740b292006-02-16 22:11:02 +000057static void win_remove __ARGS((win_T *, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static void frame_append __ARGS((frame_T *after, frame_T *frp));
59static void frame_insert __ARGS((frame_T *before, frame_T *frp));
60static void frame_remove __ARGS((frame_T *frp));
61#ifdef FEAT_VERTSPLIT
62static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000063static void win_goto_ver __ARGS((int up, long count));
64static void win_goto_hor __ARGS((int left, long count));
65#endif
66static void frame_add_height __ARGS((frame_T *frp, int n));
67static void last_status_rec __ARGS((frame_T *fr, int statusline));
68
69static void make_snapshot __ARGS((void));
70static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000071static void clear_snapshot __ARGS((tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000072static void clear_snapshot_rec __ARGS((frame_T *fr));
73static void restore_snapshot __ARGS((int close_curwin));
74static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
75static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
76
77#endif /* FEAT_WINDOWS */
78static win_T *win_alloc __ARGS((win_T *after));
79static void win_new_height __ARGS((win_T *, int));
80
81#define URL_SLASH 1 /* path_is_url() has found "://" */
82#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
83
84#define NOWIN (win_T *)-1 /* non-exisiting window */
85
Bram Moolenaar05159a02005-02-26 23:04:13 +000086#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000087# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000088#else
89# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000090#endif
91
Bram Moolenaar071d4272004-06-13 20:20:40 +000092#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000093
94static char *m_onlyone = N_("Already only one window");
95
Bram Moolenaar071d4272004-06-13 20:20:40 +000096/*
97 * all CTRL-W window commands are handled here, called from normal_cmd().
98 */
99 void
100do_window(nchar, Prenum, xchar)
101 int nchar;
102 long Prenum;
103 int xchar; /* extra char from ":wincmd gx" or NUL */
104{
105 long Prenum1;
106 win_T *wp;
107#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
108 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000109 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110#endif
111#ifdef FEAT_FIND_ID
112 int type = FIND_DEFINE;
113 int len;
114#endif
115 char_u cbuf[40];
116
117 if (Prenum == 0)
118 Prenum1 = 1;
119 else
120 Prenum1 = Prenum;
121
122#ifdef FEAT_CMDWIN
123# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
124#else
125# define CHECK_CMDWIN
126#endif
127
128 switch (nchar)
129 {
130/* split current window in two parts, horizontally */
131 case 'S':
132 case Ctrl_S:
133 case 's':
134 CHECK_CMDWIN
135#ifdef FEAT_VISUAL
136 reset_VIsual_and_resel(); /* stop Visual mode */
137#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000138#ifdef FEAT_QUICKFIX
139 /* When splitting the quickfix window open a new buffer in it,
140 * don't replicate the quickfix buffer. */
141 if (bt_quickfix(curbuf))
142 goto newwindow;
143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#ifdef FEAT_GUI
145 need_mouse_correct = TRUE;
146#endif
147 win_split((int)Prenum, 0);
148 break;
149
150#ifdef FEAT_VERTSPLIT
151/* split current window in two parts, vertically */
152 case Ctrl_V:
153 case 'v':
154 CHECK_CMDWIN
155#ifdef FEAT_VISUAL
156 reset_VIsual_and_resel(); /* stop Visual mode */
157#endif
158#ifdef FEAT_GUI
159 need_mouse_correct = TRUE;
160#endif
161 win_split((int)Prenum, WSP_VERT);
162 break;
163#endif
164
165/* split current window and edit alternate file */
166 case Ctrl_HAT:
167 case '^':
168 CHECK_CMDWIN
169#ifdef FEAT_VISUAL
170 reset_VIsual_and_resel(); /* stop Visual mode */
171#endif
172 STRCPY(cbuf, "split #");
173 if (Prenum)
174 sprintf((char *)cbuf + 7, "%ld", Prenum);
175 do_cmdline_cmd(cbuf);
176 break;
177
178/* open new window */
179 case Ctrl_N:
180 case 'n':
181 CHECK_CMDWIN
182#ifdef FEAT_VISUAL
183 reset_VIsual_and_resel(); /* stop Visual mode */
184#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000185#ifdef FEAT_QUICKFIX
186newwindow:
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 if (Prenum)
189 sprintf((char *)cbuf, "%ld", Prenum); /* window height */
190 else
191 cbuf[0] = NUL;
192 STRCAT(cbuf, "new");
193 do_cmdline_cmd(cbuf);
194 break;
195
196/* quit current window */
197 case Ctrl_Q:
198 case 'q':
199#ifdef FEAT_VISUAL
200 reset_VIsual_and_resel(); /* stop Visual mode */
201#endif
202 do_cmdline_cmd((char_u *)"quit");
203 break;
204
205/* close current window */
206 case Ctrl_C:
207 case 'c':
208#ifdef FEAT_VISUAL
209 reset_VIsual_and_resel(); /* stop Visual mode */
210#endif
211 do_cmdline_cmd((char_u *)"close");
212 break;
213
214#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
215/* close preview window */
216 case Ctrl_Z:
217 case 'z':
218 CHECK_CMDWIN
219#ifdef FEAT_VISUAL
220 reset_VIsual_and_resel(); /* stop Visual mode */
221#endif
222 do_cmdline_cmd((char_u *)"pclose");
223 break;
224
225/* cursor to preview window */
226 case 'P':
227 for (wp = firstwin; wp != NULL; wp = wp->w_next)
228 if (wp->w_p_pvw)
229 break;
230 if (wp == NULL)
231 EMSG(_("E441: There is no preview window"));
232 else
233 win_goto(wp);
234 break;
235#endif
236
237/* close all but current window */
238 case Ctrl_O:
239 case 'o':
240 CHECK_CMDWIN
241#ifdef FEAT_VISUAL
242 reset_VIsual_and_resel(); /* stop Visual mode */
243#endif
244 do_cmdline_cmd((char_u *)"only");
245 break;
246
247/* cursor to next window with wrap around */
248 case Ctrl_W:
249 case 'w':
250/* cursor to previous window with wrap around */
251 case 'W':
252 CHECK_CMDWIN
253 if (lastwin == firstwin && Prenum != 1) /* just one window */
254 beep_flush();
255 else
256 {
257 if (Prenum) /* go to specified window */
258 {
259 for (wp = firstwin; --Prenum > 0; )
260 {
261 if (wp->w_next == NULL)
262 break;
263 else
264 wp = wp->w_next;
265 }
266 }
267 else
268 {
269 if (nchar == 'W') /* go to previous window */
270 {
271 wp = curwin->w_prev;
272 if (wp == NULL)
273 wp = lastwin; /* wrap around */
274 }
275 else /* go to next window */
276 {
277 wp = curwin->w_next;
278 if (wp == NULL)
279 wp = firstwin; /* wrap around */
280 }
281 }
282 win_goto(wp);
283 }
284 break;
285
286/* cursor to window below */
287 case 'j':
288 case K_DOWN:
289 case Ctrl_J:
290 CHECK_CMDWIN
291#ifdef FEAT_VERTSPLIT
292 win_goto_ver(FALSE, Prenum1);
293#else
294 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
295 wp = wp->w_next)
296 ;
297 win_goto(wp);
298#endif
299 break;
300
301/* cursor to window above */
302 case 'k':
303 case K_UP:
304 case Ctrl_K:
305 CHECK_CMDWIN
306#ifdef FEAT_VERTSPLIT
307 win_goto_ver(TRUE, Prenum1);
308#else
309 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
310 wp = wp->w_prev)
311 ;
312 win_goto(wp);
313#endif
314 break;
315
316#ifdef FEAT_VERTSPLIT
317/* cursor to left window */
318 case 'h':
319 case K_LEFT:
320 case Ctrl_H:
321 case K_BS:
322 CHECK_CMDWIN
323 win_goto_hor(TRUE, Prenum1);
324 break;
325
326/* cursor to right window */
327 case 'l':
328 case K_RIGHT:
329 case Ctrl_L:
330 CHECK_CMDWIN
331 win_goto_hor(FALSE, Prenum1);
332 break;
333#endif
334
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000335/* move window to new tab page */
336 case 'T':
337 if (firstwin == lastwin)
338 MSG(_(m_onlyone));
339 else
340 {
341 tabpage_T *oldtab = curtab;
342 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000343
344 /* First create a new tab with the window, then go back to
345 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000346 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000347 if (win_new_tabpage((int)Prenum) == OK
348 && valid_tabpage(oldtab))
349 {
350 newtab = curtab;
351 goto_tabpage_tp(oldtab);
352 if (curwin == wp)
353 win_close(curwin, FALSE);
354 if (valid_tabpage(newtab))
355 goto_tabpage_tp(newtab);
356 }
357 }
358 break;
359
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360/* cursor to top-left window */
361 case 't':
362 case Ctrl_T:
363 win_goto(firstwin);
364 break;
365
366/* cursor to bottom-right window */
367 case 'b':
368 case Ctrl_B:
369 win_goto(lastwin);
370 break;
371
372/* cursor to last accessed (previous) window */
373 case 'p':
374 case Ctrl_P:
375 if (prevwin == NULL)
376 beep_flush();
377 else
378 win_goto(prevwin);
379 break;
380
381/* exchange current and next window */
382 case 'x':
383 case Ctrl_X:
384 CHECK_CMDWIN
385 win_exchange(Prenum);
386 break;
387
388/* rotate windows downwards */
389 case Ctrl_R:
390 case 'r':
391 CHECK_CMDWIN
392#ifdef FEAT_VISUAL
393 reset_VIsual_and_resel(); /* stop Visual mode */
394#endif
395 win_rotate(FALSE, (int)Prenum1); /* downwards */
396 break;
397
398/* rotate windows upwards */
399 case 'R':
400 CHECK_CMDWIN
401#ifdef FEAT_VISUAL
402 reset_VIsual_and_resel(); /* stop Visual mode */
403#endif
404 win_rotate(TRUE, (int)Prenum1); /* upwards */
405 break;
406
407/* move window to the very top/bottom/left/right */
408 case 'K':
409 case 'J':
410#ifdef FEAT_VERTSPLIT
411 case 'H':
412 case 'L':
413#endif
414 CHECK_CMDWIN
415 win_totop((int)Prenum,
416 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
417 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
418 break;
419
420/* make all windows the same height */
421 case '=':
422#ifdef FEAT_GUI
423 need_mouse_correct = TRUE;
424#endif
425 win_equal(NULL, FALSE, 'b');
426 break;
427
428/* increase current window height */
429 case '+':
430#ifdef FEAT_GUI
431 need_mouse_correct = TRUE;
432#endif
433 win_setheight(curwin->w_height + (int)Prenum1);
434 break;
435
436/* decrease current window height */
437 case '-':
438#ifdef FEAT_GUI
439 need_mouse_correct = TRUE;
440#endif
441 win_setheight(curwin->w_height - (int)Prenum1);
442 break;
443
444/* set current window height */
445 case Ctrl__:
446 case '_':
447#ifdef FEAT_GUI
448 need_mouse_correct = TRUE;
449#endif
450 win_setheight(Prenum ? (int)Prenum : 9999);
451 break;
452
453#ifdef FEAT_VERTSPLIT
454/* increase current window width */
455 case '>':
456#ifdef FEAT_GUI
457 need_mouse_correct = TRUE;
458#endif
459 win_setwidth(curwin->w_width + (int)Prenum1);
460 break;
461
462/* decrease current window width */
463 case '<':
464#ifdef FEAT_GUI
465 need_mouse_correct = TRUE;
466#endif
467 win_setwidth(curwin->w_width - (int)Prenum1);
468 break;
469
470/* set current window width */
471 case '|':
472#ifdef FEAT_GUI
473 need_mouse_correct = TRUE;
474#endif
475 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
476 break;
477#endif
478
479/* jump to tag and split window if tag exists (in preview window) */
480#if defined(FEAT_QUICKFIX)
481 case '}':
482 CHECK_CMDWIN
483 if (Prenum)
484 g_do_tagpreview = Prenum;
485 else
486 g_do_tagpreview = p_pvh;
487 /*FALLTHROUGH*/
488#endif
489 case ']':
490 case Ctrl_RSB:
491 CHECK_CMDWIN
492#ifdef FEAT_VISUAL
493 reset_VIsual_and_resel(); /* stop Visual mode */
494#endif
495 if (Prenum)
496 postponed_split = Prenum;
497 else
498 postponed_split = -1;
499
500 /* Execute the command right here, required when
501 * "wincmd ]" was used in a function. */
502 do_nv_ident(Ctrl_RSB, NUL);
503 break;
504
505#ifdef FEAT_SEARCHPATH
506/* edit file name under cursor in a new window */
507 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000508 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000510wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000512
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000513 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (ptr != NULL)
515 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000516# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 setpcmark();
520 if (win_split(0, 0) == OK)
521 {
522# ifdef FEAT_SCROLLBIND
523 curwin->w_p_scb = FALSE;
524# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000525 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE);
526 if (nchar == 'F' && lnum >= 0)
527 {
528 curwin->w_cursor.lnum = lnum;
529 check_cursor_lnum();
530 beginline(BL_SOL | BL_FIX);
531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 }
533 vim_free(ptr);
534 }
535 break;
536#endif
537
538#ifdef FEAT_FIND_ID
539/* Go to the first occurence of the identifier under cursor along path in a
540 * new window -- webb
541 */
542 case 'i': /* Go to any match */
543 case Ctrl_I:
544 type = FIND_ANY;
545 /* FALLTHROUGH */
546 case 'd': /* Go to definition, using 'define' */
547 case Ctrl_D:
548 CHECK_CMDWIN
549 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
550 break;
551 find_pattern_in_path(ptr, 0, len, TRUE,
552 Prenum == 0 ? TRUE : FALSE, type,
553 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
554 curwin->w_set_curswant = TRUE;
555 break;
556#endif
557
Bram Moolenaar05159a02005-02-26 23:04:13 +0000558 case K_KENTER:
559 case CAR:
560#if defined(FEAT_QUICKFIX)
561 /*
562 * In a quickfix window a <CR> jumps to the error under the
563 * cursor in a new window.
564 */
565 if (bt_quickfix(curbuf))
566 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000567 sprintf((char *)cbuf, "split +%ld%s",
568 (long)curwin->w_cursor.lnum,
569 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000570 do_cmdline_cmd(cbuf);
571 }
572#endif
573 break;
574
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576/* CTRL-W g extended commands */
577 case 'g':
578 case Ctrl_G:
579 CHECK_CMDWIN
580#ifdef USE_ON_FLY_SCROLL
581 dont_scroll = TRUE; /* disallow scrolling here */
582#endif
583 ++no_mapping;
584 ++allow_keys; /* no mapping for xchar, but allow key codes */
585 if (xchar == NUL)
586 xchar = safe_vgetc();
587#ifdef FEAT_LANGMAP
588 LANGMAP_ADJUST(xchar, TRUE);
589#endif
590 --no_mapping;
591 --allow_keys;
592#ifdef FEAT_CMDL_INFO
593 (void)add_to_showcmd(xchar);
594#endif
595 switch (xchar)
596 {
597#if defined(FEAT_QUICKFIX)
598 case '}':
599 xchar = Ctrl_RSB;
600 if (Prenum)
601 g_do_tagpreview = Prenum;
602 else
603 g_do_tagpreview = p_pvh;
604 /*FALLTHROUGH*/
605#endif
606 case ']':
607 case Ctrl_RSB:
608#ifdef FEAT_VISUAL
609 reset_VIsual_and_resel(); /* stop Visual mode */
610#endif
611 if (Prenum)
612 postponed_split = Prenum;
613 else
614 postponed_split = -1;
615
616 /* Execute the command right here, required when
617 * "wincmd g}" was used in a function. */
618 do_nv_ident('g', xchar);
619 break;
620
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000621#ifdef FEAT_SEARCHPATH
622 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000623 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000624 cmdmod.tab = TRUE;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000625 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000626 goto wingotofile;
627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 default:
629 beep_flush();
630 break;
631 }
632 break;
633
634 default: beep_flush();
635 break;
636 }
637}
638
639/*
640 * split the current window, implements CTRL-W s and :split
641 *
642 * "size" is the height or width for the new window, 0 to use half of current
643 * height or width.
644 *
645 * "flags":
646 * WSP_ROOM: require enough room for new window
647 * WSP_VERT: vertical split.
648 * WSP_TOP: open window at the top-left of the shell (help window).
649 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
650 * WSP_HELP: creating the help window, keep layout snapshot
651 *
652 * return FAIL for failure, OK otherwise
653 */
654 int
655win_split(size, flags)
656 int size;
657 int flags;
658{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000659 /* When the ":tab" modifier was used open a new tab page instead. */
660 if (may_open_tabpage() == OK)
661 return OK;
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 /* Add flags from ":vertical", ":topleft" and ":botright". */
664 flags |= cmdmod.split;
665 if ((flags & WSP_TOP) && (flags & WSP_BOT))
666 {
667 EMSG(_("E442: Can't split topleft and botright at the same time"));
668 return FAIL;
669 }
670
671 /* When creating the help window make a snapshot of the window layout.
672 * Otherwise clear the snapshot, it's now invalid. */
673 if (flags & WSP_HELP)
674 make_snapshot();
675 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000676 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678 return win_split_ins(size, flags, NULL, 0);
679}
680
681/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000682 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 * When "newwin" is not NULL: insert this window at the far
684 * top/left/right/bottom.
685 * return FAIL for failure, OK otherwise
686 */
687 static int
688win_split_ins(size, flags, newwin, dir)
689 int size;
690 int flags;
691 win_T *newwin;
692 int dir;
693{
694 win_T *wp = newwin;
695 win_T *oldwin;
696 int new_size = size;
697 int i;
698 int need_status = 0;
699 int do_equal = FALSE;
700 int needed;
701 int available;
702 int oldwin_height = 0;
703 int layout;
704 frame_T *frp, *curfrp;
705 int before;
706
707 if (flags & WSP_TOP)
708 oldwin = firstwin;
709 else if (flags & WSP_BOT)
710 oldwin = lastwin;
711 else
712 oldwin = curwin;
713
714 /* add a status line when p_ls == 1 and splitting the first window */
715 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
716 {
717 if (oldwin->w_height <= p_wmh && newwin == NULL)
718 {
719 EMSG(_(e_noroom));
720 return FAIL;
721 }
722 need_status = STATUS_HEIGHT;
723 }
724
725#ifdef FEAT_VERTSPLIT
726 if (flags & WSP_VERT)
727 {
728 layout = FR_ROW;
729 do_equal = (p_ea && new_size == 0 && *p_ead != 'v');
730
731 /*
732 * Check if we are able to split the current window and compute its
733 * width.
734 */
735 needed = p_wmw + 1;
736 if (flags & WSP_ROOM)
737 needed += p_wiw - p_wmw;
738 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
739 {
740 available = topframe->fr_width;
741 needed += frame_minwidth(topframe, NULL);
742 }
743 else
744 available = oldwin->w_width;
745 if (available < needed && newwin == NULL)
746 {
747 EMSG(_(e_noroom));
748 return FAIL;
749 }
750 if (new_size == 0)
751 new_size = oldwin->w_width / 2;
752 if (new_size > oldwin->w_width - p_wmw - 1)
753 new_size = oldwin->w_width - p_wmw - 1;
754 if (new_size < p_wmw)
755 new_size = p_wmw;
756
757 /* if it doesn't fit in the current window, need win_equal() */
758 if (oldwin->w_width - new_size - 1 < p_wmw)
759 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000760
761 /* We don't like to take lines for the new window from a
762 * 'winfixwidth' window. Take them from a window to the left or right
763 * instead, if possible. */
764 if (oldwin->w_p_wfw)
765 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 }
767 else
768#endif
769 {
770 layout = FR_COL;
771 do_equal = (p_ea && new_size == 0
772#ifdef FEAT_VERTSPLIT
773 && *p_ead != 'h'
774#endif
775 );
776
777 /*
778 * Check if we are able to split the current window and compute its
779 * height.
780 */
781 needed = p_wmh + STATUS_HEIGHT + need_status;
782 if (flags & WSP_ROOM)
783 needed += p_wh - p_wmh;
784 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
785 {
786 available = topframe->fr_height;
787 needed += frame_minheight(topframe, NULL);
788 }
789 else
790 {
791 available = oldwin->w_height;
792 needed += p_wmh;
793 }
794 if (available < needed && newwin == NULL)
795 {
796 EMSG(_(e_noroom));
797 return FAIL;
798 }
799 oldwin_height = oldwin->w_height;
800 if (need_status)
801 {
802 oldwin->w_status_height = STATUS_HEIGHT;
803 oldwin_height -= STATUS_HEIGHT;
804 }
805 if (new_size == 0)
806 new_size = oldwin_height / 2;
807
808 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
809 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
810 if (new_size < p_wmh)
811 new_size = p_wmh;
812
813 /* if it doesn't fit in the current window, need win_equal() */
814 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
815 do_equal = TRUE;
816
817 /* We don't like to take lines for the new window from a
818 * 'winfixheight' window. Take them from a window above or below
819 * instead, if possible. */
820 if (oldwin->w_p_wfh)
821 {
822 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
823 oldwin);
824 oldwin_height = oldwin->w_height;
825 if (need_status)
826 oldwin_height -= STATUS_HEIGHT;
827 }
828 }
829
830 /*
831 * allocate new window structure and link it in the window list
832 */
833 if ((flags & WSP_TOP) == 0
834 && ((flags & WSP_BOT)
835 || (flags & WSP_BELOW)
836 || (!(flags & WSP_ABOVE)
837 && (
838#ifdef FEAT_VERTSPLIT
839 (flags & WSP_VERT) ? p_spr :
840#endif
841 p_sb))))
842 {
843 /* new window below/right of current one */
844 if (newwin == NULL)
845 wp = win_alloc(oldwin);
846 else
847 win_append(oldwin, wp);
848 }
849 else
850 {
851 if (newwin == NULL)
852 wp = win_alloc(oldwin->w_prev);
853 else
854 win_append(oldwin->w_prev, wp);
855 }
856
857 if (newwin == NULL)
858 {
859 if (wp == NULL)
860 return FAIL;
861
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000862 /* make the contents of the new window the same as the current one */
863 win_init(wp, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
865
866 /*
867 * Reorganise the tree of frames to insert the new window.
868 */
869 if (flags & (WSP_TOP | WSP_BOT))
870 {
871#ifdef FEAT_VERTSPLIT
872 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
873 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
874#else
875 if (topframe->fr_layout == FR_COL)
876#endif
877 {
878 curfrp = topframe->fr_child;
879 if (flags & WSP_BOT)
880 while (curfrp->fr_next != NULL)
881 curfrp = curfrp->fr_next;
882 }
883 else
884 curfrp = topframe;
885 before = (flags & WSP_TOP);
886 }
887 else
888 {
889 curfrp = oldwin->w_frame;
890 if (flags & WSP_BELOW)
891 before = FALSE;
892 else if (flags & WSP_ABOVE)
893 before = TRUE;
894 else
895#ifdef FEAT_VERTSPLIT
896 if (flags & WSP_VERT)
897 before = !p_spr;
898 else
899#endif
900 before = !p_sb;
901 }
902 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
903 {
904 /* Need to create a new frame in the tree to make a branch. */
905 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
906 *frp = *curfrp;
907 curfrp->fr_layout = layout;
908 frp->fr_parent = curfrp;
909 frp->fr_next = NULL;
910 frp->fr_prev = NULL;
911 curfrp->fr_child = frp;
912 curfrp->fr_win = NULL;
913 curfrp = frp;
914 if (frp->fr_win != NULL)
915 oldwin->w_frame = frp;
916 else
917 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
918 frp->fr_parent = curfrp;
919 }
920
921 if (newwin == NULL)
922 {
923 /* Create a frame for the new window. */
924 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
925 frp->fr_layout = FR_LEAF;
926 frp->fr_win = wp;
927 wp->w_frame = frp;
928 }
929 else
930 frp = newwin->w_frame;
931 frp->fr_parent = curfrp->fr_parent;
932
933 /* Insert the new frame at the right place in the frame list. */
934 if (before)
935 frame_insert(curfrp, frp);
936 else
937 frame_append(curfrp, frp);
938
939#ifdef FEAT_VERTSPLIT
940 if (flags & WSP_VERT)
941 {
942 wp->w_p_scr = curwin->w_p_scr;
943 if (need_status)
944 {
945 --oldwin->w_height;
946 oldwin->w_status_height = need_status;
947 }
948 if (flags & (WSP_TOP | WSP_BOT))
949 {
950 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000951 wp->w_winrow = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 wp->w_height = curfrp->fr_height - (p_ls > 0);
953 wp->w_status_height = (p_ls > 0);
954 }
955 else
956 {
957 /* height and row of new window is same as current window */
958 wp->w_winrow = oldwin->w_winrow;
959 wp->w_height = oldwin->w_height;
960 wp->w_status_height = oldwin->w_status_height;
961 }
962 frp->fr_height = curfrp->fr_height;
963
964 /* "new_size" of the current window goes to the new window, use
965 * one column for the vertical separator */
966 wp->w_width = new_size;
967 if (before)
968 wp->w_vsep_width = 1;
969 else
970 {
971 wp->w_vsep_width = oldwin->w_vsep_width;
972 oldwin->w_vsep_width = 1;
973 }
974 if (flags & (WSP_TOP | WSP_BOT))
975 {
976 if (flags & WSP_BOT)
977 frame_add_vsep(curfrp);
978 /* Set width of neighbor frame */
979 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000980 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
981 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 }
983 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000984 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (before) /* new window left of current one */
986 {
987 wp->w_wincol = oldwin->w_wincol;
988 oldwin->w_wincol += new_size + 1;
989 }
990 else /* new window right of current one */
991 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
992 frame_fix_width(oldwin);
993 frame_fix_width(wp);
994 }
995 else
996#endif
997 {
998 /* width and column of new window is same as current window */
999#ifdef FEAT_VERTSPLIT
1000 if (flags & (WSP_TOP | WSP_BOT))
1001 {
1002 wp->w_wincol = 0;
1003 wp->w_width = Columns;
1004 wp->w_vsep_width = 0;
1005 }
1006 else
1007 {
1008 wp->w_wincol = oldwin->w_wincol;
1009 wp->w_width = oldwin->w_width;
1010 wp->w_vsep_width = oldwin->w_vsep_width;
1011 }
1012 frp->fr_width = curfrp->fr_width;
1013#endif
1014
1015 /* "new_size" of the current window goes to the new window, use
1016 * one row for the status line */
1017 win_new_height(wp, new_size);
1018 if (flags & (WSP_TOP | WSP_BOT))
1019 frame_new_height(curfrp, curfrp->fr_height
1020 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1021 else
1022 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1023 if (before) /* new window above current one */
1024 {
1025 wp->w_winrow = oldwin->w_winrow;
1026 wp->w_status_height = STATUS_HEIGHT;
1027 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1028 }
1029 else /* new window below current one */
1030 {
1031 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1032 wp->w_status_height = oldwin->w_status_height;
1033 oldwin->w_status_height = STATUS_HEIGHT;
1034 }
1035#ifdef FEAT_VERTSPLIT
1036 if (flags & WSP_BOT)
1037 frame_add_statusline(curfrp);
1038#endif
1039 frame_fix_height(wp);
1040 frame_fix_height(oldwin);
1041 }
1042
1043 if (flags & (WSP_TOP | WSP_BOT))
1044 (void)win_comp_pos();
1045
1046 /*
1047 * Both windows need redrawing
1048 */
1049 redraw_win_later(wp, NOT_VALID);
1050 wp->w_redr_status = TRUE;
1051 redraw_win_later(oldwin, NOT_VALID);
1052 oldwin->w_redr_status = TRUE;
1053
1054 if (need_status)
1055 {
1056 msg_row = Rows - 1;
1057 msg_col = sc_col;
1058 msg_clr_eos_force(); /* Old command/ruler may still be there */
1059 comp_col();
1060 msg_row = Rows - 1;
1061 msg_col = 0; /* put position back at start of line */
1062 }
1063
1064 /*
1065 * make the new window the current window and redraw
1066 */
1067 if (do_equal || dir != 0)
1068 win_equal(wp, TRUE,
1069#ifdef FEAT_VERTSPLIT
1070 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1071 : dir == 'h' ? 'b' :
1072#endif
1073 'v');
1074
1075 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1076 * size was given. */
1077#ifdef FEAT_VERTSPLIT
1078 if (flags & WSP_VERT)
1079 {
1080 i = p_wiw;
1081 if (size != 0)
1082 p_wiw = size;
1083
1084# ifdef FEAT_GUI
1085 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1086 if (gui.in_use)
1087 gui_init_which_components(NULL);
1088# endif
1089 }
1090 else
1091#endif
1092 {
1093 i = p_wh;
1094 if (size != 0)
1095 p_wh = size;
1096 }
1097 win_enter(wp, FALSE);
1098#ifdef FEAT_VERTSPLIT
1099 if (flags & WSP_VERT)
1100 p_wiw = i;
1101 else
1102#endif
1103 p_wh = i;
1104
1105 return OK;
1106}
1107
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001108/*
1109 * Initialize window "newp" from window "oldp".
1110 * Used when splitting a window and when creating a new tab page.
1111 * The windows will both edit the same buffer.
1112 */
1113 static void
1114win_init(newp, oldp)
1115 win_T *newp;
1116 win_T *oldp;
1117{
1118 int i;
1119
1120 newp->w_buffer = oldp->w_buffer;
1121 oldp->w_buffer->b_nwindows++;
1122 newp->w_cursor = oldp->w_cursor;
1123 newp->w_valid = 0;
1124 newp->w_curswant = oldp->w_curswant;
1125 newp->w_set_curswant = oldp->w_set_curswant;
1126 newp->w_topline = oldp->w_topline;
1127#ifdef FEAT_DIFF
1128 newp->w_topfill = oldp->w_topfill;
1129#endif
1130 newp->w_leftcol = oldp->w_leftcol;
1131 newp->w_pcmark = oldp->w_pcmark;
1132 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1133 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001134 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001135 newp->w_fraction = oldp->w_fraction;
1136 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1137#ifdef FEAT_JUMPLIST
1138 copy_jumplist(oldp, newp);
1139#endif
1140#ifdef FEAT_QUICKFIX
1141 copy_loclist(oldp, newp);
1142#endif
1143 if (oldp->w_localdir != NULL)
1144 newp->w_localdir = vim_strsave(oldp->w_localdir);
1145
1146 /* Use the same argument list. */
1147 newp->w_alist = oldp->w_alist;
1148 ++newp->w_alist->al_refcount;
1149 newp->w_arg_idx = oldp->w_arg_idx;
1150
1151 /*
1152 * copy tagstack and options from existing window
1153 */
1154 for (i = 0; i < oldp->w_tagstacklen; i++)
1155 {
1156 newp->w_tagstack[i] = oldp->w_tagstack[i];
1157 if (newp->w_tagstack[i].tagname != NULL)
1158 newp->w_tagstack[i].tagname =
1159 vim_strsave(newp->w_tagstack[i].tagname);
1160 }
1161 newp->w_tagstackidx = oldp->w_tagstackidx;
1162 newp->w_tagstacklen = oldp->w_tagstacklen;
1163 win_copy_options(oldp, newp);
1164# ifdef FEAT_FOLDING
1165 copyFoldingState(oldp, newp);
1166# endif
1167}
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169#endif /* FEAT_WINDOWS */
1170
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171#if defined(FEAT_WINDOWS) || defined(PROTO)
1172/*
1173 * Check if "win" is a pointer to an existing window.
1174 */
1175 int
1176win_valid(win)
1177 win_T *win;
1178{
1179 win_T *wp;
1180
1181 if (win == NULL)
1182 return FALSE;
1183 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1184 if (wp == win)
1185 return TRUE;
1186 return FALSE;
1187}
1188
1189/*
1190 * Return the number of windows.
1191 */
1192 int
1193win_count()
1194{
1195 win_T *wp;
1196 int count = 0;
1197
1198 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1199 ++count;
1200 return count;
1201}
1202
1203/*
1204 * Make "count" windows on the screen.
1205 * Return actual number of windows on the screen.
1206 * Must be called when there is just one window, filling the whole screen
1207 * (excluding the command line).
1208 */
1209/*ARGSUSED*/
1210 int
1211make_windows(count, vertical)
1212 int count;
1213 int vertical; /* split windows vertically if TRUE */
1214{
1215 int maxcount;
1216 int todo;
1217
1218#ifdef FEAT_VERTSPLIT
1219 if (vertical)
1220 {
1221 /* Each windows needs at least 'winminwidth' lines and a separator
1222 * column. */
1223 maxcount = (curwin->w_width + curwin->w_vsep_width
1224 - (p_wiw - p_wmw)) / (p_wmw + 1);
1225 }
1226 else
1227#endif
1228 {
1229 /* Each window needs at least 'winminheight' lines and a status line. */
1230 maxcount = (curwin->w_height + curwin->w_status_height
1231 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1232 }
1233
1234 if (maxcount < 2)
1235 maxcount = 2;
1236 if (count > maxcount)
1237 count = maxcount;
1238
1239 /*
1240 * add status line now, otherwise first window will be too big
1241 */
1242 if (count > 1)
1243 last_status(TRUE);
1244
1245#ifdef FEAT_AUTOCMD
1246 /*
1247 * Don't execute autocommands while creating the windows. Must do that
1248 * when putting the buffers in the windows.
1249 */
1250 ++autocmd_block;
1251#endif
1252
1253 /* todo is number of windows left to create */
1254 for (todo = count - 1; todo > 0; --todo)
1255#ifdef FEAT_VERTSPLIT
1256 if (vertical)
1257 {
1258 if (win_split(curwin->w_width - (curwin->w_width - todo)
1259 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1260 break;
1261 }
1262 else
1263#endif
1264 {
1265 if (win_split(curwin->w_height - (curwin->w_height - todo
1266 * STATUS_HEIGHT) / (todo + 1)
1267 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1268 break;
1269 }
1270
1271#ifdef FEAT_AUTOCMD
1272 --autocmd_block;
1273#endif
1274
1275 /* return actual number of windows */
1276 return (count - todo);
1277}
1278
1279/*
1280 * Exchange current and next window
1281 */
1282 static void
1283win_exchange(Prenum)
1284 long Prenum;
1285{
1286 frame_T *frp;
1287 frame_T *frp2;
1288 win_T *wp;
1289 win_T *wp2;
1290 int temp;
1291
1292 if (lastwin == firstwin) /* just one window */
1293 {
1294 beep_flush();
1295 return;
1296 }
1297
1298#ifdef FEAT_GUI
1299 need_mouse_correct = TRUE;
1300#endif
1301
1302 /*
1303 * find window to exchange with
1304 */
1305 if (Prenum)
1306 {
1307 frp = curwin->w_frame->fr_parent->fr_child;
1308 while (frp != NULL && --Prenum > 0)
1309 frp = frp->fr_next;
1310 }
1311 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1312 frp = curwin->w_frame->fr_next;
1313 else /* Swap last window in row/col with previous */
1314 frp = curwin->w_frame->fr_prev;
1315
1316 /* We can only exchange a window with another window, not with a frame
1317 * containing windows. */
1318 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1319 return;
1320 wp = frp->fr_win;
1321
1322/*
1323 * 1. remove curwin from the list. Remember after which window it was in wp2
1324 * 2. insert curwin before wp in the list
1325 * if wp != wp2
1326 * 3. remove wp from the list
1327 * 4. insert wp after wp2
1328 * 5. exchange the status line height and vsep width.
1329 */
1330 wp2 = curwin->w_prev;
1331 frp2 = curwin->w_frame->fr_prev;
1332 if (wp->w_prev != curwin)
1333 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001334 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 frame_remove(curwin->w_frame);
1336 win_append(wp->w_prev, curwin);
1337 frame_insert(frp, curwin->w_frame);
1338 }
1339 if (wp != wp2)
1340 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001341 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 frame_remove(wp->w_frame);
1343 win_append(wp2, wp);
1344 if (frp2 == NULL)
1345 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1346 else
1347 frame_append(frp2, wp->w_frame);
1348 }
1349 temp = curwin->w_status_height;
1350 curwin->w_status_height = wp->w_status_height;
1351 wp->w_status_height = temp;
1352#ifdef FEAT_VERTSPLIT
1353 temp = curwin->w_vsep_width;
1354 curwin->w_vsep_width = wp->w_vsep_width;
1355 wp->w_vsep_width = temp;
1356
1357 /* If the windows are not in the same frame, exchange the sizes to avoid
1358 * messing up the window layout. Otherwise fix the frame sizes. */
1359 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1360 {
1361 temp = curwin->w_height;
1362 curwin->w_height = wp->w_height;
1363 wp->w_height = temp;
1364 temp = curwin->w_width;
1365 curwin->w_width = wp->w_width;
1366 wp->w_width = temp;
1367 }
1368 else
1369 {
1370 frame_fix_height(curwin);
1371 frame_fix_height(wp);
1372 frame_fix_width(curwin);
1373 frame_fix_width(wp);
1374 }
1375#endif
1376
1377 (void)win_comp_pos(); /* recompute window positions */
1378
1379 win_enter(wp, TRUE);
1380 redraw_later(CLEAR);
1381}
1382
1383/*
1384 * rotate windows: if upwards TRUE the second window becomes the first one
1385 * if upwards FALSE the first window becomes the second one
1386 */
1387 static void
1388win_rotate(upwards, count)
1389 int upwards;
1390 int count;
1391{
1392 win_T *wp1;
1393 win_T *wp2;
1394 frame_T *frp;
1395 int n;
1396
1397 if (firstwin == lastwin) /* nothing to do */
1398 {
1399 beep_flush();
1400 return;
1401 }
1402
1403#ifdef FEAT_GUI
1404 need_mouse_correct = TRUE;
1405#endif
1406
1407#ifdef FEAT_VERTSPLIT
1408 /* Check if all frames in this row/col have one window. */
1409 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1410 frp = frp->fr_next)
1411 if (frp->fr_win == NULL)
1412 {
1413 EMSG(_("E443: Cannot rotate when another window is split"));
1414 return;
1415 }
1416#endif
1417
1418 while (count--)
1419 {
1420 if (upwards) /* first window becomes last window */
1421 {
1422 /* remove first window/frame from the list */
1423 frp = curwin->w_frame->fr_parent->fr_child;
1424 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001425 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 frame_remove(frp);
1427
1428 /* find last frame and append removed window/frame after it */
1429 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1430 ;
1431 win_append(frp->fr_win, wp1);
1432 frame_append(frp, wp1->w_frame);
1433
1434 wp2 = frp->fr_win; /* previously last window */
1435 }
1436 else /* last window becomes first window */
1437 {
1438 /* find last window/frame in the list and remove it */
1439 for (frp = curwin->w_frame; frp->fr_next != NULL;
1440 frp = frp->fr_next)
1441 ;
1442 wp1 = frp->fr_win;
1443 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001444 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 frame_remove(frp);
1446
1447 /* append the removed window/frame before the first in the list */
1448 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1449 frame_insert(frp->fr_parent->fr_child, frp);
1450 }
1451
1452 /* exchange status height and vsep width of old and new last window */
1453 n = wp2->w_status_height;
1454 wp2->w_status_height = wp1->w_status_height;
1455 wp1->w_status_height = n;
1456 frame_fix_height(wp1);
1457 frame_fix_height(wp2);
1458#ifdef FEAT_VERTSPLIT
1459 n = wp2->w_vsep_width;
1460 wp2->w_vsep_width = wp1->w_vsep_width;
1461 wp1->w_vsep_width = n;
1462 frame_fix_width(wp1);
1463 frame_fix_width(wp2);
1464#endif
1465
1466 /* recompute w_winrow and w_wincol for all windows */
1467 (void)win_comp_pos();
1468 }
1469
1470 redraw_later(CLEAR);
1471}
1472
1473/*
1474 * Move the current window to the very top/bottom/left/right of the screen.
1475 */
1476 static void
1477win_totop(size, flags)
1478 int size;
1479 int flags;
1480{
1481 int dir;
1482 int height = curwin->w_height;
1483
1484 if (lastwin == firstwin)
1485 {
1486 beep_flush();
1487 return;
1488 }
1489
1490 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001491 (void)winframe_remove(curwin, &dir, NULL);
1492 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 last_status(FALSE); /* may need to remove last status line */
1494 (void)win_comp_pos(); /* recompute window positions */
1495
1496 /* Split a window on the desired side and put the window there. */
1497 (void)win_split_ins(size, flags, curwin, dir);
1498 if (!(flags & WSP_VERT))
1499 {
1500 win_setheight(height);
1501 if (p_ea)
1502 win_equal(curwin, TRUE, 'v');
1503 }
1504
1505#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1506 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1507 * scrollbars. Have to update them anyway. */
1508 if (gui.in_use)
1509 {
1510 out_flush();
1511 gui_init_which_components(NULL);
1512 gui_update_scrollbars(TRUE);
1513 }
1514 need_mouse_correct = TRUE;
1515#endif
1516
1517}
1518
1519/*
1520 * Move window "win1" to below/right of "win2" and make "win1" the current
1521 * window. Only works within the same frame!
1522 */
1523 void
1524win_move_after(win1, win2)
1525 win_T *win1, *win2;
1526{
1527 int height;
1528
1529 /* check if the arguments are reasonable */
1530 if (win1 == win2)
1531 return;
1532
1533 /* check if there is something to do */
1534 if (win2->w_next != win1)
1535 {
1536 /* may need move the status line/vertical separator of the last window
1537 * */
1538 if (win1 == lastwin)
1539 {
1540 height = win1->w_prev->w_status_height;
1541 win1->w_prev->w_status_height = win1->w_status_height;
1542 win1->w_status_height = height;
1543#ifdef FEAT_VERTSPLIT
1544 win1->w_prev->w_vsep_width = 0;
1545 win1->w_vsep_width = 1;
1546#endif
1547 }
1548 else if (win2 == lastwin)
1549 {
1550 height = win1->w_status_height;
1551 win1->w_status_height = win2->w_status_height;
1552 win2->w_status_height = height;
1553#ifdef FEAT_VERTSPLIT
1554 win2->w_vsep_width = 1;
1555 win1->w_vsep_width = 0;
1556#endif
1557 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001558 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 frame_remove(win1->w_frame);
1560 win_append(win2, win1);
1561 frame_append(win2->w_frame, win1->w_frame);
1562
1563 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1564 redraw_later(NOT_VALID);
1565 }
1566 win_enter(win1, FALSE);
1567}
1568
1569/*
1570 * Make all windows the same height.
1571 * 'next_curwin' will soon be the current window, make sure it has enough
1572 * rows.
1573 */
1574 void
1575win_equal(next_curwin, current, dir)
1576 win_T *next_curwin; /* pointer to current window to be or NULL */
1577 int current; /* do only frame with current window */
1578 int dir; /* 'v' for vertically, 'h' for horizontally,
1579 'b' for both, 0 for using p_ead */
1580{
1581 if (dir == 0)
1582#ifdef FEAT_VERTSPLIT
1583 dir = *p_ead;
1584#else
1585 dir = 'b';
1586#endif
1587 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001588 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001589 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590}
1591
1592/*
1593 * Set a frame to a new position and height, spreading the available room
1594 * equally over contained frames.
1595 * The window "next_curwin" (if not NULL) should at least get the size from
1596 * 'winheight' and 'winwidth' if possible.
1597 */
1598 static void
1599win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1600 win_T *next_curwin; /* pointer to current window to be or NULL */
1601 int current; /* do only frame with current window */
1602 frame_T *topfr; /* frame to set size off */
1603 int dir; /* 'v', 'h' or 'b', see win_equal() */
1604 int col; /* horizontal position for frame */
1605 int row; /* vertical position for frame */
1606 int width; /* new width of frame */
1607 int height; /* new height of frame */
1608{
1609 int n, m;
1610 int extra_sep = 0;
1611 int wincount, totwincount = 0;
1612 frame_T *fr;
1613 int next_curwin_size = 0;
1614 int room = 0;
1615 int new_size;
1616 int has_next_curwin = 0;
1617 int hnc;
1618
1619 if (topfr->fr_layout == FR_LEAF)
1620 {
1621 /* Set the width/height of this frame.
1622 * Redraw when size or position changes */
1623 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1624#ifdef FEAT_VERTSPLIT
1625 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1626#endif
1627 )
1628 {
1629 topfr->fr_win->w_winrow = row;
1630 frame_new_height(topfr, height, FALSE, FALSE);
1631#ifdef FEAT_VERTSPLIT
1632 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001633 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634#endif
1635 redraw_all_later(CLEAR);
1636 }
1637 }
1638#ifdef FEAT_VERTSPLIT
1639 else if (topfr->fr_layout == FR_ROW)
1640 {
1641 topfr->fr_width = width;
1642 topfr->fr_height = height;
1643
1644 if (dir != 'v') /* equalize frame widths */
1645 {
1646 /* Compute the maximum number of windows horizontally in this
1647 * frame. */
1648 n = frame_minwidth(topfr, NOWIN);
1649 /* add one for the rightmost window, it doesn't have a separator */
1650 if (col + width == Columns)
1651 extra_sep = 1;
1652 else
1653 extra_sep = 0;
1654 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001655 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001657 /*
1658 * Compute width for "next_curwin" window and room available for
1659 * other windows.
1660 * "m" is the minimal width when counting p_wiw for "next_curwin".
1661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 m = frame_minwidth(topfr, next_curwin);
1663 room = width - m;
1664 if (room < 0)
1665 {
1666 next_curwin_size = p_wiw + room;
1667 room = 0;
1668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 else
1670 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001671 next_curwin_size = -1;
1672 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1673 {
1674 /* If 'winfixwidth' set keep the window width if
1675 * possible.
1676 * Watch out for this window being the next_curwin. */
1677 if (frame_fixed_width(fr))
1678 {
1679 n = frame_minwidth(fr, NOWIN);
1680 new_size = fr->fr_width;
1681 if (frame_has_win(fr, next_curwin))
1682 {
1683 room += p_wiw - p_wmw;
1684 next_curwin_size = 0;
1685 if (new_size < p_wiw)
1686 new_size = p_wiw;
1687 }
1688 else
1689 /* These windows don't use up room. */
1690 totwincount -= (n + (fr->fr_next == NULL
1691 ? extra_sep : 0)) / (p_wmw + 1);
1692 room -= new_size - n;
1693 if (room < 0)
1694 {
1695 new_size += room;
1696 room = 0;
1697 }
1698 fr->fr_newwidth = new_size;
1699 }
1700 }
1701 if (next_curwin_size == -1)
1702 {
1703 if (!has_next_curwin)
1704 next_curwin_size = 0;
1705 else if (totwincount > 1
1706 && (room + (totwincount - 2))
1707 / (totwincount - 1) > p_wiw)
1708 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001709 /* Can make all windows wider than 'winwidth', spread
1710 * the room equally. */
1711 next_curwin_size = (room + p_wiw
1712 + (totwincount - 1) * p_wmw
1713 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001714 room -= next_curwin_size - p_wiw;
1715 }
1716 else
1717 next_curwin_size = p_wiw;
1718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001720
1721 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 --totwincount; /* don't count curwin */
1723 }
1724
1725 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1726 {
1727 n = m = 0;
1728 wincount = 1;
1729 if (fr->fr_next == NULL)
1730 /* last frame gets all that remains (avoid roundoff error) */
1731 new_size = width;
1732 else if (dir == 'v')
1733 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001734 else if (frame_fixed_width(fr))
1735 {
1736 new_size = fr->fr_newwidth;
1737 wincount = 0; /* doesn't count as a sizeable window */
1738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 else
1740 {
1741 /* Compute the maximum number of windows horiz. in "fr". */
1742 n = frame_minwidth(fr, NOWIN);
1743 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1744 / (p_wmw + 1);
1745 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001746 if (has_next_curwin)
1747 hnc = frame_has_win(fr, next_curwin);
1748 else
1749 hnc = FALSE;
1750 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001752 if (totwincount == 0)
1753 new_size = room;
1754 else
1755 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001757 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 {
1759 next_curwin_size -= p_wiw - (m - n);
1760 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001761 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001763 else
1764 room -= new_size;
1765 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 }
1767
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001768 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 * window, unless equalizing all frames. */
1770 if (!current || dir != 'v' || topfr->fr_parent != NULL
1771 || (new_size != fr->fr_width)
1772 || frame_has_win(fr, next_curwin))
1773 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001774 new_size, height);
1775 col += new_size;
1776 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 totwincount -= wincount;
1778 }
1779 }
1780#endif
1781 else /* topfr->fr_layout == FR_COL */
1782 {
1783#ifdef FEAT_VERTSPLIT
1784 topfr->fr_width = width;
1785#endif
1786 topfr->fr_height = height;
1787
1788 if (dir != 'h') /* equalize frame heights */
1789 {
1790 /* Compute maximum number of windows vertically in this frame. */
1791 n = frame_minheight(topfr, NOWIN);
1792 /* add one for the bottom window if it doesn't have a statusline */
1793 if (row + height == cmdline_row && p_ls == 0)
1794 extra_sep = 1;
1795 else
1796 extra_sep = 0;
1797 totwincount = (n + extra_sep) / (p_wmh + 1);
1798 has_next_curwin = frame_has_win(topfr, next_curwin);
1799
1800 /*
1801 * Compute height for "next_curwin" window and room available for
1802 * other windows.
1803 * "m" is the minimal height when counting p_wh for "next_curwin".
1804 */
1805 m = frame_minheight(topfr, next_curwin);
1806 room = height - m;
1807 if (room < 0)
1808 {
1809 /* The room is less then 'winheight', use all space for the
1810 * current window. */
1811 next_curwin_size = p_wh + room;
1812 room = 0;
1813 }
1814 else
1815 {
1816 next_curwin_size = -1;
1817 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1818 {
1819 /* If 'winfixheight' set keep the window height if
1820 * possible.
1821 * Watch out for this window being the next_curwin. */
1822 if (frame_fixed_height(fr))
1823 {
1824 n = frame_minheight(fr, NOWIN);
1825 new_size = fr->fr_height;
1826 if (frame_has_win(fr, next_curwin))
1827 {
1828 room += p_wh - p_wmh;
1829 next_curwin_size = 0;
1830 if (new_size < p_wh)
1831 new_size = p_wh;
1832 }
1833 else
1834 /* These windows don't use up room. */
1835 totwincount -= (n + (fr->fr_next == NULL
1836 ? extra_sep : 0)) / (p_wmh + 1);
1837 room -= new_size - n;
1838 if (room < 0)
1839 {
1840 new_size += room;
1841 room = 0;
1842 }
1843 fr->fr_newheight = new_size;
1844 }
1845 }
1846 if (next_curwin_size == -1)
1847 {
1848 if (!has_next_curwin)
1849 next_curwin_size = 0;
1850 else if (totwincount > 1
1851 && (room + (totwincount - 2))
1852 / (totwincount - 1) > p_wh)
1853 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001854 /* can make all windows higher than 'winheight',
1855 * spread the room equally. */
1856 next_curwin_size = (room + p_wh
1857 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 + (totwincount - 1)) / totwincount;
1859 room -= next_curwin_size - p_wh;
1860 }
1861 else
1862 next_curwin_size = p_wh;
1863 }
1864 }
1865
1866 if (has_next_curwin)
1867 --totwincount; /* don't count curwin */
1868 }
1869
1870 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1871 {
1872 n = m = 0;
1873 wincount = 1;
1874 if (fr->fr_next == NULL)
1875 /* last frame gets all that remains (avoid roundoff error) */
1876 new_size = height;
1877 else if (dir == 'h')
1878 new_size = fr->fr_height;
1879 else if (frame_fixed_height(fr))
1880 {
1881 new_size = fr->fr_newheight;
1882 wincount = 0; /* doesn't count as a sizeable window */
1883 }
1884 else
1885 {
1886 /* Compute the maximum number of windows vert. in "fr". */
1887 n = frame_minheight(fr, NOWIN);
1888 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1889 / (p_wmh + 1);
1890 m = frame_minheight(fr, next_curwin);
1891 if (has_next_curwin)
1892 hnc = frame_has_win(fr, next_curwin);
1893 else
1894 hnc = FALSE;
1895 if (hnc) /* don't count next_curwin */
1896 --wincount;
1897 if (totwincount == 0)
1898 new_size = room;
1899 else
1900 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1901 / totwincount;
1902 if (hnc) /* add next_curwin size */
1903 {
1904 next_curwin_size -= p_wh - (m - n);
1905 new_size += next_curwin_size;
1906 room -= new_size - next_curwin_size;
1907 }
1908 else
1909 room -= new_size;
1910 new_size += n;
1911 }
1912 /* Skip frame that is full width when splitting or closing a
1913 * window, unless equalizing all frames. */
1914 if (!current || dir != 'h' || topfr->fr_parent != NULL
1915 || (new_size != fr->fr_height)
1916 || frame_has_win(fr, next_curwin))
1917 win_equal_rec(next_curwin, current, fr, dir, col, row,
1918 width, new_size);
1919 row += new_size;
1920 height -= new_size;
1921 totwincount -= wincount;
1922 }
1923 }
1924}
1925
1926/*
1927 * close all windows for buffer 'buf'
1928 */
1929 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00001930close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001932 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933{
Bram Moolenaarf740b292006-02-16 22:11:02 +00001934 win_T *wp;
1935 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001936 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937
1938 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001939
1940 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001942 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001944 win_close(wp, FALSE);
1945
1946 /* Start all over, autocommands may change the window layout. */
1947 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
1949 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00001950 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001952
1953 /* Also check windows in other tab pages. */
1954 for (tp = first_tabpage; tp != NULL; tp = nexttp)
1955 {
1956 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001957 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001958 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
1959 if (wp->w_buffer == buf)
1960 {
1961 win_close_othertab(wp, FALSE, tp);
1962
1963 /* Start all over, the tab page may be closed and
1964 * autocommands may change the window layout. */
1965 nexttp = first_tabpage;
1966 break;
1967 }
1968 }
1969
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001971
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001972 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00001973 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974}
1975
1976/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001977 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001978 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001979 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001980 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001981last_window()
1982{
1983 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
1984}
1985
1986/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001987 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 * If "free_buf" is TRUE related buffer may be unloaded.
1989 *
1990 * called by :quit, :close, :xit, :wq and findtag()
1991 */
1992 void
1993win_close(win, free_buf)
1994 win_T *win;
1995 int free_buf;
1996{
1997 win_T *wp;
1998#ifdef FEAT_AUTOCMD
1999 int other_buffer = FALSE;
2000#endif
2001 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 int dir;
2003 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002004 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002006 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 EMSG(_("E444: Cannot close last window"));
2009 return;
2010 }
2011
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002012 /*
2013 * When closing the last window in a tab page first go to another tab
2014 * page and then close the window and the tab page. This avoids that
2015 * curwin and curtab are not invalid while we are freeing memory, they may
2016 * be used in GUI events.
2017 */
2018 if (firstwin == lastwin)
2019 {
2020 goto_tabpage_tp(alt_tabpage());
2021 redraw_tabline = TRUE;
2022
2023 /* Safety check: Autocommands may have closed the window when jumping
2024 * to the other tab page. */
2025 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2026 {
2027 int h = tabline_height();
2028
2029 win_close_othertab(win, free_buf, prev_curtab);
2030 if (h != tabline_height())
2031 shell_new_rows();
2032 }
2033 return;
2034 }
2035
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 /* When closing the help window, try restoring a snapshot after closing
2037 * the window. Otherwise clear the snapshot, it's now invalid. */
2038 if (win->w_buffer->b_help)
2039 help_window = TRUE;
2040 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002041 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042
2043#ifdef FEAT_AUTOCMD
2044 if (win == curwin)
2045 {
2046 /*
2047 * Guess which window is going to be the new current window.
2048 * This may change because of the autocommands (sigh).
2049 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002050 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
2052 /*
2053 * Be careful: If autocommands delete the window, return now.
2054 */
2055 if (wp->w_buffer != curbuf)
2056 {
2057 other_buffer = TRUE;
2058 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002059 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 return;
2061 }
2062 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002063 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 return;
2065# ifdef FEAT_EVAL
2066 /* autocmds may abort script processing */
2067 if (aborting())
2068 return;
2069# endif
2070 }
2071#endif
2072
2073 /*
2074 * Close the link to the buffer.
2075 */
2076 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002077
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002079 * other window or moved to another tab page. */
2080 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 return;
2082
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002083 /* Free the memory used for the window. */
2084 wp = win_free_mem(win, &dir, NULL);
2085
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 /* Make sure curwin isn't invalid. It can cause severe trouble when
2087 * printing an error message. For win_equal() curbuf needs to be valid
2088 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002089 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
2091 curwin = wp;
2092#ifdef FEAT_QUICKFIX
2093 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2094 {
2095 /*
2096 * The cursor goes to the preview or the quickfix window, try
2097 * finding another window to go to.
2098 */
2099 for (;;)
2100 {
2101 if (wp->w_next == NULL)
2102 wp = firstwin;
2103 else
2104 wp = wp->w_next;
2105 if (wp == curwin)
2106 break;
2107 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2108 {
2109 curwin = wp;
2110 break;
2111 }
2112 }
2113 }
2114#endif
2115 curbuf = curwin->w_buffer;
2116 close_curwin = TRUE;
2117 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002118 if (p_ea
2119#ifdef FEAT_VERTSPLIT
2120 && (*p_ead == 'b' || *p_ead == dir)
2121#endif
2122 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 win_equal(curwin, TRUE,
2124#ifdef FEAT_VERTSPLIT
2125 dir
2126#else
2127 0
2128#endif
2129 );
2130 else
2131 win_comp_pos();
2132 if (close_curwin)
2133 {
2134 win_enter_ext(wp, FALSE, TRUE);
2135#ifdef FEAT_AUTOCMD
2136 if (other_buffer)
2137 /* careful: after this wp and win may be invalid! */
2138 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2139#endif
2140 }
2141
2142 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002143 * If last window has a status line now and we don't want one,
2144 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 */
2146 last_status(FALSE);
2147
2148 /* After closing the help window, try restoring the window layout from
2149 * before it was opened. */
2150 if (help_window)
2151 restore_snapshot(close_curwin);
2152
2153#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2154 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2155 if (gui.in_use && !win_hasvertsplit())
2156 gui_init_which_components(NULL);
2157#endif
2158
2159 redraw_all_later(NOT_VALID);
2160}
2161
2162/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002163 * Close window "win" in tab page "tp", which is not the current tab page.
2164 * This may be the last window ih that tab page and result in closing the tab,
2165 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002166 * Caller must check if buffer is hidden and whether the tabline needs to be
2167 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002168 */
2169 void
2170win_close_othertab(win, free_buf, tp)
2171 win_T *win;
2172 int free_buf;
2173 tabpage_T *tp;
2174{
2175 win_T *wp;
2176 int dir;
2177 tabpage_T *ptp = NULL;
2178
2179 /* Close the link to the buffer. */
2180 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2181
2182 /* Careful: Autocommands may have closed the tab page or made it the
2183 * current tab page. */
2184 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2185 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002186 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002187 return;
2188
2189 /* Autocommands may have closed the window already. */
2190 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2191 ;
2192 if (wp == NULL)
2193 return;
2194
2195 /* Free the memory used for the window. */
2196 wp = win_free_mem(win, &dir, tp);
2197
2198 /* When closing the last window in a tab page remove the tab page. */
2199 if (wp == NULL)
2200 {
2201 if (tp == first_tabpage)
2202 first_tabpage = tp->tp_next;
2203 else
2204 {
2205 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2206 ptp = ptp->tp_next)
2207 ;
2208 if (ptp == NULL)
2209 {
2210 EMSG2(_(e_intern2), "win_close_othertab()");
2211 return;
2212 }
2213 ptp->tp_next = tp->tp_next;
2214 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002215 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002216 }
2217}
2218
2219/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002220 * Free the memory used for a window.
2221 * Returns a pointer to the window that got the freed up space.
2222 */
2223 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002224win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002225 win_T *win;
2226 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002227 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002228{
2229 frame_T *frp;
2230 win_T *wp;
2231
Bram Moolenaarea408852005-06-25 22:49:46 +00002232#ifdef FEAT_FOLDING
2233 clearFolding(win);
2234#endif
2235
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002236 /* reduce the reference count to the argument list. */
2237 alist_unlink(win->w_alist);
2238
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002239 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002240 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002241 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002242 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002243 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002244
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002245 /* When deleting the current window of another tab page select a new
2246 * current window. */
2247 if (tp != NULL && win == tp->tp_curwin)
2248 tp->tp_curwin = wp;
2249
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002250 return wp;
2251}
2252
2253#if defined(EXITFREE) || defined(PROTO)
2254 void
2255win_free_all()
2256{
2257 int dummy;
2258
Bram Moolenaarf740b292006-02-16 22:11:02 +00002259# ifdef FEAT_WINDOWS
2260 while (first_tabpage->tp_next != NULL)
2261 tabpage_close(TRUE);
2262# endif
2263
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002264 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002265 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002266}
2267#endif
2268
2269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 * Remove a window and its frame from the tree of frames.
2271 * Returns a pointer to the window that got the freed up space.
2272 */
2273/*ARGSUSED*/
2274 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002275winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 win_T *win;
2277 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002278 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
2280 frame_T *frp, *frp2, *frp3;
2281 frame_T *frp_close = win->w_frame;
2282 win_T *wp;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002283 int old_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284
2285 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002286 * If there is only one window there is nothing to remove.
2287 */
2288 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2289 return NULL;
2290
2291 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 * Remove the window from its frame.
2293 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002294 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 wp = frame2win(frp2);
2296
2297 /* Remove this frame from the list of frames. */
2298 frame_remove(frp_close);
2299
2300#ifdef FEAT_VERTSPLIT
2301 if (frp_close->fr_parent->fr_layout == FR_COL)
2302 {
2303#endif
2304 /* When 'winfixheight' is set, remember its old size and restore
2305 * it later (it's a simplistic solution...). Don't do this if the
2306 * window will occupy the full height of the screen. */
2307 if (frp2->fr_win != NULL
2308 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2309 && frp2->fr_win->w_p_wfh)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002310 old_size = frp2->fr_win->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2312 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002313 if (old_size != 0)
2314 win_setheight_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315#ifdef FEAT_VERTSPLIT
2316 *dirp = 'v';
2317 }
2318 else
2319 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002320 /* When 'winfixwidth' is set, remember its old size and restore
2321 * it later (it's a simplistic solution...). Don't do this if the
2322 * window will occupy the full width of the screen. */
2323 if (frp2->fr_win != NULL
2324 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2325 && frp2->fr_win->w_p_wfw)
2326 old_size = frp2->fr_win->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002328 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2329 if (old_size != 0)
2330 win_setwidth_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 *dirp = 'h';
2332 }
2333#endif
2334
2335 /* If rows/columns go to a window below/right its positions need to be
2336 * updated. Can only be done after the sizes have been updated. */
2337 if (frp2 == frp_close->fr_next)
2338 {
2339 int row = win->w_winrow;
2340 int col = W_WINCOL(win);
2341
2342 frame_comp_pos(frp2, &row, &col);
2343 }
2344
2345 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2346 {
2347 /* There is no other frame in this list, move its info to the parent
2348 * and remove it. */
2349 frp2->fr_parent->fr_layout = frp2->fr_layout;
2350 frp2->fr_parent->fr_child = frp2->fr_child;
2351 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2352 frp->fr_parent = frp2->fr_parent;
2353 frp2->fr_parent->fr_win = frp2->fr_win;
2354 if (frp2->fr_win != NULL)
2355 frp2->fr_win->w_frame = frp2->fr_parent;
2356 frp = frp2->fr_parent;
2357 vim_free(frp2);
2358
2359 frp2 = frp->fr_parent;
2360 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2361 {
2362 /* The frame above the parent has the same layout, have to merge
2363 * the frames into this list. */
2364 if (frp2->fr_child == frp)
2365 frp2->fr_child = frp->fr_child;
2366 frp->fr_child->fr_prev = frp->fr_prev;
2367 if (frp->fr_prev != NULL)
2368 frp->fr_prev->fr_next = frp->fr_child;
2369 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2370 {
2371 frp3->fr_parent = frp2;
2372 if (frp3->fr_next == NULL)
2373 {
2374 frp3->fr_next = frp->fr_next;
2375 if (frp->fr_next != NULL)
2376 frp->fr_next->fr_prev = frp3;
2377 break;
2378 }
2379 }
2380 vim_free(frp);
2381 }
2382 }
2383
2384 return wp;
2385}
2386
2387/*
2388 * Find out which frame is going to get the freed up space when "win" is
2389 * closed.
2390 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2391 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2392 * This makes opening a window and closing it immediately keep the same window
2393 * layout.
2394 */
2395 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002396win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002398 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
2400 frame_T *frp;
2401 int b;
2402
Bram Moolenaarf740b292006-02-16 22:11:02 +00002403 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002404 /* Last window in this tab page, will go to next tab page. */
2405 return alt_tabpage()->tp_curwin->w_frame;
2406
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 frp = win->w_frame;
2408#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002409 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 b = p_spr;
2411 else
2412#endif
2413 b = p_sb;
2414 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2415 return frp->fr_next;
2416 return frp->fr_prev;
2417}
2418
2419/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002420 * Return the tabpage that will be used if the current one is closed.
2421 */
2422 static tabpage_T *
2423alt_tabpage()
2424{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002425 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002426
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002427 /* Use the next tab page if possible. */
2428 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002429 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002430
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002431 /* Find the last but one tab page. */
2432 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2433 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002434 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002435}
2436
2437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 * Find the left-upper window in frame "frp".
2439 */
2440 static win_T *
2441frame2win(frp)
2442 frame_T *frp;
2443{
2444 while (frp->fr_win == NULL)
2445 frp = frp->fr_child;
2446 return frp->fr_win;
2447}
2448
2449/*
2450 * Return TRUE if frame "frp" contains window "wp".
2451 */
2452 static int
2453frame_has_win(frp, wp)
2454 frame_T *frp;
2455 win_T *wp;
2456{
2457 frame_T *p;
2458
2459 if (frp->fr_layout == FR_LEAF)
2460 return frp->fr_win == wp;
2461
2462 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2463 if (frame_has_win(p, wp))
2464 return TRUE;
2465 return FALSE;
2466}
2467
2468/*
2469 * Set a new height for a frame. Recursively sets the height for contained
2470 * frames and windows. Caller must take care of positions.
2471 */
2472 static void
2473frame_new_height(topfrp, height, topfirst, wfh)
2474 frame_T *topfrp;
2475 int height;
2476 int topfirst; /* resize topmost contained frame first */
2477 int wfh; /* obey 'winfixheight' when there is a choice;
2478 may cause the height not to be set */
2479{
2480 frame_T *frp;
2481 int extra_lines;
2482 int h;
2483
2484 if (topfrp->fr_win != NULL)
2485 {
2486 /* Simple case: just one window. */
2487 win_new_height(topfrp->fr_win,
2488 height - topfrp->fr_win->w_status_height);
2489 }
2490#ifdef FEAT_VERTSPLIT
2491 else if (topfrp->fr_layout == FR_ROW)
2492 {
2493 do
2494 {
2495 /* All frames in this row get the same new height. */
2496 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2497 {
2498 frame_new_height(frp, height, topfirst, wfh);
2499 if (frp->fr_height > height)
2500 {
2501 /* Could not fit the windows, make the whole row higher. */
2502 height = frp->fr_height;
2503 break;
2504 }
2505 }
2506 }
2507 while (frp != NULL);
2508 }
2509#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002510 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
2512 /* Complicated case: Resize a column of frames. Resize the bottom
2513 * frame first, frames above that when needed. */
2514
2515 frp = topfrp->fr_child;
2516 if (wfh)
2517 /* Advance past frames with one window with 'wfh' set. */
2518 while (frame_fixed_height(frp))
2519 {
2520 frp = frp->fr_next;
2521 if (frp == NULL)
2522 return; /* no frame without 'wfh', give up */
2523 }
2524 if (!topfirst)
2525 {
2526 /* Find the bottom frame of this column */
2527 while (frp->fr_next != NULL)
2528 frp = frp->fr_next;
2529 if (wfh)
2530 /* Advance back for frames with one window with 'wfh' set. */
2531 while (frame_fixed_height(frp))
2532 frp = frp->fr_prev;
2533 }
2534
2535 extra_lines = height - topfrp->fr_height;
2536 if (extra_lines < 0)
2537 {
2538 /* reduce height of contained frames, bottom or top frame first */
2539 while (frp != NULL)
2540 {
2541 h = frame_minheight(frp, NULL);
2542 if (frp->fr_height + extra_lines < h)
2543 {
2544 extra_lines += frp->fr_height - h;
2545 frame_new_height(frp, h, topfirst, wfh);
2546 }
2547 else
2548 {
2549 frame_new_height(frp, frp->fr_height + extra_lines,
2550 topfirst, wfh);
2551 break;
2552 }
2553 if (topfirst)
2554 {
2555 do
2556 frp = frp->fr_next;
2557 while (wfh && frp != NULL && frame_fixed_height(frp));
2558 }
2559 else
2560 {
2561 do
2562 frp = frp->fr_prev;
2563 while (wfh && frp != NULL && frame_fixed_height(frp));
2564 }
2565 /* Increase "height" if we could not reduce enough frames. */
2566 if (frp == NULL)
2567 height -= extra_lines;
2568 }
2569 }
2570 else if (extra_lines > 0)
2571 {
2572 /* increase height of bottom or top frame */
2573 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2574 }
2575 }
2576 topfrp->fr_height = height;
2577}
2578
2579/*
2580 * Return TRUE if height of frame "frp" should not be changed because of
2581 * the 'winfixheight' option.
2582 */
2583 static int
2584frame_fixed_height(frp)
2585 frame_T *frp;
2586{
2587 /* frame with one window: fixed height if 'winfixheight' set. */
2588 if (frp->fr_win != NULL)
2589 return frp->fr_win->w_p_wfh;
2590
2591 if (frp->fr_layout == FR_ROW)
2592 {
2593 /* The frame is fixed height if one of the frames in the row is fixed
2594 * height. */
2595 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2596 if (frame_fixed_height(frp))
2597 return TRUE;
2598 return FALSE;
2599 }
2600
2601 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2602 * frames in the row are fixed height. */
2603 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2604 if (!frame_fixed_height(frp))
2605 return FALSE;
2606 return TRUE;
2607}
2608
2609#ifdef FEAT_VERTSPLIT
2610/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002611 * Return TRUE if width of frame "frp" should not be changed because of
2612 * the 'winfixwidth' option.
2613 */
2614 static int
2615frame_fixed_width(frp)
2616 frame_T *frp;
2617{
2618 /* frame with one window: fixed width if 'winfixwidth' set. */
2619 if (frp->fr_win != NULL)
2620 return frp->fr_win->w_p_wfw;
2621
2622 if (frp->fr_layout == FR_COL)
2623 {
2624 /* The frame is fixed width if one of the frames in the row is fixed
2625 * width. */
2626 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2627 if (frame_fixed_width(frp))
2628 return TRUE;
2629 return FALSE;
2630 }
2631
2632 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2633 * frames in the row are fixed width. */
2634 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2635 if (!frame_fixed_width(frp))
2636 return FALSE;
2637 return TRUE;
2638}
2639
2640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 * Add a status line to windows at the bottom of "frp".
2642 * Note: Does not check if there is room!
2643 */
2644 static void
2645frame_add_statusline(frp)
2646 frame_T *frp;
2647{
2648 win_T *wp;
2649
2650 if (frp->fr_layout == FR_LEAF)
2651 {
2652 wp = frp->fr_win;
2653 if (wp->w_status_height == 0)
2654 {
2655 if (wp->w_height > 0) /* don't make it negative */
2656 --wp->w_height;
2657 wp->w_status_height = STATUS_HEIGHT;
2658 }
2659 }
2660 else if (frp->fr_layout == FR_ROW)
2661 {
2662 /* Handle all the frames in the row. */
2663 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2664 frame_add_statusline(frp);
2665 }
2666 else /* frp->fr_layout == FR_COL */
2667 {
2668 /* Only need to handle the last frame in the column. */
2669 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2670 ;
2671 frame_add_statusline(frp);
2672 }
2673}
2674
2675/*
2676 * Set width of a frame. Handles recursively going through contained frames.
2677 * May remove separator line for windows at the right side (for win_close()).
2678 */
2679 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002680frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 frame_T *topfrp;
2682 int width;
2683 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002684 int wfw; /* obey 'winfixwidth' when there is a choice;
2685 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
2687 frame_T *frp;
2688 int extra_cols;
2689 int w;
2690 win_T *wp;
2691
2692 if (topfrp->fr_layout == FR_LEAF)
2693 {
2694 /* Simple case: just one window. */
2695 wp = topfrp->fr_win;
2696 /* Find out if there are any windows right of this one. */
2697 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2698 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2699 break;
2700 if (frp->fr_parent == NULL)
2701 wp->w_vsep_width = 0;
2702 win_new_width(wp, width - wp->w_vsep_width);
2703 }
2704 else if (topfrp->fr_layout == FR_COL)
2705 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002706 do
2707 {
2708 /* All frames in this column get the same new width. */
2709 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2710 {
2711 frame_new_width(frp, width, leftfirst, wfw);
2712 if (frp->fr_width > width)
2713 {
2714 /* Could not fit the windows, make whole column wider. */
2715 width = frp->fr_width;
2716 break;
2717 }
2718 }
2719 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 }
2721 else /* fr_layout == FR_ROW */
2722 {
2723 /* Complicated case: Resize a row of frames. Resize the rightmost
2724 * frame first, frames left of it when needed. */
2725
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002727 if (wfw)
2728 /* Advance past frames with one window with 'wfw' set. */
2729 while (frame_fixed_width(frp))
2730 {
2731 frp = frp->fr_next;
2732 if (frp == NULL)
2733 return; /* no frame without 'wfw', give up */
2734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002736 {
2737 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 while (frp->fr_next != NULL)
2739 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002740 if (wfw)
2741 /* Advance back for frames with one window with 'wfw' set. */
2742 while (frame_fixed_width(frp))
2743 frp = frp->fr_prev;
2744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
2746 extra_cols = width - topfrp->fr_width;
2747 if (extra_cols < 0)
2748 {
2749 /* reduce frame width, rightmost frame first */
2750 while (frp != NULL)
2751 {
2752 w = frame_minwidth(frp, NULL);
2753 if (frp->fr_width + extra_cols < w)
2754 {
2755 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002756 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758 else
2759 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002760 frame_new_width(frp, frp->fr_width + extra_cols,
2761 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 break;
2763 }
2764 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002765 {
2766 do
2767 frp = frp->fr_next;
2768 while (wfw && frp != NULL && frame_fixed_width(frp));
2769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002771 {
2772 do
2773 frp = frp->fr_prev;
2774 while (wfw && frp != NULL && frame_fixed_width(frp));
2775 }
2776 /* Increase "width" if we could not reduce enough frames. */
2777 if (frp == NULL)
2778 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
2780 }
2781 else if (extra_cols > 0)
2782 {
2783 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002784 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
2786 }
2787 topfrp->fr_width = width;
2788}
2789
2790/*
2791 * Add the vertical separator to windows at the right side of "frp".
2792 * Note: Does not check if there is room!
2793 */
2794 static void
2795frame_add_vsep(frp)
2796 frame_T *frp;
2797{
2798 win_T *wp;
2799
2800 if (frp->fr_layout == FR_LEAF)
2801 {
2802 wp = frp->fr_win;
2803 if (wp->w_vsep_width == 0)
2804 {
2805 if (wp->w_width > 0) /* don't make it negative */
2806 --wp->w_width;
2807 wp->w_vsep_width = 1;
2808 }
2809 }
2810 else if (frp->fr_layout == FR_COL)
2811 {
2812 /* Handle all the frames in the column. */
2813 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2814 frame_add_vsep(frp);
2815 }
2816 else /* frp->fr_layout == FR_ROW */
2817 {
2818 /* Only need to handle the last frame in the row. */
2819 frp = frp->fr_child;
2820 while (frp->fr_next != NULL)
2821 frp = frp->fr_next;
2822 frame_add_vsep(frp);
2823 }
2824}
2825
2826/*
2827 * Set frame width from the window it contains.
2828 */
2829 static void
2830frame_fix_width(wp)
2831 win_T *wp;
2832{
2833 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2834}
2835#endif
2836
2837/*
2838 * Set frame height from the window it contains.
2839 */
2840 static void
2841frame_fix_height(wp)
2842 win_T *wp;
2843{
2844 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2845}
2846
2847/*
2848 * Compute the minimal height for frame "topfrp".
2849 * Uses the 'winminheight' option.
2850 * When "next_curwin" isn't NULL, use p_wh for this window.
2851 * When "next_curwin" is NOWIN, don't use at least one line for the current
2852 * window.
2853 */
2854 static int
2855frame_minheight(topfrp, next_curwin)
2856 frame_T *topfrp;
2857 win_T *next_curwin;
2858{
2859 frame_T *frp;
2860 int m;
2861#ifdef FEAT_VERTSPLIT
2862 int n;
2863#endif
2864
2865 if (topfrp->fr_win != NULL)
2866 {
2867 if (topfrp->fr_win == next_curwin)
2868 m = p_wh + topfrp->fr_win->w_status_height;
2869 else
2870 {
2871 /* window: minimal height of the window plus status line */
2872 m = p_wmh + topfrp->fr_win->w_status_height;
2873 /* Current window is minimal one line high */
2874 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2875 ++m;
2876 }
2877 }
2878#ifdef FEAT_VERTSPLIT
2879 else if (topfrp->fr_layout == FR_ROW)
2880 {
2881 /* get the minimal height from each frame in this row */
2882 m = 0;
2883 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2884 {
2885 n = frame_minheight(frp, next_curwin);
2886 if (n > m)
2887 m = n;
2888 }
2889 }
2890#endif
2891 else
2892 {
2893 /* Add up the minimal heights for all frames in this column. */
2894 m = 0;
2895 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2896 m += frame_minheight(frp, next_curwin);
2897 }
2898
2899 return m;
2900}
2901
2902#ifdef FEAT_VERTSPLIT
2903/*
2904 * Compute the minimal width for frame "topfrp".
2905 * When "next_curwin" isn't NULL, use p_wiw for this window.
2906 * When "next_curwin" is NOWIN, don't use at least one column for the current
2907 * window.
2908 */
2909 static int
2910frame_minwidth(topfrp, next_curwin)
2911 frame_T *topfrp;
2912 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2913{
2914 frame_T *frp;
2915 int m, n;
2916
2917 if (topfrp->fr_win != NULL)
2918 {
2919 if (topfrp->fr_win == next_curwin)
2920 m = p_wiw + topfrp->fr_win->w_vsep_width;
2921 else
2922 {
2923 /* window: minimal width of the window plus separator column */
2924 m = p_wmw + topfrp->fr_win->w_vsep_width;
2925 /* Current window is minimal one column wide */
2926 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2927 ++m;
2928 }
2929 }
2930 else if (topfrp->fr_layout == FR_COL)
2931 {
2932 /* get the minimal width from each frame in this column */
2933 m = 0;
2934 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2935 {
2936 n = frame_minwidth(frp, next_curwin);
2937 if (n > m)
2938 m = n;
2939 }
2940 }
2941 else
2942 {
2943 /* Add up the minimal widths for all frames in this row. */
2944 m = 0;
2945 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2946 m += frame_minwidth(frp, next_curwin);
2947 }
2948
2949 return m;
2950}
2951#endif
2952
2953
2954/*
2955 * Try to close all windows except current one.
2956 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2957 * used and the buffer was modified.
2958 *
2959 * Used by ":bdel" and ":only".
2960 */
2961 void
2962close_others(message, forceit)
2963 int message;
2964 int forceit; /* always hide all other windows */
2965{
2966 win_T *wp;
2967 win_T *nextwp;
2968 int r;
2969
2970 if (lastwin == firstwin)
2971 {
2972 if (message
2973#ifdef FEAT_AUTOCMD
2974 && !autocmd_busy
2975#endif
2976 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00002977 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 return;
2979 }
2980
2981 /* Be very careful here: autocommands may change the window layout. */
2982 for (wp = firstwin; win_valid(wp); wp = nextwp)
2983 {
2984 nextwp = wp->w_next;
2985 if (wp != curwin) /* don't close current window */
2986 {
2987
2988 /* Check if it's allowed to abandon this window */
2989 r = can_abandon(wp->w_buffer, forceit);
2990#ifdef FEAT_AUTOCMD
2991 if (!win_valid(wp)) /* autocommands messed wp up */
2992 {
2993 nextwp = firstwin;
2994 continue;
2995 }
2996#endif
2997 if (!r)
2998 {
2999#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3000 if (message && (p_confirm || cmdmod.confirm) && p_write)
3001 {
3002 dialog_changed(wp->w_buffer, FALSE);
3003# ifdef FEAT_AUTOCMD
3004 if (!win_valid(wp)) /* autocommands messed wp up */
3005 {
3006 nextwp = firstwin;
3007 continue;
3008 }
3009# endif
3010 }
3011 if (bufIsChanged(wp->w_buffer))
3012#endif
3013 continue;
3014 }
3015 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3016 }
3017 }
3018
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003019 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 EMSG(_("E445: Other window contains changes"));
3021}
3022
3023#endif /* FEAT_WINDOWS */
3024
3025/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003026 * Init the current window "curwin".
3027 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 */
3029 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003030curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003032 redraw_win_later(curwin, NOT_VALID);
3033 curwin->w_lines_valid = 0;
3034 curwin->w_cursor.lnum = 1;
3035 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003037 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003039 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3040 curwin->w_pcmark.col = 0;
3041 curwin->w_prev_pcmark.lnum = 0;
3042 curwin->w_prev_pcmark.col = 0;
3043 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003045 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003047 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048#ifdef FEAT_FKMAP
3049 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003050 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003052 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053#endif
3054}
3055
3056/*
3057 * Allocate the first window and put an empty buffer in it.
3058 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003059 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003061 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062win_alloc_first()
3063{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003064 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003065 return FAIL;
3066
3067#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003068 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003069 if (first_tabpage == NULL)
3070 return FAIL;
3071 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003072 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003073#endif
3074 return OK;
3075}
3076
3077/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003078 * Allocate the first window or the first window in a new tab page.
3079 * When "oldwin" is NULL create an empty buffer for it.
3080 * When "oldwin" is not NULL copy info from it to the new window (only with
3081 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003082 * Return FAIL when something goes wrong (out of memory).
3083 */
3084 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003085win_alloc_firstwin(oldwin)
3086 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003087{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003089 if (oldwin == NULL)
3090 {
3091 /* Very first window, need to create an empty buffer for it and
3092 * initialize from scratch. */
3093 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3094 if (curwin == NULL || curbuf == NULL)
3095 return FAIL;
3096 curwin->w_buffer = curbuf;
3097 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003099 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003101 curwin_init(); /* init current window */
3102 }
3103#ifdef FEAT_WINDOWS
3104 else
3105 {
3106 /* First window in new tab page, initialize it from "oldwin". */
3107 win_init(curwin, oldwin);
3108
3109# ifdef FEAT_SCROLLBIND
3110 /* We don't want scroll-binding in the first window. */
3111 curwin->w_p_scb = FALSE;
3112# endif
3113 }
3114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115
3116 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3117 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003118 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 topframe->fr_layout = FR_LEAF;
3120#ifdef FEAT_VERTSPLIT
3121 topframe->fr_width = Columns;
3122#endif
3123 topframe->fr_height = Rows - p_ch;
3124 topframe->fr_win = curwin;
3125 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003126
3127 return OK;
3128}
3129
3130/*
3131 * Initialize the window and frame size to the maximum.
3132 */
3133 void
3134win_init_size()
3135{
3136 firstwin->w_height = ROWS_AVAIL;
3137 topframe->fr_height = ROWS_AVAIL;
3138#ifdef FEAT_VERTSPLIT
3139 firstwin->w_width = Columns;
3140 topframe->fr_width = Columns;
3141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142}
3143
3144#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003145
3146/*
3147 * Allocate a new tabpage_T and init the values.
3148 * Returns NULL when out of memory.
3149 */
3150 static tabpage_T *
3151alloc_tabpage()
3152{
3153 tabpage_T *tp;
3154
3155 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3156 if (tp != NULL)
3157 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003158# ifdef FEAT_GUI
3159 int i;
3160
3161 for (i = 0; i < 3; i++)
3162 tp->tp_prev_which_scrollbars[i] = -1;
3163# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003164# ifdef FEAT_DIFF
3165 tp->tp_diff_invalid = TRUE;
3166# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003167#ifdef FEAT_EVAL
3168 /* init t: variables */
3169 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3170#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003171 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003172 }
3173 return tp;
3174}
3175
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003176 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003177free_tabpage(tp)
3178 tabpage_T *tp;
3179{
3180# ifdef FEAT_DIFF
3181 diff_clear(tp);
3182# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003183 clear_snapshot(tp);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003184#ifdef FEAT_EVAL
3185 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3186#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003187 vim_free(tp);
3188}
3189
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003190/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003191 * Create a new Tab page with one window.
3192 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003193 * When "after" is 0 put it just after the current Tab page.
3194 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003195 * Return FAIL or OK.
3196 */
3197 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003198win_new_tabpage(after)
3199 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003200{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003201 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003202 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003203 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003204
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003205 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003206 if (newtp == NULL)
3207 return FAIL;
3208
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003209 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003210 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003211 {
3212 vim_free(newtp);
3213 return FAIL;
3214 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003215 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003216
3217 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003218 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003219 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003220 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003221 if (after == 1)
3222 {
3223 /* New tab page becomes the first one. */
3224 newtp->tp_next = first_tabpage;
3225 first_tabpage = newtp;
3226 }
3227 else
3228 {
3229 if (after > 0)
3230 {
3231 /* Put new tab page before tab page "after". */
3232 n = 2;
3233 for (tp = first_tabpage; tp->tp_next != NULL
3234 && n < after; tp = tp->tp_next)
3235 ++n;
3236 }
3237 newtp->tp_next = tp->tp_next;
3238 tp->tp_next = newtp;
3239 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003240 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003241 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003242 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003243
3244 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003245 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003246
3247#if defined(FEAT_GUI)
3248 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3249 * scrollbars. Have to update them anyway. */
3250 if (gui.in_use && starting == 0)
3251 {
3252 gui_init_which_components(NULL);
3253 gui_update_scrollbars(TRUE);
3254 }
3255 need_mouse_correct = TRUE;
3256#endif
3257
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003258 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003259#ifdef FEAT_AUTOCMD
3260 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3261 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3262#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003263 return OK;
3264 }
3265
3266 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003267 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003268 return FAIL;
3269}
3270
3271/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003272 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3273 * like with ":split".
3274 * Returns OK if a new tab page was created, FAIL otherwise.
3275 */
3276 int
3277may_open_tabpage()
3278{
3279 int n = cmdmod.tab;
3280
3281 if (cmdmod.tab != 0)
3282 {
3283 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3284 return win_new_tabpage(n);
3285 }
3286 return FAIL;
3287}
3288
3289/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003290 * Create up to "maxcount" tabpages with empty windows.
3291 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003292 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003293 int
3294make_tabpages(maxcount)
3295 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003296{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003297 int count = maxcount;
3298 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003299
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003300 /* Limit to 'tabpagemax' tabs. */
3301 if (count > p_tpm)
3302 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003303
3304#ifdef FEAT_AUTOCMD
3305 /*
3306 * Don't execute autocommands while creating the tab pages. Must do that
3307 * when putting the buffers in the windows.
3308 */
3309 ++autocmd_block;
3310#endif
3311
3312 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003313 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003314 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003315
3316#ifdef FEAT_AUTOCMD
3317 --autocmd_block;
3318#endif
3319
3320 /* return actual number of tab pages */
3321 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003322}
3323
3324/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003325 * Return TRUE when "tpc" points to a valid tab page.
3326 */
3327 int
3328valid_tabpage(tpc)
3329 tabpage_T *tpc;
3330{
3331 tabpage_T *tp;
3332
3333 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3334 if (tp == tpc)
3335 return TRUE;
3336 return FALSE;
3337}
3338
3339/*
3340 * Find tab page "n" (first one is 1). Returns NULL when not found.
3341 */
3342 tabpage_T *
3343find_tabpage(n)
3344 int n;
3345{
3346 tabpage_T *tp;
3347 int i = 1;
3348
3349 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3350 ++i;
3351 return tp;
3352}
3353
3354/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003355 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003356 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003357 */
3358 int
3359tabpage_index(ftp)
3360 tabpage_T *ftp;
3361{
3362 int i = 1;
3363 tabpage_T *tp;
3364
3365 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3366 ++i;
3367 return i;
3368}
3369
3370/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003371 * Prepare for leaving the current tab page.
3372 * When autocomands change "curtab" we don't leave the tab page and return
3373 * FAIL.
3374 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003375 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003376/*ARGSUSED*/
3377 static int
3378leave_tabpage(new_curbuf)
3379 buf_T *new_curbuf; /* what is going to be the new curbuf,
3380 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003381{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003382 tabpage_T *tp = curtab;
3383
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003384#ifdef FEAT_VISUAL
3385 reset_VIsual_and_resel(); /* stop Visual mode */
3386#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003387#ifdef FEAT_AUTOCMD
3388 if (new_curbuf != curbuf)
3389 {
3390 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3391 if (curtab != tp)
3392 return FAIL;
3393 }
3394 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3395 if (curtab != tp)
3396 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003397 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003398 if (curtab != tp)
3399 return FAIL;
3400#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003401#if defined(FEAT_GUI)
3402 /* Remove the scrollbars. They may be added back later. */
3403 if (gui.in_use)
3404 gui_remove_scrollbars();
3405#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003406 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003407 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003408 tp->tp_firstwin = firstwin;
3409 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003410 tp->tp_old_Rows = Rows;
3411 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003412 firstwin = NULL;
3413 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003414 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003415}
3416
3417/*
3418 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003419 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003420 */
3421/*ARGSUSED*/
3422 static void
3423enter_tabpage(tp, old_curbuf)
3424 tabpage_T *tp;
3425 buf_T *old_curbuf;
3426{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003427 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003428 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003429
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003430 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003431 firstwin = tp->tp_firstwin;
3432 lastwin = tp->tp_lastwin;
3433 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003434
3435 /* We would like doing the TabEnter event first, but we don't have a
3436 * valid current window yet, which may break some commands.
3437 * This triggers autocommands, thus may make "tp" invalid. */
3438 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3439 prevwin = next_prevwin;
3440
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003441#ifdef FEAT_AUTOCMD
3442 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003443
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003444 if (old_curbuf != curbuf)
3445 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3446#endif
3447
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003448 last_status(FALSE); /* status line may appear or disappear */
3449 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003450 must_redraw = CLEAR; /* need to redraw everything */
3451#ifdef FEAT_DIFF
3452 diff_need_scrollbind = TRUE;
3453#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003454
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003455 /* The tabpage line may have appeared or disappeared, may need to resize
3456 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003457 * frame sizes too. Use the stored value of p_ch, so that it can be
3458 * different for each tab page. */
3459 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003460 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3461#ifdef FEAT_GUI_TABLINE
3462 && !gui_use_tabline()
3463#endif
3464 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003465 shell_new_rows();
3466#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003467 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003468 shell_new_columns(); /* update window widths */
3469#endif
3470
3471#if defined(FEAT_GUI)
3472 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3473 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003474 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003475 {
3476 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003477 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003478 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003479 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003480#endif
3481
3482 redraw_all_later(CLEAR);
3483}
3484
3485/*
3486 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003487 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003488 */
3489 void
3490goto_tabpage(n)
3491 int n;
3492{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003493 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003494 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003495 int i;
3496
Bram Moolenaard68071d2006-05-02 22:08:30 +00003497 if (text_locked())
3498 {
3499 /* Not allowed when editing the command line. */
3500#ifdef FEAT_CMDWIN
3501 if (cmdwin_type != 0)
3502 EMSG(_(e_cmdwin));
3503 else
3504#endif
3505 EMSG(_(e_secure));
3506 return;
3507 }
3508
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003509 /* If there is only one it can't work. */
3510 if (first_tabpage->tp_next == NULL)
3511 {
3512 if (n > 1)
3513 beep_flush();
3514 return;
3515 }
3516
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003517 if (n == 0)
3518 {
3519 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003520 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003521 tp = first_tabpage;
3522 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003523 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003524 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003525 else if (n < 0)
3526 {
3527 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3528 * this N times. */
3529 ttp = curtab;
3530 for (i = n; i < 0; ++i)
3531 {
3532 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3533 tp = tp->tp_next)
3534 ;
3535 ttp = tp;
3536 }
3537 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003538 else if (n == 9999)
3539 {
3540 /* Go to last tab page. */
3541 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3542 ;
3543 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003544 else
3545 {
3546 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003547 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003548 if (tp == NULL)
3549 {
3550 beep_flush();
3551 return;
3552 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003553 }
3554
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003555 goto_tabpage_tp(tp);
3556
3557#ifdef FEAT_GUI_TABLINE
3558 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003559 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003560#endif
3561}
3562
3563/*
3564 * Go to tabpage "tp".
3565 * Note: doesn't update the GUI tab.
3566 */
3567 void
3568goto_tabpage_tp(tp)
3569 tabpage_T *tp;
3570{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003571 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003572 {
3573 if (valid_tabpage(tp))
3574 enter_tabpage(tp, curbuf);
3575 else
3576 enter_tabpage(curtab, curbuf);
3577 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003578}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579
3580/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003581 * Enter window "wp" in tab page "tp".
3582 * Also updates the GUI tab.
3583 */
3584 void
3585goto_tabpage_win(tp, wp)
3586 tabpage_T *tp;
3587 win_T *wp;
3588{
3589 goto_tabpage_tp(tp);
3590 if (curtab == tp && win_valid(wp))
3591 {
3592 win_enter(wp, TRUE);
3593# ifdef FEAT_GUI_TABLINE
3594 if (gui_use_tabline())
3595 gui_mch_set_curtab(tabpage_index(curtab));
3596# endif
3597 }
3598}
3599
3600/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003601 * Move the current tab page to before tab page "nr".
3602 */
3603 void
3604tabpage_move(nr)
3605 int nr;
3606{
3607 int n = nr;
3608 tabpage_T *tp;
3609
3610 if (first_tabpage->tp_next == NULL)
3611 return;
3612
3613 /* Remove the current tab page from the list of tab pages. */
3614 if (curtab == first_tabpage)
3615 first_tabpage = curtab->tp_next;
3616 else
3617 {
3618 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3619 if (tp->tp_next == curtab)
3620 break;
3621 if (tp == NULL) /* "cannot happen" */
3622 return;
3623 tp->tp_next = curtab->tp_next;
3624 }
3625
3626 /* Re-insert it at the specified position. */
3627 if (n == 0)
3628 {
3629 curtab->tp_next = first_tabpage;
3630 first_tabpage = curtab;
3631 }
3632 else
3633 {
3634 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3635 --n;
3636 curtab->tp_next = tp->tp_next;
3637 tp->tp_next = curtab;
3638 }
3639
3640 /* Need to redraw the tabline. Tab page contents doesn't change. */
3641 redraw_tabline = TRUE;
3642}
3643
3644
3645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 * Go to another window.
3647 * When jumping to another buffer, stop Visual mode. Do this before
3648 * changing windows so we can yank the selection into the '*' register.
3649 * When jumping to another window on the same buffer, adjust its cursor
3650 * position to keep the same Visual area.
3651 */
3652 void
3653win_goto(wp)
3654 win_T *wp;
3655{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003656 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
3658 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003659 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 return;
3661 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003662#ifdef FEAT_AUTOCMD
3663 if (curbuf_locked())
3664 return;
3665#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003666
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667#ifdef FEAT_VISUAL
3668 if (wp->w_buffer != curbuf)
3669 reset_VIsual_and_resel();
3670 else if (VIsual_active)
3671 wp->w_cursor = curwin->w_cursor;
3672#endif
3673
3674#ifdef FEAT_GUI
3675 need_mouse_correct = TRUE;
3676#endif
3677 win_enter(wp, TRUE);
3678}
3679
3680#if defined(FEAT_PERL) || defined(PROTO)
3681/*
3682 * Find window number "winnr" (counting top to bottom).
3683 */
3684 win_T *
3685win_find_nr(winnr)
3686 int winnr;
3687{
3688 win_T *wp;
3689
3690# ifdef FEAT_WINDOWS
3691 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3692 if (--winnr == 0)
3693 break;
3694 return wp;
3695# else
3696 return curwin;
3697# endif
3698}
3699#endif
3700
3701#ifdef FEAT_VERTSPLIT
3702/*
3703 * Move to window above or below "count" times.
3704 */
3705 static void
3706win_goto_ver(up, count)
3707 int up; /* TRUE to go to win above */
3708 long count;
3709{
3710 frame_T *fr;
3711 frame_T *nfr;
3712 frame_T *foundfr;
3713
3714 foundfr = curwin->w_frame;
3715 while (count--)
3716 {
3717 /*
3718 * First go upwards in the tree of frames until we find a upwards or
3719 * downwards neighbor.
3720 */
3721 fr = foundfr;
3722 for (;;)
3723 {
3724 if (fr == topframe)
3725 goto end;
3726 if (up)
3727 nfr = fr->fr_prev;
3728 else
3729 nfr = fr->fr_next;
3730 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3731 break;
3732 fr = fr->fr_parent;
3733 }
3734
3735 /*
3736 * Now go downwards to find the bottom or top frame in it.
3737 */
3738 for (;;)
3739 {
3740 if (nfr->fr_layout == FR_LEAF)
3741 {
3742 foundfr = nfr;
3743 break;
3744 }
3745 fr = nfr->fr_child;
3746 if (nfr->fr_layout == FR_ROW)
3747 {
3748 /* Find the frame at the cursor row. */
3749 while (fr->fr_next != NULL
3750 && frame2win(fr)->w_wincol + fr->fr_width
3751 <= curwin->w_wincol + curwin->w_wcol)
3752 fr = fr->fr_next;
3753 }
3754 if (nfr->fr_layout == FR_COL && up)
3755 while (fr->fr_next != NULL)
3756 fr = fr->fr_next;
3757 nfr = fr;
3758 }
3759 }
3760end:
3761 if (foundfr != NULL)
3762 win_goto(foundfr->fr_win);
3763}
3764
3765/*
3766 * Move to left or right window.
3767 */
3768 static void
3769win_goto_hor(left, count)
3770 int left; /* TRUE to go to left win */
3771 long count;
3772{
3773 frame_T *fr;
3774 frame_T *nfr;
3775 frame_T *foundfr;
3776
3777 foundfr = curwin->w_frame;
3778 while (count--)
3779 {
3780 /*
3781 * First go upwards in the tree of frames until we find a left or
3782 * right neighbor.
3783 */
3784 fr = foundfr;
3785 for (;;)
3786 {
3787 if (fr == topframe)
3788 goto end;
3789 if (left)
3790 nfr = fr->fr_prev;
3791 else
3792 nfr = fr->fr_next;
3793 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3794 break;
3795 fr = fr->fr_parent;
3796 }
3797
3798 /*
3799 * Now go downwards to find the leftmost or rightmost frame in it.
3800 */
3801 for (;;)
3802 {
3803 if (nfr->fr_layout == FR_LEAF)
3804 {
3805 foundfr = nfr;
3806 break;
3807 }
3808 fr = nfr->fr_child;
3809 if (nfr->fr_layout == FR_COL)
3810 {
3811 /* Find the frame at the cursor row. */
3812 while (fr->fr_next != NULL
3813 && frame2win(fr)->w_winrow + fr->fr_height
3814 <= curwin->w_winrow + curwin->w_wrow)
3815 fr = fr->fr_next;
3816 }
3817 if (nfr->fr_layout == FR_ROW && left)
3818 while (fr->fr_next != NULL)
3819 fr = fr->fr_next;
3820 nfr = fr;
3821 }
3822 }
3823end:
3824 if (foundfr != NULL)
3825 win_goto(foundfr->fr_win);
3826}
3827#endif
3828
3829/*
3830 * Make window "wp" the current window.
3831 */
3832 void
3833win_enter(wp, undo_sync)
3834 win_T *wp;
3835 int undo_sync;
3836{
3837 win_enter_ext(wp, undo_sync, FALSE);
3838}
3839
3840/*
3841 * Make window wp the current window.
3842 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3843 * been closed and isn't valid.
3844 */
3845 static void
3846win_enter_ext(wp, undo_sync, curwin_invalid)
3847 win_T *wp;
3848 int undo_sync;
3849 int curwin_invalid;
3850{
3851#ifdef FEAT_AUTOCMD
3852 int other_buffer = FALSE;
3853#endif
3854
3855 if (wp == curwin && !curwin_invalid) /* nothing to do */
3856 return;
3857
3858#ifdef FEAT_AUTOCMD
3859 if (!curwin_invalid)
3860 {
3861 /*
3862 * Be careful: If autocommands delete the window, return now.
3863 */
3864 if (wp->w_buffer != curbuf)
3865 {
3866 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3867 other_buffer = TRUE;
3868 if (!win_valid(wp))
3869 return;
3870 }
3871 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3872 if (!win_valid(wp))
3873 return;
3874# ifdef FEAT_EVAL
3875 /* autocmds may abort script processing */
3876 if (aborting())
3877 return;
3878# endif
3879 }
3880#endif
3881
3882 /* sync undo before leaving the current buffer */
3883 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003884 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 /* may have to copy the buffer options when 'cpo' contains 'S' */
3886 if (wp->w_buffer != curbuf)
3887 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3888 if (!curwin_invalid)
3889 {
3890 prevwin = curwin; /* remember for CTRL-W p */
3891 curwin->w_redr_status = TRUE;
3892 }
3893 curwin = wp;
3894 curbuf = wp->w_buffer;
3895 check_cursor();
3896#ifdef FEAT_VIRTUALEDIT
3897 if (!virtual_active())
3898 curwin->w_cursor.coladd = 0;
3899#endif
3900 changed_line_abv_curs(); /* assume cursor position needs updating */
3901
3902 if (curwin->w_localdir != NULL)
3903 {
3904 /* Window has a local directory: Save current directory as global
3905 * directory (unless that was done already) and change to the local
3906 * directory. */
3907 if (globaldir == NULL)
3908 {
3909 char_u cwd[MAXPATHL];
3910
3911 if (mch_dirname(cwd, MAXPATHL) == OK)
3912 globaldir = vim_strsave(cwd);
3913 }
3914 mch_chdir((char *)curwin->w_localdir);
3915 shorten_fnames(TRUE);
3916 }
3917 else if (globaldir != NULL)
3918 {
3919 /* Window doesn't have a local directory and we are not in the global
3920 * directory: Change to the global directory. */
3921 mch_chdir((char *)globaldir);
3922 vim_free(globaldir);
3923 globaldir = NULL;
3924 shorten_fnames(TRUE);
3925 }
3926
3927#ifdef FEAT_AUTOCMD
3928 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3929 if (other_buffer)
3930 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3931#endif
3932
3933#ifdef FEAT_TITLE
3934 maketitle();
3935#endif
3936 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003937 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 if (restart_edit)
3939 redraw_later(VALID); /* causes status line redraw */
3940
3941 /* set window height to desired minimal value */
3942 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3943 win_setheight((int)p_wh);
3944 else if (curwin->w_height == 0)
3945 win_setheight(1);
3946
3947#ifdef FEAT_VERTSPLIT
3948 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003949 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 win_setwidth((int)p_wiw);
3951#endif
3952
3953#ifdef FEAT_MOUSE
3954 setmouse(); /* in case jumped to/from help buffer */
3955#endif
3956
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003957 /* Change directories when the 'acd' option is set. */
3958 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959}
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 Moolenaar9c102382006-05-03 21:26:49 +00005196 int old_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197#define FRACTION_MULT 16384L
5198
5199 /* Don't want a negative height. Happens when splitting a tiny window.
5200 * Will equalize heights soon to fix it. */
5201 if (height < 0)
5202 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005203 if (wp->w_height == height)
5204 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205
5206 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5207 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5208 + FRACTION_MULT / 2) / (long)wp->w_height;
5209
5210 wp->w_height = height;
5211 wp->w_skipcol = 0;
5212
5213 /* Don't change w_topline when height is zero. Don't set w_topline when
5214 * 'scrollbind' is set and this isn't the current window. */
5215 if (height > 0
5216#ifdef FEAT_SCROLLBIND
5217 && (!wp->w_p_scb || wp == curwin)
5218#endif
5219 )
5220 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005221 /*
5222 * Find a value for w_topline that shows the cursor at the same
5223 * relative position in the window as before (more or less).
5224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 lnum = wp->w_cursor.lnum;
5226 if (lnum < 1) /* can happen when starting up */
5227 lnum = 1;
5228 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5229 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5230 sline = wp->w_wrow - line_size;
5231 if (sline < 0)
5232 {
5233 /*
5234 * Cursor line would go off top of screen if w_wrow was this high.
5235 */
5236 wp->w_wrow = line_size;
5237 }
5238 else
5239 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00005240 space = height - 1;
5241
Bram Moolenaar34114692005-01-02 11:28:13 +00005242 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00005244 /* When using "~" lines stop when at the old topline, don't
5245 * scroll down. */
5246 if (did_below && height < old_height && lnum <= wp->w_topline)
5247 sline = 0;
5248
Bram Moolenaar34114692005-01-02 11:28:13 +00005249 space -= line_size;
5250 if (space > 0 && sline <= 0 && !did_below)
5251 {
5252 /* Try to use "~" lines below the text to avoid that text
5253 * is above the window while there are empty lines.
5254 * Subtract the rows below the cursor from "space" and
5255 * give the rest to "sline". */
5256 did_below = TRUE;
5257 bot = wp->w_cursor.lnum;
5258 while (space > 0)
5259 {
5260 if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
5261 space = 0;
5262 else
5263 {
5264#ifdef FEAT_FOLDING
5265 hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
5266#endif
5267 if (bot >= wp->w_buffer->b_ml.ml_line_count)
5268 break;
5269 ++bot;
5270 space -= plines_win(wp, bot, TRUE);
5271 }
5272 }
5273 if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
5274 sline += space;
5275 }
5276 if (sline <= 0)
5277 break;
5278
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279#ifdef FEAT_FOLDING
5280 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5281 if (lnum == 1)
5282 {
5283 /* first line in buffer is folded */
5284 line_size = 1;
5285 --sline;
5286 break;
5287 }
5288#endif
5289 --lnum;
5290#ifdef FEAT_DIFF
5291 if (lnum == wp->w_topline)
5292 line_size = plines_win_nofill(wp, lnum, TRUE)
5293 + wp->w_topfill;
5294 else
5295#endif
5296 line_size = plines_win(wp, lnum, TRUE);
5297 sline -= line_size;
5298 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005299
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (sline < 0)
5301 {
5302 /*
5303 * Line we want at top would go off top of screen. Use next
5304 * line instead.
5305 */
5306#ifdef FEAT_FOLDING
5307 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5308#endif
5309 lnum++;
5310 wp->w_wrow -= line_size + sline;
5311 }
5312 else if (sline > 0)
5313 {
5314 /* First line of file reached, use that as topline. */
5315 lnum = 1;
5316 wp->w_wrow -= sline;
5317 }
5318 }
5319 set_topline(wp, lnum);
5320 }
5321
5322 if (wp == curwin)
5323 {
5324 if (p_so)
5325 update_topline();
5326 curs_columns(FALSE); /* validate w_wrow */
5327 }
5328 wp->w_prev_fraction_row = wp->w_wrow;
5329
5330 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005331 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332#ifdef FEAT_WINDOWS
5333 wp->w_redr_status = TRUE;
5334#endif
5335 invalidate_botline_win(wp);
5336}
5337
5338#ifdef FEAT_VERTSPLIT
5339/*
5340 * Set the width of a window.
5341 */
5342 static void
5343win_new_width(wp, width)
5344 win_T *wp;
5345 int width;
5346{
5347 wp->w_width = width;
5348 wp->w_lines_valid = 0;
5349 changed_line_abv_curs_win(wp);
5350 invalidate_botline_win(wp);
5351 if (wp == curwin)
5352 {
5353 update_topline();
5354 curs_columns(TRUE); /* validate w_wrow */
5355 }
5356 redraw_win_later(wp, NOT_VALID);
5357 wp->w_redr_status = TRUE;
5358}
5359#endif
5360
5361 void
5362win_comp_scroll(wp)
5363 win_T *wp;
5364{
5365 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5366 if (wp->w_p_scr == 0)
5367 wp->w_p_scr = 1;
5368}
5369
5370/*
5371 * command_height: called whenever p_ch has been changed
5372 */
5373 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005374command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375{
5376#ifdef FEAT_WINDOWS
5377 int h;
5378 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005379 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005381 /* Use the value of p_ch that we remembered. This is needed for when the
5382 * GUI starts up, we can't be sure in what order things happen. And when
5383 * p_ch was changed in another tab page. */
5384 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005385
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 /* Find bottom frame with width of screen. */
5387 frp = lastwin->w_frame;
5388# ifdef FEAT_VERTSPLIT
5389 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5390 frp = frp->fr_parent;
5391# endif
5392
5393 /* Avoid changing the height of a window with 'winfixheight' set. */
5394 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5395 && frp->fr_win->w_p_wfh)
5396 frp = frp->fr_prev;
5397
5398 if (starting != NO_SCREEN)
5399 {
5400 cmdline_row = Rows - p_ch;
5401
5402 if (p_ch > old_p_ch) /* p_ch got bigger */
5403 {
5404 while (p_ch > old_p_ch)
5405 {
5406 if (frp == NULL)
5407 {
5408 EMSG(_(e_noroom));
5409 p_ch = old_p_ch;
5410 cmdline_row = Rows - p_ch;
5411 break;
5412 }
5413 h = frp->fr_height - frame_minheight(frp, NULL);
5414 if (h > p_ch - old_p_ch)
5415 h = p_ch - old_p_ch;
5416 old_p_ch += h;
5417 frame_add_height(frp, -h);
5418 frp = frp->fr_prev;
5419 }
5420
5421 /* Recompute window positions. */
5422 (void)win_comp_pos();
5423
5424 /* clear the lines added to cmdline */
5425 if (full_screen)
5426 screen_fill((int)(cmdline_row), (int)Rows, 0,
5427 (int)Columns, ' ', ' ', 0);
5428 msg_row = cmdline_row;
5429 redraw_cmdline = TRUE;
5430 return;
5431 }
5432
5433 if (msg_row < cmdline_row)
5434 msg_row = cmdline_row;
5435 redraw_cmdline = TRUE;
5436 }
5437 frame_add_height(frp, (int)(old_p_ch - p_ch));
5438
5439 /* Recompute window positions. */
5440 if (frp != lastwin->w_frame)
5441 (void)win_comp_pos();
5442#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005444 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445#endif
5446}
5447
5448#if defined(FEAT_WINDOWS) || defined(PROTO)
5449/*
5450 * Resize frame "frp" to be "n" lines higher (negative for less high).
5451 * Also resize the frames it is contained in.
5452 */
5453 static void
5454frame_add_height(frp, n)
5455 frame_T *frp;
5456 int n;
5457{
5458 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5459 for (;;)
5460 {
5461 frp = frp->fr_parent;
5462 if (frp == NULL)
5463 break;
5464 frp->fr_height += n;
5465 }
5466}
5467
5468/*
5469 * Add or remove a status line for the bottom window(s), according to the
5470 * value of 'laststatus'.
5471 */
5472 void
5473last_status(morewin)
5474 int morewin; /* pretend there are two or more windows */
5475{
5476 /* Don't make a difference between horizontal or vertical split. */
5477 last_status_rec(topframe, (p_ls == 2
5478 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5479}
5480
5481 static void
5482last_status_rec(fr, statusline)
5483 frame_T *fr;
5484 int statusline;
5485{
5486 frame_T *fp;
5487 win_T *wp;
5488
5489 if (fr->fr_layout == FR_LEAF)
5490 {
5491 wp = fr->fr_win;
5492 if (wp->w_status_height != 0 && !statusline)
5493 {
5494 /* remove status line */
5495 win_new_height(wp, wp->w_height + 1);
5496 wp->w_status_height = 0;
5497 comp_col();
5498 }
5499 else if (wp->w_status_height == 0 && statusline)
5500 {
5501 /* Find a frame to take a line from. */
5502 fp = fr;
5503 while (fp->fr_height <= frame_minheight(fp, NULL))
5504 {
5505 if (fp == topframe)
5506 {
5507 EMSG(_(e_noroom));
5508 return;
5509 }
5510 /* In a column of frames: go to frame above. If already at
5511 * the top or in a row of frames: go to parent. */
5512 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5513 fp = fp->fr_prev;
5514 else
5515 fp = fp->fr_parent;
5516 }
5517 wp->w_status_height = 1;
5518 if (fp != fr)
5519 {
5520 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5521 frame_fix_height(wp);
5522 (void)win_comp_pos();
5523 }
5524 else
5525 win_new_height(wp, wp->w_height - 1);
5526 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005527 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 }
5529 }
5530#ifdef FEAT_VERTSPLIT
5531 else if (fr->fr_layout == FR_ROW)
5532 {
5533 /* vertically split windows, set status line for each one */
5534 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5535 last_status_rec(fp, statusline);
5536 }
5537#endif
5538 else
5539 {
5540 /* horizontally split window, set status line for last one */
5541 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5542 ;
5543 last_status_rec(fp, statusline);
5544 }
5545}
5546
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005547/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005548 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005549 */
5550 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005551tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005552{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005553#ifdef FEAT_GUI_TABLINE
5554 /* When the GUI has the tabline then this always returns zero. */
5555 if (gui_use_tabline())
5556 return 0;
5557#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005558 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005559 {
5560 case 0: return 0;
5561 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5562 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005563 return 1;
5564}
5565
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566#endif /* FEAT_WINDOWS */
5567
5568#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5569/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005570 * Get the file name at the cursor.
5571 * If Visual mode is active, use the selected text if it's in one line.
5572 * Returns the name in allocated memory, NULL for failure.
5573 */
5574 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005575grab_file_name(count, file_lnum)
5576 long count;
5577 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005578{
5579# ifdef FEAT_VISUAL
5580 if (VIsual_active)
5581 {
5582 int len;
5583 char_u *ptr;
5584
5585 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5586 return NULL;
5587 return find_file_name_in_path(ptr, len,
5588 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5589 }
5590# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005591 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5592 file_lnum);
5593
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005594}
5595
5596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 * Return the file name under or after the cursor.
5598 *
5599 * The 'path' option is searched if the file name is not absolute.
5600 * The string returned has been alloc'ed and should be freed by the caller.
5601 * NULL is returned if the file name or file is not found.
5602 *
5603 * options:
5604 * FNAME_MESS give error messages
5605 * FNAME_EXP expand to path
5606 * FNAME_HYP check for hypertext link
5607 * FNAME_INCL apply "includeexpr"
5608 */
5609 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005610file_name_at_cursor(options, count, file_lnum)
5611 int options;
5612 long count;
5613 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614{
5615 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005616 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5617 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618}
5619
5620/*
5621 * Return the name of the file under or after ptr[col].
5622 * Otherwise like file_name_at_cursor().
5623 */
5624 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005625file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 char_u *line;
5627 int col;
5628 int options;
5629 long count;
5630 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005631 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632{
5633 char_u *ptr;
5634 int len;
5635
5636 /*
5637 * search forward for what could be the start of a file name
5638 */
5639 ptr = line + col;
5640 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005641 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 if (*ptr == NUL) /* nothing found */
5643 {
5644 if (options & FNAME_MESS)
5645 EMSG(_("E446: No file name under cursor"));
5646 return NULL;
5647 }
5648
5649 /*
5650 * Search backward for first char of the file name.
5651 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5652 */
5653 while (ptr > line)
5654 {
5655#ifdef FEAT_MBYTE
5656 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5657 ptr -= len + 1;
5658 else
5659#endif
5660 if (vim_isfilec(ptr[-1])
5661 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5662 --ptr;
5663 else
5664 break;
5665 }
5666
5667 /*
5668 * Search forward for the last char of the file name.
5669 * Also allow "://" when ':' is not in 'isfname'.
5670 */
5671 len = 0;
5672 while (vim_isfilec(ptr[len])
5673 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5674#ifdef FEAT_MBYTE
5675 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005676 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 else
5678#endif
5679 ++len;
5680
5681 /*
5682 * If there is trailing punctuation, remove it.
5683 * But don't remove "..", could be a directory name.
5684 */
5685 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5686 && ptr[len - 2] != '.')
5687 --len;
5688
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005689 if (file_lnum != NULL)
5690 {
5691 char_u *p;
5692
5693 /* Get the number after the file name and a separator character */
5694 p = ptr + len;
5695 p = skipwhite(p);
5696 if (*p != NUL)
5697 {
5698 if (!isdigit(*p))
5699 ++p; /* skip the separator */
5700 p = skipwhite(p);
5701 if (isdigit(*p))
5702 *file_lnum = (int)getdigits(&p);
5703 }
5704 }
5705
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5707}
5708
5709# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5710static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5711
5712 static char_u *
5713eval_includeexpr(ptr, len)
5714 char_u *ptr;
5715 int len;
5716{
5717 char_u *res;
5718
5719 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005720 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005721 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 set_vim_var_string(VV_FNAME, NULL, 0);
5723 return res;
5724}
5725#endif
5726
5727/*
5728 * Return the name of the file ptr[len] in 'path'.
5729 * Otherwise like file_name_at_cursor().
5730 */
5731 char_u *
5732find_file_name_in_path(ptr, len, options, count, rel_fname)
5733 char_u *ptr;
5734 int len;
5735 int options;
5736 long count;
5737 char_u *rel_fname; /* file we are searching relative to */
5738{
5739 char_u *file_name;
5740 int c;
5741# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5742 char_u *tofree = NULL;
5743
5744 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5745 {
5746 tofree = eval_includeexpr(ptr, len);
5747 if (tofree != NULL)
5748 {
5749 ptr = tofree;
5750 len = (int)STRLEN(ptr);
5751 }
5752 }
5753# endif
5754
5755 if (options & FNAME_EXP)
5756 {
5757 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5758 TRUE, rel_fname);
5759
5760# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5761 /*
5762 * If the file could not be found in a normal way, try applying
5763 * 'includeexpr' (unless done already).
5764 */
5765 if (file_name == NULL
5766 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5767 {
5768 tofree = eval_includeexpr(ptr, len);
5769 if (tofree != NULL)
5770 {
5771 ptr = tofree;
5772 len = (int)STRLEN(ptr);
5773 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5774 TRUE, rel_fname);
5775 }
5776 }
5777# endif
5778 if (file_name == NULL && (options & FNAME_MESS))
5779 {
5780 c = ptr[len];
5781 ptr[len] = NUL;
5782 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5783 ptr[len] = c;
5784 }
5785
5786 /* Repeat finding the file "count" times. This matters when it
5787 * appears several times in the path. */
5788 while (file_name != NULL && --count > 0)
5789 {
5790 vim_free(file_name);
5791 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5792 }
5793 }
5794 else
5795 file_name = vim_strnsave(ptr, len);
5796
5797# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5798 vim_free(tofree);
5799# endif
5800
5801 return file_name;
5802}
5803#endif /* FEAT_SEARCHPATH */
5804
5805/*
5806 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5807 * Also check for ":\\", which MS Internet Explorer accepts, return
5808 * URL_BACKSLASH.
5809 */
5810 static int
5811path_is_url(p)
5812 char_u *p;
5813{
5814 if (STRNCMP(p, "://", (size_t)3) == 0)
5815 return URL_SLASH;
5816 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5817 return URL_BACKSLASH;
5818 return 0;
5819}
5820
5821/*
5822 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5823 * Return URL_BACKSLASH for "name:\\".
5824 * Return zero otherwise.
5825 */
5826 int
5827path_with_url(fname)
5828 char_u *fname;
5829{
5830 char_u *p;
5831
5832 for (p = fname; isalpha(*p); ++p)
5833 ;
5834 return path_is_url(p);
5835}
5836
5837/*
5838 * Return TRUE if "name" is a full (absolute) path name or URL.
5839 */
5840 int
5841vim_isAbsName(name)
5842 char_u *name;
5843{
5844 return (path_with_url(name) != 0 || mch_isFullName(name));
5845}
5846
5847/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005848 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 *
5850 * return FAIL for failure, OK otherwise
5851 */
5852 int
5853vim_FullName(fname, buf, len, force)
5854 char_u *fname, *buf;
5855 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005856 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857{
5858 int retval = OK;
5859 int url;
5860
5861 *buf = NUL;
5862 if (fname == NULL)
5863 return FAIL;
5864
5865 url = path_with_url(fname);
5866 if (!url)
5867 retval = mch_FullName(fname, buf, len, force);
5868 if (url || retval == FAIL)
5869 {
5870 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005871 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 }
5873#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5874 slash_adjust(buf);
5875#endif
5876 return retval;
5877}
5878
5879/*
5880 * Return the minimal number of rows that is needed on the screen to display
5881 * the current number of windows.
5882 */
5883 int
5884min_rows()
5885{
5886 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005887#ifdef FEAT_WINDOWS
5888 tabpage_T *tp;
5889 int n;
5890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891
5892 if (firstwin == NULL) /* not initialized yet */
5893 return MIN_LINES;
5894
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005896 total = 0;
5897 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5898 {
5899 n = frame_minheight(tp->tp_topframe, NULL);
5900 if (total < n)
5901 total = n;
5902 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005903 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005905 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005907 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 return total;
5909}
5910
5911/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005912 * Return TRUE if there is only one window (in the current tab page), not
5913 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 */
5915 int
5916only_one_window()
5917{
5918#ifdef FEAT_WINDOWS
5919 int count = 0;
5920 win_T *wp;
5921
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005922 /* If there is another tab page there always is another window. */
5923 if (first_tabpage->tp_next != NULL)
5924 return FALSE;
5925
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005927 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928# ifdef FEAT_QUICKFIX
5929 || wp->w_p_pvw
5930# endif
5931 ) || wp == curwin)
5932 ++count;
5933 return (count <= 1);
5934#else
5935 return TRUE;
5936#endif
5937}
5938
5939#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5940/*
5941 * Correct the cursor line number in other windows. Used after changing the
5942 * current buffer, and before applying autocommands.
5943 * When "do_curwin" is TRUE, also check current window.
5944 */
5945 void
5946check_lnums(do_curwin)
5947 int do_curwin;
5948{
5949 win_T *wp;
5950
5951#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005952 tabpage_T *tp;
5953
5954 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5956#else
5957 wp = curwin;
5958 if (do_curwin)
5959#endif
5960 {
5961 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5962 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5963 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5964 wp->w_topline = curbuf->b_ml.ml_line_count;
5965 }
5966}
5967#endif
5968
5969#if defined(FEAT_WINDOWS) || defined(PROTO)
5970
5971/*
5972 * A snapshot of the window sizes, to restore them after closing the help
5973 * window.
5974 * Only these fields are used:
5975 * fr_layout
5976 * fr_width
5977 * fr_height
5978 * fr_next
5979 * fr_child
5980 * fr_win (only valid for the old curwin, NULL otherwise)
5981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982
5983/*
5984 * Create a snapshot of the current frame sizes.
5985 */
5986 static void
5987make_snapshot()
5988{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005989 clear_snapshot(curtab);
5990 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991}
5992
5993 static void
5994make_snapshot_rec(fr, frp)
5995 frame_T *fr;
5996 frame_T **frp;
5997{
5998 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5999 if (*frp == NULL)
6000 return;
6001 (*frp)->fr_layout = fr->fr_layout;
6002# ifdef FEAT_VERTSPLIT
6003 (*frp)->fr_width = fr->fr_width;
6004# endif
6005 (*frp)->fr_height = fr->fr_height;
6006 if (fr->fr_next != NULL)
6007 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6008 if (fr->fr_child != NULL)
6009 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6010 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6011 (*frp)->fr_win = curwin;
6012}
6013
6014/*
6015 * Remove any existing snapshot.
6016 */
6017 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006018clear_snapshot(tp)
6019 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006021 clear_snapshot_rec(tp->tp_snapshot);
6022 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023}
6024
6025 static void
6026clear_snapshot_rec(fr)
6027 frame_T *fr;
6028{
6029 if (fr != NULL)
6030 {
6031 clear_snapshot_rec(fr->fr_next);
6032 clear_snapshot_rec(fr->fr_child);
6033 vim_free(fr);
6034 }
6035}
6036
6037/*
6038 * Restore a previously created snapshot, if there is any.
6039 * This is only done if the screen size didn't change and the window layout is
6040 * still the same.
6041 */
6042 static void
6043restore_snapshot(close_curwin)
6044 int close_curwin; /* closing current window */
6045{
6046 win_T *wp;
6047
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006048 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006050 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006052 && curtab->tp_snapshot->fr_height == topframe->fr_height
6053 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006055 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 win_comp_pos();
6057 if (wp != NULL && close_curwin)
6058 win_goto(wp);
6059 redraw_all_later(CLEAR);
6060 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006061 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062}
6063
6064/*
6065 * Check if frames "sn" and "fr" have the same layout, same following frames
6066 * and same children.
6067 */
6068 static int
6069check_snapshot_rec(sn, fr)
6070 frame_T *sn;
6071 frame_T *fr;
6072{
6073 if (sn->fr_layout != fr->fr_layout
6074 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6075 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6076 || (sn->fr_next != NULL
6077 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6078 || (sn->fr_child != NULL
6079 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6080 return FAIL;
6081 return OK;
6082}
6083
6084/*
6085 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6086 * following frames and children.
6087 * Returns a pointer to the old current window, or NULL.
6088 */
6089 static win_T *
6090restore_snapshot_rec(sn, fr)
6091 frame_T *sn;
6092 frame_T *fr;
6093{
6094 win_T *wp = NULL;
6095 win_T *wp2;
6096
6097 fr->fr_height = sn->fr_height;
6098# ifdef FEAT_VERTSPLIT
6099 fr->fr_width = sn->fr_width;
6100# endif
6101 if (fr->fr_layout == FR_LEAF)
6102 {
6103 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6104# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006105 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106# endif
6107 wp = sn->fr_win;
6108 }
6109 if (sn->fr_next != NULL)
6110 {
6111 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6112 if (wp2 != NULL)
6113 wp = wp2;
6114 }
6115 if (sn->fr_child != NULL)
6116 {
6117 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6118 if (wp2 != NULL)
6119 wp = wp2;
6120 }
6121 return wp;
6122}
6123
6124#endif
6125
6126#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6127/*
6128 * Return TRUE if there is any vertically split window.
6129 */
6130 int
6131win_hasvertsplit()
6132{
6133 frame_T *fr;
6134
6135 if (topframe->fr_layout == FR_ROW)
6136 return TRUE;
6137
6138 if (topframe->fr_layout == FR_COL)
6139 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6140 if (fr->fr_layout == FR_ROW)
6141 return TRUE;
6142
6143 return FALSE;
6144}
6145#endif