blob: 40aa2f062bf64bf4eb30a1459ba4cdda45af5c50 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#include "vim.h"
11
12#ifdef HAVE_FCNTL_H
13# include <fcntl.h> /* for chdir() */
14#endif
15
16static int path_is_url __ARGS((char_u *p));
17#if defined(FEAT_WINDOWS) || defined(PROTO)
18static int win_split_ins __ARGS((int size, int flags, win_T *newwin, int dir));
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000019static void win_init __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
21static void frame_setheight __ARGS((frame_T *curfrp, int height));
22#ifdef FEAT_VERTSPLIT
23static void frame_setwidth __ARGS((frame_T *curfrp, int width));
24#endif
25static void win_exchange __ARGS((long));
26static void win_rotate __ARGS((int, int));
27static void win_totop __ARGS((int size, int flags));
28static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000029static int last_window __ARGS((void));
Bram Moolenaarf740b292006-02-16 22:11:02 +000030static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
31static win_T *winframe_remove __ARGS((win_T *win, int *dirp, tabpage_T *tp));
32static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000033static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000034static win_T *frame2win __ARGS((frame_T *frp));
35static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
36static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
37static int frame_fixed_height __ARGS((frame_T *frp));
38#ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000039static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000040static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000041static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000042static void frame_add_vsep __ARGS((frame_T *frp));
43static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
44static void frame_fix_width __ARGS((win_T *wp));
45#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000046#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000047static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000048#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000049static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000050static int leave_tabpage __ARGS((buf_T *new_curbuf));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000051static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000052static void frame_fix_height __ARGS((win_T *wp));
53static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
54static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
Bram Moolenaarf740b292006-02-16 22:11:02 +000055static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000056static void win_append __ARGS((win_T *, win_T *));
Bram Moolenaarf740b292006-02-16 22:11:02 +000057static void win_remove __ARGS((win_T *, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static void frame_append __ARGS((frame_T *after, frame_T *frp));
59static void frame_insert __ARGS((frame_T *before, frame_T *frp));
60static void frame_remove __ARGS((frame_T *frp));
61#ifdef FEAT_VERTSPLIT
62static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000063static void win_goto_ver __ARGS((int up, long count));
64static void win_goto_hor __ARGS((int left, long count));
65#endif
66static void frame_add_height __ARGS((frame_T *frp, int n));
67static void last_status_rec __ARGS((frame_T *fr, int statusline));
68
69static void make_snapshot __ARGS((void));
70static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000071static void clear_snapshot __ARGS((tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000072static void clear_snapshot_rec __ARGS((frame_T *fr));
73static void restore_snapshot __ARGS((int close_curwin));
74static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
75static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
76
77#endif /* FEAT_WINDOWS */
78static win_T *win_alloc __ARGS((win_T *after));
79static void win_new_height __ARGS((win_T *, int));
80
81#define URL_SLASH 1 /* path_is_url() has found "://" */
82#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
83
84#define NOWIN (win_T *)-1 /* non-exisiting window */
85
Bram Moolenaar05159a02005-02-26 23:04:13 +000086#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000087# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000088#else
89# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000090#endif
91
Bram Moolenaar071d4272004-06-13 20:20:40 +000092#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000093
94static char *m_onlyone = N_("Already only one window");
95
Bram Moolenaar071d4272004-06-13 20:20:40 +000096/*
97 * all CTRL-W window commands are handled here, called from normal_cmd().
98 */
99 void
100do_window(nchar, Prenum, xchar)
101 int nchar;
102 long Prenum;
103 int xchar; /* extra char from ":wincmd gx" or NUL */
104{
105 long Prenum1;
106 win_T *wp;
107#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
108 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000109 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110#endif
111#ifdef FEAT_FIND_ID
112 int type = FIND_DEFINE;
113 int len;
114#endif
115 char_u cbuf[40];
116
117 if (Prenum == 0)
118 Prenum1 = 1;
119 else
120 Prenum1 = Prenum;
121
122#ifdef FEAT_CMDWIN
123# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
124#else
125# define CHECK_CMDWIN
126#endif
127
128 switch (nchar)
129 {
130/* split current window in two parts, horizontally */
131 case 'S':
132 case Ctrl_S:
133 case 's':
134 CHECK_CMDWIN
135#ifdef FEAT_VISUAL
136 reset_VIsual_and_resel(); /* stop Visual mode */
137#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000138#ifdef FEAT_QUICKFIX
139 /* When splitting the quickfix window open a new buffer in it,
140 * don't replicate the quickfix buffer. */
141 if (bt_quickfix(curbuf))
142 goto newwindow;
143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#ifdef FEAT_GUI
145 need_mouse_correct = TRUE;
146#endif
147 win_split((int)Prenum, 0);
148 break;
149
150#ifdef FEAT_VERTSPLIT
151/* split current window in two parts, vertically */
152 case Ctrl_V:
153 case 'v':
154 CHECK_CMDWIN
155#ifdef FEAT_VISUAL
156 reset_VIsual_and_resel(); /* stop Visual mode */
157#endif
158#ifdef FEAT_GUI
159 need_mouse_correct = TRUE;
160#endif
161 win_split((int)Prenum, WSP_VERT);
162 break;
163#endif
164
165/* split current window and edit alternate file */
166 case Ctrl_HAT:
167 case '^':
168 CHECK_CMDWIN
169#ifdef FEAT_VISUAL
170 reset_VIsual_and_resel(); /* stop Visual mode */
171#endif
172 STRCPY(cbuf, "split #");
173 if (Prenum)
174 sprintf((char *)cbuf + 7, "%ld", Prenum);
175 do_cmdline_cmd(cbuf);
176 break;
177
178/* open new window */
179 case Ctrl_N:
180 case 'n':
181 CHECK_CMDWIN
182#ifdef FEAT_VISUAL
183 reset_VIsual_and_resel(); /* stop Visual mode */
184#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000185#ifdef FEAT_QUICKFIX
186newwindow:
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 if (Prenum)
189 sprintf((char *)cbuf, "%ld", Prenum); /* window height */
190 else
191 cbuf[0] = NUL;
192 STRCAT(cbuf, "new");
193 do_cmdline_cmd(cbuf);
194 break;
195
196/* quit current window */
197 case Ctrl_Q:
198 case 'q':
199#ifdef FEAT_VISUAL
200 reset_VIsual_and_resel(); /* stop Visual mode */
201#endif
202 do_cmdline_cmd((char_u *)"quit");
203 break;
204
205/* close current window */
206 case Ctrl_C:
207 case 'c':
208#ifdef FEAT_VISUAL
209 reset_VIsual_and_resel(); /* stop Visual mode */
210#endif
211 do_cmdline_cmd((char_u *)"close");
212 break;
213
214#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
215/* close preview window */
216 case Ctrl_Z:
217 case 'z':
218 CHECK_CMDWIN
219#ifdef FEAT_VISUAL
220 reset_VIsual_and_resel(); /* stop Visual mode */
221#endif
222 do_cmdline_cmd((char_u *)"pclose");
223 break;
224
225/* cursor to preview window */
226 case 'P':
227 for (wp = firstwin; wp != NULL; wp = wp->w_next)
228 if (wp->w_p_pvw)
229 break;
230 if (wp == NULL)
231 EMSG(_("E441: There is no preview window"));
232 else
233 win_goto(wp);
234 break;
235#endif
236
237/* close all but current window */
238 case Ctrl_O:
239 case 'o':
240 CHECK_CMDWIN
241#ifdef FEAT_VISUAL
242 reset_VIsual_and_resel(); /* stop Visual mode */
243#endif
244 do_cmdline_cmd((char_u *)"only");
245 break;
246
247/* cursor to next window with wrap around */
248 case Ctrl_W:
249 case 'w':
250/* cursor to previous window with wrap around */
251 case 'W':
252 CHECK_CMDWIN
253 if (lastwin == firstwin && Prenum != 1) /* just one window */
254 beep_flush();
255 else
256 {
257 if (Prenum) /* go to specified window */
258 {
259 for (wp = firstwin; --Prenum > 0; )
260 {
261 if (wp->w_next == NULL)
262 break;
263 else
264 wp = wp->w_next;
265 }
266 }
267 else
268 {
269 if (nchar == 'W') /* go to previous window */
270 {
271 wp = curwin->w_prev;
272 if (wp == NULL)
273 wp = lastwin; /* wrap around */
274 }
275 else /* go to next window */
276 {
277 wp = curwin->w_next;
278 if (wp == NULL)
279 wp = firstwin; /* wrap around */
280 }
281 }
282 win_goto(wp);
283 }
284 break;
285
286/* cursor to window below */
287 case 'j':
288 case K_DOWN:
289 case Ctrl_J:
290 CHECK_CMDWIN
291#ifdef FEAT_VERTSPLIT
292 win_goto_ver(FALSE, Prenum1);
293#else
294 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
295 wp = wp->w_next)
296 ;
297 win_goto(wp);
298#endif
299 break;
300
301/* cursor to window above */
302 case 'k':
303 case K_UP:
304 case Ctrl_K:
305 CHECK_CMDWIN
306#ifdef FEAT_VERTSPLIT
307 win_goto_ver(TRUE, Prenum1);
308#else
309 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
310 wp = wp->w_prev)
311 ;
312 win_goto(wp);
313#endif
314 break;
315
316#ifdef FEAT_VERTSPLIT
317/* cursor to left window */
318 case 'h':
319 case K_LEFT:
320 case Ctrl_H:
321 case K_BS:
322 CHECK_CMDWIN
323 win_goto_hor(TRUE, Prenum1);
324 break;
325
326/* cursor to right window */
327 case 'l':
328 case K_RIGHT:
329 case Ctrl_L:
330 CHECK_CMDWIN
331 win_goto_hor(FALSE, Prenum1);
332 break;
333#endif
334
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000335/* move window to new tab page */
336 case 'T':
337 if (firstwin == lastwin)
338 MSG(_(m_onlyone));
339 else
340 {
341 tabpage_T *oldtab = curtab;
342 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000343
344 /* First create a new tab with the window, then go back to
345 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000346 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000347 if (win_new_tabpage((int)Prenum) == OK
348 && valid_tabpage(oldtab))
349 {
350 newtab = curtab;
351 goto_tabpage_tp(oldtab);
352 if (curwin == wp)
353 win_close(curwin, FALSE);
354 if (valid_tabpage(newtab))
355 goto_tabpage_tp(newtab);
356 }
357 }
358 break;
359
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360/* cursor to top-left window */
361 case 't':
362 case Ctrl_T:
363 win_goto(firstwin);
364 break;
365
366/* cursor to bottom-right window */
367 case 'b':
368 case Ctrl_B:
369 win_goto(lastwin);
370 break;
371
372/* cursor to last accessed (previous) window */
373 case 'p':
374 case Ctrl_P:
375 if (prevwin == NULL)
376 beep_flush();
377 else
378 win_goto(prevwin);
379 break;
380
381/* exchange current and next window */
382 case 'x':
383 case Ctrl_X:
384 CHECK_CMDWIN
385 win_exchange(Prenum);
386 break;
387
388/* rotate windows downwards */
389 case Ctrl_R:
390 case 'r':
391 CHECK_CMDWIN
392#ifdef FEAT_VISUAL
393 reset_VIsual_and_resel(); /* stop Visual mode */
394#endif
395 win_rotate(FALSE, (int)Prenum1); /* downwards */
396 break;
397
398/* rotate windows upwards */
399 case 'R':
400 CHECK_CMDWIN
401#ifdef FEAT_VISUAL
402 reset_VIsual_and_resel(); /* stop Visual mode */
403#endif
404 win_rotate(TRUE, (int)Prenum1); /* upwards */
405 break;
406
407/* move window to the very top/bottom/left/right */
408 case 'K':
409 case 'J':
410#ifdef FEAT_VERTSPLIT
411 case 'H':
412 case 'L':
413#endif
414 CHECK_CMDWIN
415 win_totop((int)Prenum,
416 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
417 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
418 break;
419
420/* make all windows the same height */
421 case '=':
422#ifdef FEAT_GUI
423 need_mouse_correct = TRUE;
424#endif
425 win_equal(NULL, FALSE, 'b');
426 break;
427
428/* increase current window height */
429 case '+':
430#ifdef FEAT_GUI
431 need_mouse_correct = TRUE;
432#endif
433 win_setheight(curwin->w_height + (int)Prenum1);
434 break;
435
436/* decrease current window height */
437 case '-':
438#ifdef FEAT_GUI
439 need_mouse_correct = TRUE;
440#endif
441 win_setheight(curwin->w_height - (int)Prenum1);
442 break;
443
444/* set current window height */
445 case Ctrl__:
446 case '_':
447#ifdef FEAT_GUI
448 need_mouse_correct = TRUE;
449#endif
450 win_setheight(Prenum ? (int)Prenum : 9999);
451 break;
452
453#ifdef FEAT_VERTSPLIT
454/* increase current window width */
455 case '>':
456#ifdef FEAT_GUI
457 need_mouse_correct = TRUE;
458#endif
459 win_setwidth(curwin->w_width + (int)Prenum1);
460 break;
461
462/* decrease current window width */
463 case '<':
464#ifdef FEAT_GUI
465 need_mouse_correct = TRUE;
466#endif
467 win_setwidth(curwin->w_width - (int)Prenum1);
468 break;
469
470/* set current window width */
471 case '|':
472#ifdef FEAT_GUI
473 need_mouse_correct = TRUE;
474#endif
475 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
476 break;
477#endif
478
479/* jump to tag and split window if tag exists (in preview window) */
480#if defined(FEAT_QUICKFIX)
481 case '}':
482 CHECK_CMDWIN
483 if (Prenum)
484 g_do_tagpreview = Prenum;
485 else
486 g_do_tagpreview = p_pvh;
487 /*FALLTHROUGH*/
488#endif
489 case ']':
490 case Ctrl_RSB:
491 CHECK_CMDWIN
492#ifdef FEAT_VISUAL
493 reset_VIsual_and_resel(); /* stop Visual mode */
494#endif
495 if (Prenum)
496 postponed_split = Prenum;
497 else
498 postponed_split = -1;
499
500 /* Execute the command right here, required when
501 * "wincmd ]" was used in a function. */
502 do_nv_ident(Ctrl_RSB, NUL);
503 break;
504
505#ifdef FEAT_SEARCHPATH
506/* edit file name under cursor in a new window */
507 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000508 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000510wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000512
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000513 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (ptr != NULL)
515 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000516# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 setpcmark();
520 if (win_split(0, 0) == OK)
521 {
522# ifdef FEAT_SCROLLBIND
523 curwin->w_p_scb = FALSE;
524# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000525 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE);
526 if (nchar == 'F' && lnum >= 0)
527 {
528 curwin->w_cursor.lnum = lnum;
529 check_cursor_lnum();
530 beginline(BL_SOL | BL_FIX);
531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 }
533 vim_free(ptr);
534 }
535 break;
536#endif
537
538#ifdef FEAT_FIND_ID
539/* Go to the first occurence of the identifier under cursor along path in a
540 * new window -- webb
541 */
542 case 'i': /* Go to any match */
543 case Ctrl_I:
544 type = FIND_ANY;
545 /* FALLTHROUGH */
546 case 'd': /* Go to definition, using 'define' */
547 case Ctrl_D:
548 CHECK_CMDWIN
549 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
550 break;
551 find_pattern_in_path(ptr, 0, len, TRUE,
552 Prenum == 0 ? TRUE : FALSE, type,
553 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
554 curwin->w_set_curswant = TRUE;
555 break;
556#endif
557
Bram Moolenaar05159a02005-02-26 23:04:13 +0000558 case K_KENTER:
559 case CAR:
560#if defined(FEAT_QUICKFIX)
561 /*
562 * In a quickfix window a <CR> jumps to the error under the
563 * cursor in a new window.
564 */
565 if (bt_quickfix(curbuf))
566 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000567 sprintf((char *)cbuf, "split +%ld%s",
568 (long)curwin->w_cursor.lnum,
569 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000570 do_cmdline_cmd(cbuf);
571 }
572#endif
573 break;
574
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576/* CTRL-W g extended commands */
577 case 'g':
578 case Ctrl_G:
579 CHECK_CMDWIN
580#ifdef USE_ON_FLY_SCROLL
581 dont_scroll = TRUE; /* disallow scrolling here */
582#endif
583 ++no_mapping;
584 ++allow_keys; /* no mapping for xchar, but allow key codes */
585 if (xchar == NUL)
586 xchar = safe_vgetc();
587#ifdef FEAT_LANGMAP
588 LANGMAP_ADJUST(xchar, TRUE);
589#endif
590 --no_mapping;
591 --allow_keys;
592#ifdef FEAT_CMDL_INFO
593 (void)add_to_showcmd(xchar);
594#endif
595 switch (xchar)
596 {
597#if defined(FEAT_QUICKFIX)
598 case '}':
599 xchar = Ctrl_RSB;
600 if (Prenum)
601 g_do_tagpreview = Prenum;
602 else
603 g_do_tagpreview = p_pvh;
604 /*FALLTHROUGH*/
605#endif
606 case ']':
607 case Ctrl_RSB:
608#ifdef FEAT_VISUAL
609 reset_VIsual_and_resel(); /* stop Visual mode */
610#endif
611 if (Prenum)
612 postponed_split = Prenum;
613 else
614 postponed_split = -1;
615
616 /* Execute the command right here, required when
617 * "wincmd g}" was used in a function. */
618 do_nv_ident('g', xchar);
619 break;
620
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000621#ifdef FEAT_SEARCHPATH
622 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000623 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000624 cmdmod.tab = TRUE;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000625 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000626 goto wingotofile;
627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 default:
629 beep_flush();
630 break;
631 }
632 break;
633
634 default: beep_flush();
635 break;
636 }
637}
638
639/*
640 * split the current window, implements CTRL-W s and :split
641 *
642 * "size" is the height or width for the new window, 0 to use half of current
643 * height or width.
644 *
645 * "flags":
646 * WSP_ROOM: require enough room for new window
647 * WSP_VERT: vertical split.
648 * WSP_TOP: open window at the top-left of the shell (help window).
649 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
650 * WSP_HELP: creating the help window, keep layout snapshot
651 *
652 * return FAIL for failure, OK otherwise
653 */
654 int
655win_split(size, flags)
656 int size;
657 int flags;
658{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000659 /* When the ":tab" modifier was used open a new tab page instead. */
660 if (may_open_tabpage() == OK)
661 return OK;
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 /* Add flags from ":vertical", ":topleft" and ":botright". */
664 flags |= cmdmod.split;
665 if ((flags & WSP_TOP) && (flags & WSP_BOT))
666 {
667 EMSG(_("E442: Can't split topleft and botright at the same time"));
668 return FAIL;
669 }
670
671 /* When creating the help window make a snapshot of the window layout.
672 * Otherwise clear the snapshot, it's now invalid. */
673 if (flags & WSP_HELP)
674 make_snapshot();
675 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000676 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678 return win_split_ins(size, flags, NULL, 0);
679}
680
681/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000682 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 * When "newwin" is not NULL: insert this window at the far
684 * top/left/right/bottom.
685 * return FAIL for failure, OK otherwise
686 */
687 static int
688win_split_ins(size, flags, newwin, dir)
689 int size;
690 int flags;
691 win_T *newwin;
692 int dir;
693{
694 win_T *wp = newwin;
695 win_T *oldwin;
696 int new_size = size;
697 int i;
698 int need_status = 0;
699 int do_equal = FALSE;
700 int needed;
701 int available;
702 int oldwin_height = 0;
703 int layout;
704 frame_T *frp, *curfrp;
705 int before;
706
707 if (flags & WSP_TOP)
708 oldwin = firstwin;
709 else if (flags & WSP_BOT)
710 oldwin = lastwin;
711 else
712 oldwin = curwin;
713
714 /* add a status line when p_ls == 1 and splitting the first window */
715 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
716 {
717 if (oldwin->w_height <= p_wmh && newwin == NULL)
718 {
719 EMSG(_(e_noroom));
720 return FAIL;
721 }
722 need_status = STATUS_HEIGHT;
723 }
724
725#ifdef FEAT_VERTSPLIT
726 if (flags & WSP_VERT)
727 {
728 layout = FR_ROW;
729 do_equal = (p_ea && new_size == 0 && *p_ead != 'v');
730
731 /*
732 * Check if we are able to split the current window and compute its
733 * width.
734 */
735 needed = p_wmw + 1;
736 if (flags & WSP_ROOM)
737 needed += p_wiw - p_wmw;
738 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
739 {
740 available = topframe->fr_width;
741 needed += frame_minwidth(topframe, NULL);
742 }
743 else
744 available = oldwin->w_width;
745 if (available < needed && newwin == NULL)
746 {
747 EMSG(_(e_noroom));
748 return FAIL;
749 }
750 if (new_size == 0)
751 new_size = oldwin->w_width / 2;
752 if (new_size > oldwin->w_width - p_wmw - 1)
753 new_size = oldwin->w_width - p_wmw - 1;
754 if (new_size < p_wmw)
755 new_size = p_wmw;
756
757 /* if it doesn't fit in the current window, need win_equal() */
758 if (oldwin->w_width - new_size - 1 < p_wmw)
759 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000760
761 /* We don't like to take lines for the new window from a
762 * 'winfixwidth' window. Take them from a window to the left or right
763 * instead, if possible. */
764 if (oldwin->w_p_wfw)
765 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 }
767 else
768#endif
769 {
770 layout = FR_COL;
771 do_equal = (p_ea && new_size == 0
772#ifdef FEAT_VERTSPLIT
773 && *p_ead != 'h'
774#endif
775 );
776
777 /*
778 * Check if we are able to split the current window and compute its
779 * height.
780 */
781 needed = p_wmh + STATUS_HEIGHT + need_status;
782 if (flags & WSP_ROOM)
783 needed += p_wh - p_wmh;
784 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
785 {
786 available = topframe->fr_height;
787 needed += frame_minheight(topframe, NULL);
788 }
789 else
790 {
791 available = oldwin->w_height;
792 needed += p_wmh;
793 }
794 if (available < needed && newwin == NULL)
795 {
796 EMSG(_(e_noroom));
797 return FAIL;
798 }
799 oldwin_height = oldwin->w_height;
800 if (need_status)
801 {
802 oldwin->w_status_height = STATUS_HEIGHT;
803 oldwin_height -= STATUS_HEIGHT;
804 }
805 if (new_size == 0)
806 new_size = oldwin_height / 2;
807
808 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
809 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
810 if (new_size < p_wmh)
811 new_size = p_wmh;
812
813 /* if it doesn't fit in the current window, need win_equal() */
814 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
815 do_equal = TRUE;
816
817 /* We don't like to take lines for the new window from a
818 * 'winfixheight' window. Take them from a window above or below
819 * instead, if possible. */
820 if (oldwin->w_p_wfh)
821 {
822 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
823 oldwin);
824 oldwin_height = oldwin->w_height;
825 if (need_status)
826 oldwin_height -= STATUS_HEIGHT;
827 }
828 }
829
830 /*
831 * allocate new window structure and link it in the window list
832 */
833 if ((flags & WSP_TOP) == 0
834 && ((flags & WSP_BOT)
835 || (flags & WSP_BELOW)
836 || (!(flags & WSP_ABOVE)
837 && (
838#ifdef FEAT_VERTSPLIT
839 (flags & WSP_VERT) ? p_spr :
840#endif
841 p_sb))))
842 {
843 /* new window below/right of current one */
844 if (newwin == NULL)
845 wp = win_alloc(oldwin);
846 else
847 win_append(oldwin, wp);
848 }
849 else
850 {
851 if (newwin == NULL)
852 wp = win_alloc(oldwin->w_prev);
853 else
854 win_append(oldwin->w_prev, wp);
855 }
856
857 if (newwin == NULL)
858 {
859 if (wp == NULL)
860 return FAIL;
861
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000862 /* make the contents of the new window the same as the current one */
863 win_init(wp, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
865
866 /*
867 * Reorganise the tree of frames to insert the new window.
868 */
869 if (flags & (WSP_TOP | WSP_BOT))
870 {
871#ifdef FEAT_VERTSPLIT
872 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
873 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
874#else
875 if (topframe->fr_layout == FR_COL)
876#endif
877 {
878 curfrp = topframe->fr_child;
879 if (flags & WSP_BOT)
880 while (curfrp->fr_next != NULL)
881 curfrp = curfrp->fr_next;
882 }
883 else
884 curfrp = topframe;
885 before = (flags & WSP_TOP);
886 }
887 else
888 {
889 curfrp = oldwin->w_frame;
890 if (flags & WSP_BELOW)
891 before = FALSE;
892 else if (flags & WSP_ABOVE)
893 before = TRUE;
894 else
895#ifdef FEAT_VERTSPLIT
896 if (flags & WSP_VERT)
897 before = !p_spr;
898 else
899#endif
900 before = !p_sb;
901 }
902 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
903 {
904 /* Need to create a new frame in the tree to make a branch. */
905 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
906 *frp = *curfrp;
907 curfrp->fr_layout = layout;
908 frp->fr_parent = curfrp;
909 frp->fr_next = NULL;
910 frp->fr_prev = NULL;
911 curfrp->fr_child = frp;
912 curfrp->fr_win = NULL;
913 curfrp = frp;
914 if (frp->fr_win != NULL)
915 oldwin->w_frame = frp;
916 else
917 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
918 frp->fr_parent = curfrp;
919 }
920
921 if (newwin == NULL)
922 {
923 /* Create a frame for the new window. */
924 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
925 frp->fr_layout = FR_LEAF;
926 frp->fr_win = wp;
927 wp->w_frame = frp;
928 }
929 else
930 frp = newwin->w_frame;
931 frp->fr_parent = curfrp->fr_parent;
932
933 /* Insert the new frame at the right place in the frame list. */
934 if (before)
935 frame_insert(curfrp, frp);
936 else
937 frame_append(curfrp, frp);
938
939#ifdef FEAT_VERTSPLIT
940 if (flags & WSP_VERT)
941 {
942 wp->w_p_scr = curwin->w_p_scr;
943 if (need_status)
944 {
945 --oldwin->w_height;
946 oldwin->w_status_height = need_status;
947 }
948 if (flags & (WSP_TOP | WSP_BOT))
949 {
950 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000951 wp->w_winrow = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 wp->w_height = curfrp->fr_height - (p_ls > 0);
953 wp->w_status_height = (p_ls > 0);
954 }
955 else
956 {
957 /* height and row of new window is same as current window */
958 wp->w_winrow = oldwin->w_winrow;
959 wp->w_height = oldwin->w_height;
960 wp->w_status_height = oldwin->w_status_height;
961 }
962 frp->fr_height = curfrp->fr_height;
963
964 /* "new_size" of the current window goes to the new window, use
965 * one column for the vertical separator */
966 wp->w_width = new_size;
967 if (before)
968 wp->w_vsep_width = 1;
969 else
970 {
971 wp->w_vsep_width = oldwin->w_vsep_width;
972 oldwin->w_vsep_width = 1;
973 }
974 if (flags & (WSP_TOP | WSP_BOT))
975 {
976 if (flags & WSP_BOT)
977 frame_add_vsep(curfrp);
978 /* Set width of neighbor frame */
979 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000980 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
981 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 }
983 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000984 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (before) /* new window left of current one */
986 {
987 wp->w_wincol = oldwin->w_wincol;
988 oldwin->w_wincol += new_size + 1;
989 }
990 else /* new window right of current one */
991 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
992 frame_fix_width(oldwin);
993 frame_fix_width(wp);
994 }
995 else
996#endif
997 {
998 /* width and column of new window is same as current window */
999#ifdef FEAT_VERTSPLIT
1000 if (flags & (WSP_TOP | WSP_BOT))
1001 {
1002 wp->w_wincol = 0;
1003 wp->w_width = Columns;
1004 wp->w_vsep_width = 0;
1005 }
1006 else
1007 {
1008 wp->w_wincol = oldwin->w_wincol;
1009 wp->w_width = oldwin->w_width;
1010 wp->w_vsep_width = oldwin->w_vsep_width;
1011 }
1012 frp->fr_width = curfrp->fr_width;
1013#endif
1014
1015 /* "new_size" of the current window goes to the new window, use
1016 * one row for the status line */
1017 win_new_height(wp, new_size);
1018 if (flags & (WSP_TOP | WSP_BOT))
1019 frame_new_height(curfrp, curfrp->fr_height
1020 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1021 else
1022 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1023 if (before) /* new window above current one */
1024 {
1025 wp->w_winrow = oldwin->w_winrow;
1026 wp->w_status_height = STATUS_HEIGHT;
1027 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1028 }
1029 else /* new window below current one */
1030 {
1031 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1032 wp->w_status_height = oldwin->w_status_height;
1033 oldwin->w_status_height = STATUS_HEIGHT;
1034 }
1035#ifdef FEAT_VERTSPLIT
1036 if (flags & WSP_BOT)
1037 frame_add_statusline(curfrp);
1038#endif
1039 frame_fix_height(wp);
1040 frame_fix_height(oldwin);
1041 }
1042
1043 if (flags & (WSP_TOP | WSP_BOT))
1044 (void)win_comp_pos();
1045
1046 /*
1047 * Both windows need redrawing
1048 */
1049 redraw_win_later(wp, NOT_VALID);
1050 wp->w_redr_status = TRUE;
1051 redraw_win_later(oldwin, NOT_VALID);
1052 oldwin->w_redr_status = TRUE;
1053
1054 if (need_status)
1055 {
1056 msg_row = Rows - 1;
1057 msg_col = sc_col;
1058 msg_clr_eos_force(); /* Old command/ruler may still be there */
1059 comp_col();
1060 msg_row = Rows - 1;
1061 msg_col = 0; /* put position back at start of line */
1062 }
1063
1064 /*
1065 * make the new window the current window and redraw
1066 */
1067 if (do_equal || dir != 0)
1068 win_equal(wp, TRUE,
1069#ifdef FEAT_VERTSPLIT
1070 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1071 : dir == 'h' ? 'b' :
1072#endif
1073 'v');
1074
1075 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1076 * size was given. */
1077#ifdef FEAT_VERTSPLIT
1078 if (flags & WSP_VERT)
1079 {
1080 i = p_wiw;
1081 if (size != 0)
1082 p_wiw = size;
1083
1084# ifdef FEAT_GUI
1085 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1086 if (gui.in_use)
1087 gui_init_which_components(NULL);
1088# endif
1089 }
1090 else
1091#endif
1092 {
1093 i = p_wh;
1094 if (size != 0)
1095 p_wh = size;
1096 }
1097 win_enter(wp, FALSE);
1098#ifdef FEAT_VERTSPLIT
1099 if (flags & WSP_VERT)
1100 p_wiw = i;
1101 else
1102#endif
1103 p_wh = i;
1104
1105 return OK;
1106}
1107
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001108/*
1109 * Initialize window "newp" from window "oldp".
1110 * Used when splitting a window and when creating a new tab page.
1111 * The windows will both edit the same buffer.
1112 */
1113 static void
1114win_init(newp, oldp)
1115 win_T *newp;
1116 win_T *oldp;
1117{
1118 int i;
1119
1120 newp->w_buffer = oldp->w_buffer;
1121 oldp->w_buffer->b_nwindows++;
1122 newp->w_cursor = oldp->w_cursor;
1123 newp->w_valid = 0;
1124 newp->w_curswant = oldp->w_curswant;
1125 newp->w_set_curswant = oldp->w_set_curswant;
1126 newp->w_topline = oldp->w_topline;
1127#ifdef FEAT_DIFF
1128 newp->w_topfill = oldp->w_topfill;
1129#endif
1130 newp->w_leftcol = oldp->w_leftcol;
1131 newp->w_pcmark = oldp->w_pcmark;
1132 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1133 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001134 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001135 newp->w_fraction = oldp->w_fraction;
1136 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1137#ifdef FEAT_JUMPLIST
1138 copy_jumplist(oldp, newp);
1139#endif
1140#ifdef FEAT_QUICKFIX
1141 copy_loclist(oldp, newp);
1142#endif
1143 if (oldp->w_localdir != NULL)
1144 newp->w_localdir = vim_strsave(oldp->w_localdir);
1145
1146 /* Use the same argument list. */
1147 newp->w_alist = oldp->w_alist;
1148 ++newp->w_alist->al_refcount;
1149 newp->w_arg_idx = oldp->w_arg_idx;
1150
1151 /*
1152 * copy tagstack and options from existing window
1153 */
1154 for (i = 0; i < oldp->w_tagstacklen; i++)
1155 {
1156 newp->w_tagstack[i] = oldp->w_tagstack[i];
1157 if (newp->w_tagstack[i].tagname != NULL)
1158 newp->w_tagstack[i].tagname =
1159 vim_strsave(newp->w_tagstack[i].tagname);
1160 }
1161 newp->w_tagstackidx = oldp->w_tagstackidx;
1162 newp->w_tagstacklen = oldp->w_tagstacklen;
1163 win_copy_options(oldp, newp);
1164# ifdef FEAT_FOLDING
1165 copyFoldingState(oldp, newp);
1166# endif
1167}
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169#endif /* FEAT_WINDOWS */
1170
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171#if defined(FEAT_WINDOWS) || defined(PROTO)
1172/*
1173 * Check if "win" is a pointer to an existing window.
1174 */
1175 int
1176win_valid(win)
1177 win_T *win;
1178{
1179 win_T *wp;
1180
1181 if (win == NULL)
1182 return FALSE;
1183 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1184 if (wp == win)
1185 return TRUE;
1186 return FALSE;
1187}
1188
1189/*
1190 * Return the number of windows.
1191 */
1192 int
1193win_count()
1194{
1195 win_T *wp;
1196 int count = 0;
1197
1198 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1199 ++count;
1200 return count;
1201}
1202
1203/*
1204 * Make "count" windows on the screen.
1205 * Return actual number of windows on the screen.
1206 * Must be called when there is just one window, filling the whole screen
1207 * (excluding the command line).
1208 */
1209/*ARGSUSED*/
1210 int
1211make_windows(count, vertical)
1212 int count;
1213 int vertical; /* split windows vertically if TRUE */
1214{
1215 int maxcount;
1216 int todo;
1217
1218#ifdef FEAT_VERTSPLIT
1219 if (vertical)
1220 {
1221 /* Each windows needs at least 'winminwidth' lines and a separator
1222 * column. */
1223 maxcount = (curwin->w_width + curwin->w_vsep_width
1224 - (p_wiw - p_wmw)) / (p_wmw + 1);
1225 }
1226 else
1227#endif
1228 {
1229 /* Each window needs at least 'winminheight' lines and a status line. */
1230 maxcount = (curwin->w_height + curwin->w_status_height
1231 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1232 }
1233
1234 if (maxcount < 2)
1235 maxcount = 2;
1236 if (count > maxcount)
1237 count = maxcount;
1238
1239 /*
1240 * add status line now, otherwise first window will be too big
1241 */
1242 if (count > 1)
1243 last_status(TRUE);
1244
1245#ifdef FEAT_AUTOCMD
1246 /*
1247 * Don't execute autocommands while creating the windows. Must do that
1248 * when putting the buffers in the windows.
1249 */
1250 ++autocmd_block;
1251#endif
1252
1253 /* todo is number of windows left to create */
1254 for (todo = count - 1; todo > 0; --todo)
1255#ifdef FEAT_VERTSPLIT
1256 if (vertical)
1257 {
1258 if (win_split(curwin->w_width - (curwin->w_width - todo)
1259 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1260 break;
1261 }
1262 else
1263#endif
1264 {
1265 if (win_split(curwin->w_height - (curwin->w_height - todo
1266 * STATUS_HEIGHT) / (todo + 1)
1267 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1268 break;
1269 }
1270
1271#ifdef FEAT_AUTOCMD
1272 --autocmd_block;
1273#endif
1274
1275 /* return actual number of windows */
1276 return (count - todo);
1277}
1278
1279/*
1280 * Exchange current and next window
1281 */
1282 static void
1283win_exchange(Prenum)
1284 long Prenum;
1285{
1286 frame_T *frp;
1287 frame_T *frp2;
1288 win_T *wp;
1289 win_T *wp2;
1290 int temp;
1291
1292 if (lastwin == firstwin) /* just one window */
1293 {
1294 beep_flush();
1295 return;
1296 }
1297
1298#ifdef FEAT_GUI
1299 need_mouse_correct = TRUE;
1300#endif
1301
1302 /*
1303 * find window to exchange with
1304 */
1305 if (Prenum)
1306 {
1307 frp = curwin->w_frame->fr_parent->fr_child;
1308 while (frp != NULL && --Prenum > 0)
1309 frp = frp->fr_next;
1310 }
1311 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1312 frp = curwin->w_frame->fr_next;
1313 else /* Swap last window in row/col with previous */
1314 frp = curwin->w_frame->fr_prev;
1315
1316 /* We can only exchange a window with another window, not with a frame
1317 * containing windows. */
1318 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1319 return;
1320 wp = frp->fr_win;
1321
1322/*
1323 * 1. remove curwin from the list. Remember after which window it was in wp2
1324 * 2. insert curwin before wp in the list
1325 * if wp != wp2
1326 * 3. remove wp from the list
1327 * 4. insert wp after wp2
1328 * 5. exchange the status line height and vsep width.
1329 */
1330 wp2 = curwin->w_prev;
1331 frp2 = curwin->w_frame->fr_prev;
1332 if (wp->w_prev != curwin)
1333 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001334 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 frame_remove(curwin->w_frame);
1336 win_append(wp->w_prev, curwin);
1337 frame_insert(frp, curwin->w_frame);
1338 }
1339 if (wp != wp2)
1340 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001341 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 frame_remove(wp->w_frame);
1343 win_append(wp2, wp);
1344 if (frp2 == NULL)
1345 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1346 else
1347 frame_append(frp2, wp->w_frame);
1348 }
1349 temp = curwin->w_status_height;
1350 curwin->w_status_height = wp->w_status_height;
1351 wp->w_status_height = temp;
1352#ifdef FEAT_VERTSPLIT
1353 temp = curwin->w_vsep_width;
1354 curwin->w_vsep_width = wp->w_vsep_width;
1355 wp->w_vsep_width = temp;
1356
1357 /* If the windows are not in the same frame, exchange the sizes to avoid
1358 * messing up the window layout. Otherwise fix the frame sizes. */
1359 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1360 {
1361 temp = curwin->w_height;
1362 curwin->w_height = wp->w_height;
1363 wp->w_height = temp;
1364 temp = curwin->w_width;
1365 curwin->w_width = wp->w_width;
1366 wp->w_width = temp;
1367 }
1368 else
1369 {
1370 frame_fix_height(curwin);
1371 frame_fix_height(wp);
1372 frame_fix_width(curwin);
1373 frame_fix_width(wp);
1374 }
1375#endif
1376
1377 (void)win_comp_pos(); /* recompute window positions */
1378
1379 win_enter(wp, TRUE);
1380 redraw_later(CLEAR);
1381}
1382
1383/*
1384 * rotate windows: if upwards TRUE the second window becomes the first one
1385 * if upwards FALSE the first window becomes the second one
1386 */
1387 static void
1388win_rotate(upwards, count)
1389 int upwards;
1390 int count;
1391{
1392 win_T *wp1;
1393 win_T *wp2;
1394 frame_T *frp;
1395 int n;
1396
1397 if (firstwin == lastwin) /* nothing to do */
1398 {
1399 beep_flush();
1400 return;
1401 }
1402
1403#ifdef FEAT_GUI
1404 need_mouse_correct = TRUE;
1405#endif
1406
1407#ifdef FEAT_VERTSPLIT
1408 /* Check if all frames in this row/col have one window. */
1409 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1410 frp = frp->fr_next)
1411 if (frp->fr_win == NULL)
1412 {
1413 EMSG(_("E443: Cannot rotate when another window is split"));
1414 return;
1415 }
1416#endif
1417
1418 while (count--)
1419 {
1420 if (upwards) /* first window becomes last window */
1421 {
1422 /* remove first window/frame from the list */
1423 frp = curwin->w_frame->fr_parent->fr_child;
1424 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001425 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 frame_remove(frp);
1427
1428 /* find last frame and append removed window/frame after it */
1429 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1430 ;
1431 win_append(frp->fr_win, wp1);
1432 frame_append(frp, wp1->w_frame);
1433
1434 wp2 = frp->fr_win; /* previously last window */
1435 }
1436 else /* last window becomes first window */
1437 {
1438 /* find last window/frame in the list and remove it */
1439 for (frp = curwin->w_frame; frp->fr_next != NULL;
1440 frp = frp->fr_next)
1441 ;
1442 wp1 = frp->fr_win;
1443 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001444 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 frame_remove(frp);
1446
1447 /* append the removed window/frame before the first in the list */
1448 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1449 frame_insert(frp->fr_parent->fr_child, frp);
1450 }
1451
1452 /* exchange status height and vsep width of old and new last window */
1453 n = wp2->w_status_height;
1454 wp2->w_status_height = wp1->w_status_height;
1455 wp1->w_status_height = n;
1456 frame_fix_height(wp1);
1457 frame_fix_height(wp2);
1458#ifdef FEAT_VERTSPLIT
1459 n = wp2->w_vsep_width;
1460 wp2->w_vsep_width = wp1->w_vsep_width;
1461 wp1->w_vsep_width = n;
1462 frame_fix_width(wp1);
1463 frame_fix_width(wp2);
1464#endif
1465
1466 /* recompute w_winrow and w_wincol for all windows */
1467 (void)win_comp_pos();
1468 }
1469
1470 redraw_later(CLEAR);
1471}
1472
1473/*
1474 * Move the current window to the very top/bottom/left/right of the screen.
1475 */
1476 static void
1477win_totop(size, flags)
1478 int size;
1479 int flags;
1480{
1481 int dir;
1482 int height = curwin->w_height;
1483
1484 if (lastwin == firstwin)
1485 {
1486 beep_flush();
1487 return;
1488 }
1489
1490 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001491 (void)winframe_remove(curwin, &dir, NULL);
1492 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 last_status(FALSE); /* may need to remove last status line */
1494 (void)win_comp_pos(); /* recompute window positions */
1495
1496 /* Split a window on the desired side and put the window there. */
1497 (void)win_split_ins(size, flags, curwin, dir);
1498 if (!(flags & WSP_VERT))
1499 {
1500 win_setheight(height);
1501 if (p_ea)
1502 win_equal(curwin, TRUE, 'v');
1503 }
1504
1505#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1506 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1507 * scrollbars. Have to update them anyway. */
1508 if (gui.in_use)
1509 {
1510 out_flush();
1511 gui_init_which_components(NULL);
1512 gui_update_scrollbars(TRUE);
1513 }
1514 need_mouse_correct = TRUE;
1515#endif
1516
1517}
1518
1519/*
1520 * Move window "win1" to below/right of "win2" and make "win1" the current
1521 * window. Only works within the same frame!
1522 */
1523 void
1524win_move_after(win1, win2)
1525 win_T *win1, *win2;
1526{
1527 int height;
1528
1529 /* check if the arguments are reasonable */
1530 if (win1 == win2)
1531 return;
1532
1533 /* check if there is something to do */
1534 if (win2->w_next != win1)
1535 {
1536 /* may need move the status line/vertical separator of the last window
1537 * */
1538 if (win1 == lastwin)
1539 {
1540 height = win1->w_prev->w_status_height;
1541 win1->w_prev->w_status_height = win1->w_status_height;
1542 win1->w_status_height = height;
1543#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001544 if (win1->w_prev->w_vsep_width == 1)
1545 {
1546 /* Remove the vertical separator from the last-but-one window,
1547 * add it to the last window. Adjust the frame widths. */
1548 win1->w_prev->w_vsep_width = 0;
1549 win1->w_prev->w_frame->fr_width -= 1;
1550 win1->w_vsep_width = 1;
1551 win1->w_frame->fr_width += 1;
1552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553#endif
1554 }
1555 else if (win2 == lastwin)
1556 {
1557 height = win1->w_status_height;
1558 win1->w_status_height = win2->w_status_height;
1559 win2->w_status_height = height;
1560#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001561 if (win1->w_vsep_width == 1)
1562 {
1563 /* Remove the vertical separator from win1, add it to the last
1564 * window, win2. Adjust the frame widths. */
1565 win2->w_vsep_width = 1;
1566 win2->w_frame->fr_width += 1;
1567 win1->w_vsep_width = 0;
1568 win1->w_frame->fr_width -= 1;
1569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570#endif
1571 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001572 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 frame_remove(win1->w_frame);
1574 win_append(win2, win1);
1575 frame_append(win2->w_frame, win1->w_frame);
1576
1577 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1578 redraw_later(NOT_VALID);
1579 }
1580 win_enter(win1, FALSE);
1581}
1582
1583/*
1584 * Make all windows the same height.
1585 * 'next_curwin' will soon be the current window, make sure it has enough
1586 * rows.
1587 */
1588 void
1589win_equal(next_curwin, current, dir)
1590 win_T *next_curwin; /* pointer to current window to be or NULL */
1591 int current; /* do only frame with current window */
1592 int dir; /* 'v' for vertically, 'h' for horizontally,
1593 'b' for both, 0 for using p_ead */
1594{
1595 if (dir == 0)
1596#ifdef FEAT_VERTSPLIT
1597 dir = *p_ead;
1598#else
1599 dir = 'b';
1600#endif
1601 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001602 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001603 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604}
1605
1606/*
1607 * Set a frame to a new position and height, spreading the available room
1608 * equally over contained frames.
1609 * The window "next_curwin" (if not NULL) should at least get the size from
1610 * 'winheight' and 'winwidth' if possible.
1611 */
1612 static void
1613win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1614 win_T *next_curwin; /* pointer to current window to be or NULL */
1615 int current; /* do only frame with current window */
1616 frame_T *topfr; /* frame to set size off */
1617 int dir; /* 'v', 'h' or 'b', see win_equal() */
1618 int col; /* horizontal position for frame */
1619 int row; /* vertical position for frame */
1620 int width; /* new width of frame */
1621 int height; /* new height of frame */
1622{
1623 int n, m;
1624 int extra_sep = 0;
1625 int wincount, totwincount = 0;
1626 frame_T *fr;
1627 int next_curwin_size = 0;
1628 int room = 0;
1629 int new_size;
1630 int has_next_curwin = 0;
1631 int hnc;
1632
1633 if (topfr->fr_layout == FR_LEAF)
1634 {
1635 /* Set the width/height of this frame.
1636 * Redraw when size or position changes */
1637 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1638#ifdef FEAT_VERTSPLIT
1639 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1640#endif
1641 )
1642 {
1643 topfr->fr_win->w_winrow = row;
1644 frame_new_height(topfr, height, FALSE, FALSE);
1645#ifdef FEAT_VERTSPLIT
1646 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001647 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648#endif
1649 redraw_all_later(CLEAR);
1650 }
1651 }
1652#ifdef FEAT_VERTSPLIT
1653 else if (topfr->fr_layout == FR_ROW)
1654 {
1655 topfr->fr_width = width;
1656 topfr->fr_height = height;
1657
1658 if (dir != 'v') /* equalize frame widths */
1659 {
1660 /* Compute the maximum number of windows horizontally in this
1661 * frame. */
1662 n = frame_minwidth(topfr, NOWIN);
1663 /* add one for the rightmost window, it doesn't have a separator */
1664 if (col + width == Columns)
1665 extra_sep = 1;
1666 else
1667 extra_sep = 0;
1668 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001669 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001671 /*
1672 * Compute width for "next_curwin" window and room available for
1673 * other windows.
1674 * "m" is the minimal width when counting p_wiw for "next_curwin".
1675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 m = frame_minwidth(topfr, next_curwin);
1677 room = width - m;
1678 if (room < 0)
1679 {
1680 next_curwin_size = p_wiw + room;
1681 room = 0;
1682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 else
1684 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001685 next_curwin_size = -1;
1686 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1687 {
1688 /* If 'winfixwidth' set keep the window width if
1689 * possible.
1690 * Watch out for this window being the next_curwin. */
1691 if (frame_fixed_width(fr))
1692 {
1693 n = frame_minwidth(fr, NOWIN);
1694 new_size = fr->fr_width;
1695 if (frame_has_win(fr, next_curwin))
1696 {
1697 room += p_wiw - p_wmw;
1698 next_curwin_size = 0;
1699 if (new_size < p_wiw)
1700 new_size = p_wiw;
1701 }
1702 else
1703 /* These windows don't use up room. */
1704 totwincount -= (n + (fr->fr_next == NULL
1705 ? extra_sep : 0)) / (p_wmw + 1);
1706 room -= new_size - n;
1707 if (room < 0)
1708 {
1709 new_size += room;
1710 room = 0;
1711 }
1712 fr->fr_newwidth = new_size;
1713 }
1714 }
1715 if (next_curwin_size == -1)
1716 {
1717 if (!has_next_curwin)
1718 next_curwin_size = 0;
1719 else if (totwincount > 1
1720 && (room + (totwincount - 2))
1721 / (totwincount - 1) > p_wiw)
1722 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001723 /* Can make all windows wider than 'winwidth', spread
1724 * the room equally. */
1725 next_curwin_size = (room + p_wiw
1726 + (totwincount - 1) * p_wmw
1727 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001728 room -= next_curwin_size - p_wiw;
1729 }
1730 else
1731 next_curwin_size = p_wiw;
1732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001734
1735 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 --totwincount; /* don't count curwin */
1737 }
1738
1739 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1740 {
1741 n = m = 0;
1742 wincount = 1;
1743 if (fr->fr_next == NULL)
1744 /* last frame gets all that remains (avoid roundoff error) */
1745 new_size = width;
1746 else if (dir == 'v')
1747 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001748 else if (frame_fixed_width(fr))
1749 {
1750 new_size = fr->fr_newwidth;
1751 wincount = 0; /* doesn't count as a sizeable window */
1752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 else
1754 {
1755 /* Compute the maximum number of windows horiz. in "fr". */
1756 n = frame_minwidth(fr, NOWIN);
1757 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1758 / (p_wmw + 1);
1759 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001760 if (has_next_curwin)
1761 hnc = frame_has_win(fr, next_curwin);
1762 else
1763 hnc = FALSE;
1764 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001766 if (totwincount == 0)
1767 new_size = room;
1768 else
1769 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001771 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 {
1773 next_curwin_size -= p_wiw - (m - n);
1774 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001775 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001777 else
1778 room -= new_size;
1779 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 }
1781
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001782 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 * window, unless equalizing all frames. */
1784 if (!current || dir != 'v' || topfr->fr_parent != NULL
1785 || (new_size != fr->fr_width)
1786 || frame_has_win(fr, next_curwin))
1787 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001788 new_size, height);
1789 col += new_size;
1790 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 totwincount -= wincount;
1792 }
1793 }
1794#endif
1795 else /* topfr->fr_layout == FR_COL */
1796 {
1797#ifdef FEAT_VERTSPLIT
1798 topfr->fr_width = width;
1799#endif
1800 topfr->fr_height = height;
1801
1802 if (dir != 'h') /* equalize frame heights */
1803 {
1804 /* Compute maximum number of windows vertically in this frame. */
1805 n = frame_minheight(topfr, NOWIN);
1806 /* add one for the bottom window if it doesn't have a statusline */
1807 if (row + height == cmdline_row && p_ls == 0)
1808 extra_sep = 1;
1809 else
1810 extra_sep = 0;
1811 totwincount = (n + extra_sep) / (p_wmh + 1);
1812 has_next_curwin = frame_has_win(topfr, next_curwin);
1813
1814 /*
1815 * Compute height for "next_curwin" window and room available for
1816 * other windows.
1817 * "m" is the minimal height when counting p_wh for "next_curwin".
1818 */
1819 m = frame_minheight(topfr, next_curwin);
1820 room = height - m;
1821 if (room < 0)
1822 {
1823 /* The room is less then 'winheight', use all space for the
1824 * current window. */
1825 next_curwin_size = p_wh + room;
1826 room = 0;
1827 }
1828 else
1829 {
1830 next_curwin_size = -1;
1831 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1832 {
1833 /* If 'winfixheight' set keep the window height if
1834 * possible.
1835 * Watch out for this window being the next_curwin. */
1836 if (frame_fixed_height(fr))
1837 {
1838 n = frame_minheight(fr, NOWIN);
1839 new_size = fr->fr_height;
1840 if (frame_has_win(fr, next_curwin))
1841 {
1842 room += p_wh - p_wmh;
1843 next_curwin_size = 0;
1844 if (new_size < p_wh)
1845 new_size = p_wh;
1846 }
1847 else
1848 /* These windows don't use up room. */
1849 totwincount -= (n + (fr->fr_next == NULL
1850 ? extra_sep : 0)) / (p_wmh + 1);
1851 room -= new_size - n;
1852 if (room < 0)
1853 {
1854 new_size += room;
1855 room = 0;
1856 }
1857 fr->fr_newheight = new_size;
1858 }
1859 }
1860 if (next_curwin_size == -1)
1861 {
1862 if (!has_next_curwin)
1863 next_curwin_size = 0;
1864 else if (totwincount > 1
1865 && (room + (totwincount - 2))
1866 / (totwincount - 1) > p_wh)
1867 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001868 /* can make all windows higher than 'winheight',
1869 * spread the room equally. */
1870 next_curwin_size = (room + p_wh
1871 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 + (totwincount - 1)) / totwincount;
1873 room -= next_curwin_size - p_wh;
1874 }
1875 else
1876 next_curwin_size = p_wh;
1877 }
1878 }
1879
1880 if (has_next_curwin)
1881 --totwincount; /* don't count curwin */
1882 }
1883
1884 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1885 {
1886 n = m = 0;
1887 wincount = 1;
1888 if (fr->fr_next == NULL)
1889 /* last frame gets all that remains (avoid roundoff error) */
1890 new_size = height;
1891 else if (dir == 'h')
1892 new_size = fr->fr_height;
1893 else if (frame_fixed_height(fr))
1894 {
1895 new_size = fr->fr_newheight;
1896 wincount = 0; /* doesn't count as a sizeable window */
1897 }
1898 else
1899 {
1900 /* Compute the maximum number of windows vert. in "fr". */
1901 n = frame_minheight(fr, NOWIN);
1902 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1903 / (p_wmh + 1);
1904 m = frame_minheight(fr, next_curwin);
1905 if (has_next_curwin)
1906 hnc = frame_has_win(fr, next_curwin);
1907 else
1908 hnc = FALSE;
1909 if (hnc) /* don't count next_curwin */
1910 --wincount;
1911 if (totwincount == 0)
1912 new_size = room;
1913 else
1914 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1915 / totwincount;
1916 if (hnc) /* add next_curwin size */
1917 {
1918 next_curwin_size -= p_wh - (m - n);
1919 new_size += next_curwin_size;
1920 room -= new_size - next_curwin_size;
1921 }
1922 else
1923 room -= new_size;
1924 new_size += n;
1925 }
1926 /* Skip frame that is full width when splitting or closing a
1927 * window, unless equalizing all frames. */
1928 if (!current || dir != 'h' || topfr->fr_parent != NULL
1929 || (new_size != fr->fr_height)
1930 || frame_has_win(fr, next_curwin))
1931 win_equal_rec(next_curwin, current, fr, dir, col, row,
1932 width, new_size);
1933 row += new_size;
1934 height -= new_size;
1935 totwincount -= wincount;
1936 }
1937 }
1938}
1939
1940/*
1941 * close all windows for buffer 'buf'
1942 */
1943 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00001944close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001946 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947{
Bram Moolenaarf740b292006-02-16 22:11:02 +00001948 win_T *wp;
1949 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001950 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951
1952 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001953
1954 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001956 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001958 win_close(wp, FALSE);
1959
1960 /* Start all over, autocommands may change the window layout. */
1961 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 }
1963 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00001964 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001966
1967 /* Also check windows in other tab pages. */
1968 for (tp = first_tabpage; tp != NULL; tp = nexttp)
1969 {
1970 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001971 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001972 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
1973 if (wp->w_buffer == buf)
1974 {
1975 win_close_othertab(wp, FALSE, tp);
1976
1977 /* Start all over, the tab page may be closed and
1978 * autocommands may change the window layout. */
1979 nexttp = first_tabpage;
1980 break;
1981 }
1982 }
1983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001985
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001986 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00001987 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988}
1989
1990/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001991 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001992 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001993 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001994 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001995last_window()
1996{
1997 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
1998}
1999
2000/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002001 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 * If "free_buf" is TRUE related buffer may be unloaded.
2003 *
2004 * called by :quit, :close, :xit, :wq and findtag()
2005 */
2006 void
2007win_close(win, free_buf)
2008 win_T *win;
2009 int free_buf;
2010{
2011 win_T *wp;
2012#ifdef FEAT_AUTOCMD
2013 int other_buffer = FALSE;
2014#endif
2015 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 int dir;
2017 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002018 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002020 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
2022 EMSG(_("E444: Cannot close last window"));
2023 return;
2024 }
2025
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002026 /*
2027 * When closing the last window in a tab page first go to another tab
2028 * page and then close the window and the tab page. This avoids that
2029 * curwin and curtab are not invalid while we are freeing memory, they may
2030 * be used in GUI events.
2031 */
2032 if (firstwin == lastwin)
2033 {
2034 goto_tabpage_tp(alt_tabpage());
2035 redraw_tabline = TRUE;
2036
2037 /* Safety check: Autocommands may have closed the window when jumping
2038 * to the other tab page. */
2039 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2040 {
2041 int h = tabline_height();
2042
2043 win_close_othertab(win, free_buf, prev_curtab);
2044 if (h != tabline_height())
2045 shell_new_rows();
2046 }
2047 return;
2048 }
2049
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 /* When closing the help window, try restoring a snapshot after closing
2051 * the window. Otherwise clear the snapshot, it's now invalid. */
2052 if (win->w_buffer->b_help)
2053 help_window = TRUE;
2054 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002055 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
2057#ifdef FEAT_AUTOCMD
2058 if (win == curwin)
2059 {
2060 /*
2061 * Guess which window is going to be the new current window.
2062 * This may change because of the autocommands (sigh).
2063 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002064 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
2066 /*
2067 * Be careful: If autocommands delete the window, return now.
2068 */
2069 if (wp->w_buffer != curbuf)
2070 {
2071 other_buffer = TRUE;
2072 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002073 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 return;
2075 }
2076 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002077 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 return;
2079# ifdef FEAT_EVAL
2080 /* autocmds may abort script processing */
2081 if (aborting())
2082 return;
2083# endif
2084 }
2085#endif
2086
2087 /*
2088 * Close the link to the buffer.
2089 */
2090 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002091
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002093 * other window or moved to another tab page. */
2094 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 return;
2096
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002097 /* Free the memory used for the window. */
2098 wp = win_free_mem(win, &dir, NULL);
2099
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 /* Make sure curwin isn't invalid. It can cause severe trouble when
2101 * printing an error message. For win_equal() curbuf needs to be valid
2102 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002103 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
2105 curwin = wp;
2106#ifdef FEAT_QUICKFIX
2107 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2108 {
2109 /*
2110 * The cursor goes to the preview or the quickfix window, try
2111 * finding another window to go to.
2112 */
2113 for (;;)
2114 {
2115 if (wp->w_next == NULL)
2116 wp = firstwin;
2117 else
2118 wp = wp->w_next;
2119 if (wp == curwin)
2120 break;
2121 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2122 {
2123 curwin = wp;
2124 break;
2125 }
2126 }
2127 }
2128#endif
2129 curbuf = curwin->w_buffer;
2130 close_curwin = TRUE;
2131 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002132 if (p_ea
2133#ifdef FEAT_VERTSPLIT
2134 && (*p_ead == 'b' || *p_ead == dir)
2135#endif
2136 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 win_equal(curwin, TRUE,
2138#ifdef FEAT_VERTSPLIT
2139 dir
2140#else
2141 0
2142#endif
2143 );
2144 else
2145 win_comp_pos();
2146 if (close_curwin)
2147 {
2148 win_enter_ext(wp, FALSE, TRUE);
2149#ifdef FEAT_AUTOCMD
2150 if (other_buffer)
2151 /* careful: after this wp and win may be invalid! */
2152 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2153#endif
2154 }
2155
2156 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002157 * If last window has a status line now and we don't want one,
2158 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 */
2160 last_status(FALSE);
2161
2162 /* After closing the help window, try restoring the window layout from
2163 * before it was opened. */
2164 if (help_window)
2165 restore_snapshot(close_curwin);
2166
2167#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2168 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2169 if (gui.in_use && !win_hasvertsplit())
2170 gui_init_which_components(NULL);
2171#endif
2172
2173 redraw_all_later(NOT_VALID);
2174}
2175
2176/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002177 * Close window "win" in tab page "tp", which is not the current tab page.
2178 * This may be the last window ih that tab page and result in closing the tab,
2179 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002180 * Caller must check if buffer is hidden and whether the tabline needs to be
2181 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002182 */
2183 void
2184win_close_othertab(win, free_buf, tp)
2185 win_T *win;
2186 int free_buf;
2187 tabpage_T *tp;
2188{
2189 win_T *wp;
2190 int dir;
2191 tabpage_T *ptp = NULL;
2192
2193 /* Close the link to the buffer. */
2194 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2195
2196 /* Careful: Autocommands may have closed the tab page or made it the
2197 * current tab page. */
2198 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2199 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002200 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002201 return;
2202
2203 /* Autocommands may have closed the window already. */
2204 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2205 ;
2206 if (wp == NULL)
2207 return;
2208
2209 /* Free the memory used for the window. */
2210 wp = win_free_mem(win, &dir, tp);
2211
2212 /* When closing the last window in a tab page remove the tab page. */
2213 if (wp == NULL)
2214 {
2215 if (tp == first_tabpage)
2216 first_tabpage = tp->tp_next;
2217 else
2218 {
2219 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2220 ptp = ptp->tp_next)
2221 ;
2222 if (ptp == NULL)
2223 {
2224 EMSG2(_(e_intern2), "win_close_othertab()");
2225 return;
2226 }
2227 ptp->tp_next = tp->tp_next;
2228 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002229 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002230 }
2231}
2232
2233/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002234 * Free the memory used for a window.
2235 * Returns a pointer to the window that got the freed up space.
2236 */
2237 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002238win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002239 win_T *win;
2240 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002241 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002242{
2243 frame_T *frp;
2244 win_T *wp;
2245
Bram Moolenaarea408852005-06-25 22:49:46 +00002246#ifdef FEAT_FOLDING
2247 clearFolding(win);
2248#endif
2249
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002250 /* reduce the reference count to the argument list. */
2251 alist_unlink(win->w_alist);
2252
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002253 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002254 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002255 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002256 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002257 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002258
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002259 /* When deleting the current window of another tab page select a new
2260 * current window. */
2261 if (tp != NULL && win == tp->tp_curwin)
2262 tp->tp_curwin = wp;
2263
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002264 return wp;
2265}
2266
2267#if defined(EXITFREE) || defined(PROTO)
2268 void
2269win_free_all()
2270{
2271 int dummy;
2272
Bram Moolenaarf740b292006-02-16 22:11:02 +00002273# ifdef FEAT_WINDOWS
2274 while (first_tabpage->tp_next != NULL)
2275 tabpage_close(TRUE);
2276# endif
2277
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002278 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002279 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002280}
2281#endif
2282
2283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 * Remove a window and its frame from the tree of frames.
2285 * Returns a pointer to the window that got the freed up space.
2286 */
2287/*ARGSUSED*/
2288 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002289winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 win_T *win;
2291 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002292 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293{
2294 frame_T *frp, *frp2, *frp3;
2295 frame_T *frp_close = win->w_frame;
2296 win_T *wp;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002297 int old_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
2299 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002300 * If there is only one window there is nothing to remove.
2301 */
2302 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2303 return NULL;
2304
2305 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 * Remove the window from its frame.
2307 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002308 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 wp = frame2win(frp2);
2310
2311 /* Remove this frame from the list of frames. */
2312 frame_remove(frp_close);
2313
2314#ifdef FEAT_VERTSPLIT
2315 if (frp_close->fr_parent->fr_layout == FR_COL)
2316 {
2317#endif
2318 /* When 'winfixheight' is set, remember its old size and restore
2319 * it later (it's a simplistic solution...). Don't do this if the
2320 * window will occupy the full height of the screen. */
2321 if (frp2->fr_win != NULL
2322 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2323 && frp2->fr_win->w_p_wfh)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002324 old_size = frp2->fr_win->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2326 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002327 if (old_size != 0)
2328 win_setheight_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329#ifdef FEAT_VERTSPLIT
2330 *dirp = 'v';
2331 }
2332 else
2333 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002334 /* When 'winfixwidth' is set, remember its old size and restore
2335 * it later (it's a simplistic solution...). Don't do this if the
2336 * window will occupy the full width of the screen. */
2337 if (frp2->fr_win != NULL
2338 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2339 && frp2->fr_win->w_p_wfw)
2340 old_size = frp2->fr_win->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002342 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2343 if (old_size != 0)
2344 win_setwidth_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 *dirp = 'h';
2346 }
2347#endif
2348
2349 /* If rows/columns go to a window below/right its positions need to be
2350 * updated. Can only be done after the sizes have been updated. */
2351 if (frp2 == frp_close->fr_next)
2352 {
2353 int row = win->w_winrow;
2354 int col = W_WINCOL(win);
2355
2356 frame_comp_pos(frp2, &row, &col);
2357 }
2358
2359 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2360 {
2361 /* There is no other frame in this list, move its info to the parent
2362 * and remove it. */
2363 frp2->fr_parent->fr_layout = frp2->fr_layout;
2364 frp2->fr_parent->fr_child = frp2->fr_child;
2365 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2366 frp->fr_parent = frp2->fr_parent;
2367 frp2->fr_parent->fr_win = frp2->fr_win;
2368 if (frp2->fr_win != NULL)
2369 frp2->fr_win->w_frame = frp2->fr_parent;
2370 frp = frp2->fr_parent;
2371 vim_free(frp2);
2372
2373 frp2 = frp->fr_parent;
2374 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2375 {
2376 /* The frame above the parent has the same layout, have to merge
2377 * the frames into this list. */
2378 if (frp2->fr_child == frp)
2379 frp2->fr_child = frp->fr_child;
2380 frp->fr_child->fr_prev = frp->fr_prev;
2381 if (frp->fr_prev != NULL)
2382 frp->fr_prev->fr_next = frp->fr_child;
2383 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2384 {
2385 frp3->fr_parent = frp2;
2386 if (frp3->fr_next == NULL)
2387 {
2388 frp3->fr_next = frp->fr_next;
2389 if (frp->fr_next != NULL)
2390 frp->fr_next->fr_prev = frp3;
2391 break;
2392 }
2393 }
2394 vim_free(frp);
2395 }
2396 }
2397
2398 return wp;
2399}
2400
2401/*
2402 * Find out which frame is going to get the freed up space when "win" is
2403 * closed.
2404 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2405 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2406 * This makes opening a window and closing it immediately keep the same window
2407 * layout.
2408 */
2409 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002410win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002412 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
2414 frame_T *frp;
2415 int b;
2416
Bram Moolenaarf740b292006-02-16 22:11:02 +00002417 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002418 /* Last window in this tab page, will go to next tab page. */
2419 return alt_tabpage()->tp_curwin->w_frame;
2420
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 frp = win->w_frame;
2422#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002423 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 b = p_spr;
2425 else
2426#endif
2427 b = p_sb;
2428 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2429 return frp->fr_next;
2430 return frp->fr_prev;
2431}
2432
2433/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002434 * Return the tabpage that will be used if the current one is closed.
2435 */
2436 static tabpage_T *
2437alt_tabpage()
2438{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002439 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002440
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002441 /* Use the next tab page if possible. */
2442 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002443 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002444
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002445 /* Find the last but one tab page. */
2446 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2447 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002448 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002449}
2450
2451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 * Find the left-upper window in frame "frp".
2453 */
2454 static win_T *
2455frame2win(frp)
2456 frame_T *frp;
2457{
2458 while (frp->fr_win == NULL)
2459 frp = frp->fr_child;
2460 return frp->fr_win;
2461}
2462
2463/*
2464 * Return TRUE if frame "frp" contains window "wp".
2465 */
2466 static int
2467frame_has_win(frp, wp)
2468 frame_T *frp;
2469 win_T *wp;
2470{
2471 frame_T *p;
2472
2473 if (frp->fr_layout == FR_LEAF)
2474 return frp->fr_win == wp;
2475
2476 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2477 if (frame_has_win(p, wp))
2478 return TRUE;
2479 return FALSE;
2480}
2481
2482/*
2483 * Set a new height for a frame. Recursively sets the height for contained
2484 * frames and windows. Caller must take care of positions.
2485 */
2486 static void
2487frame_new_height(topfrp, height, topfirst, wfh)
2488 frame_T *topfrp;
2489 int height;
2490 int topfirst; /* resize topmost contained frame first */
2491 int wfh; /* obey 'winfixheight' when there is a choice;
2492 may cause the height not to be set */
2493{
2494 frame_T *frp;
2495 int extra_lines;
2496 int h;
2497
2498 if (topfrp->fr_win != NULL)
2499 {
2500 /* Simple case: just one window. */
2501 win_new_height(topfrp->fr_win,
2502 height - topfrp->fr_win->w_status_height);
2503 }
2504#ifdef FEAT_VERTSPLIT
2505 else if (topfrp->fr_layout == FR_ROW)
2506 {
2507 do
2508 {
2509 /* All frames in this row get the same new height. */
2510 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2511 {
2512 frame_new_height(frp, height, topfirst, wfh);
2513 if (frp->fr_height > height)
2514 {
2515 /* Could not fit the windows, make the whole row higher. */
2516 height = frp->fr_height;
2517 break;
2518 }
2519 }
2520 }
2521 while (frp != NULL);
2522 }
2523#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002524 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {
2526 /* Complicated case: Resize a column of frames. Resize the bottom
2527 * frame first, frames above that when needed. */
2528
2529 frp = topfrp->fr_child;
2530 if (wfh)
2531 /* Advance past frames with one window with 'wfh' set. */
2532 while (frame_fixed_height(frp))
2533 {
2534 frp = frp->fr_next;
2535 if (frp == NULL)
2536 return; /* no frame without 'wfh', give up */
2537 }
2538 if (!topfirst)
2539 {
2540 /* Find the bottom frame of this column */
2541 while (frp->fr_next != NULL)
2542 frp = frp->fr_next;
2543 if (wfh)
2544 /* Advance back for frames with one window with 'wfh' set. */
2545 while (frame_fixed_height(frp))
2546 frp = frp->fr_prev;
2547 }
2548
2549 extra_lines = height - topfrp->fr_height;
2550 if (extra_lines < 0)
2551 {
2552 /* reduce height of contained frames, bottom or top frame first */
2553 while (frp != NULL)
2554 {
2555 h = frame_minheight(frp, NULL);
2556 if (frp->fr_height + extra_lines < h)
2557 {
2558 extra_lines += frp->fr_height - h;
2559 frame_new_height(frp, h, topfirst, wfh);
2560 }
2561 else
2562 {
2563 frame_new_height(frp, frp->fr_height + extra_lines,
2564 topfirst, wfh);
2565 break;
2566 }
2567 if (topfirst)
2568 {
2569 do
2570 frp = frp->fr_next;
2571 while (wfh && frp != NULL && frame_fixed_height(frp));
2572 }
2573 else
2574 {
2575 do
2576 frp = frp->fr_prev;
2577 while (wfh && frp != NULL && frame_fixed_height(frp));
2578 }
2579 /* Increase "height" if we could not reduce enough frames. */
2580 if (frp == NULL)
2581 height -= extra_lines;
2582 }
2583 }
2584 else if (extra_lines > 0)
2585 {
2586 /* increase height of bottom or top frame */
2587 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2588 }
2589 }
2590 topfrp->fr_height = height;
2591}
2592
2593/*
2594 * Return TRUE if height of frame "frp" should not be changed because of
2595 * the 'winfixheight' option.
2596 */
2597 static int
2598frame_fixed_height(frp)
2599 frame_T *frp;
2600{
2601 /* frame with one window: fixed height if 'winfixheight' set. */
2602 if (frp->fr_win != NULL)
2603 return frp->fr_win->w_p_wfh;
2604
2605 if (frp->fr_layout == FR_ROW)
2606 {
2607 /* The frame is fixed height if one of the frames in the row is fixed
2608 * height. */
2609 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2610 if (frame_fixed_height(frp))
2611 return TRUE;
2612 return FALSE;
2613 }
2614
2615 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2616 * frames in the row are fixed height. */
2617 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2618 if (!frame_fixed_height(frp))
2619 return FALSE;
2620 return TRUE;
2621}
2622
2623#ifdef FEAT_VERTSPLIT
2624/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002625 * Return TRUE if width of frame "frp" should not be changed because of
2626 * the 'winfixwidth' option.
2627 */
2628 static int
2629frame_fixed_width(frp)
2630 frame_T *frp;
2631{
2632 /* frame with one window: fixed width if 'winfixwidth' set. */
2633 if (frp->fr_win != NULL)
2634 return frp->fr_win->w_p_wfw;
2635
2636 if (frp->fr_layout == FR_COL)
2637 {
2638 /* The frame is fixed width if one of the frames in the row is fixed
2639 * width. */
2640 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2641 if (frame_fixed_width(frp))
2642 return TRUE;
2643 return FALSE;
2644 }
2645
2646 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2647 * frames in the row are fixed width. */
2648 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2649 if (!frame_fixed_width(frp))
2650 return FALSE;
2651 return TRUE;
2652}
2653
2654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 * Add a status line to windows at the bottom of "frp".
2656 * Note: Does not check if there is room!
2657 */
2658 static void
2659frame_add_statusline(frp)
2660 frame_T *frp;
2661{
2662 win_T *wp;
2663
2664 if (frp->fr_layout == FR_LEAF)
2665 {
2666 wp = frp->fr_win;
2667 if (wp->w_status_height == 0)
2668 {
2669 if (wp->w_height > 0) /* don't make it negative */
2670 --wp->w_height;
2671 wp->w_status_height = STATUS_HEIGHT;
2672 }
2673 }
2674 else if (frp->fr_layout == FR_ROW)
2675 {
2676 /* Handle all the frames in the row. */
2677 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2678 frame_add_statusline(frp);
2679 }
2680 else /* frp->fr_layout == FR_COL */
2681 {
2682 /* Only need to handle the last frame in the column. */
2683 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2684 ;
2685 frame_add_statusline(frp);
2686 }
2687}
2688
2689/*
2690 * Set width of a frame. Handles recursively going through contained frames.
2691 * May remove separator line for windows at the right side (for win_close()).
2692 */
2693 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002694frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 frame_T *topfrp;
2696 int width;
2697 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002698 int wfw; /* obey 'winfixwidth' when there is a choice;
2699 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700{
2701 frame_T *frp;
2702 int extra_cols;
2703 int w;
2704 win_T *wp;
2705
2706 if (topfrp->fr_layout == FR_LEAF)
2707 {
2708 /* Simple case: just one window. */
2709 wp = topfrp->fr_win;
2710 /* Find out if there are any windows right of this one. */
2711 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2712 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2713 break;
2714 if (frp->fr_parent == NULL)
2715 wp->w_vsep_width = 0;
2716 win_new_width(wp, width - wp->w_vsep_width);
2717 }
2718 else if (topfrp->fr_layout == FR_COL)
2719 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002720 do
2721 {
2722 /* All frames in this column get the same new width. */
2723 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2724 {
2725 frame_new_width(frp, width, leftfirst, wfw);
2726 if (frp->fr_width > width)
2727 {
2728 /* Could not fit the windows, make whole column wider. */
2729 width = frp->fr_width;
2730 break;
2731 }
2732 }
2733 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 }
2735 else /* fr_layout == FR_ROW */
2736 {
2737 /* Complicated case: Resize a row of frames. Resize the rightmost
2738 * frame first, frames left of it when needed. */
2739
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002741 if (wfw)
2742 /* Advance past frames with one window with 'wfw' set. */
2743 while (frame_fixed_width(frp))
2744 {
2745 frp = frp->fr_next;
2746 if (frp == NULL)
2747 return; /* no frame without 'wfw', give up */
2748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002750 {
2751 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 while (frp->fr_next != NULL)
2753 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002754 if (wfw)
2755 /* Advance back for frames with one window with 'wfw' set. */
2756 while (frame_fixed_width(frp))
2757 frp = frp->fr_prev;
2758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
2760 extra_cols = width - topfrp->fr_width;
2761 if (extra_cols < 0)
2762 {
2763 /* reduce frame width, rightmost frame first */
2764 while (frp != NULL)
2765 {
2766 w = frame_minwidth(frp, NULL);
2767 if (frp->fr_width + extra_cols < w)
2768 {
2769 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002770 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 }
2772 else
2773 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002774 frame_new_width(frp, frp->fr_width + extra_cols,
2775 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 break;
2777 }
2778 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002779 {
2780 do
2781 frp = frp->fr_next;
2782 while (wfw && frp != NULL && frame_fixed_width(frp));
2783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002785 {
2786 do
2787 frp = frp->fr_prev;
2788 while (wfw && frp != NULL && frame_fixed_width(frp));
2789 }
2790 /* Increase "width" if we could not reduce enough frames. */
2791 if (frp == NULL)
2792 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
2794 }
2795 else if (extra_cols > 0)
2796 {
2797 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002798 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 }
2800 }
2801 topfrp->fr_width = width;
2802}
2803
2804/*
2805 * Add the vertical separator to windows at the right side of "frp".
2806 * Note: Does not check if there is room!
2807 */
2808 static void
2809frame_add_vsep(frp)
2810 frame_T *frp;
2811{
2812 win_T *wp;
2813
2814 if (frp->fr_layout == FR_LEAF)
2815 {
2816 wp = frp->fr_win;
2817 if (wp->w_vsep_width == 0)
2818 {
2819 if (wp->w_width > 0) /* don't make it negative */
2820 --wp->w_width;
2821 wp->w_vsep_width = 1;
2822 }
2823 }
2824 else if (frp->fr_layout == FR_COL)
2825 {
2826 /* Handle all the frames in the column. */
2827 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2828 frame_add_vsep(frp);
2829 }
2830 else /* frp->fr_layout == FR_ROW */
2831 {
2832 /* Only need to handle the last frame in the row. */
2833 frp = frp->fr_child;
2834 while (frp->fr_next != NULL)
2835 frp = frp->fr_next;
2836 frame_add_vsep(frp);
2837 }
2838}
2839
2840/*
2841 * Set frame width from the window it contains.
2842 */
2843 static void
2844frame_fix_width(wp)
2845 win_T *wp;
2846{
2847 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2848}
2849#endif
2850
2851/*
2852 * Set frame height from the window it contains.
2853 */
2854 static void
2855frame_fix_height(wp)
2856 win_T *wp;
2857{
2858 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2859}
2860
2861/*
2862 * Compute the minimal height for frame "topfrp".
2863 * Uses the 'winminheight' option.
2864 * When "next_curwin" isn't NULL, use p_wh for this window.
2865 * When "next_curwin" is NOWIN, don't use at least one line for the current
2866 * window.
2867 */
2868 static int
2869frame_minheight(topfrp, next_curwin)
2870 frame_T *topfrp;
2871 win_T *next_curwin;
2872{
2873 frame_T *frp;
2874 int m;
2875#ifdef FEAT_VERTSPLIT
2876 int n;
2877#endif
2878
2879 if (topfrp->fr_win != NULL)
2880 {
2881 if (topfrp->fr_win == next_curwin)
2882 m = p_wh + topfrp->fr_win->w_status_height;
2883 else
2884 {
2885 /* window: minimal height of the window plus status line */
2886 m = p_wmh + topfrp->fr_win->w_status_height;
2887 /* Current window is minimal one line high */
2888 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2889 ++m;
2890 }
2891 }
2892#ifdef FEAT_VERTSPLIT
2893 else if (topfrp->fr_layout == FR_ROW)
2894 {
2895 /* get the minimal height from each frame in this row */
2896 m = 0;
2897 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2898 {
2899 n = frame_minheight(frp, next_curwin);
2900 if (n > m)
2901 m = n;
2902 }
2903 }
2904#endif
2905 else
2906 {
2907 /* Add up the minimal heights for all frames in this column. */
2908 m = 0;
2909 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2910 m += frame_minheight(frp, next_curwin);
2911 }
2912
2913 return m;
2914}
2915
2916#ifdef FEAT_VERTSPLIT
2917/*
2918 * Compute the minimal width for frame "topfrp".
2919 * When "next_curwin" isn't NULL, use p_wiw for this window.
2920 * When "next_curwin" is NOWIN, don't use at least one column for the current
2921 * window.
2922 */
2923 static int
2924frame_minwidth(topfrp, next_curwin)
2925 frame_T *topfrp;
2926 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2927{
2928 frame_T *frp;
2929 int m, n;
2930
2931 if (topfrp->fr_win != NULL)
2932 {
2933 if (topfrp->fr_win == next_curwin)
2934 m = p_wiw + topfrp->fr_win->w_vsep_width;
2935 else
2936 {
2937 /* window: minimal width of the window plus separator column */
2938 m = p_wmw + topfrp->fr_win->w_vsep_width;
2939 /* Current window is minimal one column wide */
2940 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2941 ++m;
2942 }
2943 }
2944 else if (topfrp->fr_layout == FR_COL)
2945 {
2946 /* get the minimal width from each frame in this column */
2947 m = 0;
2948 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2949 {
2950 n = frame_minwidth(frp, next_curwin);
2951 if (n > m)
2952 m = n;
2953 }
2954 }
2955 else
2956 {
2957 /* Add up the minimal widths for all frames in this row. */
2958 m = 0;
2959 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2960 m += frame_minwidth(frp, next_curwin);
2961 }
2962
2963 return m;
2964}
2965#endif
2966
2967
2968/*
2969 * Try to close all windows except current one.
2970 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2971 * used and the buffer was modified.
2972 *
2973 * Used by ":bdel" and ":only".
2974 */
2975 void
2976close_others(message, forceit)
2977 int message;
2978 int forceit; /* always hide all other windows */
2979{
2980 win_T *wp;
2981 win_T *nextwp;
2982 int r;
2983
2984 if (lastwin == firstwin)
2985 {
2986 if (message
2987#ifdef FEAT_AUTOCMD
2988 && !autocmd_busy
2989#endif
2990 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00002991 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 return;
2993 }
2994
2995 /* Be very careful here: autocommands may change the window layout. */
2996 for (wp = firstwin; win_valid(wp); wp = nextwp)
2997 {
2998 nextwp = wp->w_next;
2999 if (wp != curwin) /* don't close current window */
3000 {
3001
3002 /* Check if it's allowed to abandon this window */
3003 r = can_abandon(wp->w_buffer, forceit);
3004#ifdef FEAT_AUTOCMD
3005 if (!win_valid(wp)) /* autocommands messed wp up */
3006 {
3007 nextwp = firstwin;
3008 continue;
3009 }
3010#endif
3011 if (!r)
3012 {
3013#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3014 if (message && (p_confirm || cmdmod.confirm) && p_write)
3015 {
3016 dialog_changed(wp->w_buffer, FALSE);
3017# ifdef FEAT_AUTOCMD
3018 if (!win_valid(wp)) /* autocommands messed wp up */
3019 {
3020 nextwp = firstwin;
3021 continue;
3022 }
3023# endif
3024 }
3025 if (bufIsChanged(wp->w_buffer))
3026#endif
3027 continue;
3028 }
3029 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3030 }
3031 }
3032
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003033 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 EMSG(_("E445: Other window contains changes"));
3035}
3036
3037#endif /* FEAT_WINDOWS */
3038
3039/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003040 * Init the current window "curwin".
3041 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 */
3043 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003044curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003046 redraw_win_later(curwin, NOT_VALID);
3047 curwin->w_lines_valid = 0;
3048 curwin->w_cursor.lnum = 1;
3049 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003051 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003053 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3054 curwin->w_pcmark.col = 0;
3055 curwin->w_prev_pcmark.lnum = 0;
3056 curwin->w_prev_pcmark.col = 0;
3057 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003059 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003061 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062#ifdef FEAT_FKMAP
3063 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003064 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003066 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067#endif
3068}
3069
3070/*
3071 * Allocate the first window and put an empty buffer in it.
3072 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003073 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003075 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076win_alloc_first()
3077{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003078 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003079 return FAIL;
3080
3081#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003082 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003083 if (first_tabpage == NULL)
3084 return FAIL;
3085 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003086 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003087#endif
3088 return OK;
3089}
3090
3091/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003092 * Allocate the first window or the first window in a new tab page.
3093 * When "oldwin" is NULL create an empty buffer for it.
3094 * When "oldwin" is not NULL copy info from it to the new window (only with
3095 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003096 * Return FAIL when something goes wrong (out of memory).
3097 */
3098 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003099win_alloc_firstwin(oldwin)
3100 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003101{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003103 if (oldwin == NULL)
3104 {
3105 /* Very first window, need to create an empty buffer for it and
3106 * initialize from scratch. */
3107 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3108 if (curwin == NULL || curbuf == NULL)
3109 return FAIL;
3110 curwin->w_buffer = curbuf;
3111 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003113 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003115 curwin_init(); /* init current window */
3116 }
3117#ifdef FEAT_WINDOWS
3118 else
3119 {
3120 /* First window in new tab page, initialize it from "oldwin". */
3121 win_init(curwin, oldwin);
3122
3123# ifdef FEAT_SCROLLBIND
3124 /* We don't want scroll-binding in the first window. */
3125 curwin->w_p_scb = FALSE;
3126# endif
3127 }
3128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129
3130 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3131 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003132 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 topframe->fr_layout = FR_LEAF;
3134#ifdef FEAT_VERTSPLIT
3135 topframe->fr_width = Columns;
3136#endif
3137 topframe->fr_height = Rows - p_ch;
3138 topframe->fr_win = curwin;
3139 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003140
3141 return OK;
3142}
3143
3144/*
3145 * Initialize the window and frame size to the maximum.
3146 */
3147 void
3148win_init_size()
3149{
3150 firstwin->w_height = ROWS_AVAIL;
3151 topframe->fr_height = ROWS_AVAIL;
3152#ifdef FEAT_VERTSPLIT
3153 firstwin->w_width = Columns;
3154 topframe->fr_width = Columns;
3155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156}
3157
3158#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003159
3160/*
3161 * Allocate a new tabpage_T and init the values.
3162 * Returns NULL when out of memory.
3163 */
3164 static tabpage_T *
3165alloc_tabpage()
3166{
3167 tabpage_T *tp;
3168
3169 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3170 if (tp != NULL)
3171 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003172# ifdef FEAT_GUI
3173 int i;
3174
3175 for (i = 0; i < 3; i++)
3176 tp->tp_prev_which_scrollbars[i] = -1;
3177# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003178# ifdef FEAT_DIFF
3179 tp->tp_diff_invalid = TRUE;
3180# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003181#ifdef FEAT_EVAL
3182 /* init t: variables */
3183 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3184#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003185 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003186 }
3187 return tp;
3188}
3189
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003190 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003191free_tabpage(tp)
3192 tabpage_T *tp;
3193{
3194# ifdef FEAT_DIFF
3195 diff_clear(tp);
3196# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003197 clear_snapshot(tp);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003198#ifdef FEAT_EVAL
3199 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3200#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003201 vim_free(tp);
3202}
3203
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003204/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003205 * Create a new Tab page with one window.
3206 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003207 * When "after" is 0 put it just after the current Tab page.
3208 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003209 * Return FAIL or OK.
3210 */
3211 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003212win_new_tabpage(after)
3213 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003214{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003215 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003216 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003217 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003218
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003219 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003220 if (newtp == NULL)
3221 return FAIL;
3222
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003223 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003224 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003225 {
3226 vim_free(newtp);
3227 return FAIL;
3228 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003229 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003230
3231 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003232 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003233 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003234 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003235 if (after == 1)
3236 {
3237 /* New tab page becomes the first one. */
3238 newtp->tp_next = first_tabpage;
3239 first_tabpage = newtp;
3240 }
3241 else
3242 {
3243 if (after > 0)
3244 {
3245 /* Put new tab page before tab page "after". */
3246 n = 2;
3247 for (tp = first_tabpage; tp->tp_next != NULL
3248 && n < after; tp = tp->tp_next)
3249 ++n;
3250 }
3251 newtp->tp_next = tp->tp_next;
3252 tp->tp_next = newtp;
3253 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003254 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003255 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003256 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003257
3258 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003259 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003260
3261#if defined(FEAT_GUI)
3262 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3263 * scrollbars. Have to update them anyway. */
3264 if (gui.in_use && starting == 0)
3265 {
3266 gui_init_which_components(NULL);
3267 gui_update_scrollbars(TRUE);
3268 }
3269 need_mouse_correct = TRUE;
3270#endif
3271
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003272 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003273#ifdef FEAT_AUTOCMD
3274 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3275 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3276#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003277 return OK;
3278 }
3279
3280 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003281 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003282 return FAIL;
3283}
3284
3285/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003286 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3287 * like with ":split".
3288 * Returns OK if a new tab page was created, FAIL otherwise.
3289 */
3290 int
3291may_open_tabpage()
3292{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003293 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003294
Bram Moolenaard326ce82007-03-11 14:48:29 +00003295 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003296 {
3297 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003298 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003299 return win_new_tabpage(n);
3300 }
3301 return FAIL;
3302}
3303
3304/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003305 * Create up to "maxcount" tabpages with empty windows.
3306 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003307 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003308 int
3309make_tabpages(maxcount)
3310 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003311{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003312 int count = maxcount;
3313 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003314
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003315 /* Limit to 'tabpagemax' tabs. */
3316 if (count > p_tpm)
3317 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003318
3319#ifdef FEAT_AUTOCMD
3320 /*
3321 * Don't execute autocommands while creating the tab pages. Must do that
3322 * when putting the buffers in the windows.
3323 */
3324 ++autocmd_block;
3325#endif
3326
3327 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003328 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003329 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003330
3331#ifdef FEAT_AUTOCMD
3332 --autocmd_block;
3333#endif
3334
3335 /* return actual number of tab pages */
3336 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003337}
3338
3339/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003340 * Return TRUE when "tpc" points to a valid tab page.
3341 */
3342 int
3343valid_tabpage(tpc)
3344 tabpage_T *tpc;
3345{
3346 tabpage_T *tp;
3347
3348 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3349 if (tp == tpc)
3350 return TRUE;
3351 return FALSE;
3352}
3353
3354/*
3355 * Find tab page "n" (first one is 1). Returns NULL when not found.
3356 */
3357 tabpage_T *
3358find_tabpage(n)
3359 int n;
3360{
3361 tabpage_T *tp;
3362 int i = 1;
3363
3364 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3365 ++i;
3366 return tp;
3367}
3368
3369/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003370 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003371 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003372 */
3373 int
3374tabpage_index(ftp)
3375 tabpage_T *ftp;
3376{
3377 int i = 1;
3378 tabpage_T *tp;
3379
3380 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3381 ++i;
3382 return i;
3383}
3384
3385/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003386 * Prepare for leaving the current tab page.
3387 * When autocomands change "curtab" we don't leave the tab page and return
3388 * FAIL.
3389 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003390 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003391/*ARGSUSED*/
3392 static int
3393leave_tabpage(new_curbuf)
3394 buf_T *new_curbuf; /* what is going to be the new curbuf,
3395 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003396{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003397 tabpage_T *tp = curtab;
3398
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003399#ifdef FEAT_VISUAL
3400 reset_VIsual_and_resel(); /* stop Visual mode */
3401#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003402#ifdef FEAT_AUTOCMD
3403 if (new_curbuf != curbuf)
3404 {
3405 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3406 if (curtab != tp)
3407 return FAIL;
3408 }
3409 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3410 if (curtab != tp)
3411 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003412 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003413 if (curtab != tp)
3414 return FAIL;
3415#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003416#if defined(FEAT_GUI)
3417 /* Remove the scrollbars. They may be added back later. */
3418 if (gui.in_use)
3419 gui_remove_scrollbars();
3420#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003421 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003422 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003423 tp->tp_firstwin = firstwin;
3424 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003425 tp->tp_old_Rows = Rows;
3426 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003427 firstwin = NULL;
3428 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003429 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003430}
3431
3432/*
3433 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003434 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003435 */
3436/*ARGSUSED*/
3437 static void
3438enter_tabpage(tp, old_curbuf)
3439 tabpage_T *tp;
3440 buf_T *old_curbuf;
3441{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003442 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003443 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003444
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003445 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003446 firstwin = tp->tp_firstwin;
3447 lastwin = tp->tp_lastwin;
3448 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003449
3450 /* We would like doing the TabEnter event first, but we don't have a
3451 * valid current window yet, which may break some commands.
3452 * This triggers autocommands, thus may make "tp" invalid. */
3453 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3454 prevwin = next_prevwin;
3455
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003456#ifdef FEAT_AUTOCMD
3457 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003458
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003459 if (old_curbuf != curbuf)
3460 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3461#endif
3462
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003463 last_status(FALSE); /* status line may appear or disappear */
3464 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003465 must_redraw = CLEAR; /* need to redraw everything */
3466#ifdef FEAT_DIFF
3467 diff_need_scrollbind = TRUE;
3468#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003469
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003470 /* The tabpage line may have appeared or disappeared, may need to resize
3471 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003472 * frame sizes too. Use the stored value of p_ch, so that it can be
3473 * different for each tab page. */
3474 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003475 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3476#ifdef FEAT_GUI_TABLINE
3477 && !gui_use_tabline()
3478#endif
3479 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003480 shell_new_rows();
3481#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003482 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003483 shell_new_columns(); /* update window widths */
3484#endif
3485
3486#if defined(FEAT_GUI)
3487 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3488 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003489 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003490 {
3491 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003492 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003493 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003494 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003495#endif
3496
3497 redraw_all_later(CLEAR);
3498}
3499
3500/*
3501 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003502 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003503 */
3504 void
3505goto_tabpage(n)
3506 int n;
3507{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003508 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003509 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003510 int i;
3511
Bram Moolenaard68071d2006-05-02 22:08:30 +00003512 if (text_locked())
3513 {
3514 /* Not allowed when editing the command line. */
3515#ifdef FEAT_CMDWIN
3516 if (cmdwin_type != 0)
3517 EMSG(_(e_cmdwin));
3518 else
3519#endif
3520 EMSG(_(e_secure));
3521 return;
3522 }
3523
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003524 /* If there is only one it can't work. */
3525 if (first_tabpage->tp_next == NULL)
3526 {
3527 if (n > 1)
3528 beep_flush();
3529 return;
3530 }
3531
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003532 if (n == 0)
3533 {
3534 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003535 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003536 tp = first_tabpage;
3537 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003538 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003539 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003540 else if (n < 0)
3541 {
3542 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3543 * this N times. */
3544 ttp = curtab;
3545 for (i = n; i < 0; ++i)
3546 {
3547 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3548 tp = tp->tp_next)
3549 ;
3550 ttp = tp;
3551 }
3552 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003553 else if (n == 9999)
3554 {
3555 /* Go to last tab page. */
3556 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3557 ;
3558 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003559 else
3560 {
3561 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003562 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003563 if (tp == NULL)
3564 {
3565 beep_flush();
3566 return;
3567 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003568 }
3569
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003570 goto_tabpage_tp(tp);
3571
3572#ifdef FEAT_GUI_TABLINE
3573 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003574 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003575#endif
3576}
3577
3578/*
3579 * Go to tabpage "tp".
3580 * Note: doesn't update the GUI tab.
3581 */
3582 void
3583goto_tabpage_tp(tp)
3584 tabpage_T *tp;
3585{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003586 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003587 {
3588 if (valid_tabpage(tp))
3589 enter_tabpage(tp, curbuf);
3590 else
3591 enter_tabpage(curtab, curbuf);
3592 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003593}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594
3595/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003596 * Enter window "wp" in tab page "tp".
3597 * Also updates the GUI tab.
3598 */
3599 void
3600goto_tabpage_win(tp, wp)
3601 tabpage_T *tp;
3602 win_T *wp;
3603{
3604 goto_tabpage_tp(tp);
3605 if (curtab == tp && win_valid(wp))
3606 {
3607 win_enter(wp, TRUE);
3608# ifdef FEAT_GUI_TABLINE
3609 if (gui_use_tabline())
3610 gui_mch_set_curtab(tabpage_index(curtab));
3611# endif
3612 }
3613}
3614
3615/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003616 * Move the current tab page to before tab page "nr".
3617 */
3618 void
3619tabpage_move(nr)
3620 int nr;
3621{
3622 int n = nr;
3623 tabpage_T *tp;
3624
3625 if (first_tabpage->tp_next == NULL)
3626 return;
3627
3628 /* Remove the current tab page from the list of tab pages. */
3629 if (curtab == first_tabpage)
3630 first_tabpage = curtab->tp_next;
3631 else
3632 {
3633 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3634 if (tp->tp_next == curtab)
3635 break;
3636 if (tp == NULL) /* "cannot happen" */
3637 return;
3638 tp->tp_next = curtab->tp_next;
3639 }
3640
3641 /* Re-insert it at the specified position. */
3642 if (n == 0)
3643 {
3644 curtab->tp_next = first_tabpage;
3645 first_tabpage = curtab;
3646 }
3647 else
3648 {
3649 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3650 --n;
3651 curtab->tp_next = tp->tp_next;
3652 tp->tp_next = curtab;
3653 }
3654
3655 /* Need to redraw the tabline. Tab page contents doesn't change. */
3656 redraw_tabline = TRUE;
3657}
3658
3659
3660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 * Go to another window.
3662 * When jumping to another buffer, stop Visual mode. Do this before
3663 * changing windows so we can yank the selection into the '*' register.
3664 * When jumping to another window on the same buffer, adjust its cursor
3665 * position to keep the same Visual area.
3666 */
3667 void
3668win_goto(wp)
3669 win_T *wp;
3670{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003671 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
3673 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003674 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 return;
3676 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003677#ifdef FEAT_AUTOCMD
3678 if (curbuf_locked())
3679 return;
3680#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003681
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682#ifdef FEAT_VISUAL
3683 if (wp->w_buffer != curbuf)
3684 reset_VIsual_and_resel();
3685 else if (VIsual_active)
3686 wp->w_cursor = curwin->w_cursor;
3687#endif
3688
3689#ifdef FEAT_GUI
3690 need_mouse_correct = TRUE;
3691#endif
3692 win_enter(wp, TRUE);
3693}
3694
3695#if defined(FEAT_PERL) || defined(PROTO)
3696/*
3697 * Find window number "winnr" (counting top to bottom).
3698 */
3699 win_T *
3700win_find_nr(winnr)
3701 int winnr;
3702{
3703 win_T *wp;
3704
3705# ifdef FEAT_WINDOWS
3706 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3707 if (--winnr == 0)
3708 break;
3709 return wp;
3710# else
3711 return curwin;
3712# endif
3713}
3714#endif
3715
3716#ifdef FEAT_VERTSPLIT
3717/*
3718 * Move to window above or below "count" times.
3719 */
3720 static void
3721win_goto_ver(up, count)
3722 int up; /* TRUE to go to win above */
3723 long count;
3724{
3725 frame_T *fr;
3726 frame_T *nfr;
3727 frame_T *foundfr;
3728
3729 foundfr = curwin->w_frame;
3730 while (count--)
3731 {
3732 /*
3733 * First go upwards in the tree of frames until we find a upwards or
3734 * downwards neighbor.
3735 */
3736 fr = foundfr;
3737 for (;;)
3738 {
3739 if (fr == topframe)
3740 goto end;
3741 if (up)
3742 nfr = fr->fr_prev;
3743 else
3744 nfr = fr->fr_next;
3745 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3746 break;
3747 fr = fr->fr_parent;
3748 }
3749
3750 /*
3751 * Now go downwards to find the bottom or top frame in it.
3752 */
3753 for (;;)
3754 {
3755 if (nfr->fr_layout == FR_LEAF)
3756 {
3757 foundfr = nfr;
3758 break;
3759 }
3760 fr = nfr->fr_child;
3761 if (nfr->fr_layout == FR_ROW)
3762 {
3763 /* Find the frame at the cursor row. */
3764 while (fr->fr_next != NULL
3765 && frame2win(fr)->w_wincol + fr->fr_width
3766 <= curwin->w_wincol + curwin->w_wcol)
3767 fr = fr->fr_next;
3768 }
3769 if (nfr->fr_layout == FR_COL && up)
3770 while (fr->fr_next != NULL)
3771 fr = fr->fr_next;
3772 nfr = fr;
3773 }
3774 }
3775end:
3776 if (foundfr != NULL)
3777 win_goto(foundfr->fr_win);
3778}
3779
3780/*
3781 * Move to left or right window.
3782 */
3783 static void
3784win_goto_hor(left, count)
3785 int left; /* TRUE to go to left win */
3786 long count;
3787{
3788 frame_T *fr;
3789 frame_T *nfr;
3790 frame_T *foundfr;
3791
3792 foundfr = curwin->w_frame;
3793 while (count--)
3794 {
3795 /*
3796 * First go upwards in the tree of frames until we find a left or
3797 * right neighbor.
3798 */
3799 fr = foundfr;
3800 for (;;)
3801 {
3802 if (fr == topframe)
3803 goto end;
3804 if (left)
3805 nfr = fr->fr_prev;
3806 else
3807 nfr = fr->fr_next;
3808 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3809 break;
3810 fr = fr->fr_parent;
3811 }
3812
3813 /*
3814 * Now go downwards to find the leftmost or rightmost frame in it.
3815 */
3816 for (;;)
3817 {
3818 if (nfr->fr_layout == FR_LEAF)
3819 {
3820 foundfr = nfr;
3821 break;
3822 }
3823 fr = nfr->fr_child;
3824 if (nfr->fr_layout == FR_COL)
3825 {
3826 /* Find the frame at the cursor row. */
3827 while (fr->fr_next != NULL
3828 && frame2win(fr)->w_winrow + fr->fr_height
3829 <= curwin->w_winrow + curwin->w_wrow)
3830 fr = fr->fr_next;
3831 }
3832 if (nfr->fr_layout == FR_ROW && left)
3833 while (fr->fr_next != NULL)
3834 fr = fr->fr_next;
3835 nfr = fr;
3836 }
3837 }
3838end:
3839 if (foundfr != NULL)
3840 win_goto(foundfr->fr_win);
3841}
3842#endif
3843
3844/*
3845 * Make window "wp" the current window.
3846 */
3847 void
3848win_enter(wp, undo_sync)
3849 win_T *wp;
3850 int undo_sync;
3851{
3852 win_enter_ext(wp, undo_sync, FALSE);
3853}
3854
3855/*
3856 * Make window wp the current window.
3857 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3858 * been closed and isn't valid.
3859 */
3860 static void
3861win_enter_ext(wp, undo_sync, curwin_invalid)
3862 win_T *wp;
3863 int undo_sync;
3864 int curwin_invalid;
3865{
3866#ifdef FEAT_AUTOCMD
3867 int other_buffer = FALSE;
3868#endif
3869
3870 if (wp == curwin && !curwin_invalid) /* nothing to do */
3871 return;
3872
3873#ifdef FEAT_AUTOCMD
3874 if (!curwin_invalid)
3875 {
3876 /*
3877 * Be careful: If autocommands delete the window, return now.
3878 */
3879 if (wp->w_buffer != curbuf)
3880 {
3881 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3882 other_buffer = TRUE;
3883 if (!win_valid(wp))
3884 return;
3885 }
3886 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3887 if (!win_valid(wp))
3888 return;
3889# ifdef FEAT_EVAL
3890 /* autocmds may abort script processing */
3891 if (aborting())
3892 return;
3893# endif
3894 }
3895#endif
3896
3897 /* sync undo before leaving the current buffer */
3898 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003899 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 /* may have to copy the buffer options when 'cpo' contains 'S' */
3901 if (wp->w_buffer != curbuf)
3902 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3903 if (!curwin_invalid)
3904 {
3905 prevwin = curwin; /* remember for CTRL-W p */
3906 curwin->w_redr_status = TRUE;
3907 }
3908 curwin = wp;
3909 curbuf = wp->w_buffer;
3910 check_cursor();
3911#ifdef FEAT_VIRTUALEDIT
3912 if (!virtual_active())
3913 curwin->w_cursor.coladd = 0;
3914#endif
3915 changed_line_abv_curs(); /* assume cursor position needs updating */
3916
3917 if (curwin->w_localdir != NULL)
3918 {
3919 /* Window has a local directory: Save current directory as global
3920 * directory (unless that was done already) and change to the local
3921 * directory. */
3922 if (globaldir == NULL)
3923 {
3924 char_u cwd[MAXPATHL];
3925
3926 if (mch_dirname(cwd, MAXPATHL) == OK)
3927 globaldir = vim_strsave(cwd);
3928 }
3929 mch_chdir((char *)curwin->w_localdir);
3930 shorten_fnames(TRUE);
3931 }
3932 else if (globaldir != NULL)
3933 {
3934 /* Window doesn't have a local directory and we are not in the global
3935 * directory: Change to the global directory. */
3936 mch_chdir((char *)globaldir);
3937 vim_free(globaldir);
3938 globaldir = NULL;
3939 shorten_fnames(TRUE);
3940 }
3941
3942#ifdef FEAT_AUTOCMD
3943 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3944 if (other_buffer)
3945 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3946#endif
3947
3948#ifdef FEAT_TITLE
3949 maketitle();
3950#endif
3951 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003952 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 if (restart_edit)
3954 redraw_later(VALID); /* causes status line redraw */
3955
3956 /* set window height to desired minimal value */
3957 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3958 win_setheight((int)p_wh);
3959 else if (curwin->w_height == 0)
3960 win_setheight(1);
3961
3962#ifdef FEAT_VERTSPLIT
3963 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003964 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 win_setwidth((int)p_wiw);
3966#endif
3967
3968#ifdef FEAT_MOUSE
3969 setmouse(); /* in case jumped to/from help buffer */
3970#endif
3971
Bram Moolenaar498efdb2006-09-05 14:31:54 +00003972 /* Change directories when the 'acd' option is set. */
3973 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974}
3975
3976#endif /* FEAT_WINDOWS */
3977
3978#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3979/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003980 * Jump to the first open window that contains buffer "buf", if one exists.
3981 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 */
3983 win_T *
3984buf_jump_open_win(buf)
3985 buf_T *buf;
3986{
3987# ifdef FEAT_WINDOWS
3988 win_T *wp;
3989
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003990 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 if (wp->w_buffer == buf)
3992 break;
3993 if (wp != NULL)
3994 win_enter(wp, FALSE);
3995 return wp;
3996# else
3997 if (curwin->w_buffer == buf)
3998 return curwin;
3999 return NULL;
4000# endif
4001}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004002
4003/*
4004 * Jump to the first open window in any tab page that contains buffer "buf",
4005 * if one exists.
4006 * Returns a pointer to the window found, otherwise NULL.
4007 */
4008 win_T *
4009buf_jump_open_tab(buf)
4010 buf_T *buf;
4011{
4012# ifdef FEAT_WINDOWS
4013 win_T *wp;
4014 tabpage_T *tp;
4015
4016 /* First try the current tab page. */
4017 wp = buf_jump_open_win(buf);
4018 if (wp != NULL)
4019 return wp;
4020
4021 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4022 if (tp != curtab)
4023 {
4024 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4025 if (wp->w_buffer == buf)
4026 break;
4027 if (wp != NULL)
4028 {
4029 goto_tabpage_win(tp, wp);
4030 if (curwin != wp)
4031 wp = NULL; /* something went wrong */
4032 break;
4033 }
4034 }
4035
4036 return wp;
4037# else
4038 if (curwin->w_buffer == buf)
4039 return curwin;
4040 return NULL;
4041# endif
4042}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043#endif
4044
4045/*
4046 * allocate a window structure and link it in the window list
4047 */
4048/*ARGSUSED*/
4049 static win_T *
4050win_alloc(after)
4051 win_T *after;
4052{
4053 win_T *newwin;
4054
4055 /*
4056 * allocate window structure and linesizes arrays
4057 */
4058 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4059 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4060 {
4061 vim_free(newwin);
4062 newwin = NULL;
4063 }
4064
4065 if (newwin != NULL)
4066 {
4067 /*
4068 * link the window in the window list
4069 */
4070#ifdef FEAT_WINDOWS
4071 win_append(after, newwin);
4072#endif
4073#ifdef FEAT_VERTSPLIT
4074 newwin->w_wincol = 0;
4075 newwin->w_width = Columns;
4076#endif
4077
4078 /* position the display and the cursor at the top of the file. */
4079 newwin->w_topline = 1;
4080#ifdef FEAT_DIFF
4081 newwin->w_topfill = 0;
4082#endif
4083 newwin->w_botline = 2;
4084 newwin->w_cursor.lnum = 1;
4085#ifdef FEAT_SCROLLBIND
4086 newwin->w_scbind_pos = 1;
4087#endif
4088
4089 /* We won't calculate w_fraction until resizing the window */
4090 newwin->w_fraction = 0;
4091 newwin->w_prev_fraction_row = -1;
4092
4093#ifdef FEAT_GUI
4094 if (gui.in_use)
4095 {
4096 out_flush();
4097 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4098 SBAR_LEFT, newwin);
4099 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4100 SBAR_RIGHT, newwin);
4101 }
4102#endif
4103#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004104 /* init w: variables */
4105 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106#endif
4107#ifdef FEAT_FOLDING
4108 foldInitWin(newwin);
4109#endif
4110 }
4111 return newwin;
4112}
4113
4114#if defined(FEAT_WINDOWS) || defined(PROTO)
4115
4116/*
4117 * remove window 'wp' from the window list and free the structure
4118 */
4119 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004120win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004122 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123{
4124 int i;
4125
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004126#ifdef FEAT_MZSCHEME
4127 mzscheme_window_free(wp);
4128#endif
4129
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130#ifdef FEAT_PERL
4131 perl_win_free(wp);
4132#endif
4133
4134#ifdef FEAT_PYTHON
4135 python_window_free(wp);
4136#endif
4137
4138#ifdef FEAT_TCL
4139 tcl_window_free(wp);
4140#endif
4141
4142#ifdef FEAT_RUBY
4143 ruby_window_free(wp);
4144#endif
4145
4146 clear_winopt(&wp->w_onebuf_opt);
4147 clear_winopt(&wp->w_allbuf_opt);
4148
4149#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004150 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151#endif
4152
4153 if (prevwin == wp)
4154 prevwin = NULL;
4155 win_free_lsize(wp);
4156
4157 for (i = 0; i < wp->w_tagstacklen; ++i)
4158 vim_free(wp->w_tagstack[i].tagname);
4159
4160 vim_free(wp->w_localdir);
4161#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004162 vim_free(wp->w_match[0].regprog);
4163 vim_free(wp->w_match[1].regprog);
4164 vim_free(wp->w_match[2].regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165#endif
4166#ifdef FEAT_JUMPLIST
4167 free_jumplist(wp);
4168#endif
4169
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004170#ifdef FEAT_QUICKFIX
4171 qf_free_all(wp);
4172#endif
4173
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174#ifdef FEAT_GUI
4175 if (gui.in_use)
4176 {
4177 out_flush();
4178 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4179 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4180 }
4181#endif /* FEAT_GUI */
4182
Bram Moolenaarf740b292006-02-16 22:11:02 +00004183 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 vim_free(wp);
4185}
4186
4187/*
4188 * Append window "wp" in the window list after window "after".
4189 */
4190 static void
4191win_append(after, wp)
4192 win_T *after, *wp;
4193{
4194 win_T *before;
4195
4196 if (after == NULL) /* after NULL is in front of the first */
4197 before = firstwin;
4198 else
4199 before = after->w_next;
4200
4201 wp->w_next = before;
4202 wp->w_prev = after;
4203 if (after == NULL)
4204 firstwin = wp;
4205 else
4206 after->w_next = wp;
4207 if (before == NULL)
4208 lastwin = wp;
4209 else
4210 before->w_prev = wp;
4211}
4212
4213/*
4214 * Remove a window from the window list.
4215 */
4216 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004217win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004219 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220{
4221 if (wp->w_prev != NULL)
4222 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004223 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004225 else
4226 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 if (wp->w_next != NULL)
4228 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004229 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004231 else
4232 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233}
4234
4235/*
4236 * Append frame "frp" in a frame list after frame "after".
4237 */
4238 static void
4239frame_append(after, frp)
4240 frame_T *after, *frp;
4241{
4242 frp->fr_next = after->fr_next;
4243 after->fr_next = frp;
4244 if (frp->fr_next != NULL)
4245 frp->fr_next->fr_prev = frp;
4246 frp->fr_prev = after;
4247}
4248
4249/*
4250 * Insert frame "frp" in a frame list before frame "before".
4251 */
4252 static void
4253frame_insert(before, frp)
4254 frame_T *before, *frp;
4255{
4256 frp->fr_next = before;
4257 frp->fr_prev = before->fr_prev;
4258 before->fr_prev = frp;
4259 if (frp->fr_prev != NULL)
4260 frp->fr_prev->fr_next = frp;
4261 else
4262 frp->fr_parent->fr_child = frp;
4263}
4264
4265/*
4266 * Remove a frame from a frame list.
4267 */
4268 static void
4269frame_remove(frp)
4270 frame_T *frp;
4271{
4272 if (frp->fr_prev != NULL)
4273 frp->fr_prev->fr_next = frp->fr_next;
4274 else
4275 frp->fr_parent->fr_child = frp->fr_next;
4276 if (frp->fr_next != NULL)
4277 frp->fr_next->fr_prev = frp->fr_prev;
4278}
4279
4280#endif /* FEAT_WINDOWS */
4281
4282/*
4283 * Allocate w_lines[] for window "wp".
4284 * Return FAIL for failure, OK for success.
4285 */
4286 int
4287win_alloc_lines(wp)
4288 win_T *wp;
4289{
4290 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004291 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 if (wp->w_lines == NULL)
4293 return FAIL;
4294 return OK;
4295}
4296
4297/*
4298 * free lsize arrays for a window
4299 */
4300 void
4301win_free_lsize(wp)
4302 win_T *wp;
4303{
4304 vim_free(wp->w_lines);
4305 wp->w_lines = NULL;
4306}
4307
4308/*
4309 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004310 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 */
4312 void
4313shell_new_rows()
4314{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004315 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316
4317 if (firstwin == NULL) /* not initialized yet */
4318 return;
4319#ifdef FEAT_WINDOWS
4320 if (h < frame_minheight(topframe, NULL))
4321 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004322
4323 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 * that doesn't result in the right height, forget about that option. */
4325 frame_new_height(topframe, h, FALSE, TRUE);
4326 if (topframe->fr_height != h)
4327 frame_new_height(topframe, h, FALSE, FALSE);
4328
4329 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4330#else
4331 if (h < 1)
4332 h = 1;
4333 win_new_height(firstwin, h);
4334#endif
4335 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004336#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004337 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004338#endif
4339
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340#if 0
4341 /* Disabled: don't want making the screen smaller make a window larger. */
4342 if (p_ea)
4343 win_equal(curwin, FALSE, 'v');
4344#endif
4345}
4346
4347#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4348/*
4349 * Called from win_new_shellsize() after Columns changed.
4350 */
4351 void
4352shell_new_columns()
4353{
4354 if (firstwin == NULL) /* not initialized yet */
4355 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004356
4357 /* First try setting the widths of windows with 'winfixwidth'. If that
4358 * doesn't result in the right width, forget about that option. */
4359 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4360 if (topframe->fr_width != Columns)
4361 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4362
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4364#if 0
4365 /* Disabled: don't want making the screen smaller make a window larger. */
4366 if (p_ea)
4367 win_equal(curwin, FALSE, 'h');
4368#endif
4369}
4370#endif
4371
4372#if defined(FEAT_CMDWIN) || defined(PROTO)
4373/*
4374 * Save the size of all windows in "gap".
4375 */
4376 void
4377win_size_save(gap)
4378 garray_T *gap;
4379
4380{
4381 win_T *wp;
4382
4383 ga_init2(gap, (int)sizeof(int), 1);
4384 if (ga_grow(gap, win_count() * 2) == OK)
4385 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4386 {
4387 ((int *)gap->ga_data)[gap->ga_len++] =
4388 wp->w_width + wp->w_vsep_width;
4389 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4390 }
4391}
4392
4393/*
4394 * Restore window sizes, but only if the number of windows is still the same.
4395 * Does not free the growarray.
4396 */
4397 void
4398win_size_restore(gap)
4399 garray_T *gap;
4400{
4401 win_T *wp;
4402 int i;
4403
4404 if (win_count() * 2 == gap->ga_len)
4405 {
4406 i = 0;
4407 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4408 {
4409 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4410 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4411 }
4412 /* recompute the window positions */
4413 (void)win_comp_pos();
4414 }
4415}
4416#endif /* FEAT_CMDWIN */
4417
4418#if defined(FEAT_WINDOWS) || defined(PROTO)
4419/*
4420 * Update the position for all windows, using the width and height of the
4421 * frames.
4422 * Returns the row just after the last window.
4423 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004424 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425win_comp_pos()
4426{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004427 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 int col = 0;
4429
4430 frame_comp_pos(topframe, &row, &col);
4431 return row;
4432}
4433
4434/*
4435 * Update the position of the windows in frame "topfrp", using the width and
4436 * height of the frames.
4437 * "*row" and "*col" are the top-left position of the frame. They are updated
4438 * to the bottom-right position plus one.
4439 */
4440 static void
4441frame_comp_pos(topfrp, row, col)
4442 frame_T *topfrp;
4443 int *row;
4444 int *col;
4445{
4446 win_T *wp;
4447 frame_T *frp;
4448#ifdef FEAT_VERTSPLIT
4449 int startcol;
4450 int startrow;
4451#endif
4452
4453 wp = topfrp->fr_win;
4454 if (wp != NULL)
4455 {
4456 if (wp->w_winrow != *row
4457#ifdef FEAT_VERTSPLIT
4458 || wp->w_wincol != *col
4459#endif
4460 )
4461 {
4462 /* position changed, redraw */
4463 wp->w_winrow = *row;
4464#ifdef FEAT_VERTSPLIT
4465 wp->w_wincol = *col;
4466#endif
4467 redraw_win_later(wp, NOT_VALID);
4468 wp->w_redr_status = TRUE;
4469 }
4470 *row += wp->w_height + wp->w_status_height;
4471#ifdef FEAT_VERTSPLIT
4472 *col += wp->w_width + wp->w_vsep_width;
4473#endif
4474 }
4475 else
4476 {
4477#ifdef FEAT_VERTSPLIT
4478 startrow = *row;
4479 startcol = *col;
4480#endif
4481 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4482 {
4483#ifdef FEAT_VERTSPLIT
4484 if (topfrp->fr_layout == FR_ROW)
4485 *row = startrow; /* all frames are at the same row */
4486 else
4487 *col = startcol; /* all frames are at the same col */
4488#endif
4489 frame_comp_pos(frp, row, col);
4490 }
4491 }
4492}
4493
4494#endif /* FEAT_WINDOWS */
4495
4496/*
4497 * Set current window height and take care of repositioning other windows to
4498 * fit around it.
4499 */
4500 void
4501win_setheight(height)
4502 int height;
4503{
4504 win_setheight_win(height, curwin);
4505}
4506
4507/*
4508 * Set the window height of window "win" and take care of repositioning other
4509 * windows to fit around it.
4510 */
4511 void
4512win_setheight_win(height, win)
4513 int height;
4514 win_T *win;
4515{
4516 int row;
4517
4518 if (win == curwin)
4519 {
4520 /* Always keep current window at least one line high, even when
4521 * 'winminheight' is zero. */
4522#ifdef FEAT_WINDOWS
4523 if (height < p_wmh)
4524 height = p_wmh;
4525#endif
4526 if (height == 0)
4527 height = 1;
4528 }
4529
4530#ifdef FEAT_WINDOWS
4531 frame_setheight(win->w_frame, height + win->w_status_height);
4532
4533 /* recompute the window positions */
4534 row = win_comp_pos();
4535#else
4536 if (height > topframe->fr_height)
4537 height = topframe->fr_height;
4538 win->w_height = height;
4539 row = height;
4540#endif
4541
4542 /*
4543 * If there is extra space created between the last window and the command
4544 * line, clear it.
4545 */
4546 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4547 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4548 cmdline_row = row;
4549 msg_row = row;
4550 msg_col = 0;
4551
4552 redraw_all_later(NOT_VALID);
4553}
4554
4555#if defined(FEAT_WINDOWS) || defined(PROTO)
4556
4557/*
4558 * Set the height of a frame to "height" and take care that all frames and
4559 * windows inside it are resized. Also resize frames on the left and right if
4560 * the are in the same FR_ROW frame.
4561 *
4562 * Strategy:
4563 * If the frame is part of a FR_COL frame, try fitting the frame in that
4564 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4565 * go to containing frames to resize them and make room.
4566 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4567 * Check for the minimal height of the FR_ROW frame.
4568 * At the top level we can also use change the command line height.
4569 */
4570 static void
4571frame_setheight(curfrp, height)
4572 frame_T *curfrp;
4573 int height;
4574{
4575 int room; /* total number of lines available */
4576 int take; /* number of lines taken from other windows */
4577 int room_cmdline; /* lines available from cmdline */
4578 int run;
4579 frame_T *frp;
4580 int h;
4581 int room_reserved;
4582
4583 /* If the height already is the desired value, nothing to do. */
4584 if (curfrp->fr_height == height)
4585 return;
4586
4587 if (curfrp->fr_parent == NULL)
4588 {
4589 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004590 if (height > ROWS_AVAIL)
4591 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 if (height > 0)
4593 frame_new_height(curfrp, height, FALSE, FALSE);
4594 }
4595 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4596 {
4597 /* Row of frames: Also need to resize frames left and right of this
4598 * one. First check for the minimal height of these. */
4599 h = frame_minheight(curfrp->fr_parent, NULL);
4600 if (height < h)
4601 height = h;
4602 frame_setheight(curfrp->fr_parent, height);
4603 }
4604 else
4605 {
4606 /*
4607 * Column of frames: try to change only frames in this column.
4608 */
4609#ifdef FEAT_VERTSPLIT
4610 /*
4611 * Do this twice:
4612 * 1: compute room available, if it's not enough try resizing the
4613 * containing frame.
4614 * 2: compute the room available and adjust the height to it.
4615 * Try not to reduce the height of a window with 'winfixheight' set.
4616 */
4617 for (run = 1; run <= 2; ++run)
4618#else
4619 for (;;)
4620#endif
4621 {
4622 room = 0;
4623 room_reserved = 0;
4624 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4625 frp = frp->fr_next)
4626 {
4627 if (frp != curfrp
4628 && frp->fr_win != NULL
4629 && frp->fr_win->w_p_wfh)
4630 room_reserved += frp->fr_height;
4631 room += frp->fr_height;
4632 if (frp != curfrp)
4633 room -= frame_minheight(frp, NULL);
4634 }
4635#ifdef FEAT_VERTSPLIT
4636 if (curfrp->fr_width != Columns)
4637 room_cmdline = 0;
4638 else
4639#endif
4640 {
4641 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4642 + lastwin->w_height + lastwin->w_status_height);
4643 if (room_cmdline < 0)
4644 room_cmdline = 0;
4645 }
4646
4647 if (height <= room + room_cmdline)
4648 break;
4649#ifdef FEAT_VERTSPLIT
4650 if (run == 2 || curfrp->fr_width == Columns)
4651#endif
4652 {
4653 if (height > room + room_cmdline)
4654 height = room + room_cmdline;
4655 break;
4656 }
4657#ifdef FEAT_VERTSPLIT
4658 frame_setheight(curfrp->fr_parent, height
4659 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4660#endif
4661 /*NOTREACHED*/
4662 }
4663
4664 /*
4665 * Compute the number of lines we will take from others frames (can be
4666 * negative!).
4667 */
4668 take = height - curfrp->fr_height;
4669
4670 /* If there is not enough room, also reduce the height of a window
4671 * with 'winfixheight' set. */
4672 if (height > room + room_cmdline - room_reserved)
4673 room_reserved = room + room_cmdline - height;
4674 /* If there is only a 'winfixheight' window and making the
4675 * window smaller, need to make the other window taller. */
4676 if (take < 0 && room - curfrp->fr_height < room_reserved)
4677 room_reserved = 0;
4678
4679 if (take > 0 && room_cmdline > 0)
4680 {
4681 /* use lines from cmdline first */
4682 if (take < room_cmdline)
4683 room_cmdline = take;
4684 take -= room_cmdline;
4685 topframe->fr_height += room_cmdline;
4686 }
4687
4688 /*
4689 * set the current frame to the new height
4690 */
4691 frame_new_height(curfrp, height, FALSE, FALSE);
4692
4693 /*
4694 * First take lines from the frames after the current frame. If
4695 * that is not enough, takes lines from frames above the current
4696 * frame.
4697 */
4698 for (run = 0; run < 2; ++run)
4699 {
4700 if (run == 0)
4701 frp = curfrp->fr_next; /* 1st run: start with next window */
4702 else
4703 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4704 while (frp != NULL && take != 0)
4705 {
4706 h = frame_minheight(frp, NULL);
4707 if (room_reserved > 0
4708 && frp->fr_win != NULL
4709 && frp->fr_win->w_p_wfh)
4710 {
4711 if (room_reserved >= frp->fr_height)
4712 room_reserved -= frp->fr_height;
4713 else
4714 {
4715 if (frp->fr_height - room_reserved > take)
4716 room_reserved = frp->fr_height - take;
4717 take -= frp->fr_height - room_reserved;
4718 frame_new_height(frp, room_reserved, FALSE, FALSE);
4719 room_reserved = 0;
4720 }
4721 }
4722 else
4723 {
4724 if (frp->fr_height - take < h)
4725 {
4726 take -= frp->fr_height - h;
4727 frame_new_height(frp, h, FALSE, FALSE);
4728 }
4729 else
4730 {
4731 frame_new_height(frp, frp->fr_height - take,
4732 FALSE, FALSE);
4733 take = 0;
4734 }
4735 }
4736 if (run == 0)
4737 frp = frp->fr_next;
4738 else
4739 frp = frp->fr_prev;
4740 }
4741 }
4742 }
4743}
4744
4745#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4746/*
4747 * Set current window width and take care of repositioning other windows to
4748 * fit around it.
4749 */
4750 void
4751win_setwidth(width)
4752 int width;
4753{
4754 win_setwidth_win(width, curwin);
4755}
4756
4757 void
4758win_setwidth_win(width, wp)
4759 int width;
4760 win_T *wp;
4761{
4762 /* Always keep current window at least one column wide, even when
4763 * 'winminwidth' is zero. */
4764 if (wp == curwin)
4765 {
4766 if (width < p_wmw)
4767 width = p_wmw;
4768 if (width == 0)
4769 width = 1;
4770 }
4771
4772 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4773
4774 /* recompute the window positions */
4775 (void)win_comp_pos();
4776
4777 redraw_all_later(NOT_VALID);
4778}
4779
4780/*
4781 * Set the width of a frame to "width" and take care that all frames and
4782 * windows inside it are resized. Also resize frames above and below if the
4783 * are in the same FR_ROW frame.
4784 *
4785 * Strategy is similar to frame_setheight().
4786 */
4787 static void
4788frame_setwidth(curfrp, width)
4789 frame_T *curfrp;
4790 int width;
4791{
4792 int room; /* total number of lines available */
4793 int take; /* number of lines taken from other windows */
4794 int run;
4795 frame_T *frp;
4796 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004797 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798
4799 /* If the width already is the desired value, nothing to do. */
4800 if (curfrp->fr_width == width)
4801 return;
4802
4803 if (curfrp->fr_parent == NULL)
4804 /* topframe: can't change width */
4805 return;
4806
4807 if (curfrp->fr_parent->fr_layout == FR_COL)
4808 {
4809 /* Column of frames: Also need to resize frames above and below of
4810 * this one. First check for the minimal width of these. */
4811 w = frame_minwidth(curfrp->fr_parent, NULL);
4812 if (width < w)
4813 width = w;
4814 frame_setwidth(curfrp->fr_parent, width);
4815 }
4816 else
4817 {
4818 /*
4819 * Row of frames: try to change only frames in this row.
4820 *
4821 * Do this twice:
4822 * 1: compute room available, if it's not enough try resizing the
4823 * containing frame.
4824 * 2: compute the room available and adjust the width to it.
4825 */
4826 for (run = 1; run <= 2; ++run)
4827 {
4828 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004829 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4831 frp = frp->fr_next)
4832 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004833 if (frp != curfrp
4834 && frp->fr_win != NULL
4835 && frp->fr_win->w_p_wfw)
4836 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 room += frp->fr_width;
4838 if (frp != curfrp)
4839 room -= frame_minwidth(frp, NULL);
4840 }
4841
4842 if (width <= room)
4843 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004844 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 {
4846 if (width > room)
4847 width = room;
4848 break;
4849 }
4850 frame_setwidth(curfrp->fr_parent, width
4851 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4852 }
4853
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 /*
4855 * Compute the number of lines we will take from others frames (can be
4856 * negative!).
4857 */
4858 take = width - curfrp->fr_width;
4859
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004860 /* If there is not enough room, also reduce the width of a window
4861 * with 'winfixwidth' set. */
4862 if (width > room - room_reserved)
4863 room_reserved = room - width;
4864 /* If there is only a 'winfixwidth' window and making the
4865 * window smaller, need to make the other window narrower. */
4866 if (take < 0 && room - curfrp->fr_width < room_reserved)
4867 room_reserved = 0;
4868
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 /*
4870 * set the current frame to the new width
4871 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004872 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873
4874 /*
4875 * First take lines from the frames right of the current frame. If
4876 * that is not enough, takes lines from frames left of the current
4877 * frame.
4878 */
4879 for (run = 0; run < 2; ++run)
4880 {
4881 if (run == 0)
4882 frp = curfrp->fr_next; /* 1st run: start with next window */
4883 else
4884 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4885 while (frp != NULL && take != 0)
4886 {
4887 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004888 if (room_reserved > 0
4889 && frp->fr_win != NULL
4890 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004892 if (room_reserved >= frp->fr_width)
4893 room_reserved -= frp->fr_width;
4894 else
4895 {
4896 if (frp->fr_width - room_reserved > take)
4897 room_reserved = frp->fr_width - take;
4898 take -= frp->fr_width - room_reserved;
4899 frame_new_width(frp, room_reserved, FALSE, FALSE);
4900 room_reserved = 0;
4901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903 else
4904 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004905 if (frp->fr_width - take < w)
4906 {
4907 take -= frp->fr_width - w;
4908 frame_new_width(frp, w, FALSE, FALSE);
4909 }
4910 else
4911 {
4912 frame_new_width(frp, frp->fr_width - take,
4913 FALSE, FALSE);
4914 take = 0;
4915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 if (run == 0)
4918 frp = frp->fr_next;
4919 else
4920 frp = frp->fr_prev;
4921 }
4922 }
4923 }
4924}
4925#endif /* FEAT_VERTSPLIT */
4926
4927/*
4928 * Check 'winminheight' for a valid value.
4929 */
4930 void
4931win_setminheight()
4932{
4933 int room;
4934 int first = TRUE;
4935 win_T *wp;
4936
4937 /* loop until there is a 'winminheight' that is possible */
4938 while (p_wmh > 0)
4939 {
4940 /* TODO: handle vertical splits */
4941 room = -p_wh;
4942 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4943 room += wp->w_height - p_wmh;
4944 if (room >= 0)
4945 break;
4946 --p_wmh;
4947 if (first)
4948 {
4949 EMSG(_(e_noroom));
4950 first = FALSE;
4951 }
4952 }
4953}
4954
4955#ifdef FEAT_MOUSE
4956
4957/*
4958 * Status line of dragwin is dragged "offset" lines down (negative is up).
4959 */
4960 void
4961win_drag_status_line(dragwin, offset)
4962 win_T *dragwin;
4963 int offset;
4964{
4965 frame_T *curfr;
4966 frame_T *fr;
4967 int room;
4968 int row;
4969 int up; /* if TRUE, drag status line up, otherwise down */
4970 int n;
4971
4972 fr = dragwin->w_frame;
4973 curfr = fr;
4974 if (fr != topframe) /* more than one window */
4975 {
4976 fr = fr->fr_parent;
4977 /* When the parent frame is not a column of frames, its parent should
4978 * be. */
4979 if (fr->fr_layout != FR_COL)
4980 {
4981 curfr = fr;
4982 if (fr != topframe) /* only a row of windows, may drag statusline */
4983 fr = fr->fr_parent;
4984 }
4985 }
4986
4987 /* If this is the last frame in a column, may want to resize the parent
4988 * frame instead (go two up to skip a row of frames). */
4989 while (curfr != topframe && curfr->fr_next == NULL)
4990 {
4991 if (fr != topframe)
4992 fr = fr->fr_parent;
4993 curfr = fr;
4994 if (fr != topframe)
4995 fr = fr->fr_parent;
4996 }
4997
4998 if (offset < 0) /* drag up */
4999 {
5000 up = TRUE;
5001 offset = -offset;
5002 /* sum up the room of the current frame and above it */
5003 if (fr == curfr)
5004 {
5005 /* only one window */
5006 room = fr->fr_height - frame_minheight(fr, NULL);
5007 }
5008 else
5009 {
5010 room = 0;
5011 for (fr = fr->fr_child; ; fr = fr->fr_next)
5012 {
5013 room += fr->fr_height - frame_minheight(fr, NULL);
5014 if (fr == curfr)
5015 break;
5016 }
5017 }
5018 fr = curfr->fr_next; /* put fr at frame that grows */
5019 }
5020 else /* drag down */
5021 {
5022 up = FALSE;
5023 /*
5024 * Only dragging the last status line can reduce p_ch.
5025 */
5026 room = Rows - cmdline_row;
5027 if (curfr->fr_next == NULL)
5028 room -= 1;
5029 else
5030 room -= p_ch;
5031 if (room < 0)
5032 room = 0;
5033 /* sum up the room of frames below of the current one */
5034 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5035 room += fr->fr_height - frame_minheight(fr, NULL);
5036 fr = curfr; /* put fr at window that grows */
5037 }
5038
5039 if (room < offset) /* Not enough room */
5040 offset = room; /* Move as far as we can */
5041 if (offset <= 0)
5042 return;
5043
5044 /*
5045 * Grow frame fr by "offset" lines.
5046 * Doesn't happen when dragging the last status line up.
5047 */
5048 if (fr != NULL)
5049 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5050
5051 if (up)
5052 fr = curfr; /* current frame gets smaller */
5053 else
5054 fr = curfr->fr_next; /* next frame gets smaller */
5055
5056 /*
5057 * Now make the other frames smaller.
5058 */
5059 while (fr != NULL && offset > 0)
5060 {
5061 n = frame_minheight(fr, NULL);
5062 if (fr->fr_height - offset <= n)
5063 {
5064 offset -= fr->fr_height - n;
5065 frame_new_height(fr, n, !up, FALSE);
5066 }
5067 else
5068 {
5069 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5070 break;
5071 }
5072 if (up)
5073 fr = fr->fr_prev;
5074 else
5075 fr = fr->fr_next;
5076 }
5077 row = win_comp_pos();
5078 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5079 cmdline_row = row;
5080 p_ch = Rows - cmdline_row;
5081 if (p_ch < 1)
5082 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005083 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005084 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 showmode();
5086}
5087
5088#ifdef FEAT_VERTSPLIT
5089/*
5090 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5091 */
5092 void
5093win_drag_vsep_line(dragwin, offset)
5094 win_T *dragwin;
5095 int offset;
5096{
5097 frame_T *curfr;
5098 frame_T *fr;
5099 int room;
5100 int left; /* if TRUE, drag separator line left, otherwise right */
5101 int n;
5102
5103 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005104 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 return;
5106 curfr = fr;
5107 fr = fr->fr_parent;
5108 /* When the parent frame is not a row of frames, its parent should be. */
5109 if (fr->fr_layout != FR_ROW)
5110 {
5111 if (fr == topframe) /* only a column of windows (cannot happen?) */
5112 return;
5113 curfr = fr;
5114 fr = fr->fr_parent;
5115 }
5116
5117 /* If this is the last frame in a row, may want to resize a parent
5118 * frame instead. */
5119 while (curfr->fr_next == NULL)
5120 {
5121 if (fr == topframe)
5122 break;
5123 curfr = fr;
5124 fr = fr->fr_parent;
5125 if (fr != topframe)
5126 {
5127 curfr = fr;
5128 fr = fr->fr_parent;
5129 }
5130 }
5131
5132 if (offset < 0) /* drag left */
5133 {
5134 left = TRUE;
5135 offset = -offset;
5136 /* sum up the room of the current frame and left of it */
5137 room = 0;
5138 for (fr = fr->fr_child; ; fr = fr->fr_next)
5139 {
5140 room += fr->fr_width - frame_minwidth(fr, NULL);
5141 if (fr == curfr)
5142 break;
5143 }
5144 fr = curfr->fr_next; /* put fr at frame that grows */
5145 }
5146 else /* drag right */
5147 {
5148 left = FALSE;
5149 /* sum up the room of frames right of the current one */
5150 room = 0;
5151 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5152 room += fr->fr_width - frame_minwidth(fr, NULL);
5153 fr = curfr; /* put fr at window that grows */
5154 }
5155
5156 if (room < offset) /* Not enough room */
5157 offset = room; /* Move as far as we can */
5158 if (offset <= 0) /* No room at all, quit. */
5159 return;
5160
5161 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005162 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
5164 /* shrink other frames: current and at the left or at the right */
5165 if (left)
5166 fr = curfr; /* current frame gets smaller */
5167 else
5168 fr = curfr->fr_next; /* next frame gets smaller */
5169
5170 while (fr != NULL && offset > 0)
5171 {
5172 n = frame_minwidth(fr, NULL);
5173 if (fr->fr_width - offset <= n)
5174 {
5175 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005176 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178 else
5179 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005180 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 break;
5182 }
5183 if (left)
5184 fr = fr->fr_prev;
5185 else
5186 fr = fr->fr_next;
5187 }
5188 (void)win_comp_pos();
5189 redraw_all_later(NOT_VALID);
5190}
5191#endif /* FEAT_VERTSPLIT */
5192#endif /* FEAT_MOUSE */
5193
5194#endif /* FEAT_WINDOWS */
5195
5196/*
5197 * Set the height of a window.
5198 * This takes care of the things inside the window, not what happens to the
5199 * window position, the frame or to other windows.
5200 */
5201 static void
5202win_new_height(wp, height)
5203 win_T *wp;
5204 int height;
5205{
5206 linenr_T lnum;
5207 int sline, line_size;
5208#define FRACTION_MULT 16384L
5209
5210 /* Don't want a negative height. Happens when splitting a tiny window.
5211 * Will equalize heights soon to fix it. */
5212 if (height < 0)
5213 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005214 if (wp->w_height == height)
5215 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216
5217 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5218 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5219 + FRACTION_MULT / 2) / (long)wp->w_height;
5220
5221 wp->w_height = height;
5222 wp->w_skipcol = 0;
5223
5224 /* Don't change w_topline when height is zero. Don't set w_topline when
5225 * 'scrollbind' is set and this isn't the current window. */
5226 if (height > 0
5227#ifdef FEAT_SCROLLBIND
5228 && (!wp->w_p_scb || wp == curwin)
5229#endif
5230 )
5231 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005232 /*
5233 * Find a value for w_topline that shows the cursor at the same
5234 * relative position in the window as before (more or less).
5235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 lnum = wp->w_cursor.lnum;
5237 if (lnum < 1) /* can happen when starting up */
5238 lnum = 1;
5239 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5240 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5241 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005242
5243 if (sline >= 0)
5244 {
5245 /* Make sure the whole cursor line is visible, if possible. */
5246 int rows = plines_win(wp, lnum, FALSE);
5247
5248 if (sline > wp->w_height - rows)
5249 {
5250 sline = wp->w_height - rows;
5251 wp->w_wrow -= rows - line_size;
5252 }
5253 }
5254
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 if (sline < 0)
5256 {
5257 /*
5258 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005259 * Make cursor line the first line in the window. If not enough
5260 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 */
5262 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005263 if (wp->w_wrow >= wp->w_height
5264 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5265 {
5266 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5267 --wp->w_wrow;
5268 while (wp->w_wrow >= wp->w_height)
5269 {
5270 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5271 + win_col_off2(wp);
5272 --wp->w_wrow;
5273 }
5274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 }
5276 else
5277 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005278 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
5280#ifdef FEAT_FOLDING
5281 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5282 if (lnum == 1)
5283 {
5284 /* first line in buffer is folded */
5285 line_size = 1;
5286 --sline;
5287 break;
5288 }
5289#endif
5290 --lnum;
5291#ifdef FEAT_DIFF
5292 if (lnum == wp->w_topline)
5293 line_size = plines_win_nofill(wp, lnum, TRUE)
5294 + wp->w_topfill;
5295 else
5296#endif
5297 line_size = plines_win(wp, lnum, TRUE);
5298 sline -= line_size;
5299 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005300
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 if (sline < 0)
5302 {
5303 /*
5304 * Line we want at top would go off top of screen. Use next
5305 * line instead.
5306 */
5307#ifdef FEAT_FOLDING
5308 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5309#endif
5310 lnum++;
5311 wp->w_wrow -= line_size + sline;
5312 }
5313 else if (sline > 0)
5314 {
5315 /* First line of file reached, use that as topline. */
5316 lnum = 1;
5317 wp->w_wrow -= sline;
5318 }
5319 }
5320 set_topline(wp, lnum);
5321 }
5322
5323 if (wp == curwin)
5324 {
5325 if (p_so)
5326 update_topline();
5327 curs_columns(FALSE); /* validate w_wrow */
5328 }
5329 wp->w_prev_fraction_row = wp->w_wrow;
5330
5331 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005332 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333#ifdef FEAT_WINDOWS
5334 wp->w_redr_status = TRUE;
5335#endif
5336 invalidate_botline_win(wp);
5337}
5338
5339#ifdef FEAT_VERTSPLIT
5340/*
5341 * Set the width of a window.
5342 */
5343 static void
5344win_new_width(wp, width)
5345 win_T *wp;
5346 int width;
5347{
5348 wp->w_width = width;
5349 wp->w_lines_valid = 0;
5350 changed_line_abv_curs_win(wp);
5351 invalidate_botline_win(wp);
5352 if (wp == curwin)
5353 {
5354 update_topline();
5355 curs_columns(TRUE); /* validate w_wrow */
5356 }
5357 redraw_win_later(wp, NOT_VALID);
5358 wp->w_redr_status = TRUE;
5359}
5360#endif
5361
5362 void
5363win_comp_scroll(wp)
5364 win_T *wp;
5365{
5366 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5367 if (wp->w_p_scr == 0)
5368 wp->w_p_scr = 1;
5369}
5370
5371/*
5372 * command_height: called whenever p_ch has been changed
5373 */
5374 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005375command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
5377#ifdef FEAT_WINDOWS
5378 int h;
5379 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005380 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005382 /* Use the value of p_ch that we remembered. This is needed for when the
5383 * GUI starts up, we can't be sure in what order things happen. And when
5384 * p_ch was changed in another tab page. */
5385 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005386
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 /* Find bottom frame with width of screen. */
5388 frp = lastwin->w_frame;
5389# ifdef FEAT_VERTSPLIT
5390 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5391 frp = frp->fr_parent;
5392# endif
5393
5394 /* Avoid changing the height of a window with 'winfixheight' set. */
5395 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5396 && frp->fr_win->w_p_wfh)
5397 frp = frp->fr_prev;
5398
5399 if (starting != NO_SCREEN)
5400 {
5401 cmdline_row = Rows - p_ch;
5402
5403 if (p_ch > old_p_ch) /* p_ch got bigger */
5404 {
5405 while (p_ch > old_p_ch)
5406 {
5407 if (frp == NULL)
5408 {
5409 EMSG(_(e_noroom));
5410 p_ch = old_p_ch;
5411 cmdline_row = Rows - p_ch;
5412 break;
5413 }
5414 h = frp->fr_height - frame_minheight(frp, NULL);
5415 if (h > p_ch - old_p_ch)
5416 h = p_ch - old_p_ch;
5417 old_p_ch += h;
5418 frame_add_height(frp, -h);
5419 frp = frp->fr_prev;
5420 }
5421
5422 /* Recompute window positions. */
5423 (void)win_comp_pos();
5424
5425 /* clear the lines added to cmdline */
5426 if (full_screen)
5427 screen_fill((int)(cmdline_row), (int)Rows, 0,
5428 (int)Columns, ' ', ' ', 0);
5429 msg_row = cmdline_row;
5430 redraw_cmdline = TRUE;
5431 return;
5432 }
5433
5434 if (msg_row < cmdline_row)
5435 msg_row = cmdline_row;
5436 redraw_cmdline = TRUE;
5437 }
5438 frame_add_height(frp, (int)(old_p_ch - p_ch));
5439
5440 /* Recompute window positions. */
5441 if (frp != lastwin->w_frame)
5442 (void)win_comp_pos();
5443#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005445 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446#endif
5447}
5448
5449#if defined(FEAT_WINDOWS) || defined(PROTO)
5450/*
5451 * Resize frame "frp" to be "n" lines higher (negative for less high).
5452 * Also resize the frames it is contained in.
5453 */
5454 static void
5455frame_add_height(frp, n)
5456 frame_T *frp;
5457 int n;
5458{
5459 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5460 for (;;)
5461 {
5462 frp = frp->fr_parent;
5463 if (frp == NULL)
5464 break;
5465 frp->fr_height += n;
5466 }
5467}
5468
5469/*
5470 * Add or remove a status line for the bottom window(s), according to the
5471 * value of 'laststatus'.
5472 */
5473 void
5474last_status(morewin)
5475 int morewin; /* pretend there are two or more windows */
5476{
5477 /* Don't make a difference between horizontal or vertical split. */
5478 last_status_rec(topframe, (p_ls == 2
5479 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5480}
5481
5482 static void
5483last_status_rec(fr, statusline)
5484 frame_T *fr;
5485 int statusline;
5486{
5487 frame_T *fp;
5488 win_T *wp;
5489
5490 if (fr->fr_layout == FR_LEAF)
5491 {
5492 wp = fr->fr_win;
5493 if (wp->w_status_height != 0 && !statusline)
5494 {
5495 /* remove status line */
5496 win_new_height(wp, wp->w_height + 1);
5497 wp->w_status_height = 0;
5498 comp_col();
5499 }
5500 else if (wp->w_status_height == 0 && statusline)
5501 {
5502 /* Find a frame to take a line from. */
5503 fp = fr;
5504 while (fp->fr_height <= frame_minheight(fp, NULL))
5505 {
5506 if (fp == topframe)
5507 {
5508 EMSG(_(e_noroom));
5509 return;
5510 }
5511 /* In a column of frames: go to frame above. If already at
5512 * the top or in a row of frames: go to parent. */
5513 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5514 fp = fp->fr_prev;
5515 else
5516 fp = fp->fr_parent;
5517 }
5518 wp->w_status_height = 1;
5519 if (fp != fr)
5520 {
5521 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5522 frame_fix_height(wp);
5523 (void)win_comp_pos();
5524 }
5525 else
5526 win_new_height(wp, wp->w_height - 1);
5527 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005528 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 }
5530 }
5531#ifdef FEAT_VERTSPLIT
5532 else if (fr->fr_layout == FR_ROW)
5533 {
5534 /* vertically split windows, set status line for each one */
5535 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5536 last_status_rec(fp, statusline);
5537 }
5538#endif
5539 else
5540 {
5541 /* horizontally split window, set status line for last one */
5542 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5543 ;
5544 last_status_rec(fp, statusline);
5545 }
5546}
5547
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005548/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005549 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005550 */
5551 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005552tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005553{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005554#ifdef FEAT_GUI_TABLINE
5555 /* When the GUI has the tabline then this always returns zero. */
5556 if (gui_use_tabline())
5557 return 0;
5558#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005559 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005560 {
5561 case 0: return 0;
5562 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5563 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005564 return 1;
5565}
5566
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567#endif /* FEAT_WINDOWS */
5568
5569#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5570/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005571 * Get the file name at the cursor.
5572 * If Visual mode is active, use the selected text if it's in one line.
5573 * Returns the name in allocated memory, NULL for failure.
5574 */
5575 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005576grab_file_name(count, file_lnum)
5577 long count;
5578 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005579{
5580# ifdef FEAT_VISUAL
5581 if (VIsual_active)
5582 {
5583 int len;
5584 char_u *ptr;
5585
5586 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5587 return NULL;
5588 return find_file_name_in_path(ptr, len,
5589 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5590 }
5591# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005592 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5593 file_lnum);
5594
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005595}
5596
5597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 * Return the file name under or after the cursor.
5599 *
5600 * The 'path' option is searched if the file name is not absolute.
5601 * The string returned has been alloc'ed and should be freed by the caller.
5602 * NULL is returned if the file name or file is not found.
5603 *
5604 * options:
5605 * FNAME_MESS give error messages
5606 * FNAME_EXP expand to path
5607 * FNAME_HYP check for hypertext link
5608 * FNAME_INCL apply "includeexpr"
5609 */
5610 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005611file_name_at_cursor(options, count, file_lnum)
5612 int options;
5613 long count;
5614 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615{
5616 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005617 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5618 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619}
5620
5621/*
5622 * Return the name of the file under or after ptr[col].
5623 * Otherwise like file_name_at_cursor().
5624 */
5625 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005626file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 char_u *line;
5628 int col;
5629 int options;
5630 long count;
5631 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005632 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633{
5634 char_u *ptr;
5635 int len;
5636
5637 /*
5638 * search forward for what could be the start of a file name
5639 */
5640 ptr = line + col;
5641 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005642 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 if (*ptr == NUL) /* nothing found */
5644 {
5645 if (options & FNAME_MESS)
5646 EMSG(_("E446: No file name under cursor"));
5647 return NULL;
5648 }
5649
5650 /*
5651 * Search backward for first char of the file name.
5652 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5653 */
5654 while (ptr > line)
5655 {
5656#ifdef FEAT_MBYTE
5657 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5658 ptr -= len + 1;
5659 else
5660#endif
5661 if (vim_isfilec(ptr[-1])
5662 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5663 --ptr;
5664 else
5665 break;
5666 }
5667
5668 /*
5669 * Search forward for the last char of the file name.
5670 * Also allow "://" when ':' is not in 'isfname'.
5671 */
5672 len = 0;
5673 while (vim_isfilec(ptr[len])
5674 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5675#ifdef FEAT_MBYTE
5676 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005677 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 else
5679#endif
5680 ++len;
5681
5682 /*
5683 * If there is trailing punctuation, remove it.
5684 * But don't remove "..", could be a directory name.
5685 */
5686 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5687 && ptr[len - 2] != '.')
5688 --len;
5689
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005690 if (file_lnum != NULL)
5691 {
5692 char_u *p;
5693
5694 /* Get the number after the file name and a separator character */
5695 p = ptr + len;
5696 p = skipwhite(p);
5697 if (*p != NUL)
5698 {
5699 if (!isdigit(*p))
5700 ++p; /* skip the separator */
5701 p = skipwhite(p);
5702 if (isdigit(*p))
5703 *file_lnum = (int)getdigits(&p);
5704 }
5705 }
5706
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5708}
5709
5710# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5711static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5712
5713 static char_u *
5714eval_includeexpr(ptr, len)
5715 char_u *ptr;
5716 int len;
5717{
5718 char_u *res;
5719
5720 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005721 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005722 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 set_vim_var_string(VV_FNAME, NULL, 0);
5724 return res;
5725}
5726#endif
5727
5728/*
5729 * Return the name of the file ptr[len] in 'path'.
5730 * Otherwise like file_name_at_cursor().
5731 */
5732 char_u *
5733find_file_name_in_path(ptr, len, options, count, rel_fname)
5734 char_u *ptr;
5735 int len;
5736 int options;
5737 long count;
5738 char_u *rel_fname; /* file we are searching relative to */
5739{
5740 char_u *file_name;
5741 int c;
5742# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5743 char_u *tofree = NULL;
5744
5745 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5746 {
5747 tofree = eval_includeexpr(ptr, len);
5748 if (tofree != NULL)
5749 {
5750 ptr = tofree;
5751 len = (int)STRLEN(ptr);
5752 }
5753 }
5754# endif
5755
5756 if (options & FNAME_EXP)
5757 {
5758 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5759 TRUE, rel_fname);
5760
5761# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5762 /*
5763 * If the file could not be found in a normal way, try applying
5764 * 'includeexpr' (unless done already).
5765 */
5766 if (file_name == NULL
5767 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5768 {
5769 tofree = eval_includeexpr(ptr, len);
5770 if (tofree != NULL)
5771 {
5772 ptr = tofree;
5773 len = (int)STRLEN(ptr);
5774 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5775 TRUE, rel_fname);
5776 }
5777 }
5778# endif
5779 if (file_name == NULL && (options & FNAME_MESS))
5780 {
5781 c = ptr[len];
5782 ptr[len] = NUL;
5783 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5784 ptr[len] = c;
5785 }
5786
5787 /* Repeat finding the file "count" times. This matters when it
5788 * appears several times in the path. */
5789 while (file_name != NULL && --count > 0)
5790 {
5791 vim_free(file_name);
5792 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5793 }
5794 }
5795 else
5796 file_name = vim_strnsave(ptr, len);
5797
5798# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5799 vim_free(tofree);
5800# endif
5801
5802 return file_name;
5803}
5804#endif /* FEAT_SEARCHPATH */
5805
5806/*
5807 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5808 * Also check for ":\\", which MS Internet Explorer accepts, return
5809 * URL_BACKSLASH.
5810 */
5811 static int
5812path_is_url(p)
5813 char_u *p;
5814{
5815 if (STRNCMP(p, "://", (size_t)3) == 0)
5816 return URL_SLASH;
5817 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5818 return URL_BACKSLASH;
5819 return 0;
5820}
5821
5822/*
5823 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5824 * Return URL_BACKSLASH for "name:\\".
5825 * Return zero otherwise.
5826 */
5827 int
5828path_with_url(fname)
5829 char_u *fname;
5830{
5831 char_u *p;
5832
5833 for (p = fname; isalpha(*p); ++p)
5834 ;
5835 return path_is_url(p);
5836}
5837
5838/*
5839 * Return TRUE if "name" is a full (absolute) path name or URL.
5840 */
5841 int
5842vim_isAbsName(name)
5843 char_u *name;
5844{
5845 return (path_with_url(name) != 0 || mch_isFullName(name));
5846}
5847
5848/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005849 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 *
5851 * return FAIL for failure, OK otherwise
5852 */
5853 int
5854vim_FullName(fname, buf, len, force)
5855 char_u *fname, *buf;
5856 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005857 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858{
5859 int retval = OK;
5860 int url;
5861
5862 *buf = NUL;
5863 if (fname == NULL)
5864 return FAIL;
5865
5866 url = path_with_url(fname);
5867 if (!url)
5868 retval = mch_FullName(fname, buf, len, force);
5869 if (url || retval == FAIL)
5870 {
5871 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005872 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 }
5874#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5875 slash_adjust(buf);
5876#endif
5877 return retval;
5878}
5879
5880/*
5881 * Return the minimal number of rows that is needed on the screen to display
5882 * the current number of windows.
5883 */
5884 int
5885min_rows()
5886{
5887 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005888#ifdef FEAT_WINDOWS
5889 tabpage_T *tp;
5890 int n;
5891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892
5893 if (firstwin == NULL) /* not initialized yet */
5894 return MIN_LINES;
5895
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005897 total = 0;
5898 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5899 {
5900 n = frame_minheight(tp->tp_topframe, NULL);
5901 if (total < n)
5902 total = n;
5903 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005904 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005906 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005908 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 return total;
5910}
5911
5912/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005913 * Return TRUE if there is only one window (in the current tab page), not
5914 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 */
5916 int
5917only_one_window()
5918{
5919#ifdef FEAT_WINDOWS
5920 int count = 0;
5921 win_T *wp;
5922
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005923 /* If there is another tab page there always is another window. */
5924 if (first_tabpage->tp_next != NULL)
5925 return FALSE;
5926
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005928 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929# ifdef FEAT_QUICKFIX
5930 || wp->w_p_pvw
5931# endif
5932 ) || wp == curwin)
5933 ++count;
5934 return (count <= 1);
5935#else
5936 return TRUE;
5937#endif
5938}
5939
5940#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5941/*
5942 * Correct the cursor line number in other windows. Used after changing the
5943 * current buffer, and before applying autocommands.
5944 * When "do_curwin" is TRUE, also check current window.
5945 */
5946 void
5947check_lnums(do_curwin)
5948 int do_curwin;
5949{
5950 win_T *wp;
5951
5952#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005953 tabpage_T *tp;
5954
5955 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5957#else
5958 wp = curwin;
5959 if (do_curwin)
5960#endif
5961 {
5962 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5963 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5964 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5965 wp->w_topline = curbuf->b_ml.ml_line_count;
5966 }
5967}
5968#endif
5969
5970#if defined(FEAT_WINDOWS) || defined(PROTO)
5971
5972/*
5973 * A snapshot of the window sizes, to restore them after closing the help
5974 * window.
5975 * Only these fields are used:
5976 * fr_layout
5977 * fr_width
5978 * fr_height
5979 * fr_next
5980 * fr_child
5981 * fr_win (only valid for the old curwin, NULL otherwise)
5982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983
5984/*
5985 * Create a snapshot of the current frame sizes.
5986 */
5987 static void
5988make_snapshot()
5989{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005990 clear_snapshot(curtab);
5991 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992}
5993
5994 static void
5995make_snapshot_rec(fr, frp)
5996 frame_T *fr;
5997 frame_T **frp;
5998{
5999 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6000 if (*frp == NULL)
6001 return;
6002 (*frp)->fr_layout = fr->fr_layout;
6003# ifdef FEAT_VERTSPLIT
6004 (*frp)->fr_width = fr->fr_width;
6005# endif
6006 (*frp)->fr_height = fr->fr_height;
6007 if (fr->fr_next != NULL)
6008 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6009 if (fr->fr_child != NULL)
6010 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6011 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6012 (*frp)->fr_win = curwin;
6013}
6014
6015/*
6016 * Remove any existing snapshot.
6017 */
6018 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006019clear_snapshot(tp)
6020 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006022 clear_snapshot_rec(tp->tp_snapshot);
6023 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024}
6025
6026 static void
6027clear_snapshot_rec(fr)
6028 frame_T *fr;
6029{
6030 if (fr != NULL)
6031 {
6032 clear_snapshot_rec(fr->fr_next);
6033 clear_snapshot_rec(fr->fr_child);
6034 vim_free(fr);
6035 }
6036}
6037
6038/*
6039 * Restore a previously created snapshot, if there is any.
6040 * This is only done if the screen size didn't change and the window layout is
6041 * still the same.
6042 */
6043 static void
6044restore_snapshot(close_curwin)
6045 int close_curwin; /* closing current window */
6046{
6047 win_T *wp;
6048
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006049 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006051 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006053 && curtab->tp_snapshot->fr_height == topframe->fr_height
6054 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006056 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 win_comp_pos();
6058 if (wp != NULL && close_curwin)
6059 win_goto(wp);
6060 redraw_all_later(CLEAR);
6061 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006062 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063}
6064
6065/*
6066 * Check if frames "sn" and "fr" have the same layout, same following frames
6067 * and same children.
6068 */
6069 static int
6070check_snapshot_rec(sn, fr)
6071 frame_T *sn;
6072 frame_T *fr;
6073{
6074 if (sn->fr_layout != fr->fr_layout
6075 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6076 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6077 || (sn->fr_next != NULL
6078 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6079 || (sn->fr_child != NULL
6080 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6081 return FAIL;
6082 return OK;
6083}
6084
6085/*
6086 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6087 * following frames and children.
6088 * Returns a pointer to the old current window, or NULL.
6089 */
6090 static win_T *
6091restore_snapshot_rec(sn, fr)
6092 frame_T *sn;
6093 frame_T *fr;
6094{
6095 win_T *wp = NULL;
6096 win_T *wp2;
6097
6098 fr->fr_height = sn->fr_height;
6099# ifdef FEAT_VERTSPLIT
6100 fr->fr_width = sn->fr_width;
6101# endif
6102 if (fr->fr_layout == FR_LEAF)
6103 {
6104 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6105# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006106 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107# endif
6108 wp = sn->fr_win;
6109 }
6110 if (sn->fr_next != NULL)
6111 {
6112 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6113 if (wp2 != NULL)
6114 wp = wp2;
6115 }
6116 if (sn->fr_child != NULL)
6117 {
6118 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6119 if (wp2 != NULL)
6120 wp = wp2;
6121 }
6122 return wp;
6123}
6124
6125#endif
6126
6127#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6128/*
6129 * Return TRUE if there is any vertically split window.
6130 */
6131 int
6132win_hasvertsplit()
6133{
6134 frame_T *fr;
6135
6136 if (topframe->fr_layout == FR_ROW)
6137 return TRUE;
6138
6139 if (topframe->fr_layout == FR_COL)
6140 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6141 if (fr->fr_layout == FR_ROW)
6142 return TRUE;
6143
6144 return FALSE;
6145}
6146#endif