blob: 10bb8734340d3315306d2e1ce50c2d18b985909d [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
39static void frame_add_statusline __ARGS((frame_T *frp));
40static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst));
41static void frame_add_vsep __ARGS((frame_T *frp));
42static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
43static void frame_fix_width __ARGS((win_T *wp));
44#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000045#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000046static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000047#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000048static tabpage_T *alloc_tabpage __ARGS((void));
49static void free_tabpage __ARGS((tabpage_T *tp));
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
87static long p_ch_used = 1L; /* value of 'cmdheight' when frame
88 size was set */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000089# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000090#else
91# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000092#endif
93
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#if defined(FEAT_WINDOWS) || defined(PROTO)
95/*
96 * all CTRL-W window commands are handled here, called from normal_cmd().
97 */
98 void
99do_window(nchar, Prenum, xchar)
100 int nchar;
101 long Prenum;
102 int xchar; /* extra char from ":wincmd gx" or NUL */
103{
104 long Prenum1;
105 win_T *wp;
106#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
107 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000108 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#endif
110#ifdef FEAT_FIND_ID
111 int type = FIND_DEFINE;
112 int len;
113#endif
114 char_u cbuf[40];
115
116 if (Prenum == 0)
117 Prenum1 = 1;
118 else
119 Prenum1 = Prenum;
120
121#ifdef FEAT_CMDWIN
122# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
123#else
124# define CHECK_CMDWIN
125#endif
126
127 switch (nchar)
128 {
129/* split current window in two parts, horizontally */
130 case 'S':
131 case Ctrl_S:
132 case 's':
133 CHECK_CMDWIN
134#ifdef FEAT_VISUAL
135 reset_VIsual_and_resel(); /* stop Visual mode */
136#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000137#ifdef FEAT_QUICKFIX
138 /* When splitting the quickfix window open a new buffer in it,
139 * don't replicate the quickfix buffer. */
140 if (bt_quickfix(curbuf))
141 goto newwindow;
142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143#ifdef FEAT_GUI
144 need_mouse_correct = TRUE;
145#endif
146 win_split((int)Prenum, 0);
147 break;
148
149#ifdef FEAT_VERTSPLIT
150/* split current window in two parts, vertically */
151 case Ctrl_V:
152 case 'v':
153 CHECK_CMDWIN
154#ifdef FEAT_VISUAL
155 reset_VIsual_and_resel(); /* stop Visual mode */
156#endif
157#ifdef FEAT_GUI
158 need_mouse_correct = TRUE;
159#endif
160 win_split((int)Prenum, WSP_VERT);
161 break;
162#endif
163
164/* split current window and edit alternate file */
165 case Ctrl_HAT:
166 case '^':
167 CHECK_CMDWIN
168#ifdef FEAT_VISUAL
169 reset_VIsual_and_resel(); /* stop Visual mode */
170#endif
171 STRCPY(cbuf, "split #");
172 if (Prenum)
173 sprintf((char *)cbuf + 7, "%ld", Prenum);
174 do_cmdline_cmd(cbuf);
175 break;
176
177/* open new window */
178 case Ctrl_N:
179 case 'n':
180 CHECK_CMDWIN
181#ifdef FEAT_VISUAL
182 reset_VIsual_and_resel(); /* stop Visual mode */
183#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000184#ifdef FEAT_QUICKFIX
185newwindow:
186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 if (Prenum)
188 sprintf((char *)cbuf, "%ld", Prenum); /* window height */
189 else
190 cbuf[0] = NUL;
191 STRCAT(cbuf, "new");
192 do_cmdline_cmd(cbuf);
193 break;
194
195/* quit current window */
196 case Ctrl_Q:
197 case 'q':
198#ifdef FEAT_VISUAL
199 reset_VIsual_and_resel(); /* stop Visual mode */
200#endif
201 do_cmdline_cmd((char_u *)"quit");
202 break;
203
204/* close current window */
205 case Ctrl_C:
206 case 'c':
207#ifdef FEAT_VISUAL
208 reset_VIsual_and_resel(); /* stop Visual mode */
209#endif
210 do_cmdline_cmd((char_u *)"close");
211 break;
212
213#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
214/* close preview window */
215 case Ctrl_Z:
216 case 'z':
217 CHECK_CMDWIN
218#ifdef FEAT_VISUAL
219 reset_VIsual_and_resel(); /* stop Visual mode */
220#endif
221 do_cmdline_cmd((char_u *)"pclose");
222 break;
223
224/* cursor to preview window */
225 case 'P':
226 for (wp = firstwin; wp != NULL; wp = wp->w_next)
227 if (wp->w_p_pvw)
228 break;
229 if (wp == NULL)
230 EMSG(_("E441: There is no preview window"));
231 else
232 win_goto(wp);
233 break;
234#endif
235
236/* close all but current window */
237 case Ctrl_O:
238 case 'o':
239 CHECK_CMDWIN
240#ifdef FEAT_VISUAL
241 reset_VIsual_and_resel(); /* stop Visual mode */
242#endif
243 do_cmdline_cmd((char_u *)"only");
244 break;
245
246/* cursor to next window with wrap around */
247 case Ctrl_W:
248 case 'w':
249/* cursor to previous window with wrap around */
250 case 'W':
251 CHECK_CMDWIN
252 if (lastwin == firstwin && Prenum != 1) /* just one window */
253 beep_flush();
254 else
255 {
256 if (Prenum) /* go to specified window */
257 {
258 for (wp = firstwin; --Prenum > 0; )
259 {
260 if (wp->w_next == NULL)
261 break;
262 else
263 wp = wp->w_next;
264 }
265 }
266 else
267 {
268 if (nchar == 'W') /* go to previous window */
269 {
270 wp = curwin->w_prev;
271 if (wp == NULL)
272 wp = lastwin; /* wrap around */
273 }
274 else /* go to next window */
275 {
276 wp = curwin->w_next;
277 if (wp == NULL)
278 wp = firstwin; /* wrap around */
279 }
280 }
281 win_goto(wp);
282 }
283 break;
284
285/* cursor to window below */
286 case 'j':
287 case K_DOWN:
288 case Ctrl_J:
289 CHECK_CMDWIN
290#ifdef FEAT_VERTSPLIT
291 win_goto_ver(FALSE, Prenum1);
292#else
293 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
294 wp = wp->w_next)
295 ;
296 win_goto(wp);
297#endif
298 break;
299
300/* cursor to window above */
301 case 'k':
302 case K_UP:
303 case Ctrl_K:
304 CHECK_CMDWIN
305#ifdef FEAT_VERTSPLIT
306 win_goto_ver(TRUE, Prenum1);
307#else
308 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
309 wp = wp->w_prev)
310 ;
311 win_goto(wp);
312#endif
313 break;
314
315#ifdef FEAT_VERTSPLIT
316/* cursor to left window */
317 case 'h':
318 case K_LEFT:
319 case Ctrl_H:
320 case K_BS:
321 CHECK_CMDWIN
322 win_goto_hor(TRUE, Prenum1);
323 break;
324
325/* cursor to right window */
326 case 'l':
327 case K_RIGHT:
328 case Ctrl_L:
329 CHECK_CMDWIN
330 win_goto_hor(FALSE, Prenum1);
331 break;
332#endif
333
334/* cursor to top-left window */
335 case 't':
336 case Ctrl_T:
337 win_goto(firstwin);
338 break;
339
340/* cursor to bottom-right window */
341 case 'b':
342 case Ctrl_B:
343 win_goto(lastwin);
344 break;
345
346/* cursor to last accessed (previous) window */
347 case 'p':
348 case Ctrl_P:
349 if (prevwin == NULL)
350 beep_flush();
351 else
352 win_goto(prevwin);
353 break;
354
355/* exchange current and next window */
356 case 'x':
357 case Ctrl_X:
358 CHECK_CMDWIN
359 win_exchange(Prenum);
360 break;
361
362/* rotate windows downwards */
363 case Ctrl_R:
364 case 'r':
365 CHECK_CMDWIN
366#ifdef FEAT_VISUAL
367 reset_VIsual_and_resel(); /* stop Visual mode */
368#endif
369 win_rotate(FALSE, (int)Prenum1); /* downwards */
370 break;
371
372/* rotate windows upwards */
373 case 'R':
374 CHECK_CMDWIN
375#ifdef FEAT_VISUAL
376 reset_VIsual_and_resel(); /* stop Visual mode */
377#endif
378 win_rotate(TRUE, (int)Prenum1); /* upwards */
379 break;
380
381/* move window to the very top/bottom/left/right */
382 case 'K':
383 case 'J':
384#ifdef FEAT_VERTSPLIT
385 case 'H':
386 case 'L':
387#endif
388 CHECK_CMDWIN
389 win_totop((int)Prenum,
390 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
391 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
392 break;
393
394/* make all windows the same height */
395 case '=':
396#ifdef FEAT_GUI
397 need_mouse_correct = TRUE;
398#endif
399 win_equal(NULL, FALSE, 'b');
400 break;
401
402/* increase current window height */
403 case '+':
404#ifdef FEAT_GUI
405 need_mouse_correct = TRUE;
406#endif
407 win_setheight(curwin->w_height + (int)Prenum1);
408 break;
409
410/* decrease current window height */
411 case '-':
412#ifdef FEAT_GUI
413 need_mouse_correct = TRUE;
414#endif
415 win_setheight(curwin->w_height - (int)Prenum1);
416 break;
417
418/* set current window height */
419 case Ctrl__:
420 case '_':
421#ifdef FEAT_GUI
422 need_mouse_correct = TRUE;
423#endif
424 win_setheight(Prenum ? (int)Prenum : 9999);
425 break;
426
427#ifdef FEAT_VERTSPLIT
428/* increase current window width */
429 case '>':
430#ifdef FEAT_GUI
431 need_mouse_correct = TRUE;
432#endif
433 win_setwidth(curwin->w_width + (int)Prenum1);
434 break;
435
436/* decrease current window width */
437 case '<':
438#ifdef FEAT_GUI
439 need_mouse_correct = TRUE;
440#endif
441 win_setwidth(curwin->w_width - (int)Prenum1);
442 break;
443
444/* set current window width */
445 case '|':
446#ifdef FEAT_GUI
447 need_mouse_correct = TRUE;
448#endif
449 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
450 break;
451#endif
452
453/* jump to tag and split window if tag exists (in preview window) */
454#if defined(FEAT_QUICKFIX)
455 case '}':
456 CHECK_CMDWIN
457 if (Prenum)
458 g_do_tagpreview = Prenum;
459 else
460 g_do_tagpreview = p_pvh;
461 /*FALLTHROUGH*/
462#endif
463 case ']':
464 case Ctrl_RSB:
465 CHECK_CMDWIN
466#ifdef FEAT_VISUAL
467 reset_VIsual_and_resel(); /* stop Visual mode */
468#endif
469 if (Prenum)
470 postponed_split = Prenum;
471 else
472 postponed_split = -1;
473
474 /* Execute the command right here, required when
475 * "wincmd ]" was used in a function. */
476 do_nv_ident(Ctrl_RSB, NUL);
477 break;
478
479#ifdef FEAT_SEARCHPATH
480/* edit file name under cursor in a new window */
481 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000482 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 case Ctrl_F:
484 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000485
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000486 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 if (ptr != NULL)
488 {
489#ifdef FEAT_GUI
490 need_mouse_correct = TRUE;
491#endif
492 setpcmark();
493 if (win_split(0, 0) == OK)
494 {
495# ifdef FEAT_SCROLLBIND
496 curwin->w_p_scb = FALSE;
497# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000498 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE);
499 if (nchar == 'F' && lnum >= 0)
500 {
501 curwin->w_cursor.lnum = lnum;
502 check_cursor_lnum();
503 beginline(BL_SOL | BL_FIX);
504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 }
506 vim_free(ptr);
507 }
508 break;
509#endif
510
511#ifdef FEAT_FIND_ID
512/* Go to the first occurence of the identifier under cursor along path in a
513 * new window -- webb
514 */
515 case 'i': /* Go to any match */
516 case Ctrl_I:
517 type = FIND_ANY;
518 /* FALLTHROUGH */
519 case 'd': /* Go to definition, using 'define' */
520 case Ctrl_D:
521 CHECK_CMDWIN
522 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
523 break;
524 find_pattern_in_path(ptr, 0, len, TRUE,
525 Prenum == 0 ? TRUE : FALSE, type,
526 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
527 curwin->w_set_curswant = TRUE;
528 break;
529#endif
530
Bram Moolenaar05159a02005-02-26 23:04:13 +0000531 case K_KENTER:
532 case CAR:
533#if defined(FEAT_QUICKFIX)
534 /*
535 * In a quickfix window a <CR> jumps to the error under the
536 * cursor in a new window.
537 */
538 if (bt_quickfix(curbuf))
539 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000540 sprintf((char *)cbuf, "split +%ld%s",
541 (long)curwin->w_cursor.lnum,
542 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000543 do_cmdline_cmd(cbuf);
544 }
545#endif
546 break;
547
548
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549/* CTRL-W g extended commands */
550 case 'g':
551 case Ctrl_G:
552 CHECK_CMDWIN
553#ifdef USE_ON_FLY_SCROLL
554 dont_scroll = TRUE; /* disallow scrolling here */
555#endif
556 ++no_mapping;
557 ++allow_keys; /* no mapping for xchar, but allow key codes */
558 if (xchar == NUL)
559 xchar = safe_vgetc();
560#ifdef FEAT_LANGMAP
561 LANGMAP_ADJUST(xchar, TRUE);
562#endif
563 --no_mapping;
564 --allow_keys;
565#ifdef FEAT_CMDL_INFO
566 (void)add_to_showcmd(xchar);
567#endif
568 switch (xchar)
569 {
570#if defined(FEAT_QUICKFIX)
571 case '}':
572 xchar = Ctrl_RSB;
573 if (Prenum)
574 g_do_tagpreview = Prenum;
575 else
576 g_do_tagpreview = p_pvh;
577 /*FALLTHROUGH*/
578#endif
579 case ']':
580 case Ctrl_RSB:
581#ifdef FEAT_VISUAL
582 reset_VIsual_and_resel(); /* stop Visual mode */
583#endif
584 if (Prenum)
585 postponed_split = Prenum;
586 else
587 postponed_split = -1;
588
589 /* Execute the command right here, required when
590 * "wincmd g}" was used in a function. */
591 do_nv_ident('g', xchar);
592 break;
593
594 default:
595 beep_flush();
596 break;
597 }
598 break;
599
600 default: beep_flush();
601 break;
602 }
603}
604
605/*
606 * split the current window, implements CTRL-W s and :split
607 *
608 * "size" is the height or width for the new window, 0 to use half of current
609 * height or width.
610 *
611 * "flags":
612 * WSP_ROOM: require enough room for new window
613 * WSP_VERT: vertical split.
614 * WSP_TOP: open window at the top-left of the shell (help window).
615 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
616 * WSP_HELP: creating the help window, keep layout snapshot
617 *
618 * return FAIL for failure, OK otherwise
619 */
620 int
621win_split(size, flags)
622 int size;
623 int flags;
624{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000625 /* When the ":tab" modifier was used open a new tab page instead. */
626 if (may_open_tabpage() == OK)
627 return OK;
628
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 /* Add flags from ":vertical", ":topleft" and ":botright". */
630 flags |= cmdmod.split;
631 if ((flags & WSP_TOP) && (flags & WSP_BOT))
632 {
633 EMSG(_("E442: Can't split topleft and botright at the same time"));
634 return FAIL;
635 }
636
637 /* When creating the help window make a snapshot of the window layout.
638 * Otherwise clear the snapshot, it's now invalid. */
639 if (flags & WSP_HELP)
640 make_snapshot();
641 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000642 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
644 return win_split_ins(size, flags, NULL, 0);
645}
646
647/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000648 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 * When "newwin" is not NULL: insert this window at the far
650 * top/left/right/bottom.
651 * return FAIL for failure, OK otherwise
652 */
653 static int
654win_split_ins(size, flags, newwin, dir)
655 int size;
656 int flags;
657 win_T *newwin;
658 int dir;
659{
660 win_T *wp = newwin;
661 win_T *oldwin;
662 int new_size = size;
663 int i;
664 int need_status = 0;
665 int do_equal = FALSE;
666 int needed;
667 int available;
668 int oldwin_height = 0;
669 int layout;
670 frame_T *frp, *curfrp;
671 int before;
672
673 if (flags & WSP_TOP)
674 oldwin = firstwin;
675 else if (flags & WSP_BOT)
676 oldwin = lastwin;
677 else
678 oldwin = curwin;
679
680 /* add a status line when p_ls == 1 and splitting the first window */
681 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
682 {
683 if (oldwin->w_height <= p_wmh && newwin == NULL)
684 {
685 EMSG(_(e_noroom));
686 return FAIL;
687 }
688 need_status = STATUS_HEIGHT;
689 }
690
691#ifdef FEAT_VERTSPLIT
692 if (flags & WSP_VERT)
693 {
694 layout = FR_ROW;
695 do_equal = (p_ea && new_size == 0 && *p_ead != 'v');
696
697 /*
698 * Check if we are able to split the current window and compute its
699 * width.
700 */
701 needed = p_wmw + 1;
702 if (flags & WSP_ROOM)
703 needed += p_wiw - p_wmw;
704 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
705 {
706 available = topframe->fr_width;
707 needed += frame_minwidth(topframe, NULL);
708 }
709 else
710 available = oldwin->w_width;
711 if (available < needed && newwin == NULL)
712 {
713 EMSG(_(e_noroom));
714 return FAIL;
715 }
716 if (new_size == 0)
717 new_size = oldwin->w_width / 2;
718 if (new_size > oldwin->w_width - p_wmw - 1)
719 new_size = oldwin->w_width - p_wmw - 1;
720 if (new_size < p_wmw)
721 new_size = p_wmw;
722
723 /* if it doesn't fit in the current window, need win_equal() */
724 if (oldwin->w_width - new_size - 1 < p_wmw)
725 do_equal = TRUE;
726 }
727 else
728#endif
729 {
730 layout = FR_COL;
731 do_equal = (p_ea && new_size == 0
732#ifdef FEAT_VERTSPLIT
733 && *p_ead != 'h'
734#endif
735 );
736
737 /*
738 * Check if we are able to split the current window and compute its
739 * height.
740 */
741 needed = p_wmh + STATUS_HEIGHT + need_status;
742 if (flags & WSP_ROOM)
743 needed += p_wh - p_wmh;
744 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
745 {
746 available = topframe->fr_height;
747 needed += frame_minheight(topframe, NULL);
748 }
749 else
750 {
751 available = oldwin->w_height;
752 needed += p_wmh;
753 }
754 if (available < needed && newwin == NULL)
755 {
756 EMSG(_(e_noroom));
757 return FAIL;
758 }
759 oldwin_height = oldwin->w_height;
760 if (need_status)
761 {
762 oldwin->w_status_height = STATUS_HEIGHT;
763 oldwin_height -= STATUS_HEIGHT;
764 }
765 if (new_size == 0)
766 new_size = oldwin_height / 2;
767
768 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
769 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
770 if (new_size < p_wmh)
771 new_size = p_wmh;
772
773 /* if it doesn't fit in the current window, need win_equal() */
774 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
775 do_equal = TRUE;
776
777 /* We don't like to take lines for the new window from a
778 * 'winfixheight' window. Take them from a window above or below
779 * instead, if possible. */
780 if (oldwin->w_p_wfh)
781 {
782 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
783 oldwin);
784 oldwin_height = oldwin->w_height;
785 if (need_status)
786 oldwin_height -= STATUS_HEIGHT;
787 }
788 }
789
790 /*
791 * allocate new window structure and link it in the window list
792 */
793 if ((flags & WSP_TOP) == 0
794 && ((flags & WSP_BOT)
795 || (flags & WSP_BELOW)
796 || (!(flags & WSP_ABOVE)
797 && (
798#ifdef FEAT_VERTSPLIT
799 (flags & WSP_VERT) ? p_spr :
800#endif
801 p_sb))))
802 {
803 /* new window below/right of current one */
804 if (newwin == NULL)
805 wp = win_alloc(oldwin);
806 else
807 win_append(oldwin, wp);
808 }
809 else
810 {
811 if (newwin == NULL)
812 wp = win_alloc(oldwin->w_prev);
813 else
814 win_append(oldwin->w_prev, wp);
815 }
816
817 if (newwin == NULL)
818 {
819 if (wp == NULL)
820 return FAIL;
821
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000822 /* make the contents of the new window the same as the current one */
823 win_init(wp, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
825
826 /*
827 * Reorganise the tree of frames to insert the new window.
828 */
829 if (flags & (WSP_TOP | WSP_BOT))
830 {
831#ifdef FEAT_VERTSPLIT
832 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
833 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
834#else
835 if (topframe->fr_layout == FR_COL)
836#endif
837 {
838 curfrp = topframe->fr_child;
839 if (flags & WSP_BOT)
840 while (curfrp->fr_next != NULL)
841 curfrp = curfrp->fr_next;
842 }
843 else
844 curfrp = topframe;
845 before = (flags & WSP_TOP);
846 }
847 else
848 {
849 curfrp = oldwin->w_frame;
850 if (flags & WSP_BELOW)
851 before = FALSE;
852 else if (flags & WSP_ABOVE)
853 before = TRUE;
854 else
855#ifdef FEAT_VERTSPLIT
856 if (flags & WSP_VERT)
857 before = !p_spr;
858 else
859#endif
860 before = !p_sb;
861 }
862 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
863 {
864 /* Need to create a new frame in the tree to make a branch. */
865 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
866 *frp = *curfrp;
867 curfrp->fr_layout = layout;
868 frp->fr_parent = curfrp;
869 frp->fr_next = NULL;
870 frp->fr_prev = NULL;
871 curfrp->fr_child = frp;
872 curfrp->fr_win = NULL;
873 curfrp = frp;
874 if (frp->fr_win != NULL)
875 oldwin->w_frame = frp;
876 else
877 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
878 frp->fr_parent = curfrp;
879 }
880
881 if (newwin == NULL)
882 {
883 /* Create a frame for the new window. */
884 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
885 frp->fr_layout = FR_LEAF;
886 frp->fr_win = wp;
887 wp->w_frame = frp;
888 }
889 else
890 frp = newwin->w_frame;
891 frp->fr_parent = curfrp->fr_parent;
892
893 /* Insert the new frame at the right place in the frame list. */
894 if (before)
895 frame_insert(curfrp, frp);
896 else
897 frame_append(curfrp, frp);
898
899#ifdef FEAT_VERTSPLIT
900 if (flags & WSP_VERT)
901 {
902 wp->w_p_scr = curwin->w_p_scr;
903 if (need_status)
904 {
905 --oldwin->w_height;
906 oldwin->w_status_height = need_status;
907 }
908 if (flags & (WSP_TOP | WSP_BOT))
909 {
910 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000911 wp->w_winrow = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 wp->w_height = curfrp->fr_height - (p_ls > 0);
913 wp->w_status_height = (p_ls > 0);
914 }
915 else
916 {
917 /* height and row of new window is same as current window */
918 wp->w_winrow = oldwin->w_winrow;
919 wp->w_height = oldwin->w_height;
920 wp->w_status_height = oldwin->w_status_height;
921 }
922 frp->fr_height = curfrp->fr_height;
923
924 /* "new_size" of the current window goes to the new window, use
925 * one column for the vertical separator */
926 wp->w_width = new_size;
927 if (before)
928 wp->w_vsep_width = 1;
929 else
930 {
931 wp->w_vsep_width = oldwin->w_vsep_width;
932 oldwin->w_vsep_width = 1;
933 }
934 if (flags & (WSP_TOP | WSP_BOT))
935 {
936 if (flags & WSP_BOT)
937 frame_add_vsep(curfrp);
938 /* Set width of neighbor frame */
939 frame_new_width(curfrp, curfrp->fr_width
940 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP);
941 }
942 else
943 oldwin->w_width -= new_size + 1;
944 if (before) /* new window left of current one */
945 {
946 wp->w_wincol = oldwin->w_wincol;
947 oldwin->w_wincol += new_size + 1;
948 }
949 else /* new window right of current one */
950 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
951 frame_fix_width(oldwin);
952 frame_fix_width(wp);
953 }
954 else
955#endif
956 {
957 /* width and column of new window is same as current window */
958#ifdef FEAT_VERTSPLIT
959 if (flags & (WSP_TOP | WSP_BOT))
960 {
961 wp->w_wincol = 0;
962 wp->w_width = Columns;
963 wp->w_vsep_width = 0;
964 }
965 else
966 {
967 wp->w_wincol = oldwin->w_wincol;
968 wp->w_width = oldwin->w_width;
969 wp->w_vsep_width = oldwin->w_vsep_width;
970 }
971 frp->fr_width = curfrp->fr_width;
972#endif
973
974 /* "new_size" of the current window goes to the new window, use
975 * one row for the status line */
976 win_new_height(wp, new_size);
977 if (flags & (WSP_TOP | WSP_BOT))
978 frame_new_height(curfrp, curfrp->fr_height
979 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
980 else
981 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
982 if (before) /* new window above current one */
983 {
984 wp->w_winrow = oldwin->w_winrow;
985 wp->w_status_height = STATUS_HEIGHT;
986 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
987 }
988 else /* new window below current one */
989 {
990 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
991 wp->w_status_height = oldwin->w_status_height;
992 oldwin->w_status_height = STATUS_HEIGHT;
993 }
994#ifdef FEAT_VERTSPLIT
995 if (flags & WSP_BOT)
996 frame_add_statusline(curfrp);
997#endif
998 frame_fix_height(wp);
999 frame_fix_height(oldwin);
1000 }
1001
1002 if (flags & (WSP_TOP | WSP_BOT))
1003 (void)win_comp_pos();
1004
1005 /*
1006 * Both windows need redrawing
1007 */
1008 redraw_win_later(wp, NOT_VALID);
1009 wp->w_redr_status = TRUE;
1010 redraw_win_later(oldwin, NOT_VALID);
1011 oldwin->w_redr_status = TRUE;
1012
1013 if (need_status)
1014 {
1015 msg_row = Rows - 1;
1016 msg_col = sc_col;
1017 msg_clr_eos_force(); /* Old command/ruler may still be there */
1018 comp_col();
1019 msg_row = Rows - 1;
1020 msg_col = 0; /* put position back at start of line */
1021 }
1022
1023 /*
1024 * make the new window the current window and redraw
1025 */
1026 if (do_equal || dir != 0)
1027 win_equal(wp, TRUE,
1028#ifdef FEAT_VERTSPLIT
1029 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1030 : dir == 'h' ? 'b' :
1031#endif
1032 'v');
1033
1034 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1035 * size was given. */
1036#ifdef FEAT_VERTSPLIT
1037 if (flags & WSP_VERT)
1038 {
1039 i = p_wiw;
1040 if (size != 0)
1041 p_wiw = size;
1042
1043# ifdef FEAT_GUI
1044 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1045 if (gui.in_use)
1046 gui_init_which_components(NULL);
1047# endif
1048 }
1049 else
1050#endif
1051 {
1052 i = p_wh;
1053 if (size != 0)
1054 p_wh = size;
1055 }
1056 win_enter(wp, FALSE);
1057#ifdef FEAT_VERTSPLIT
1058 if (flags & WSP_VERT)
1059 p_wiw = i;
1060 else
1061#endif
1062 p_wh = i;
1063
1064 return OK;
1065}
1066
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001067/*
1068 * Initialize window "newp" from window "oldp".
1069 * Used when splitting a window and when creating a new tab page.
1070 * The windows will both edit the same buffer.
1071 */
1072 static void
1073win_init(newp, oldp)
1074 win_T *newp;
1075 win_T *oldp;
1076{
1077 int i;
1078
1079 newp->w_buffer = oldp->w_buffer;
1080 oldp->w_buffer->b_nwindows++;
1081 newp->w_cursor = oldp->w_cursor;
1082 newp->w_valid = 0;
1083 newp->w_curswant = oldp->w_curswant;
1084 newp->w_set_curswant = oldp->w_set_curswant;
1085 newp->w_topline = oldp->w_topline;
1086#ifdef FEAT_DIFF
1087 newp->w_topfill = oldp->w_topfill;
1088#endif
1089 newp->w_leftcol = oldp->w_leftcol;
1090 newp->w_pcmark = oldp->w_pcmark;
1091 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1092 newp->w_alt_fnum = oldp->w_alt_fnum;
1093 newp->w_fraction = oldp->w_fraction;
1094 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1095#ifdef FEAT_JUMPLIST
1096 copy_jumplist(oldp, newp);
1097#endif
1098#ifdef FEAT_QUICKFIX
1099 copy_loclist(oldp, newp);
1100#endif
1101 if (oldp->w_localdir != NULL)
1102 newp->w_localdir = vim_strsave(oldp->w_localdir);
1103
1104 /* Use the same argument list. */
1105 newp->w_alist = oldp->w_alist;
1106 ++newp->w_alist->al_refcount;
1107 newp->w_arg_idx = oldp->w_arg_idx;
1108
1109 /*
1110 * copy tagstack and options from existing window
1111 */
1112 for (i = 0; i < oldp->w_tagstacklen; i++)
1113 {
1114 newp->w_tagstack[i] = oldp->w_tagstack[i];
1115 if (newp->w_tagstack[i].tagname != NULL)
1116 newp->w_tagstack[i].tagname =
1117 vim_strsave(newp->w_tagstack[i].tagname);
1118 }
1119 newp->w_tagstackidx = oldp->w_tagstackidx;
1120 newp->w_tagstacklen = oldp->w_tagstacklen;
1121 win_copy_options(oldp, newp);
1122# ifdef FEAT_FOLDING
1123 copyFoldingState(oldp, newp);
1124# endif
1125}
1126
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127#endif /* FEAT_WINDOWS */
1128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129#if defined(FEAT_WINDOWS) || defined(PROTO)
1130/*
1131 * Check if "win" is a pointer to an existing window.
1132 */
1133 int
1134win_valid(win)
1135 win_T *win;
1136{
1137 win_T *wp;
1138
1139 if (win == NULL)
1140 return FALSE;
1141 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1142 if (wp == win)
1143 return TRUE;
1144 return FALSE;
1145}
1146
1147/*
1148 * Return the number of windows.
1149 */
1150 int
1151win_count()
1152{
1153 win_T *wp;
1154 int count = 0;
1155
1156 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1157 ++count;
1158 return count;
1159}
1160
1161/*
1162 * Make "count" windows on the screen.
1163 * Return actual number of windows on the screen.
1164 * Must be called when there is just one window, filling the whole screen
1165 * (excluding the command line).
1166 */
1167/*ARGSUSED*/
1168 int
1169make_windows(count, vertical)
1170 int count;
1171 int vertical; /* split windows vertically if TRUE */
1172{
1173 int maxcount;
1174 int todo;
1175
1176#ifdef FEAT_VERTSPLIT
1177 if (vertical)
1178 {
1179 /* Each windows needs at least 'winminwidth' lines and a separator
1180 * column. */
1181 maxcount = (curwin->w_width + curwin->w_vsep_width
1182 - (p_wiw - p_wmw)) / (p_wmw + 1);
1183 }
1184 else
1185#endif
1186 {
1187 /* Each window needs at least 'winminheight' lines and a status line. */
1188 maxcount = (curwin->w_height + curwin->w_status_height
1189 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1190 }
1191
1192 if (maxcount < 2)
1193 maxcount = 2;
1194 if (count > maxcount)
1195 count = maxcount;
1196
1197 /*
1198 * add status line now, otherwise first window will be too big
1199 */
1200 if (count > 1)
1201 last_status(TRUE);
1202
1203#ifdef FEAT_AUTOCMD
1204 /*
1205 * Don't execute autocommands while creating the windows. Must do that
1206 * when putting the buffers in the windows.
1207 */
1208 ++autocmd_block;
1209#endif
1210
1211 /* todo is number of windows left to create */
1212 for (todo = count - 1; todo > 0; --todo)
1213#ifdef FEAT_VERTSPLIT
1214 if (vertical)
1215 {
1216 if (win_split(curwin->w_width - (curwin->w_width - todo)
1217 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1218 break;
1219 }
1220 else
1221#endif
1222 {
1223 if (win_split(curwin->w_height - (curwin->w_height - todo
1224 * STATUS_HEIGHT) / (todo + 1)
1225 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1226 break;
1227 }
1228
1229#ifdef FEAT_AUTOCMD
1230 --autocmd_block;
1231#endif
1232
1233 /* return actual number of windows */
1234 return (count - todo);
1235}
1236
1237/*
1238 * Exchange current and next window
1239 */
1240 static void
1241win_exchange(Prenum)
1242 long Prenum;
1243{
1244 frame_T *frp;
1245 frame_T *frp2;
1246 win_T *wp;
1247 win_T *wp2;
1248 int temp;
1249
1250 if (lastwin == firstwin) /* just one window */
1251 {
1252 beep_flush();
1253 return;
1254 }
1255
1256#ifdef FEAT_GUI
1257 need_mouse_correct = TRUE;
1258#endif
1259
1260 /*
1261 * find window to exchange with
1262 */
1263 if (Prenum)
1264 {
1265 frp = curwin->w_frame->fr_parent->fr_child;
1266 while (frp != NULL && --Prenum > 0)
1267 frp = frp->fr_next;
1268 }
1269 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1270 frp = curwin->w_frame->fr_next;
1271 else /* Swap last window in row/col with previous */
1272 frp = curwin->w_frame->fr_prev;
1273
1274 /* We can only exchange a window with another window, not with a frame
1275 * containing windows. */
1276 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1277 return;
1278 wp = frp->fr_win;
1279
1280/*
1281 * 1. remove curwin from the list. Remember after which window it was in wp2
1282 * 2. insert curwin before wp in the list
1283 * if wp != wp2
1284 * 3. remove wp from the list
1285 * 4. insert wp after wp2
1286 * 5. exchange the status line height and vsep width.
1287 */
1288 wp2 = curwin->w_prev;
1289 frp2 = curwin->w_frame->fr_prev;
1290 if (wp->w_prev != curwin)
1291 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001292 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 frame_remove(curwin->w_frame);
1294 win_append(wp->w_prev, curwin);
1295 frame_insert(frp, curwin->w_frame);
1296 }
1297 if (wp != wp2)
1298 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001299 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 frame_remove(wp->w_frame);
1301 win_append(wp2, wp);
1302 if (frp2 == NULL)
1303 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1304 else
1305 frame_append(frp2, wp->w_frame);
1306 }
1307 temp = curwin->w_status_height;
1308 curwin->w_status_height = wp->w_status_height;
1309 wp->w_status_height = temp;
1310#ifdef FEAT_VERTSPLIT
1311 temp = curwin->w_vsep_width;
1312 curwin->w_vsep_width = wp->w_vsep_width;
1313 wp->w_vsep_width = temp;
1314
1315 /* If the windows are not in the same frame, exchange the sizes to avoid
1316 * messing up the window layout. Otherwise fix the frame sizes. */
1317 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1318 {
1319 temp = curwin->w_height;
1320 curwin->w_height = wp->w_height;
1321 wp->w_height = temp;
1322 temp = curwin->w_width;
1323 curwin->w_width = wp->w_width;
1324 wp->w_width = temp;
1325 }
1326 else
1327 {
1328 frame_fix_height(curwin);
1329 frame_fix_height(wp);
1330 frame_fix_width(curwin);
1331 frame_fix_width(wp);
1332 }
1333#endif
1334
1335 (void)win_comp_pos(); /* recompute window positions */
1336
1337 win_enter(wp, TRUE);
1338 redraw_later(CLEAR);
1339}
1340
1341/*
1342 * rotate windows: if upwards TRUE the second window becomes the first one
1343 * if upwards FALSE the first window becomes the second one
1344 */
1345 static void
1346win_rotate(upwards, count)
1347 int upwards;
1348 int count;
1349{
1350 win_T *wp1;
1351 win_T *wp2;
1352 frame_T *frp;
1353 int n;
1354
1355 if (firstwin == lastwin) /* nothing to do */
1356 {
1357 beep_flush();
1358 return;
1359 }
1360
1361#ifdef FEAT_GUI
1362 need_mouse_correct = TRUE;
1363#endif
1364
1365#ifdef FEAT_VERTSPLIT
1366 /* Check if all frames in this row/col have one window. */
1367 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1368 frp = frp->fr_next)
1369 if (frp->fr_win == NULL)
1370 {
1371 EMSG(_("E443: Cannot rotate when another window is split"));
1372 return;
1373 }
1374#endif
1375
1376 while (count--)
1377 {
1378 if (upwards) /* first window becomes last window */
1379 {
1380 /* remove first window/frame from the list */
1381 frp = curwin->w_frame->fr_parent->fr_child;
1382 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001383 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 frame_remove(frp);
1385
1386 /* find last frame and append removed window/frame after it */
1387 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1388 ;
1389 win_append(frp->fr_win, wp1);
1390 frame_append(frp, wp1->w_frame);
1391
1392 wp2 = frp->fr_win; /* previously last window */
1393 }
1394 else /* last window becomes first window */
1395 {
1396 /* find last window/frame in the list and remove it */
1397 for (frp = curwin->w_frame; frp->fr_next != NULL;
1398 frp = frp->fr_next)
1399 ;
1400 wp1 = frp->fr_win;
1401 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001402 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 frame_remove(frp);
1404
1405 /* append the removed window/frame before the first in the list */
1406 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1407 frame_insert(frp->fr_parent->fr_child, frp);
1408 }
1409
1410 /* exchange status height and vsep width of old and new last window */
1411 n = wp2->w_status_height;
1412 wp2->w_status_height = wp1->w_status_height;
1413 wp1->w_status_height = n;
1414 frame_fix_height(wp1);
1415 frame_fix_height(wp2);
1416#ifdef FEAT_VERTSPLIT
1417 n = wp2->w_vsep_width;
1418 wp2->w_vsep_width = wp1->w_vsep_width;
1419 wp1->w_vsep_width = n;
1420 frame_fix_width(wp1);
1421 frame_fix_width(wp2);
1422#endif
1423
1424 /* recompute w_winrow and w_wincol for all windows */
1425 (void)win_comp_pos();
1426 }
1427
1428 redraw_later(CLEAR);
1429}
1430
1431/*
1432 * Move the current window to the very top/bottom/left/right of the screen.
1433 */
1434 static void
1435win_totop(size, flags)
1436 int size;
1437 int flags;
1438{
1439 int dir;
1440 int height = curwin->w_height;
1441
1442 if (lastwin == firstwin)
1443 {
1444 beep_flush();
1445 return;
1446 }
1447
1448 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001449 (void)winframe_remove(curwin, &dir, NULL);
1450 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 last_status(FALSE); /* may need to remove last status line */
1452 (void)win_comp_pos(); /* recompute window positions */
1453
1454 /* Split a window on the desired side and put the window there. */
1455 (void)win_split_ins(size, flags, curwin, dir);
1456 if (!(flags & WSP_VERT))
1457 {
1458 win_setheight(height);
1459 if (p_ea)
1460 win_equal(curwin, TRUE, 'v');
1461 }
1462
1463#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1464 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1465 * scrollbars. Have to update them anyway. */
1466 if (gui.in_use)
1467 {
1468 out_flush();
1469 gui_init_which_components(NULL);
1470 gui_update_scrollbars(TRUE);
1471 }
1472 need_mouse_correct = TRUE;
1473#endif
1474
1475}
1476
1477/*
1478 * Move window "win1" to below/right of "win2" and make "win1" the current
1479 * window. Only works within the same frame!
1480 */
1481 void
1482win_move_after(win1, win2)
1483 win_T *win1, *win2;
1484{
1485 int height;
1486
1487 /* check if the arguments are reasonable */
1488 if (win1 == win2)
1489 return;
1490
1491 /* check if there is something to do */
1492 if (win2->w_next != win1)
1493 {
1494 /* may need move the status line/vertical separator of the last window
1495 * */
1496 if (win1 == lastwin)
1497 {
1498 height = win1->w_prev->w_status_height;
1499 win1->w_prev->w_status_height = win1->w_status_height;
1500 win1->w_status_height = height;
1501#ifdef FEAT_VERTSPLIT
1502 win1->w_prev->w_vsep_width = 0;
1503 win1->w_vsep_width = 1;
1504#endif
1505 }
1506 else if (win2 == lastwin)
1507 {
1508 height = win1->w_status_height;
1509 win1->w_status_height = win2->w_status_height;
1510 win2->w_status_height = height;
1511#ifdef FEAT_VERTSPLIT
1512 win2->w_vsep_width = 1;
1513 win1->w_vsep_width = 0;
1514#endif
1515 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001516 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 frame_remove(win1->w_frame);
1518 win_append(win2, win1);
1519 frame_append(win2->w_frame, win1->w_frame);
1520
1521 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1522 redraw_later(NOT_VALID);
1523 }
1524 win_enter(win1, FALSE);
1525}
1526
1527/*
1528 * Make all windows the same height.
1529 * 'next_curwin' will soon be the current window, make sure it has enough
1530 * rows.
1531 */
1532 void
1533win_equal(next_curwin, current, dir)
1534 win_T *next_curwin; /* pointer to current window to be or NULL */
1535 int current; /* do only frame with current window */
1536 int dir; /* 'v' for vertically, 'h' for horizontally,
1537 'b' for both, 0 for using p_ead */
1538{
1539 if (dir == 0)
1540#ifdef FEAT_VERTSPLIT
1541 dir = *p_ead;
1542#else
1543 dir = 'b';
1544#endif
1545 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001546 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001547 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548}
1549
1550/*
1551 * Set a frame to a new position and height, spreading the available room
1552 * equally over contained frames.
1553 * The window "next_curwin" (if not NULL) should at least get the size from
1554 * 'winheight' and 'winwidth' if possible.
1555 */
1556 static void
1557win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1558 win_T *next_curwin; /* pointer to current window to be or NULL */
1559 int current; /* do only frame with current window */
1560 frame_T *topfr; /* frame to set size off */
1561 int dir; /* 'v', 'h' or 'b', see win_equal() */
1562 int col; /* horizontal position for frame */
1563 int row; /* vertical position for frame */
1564 int width; /* new width of frame */
1565 int height; /* new height of frame */
1566{
1567 int n, m;
1568 int extra_sep = 0;
1569 int wincount, totwincount = 0;
1570 frame_T *fr;
1571 int next_curwin_size = 0;
1572 int room = 0;
1573 int new_size;
1574 int has_next_curwin = 0;
1575 int hnc;
1576
1577 if (topfr->fr_layout == FR_LEAF)
1578 {
1579 /* Set the width/height of this frame.
1580 * Redraw when size or position changes */
1581 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1582#ifdef FEAT_VERTSPLIT
1583 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1584#endif
1585 )
1586 {
1587 topfr->fr_win->w_winrow = row;
1588 frame_new_height(topfr, height, FALSE, FALSE);
1589#ifdef FEAT_VERTSPLIT
1590 topfr->fr_win->w_wincol = col;
1591 frame_new_width(topfr, width, FALSE);
1592#endif
1593 redraw_all_later(CLEAR);
1594 }
1595 }
1596#ifdef FEAT_VERTSPLIT
1597 else if (topfr->fr_layout == FR_ROW)
1598 {
1599 topfr->fr_width = width;
1600 topfr->fr_height = height;
1601
1602 if (dir != 'v') /* equalize frame widths */
1603 {
1604 /* Compute the maximum number of windows horizontally in this
1605 * frame. */
1606 n = frame_minwidth(topfr, NOWIN);
1607 /* add one for the rightmost window, it doesn't have a separator */
1608 if (col + width == Columns)
1609 extra_sep = 1;
1610 else
1611 extra_sep = 0;
1612 totwincount = (n + extra_sep) / (p_wmw + 1);
1613
1614 /* Compute room available for windows other than "next_curwin" */
1615 m = frame_minwidth(topfr, next_curwin);
1616 room = width - m;
1617 if (room < 0)
1618 {
1619 next_curwin_size = p_wiw + room;
1620 room = 0;
1621 }
1622 else if (n == m) /* doesn't contain curwin */
1623 next_curwin_size = 0;
1624 else
1625 {
1626 next_curwin_size = (room + p_wiw + (totwincount - 1) * p_wmw
1627 + (totwincount - 1)) / totwincount;
1628 if (next_curwin_size > p_wiw)
1629 room -= next_curwin_size - p_wiw;
1630 else
1631 next_curwin_size = p_wiw;
1632 }
1633 if (n != m)
1634 --totwincount; /* don't count curwin */
1635 }
1636
1637 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1638 {
1639 n = m = 0;
1640 wincount = 1;
1641 if (fr->fr_next == NULL)
1642 /* last frame gets all that remains (avoid roundoff error) */
1643 new_size = width;
1644 else if (dir == 'v')
1645 new_size = fr->fr_width;
1646 else
1647 {
1648 /* Compute the maximum number of windows horiz. in "fr". */
1649 n = frame_minwidth(fr, NOWIN);
1650 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1651 / (p_wmw + 1);
1652 m = frame_minwidth(fr, next_curwin);
1653 if (n != m) /* don't count next_curwin */
1654 --wincount;
1655 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1656 / totwincount;
1657 if (n != m) /* add next_curwin size */
1658 {
1659 next_curwin_size -= p_wiw - (m - n);
1660 new_size += next_curwin_size;
1661 }
1662 }
1663
1664 /* Skip frame that is full height when splitting or closing a
1665 * window, unless equalizing all frames. */
1666 if (!current || dir != 'v' || topfr->fr_parent != NULL
1667 || (new_size != fr->fr_width)
1668 || frame_has_win(fr, next_curwin))
1669 win_equal_rec(next_curwin, current, fr, dir, col, row,
1670 new_size + n, height);
1671 col += new_size + n;
1672 width -= new_size + n;
1673 if (n != m) /* contains curwin */
1674 room -= new_size - next_curwin_size;
1675 else
1676 room -= new_size;
1677 totwincount -= wincount;
1678 }
1679 }
1680#endif
1681 else /* topfr->fr_layout == FR_COL */
1682 {
1683#ifdef FEAT_VERTSPLIT
1684 topfr->fr_width = width;
1685#endif
1686 topfr->fr_height = height;
1687
1688 if (dir != 'h') /* equalize frame heights */
1689 {
1690 /* Compute maximum number of windows vertically in this frame. */
1691 n = frame_minheight(topfr, NOWIN);
1692 /* add one for the bottom window if it doesn't have a statusline */
1693 if (row + height == cmdline_row && p_ls == 0)
1694 extra_sep = 1;
1695 else
1696 extra_sep = 0;
1697 totwincount = (n + extra_sep) / (p_wmh + 1);
1698 has_next_curwin = frame_has_win(topfr, next_curwin);
1699
1700 /*
1701 * Compute height for "next_curwin" window and room available for
1702 * other windows.
1703 * "m" is the minimal height when counting p_wh for "next_curwin".
1704 */
1705 m = frame_minheight(topfr, next_curwin);
1706 room = height - m;
1707 if (room < 0)
1708 {
1709 /* The room is less then 'winheight', use all space for the
1710 * current window. */
1711 next_curwin_size = p_wh + room;
1712 room = 0;
1713 }
1714 else
1715 {
1716 next_curwin_size = -1;
1717 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1718 {
1719 /* If 'winfixheight' set keep the window height if
1720 * possible.
1721 * Watch out for this window being the next_curwin. */
1722 if (frame_fixed_height(fr))
1723 {
1724 n = frame_minheight(fr, NOWIN);
1725 new_size = fr->fr_height;
1726 if (frame_has_win(fr, next_curwin))
1727 {
1728 room += p_wh - p_wmh;
1729 next_curwin_size = 0;
1730 if (new_size < p_wh)
1731 new_size = p_wh;
1732 }
1733 else
1734 /* These windows don't use up room. */
1735 totwincount -= (n + (fr->fr_next == NULL
1736 ? extra_sep : 0)) / (p_wmh + 1);
1737 room -= new_size - n;
1738 if (room < 0)
1739 {
1740 new_size += room;
1741 room = 0;
1742 }
1743 fr->fr_newheight = new_size;
1744 }
1745 }
1746 if (next_curwin_size == -1)
1747 {
1748 if (!has_next_curwin)
1749 next_curwin_size = 0;
1750 else if (totwincount > 1
1751 && (room + (totwincount - 2))
1752 / (totwincount - 1) > p_wh)
1753 {
1754 next_curwin_size = (room + p_wh + totwincount * p_wmh
1755 + (totwincount - 1)) / totwincount;
1756 room -= next_curwin_size - p_wh;
1757 }
1758 else
1759 next_curwin_size = p_wh;
1760 }
1761 }
1762
1763 if (has_next_curwin)
1764 --totwincount; /* don't count curwin */
1765 }
1766
1767 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1768 {
1769 n = m = 0;
1770 wincount = 1;
1771 if (fr->fr_next == NULL)
1772 /* last frame gets all that remains (avoid roundoff error) */
1773 new_size = height;
1774 else if (dir == 'h')
1775 new_size = fr->fr_height;
1776 else if (frame_fixed_height(fr))
1777 {
1778 new_size = fr->fr_newheight;
1779 wincount = 0; /* doesn't count as a sizeable window */
1780 }
1781 else
1782 {
1783 /* Compute the maximum number of windows vert. in "fr". */
1784 n = frame_minheight(fr, NOWIN);
1785 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1786 / (p_wmh + 1);
1787 m = frame_minheight(fr, next_curwin);
1788 if (has_next_curwin)
1789 hnc = frame_has_win(fr, next_curwin);
1790 else
1791 hnc = FALSE;
1792 if (hnc) /* don't count next_curwin */
1793 --wincount;
1794 if (totwincount == 0)
1795 new_size = room;
1796 else
1797 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1798 / totwincount;
1799 if (hnc) /* add next_curwin size */
1800 {
1801 next_curwin_size -= p_wh - (m - n);
1802 new_size += next_curwin_size;
1803 room -= new_size - next_curwin_size;
1804 }
1805 else
1806 room -= new_size;
1807 new_size += n;
1808 }
1809 /* Skip frame that is full width when splitting or closing a
1810 * window, unless equalizing all frames. */
1811 if (!current || dir != 'h' || topfr->fr_parent != NULL
1812 || (new_size != fr->fr_height)
1813 || frame_has_win(fr, next_curwin))
1814 win_equal_rec(next_curwin, current, fr, dir, col, row,
1815 width, new_size);
1816 row += new_size;
1817 height -= new_size;
1818 totwincount -= wincount;
1819 }
1820 }
1821}
1822
1823/*
1824 * close all windows for buffer 'buf'
1825 */
1826 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00001827close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001829 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830{
Bram Moolenaarf740b292006-02-16 22:11:02 +00001831 win_T *wp;
1832 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001833 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
1835 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001836
1837 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001839 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001841 win_close(wp, FALSE);
1842
1843 /* Start all over, autocommands may change the window layout. */
1844 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 }
1846 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00001847 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001849
1850 /* Also check windows in other tab pages. */
1851 for (tp = first_tabpage; tp != NULL; tp = nexttp)
1852 {
1853 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001854 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001855 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
1856 if (wp->w_buffer == buf)
1857 {
1858 win_close_othertab(wp, FALSE, tp);
1859
1860 /* Start all over, the tab page may be closed and
1861 * autocommands may change the window layout. */
1862 nexttp = first_tabpage;
1863 break;
1864 }
1865 }
1866
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001868
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001869 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00001870 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871}
1872
1873/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001874 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001875 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001876 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001877 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001878last_window()
1879{
1880 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
1881}
1882
1883/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00001884 * Close window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 * If "free_buf" is TRUE related buffer may be unloaded.
1886 *
1887 * called by :quit, :close, :xit, :wq and findtag()
1888 */
1889 void
1890win_close(win, free_buf)
1891 win_T *win;
1892 int free_buf;
1893{
1894 win_T *wp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001895 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896#ifdef FEAT_AUTOCMD
1897 int other_buffer = FALSE;
1898#endif
1899 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 int dir;
1901 int help_window = FALSE;
1902
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001903 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 {
1905 EMSG(_("E444: Cannot close last window"));
1906 return;
1907 }
1908
1909 /* When closing the help window, try restoring a snapshot after closing
1910 * the window. Otherwise clear the snapshot, it's now invalid. */
1911 if (win->w_buffer->b_help)
1912 help_window = TRUE;
1913 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001914 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
1916#ifdef FEAT_AUTOCMD
1917 if (win == curwin)
1918 {
1919 /*
1920 * Guess which window is going to be the new current window.
1921 * This may change because of the autocommands (sigh).
1922 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001923 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924
1925 /*
1926 * Be careful: If autocommands delete the window, return now.
1927 */
1928 if (wp->w_buffer != curbuf)
1929 {
1930 other_buffer = TRUE;
1931 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001932 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 return;
1934 }
1935 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001936 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 return;
1938# ifdef FEAT_EVAL
1939 /* autocmds may abort script processing */
1940 if (aborting())
1941 return;
1942# endif
1943 }
1944#endif
1945
1946 /*
1947 * Close the link to the buffer.
1948 */
1949 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
1950 /* Autocommands may have closed the window already, or closed the only
1951 * other window. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001952 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 return;
1954
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001955 /* Free the memory used for the window. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001956 wp = win_free_mem(win, &dir, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001958 /* When closing the last window in a tab page go to another tab page. */
1959 if (wp == NULL)
1960 {
1961 tabpage_T *ptp = NULL;
1962 tabpage_T *tp;
1963 tabpage_T *atp = alt_tabpage();
1964
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001965 for (tp = first_tabpage; tp != curtab; tp = tp->tp_next)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001966 ptp = tp;
1967 if (tp == NULL)
1968 {
1969 EMSG2(_(e_intern2), "win_close()");
1970 return;
1971 }
1972 if (ptp == NULL)
1973 first_tabpage = tp->tp_next;
1974 else
1975 ptp->tp_next = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001976 free_tabpage(tp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001977
1978 /* We don't do the window resizing stuff, let enter_tabpage() take
1979 * care of entering a window in another tab page. */
1980 enter_tabpage(atp, old_curbuf);
1981 return;
1982 }
1983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 /* Make sure curwin isn't invalid. It can cause severe trouble when
1985 * printing an error message. For win_equal() curbuf needs to be valid
1986 * too. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001987 else if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
1989 curwin = wp;
1990#ifdef FEAT_QUICKFIX
1991 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
1992 {
1993 /*
1994 * The cursor goes to the preview or the quickfix window, try
1995 * finding another window to go to.
1996 */
1997 for (;;)
1998 {
1999 if (wp->w_next == NULL)
2000 wp = firstwin;
2001 else
2002 wp = wp->w_next;
2003 if (wp == curwin)
2004 break;
2005 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2006 {
2007 curwin = wp;
2008 break;
2009 }
2010 }
2011 }
2012#endif
2013 curbuf = curwin->w_buffer;
2014 close_curwin = TRUE;
2015 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002016 if (p_ea
2017#ifdef FEAT_VERTSPLIT
2018 && (*p_ead == 'b' || *p_ead == dir)
2019#endif
2020 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 win_equal(curwin, TRUE,
2022#ifdef FEAT_VERTSPLIT
2023 dir
2024#else
2025 0
2026#endif
2027 );
2028 else
2029 win_comp_pos();
2030 if (close_curwin)
2031 {
2032 win_enter_ext(wp, FALSE, TRUE);
2033#ifdef FEAT_AUTOCMD
2034 if (other_buffer)
2035 /* careful: after this wp and win may be invalid! */
2036 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2037#endif
2038 }
2039
2040 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002041 * If last window has a status line now and we don't want one,
2042 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 */
2044 last_status(FALSE);
2045
2046 /* After closing the help window, try restoring the window layout from
2047 * before it was opened. */
2048 if (help_window)
2049 restore_snapshot(close_curwin);
2050
2051#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2052 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2053 if (gui.in_use && !win_hasvertsplit())
2054 gui_init_which_components(NULL);
2055#endif
2056
2057 redraw_all_later(NOT_VALID);
2058}
2059
2060/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002061 * Close window "win" in tab page "tp", which is not the current tab page.
2062 * This may be the last window ih that tab page and result in closing the tab,
2063 * thus "tp" may become invalid!
2064 * Called must check if buffer is hidden.
2065 */
2066 void
2067win_close_othertab(win, free_buf, tp)
2068 win_T *win;
2069 int free_buf;
2070 tabpage_T *tp;
2071{
2072 win_T *wp;
2073 int dir;
2074 tabpage_T *ptp = NULL;
2075
2076 /* Close the link to the buffer. */
2077 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2078
2079 /* Careful: Autocommands may have closed the tab page or made it the
2080 * current tab page. */
2081 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2082 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002083 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002084 return;
2085
2086 /* Autocommands may have closed the window already. */
2087 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2088 ;
2089 if (wp == NULL)
2090 return;
2091
2092 /* Free the memory used for the window. */
2093 wp = win_free_mem(win, &dir, tp);
2094
2095 /* When closing the last window in a tab page remove the tab page. */
2096 if (wp == NULL)
2097 {
2098 if (tp == first_tabpage)
2099 first_tabpage = tp->tp_next;
2100 else
2101 {
2102 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2103 ptp = ptp->tp_next)
2104 ;
2105 if (ptp == NULL)
2106 {
2107 EMSG2(_(e_intern2), "win_close_othertab()");
2108 return;
2109 }
2110 ptp->tp_next = tp->tp_next;
2111 }
2112 vim_free(tp);
2113 }
2114}
2115
2116/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002117 * Free the memory used for a window.
2118 * Returns a pointer to the window that got the freed up space.
2119 */
2120 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002121win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002122 win_T *win;
2123 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002124 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002125{
2126 frame_T *frp;
2127 win_T *wp;
2128
Bram Moolenaarea408852005-06-25 22:49:46 +00002129#ifdef FEAT_FOLDING
2130 clearFolding(win);
2131#endif
2132
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002133 /* reduce the reference count to the argument list. */
2134 alist_unlink(win->w_alist);
2135
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002136 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002137 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002138 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002139 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002140 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002141
2142 return wp;
2143}
2144
2145#if defined(EXITFREE) || defined(PROTO)
2146 void
2147win_free_all()
2148{
2149 int dummy;
2150
Bram Moolenaarf740b292006-02-16 22:11:02 +00002151# ifdef FEAT_WINDOWS
2152 while (first_tabpage->tp_next != NULL)
2153 tabpage_close(TRUE);
2154# endif
2155
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002156 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002157 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002158}
2159#endif
2160
2161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 * Remove a window and its frame from the tree of frames.
2163 * Returns a pointer to the window that got the freed up space.
2164 */
2165/*ARGSUSED*/
2166 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002167winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 win_T *win;
2169 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002170 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
2172 frame_T *frp, *frp2, *frp3;
2173 frame_T *frp_close = win->w_frame;
2174 win_T *wp;
2175 int old_height = 0;
2176
2177 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002178 * If there is only one window there is nothing to remove.
2179 */
2180 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2181 return NULL;
2182
2183 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 * Remove the window from its frame.
2185 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002186 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 wp = frame2win(frp2);
2188
2189 /* Remove this frame from the list of frames. */
2190 frame_remove(frp_close);
2191
2192#ifdef FEAT_VERTSPLIT
2193 if (frp_close->fr_parent->fr_layout == FR_COL)
2194 {
2195#endif
2196 /* When 'winfixheight' is set, remember its old size and restore
2197 * it later (it's a simplistic solution...). Don't do this if the
2198 * window will occupy the full height of the screen. */
2199 if (frp2->fr_win != NULL
2200 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2201 && frp2->fr_win->w_p_wfh)
2202 old_height = frp2->fr_win->w_height;
2203 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2204 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2205 if (old_height != 0)
2206 win_setheight_win(old_height, frp2->fr_win);
2207#ifdef FEAT_VERTSPLIT
2208 *dirp = 'v';
2209 }
2210 else
2211 {
2212 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2213 frp2 == frp_close->fr_next ? TRUE : FALSE);
2214 *dirp = 'h';
2215 }
2216#endif
2217
2218 /* If rows/columns go to a window below/right its positions need to be
2219 * updated. Can only be done after the sizes have been updated. */
2220 if (frp2 == frp_close->fr_next)
2221 {
2222 int row = win->w_winrow;
2223 int col = W_WINCOL(win);
2224
2225 frame_comp_pos(frp2, &row, &col);
2226 }
2227
2228 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2229 {
2230 /* There is no other frame in this list, move its info to the parent
2231 * and remove it. */
2232 frp2->fr_parent->fr_layout = frp2->fr_layout;
2233 frp2->fr_parent->fr_child = frp2->fr_child;
2234 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2235 frp->fr_parent = frp2->fr_parent;
2236 frp2->fr_parent->fr_win = frp2->fr_win;
2237 if (frp2->fr_win != NULL)
2238 frp2->fr_win->w_frame = frp2->fr_parent;
2239 frp = frp2->fr_parent;
2240 vim_free(frp2);
2241
2242 frp2 = frp->fr_parent;
2243 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2244 {
2245 /* The frame above the parent has the same layout, have to merge
2246 * the frames into this list. */
2247 if (frp2->fr_child == frp)
2248 frp2->fr_child = frp->fr_child;
2249 frp->fr_child->fr_prev = frp->fr_prev;
2250 if (frp->fr_prev != NULL)
2251 frp->fr_prev->fr_next = frp->fr_child;
2252 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2253 {
2254 frp3->fr_parent = frp2;
2255 if (frp3->fr_next == NULL)
2256 {
2257 frp3->fr_next = frp->fr_next;
2258 if (frp->fr_next != NULL)
2259 frp->fr_next->fr_prev = frp3;
2260 break;
2261 }
2262 }
2263 vim_free(frp);
2264 }
2265 }
2266
2267 return wp;
2268}
2269
2270/*
2271 * Find out which frame is going to get the freed up space when "win" is
2272 * closed.
2273 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2274 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2275 * This makes opening a window and closing it immediately keep the same window
2276 * layout.
2277 */
2278 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002279win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002281 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282{
2283 frame_T *frp;
2284 int b;
2285
Bram Moolenaarf740b292006-02-16 22:11:02 +00002286 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002287 /* Last window in this tab page, will go to next tab page. */
2288 return alt_tabpage()->tp_curwin->w_frame;
2289
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 frp = win->w_frame;
2291#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002292 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 b = p_spr;
2294 else
2295#endif
2296 b = p_sb;
2297 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2298 return frp->fr_next;
2299 return frp->fr_prev;
2300}
2301
2302/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002303 * Return the tabpage that will be used if the current one is closed.
2304 */
2305 static tabpage_T *
2306alt_tabpage()
2307{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002308 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002309
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002310 /* Use the next tab page if possible. */
2311 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002312 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002313
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002314 /* Find the last but one tab page. */
2315 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2316 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002317 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002318}
2319
2320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 * Find the left-upper window in frame "frp".
2322 */
2323 static win_T *
2324frame2win(frp)
2325 frame_T *frp;
2326{
2327 while (frp->fr_win == NULL)
2328 frp = frp->fr_child;
2329 return frp->fr_win;
2330}
2331
2332/*
2333 * Return TRUE if frame "frp" contains window "wp".
2334 */
2335 static int
2336frame_has_win(frp, wp)
2337 frame_T *frp;
2338 win_T *wp;
2339{
2340 frame_T *p;
2341
2342 if (frp->fr_layout == FR_LEAF)
2343 return frp->fr_win == wp;
2344
2345 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2346 if (frame_has_win(p, wp))
2347 return TRUE;
2348 return FALSE;
2349}
2350
2351/*
2352 * Set a new height for a frame. Recursively sets the height for contained
2353 * frames and windows. Caller must take care of positions.
2354 */
2355 static void
2356frame_new_height(topfrp, height, topfirst, wfh)
2357 frame_T *topfrp;
2358 int height;
2359 int topfirst; /* resize topmost contained frame first */
2360 int wfh; /* obey 'winfixheight' when there is a choice;
2361 may cause the height not to be set */
2362{
2363 frame_T *frp;
2364 int extra_lines;
2365 int h;
2366
2367 if (topfrp->fr_win != NULL)
2368 {
2369 /* Simple case: just one window. */
2370 win_new_height(topfrp->fr_win,
2371 height - topfrp->fr_win->w_status_height);
2372 }
2373#ifdef FEAT_VERTSPLIT
2374 else if (topfrp->fr_layout == FR_ROW)
2375 {
2376 do
2377 {
2378 /* All frames in this row get the same new height. */
2379 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2380 {
2381 frame_new_height(frp, height, topfirst, wfh);
2382 if (frp->fr_height > height)
2383 {
2384 /* Could not fit the windows, make the whole row higher. */
2385 height = frp->fr_height;
2386 break;
2387 }
2388 }
2389 }
2390 while (frp != NULL);
2391 }
2392#endif
2393 else
2394 {
2395 /* Complicated case: Resize a column of frames. Resize the bottom
2396 * frame first, frames above that when needed. */
2397
2398 frp = topfrp->fr_child;
2399 if (wfh)
2400 /* Advance past frames with one window with 'wfh' set. */
2401 while (frame_fixed_height(frp))
2402 {
2403 frp = frp->fr_next;
2404 if (frp == NULL)
2405 return; /* no frame without 'wfh', give up */
2406 }
2407 if (!topfirst)
2408 {
2409 /* Find the bottom frame of this column */
2410 while (frp->fr_next != NULL)
2411 frp = frp->fr_next;
2412 if (wfh)
2413 /* Advance back for frames with one window with 'wfh' set. */
2414 while (frame_fixed_height(frp))
2415 frp = frp->fr_prev;
2416 }
2417
2418 extra_lines = height - topfrp->fr_height;
2419 if (extra_lines < 0)
2420 {
2421 /* reduce height of contained frames, bottom or top frame first */
2422 while (frp != NULL)
2423 {
2424 h = frame_minheight(frp, NULL);
2425 if (frp->fr_height + extra_lines < h)
2426 {
2427 extra_lines += frp->fr_height - h;
2428 frame_new_height(frp, h, topfirst, wfh);
2429 }
2430 else
2431 {
2432 frame_new_height(frp, frp->fr_height + extra_lines,
2433 topfirst, wfh);
2434 break;
2435 }
2436 if (topfirst)
2437 {
2438 do
2439 frp = frp->fr_next;
2440 while (wfh && frp != NULL && frame_fixed_height(frp));
2441 }
2442 else
2443 {
2444 do
2445 frp = frp->fr_prev;
2446 while (wfh && frp != NULL && frame_fixed_height(frp));
2447 }
2448 /* Increase "height" if we could not reduce enough frames. */
2449 if (frp == NULL)
2450 height -= extra_lines;
2451 }
2452 }
2453 else if (extra_lines > 0)
2454 {
2455 /* increase height of bottom or top frame */
2456 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2457 }
2458 }
2459 topfrp->fr_height = height;
2460}
2461
2462/*
2463 * Return TRUE if height of frame "frp" should not be changed because of
2464 * the 'winfixheight' option.
2465 */
2466 static int
2467frame_fixed_height(frp)
2468 frame_T *frp;
2469{
2470 /* frame with one window: fixed height if 'winfixheight' set. */
2471 if (frp->fr_win != NULL)
2472 return frp->fr_win->w_p_wfh;
2473
2474 if (frp->fr_layout == FR_ROW)
2475 {
2476 /* The frame is fixed height if one of the frames in the row is fixed
2477 * height. */
2478 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2479 if (frame_fixed_height(frp))
2480 return TRUE;
2481 return FALSE;
2482 }
2483
2484 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2485 * frames in the row are fixed height. */
2486 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2487 if (!frame_fixed_height(frp))
2488 return FALSE;
2489 return TRUE;
2490}
2491
2492#ifdef FEAT_VERTSPLIT
2493/*
2494 * Add a status line to windows at the bottom of "frp".
2495 * Note: Does not check if there is room!
2496 */
2497 static void
2498frame_add_statusline(frp)
2499 frame_T *frp;
2500{
2501 win_T *wp;
2502
2503 if (frp->fr_layout == FR_LEAF)
2504 {
2505 wp = frp->fr_win;
2506 if (wp->w_status_height == 0)
2507 {
2508 if (wp->w_height > 0) /* don't make it negative */
2509 --wp->w_height;
2510 wp->w_status_height = STATUS_HEIGHT;
2511 }
2512 }
2513 else if (frp->fr_layout == FR_ROW)
2514 {
2515 /* Handle all the frames in the row. */
2516 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2517 frame_add_statusline(frp);
2518 }
2519 else /* frp->fr_layout == FR_COL */
2520 {
2521 /* Only need to handle the last frame in the column. */
2522 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2523 ;
2524 frame_add_statusline(frp);
2525 }
2526}
2527
2528/*
2529 * Set width of a frame. Handles recursively going through contained frames.
2530 * May remove separator line for windows at the right side (for win_close()).
2531 */
2532 static void
2533frame_new_width(topfrp, width, leftfirst)
2534 frame_T *topfrp;
2535 int width;
2536 int leftfirst; /* resize leftmost contained frame first */
2537{
2538 frame_T *frp;
2539 int extra_cols;
2540 int w;
2541 win_T *wp;
2542
2543 if (topfrp->fr_layout == FR_LEAF)
2544 {
2545 /* Simple case: just one window. */
2546 wp = topfrp->fr_win;
2547 /* Find out if there are any windows right of this one. */
2548 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2549 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2550 break;
2551 if (frp->fr_parent == NULL)
2552 wp->w_vsep_width = 0;
2553 win_new_width(wp, width - wp->w_vsep_width);
2554 }
2555 else if (topfrp->fr_layout == FR_COL)
2556 {
2557 /* All frames in this column get the same new width. */
2558 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2559 frame_new_width(frp, width, leftfirst);
2560 }
2561 else /* fr_layout == FR_ROW */
2562 {
2563 /* Complicated case: Resize a row of frames. Resize the rightmost
2564 * frame first, frames left of it when needed. */
2565
2566 /* Find the rightmost frame of this row */
2567 frp = topfrp->fr_child;
2568 if (!leftfirst)
2569 while (frp->fr_next != NULL)
2570 frp = frp->fr_next;
2571
2572 extra_cols = width - topfrp->fr_width;
2573 if (extra_cols < 0)
2574 {
2575 /* reduce frame width, rightmost frame first */
2576 while (frp != NULL)
2577 {
2578 w = frame_minwidth(frp, NULL);
2579 if (frp->fr_width + extra_cols < w)
2580 {
2581 extra_cols += frp->fr_width - w;
2582 frame_new_width(frp, w, leftfirst);
2583 }
2584 else
2585 {
2586 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst);
2587 break;
2588 }
2589 if (leftfirst)
2590 frp = frp->fr_next;
2591 else
2592 frp = frp->fr_prev;
2593 }
2594 }
2595 else if (extra_cols > 0)
2596 {
2597 /* increase width of rightmost frame */
2598 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst);
2599 }
2600 }
2601 topfrp->fr_width = width;
2602}
2603
2604/*
2605 * Add the vertical separator to windows at the right side of "frp".
2606 * Note: Does not check if there is room!
2607 */
2608 static void
2609frame_add_vsep(frp)
2610 frame_T *frp;
2611{
2612 win_T *wp;
2613
2614 if (frp->fr_layout == FR_LEAF)
2615 {
2616 wp = frp->fr_win;
2617 if (wp->w_vsep_width == 0)
2618 {
2619 if (wp->w_width > 0) /* don't make it negative */
2620 --wp->w_width;
2621 wp->w_vsep_width = 1;
2622 }
2623 }
2624 else if (frp->fr_layout == FR_COL)
2625 {
2626 /* Handle all the frames in the column. */
2627 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2628 frame_add_vsep(frp);
2629 }
2630 else /* frp->fr_layout == FR_ROW */
2631 {
2632 /* Only need to handle the last frame in the row. */
2633 frp = frp->fr_child;
2634 while (frp->fr_next != NULL)
2635 frp = frp->fr_next;
2636 frame_add_vsep(frp);
2637 }
2638}
2639
2640/*
2641 * Set frame width from the window it contains.
2642 */
2643 static void
2644frame_fix_width(wp)
2645 win_T *wp;
2646{
2647 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2648}
2649#endif
2650
2651/*
2652 * Set frame height from the window it contains.
2653 */
2654 static void
2655frame_fix_height(wp)
2656 win_T *wp;
2657{
2658 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2659}
2660
2661/*
2662 * Compute the minimal height for frame "topfrp".
2663 * Uses the 'winminheight' option.
2664 * When "next_curwin" isn't NULL, use p_wh for this window.
2665 * When "next_curwin" is NOWIN, don't use at least one line for the current
2666 * window.
2667 */
2668 static int
2669frame_minheight(topfrp, next_curwin)
2670 frame_T *topfrp;
2671 win_T *next_curwin;
2672{
2673 frame_T *frp;
2674 int m;
2675#ifdef FEAT_VERTSPLIT
2676 int n;
2677#endif
2678
2679 if (topfrp->fr_win != NULL)
2680 {
2681 if (topfrp->fr_win == next_curwin)
2682 m = p_wh + topfrp->fr_win->w_status_height;
2683 else
2684 {
2685 /* window: minimal height of the window plus status line */
2686 m = p_wmh + topfrp->fr_win->w_status_height;
2687 /* Current window is minimal one line high */
2688 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2689 ++m;
2690 }
2691 }
2692#ifdef FEAT_VERTSPLIT
2693 else if (topfrp->fr_layout == FR_ROW)
2694 {
2695 /* get the minimal height from each frame in this row */
2696 m = 0;
2697 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2698 {
2699 n = frame_minheight(frp, next_curwin);
2700 if (n > m)
2701 m = n;
2702 }
2703 }
2704#endif
2705 else
2706 {
2707 /* Add up the minimal heights for all frames in this column. */
2708 m = 0;
2709 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2710 m += frame_minheight(frp, next_curwin);
2711 }
2712
2713 return m;
2714}
2715
2716#ifdef FEAT_VERTSPLIT
2717/*
2718 * Compute the minimal width for frame "topfrp".
2719 * When "next_curwin" isn't NULL, use p_wiw for this window.
2720 * When "next_curwin" is NOWIN, don't use at least one column for the current
2721 * window.
2722 */
2723 static int
2724frame_minwidth(topfrp, next_curwin)
2725 frame_T *topfrp;
2726 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2727{
2728 frame_T *frp;
2729 int m, n;
2730
2731 if (topfrp->fr_win != NULL)
2732 {
2733 if (topfrp->fr_win == next_curwin)
2734 m = p_wiw + topfrp->fr_win->w_vsep_width;
2735 else
2736 {
2737 /* window: minimal width of the window plus separator column */
2738 m = p_wmw + topfrp->fr_win->w_vsep_width;
2739 /* Current window is minimal one column wide */
2740 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2741 ++m;
2742 }
2743 }
2744 else if (topfrp->fr_layout == FR_COL)
2745 {
2746 /* get the minimal width from each frame in this column */
2747 m = 0;
2748 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2749 {
2750 n = frame_minwidth(frp, next_curwin);
2751 if (n > m)
2752 m = n;
2753 }
2754 }
2755 else
2756 {
2757 /* Add up the minimal widths for all frames in this row. */
2758 m = 0;
2759 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2760 m += frame_minwidth(frp, next_curwin);
2761 }
2762
2763 return m;
2764}
2765#endif
2766
2767
2768/*
2769 * Try to close all windows except current one.
2770 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2771 * used and the buffer was modified.
2772 *
2773 * Used by ":bdel" and ":only".
2774 */
2775 void
2776close_others(message, forceit)
2777 int message;
2778 int forceit; /* always hide all other windows */
2779{
2780 win_T *wp;
2781 win_T *nextwp;
2782 int r;
2783
2784 if (lastwin == firstwin)
2785 {
2786 if (message
2787#ifdef FEAT_AUTOCMD
2788 && !autocmd_busy
2789#endif
2790 )
2791 MSG(_("Already only one window"));
2792 return;
2793 }
2794
2795 /* Be very careful here: autocommands may change the window layout. */
2796 for (wp = firstwin; win_valid(wp); wp = nextwp)
2797 {
2798 nextwp = wp->w_next;
2799 if (wp != curwin) /* don't close current window */
2800 {
2801
2802 /* Check if it's allowed to abandon this window */
2803 r = can_abandon(wp->w_buffer, forceit);
2804#ifdef FEAT_AUTOCMD
2805 if (!win_valid(wp)) /* autocommands messed wp up */
2806 {
2807 nextwp = firstwin;
2808 continue;
2809 }
2810#endif
2811 if (!r)
2812 {
2813#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2814 if (message && (p_confirm || cmdmod.confirm) && p_write)
2815 {
2816 dialog_changed(wp->w_buffer, FALSE);
2817# ifdef FEAT_AUTOCMD
2818 if (!win_valid(wp)) /* autocommands messed wp up */
2819 {
2820 nextwp = firstwin;
2821 continue;
2822 }
2823# endif
2824 }
2825 if (bufIsChanged(wp->w_buffer))
2826#endif
2827 continue;
2828 }
2829 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
2830 }
2831 }
2832
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002833 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 EMSG(_("E445: Other window contains changes"));
2835}
2836
2837#endif /* FEAT_WINDOWS */
2838
2839/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002840 * Init the current window "curwin".
2841 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 */
2843 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002844curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002846 redraw_win_later(curwin, NOT_VALID);
2847 curwin->w_lines_valid = 0;
2848 curwin->w_cursor.lnum = 1;
2849 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002851 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002853 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
2854 curwin->w_pcmark.col = 0;
2855 curwin->w_prev_pcmark.lnum = 0;
2856 curwin->w_prev_pcmark.col = 0;
2857 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002859 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002861 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862#ifdef FEAT_FKMAP
2863 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002864 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002866 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867#endif
2868}
2869
2870/*
2871 * Allocate the first window and put an empty buffer in it.
2872 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002873 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002875 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876win_alloc_first()
2877{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002878 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002879 return FAIL;
2880
2881#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002882 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002883 if (first_tabpage == NULL)
2884 return FAIL;
2885 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002886 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002887#endif
2888 return OK;
2889}
2890
2891/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002892 * Allocate the first window or the first window in a new tab page.
2893 * When "oldwin" is NULL create an empty buffer for it.
2894 * When "oldwin" is not NULL copy info from it to the new window (only with
2895 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002896 * Return FAIL when something goes wrong (out of memory).
2897 */
2898 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002899win_alloc_firstwin(oldwin)
2900 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002901{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002903 if (oldwin == NULL)
2904 {
2905 /* Very first window, need to create an empty buffer for it and
2906 * initialize from scratch. */
2907 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
2908 if (curwin == NULL || curbuf == NULL)
2909 return FAIL;
2910 curwin->w_buffer = curbuf;
2911 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002913 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002915 curwin_init(); /* init current window */
2916 }
2917#ifdef FEAT_WINDOWS
2918 else
2919 {
2920 /* First window in new tab page, initialize it from "oldwin". */
2921 win_init(curwin, oldwin);
2922
2923# ifdef FEAT_SCROLLBIND
2924 /* We don't want scroll-binding in the first window. */
2925 curwin->w_p_scb = FALSE;
2926# endif
2927 }
2928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929
2930 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
2931 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002932 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 topframe->fr_layout = FR_LEAF;
2934#ifdef FEAT_VERTSPLIT
2935 topframe->fr_width = Columns;
2936#endif
2937 topframe->fr_height = Rows - p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002938#ifdef FEAT_WINDOWS
2939 p_ch_used = p_ch;
2940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 topframe->fr_win = curwin;
2942 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002943
2944 return OK;
2945}
2946
2947/*
2948 * Initialize the window and frame size to the maximum.
2949 */
2950 void
2951win_init_size()
2952{
2953 firstwin->w_height = ROWS_AVAIL;
2954 topframe->fr_height = ROWS_AVAIL;
2955#ifdef FEAT_VERTSPLIT
2956 firstwin->w_width = Columns;
2957 topframe->fr_width = Columns;
2958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959}
2960
2961#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002962
2963/*
2964 * Allocate a new tabpage_T and init the values.
2965 * Returns NULL when out of memory.
2966 */
2967 static tabpage_T *
2968alloc_tabpage()
2969{
2970 tabpage_T *tp;
2971
2972 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
2973 if (tp != NULL)
2974 {
2975# ifdef FEAT_DIFF
2976 tp->tp_diff_invalid = TRUE;
2977# endif
2978 }
2979 return tp;
2980}
2981
2982 static void
2983free_tabpage(tp)
2984 tabpage_T *tp;
2985{
2986# ifdef FEAT_DIFF
2987 diff_clear(tp);
2988# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002989 clear_snapshot(tp);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002990 vim_free(tp);
2991}
2992
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002993/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002994 * Create a new Tab page with one window.
2995 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002996 * When "after" is 0 put it just after the current Tab page.
2997 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002998 * Return FAIL or OK.
2999 */
3000 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003001win_new_tabpage(after)
3002 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003003{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003004 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003005 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003006 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003007
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003008 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003009 if (newtp == NULL)
3010 return FAIL;
3011
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003012 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003013 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003014 {
3015 vim_free(newtp);
3016 return FAIL;
3017 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003018 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003019
3020 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003021 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003022 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003023 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003024 if (after == 1)
3025 {
3026 /* New tab page becomes the first one. */
3027 newtp->tp_next = first_tabpage;
3028 first_tabpage = newtp;
3029 }
3030 else
3031 {
3032 if (after > 0)
3033 {
3034 /* Put new tab page before tab page "after". */
3035 n = 2;
3036 for (tp = first_tabpage; tp->tp_next != NULL
3037 && n < after; tp = tp->tp_next)
3038 ++n;
3039 }
3040 newtp->tp_next = tp->tp_next;
3041 tp->tp_next = newtp;
3042 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003043 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003044 firstwin->w_winrow = tabline_height();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003045
3046 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003047 last_status(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003048 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003049#ifdef FEAT_AUTOCMD
3050 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3051 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3052#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003053 return OK;
3054 }
3055
3056 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003057 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003058 return FAIL;
3059}
3060
3061/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003062 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3063 * like with ":split".
3064 * Returns OK if a new tab page was created, FAIL otherwise.
3065 */
3066 int
3067may_open_tabpage()
3068{
3069 int n = cmdmod.tab;
3070
3071 if (cmdmod.tab != 0)
3072 {
3073 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3074 return win_new_tabpage(n);
3075 }
3076 return FAIL;
3077}
3078
3079/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003080 * Create up to "maxcount" tabpages with empty windows.
3081 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003082 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003083 int
3084make_tabpages(maxcount)
3085 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003086{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003087 int count = maxcount;
3088 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003089
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003090 /* Limit to 'tabpagemax' tabs. */
3091 if (count > p_tpm)
3092 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003093
3094#ifdef FEAT_AUTOCMD
3095 /*
3096 * Don't execute autocommands while creating the tab pages. Must do that
3097 * when putting the buffers in the windows.
3098 */
3099 ++autocmd_block;
3100#endif
3101
3102 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003103 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003104 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003105
3106#ifdef FEAT_AUTOCMD
3107 --autocmd_block;
3108#endif
3109
3110 /* return actual number of tab pages */
3111 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003112}
3113
3114/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003115 * Return TRUE when "tpc" points to a valid tab page.
3116 */
3117 int
3118valid_tabpage(tpc)
3119 tabpage_T *tpc;
3120{
3121 tabpage_T *tp;
3122
3123 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3124 if (tp == tpc)
3125 return TRUE;
3126 return FALSE;
3127}
3128
3129/*
3130 * Find tab page "n" (first one is 1). Returns NULL when not found.
3131 */
3132 tabpage_T *
3133find_tabpage(n)
3134 int n;
3135{
3136 tabpage_T *tp;
3137 int i = 1;
3138
3139 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3140 ++i;
3141 return tp;
3142}
3143
3144/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003145 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003146 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003147 */
3148 int
3149tabpage_index(ftp)
3150 tabpage_T *ftp;
3151{
3152 int i = 1;
3153 tabpage_T *tp;
3154
3155 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3156 ++i;
3157 return i;
3158}
3159
3160/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003161 * Prepare for leaving the current tab page.
3162 * When autocomands change "curtab" we don't leave the tab page and return
3163 * FAIL.
3164 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003165 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003166/*ARGSUSED*/
3167 static int
3168leave_tabpage(new_curbuf)
3169 buf_T *new_curbuf; /* what is going to be the new curbuf,
3170 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003171{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003172 tabpage_T *tp = curtab;
3173
3174#ifdef FEAT_AUTOCMD
3175 if (new_curbuf != curbuf)
3176 {
3177 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3178 if (curtab != tp)
3179 return FAIL;
3180 }
3181 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3182 if (curtab != tp)
3183 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003184 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003185 if (curtab != tp)
3186 return FAIL;
3187#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003188#if defined(FEAT_GUI)
3189 /* Remove the scrollbars. They may be added back later. */
3190 if (gui.in_use)
3191 gui_remove_scrollbars();
3192#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003193 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003194 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003195 tp->tp_firstwin = firstwin;
3196 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003197 tp->tp_old_Rows = Rows;
3198 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003199 firstwin = NULL;
3200 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003201 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003202}
3203
3204/*
3205 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003206 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003207 */
3208/*ARGSUSED*/
3209 static void
3210enter_tabpage(tp, old_curbuf)
3211 tabpage_T *tp;
3212 buf_T *old_curbuf;
3213{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003214 int old_off = tp->tp_firstwin->w_winrow;
3215
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003216 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003217 firstwin = tp->tp_firstwin;
3218 lastwin = tp->tp_lastwin;
3219 topframe = tp->tp_topframe;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003220#ifdef FEAT_AUTOCMD
3221 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3222#endif
3223
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003224 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003225 prevwin = tp->tp_prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003226
3227#ifdef FEAT_AUTOCMD
3228 if (old_curbuf != curbuf)
3229 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3230#endif
3231
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003232 last_status(FALSE); /* status line may appear or disappear */
3233 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003234 must_redraw = CLEAR; /* need to redraw everything */
3235#ifdef FEAT_DIFF
3236 diff_need_scrollbind = TRUE;
3237#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003238
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003239 /* The tabpage line may have appeared or disappeared, may need to resize
3240 * the frames for that. When the Vim window was resized need to update
3241 * frame sizes too. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003242 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3243#ifdef FEAT_GUI_TABLINE
3244 && !gui_use_tabline()
3245#endif
3246 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003247 shell_new_rows();
3248#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003249 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003250 shell_new_columns(); /* update window widths */
3251#endif
3252
3253#if defined(FEAT_GUI)
3254 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3255 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003256 if (gui.in_use && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003257 gui_update_scrollbars(TRUE);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003258 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003259#endif
3260
3261 redraw_all_later(CLEAR);
3262}
3263
3264/*
3265 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003266 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003267 */
3268 void
3269goto_tabpage(n)
3270 int n;
3271{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003272 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003273 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003274 int i;
3275
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003276 /* If there is only one it can't work. */
3277 if (first_tabpage->tp_next == NULL)
3278 {
3279 if (n > 1)
3280 beep_flush();
3281 return;
3282 }
3283
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003284 if (n == 0)
3285 {
3286 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003287 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003288 tp = first_tabpage;
3289 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003290 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003291 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003292 else if (n < 0)
3293 {
3294 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3295 * this N times. */
3296 ttp = curtab;
3297 for (i = n; i < 0; ++i)
3298 {
3299 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3300 tp = tp->tp_next)
3301 ;
3302 ttp = tp;
3303 }
3304 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003305 else if (n == 9999)
3306 {
3307 /* Go to last tab page. */
3308 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3309 ;
3310 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003311 else
3312 {
3313 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003314 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003315 if (tp == NULL)
3316 {
3317 beep_flush();
3318 return;
3319 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003320 }
3321
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003322 goto_tabpage_tp(tp);
3323
3324#ifdef FEAT_GUI_TABLINE
3325 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003326 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003327#endif
3328}
3329
3330/*
3331 * Go to tabpage "tp".
3332 * Note: doesn't update the GUI tab.
3333 */
3334 void
3335goto_tabpage_tp(tp)
3336 tabpage_T *tp;
3337{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003338 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003339 {
3340 if (valid_tabpage(tp))
3341 enter_tabpage(tp, curbuf);
3342 else
3343 enter_tabpage(curtab, curbuf);
3344 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003345}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346
3347/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003348 * Move the current tab page to before tab page "nr".
3349 */
3350 void
3351tabpage_move(nr)
3352 int nr;
3353{
3354 int n = nr;
3355 tabpage_T *tp;
3356
3357 if (first_tabpage->tp_next == NULL)
3358 return;
3359
3360 /* Remove the current tab page from the list of tab pages. */
3361 if (curtab == first_tabpage)
3362 first_tabpage = curtab->tp_next;
3363 else
3364 {
3365 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3366 if (tp->tp_next == curtab)
3367 break;
3368 if (tp == NULL) /* "cannot happen" */
3369 return;
3370 tp->tp_next = curtab->tp_next;
3371 }
3372
3373 /* Re-insert it at the specified position. */
3374 if (n == 0)
3375 {
3376 curtab->tp_next = first_tabpage;
3377 first_tabpage = curtab;
3378 }
3379 else
3380 {
3381 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3382 --n;
3383 curtab->tp_next = tp->tp_next;
3384 tp->tp_next = curtab;
3385 }
3386
3387 /* Need to redraw the tabline. Tab page contents doesn't change. */
3388 redraw_tabline = TRUE;
3389}
3390
3391
3392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 * Go to another window.
3394 * When jumping to another buffer, stop Visual mode. Do this before
3395 * changing windows so we can yank the selection into the '*' register.
3396 * When jumping to another window on the same buffer, adjust its cursor
3397 * position to keep the same Visual area.
3398 */
3399 void
3400win_goto(wp)
3401 win_T *wp;
3402{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003403 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 {
3405 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003406 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 return;
3408 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003409
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410#ifdef FEAT_VISUAL
3411 if (wp->w_buffer != curbuf)
3412 reset_VIsual_and_resel();
3413 else if (VIsual_active)
3414 wp->w_cursor = curwin->w_cursor;
3415#endif
3416
3417#ifdef FEAT_GUI
3418 need_mouse_correct = TRUE;
3419#endif
3420 win_enter(wp, TRUE);
3421}
3422
3423#if defined(FEAT_PERL) || defined(PROTO)
3424/*
3425 * Find window number "winnr" (counting top to bottom).
3426 */
3427 win_T *
3428win_find_nr(winnr)
3429 int winnr;
3430{
3431 win_T *wp;
3432
3433# ifdef FEAT_WINDOWS
3434 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3435 if (--winnr == 0)
3436 break;
3437 return wp;
3438# else
3439 return curwin;
3440# endif
3441}
3442#endif
3443
3444#ifdef FEAT_VERTSPLIT
3445/*
3446 * Move to window above or below "count" times.
3447 */
3448 static void
3449win_goto_ver(up, count)
3450 int up; /* TRUE to go to win above */
3451 long count;
3452{
3453 frame_T *fr;
3454 frame_T *nfr;
3455 frame_T *foundfr;
3456
3457 foundfr = curwin->w_frame;
3458 while (count--)
3459 {
3460 /*
3461 * First go upwards in the tree of frames until we find a upwards or
3462 * downwards neighbor.
3463 */
3464 fr = foundfr;
3465 for (;;)
3466 {
3467 if (fr == topframe)
3468 goto end;
3469 if (up)
3470 nfr = fr->fr_prev;
3471 else
3472 nfr = fr->fr_next;
3473 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3474 break;
3475 fr = fr->fr_parent;
3476 }
3477
3478 /*
3479 * Now go downwards to find the bottom or top frame in it.
3480 */
3481 for (;;)
3482 {
3483 if (nfr->fr_layout == FR_LEAF)
3484 {
3485 foundfr = nfr;
3486 break;
3487 }
3488 fr = nfr->fr_child;
3489 if (nfr->fr_layout == FR_ROW)
3490 {
3491 /* Find the frame at the cursor row. */
3492 while (fr->fr_next != NULL
3493 && frame2win(fr)->w_wincol + fr->fr_width
3494 <= curwin->w_wincol + curwin->w_wcol)
3495 fr = fr->fr_next;
3496 }
3497 if (nfr->fr_layout == FR_COL && up)
3498 while (fr->fr_next != NULL)
3499 fr = fr->fr_next;
3500 nfr = fr;
3501 }
3502 }
3503end:
3504 if (foundfr != NULL)
3505 win_goto(foundfr->fr_win);
3506}
3507
3508/*
3509 * Move to left or right window.
3510 */
3511 static void
3512win_goto_hor(left, count)
3513 int left; /* TRUE to go to left win */
3514 long count;
3515{
3516 frame_T *fr;
3517 frame_T *nfr;
3518 frame_T *foundfr;
3519
3520 foundfr = curwin->w_frame;
3521 while (count--)
3522 {
3523 /*
3524 * First go upwards in the tree of frames until we find a left or
3525 * right neighbor.
3526 */
3527 fr = foundfr;
3528 for (;;)
3529 {
3530 if (fr == topframe)
3531 goto end;
3532 if (left)
3533 nfr = fr->fr_prev;
3534 else
3535 nfr = fr->fr_next;
3536 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3537 break;
3538 fr = fr->fr_parent;
3539 }
3540
3541 /*
3542 * Now go downwards to find the leftmost or rightmost frame in it.
3543 */
3544 for (;;)
3545 {
3546 if (nfr->fr_layout == FR_LEAF)
3547 {
3548 foundfr = nfr;
3549 break;
3550 }
3551 fr = nfr->fr_child;
3552 if (nfr->fr_layout == FR_COL)
3553 {
3554 /* Find the frame at the cursor row. */
3555 while (fr->fr_next != NULL
3556 && frame2win(fr)->w_winrow + fr->fr_height
3557 <= curwin->w_winrow + curwin->w_wrow)
3558 fr = fr->fr_next;
3559 }
3560 if (nfr->fr_layout == FR_ROW && left)
3561 while (fr->fr_next != NULL)
3562 fr = fr->fr_next;
3563 nfr = fr;
3564 }
3565 }
3566end:
3567 if (foundfr != NULL)
3568 win_goto(foundfr->fr_win);
3569}
3570#endif
3571
3572/*
3573 * Make window "wp" the current window.
3574 */
3575 void
3576win_enter(wp, undo_sync)
3577 win_T *wp;
3578 int undo_sync;
3579{
3580 win_enter_ext(wp, undo_sync, FALSE);
3581}
3582
3583/*
3584 * Make window wp the current window.
3585 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3586 * been closed and isn't valid.
3587 */
3588 static void
3589win_enter_ext(wp, undo_sync, curwin_invalid)
3590 win_T *wp;
3591 int undo_sync;
3592 int curwin_invalid;
3593{
3594#ifdef FEAT_AUTOCMD
3595 int other_buffer = FALSE;
3596#endif
3597
3598 if (wp == curwin && !curwin_invalid) /* nothing to do */
3599 return;
3600
3601#ifdef FEAT_AUTOCMD
3602 if (!curwin_invalid)
3603 {
3604 /*
3605 * Be careful: If autocommands delete the window, return now.
3606 */
3607 if (wp->w_buffer != curbuf)
3608 {
3609 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3610 other_buffer = TRUE;
3611 if (!win_valid(wp))
3612 return;
3613 }
3614 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3615 if (!win_valid(wp))
3616 return;
3617# ifdef FEAT_EVAL
3618 /* autocmds may abort script processing */
3619 if (aborting())
3620 return;
3621# endif
3622 }
3623#endif
3624
3625 /* sync undo before leaving the current buffer */
3626 if (undo_sync && curbuf != wp->w_buffer)
3627 u_sync();
3628 /* may have to copy the buffer options when 'cpo' contains 'S' */
3629 if (wp->w_buffer != curbuf)
3630 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3631 if (!curwin_invalid)
3632 {
3633 prevwin = curwin; /* remember for CTRL-W p */
3634 curwin->w_redr_status = TRUE;
3635 }
3636 curwin = wp;
3637 curbuf = wp->w_buffer;
3638 check_cursor();
3639#ifdef FEAT_VIRTUALEDIT
3640 if (!virtual_active())
3641 curwin->w_cursor.coladd = 0;
3642#endif
3643 changed_line_abv_curs(); /* assume cursor position needs updating */
3644
3645 if (curwin->w_localdir != NULL)
3646 {
3647 /* Window has a local directory: Save current directory as global
3648 * directory (unless that was done already) and change to the local
3649 * directory. */
3650 if (globaldir == NULL)
3651 {
3652 char_u cwd[MAXPATHL];
3653
3654 if (mch_dirname(cwd, MAXPATHL) == OK)
3655 globaldir = vim_strsave(cwd);
3656 }
3657 mch_chdir((char *)curwin->w_localdir);
3658 shorten_fnames(TRUE);
3659 }
3660 else if (globaldir != NULL)
3661 {
3662 /* Window doesn't have a local directory and we are not in the global
3663 * directory: Change to the global directory. */
3664 mch_chdir((char *)globaldir);
3665 vim_free(globaldir);
3666 globaldir = NULL;
3667 shorten_fnames(TRUE);
3668 }
3669
3670#ifdef FEAT_AUTOCMD
3671 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3672 if (other_buffer)
3673 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3674#endif
3675
3676#ifdef FEAT_TITLE
3677 maketitle();
3678#endif
3679 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003680 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 if (restart_edit)
3682 redraw_later(VALID); /* causes status line redraw */
3683
3684 /* set window height to desired minimal value */
3685 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3686 win_setheight((int)p_wh);
3687 else if (curwin->w_height == 0)
3688 win_setheight(1);
3689
3690#ifdef FEAT_VERTSPLIT
3691 /* set window width to desired minimal value */
3692 if (curwin->w_width < p_wiw)
3693 win_setwidth((int)p_wiw);
3694#endif
3695
3696#ifdef FEAT_MOUSE
3697 setmouse(); /* in case jumped to/from help buffer */
3698#endif
3699
3700#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)
3701 /* Change directories when the acd option is set on and after
3702 * switching windows. */
3703 if (p_acd && curbuf->b_ffname != NULL
3704 && vim_chdirfile(curbuf->b_ffname) == OK)
3705 shorten_fnames(TRUE);
3706#endif
3707}
3708
3709#endif /* FEAT_WINDOWS */
3710
3711#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3712/*
3713 * Jump to the first open window that contains buffer buf if one exists
3714 * TODO: Alternatively jump to last open window? Dependent from 'splitbelow'?
3715 * Returns pointer to window if it exists, otherwise NULL.
3716 */
3717 win_T *
3718buf_jump_open_win(buf)
3719 buf_T *buf;
3720{
3721# ifdef FEAT_WINDOWS
3722 win_T *wp;
3723
3724 for (wp = firstwin; wp; wp = wp->w_next)
3725 if (wp->w_buffer == buf)
3726 break;
3727 if (wp != NULL)
3728 win_enter(wp, FALSE);
3729 return wp;
3730# else
3731 if (curwin->w_buffer == buf)
3732 return curwin;
3733 return NULL;
3734# endif
3735}
3736#endif
3737
3738/*
3739 * allocate a window structure and link it in the window list
3740 */
3741/*ARGSUSED*/
3742 static win_T *
3743win_alloc(after)
3744 win_T *after;
3745{
3746 win_T *newwin;
3747
3748 /*
3749 * allocate window structure and linesizes arrays
3750 */
3751 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
3752 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
3753 {
3754 vim_free(newwin);
3755 newwin = NULL;
3756 }
3757
3758 if (newwin != NULL)
3759 {
3760 /*
3761 * link the window in the window list
3762 */
3763#ifdef FEAT_WINDOWS
3764 win_append(after, newwin);
3765#endif
3766#ifdef FEAT_VERTSPLIT
3767 newwin->w_wincol = 0;
3768 newwin->w_width = Columns;
3769#endif
3770
3771 /* position the display and the cursor at the top of the file. */
3772 newwin->w_topline = 1;
3773#ifdef FEAT_DIFF
3774 newwin->w_topfill = 0;
3775#endif
3776 newwin->w_botline = 2;
3777 newwin->w_cursor.lnum = 1;
3778#ifdef FEAT_SCROLLBIND
3779 newwin->w_scbind_pos = 1;
3780#endif
3781
3782 /* We won't calculate w_fraction until resizing the window */
3783 newwin->w_fraction = 0;
3784 newwin->w_prev_fraction_row = -1;
3785
3786#ifdef FEAT_GUI
3787 if (gui.in_use)
3788 {
3789 out_flush();
3790 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
3791 SBAR_LEFT, newwin);
3792 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
3793 SBAR_RIGHT, newwin);
3794 }
3795#endif
3796#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003797 /* init w: variables */
3798 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799#endif
3800#ifdef FEAT_FOLDING
3801 foldInitWin(newwin);
3802#endif
3803 }
3804 return newwin;
3805}
3806
3807#if defined(FEAT_WINDOWS) || defined(PROTO)
3808
3809/*
3810 * remove window 'wp' from the window list and free the structure
3811 */
3812 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00003813win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003815 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816{
3817 int i;
3818
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003819#ifdef FEAT_MZSCHEME
3820 mzscheme_window_free(wp);
3821#endif
3822
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823#ifdef FEAT_PERL
3824 perl_win_free(wp);
3825#endif
3826
3827#ifdef FEAT_PYTHON
3828 python_window_free(wp);
3829#endif
3830
3831#ifdef FEAT_TCL
3832 tcl_window_free(wp);
3833#endif
3834
3835#ifdef FEAT_RUBY
3836 ruby_window_free(wp);
3837#endif
3838
3839 clear_winopt(&wp->w_onebuf_opt);
3840 clear_winopt(&wp->w_allbuf_opt);
3841
3842#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003843 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844#endif
3845
3846 if (prevwin == wp)
3847 prevwin = NULL;
3848 win_free_lsize(wp);
3849
3850 for (i = 0; i < wp->w_tagstacklen; ++i)
3851 vim_free(wp->w_tagstack[i].tagname);
3852
3853 vim_free(wp->w_localdir);
3854#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003855 vim_free(wp->w_match[0].regprog);
3856 vim_free(wp->w_match[1].regprog);
3857 vim_free(wp->w_match[2].regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858#endif
3859#ifdef FEAT_JUMPLIST
3860 free_jumplist(wp);
3861#endif
3862
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003863#ifdef FEAT_QUICKFIX
3864 qf_free_all(wp);
3865#endif
3866
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867#ifdef FEAT_GUI
3868 if (gui.in_use)
3869 {
3870 out_flush();
3871 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
3872 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
3873 }
3874#endif /* FEAT_GUI */
3875
Bram Moolenaarf740b292006-02-16 22:11:02 +00003876 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 vim_free(wp);
3878}
3879
3880/*
3881 * Append window "wp" in the window list after window "after".
3882 */
3883 static void
3884win_append(after, wp)
3885 win_T *after, *wp;
3886{
3887 win_T *before;
3888
3889 if (after == NULL) /* after NULL is in front of the first */
3890 before = firstwin;
3891 else
3892 before = after->w_next;
3893
3894 wp->w_next = before;
3895 wp->w_prev = after;
3896 if (after == NULL)
3897 firstwin = wp;
3898 else
3899 after->w_next = wp;
3900 if (before == NULL)
3901 lastwin = wp;
3902 else
3903 before->w_prev = wp;
3904}
3905
3906/*
3907 * Remove a window from the window list.
3908 */
3909 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00003910win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003912 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913{
3914 if (wp->w_prev != NULL)
3915 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003916 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003918 else
3919 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 if (wp->w_next != NULL)
3921 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003922 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003924 else
3925 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926}
3927
3928/*
3929 * Append frame "frp" in a frame list after frame "after".
3930 */
3931 static void
3932frame_append(after, frp)
3933 frame_T *after, *frp;
3934{
3935 frp->fr_next = after->fr_next;
3936 after->fr_next = frp;
3937 if (frp->fr_next != NULL)
3938 frp->fr_next->fr_prev = frp;
3939 frp->fr_prev = after;
3940}
3941
3942/*
3943 * Insert frame "frp" in a frame list before frame "before".
3944 */
3945 static void
3946frame_insert(before, frp)
3947 frame_T *before, *frp;
3948{
3949 frp->fr_next = before;
3950 frp->fr_prev = before->fr_prev;
3951 before->fr_prev = frp;
3952 if (frp->fr_prev != NULL)
3953 frp->fr_prev->fr_next = frp;
3954 else
3955 frp->fr_parent->fr_child = frp;
3956}
3957
3958/*
3959 * Remove a frame from a frame list.
3960 */
3961 static void
3962frame_remove(frp)
3963 frame_T *frp;
3964{
3965 if (frp->fr_prev != NULL)
3966 frp->fr_prev->fr_next = frp->fr_next;
3967 else
3968 frp->fr_parent->fr_child = frp->fr_next;
3969 if (frp->fr_next != NULL)
3970 frp->fr_next->fr_prev = frp->fr_prev;
3971}
3972
3973#endif /* FEAT_WINDOWS */
3974
3975/*
3976 * Allocate w_lines[] for window "wp".
3977 * Return FAIL for failure, OK for success.
3978 */
3979 int
3980win_alloc_lines(wp)
3981 win_T *wp;
3982{
3983 wp->w_lines_valid = 0;
3984 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
3985 if (wp->w_lines == NULL)
3986 return FAIL;
3987 return OK;
3988}
3989
3990/*
3991 * free lsize arrays for a window
3992 */
3993 void
3994win_free_lsize(wp)
3995 win_T *wp;
3996{
3997 vim_free(wp->w_lines);
3998 wp->w_lines = NULL;
3999}
4000
4001/*
4002 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004003 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 */
4005 void
4006shell_new_rows()
4007{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004008 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 if (firstwin == NULL) /* not initialized yet */
4011 return;
4012#ifdef FEAT_WINDOWS
4013 if (h < frame_minheight(topframe, NULL))
4014 h = frame_minheight(topframe, NULL);
4015 /* First try setting the heights of windows without 'winfixheight'. If
4016 * that doesn't result in the right height, forget about that option. */
4017 frame_new_height(topframe, h, FALSE, TRUE);
4018 if (topframe->fr_height != h)
4019 frame_new_height(topframe, h, FALSE, FALSE);
4020
4021 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4022#else
4023 if (h < 1)
4024 h = 1;
4025 win_new_height(firstwin, h);
4026#endif
4027 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004028#ifdef FEAT_WINDOWS
4029 p_ch_used = p_ch;
4030#endif
4031
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032#if 0
4033 /* Disabled: don't want making the screen smaller make a window larger. */
4034 if (p_ea)
4035 win_equal(curwin, FALSE, 'v');
4036#endif
4037}
4038
4039#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4040/*
4041 * Called from win_new_shellsize() after Columns changed.
4042 */
4043 void
4044shell_new_columns()
4045{
4046 if (firstwin == NULL) /* not initialized yet */
4047 return;
4048 frame_new_width(topframe, (int)Columns, FALSE);
4049 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4050#if 0
4051 /* Disabled: don't want making the screen smaller make a window larger. */
4052 if (p_ea)
4053 win_equal(curwin, FALSE, 'h');
4054#endif
4055}
4056#endif
4057
4058#if defined(FEAT_CMDWIN) || defined(PROTO)
4059/*
4060 * Save the size of all windows in "gap".
4061 */
4062 void
4063win_size_save(gap)
4064 garray_T *gap;
4065
4066{
4067 win_T *wp;
4068
4069 ga_init2(gap, (int)sizeof(int), 1);
4070 if (ga_grow(gap, win_count() * 2) == OK)
4071 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4072 {
4073 ((int *)gap->ga_data)[gap->ga_len++] =
4074 wp->w_width + wp->w_vsep_width;
4075 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4076 }
4077}
4078
4079/*
4080 * Restore window sizes, but only if the number of windows is still the same.
4081 * Does not free the growarray.
4082 */
4083 void
4084win_size_restore(gap)
4085 garray_T *gap;
4086{
4087 win_T *wp;
4088 int i;
4089
4090 if (win_count() * 2 == gap->ga_len)
4091 {
4092 i = 0;
4093 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4094 {
4095 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4096 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4097 }
4098 /* recompute the window positions */
4099 (void)win_comp_pos();
4100 }
4101}
4102#endif /* FEAT_CMDWIN */
4103
4104#if defined(FEAT_WINDOWS) || defined(PROTO)
4105/*
4106 * Update the position for all windows, using the width and height of the
4107 * frames.
4108 * Returns the row just after the last window.
4109 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004110 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111win_comp_pos()
4112{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004113 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 int col = 0;
4115
4116 frame_comp_pos(topframe, &row, &col);
4117 return row;
4118}
4119
4120/*
4121 * Update the position of the windows in frame "topfrp", using the width and
4122 * height of the frames.
4123 * "*row" and "*col" are the top-left position of the frame. They are updated
4124 * to the bottom-right position plus one.
4125 */
4126 static void
4127frame_comp_pos(topfrp, row, col)
4128 frame_T *topfrp;
4129 int *row;
4130 int *col;
4131{
4132 win_T *wp;
4133 frame_T *frp;
4134#ifdef FEAT_VERTSPLIT
4135 int startcol;
4136 int startrow;
4137#endif
4138
4139 wp = topfrp->fr_win;
4140 if (wp != NULL)
4141 {
4142 if (wp->w_winrow != *row
4143#ifdef FEAT_VERTSPLIT
4144 || wp->w_wincol != *col
4145#endif
4146 )
4147 {
4148 /* position changed, redraw */
4149 wp->w_winrow = *row;
4150#ifdef FEAT_VERTSPLIT
4151 wp->w_wincol = *col;
4152#endif
4153 redraw_win_later(wp, NOT_VALID);
4154 wp->w_redr_status = TRUE;
4155 }
4156 *row += wp->w_height + wp->w_status_height;
4157#ifdef FEAT_VERTSPLIT
4158 *col += wp->w_width + wp->w_vsep_width;
4159#endif
4160 }
4161 else
4162 {
4163#ifdef FEAT_VERTSPLIT
4164 startrow = *row;
4165 startcol = *col;
4166#endif
4167 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4168 {
4169#ifdef FEAT_VERTSPLIT
4170 if (topfrp->fr_layout == FR_ROW)
4171 *row = startrow; /* all frames are at the same row */
4172 else
4173 *col = startcol; /* all frames are at the same col */
4174#endif
4175 frame_comp_pos(frp, row, col);
4176 }
4177 }
4178}
4179
4180#endif /* FEAT_WINDOWS */
4181
4182/*
4183 * Set current window height and take care of repositioning other windows to
4184 * fit around it.
4185 */
4186 void
4187win_setheight(height)
4188 int height;
4189{
4190 win_setheight_win(height, curwin);
4191}
4192
4193/*
4194 * Set the window height of window "win" and take care of repositioning other
4195 * windows to fit around it.
4196 */
4197 void
4198win_setheight_win(height, win)
4199 int height;
4200 win_T *win;
4201{
4202 int row;
4203
4204 if (win == curwin)
4205 {
4206 /* Always keep current window at least one line high, even when
4207 * 'winminheight' is zero. */
4208#ifdef FEAT_WINDOWS
4209 if (height < p_wmh)
4210 height = p_wmh;
4211#endif
4212 if (height == 0)
4213 height = 1;
4214 }
4215
4216#ifdef FEAT_WINDOWS
4217 frame_setheight(win->w_frame, height + win->w_status_height);
4218
4219 /* recompute the window positions */
4220 row = win_comp_pos();
4221#else
4222 if (height > topframe->fr_height)
4223 height = topframe->fr_height;
4224 win->w_height = height;
4225 row = height;
4226#endif
4227
4228 /*
4229 * If there is extra space created between the last window and the command
4230 * line, clear it.
4231 */
4232 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4233 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4234 cmdline_row = row;
4235 msg_row = row;
4236 msg_col = 0;
4237
4238 redraw_all_later(NOT_VALID);
4239}
4240
4241#if defined(FEAT_WINDOWS) || defined(PROTO)
4242
4243/*
4244 * Set the height of a frame to "height" and take care that all frames and
4245 * windows inside it are resized. Also resize frames on the left and right if
4246 * the are in the same FR_ROW frame.
4247 *
4248 * Strategy:
4249 * If the frame is part of a FR_COL frame, try fitting the frame in that
4250 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4251 * go to containing frames to resize them and make room.
4252 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4253 * Check for the minimal height of the FR_ROW frame.
4254 * At the top level we can also use change the command line height.
4255 */
4256 static void
4257frame_setheight(curfrp, height)
4258 frame_T *curfrp;
4259 int height;
4260{
4261 int room; /* total number of lines available */
4262 int take; /* number of lines taken from other windows */
4263 int room_cmdline; /* lines available from cmdline */
4264 int run;
4265 frame_T *frp;
4266 int h;
4267 int room_reserved;
4268
4269 /* If the height already is the desired value, nothing to do. */
4270 if (curfrp->fr_height == height)
4271 return;
4272
4273 if (curfrp->fr_parent == NULL)
4274 {
4275 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004276 if (height > ROWS_AVAIL)
4277 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 if (height > 0)
4279 frame_new_height(curfrp, height, FALSE, FALSE);
4280 }
4281 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4282 {
4283 /* Row of frames: Also need to resize frames left and right of this
4284 * one. First check for the minimal height of these. */
4285 h = frame_minheight(curfrp->fr_parent, NULL);
4286 if (height < h)
4287 height = h;
4288 frame_setheight(curfrp->fr_parent, height);
4289 }
4290 else
4291 {
4292 /*
4293 * Column of frames: try to change only frames in this column.
4294 */
4295#ifdef FEAT_VERTSPLIT
4296 /*
4297 * Do this twice:
4298 * 1: compute room available, if it's not enough try resizing the
4299 * containing frame.
4300 * 2: compute the room available and adjust the height to it.
4301 * Try not to reduce the height of a window with 'winfixheight' set.
4302 */
4303 for (run = 1; run <= 2; ++run)
4304#else
4305 for (;;)
4306#endif
4307 {
4308 room = 0;
4309 room_reserved = 0;
4310 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4311 frp = frp->fr_next)
4312 {
4313 if (frp != curfrp
4314 && frp->fr_win != NULL
4315 && frp->fr_win->w_p_wfh)
4316 room_reserved += frp->fr_height;
4317 room += frp->fr_height;
4318 if (frp != curfrp)
4319 room -= frame_minheight(frp, NULL);
4320 }
4321#ifdef FEAT_VERTSPLIT
4322 if (curfrp->fr_width != Columns)
4323 room_cmdline = 0;
4324 else
4325#endif
4326 {
4327 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4328 + lastwin->w_height + lastwin->w_status_height);
4329 if (room_cmdline < 0)
4330 room_cmdline = 0;
4331 }
4332
4333 if (height <= room + room_cmdline)
4334 break;
4335#ifdef FEAT_VERTSPLIT
4336 if (run == 2 || curfrp->fr_width == Columns)
4337#endif
4338 {
4339 if (height > room + room_cmdline)
4340 height = room + room_cmdline;
4341 break;
4342 }
4343#ifdef FEAT_VERTSPLIT
4344 frame_setheight(curfrp->fr_parent, height
4345 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4346#endif
4347 /*NOTREACHED*/
4348 }
4349
4350 /*
4351 * Compute the number of lines we will take from others frames (can be
4352 * negative!).
4353 */
4354 take = height - curfrp->fr_height;
4355
4356 /* If there is not enough room, also reduce the height of a window
4357 * with 'winfixheight' set. */
4358 if (height > room + room_cmdline - room_reserved)
4359 room_reserved = room + room_cmdline - height;
4360 /* If there is only a 'winfixheight' window and making the
4361 * window smaller, need to make the other window taller. */
4362 if (take < 0 && room - curfrp->fr_height < room_reserved)
4363 room_reserved = 0;
4364
4365 if (take > 0 && room_cmdline > 0)
4366 {
4367 /* use lines from cmdline first */
4368 if (take < room_cmdline)
4369 room_cmdline = take;
4370 take -= room_cmdline;
4371 topframe->fr_height += room_cmdline;
4372 }
4373
4374 /*
4375 * set the current frame to the new height
4376 */
4377 frame_new_height(curfrp, height, FALSE, FALSE);
4378
4379 /*
4380 * First take lines from the frames after the current frame. If
4381 * that is not enough, takes lines from frames above the current
4382 * frame.
4383 */
4384 for (run = 0; run < 2; ++run)
4385 {
4386 if (run == 0)
4387 frp = curfrp->fr_next; /* 1st run: start with next window */
4388 else
4389 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4390 while (frp != NULL && take != 0)
4391 {
4392 h = frame_minheight(frp, NULL);
4393 if (room_reserved > 0
4394 && frp->fr_win != NULL
4395 && frp->fr_win->w_p_wfh)
4396 {
4397 if (room_reserved >= frp->fr_height)
4398 room_reserved -= frp->fr_height;
4399 else
4400 {
4401 if (frp->fr_height - room_reserved > take)
4402 room_reserved = frp->fr_height - take;
4403 take -= frp->fr_height - room_reserved;
4404 frame_new_height(frp, room_reserved, FALSE, FALSE);
4405 room_reserved = 0;
4406 }
4407 }
4408 else
4409 {
4410 if (frp->fr_height - take < h)
4411 {
4412 take -= frp->fr_height - h;
4413 frame_new_height(frp, h, FALSE, FALSE);
4414 }
4415 else
4416 {
4417 frame_new_height(frp, frp->fr_height - take,
4418 FALSE, FALSE);
4419 take = 0;
4420 }
4421 }
4422 if (run == 0)
4423 frp = frp->fr_next;
4424 else
4425 frp = frp->fr_prev;
4426 }
4427 }
4428 }
4429}
4430
4431#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4432/*
4433 * Set current window width and take care of repositioning other windows to
4434 * fit around it.
4435 */
4436 void
4437win_setwidth(width)
4438 int width;
4439{
4440 win_setwidth_win(width, curwin);
4441}
4442
4443 void
4444win_setwidth_win(width, wp)
4445 int width;
4446 win_T *wp;
4447{
4448 /* Always keep current window at least one column wide, even when
4449 * 'winminwidth' is zero. */
4450 if (wp == curwin)
4451 {
4452 if (width < p_wmw)
4453 width = p_wmw;
4454 if (width == 0)
4455 width = 1;
4456 }
4457
4458 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4459
4460 /* recompute the window positions */
4461 (void)win_comp_pos();
4462
4463 redraw_all_later(NOT_VALID);
4464}
4465
4466/*
4467 * Set the width of a frame to "width" and take care that all frames and
4468 * windows inside it are resized. Also resize frames above and below if the
4469 * are in the same FR_ROW frame.
4470 *
4471 * Strategy is similar to frame_setheight().
4472 */
4473 static void
4474frame_setwidth(curfrp, width)
4475 frame_T *curfrp;
4476 int width;
4477{
4478 int room; /* total number of lines available */
4479 int take; /* number of lines taken from other windows */
4480 int run;
4481 frame_T *frp;
4482 int w;
4483
4484 /* If the width already is the desired value, nothing to do. */
4485 if (curfrp->fr_width == width)
4486 return;
4487
4488 if (curfrp->fr_parent == NULL)
4489 /* topframe: can't change width */
4490 return;
4491
4492 if (curfrp->fr_parent->fr_layout == FR_COL)
4493 {
4494 /* Column of frames: Also need to resize frames above and below of
4495 * this one. First check for the minimal width of these. */
4496 w = frame_minwidth(curfrp->fr_parent, NULL);
4497 if (width < w)
4498 width = w;
4499 frame_setwidth(curfrp->fr_parent, width);
4500 }
4501 else
4502 {
4503 /*
4504 * Row of frames: try to change only frames in this row.
4505 *
4506 * Do this twice:
4507 * 1: compute room available, if it's not enough try resizing the
4508 * containing frame.
4509 * 2: compute the room available and adjust the width to it.
4510 */
4511 for (run = 1; run <= 2; ++run)
4512 {
4513 room = 0;
4514 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4515 frp = frp->fr_next)
4516 {
4517 room += frp->fr_width;
4518 if (frp != curfrp)
4519 room -= frame_minwidth(frp, NULL);
4520 }
4521
4522 if (width <= room)
4523 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004524 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 {
4526 if (width > room)
4527 width = room;
4528 break;
4529 }
4530 frame_setwidth(curfrp->fr_parent, width
4531 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4532 }
4533
4534
4535 /*
4536 * Compute the number of lines we will take from others frames (can be
4537 * negative!).
4538 */
4539 take = width - curfrp->fr_width;
4540
4541 /*
4542 * set the current frame to the new width
4543 */
4544 frame_new_width(curfrp, width, FALSE);
4545
4546 /*
4547 * First take lines from the frames right of the current frame. If
4548 * that is not enough, takes lines from frames left of the current
4549 * frame.
4550 */
4551 for (run = 0; run < 2; ++run)
4552 {
4553 if (run == 0)
4554 frp = curfrp->fr_next; /* 1st run: start with next window */
4555 else
4556 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4557 while (frp != NULL && take != 0)
4558 {
4559 w = frame_minwidth(frp, NULL);
4560 if (frp->fr_width - take < w)
4561 {
4562 take -= frp->fr_width - w;
4563 frame_new_width(frp, w, FALSE);
4564 }
4565 else
4566 {
4567 frame_new_width(frp, frp->fr_width - take, FALSE);
4568 take = 0;
4569 }
4570 if (run == 0)
4571 frp = frp->fr_next;
4572 else
4573 frp = frp->fr_prev;
4574 }
4575 }
4576 }
4577}
4578#endif /* FEAT_VERTSPLIT */
4579
4580/*
4581 * Check 'winminheight' for a valid value.
4582 */
4583 void
4584win_setminheight()
4585{
4586 int room;
4587 int first = TRUE;
4588 win_T *wp;
4589
4590 /* loop until there is a 'winminheight' that is possible */
4591 while (p_wmh > 0)
4592 {
4593 /* TODO: handle vertical splits */
4594 room = -p_wh;
4595 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4596 room += wp->w_height - p_wmh;
4597 if (room >= 0)
4598 break;
4599 --p_wmh;
4600 if (first)
4601 {
4602 EMSG(_(e_noroom));
4603 first = FALSE;
4604 }
4605 }
4606}
4607
4608#ifdef FEAT_MOUSE
4609
4610/*
4611 * Status line of dragwin is dragged "offset" lines down (negative is up).
4612 */
4613 void
4614win_drag_status_line(dragwin, offset)
4615 win_T *dragwin;
4616 int offset;
4617{
4618 frame_T *curfr;
4619 frame_T *fr;
4620 int room;
4621 int row;
4622 int up; /* if TRUE, drag status line up, otherwise down */
4623 int n;
4624
4625 fr = dragwin->w_frame;
4626 curfr = fr;
4627 if (fr != topframe) /* more than one window */
4628 {
4629 fr = fr->fr_parent;
4630 /* When the parent frame is not a column of frames, its parent should
4631 * be. */
4632 if (fr->fr_layout != FR_COL)
4633 {
4634 curfr = fr;
4635 if (fr != topframe) /* only a row of windows, may drag statusline */
4636 fr = fr->fr_parent;
4637 }
4638 }
4639
4640 /* If this is the last frame in a column, may want to resize the parent
4641 * frame instead (go two up to skip a row of frames). */
4642 while (curfr != topframe && curfr->fr_next == NULL)
4643 {
4644 if (fr != topframe)
4645 fr = fr->fr_parent;
4646 curfr = fr;
4647 if (fr != topframe)
4648 fr = fr->fr_parent;
4649 }
4650
4651 if (offset < 0) /* drag up */
4652 {
4653 up = TRUE;
4654 offset = -offset;
4655 /* sum up the room of the current frame and above it */
4656 if (fr == curfr)
4657 {
4658 /* only one window */
4659 room = fr->fr_height - frame_minheight(fr, NULL);
4660 }
4661 else
4662 {
4663 room = 0;
4664 for (fr = fr->fr_child; ; fr = fr->fr_next)
4665 {
4666 room += fr->fr_height - frame_minheight(fr, NULL);
4667 if (fr == curfr)
4668 break;
4669 }
4670 }
4671 fr = curfr->fr_next; /* put fr at frame that grows */
4672 }
4673 else /* drag down */
4674 {
4675 up = FALSE;
4676 /*
4677 * Only dragging the last status line can reduce p_ch.
4678 */
4679 room = Rows - cmdline_row;
4680 if (curfr->fr_next == NULL)
4681 room -= 1;
4682 else
4683 room -= p_ch;
4684 if (room < 0)
4685 room = 0;
4686 /* sum up the room of frames below of the current one */
4687 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4688 room += fr->fr_height - frame_minheight(fr, NULL);
4689 fr = curfr; /* put fr at window that grows */
4690 }
4691
4692 if (room < offset) /* Not enough room */
4693 offset = room; /* Move as far as we can */
4694 if (offset <= 0)
4695 return;
4696
4697 /*
4698 * Grow frame fr by "offset" lines.
4699 * Doesn't happen when dragging the last status line up.
4700 */
4701 if (fr != NULL)
4702 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
4703
4704 if (up)
4705 fr = curfr; /* current frame gets smaller */
4706 else
4707 fr = curfr->fr_next; /* next frame gets smaller */
4708
4709 /*
4710 * Now make the other frames smaller.
4711 */
4712 while (fr != NULL && offset > 0)
4713 {
4714 n = frame_minheight(fr, NULL);
4715 if (fr->fr_height - offset <= n)
4716 {
4717 offset -= fr->fr_height - n;
4718 frame_new_height(fr, n, !up, FALSE);
4719 }
4720 else
4721 {
4722 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
4723 break;
4724 }
4725 if (up)
4726 fr = fr->fr_prev;
4727 else
4728 fr = fr->fr_next;
4729 }
4730 row = win_comp_pos();
4731 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4732 cmdline_row = row;
4733 p_ch = Rows - cmdline_row;
4734 if (p_ch < 1)
4735 p_ch = 1;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004736 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 showmode();
4738}
4739
4740#ifdef FEAT_VERTSPLIT
4741/*
4742 * Separator line of dragwin is dragged "offset" lines right (negative is left).
4743 */
4744 void
4745win_drag_vsep_line(dragwin, offset)
4746 win_T *dragwin;
4747 int offset;
4748{
4749 frame_T *curfr;
4750 frame_T *fr;
4751 int room;
4752 int left; /* if TRUE, drag separator line left, otherwise right */
4753 int n;
4754
4755 fr = dragwin->w_frame;
4756 if (fr == topframe) /* only one window (cannot happe?) */
4757 return;
4758 curfr = fr;
4759 fr = fr->fr_parent;
4760 /* When the parent frame is not a row of frames, its parent should be. */
4761 if (fr->fr_layout != FR_ROW)
4762 {
4763 if (fr == topframe) /* only a column of windows (cannot happen?) */
4764 return;
4765 curfr = fr;
4766 fr = fr->fr_parent;
4767 }
4768
4769 /* If this is the last frame in a row, may want to resize a parent
4770 * frame instead. */
4771 while (curfr->fr_next == NULL)
4772 {
4773 if (fr == topframe)
4774 break;
4775 curfr = fr;
4776 fr = fr->fr_parent;
4777 if (fr != topframe)
4778 {
4779 curfr = fr;
4780 fr = fr->fr_parent;
4781 }
4782 }
4783
4784 if (offset < 0) /* drag left */
4785 {
4786 left = TRUE;
4787 offset = -offset;
4788 /* sum up the room of the current frame and left of it */
4789 room = 0;
4790 for (fr = fr->fr_child; ; fr = fr->fr_next)
4791 {
4792 room += fr->fr_width - frame_minwidth(fr, NULL);
4793 if (fr == curfr)
4794 break;
4795 }
4796 fr = curfr->fr_next; /* put fr at frame that grows */
4797 }
4798 else /* drag right */
4799 {
4800 left = FALSE;
4801 /* sum up the room of frames right of the current one */
4802 room = 0;
4803 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4804 room += fr->fr_width - frame_minwidth(fr, NULL);
4805 fr = curfr; /* put fr at window that grows */
4806 }
4807
4808 if (room < offset) /* Not enough room */
4809 offset = room; /* Move as far as we can */
4810 if (offset <= 0) /* No room at all, quit. */
4811 return;
4812
4813 /* grow frame fr by offset lines */
4814 frame_new_width(fr, fr->fr_width + offset, left);
4815
4816 /* shrink other frames: current and at the left or at the right */
4817 if (left)
4818 fr = curfr; /* current frame gets smaller */
4819 else
4820 fr = curfr->fr_next; /* next frame gets smaller */
4821
4822 while (fr != NULL && offset > 0)
4823 {
4824 n = frame_minwidth(fr, NULL);
4825 if (fr->fr_width - offset <= n)
4826 {
4827 offset -= fr->fr_width - n;
4828 frame_new_width(fr, n, !left);
4829 }
4830 else
4831 {
4832 frame_new_width(fr, fr->fr_width - offset, !left);
4833 break;
4834 }
4835 if (left)
4836 fr = fr->fr_prev;
4837 else
4838 fr = fr->fr_next;
4839 }
4840 (void)win_comp_pos();
4841 redraw_all_later(NOT_VALID);
4842}
4843#endif /* FEAT_VERTSPLIT */
4844#endif /* FEAT_MOUSE */
4845
4846#endif /* FEAT_WINDOWS */
4847
4848/*
4849 * Set the height of a window.
4850 * This takes care of the things inside the window, not what happens to the
4851 * window position, the frame or to other windows.
4852 */
4853 static void
4854win_new_height(wp, height)
4855 win_T *wp;
4856 int height;
4857{
4858 linenr_T lnum;
Bram Moolenaar34114692005-01-02 11:28:13 +00004859 linenr_T bot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 int sline, line_size;
Bram Moolenaar34114692005-01-02 11:28:13 +00004861 int space;
4862 int did_below = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863#define FRACTION_MULT 16384L
4864
4865 /* Don't want a negative height. Happens when splitting a tiny window.
4866 * Will equalize heights soon to fix it. */
4867 if (height < 0)
4868 height = 0;
4869
4870 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
4871 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
4872 + FRACTION_MULT / 2) / (long)wp->w_height;
4873
4874 wp->w_height = height;
4875 wp->w_skipcol = 0;
4876
4877 /* Don't change w_topline when height is zero. Don't set w_topline when
4878 * 'scrollbind' is set and this isn't the current window. */
4879 if (height > 0
4880#ifdef FEAT_SCROLLBIND
4881 && (!wp->w_p_scb || wp == curwin)
4882#endif
4883 )
4884 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004885 /*
4886 * Find a value for w_topline that shows the cursor at the same
4887 * relative position in the window as before (more or less).
4888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 lnum = wp->w_cursor.lnum;
4890 if (lnum < 1) /* can happen when starting up */
4891 lnum = 1;
4892 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
4893 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
4894 sline = wp->w_wrow - line_size;
4895 if (sline < 0)
4896 {
4897 /*
4898 * Cursor line would go off top of screen if w_wrow was this high.
4899 */
4900 wp->w_wrow = line_size;
4901 }
4902 else
4903 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004904 space = height;
4905 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004907 space -= line_size;
4908 if (space > 0 && sline <= 0 && !did_below)
4909 {
4910 /* Try to use "~" lines below the text to avoid that text
4911 * is above the window while there are empty lines.
4912 * Subtract the rows below the cursor from "space" and
4913 * give the rest to "sline". */
4914 did_below = TRUE;
4915 bot = wp->w_cursor.lnum;
4916 while (space > 0)
4917 {
4918 if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
4919 space = 0;
4920 else
4921 {
4922#ifdef FEAT_FOLDING
4923 hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
4924#endif
4925 if (bot >= wp->w_buffer->b_ml.ml_line_count)
4926 break;
4927 ++bot;
4928 space -= plines_win(wp, bot, TRUE);
4929 }
4930 }
4931 if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
4932 sline += space;
4933 }
4934 if (sline <= 0)
4935 break;
4936
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937#ifdef FEAT_FOLDING
4938 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
4939 if (lnum == 1)
4940 {
4941 /* first line in buffer is folded */
4942 line_size = 1;
4943 --sline;
4944 break;
4945 }
4946#endif
4947 --lnum;
4948#ifdef FEAT_DIFF
4949 if (lnum == wp->w_topline)
4950 line_size = plines_win_nofill(wp, lnum, TRUE)
4951 + wp->w_topfill;
4952 else
4953#endif
4954 line_size = plines_win(wp, lnum, TRUE);
4955 sline -= line_size;
4956 }
Bram Moolenaar34114692005-01-02 11:28:13 +00004957
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 if (sline < 0)
4959 {
4960 /*
4961 * Line we want at top would go off top of screen. Use next
4962 * line instead.
4963 */
4964#ifdef FEAT_FOLDING
4965 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
4966#endif
4967 lnum++;
4968 wp->w_wrow -= line_size + sline;
4969 }
4970 else if (sline > 0)
4971 {
4972 /* First line of file reached, use that as topline. */
4973 lnum = 1;
4974 wp->w_wrow -= sline;
4975 }
4976 }
4977 set_topline(wp, lnum);
4978 }
4979
4980 if (wp == curwin)
4981 {
4982 if (p_so)
4983 update_topline();
4984 curs_columns(FALSE); /* validate w_wrow */
4985 }
4986 wp->w_prev_fraction_row = wp->w_wrow;
4987
4988 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004989 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990#ifdef FEAT_WINDOWS
4991 wp->w_redr_status = TRUE;
4992#endif
4993 invalidate_botline_win(wp);
4994}
4995
4996#ifdef FEAT_VERTSPLIT
4997/*
4998 * Set the width of a window.
4999 */
5000 static void
5001win_new_width(wp, width)
5002 win_T *wp;
5003 int width;
5004{
5005 wp->w_width = width;
5006 wp->w_lines_valid = 0;
5007 changed_line_abv_curs_win(wp);
5008 invalidate_botline_win(wp);
5009 if (wp == curwin)
5010 {
5011 update_topline();
5012 curs_columns(TRUE); /* validate w_wrow */
5013 }
5014 redraw_win_later(wp, NOT_VALID);
5015 wp->w_redr_status = TRUE;
5016}
5017#endif
5018
5019 void
5020win_comp_scroll(wp)
5021 win_T *wp;
5022{
5023 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5024 if (wp->w_p_scr == 0)
5025 wp->w_p_scr = 1;
5026}
5027
5028/*
5029 * command_height: called whenever p_ch has been changed
5030 */
5031 void
5032command_height(old_p_ch)
5033 long old_p_ch;
5034{
5035#ifdef FEAT_WINDOWS
5036 int h;
5037 frame_T *frp;
5038
Bram Moolenaar05159a02005-02-26 23:04:13 +00005039 /* When passed a negative value use the value of p_ch that we remembered.
5040 * This is needed for when the GUI starts up, we can't be sure in what
5041 * order things happen. */
5042 if (old_p_ch < 0)
5043 old_p_ch = p_ch_used;
5044 p_ch_used = p_ch;
5045
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 /* Find bottom frame with width of screen. */
5047 frp = lastwin->w_frame;
5048# ifdef FEAT_VERTSPLIT
5049 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5050 frp = frp->fr_parent;
5051# endif
5052
5053 /* Avoid changing the height of a window with 'winfixheight' set. */
5054 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5055 && frp->fr_win->w_p_wfh)
5056 frp = frp->fr_prev;
5057
5058 if (starting != NO_SCREEN)
5059 {
5060 cmdline_row = Rows - p_ch;
5061
5062 if (p_ch > old_p_ch) /* p_ch got bigger */
5063 {
5064 while (p_ch > old_p_ch)
5065 {
5066 if (frp == NULL)
5067 {
5068 EMSG(_(e_noroom));
5069 p_ch = old_p_ch;
5070 cmdline_row = Rows - p_ch;
5071 break;
5072 }
5073 h = frp->fr_height - frame_minheight(frp, NULL);
5074 if (h > p_ch - old_p_ch)
5075 h = p_ch - old_p_ch;
5076 old_p_ch += h;
5077 frame_add_height(frp, -h);
5078 frp = frp->fr_prev;
5079 }
5080
5081 /* Recompute window positions. */
5082 (void)win_comp_pos();
5083
5084 /* clear the lines added to cmdline */
5085 if (full_screen)
5086 screen_fill((int)(cmdline_row), (int)Rows, 0,
5087 (int)Columns, ' ', ' ', 0);
5088 msg_row = cmdline_row;
5089 redraw_cmdline = TRUE;
5090 return;
5091 }
5092
5093 if (msg_row < cmdline_row)
5094 msg_row = cmdline_row;
5095 redraw_cmdline = TRUE;
5096 }
5097 frame_add_height(frp, (int)(old_p_ch - p_ch));
5098
5099 /* Recompute window positions. */
5100 if (frp != lastwin->w_frame)
5101 (void)win_comp_pos();
5102#else
5103 win_setheight((int)(firstwin->w_height + old_p_ch - p_ch));
5104 cmdline_row = Rows - p_ch;
5105#endif
5106}
5107
5108#if defined(FEAT_WINDOWS) || defined(PROTO)
5109/*
5110 * Resize frame "frp" to be "n" lines higher (negative for less high).
5111 * Also resize the frames it is contained in.
5112 */
5113 static void
5114frame_add_height(frp, n)
5115 frame_T *frp;
5116 int n;
5117{
5118 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5119 for (;;)
5120 {
5121 frp = frp->fr_parent;
5122 if (frp == NULL)
5123 break;
5124 frp->fr_height += n;
5125 }
5126}
5127
5128/*
5129 * Add or remove a status line for the bottom window(s), according to the
5130 * value of 'laststatus'.
5131 */
5132 void
5133last_status(morewin)
5134 int morewin; /* pretend there are two or more windows */
5135{
5136 /* Don't make a difference between horizontal or vertical split. */
5137 last_status_rec(topframe, (p_ls == 2
5138 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5139}
5140
5141 static void
5142last_status_rec(fr, statusline)
5143 frame_T *fr;
5144 int statusline;
5145{
5146 frame_T *fp;
5147 win_T *wp;
5148
5149 if (fr->fr_layout == FR_LEAF)
5150 {
5151 wp = fr->fr_win;
5152 if (wp->w_status_height != 0 && !statusline)
5153 {
5154 /* remove status line */
5155 win_new_height(wp, wp->w_height + 1);
5156 wp->w_status_height = 0;
5157 comp_col();
5158 }
5159 else if (wp->w_status_height == 0 && statusline)
5160 {
5161 /* Find a frame to take a line from. */
5162 fp = fr;
5163 while (fp->fr_height <= frame_minheight(fp, NULL))
5164 {
5165 if (fp == topframe)
5166 {
5167 EMSG(_(e_noroom));
5168 return;
5169 }
5170 /* In a column of frames: go to frame above. If already at
5171 * the top or in a row of frames: go to parent. */
5172 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5173 fp = fp->fr_prev;
5174 else
5175 fp = fp->fr_parent;
5176 }
5177 wp->w_status_height = 1;
5178 if (fp != fr)
5179 {
5180 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5181 frame_fix_height(wp);
5182 (void)win_comp_pos();
5183 }
5184 else
5185 win_new_height(wp, wp->w_height - 1);
5186 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005187 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
5189 }
5190#ifdef FEAT_VERTSPLIT
5191 else if (fr->fr_layout == FR_ROW)
5192 {
5193 /* vertically split windows, set status line for each one */
5194 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5195 last_status_rec(fp, statusline);
5196 }
5197#endif
5198 else
5199 {
5200 /* horizontally split window, set status line for last one */
5201 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5202 ;
5203 last_status_rec(fp, statusline);
5204 }
5205}
5206
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005207/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005208 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005209 */
5210 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005211tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005212{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005213#ifdef FEAT_GUI_TABLINE
5214 /* When the GUI has the tabline then this always returns zero. */
5215 if (gui_use_tabline())
5216 return 0;
5217#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005218 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005219 {
5220 case 0: return 0;
5221 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5222 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005223 return 1;
5224}
5225
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226#endif /* FEAT_WINDOWS */
5227
5228#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5229/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005230 * Get the file name at the cursor.
5231 * If Visual mode is active, use the selected text if it's in one line.
5232 * Returns the name in allocated memory, NULL for failure.
5233 */
5234 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005235grab_file_name(count, file_lnum)
5236 long count;
5237 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005238{
5239# ifdef FEAT_VISUAL
5240 if (VIsual_active)
5241 {
5242 int len;
5243 char_u *ptr;
5244
5245 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5246 return NULL;
5247 return find_file_name_in_path(ptr, len,
5248 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5249 }
5250# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005251 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5252 file_lnum);
5253
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005254}
5255
5256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 * Return the file name under or after the cursor.
5258 *
5259 * The 'path' option is searched if the file name is not absolute.
5260 * The string returned has been alloc'ed and should be freed by the caller.
5261 * NULL is returned if the file name or file is not found.
5262 *
5263 * options:
5264 * FNAME_MESS give error messages
5265 * FNAME_EXP expand to path
5266 * FNAME_HYP check for hypertext link
5267 * FNAME_INCL apply "includeexpr"
5268 */
5269 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005270file_name_at_cursor(options, count, file_lnum)
5271 int options;
5272 long count;
5273 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274{
5275 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005276 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5277 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278}
5279
5280/*
5281 * Return the name of the file under or after ptr[col].
5282 * Otherwise like file_name_at_cursor().
5283 */
5284 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005285file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 char_u *line;
5287 int col;
5288 int options;
5289 long count;
5290 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005291 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292{
5293 char_u *ptr;
5294 int len;
5295
5296 /*
5297 * search forward for what could be the start of a file name
5298 */
5299 ptr = line + col;
5300 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005301 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 if (*ptr == NUL) /* nothing found */
5303 {
5304 if (options & FNAME_MESS)
5305 EMSG(_("E446: No file name under cursor"));
5306 return NULL;
5307 }
5308
5309 /*
5310 * Search backward for first char of the file name.
5311 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5312 */
5313 while (ptr > line)
5314 {
5315#ifdef FEAT_MBYTE
5316 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5317 ptr -= len + 1;
5318 else
5319#endif
5320 if (vim_isfilec(ptr[-1])
5321 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5322 --ptr;
5323 else
5324 break;
5325 }
5326
5327 /*
5328 * Search forward for the last char of the file name.
5329 * Also allow "://" when ':' is not in 'isfname'.
5330 */
5331 len = 0;
5332 while (vim_isfilec(ptr[len])
5333 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5334#ifdef FEAT_MBYTE
5335 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005336 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 else
5338#endif
5339 ++len;
5340
5341 /*
5342 * If there is trailing punctuation, remove it.
5343 * But don't remove "..", could be a directory name.
5344 */
5345 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5346 && ptr[len - 2] != '.')
5347 --len;
5348
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005349 if (file_lnum != NULL)
5350 {
5351 char_u *p;
5352
5353 /* Get the number after the file name and a separator character */
5354 p = ptr + len;
5355 p = skipwhite(p);
5356 if (*p != NUL)
5357 {
5358 if (!isdigit(*p))
5359 ++p; /* skip the separator */
5360 p = skipwhite(p);
5361 if (isdigit(*p))
5362 *file_lnum = (int)getdigits(&p);
5363 }
5364 }
5365
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5367}
5368
5369# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5370static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5371
5372 static char_u *
5373eval_includeexpr(ptr, len)
5374 char_u *ptr;
5375 int len;
5376{
5377 char_u *res;
5378
5379 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005380 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005381 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 set_vim_var_string(VV_FNAME, NULL, 0);
5383 return res;
5384}
5385#endif
5386
5387/*
5388 * Return the name of the file ptr[len] in 'path'.
5389 * Otherwise like file_name_at_cursor().
5390 */
5391 char_u *
5392find_file_name_in_path(ptr, len, options, count, rel_fname)
5393 char_u *ptr;
5394 int len;
5395 int options;
5396 long count;
5397 char_u *rel_fname; /* file we are searching relative to */
5398{
5399 char_u *file_name;
5400 int c;
5401# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5402 char_u *tofree = NULL;
5403
5404 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5405 {
5406 tofree = eval_includeexpr(ptr, len);
5407 if (tofree != NULL)
5408 {
5409 ptr = tofree;
5410 len = (int)STRLEN(ptr);
5411 }
5412 }
5413# endif
5414
5415 if (options & FNAME_EXP)
5416 {
5417 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5418 TRUE, rel_fname);
5419
5420# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5421 /*
5422 * If the file could not be found in a normal way, try applying
5423 * 'includeexpr' (unless done already).
5424 */
5425 if (file_name == NULL
5426 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5427 {
5428 tofree = eval_includeexpr(ptr, len);
5429 if (tofree != NULL)
5430 {
5431 ptr = tofree;
5432 len = (int)STRLEN(ptr);
5433 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5434 TRUE, rel_fname);
5435 }
5436 }
5437# endif
5438 if (file_name == NULL && (options & FNAME_MESS))
5439 {
5440 c = ptr[len];
5441 ptr[len] = NUL;
5442 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5443 ptr[len] = c;
5444 }
5445
5446 /* Repeat finding the file "count" times. This matters when it
5447 * appears several times in the path. */
5448 while (file_name != NULL && --count > 0)
5449 {
5450 vim_free(file_name);
5451 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5452 }
5453 }
5454 else
5455 file_name = vim_strnsave(ptr, len);
5456
5457# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5458 vim_free(tofree);
5459# endif
5460
5461 return file_name;
5462}
5463#endif /* FEAT_SEARCHPATH */
5464
5465/*
5466 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5467 * Also check for ":\\", which MS Internet Explorer accepts, return
5468 * URL_BACKSLASH.
5469 */
5470 static int
5471path_is_url(p)
5472 char_u *p;
5473{
5474 if (STRNCMP(p, "://", (size_t)3) == 0)
5475 return URL_SLASH;
5476 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5477 return URL_BACKSLASH;
5478 return 0;
5479}
5480
5481/*
5482 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5483 * Return URL_BACKSLASH for "name:\\".
5484 * Return zero otherwise.
5485 */
5486 int
5487path_with_url(fname)
5488 char_u *fname;
5489{
5490 char_u *p;
5491
5492 for (p = fname; isalpha(*p); ++p)
5493 ;
5494 return path_is_url(p);
5495}
5496
5497/*
5498 * Return TRUE if "name" is a full (absolute) path name or URL.
5499 */
5500 int
5501vim_isAbsName(name)
5502 char_u *name;
5503{
5504 return (path_with_url(name) != 0 || mch_isFullName(name));
5505}
5506
5507/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005508 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 *
5510 * return FAIL for failure, OK otherwise
5511 */
5512 int
5513vim_FullName(fname, buf, len, force)
5514 char_u *fname, *buf;
5515 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005516 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517{
5518 int retval = OK;
5519 int url;
5520
5521 *buf = NUL;
5522 if (fname == NULL)
5523 return FAIL;
5524
5525 url = path_with_url(fname);
5526 if (!url)
5527 retval = mch_FullName(fname, buf, len, force);
5528 if (url || retval == FAIL)
5529 {
5530 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005531 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 }
5533#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5534 slash_adjust(buf);
5535#endif
5536 return retval;
5537}
5538
5539/*
5540 * Return the minimal number of rows that is needed on the screen to display
5541 * the current number of windows.
5542 */
5543 int
5544min_rows()
5545{
5546 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005547#ifdef FEAT_WINDOWS
5548 tabpage_T *tp;
5549 int n;
5550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551
5552 if (firstwin == NULL) /* not initialized yet */
5553 return MIN_LINES;
5554
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005556 total = 0;
5557 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5558 {
5559 n = frame_minheight(tp->tp_topframe, NULL);
5560 if (total < n)
5561 total = n;
5562 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005563 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005565 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005567 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 return total;
5569}
5570
5571/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005572 * Return TRUE if there is only one window (in the current tab page), not
5573 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 */
5575 int
5576only_one_window()
5577{
5578#ifdef FEAT_WINDOWS
5579 int count = 0;
5580 win_T *wp;
5581
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005582 /* If there is another tab page there always is another window. */
5583 if (first_tabpage->tp_next != NULL)
5584 return FALSE;
5585
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005587 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588# ifdef FEAT_QUICKFIX
5589 || wp->w_p_pvw
5590# endif
5591 ) || wp == curwin)
5592 ++count;
5593 return (count <= 1);
5594#else
5595 return TRUE;
5596#endif
5597}
5598
5599#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5600/*
5601 * Correct the cursor line number in other windows. Used after changing the
5602 * current buffer, and before applying autocommands.
5603 * When "do_curwin" is TRUE, also check current window.
5604 */
5605 void
5606check_lnums(do_curwin)
5607 int do_curwin;
5608{
5609 win_T *wp;
5610
5611#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005612 tabpage_T *tp;
5613
5614 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5616#else
5617 wp = curwin;
5618 if (do_curwin)
5619#endif
5620 {
5621 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5622 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5623 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5624 wp->w_topline = curbuf->b_ml.ml_line_count;
5625 }
5626}
5627#endif
5628
5629#if defined(FEAT_WINDOWS) || defined(PROTO)
5630
5631/*
5632 * A snapshot of the window sizes, to restore them after closing the help
5633 * window.
5634 * Only these fields are used:
5635 * fr_layout
5636 * fr_width
5637 * fr_height
5638 * fr_next
5639 * fr_child
5640 * fr_win (only valid for the old curwin, NULL otherwise)
5641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642
5643/*
5644 * Create a snapshot of the current frame sizes.
5645 */
5646 static void
5647make_snapshot()
5648{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005649 clear_snapshot(curtab);
5650 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651}
5652
5653 static void
5654make_snapshot_rec(fr, frp)
5655 frame_T *fr;
5656 frame_T **frp;
5657{
5658 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5659 if (*frp == NULL)
5660 return;
5661 (*frp)->fr_layout = fr->fr_layout;
5662# ifdef FEAT_VERTSPLIT
5663 (*frp)->fr_width = fr->fr_width;
5664# endif
5665 (*frp)->fr_height = fr->fr_height;
5666 if (fr->fr_next != NULL)
5667 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
5668 if (fr->fr_child != NULL)
5669 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
5670 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
5671 (*frp)->fr_win = curwin;
5672}
5673
5674/*
5675 * Remove any existing snapshot.
5676 */
5677 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005678clear_snapshot(tp)
5679 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005681 clear_snapshot_rec(tp->tp_snapshot);
5682 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683}
5684
5685 static void
5686clear_snapshot_rec(fr)
5687 frame_T *fr;
5688{
5689 if (fr != NULL)
5690 {
5691 clear_snapshot_rec(fr->fr_next);
5692 clear_snapshot_rec(fr->fr_child);
5693 vim_free(fr);
5694 }
5695}
5696
5697/*
5698 * Restore a previously created snapshot, if there is any.
5699 * This is only done if the screen size didn't change and the window layout is
5700 * still the same.
5701 */
5702 static void
5703restore_snapshot(close_curwin)
5704 int close_curwin; /* closing current window */
5705{
5706 win_T *wp;
5707
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005708 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005710 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005712 && curtab->tp_snapshot->fr_height == topframe->fr_height
5713 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005715 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 win_comp_pos();
5717 if (wp != NULL && close_curwin)
5718 win_goto(wp);
5719 redraw_all_later(CLEAR);
5720 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005721 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722}
5723
5724/*
5725 * Check if frames "sn" and "fr" have the same layout, same following frames
5726 * and same children.
5727 */
5728 static int
5729check_snapshot_rec(sn, fr)
5730 frame_T *sn;
5731 frame_T *fr;
5732{
5733 if (sn->fr_layout != fr->fr_layout
5734 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
5735 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
5736 || (sn->fr_next != NULL
5737 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
5738 || (sn->fr_child != NULL
5739 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
5740 return FAIL;
5741 return OK;
5742}
5743
5744/*
5745 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
5746 * following frames and children.
5747 * Returns a pointer to the old current window, or NULL.
5748 */
5749 static win_T *
5750restore_snapshot_rec(sn, fr)
5751 frame_T *sn;
5752 frame_T *fr;
5753{
5754 win_T *wp = NULL;
5755 win_T *wp2;
5756
5757 fr->fr_height = sn->fr_height;
5758# ifdef FEAT_VERTSPLIT
5759 fr->fr_width = sn->fr_width;
5760# endif
5761 if (fr->fr_layout == FR_LEAF)
5762 {
5763 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
5764# ifdef FEAT_VERTSPLIT
5765 frame_new_width(fr, fr->fr_width, FALSE);
5766# endif
5767 wp = sn->fr_win;
5768 }
5769 if (sn->fr_next != NULL)
5770 {
5771 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
5772 if (wp2 != NULL)
5773 wp = wp2;
5774 }
5775 if (sn->fr_child != NULL)
5776 {
5777 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
5778 if (wp2 != NULL)
5779 wp = wp2;
5780 }
5781 return wp;
5782}
5783
5784#endif
5785
5786#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5787/*
5788 * Return TRUE if there is any vertically split window.
5789 */
5790 int
5791win_hasvertsplit()
5792{
5793 frame_T *fr;
5794
5795 if (topframe->fr_layout == FR_ROW)
5796 return TRUE;
5797
5798 if (topframe->fr_layout == FR_COL)
5799 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
5800 if (fr->fr_layout == FR_ROW)
5801 return TRUE;
5802
5803 return FALSE;
5804}
5805#endif