blob: 548113dfc087511080d385c11c1115d82329578b [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 Moolenaar1d2ba7f2006-02-14 22:29:30 +000089# define ROWS_AVAIL (Rows - p_ch - tabpageline_height())
90#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{
625 /* Add flags from ":vertical", ":topleft" and ":botright". */
626 flags |= cmdmod.split;
627 if ((flags & WSP_TOP) && (flags & WSP_BOT))
628 {
629 EMSG(_("E442: Can't split topleft and botright at the same time"));
630 return FAIL;
631 }
632
633 /* When creating the help window make a snapshot of the window layout.
634 * Otherwise clear the snapshot, it's now invalid. */
635 if (flags & WSP_HELP)
636 make_snapshot();
637 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000638 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
640 return win_split_ins(size, flags, NULL, 0);
641}
642
643/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000644 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 * When "newwin" is not NULL: insert this window at the far
646 * top/left/right/bottom.
647 * return FAIL for failure, OK otherwise
648 */
649 static int
650win_split_ins(size, flags, newwin, dir)
651 int size;
652 int flags;
653 win_T *newwin;
654 int dir;
655{
656 win_T *wp = newwin;
657 win_T *oldwin;
658 int new_size = size;
659 int i;
660 int need_status = 0;
661 int do_equal = FALSE;
662 int needed;
663 int available;
664 int oldwin_height = 0;
665 int layout;
666 frame_T *frp, *curfrp;
667 int before;
668
669 if (flags & WSP_TOP)
670 oldwin = firstwin;
671 else if (flags & WSP_BOT)
672 oldwin = lastwin;
673 else
674 oldwin = curwin;
675
676 /* add a status line when p_ls == 1 and splitting the first window */
677 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
678 {
679 if (oldwin->w_height <= p_wmh && newwin == NULL)
680 {
681 EMSG(_(e_noroom));
682 return FAIL;
683 }
684 need_status = STATUS_HEIGHT;
685 }
686
687#ifdef FEAT_VERTSPLIT
688 if (flags & WSP_VERT)
689 {
690 layout = FR_ROW;
691 do_equal = (p_ea && new_size == 0 && *p_ead != 'v');
692
693 /*
694 * Check if we are able to split the current window and compute its
695 * width.
696 */
697 needed = p_wmw + 1;
698 if (flags & WSP_ROOM)
699 needed += p_wiw - p_wmw;
700 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
701 {
702 available = topframe->fr_width;
703 needed += frame_minwidth(topframe, NULL);
704 }
705 else
706 available = oldwin->w_width;
707 if (available < needed && newwin == NULL)
708 {
709 EMSG(_(e_noroom));
710 return FAIL;
711 }
712 if (new_size == 0)
713 new_size = oldwin->w_width / 2;
714 if (new_size > oldwin->w_width - p_wmw - 1)
715 new_size = oldwin->w_width - p_wmw - 1;
716 if (new_size < p_wmw)
717 new_size = p_wmw;
718
719 /* if it doesn't fit in the current window, need win_equal() */
720 if (oldwin->w_width - new_size - 1 < p_wmw)
721 do_equal = TRUE;
722 }
723 else
724#endif
725 {
726 layout = FR_COL;
727 do_equal = (p_ea && new_size == 0
728#ifdef FEAT_VERTSPLIT
729 && *p_ead != 'h'
730#endif
731 );
732
733 /*
734 * Check if we are able to split the current window and compute its
735 * height.
736 */
737 needed = p_wmh + STATUS_HEIGHT + need_status;
738 if (flags & WSP_ROOM)
739 needed += p_wh - p_wmh;
740 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
741 {
742 available = topframe->fr_height;
743 needed += frame_minheight(topframe, NULL);
744 }
745 else
746 {
747 available = oldwin->w_height;
748 needed += p_wmh;
749 }
750 if (available < needed && newwin == NULL)
751 {
752 EMSG(_(e_noroom));
753 return FAIL;
754 }
755 oldwin_height = oldwin->w_height;
756 if (need_status)
757 {
758 oldwin->w_status_height = STATUS_HEIGHT;
759 oldwin_height -= STATUS_HEIGHT;
760 }
761 if (new_size == 0)
762 new_size = oldwin_height / 2;
763
764 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
765 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
766 if (new_size < p_wmh)
767 new_size = p_wmh;
768
769 /* if it doesn't fit in the current window, need win_equal() */
770 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
771 do_equal = TRUE;
772
773 /* We don't like to take lines for the new window from a
774 * 'winfixheight' window. Take them from a window above or below
775 * instead, if possible. */
776 if (oldwin->w_p_wfh)
777 {
778 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
779 oldwin);
780 oldwin_height = oldwin->w_height;
781 if (need_status)
782 oldwin_height -= STATUS_HEIGHT;
783 }
784 }
785
786 /*
787 * allocate new window structure and link it in the window list
788 */
789 if ((flags & WSP_TOP) == 0
790 && ((flags & WSP_BOT)
791 || (flags & WSP_BELOW)
792 || (!(flags & WSP_ABOVE)
793 && (
794#ifdef FEAT_VERTSPLIT
795 (flags & WSP_VERT) ? p_spr :
796#endif
797 p_sb))))
798 {
799 /* new window below/right of current one */
800 if (newwin == NULL)
801 wp = win_alloc(oldwin);
802 else
803 win_append(oldwin, wp);
804 }
805 else
806 {
807 if (newwin == NULL)
808 wp = win_alloc(oldwin->w_prev);
809 else
810 win_append(oldwin->w_prev, wp);
811 }
812
813 if (newwin == NULL)
814 {
815 if (wp == NULL)
816 return FAIL;
817
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000818 /* make the contents of the new window the same as the current one */
819 win_init(wp, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 }
821
822 /*
823 * Reorganise the tree of frames to insert the new window.
824 */
825 if (flags & (WSP_TOP | WSP_BOT))
826 {
827#ifdef FEAT_VERTSPLIT
828 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
829 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
830#else
831 if (topframe->fr_layout == FR_COL)
832#endif
833 {
834 curfrp = topframe->fr_child;
835 if (flags & WSP_BOT)
836 while (curfrp->fr_next != NULL)
837 curfrp = curfrp->fr_next;
838 }
839 else
840 curfrp = topframe;
841 before = (flags & WSP_TOP);
842 }
843 else
844 {
845 curfrp = oldwin->w_frame;
846 if (flags & WSP_BELOW)
847 before = FALSE;
848 else if (flags & WSP_ABOVE)
849 before = TRUE;
850 else
851#ifdef FEAT_VERTSPLIT
852 if (flags & WSP_VERT)
853 before = !p_spr;
854 else
855#endif
856 before = !p_sb;
857 }
858 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
859 {
860 /* Need to create a new frame in the tree to make a branch. */
861 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
862 *frp = *curfrp;
863 curfrp->fr_layout = layout;
864 frp->fr_parent = curfrp;
865 frp->fr_next = NULL;
866 frp->fr_prev = NULL;
867 curfrp->fr_child = frp;
868 curfrp->fr_win = NULL;
869 curfrp = frp;
870 if (frp->fr_win != NULL)
871 oldwin->w_frame = frp;
872 else
873 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
874 frp->fr_parent = curfrp;
875 }
876
877 if (newwin == NULL)
878 {
879 /* Create a frame for the new window. */
880 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
881 frp->fr_layout = FR_LEAF;
882 frp->fr_win = wp;
883 wp->w_frame = frp;
884 }
885 else
886 frp = newwin->w_frame;
887 frp->fr_parent = curfrp->fr_parent;
888
889 /* Insert the new frame at the right place in the frame list. */
890 if (before)
891 frame_insert(curfrp, frp);
892 else
893 frame_append(curfrp, frp);
894
895#ifdef FEAT_VERTSPLIT
896 if (flags & WSP_VERT)
897 {
898 wp->w_p_scr = curwin->w_p_scr;
899 if (need_status)
900 {
901 --oldwin->w_height;
902 oldwin->w_status_height = need_status;
903 }
904 if (flags & (WSP_TOP | WSP_BOT))
905 {
906 /* set height and row of new window to full height */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000907 wp->w_winrow = tabpageline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 wp->w_height = curfrp->fr_height - (p_ls > 0);
909 wp->w_status_height = (p_ls > 0);
910 }
911 else
912 {
913 /* height and row of new window is same as current window */
914 wp->w_winrow = oldwin->w_winrow;
915 wp->w_height = oldwin->w_height;
916 wp->w_status_height = oldwin->w_status_height;
917 }
918 frp->fr_height = curfrp->fr_height;
919
920 /* "new_size" of the current window goes to the new window, use
921 * one column for the vertical separator */
922 wp->w_width = new_size;
923 if (before)
924 wp->w_vsep_width = 1;
925 else
926 {
927 wp->w_vsep_width = oldwin->w_vsep_width;
928 oldwin->w_vsep_width = 1;
929 }
930 if (flags & (WSP_TOP | WSP_BOT))
931 {
932 if (flags & WSP_BOT)
933 frame_add_vsep(curfrp);
934 /* Set width of neighbor frame */
935 frame_new_width(curfrp, curfrp->fr_width
936 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP);
937 }
938 else
939 oldwin->w_width -= new_size + 1;
940 if (before) /* new window left of current one */
941 {
942 wp->w_wincol = oldwin->w_wincol;
943 oldwin->w_wincol += new_size + 1;
944 }
945 else /* new window right of current one */
946 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
947 frame_fix_width(oldwin);
948 frame_fix_width(wp);
949 }
950 else
951#endif
952 {
953 /* width and column of new window is same as current window */
954#ifdef FEAT_VERTSPLIT
955 if (flags & (WSP_TOP | WSP_BOT))
956 {
957 wp->w_wincol = 0;
958 wp->w_width = Columns;
959 wp->w_vsep_width = 0;
960 }
961 else
962 {
963 wp->w_wincol = oldwin->w_wincol;
964 wp->w_width = oldwin->w_width;
965 wp->w_vsep_width = oldwin->w_vsep_width;
966 }
967 frp->fr_width = curfrp->fr_width;
968#endif
969
970 /* "new_size" of the current window goes to the new window, use
971 * one row for the status line */
972 win_new_height(wp, new_size);
973 if (flags & (WSP_TOP | WSP_BOT))
974 frame_new_height(curfrp, curfrp->fr_height
975 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
976 else
977 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
978 if (before) /* new window above current one */
979 {
980 wp->w_winrow = oldwin->w_winrow;
981 wp->w_status_height = STATUS_HEIGHT;
982 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
983 }
984 else /* new window below current one */
985 {
986 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
987 wp->w_status_height = oldwin->w_status_height;
988 oldwin->w_status_height = STATUS_HEIGHT;
989 }
990#ifdef FEAT_VERTSPLIT
991 if (flags & WSP_BOT)
992 frame_add_statusline(curfrp);
993#endif
994 frame_fix_height(wp);
995 frame_fix_height(oldwin);
996 }
997
998 if (flags & (WSP_TOP | WSP_BOT))
999 (void)win_comp_pos();
1000
1001 /*
1002 * Both windows need redrawing
1003 */
1004 redraw_win_later(wp, NOT_VALID);
1005 wp->w_redr_status = TRUE;
1006 redraw_win_later(oldwin, NOT_VALID);
1007 oldwin->w_redr_status = TRUE;
1008
1009 if (need_status)
1010 {
1011 msg_row = Rows - 1;
1012 msg_col = sc_col;
1013 msg_clr_eos_force(); /* Old command/ruler may still be there */
1014 comp_col();
1015 msg_row = Rows - 1;
1016 msg_col = 0; /* put position back at start of line */
1017 }
1018
1019 /*
1020 * make the new window the current window and redraw
1021 */
1022 if (do_equal || dir != 0)
1023 win_equal(wp, TRUE,
1024#ifdef FEAT_VERTSPLIT
1025 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1026 : dir == 'h' ? 'b' :
1027#endif
1028 'v');
1029
1030 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1031 * size was given. */
1032#ifdef FEAT_VERTSPLIT
1033 if (flags & WSP_VERT)
1034 {
1035 i = p_wiw;
1036 if (size != 0)
1037 p_wiw = size;
1038
1039# ifdef FEAT_GUI
1040 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1041 if (gui.in_use)
1042 gui_init_which_components(NULL);
1043# endif
1044 }
1045 else
1046#endif
1047 {
1048 i = p_wh;
1049 if (size != 0)
1050 p_wh = size;
1051 }
1052 win_enter(wp, FALSE);
1053#ifdef FEAT_VERTSPLIT
1054 if (flags & WSP_VERT)
1055 p_wiw = i;
1056 else
1057#endif
1058 p_wh = i;
1059
1060 return OK;
1061}
1062
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001063/*
1064 * Initialize window "newp" from window "oldp".
1065 * Used when splitting a window and when creating a new tab page.
1066 * The windows will both edit the same buffer.
1067 */
1068 static void
1069win_init(newp, oldp)
1070 win_T *newp;
1071 win_T *oldp;
1072{
1073 int i;
1074
1075 newp->w_buffer = oldp->w_buffer;
1076 oldp->w_buffer->b_nwindows++;
1077 newp->w_cursor = oldp->w_cursor;
1078 newp->w_valid = 0;
1079 newp->w_curswant = oldp->w_curswant;
1080 newp->w_set_curswant = oldp->w_set_curswant;
1081 newp->w_topline = oldp->w_topline;
1082#ifdef FEAT_DIFF
1083 newp->w_topfill = oldp->w_topfill;
1084#endif
1085 newp->w_leftcol = oldp->w_leftcol;
1086 newp->w_pcmark = oldp->w_pcmark;
1087 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1088 newp->w_alt_fnum = oldp->w_alt_fnum;
1089 newp->w_fraction = oldp->w_fraction;
1090 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1091#ifdef FEAT_JUMPLIST
1092 copy_jumplist(oldp, newp);
1093#endif
1094#ifdef FEAT_QUICKFIX
1095 copy_loclist(oldp, newp);
1096#endif
1097 if (oldp->w_localdir != NULL)
1098 newp->w_localdir = vim_strsave(oldp->w_localdir);
1099
1100 /* Use the same argument list. */
1101 newp->w_alist = oldp->w_alist;
1102 ++newp->w_alist->al_refcount;
1103 newp->w_arg_idx = oldp->w_arg_idx;
1104
1105 /*
1106 * copy tagstack and options from existing window
1107 */
1108 for (i = 0; i < oldp->w_tagstacklen; i++)
1109 {
1110 newp->w_tagstack[i] = oldp->w_tagstack[i];
1111 if (newp->w_tagstack[i].tagname != NULL)
1112 newp->w_tagstack[i].tagname =
1113 vim_strsave(newp->w_tagstack[i].tagname);
1114 }
1115 newp->w_tagstackidx = oldp->w_tagstackidx;
1116 newp->w_tagstacklen = oldp->w_tagstacklen;
1117 win_copy_options(oldp, newp);
1118# ifdef FEAT_FOLDING
1119 copyFoldingState(oldp, newp);
1120# endif
1121}
1122
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#endif /* FEAT_WINDOWS */
1124
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125#if defined(FEAT_WINDOWS) || defined(PROTO)
1126/*
1127 * Check if "win" is a pointer to an existing window.
1128 */
1129 int
1130win_valid(win)
1131 win_T *win;
1132{
1133 win_T *wp;
1134
1135 if (win == NULL)
1136 return FALSE;
1137 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1138 if (wp == win)
1139 return TRUE;
1140 return FALSE;
1141}
1142
1143/*
1144 * Return the number of windows.
1145 */
1146 int
1147win_count()
1148{
1149 win_T *wp;
1150 int count = 0;
1151
1152 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1153 ++count;
1154 return count;
1155}
1156
1157/*
1158 * Make "count" windows on the screen.
1159 * Return actual number of windows on the screen.
1160 * Must be called when there is just one window, filling the whole screen
1161 * (excluding the command line).
1162 */
1163/*ARGSUSED*/
1164 int
1165make_windows(count, vertical)
1166 int count;
1167 int vertical; /* split windows vertically if TRUE */
1168{
1169 int maxcount;
1170 int todo;
1171
1172#ifdef FEAT_VERTSPLIT
1173 if (vertical)
1174 {
1175 /* Each windows needs at least 'winminwidth' lines and a separator
1176 * column. */
1177 maxcount = (curwin->w_width + curwin->w_vsep_width
1178 - (p_wiw - p_wmw)) / (p_wmw + 1);
1179 }
1180 else
1181#endif
1182 {
1183 /* Each window needs at least 'winminheight' lines and a status line. */
1184 maxcount = (curwin->w_height + curwin->w_status_height
1185 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1186 }
1187
1188 if (maxcount < 2)
1189 maxcount = 2;
1190 if (count > maxcount)
1191 count = maxcount;
1192
1193 /*
1194 * add status line now, otherwise first window will be too big
1195 */
1196 if (count > 1)
1197 last_status(TRUE);
1198
1199#ifdef FEAT_AUTOCMD
1200 /*
1201 * Don't execute autocommands while creating the windows. Must do that
1202 * when putting the buffers in the windows.
1203 */
1204 ++autocmd_block;
1205#endif
1206
1207 /* todo is number of windows left to create */
1208 for (todo = count - 1; todo > 0; --todo)
1209#ifdef FEAT_VERTSPLIT
1210 if (vertical)
1211 {
1212 if (win_split(curwin->w_width - (curwin->w_width - todo)
1213 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1214 break;
1215 }
1216 else
1217#endif
1218 {
1219 if (win_split(curwin->w_height - (curwin->w_height - todo
1220 * STATUS_HEIGHT) / (todo + 1)
1221 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1222 break;
1223 }
1224
1225#ifdef FEAT_AUTOCMD
1226 --autocmd_block;
1227#endif
1228
1229 /* return actual number of windows */
1230 return (count - todo);
1231}
1232
1233/*
1234 * Exchange current and next window
1235 */
1236 static void
1237win_exchange(Prenum)
1238 long Prenum;
1239{
1240 frame_T *frp;
1241 frame_T *frp2;
1242 win_T *wp;
1243 win_T *wp2;
1244 int temp;
1245
1246 if (lastwin == firstwin) /* just one window */
1247 {
1248 beep_flush();
1249 return;
1250 }
1251
1252#ifdef FEAT_GUI
1253 need_mouse_correct = TRUE;
1254#endif
1255
1256 /*
1257 * find window to exchange with
1258 */
1259 if (Prenum)
1260 {
1261 frp = curwin->w_frame->fr_parent->fr_child;
1262 while (frp != NULL && --Prenum > 0)
1263 frp = frp->fr_next;
1264 }
1265 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1266 frp = curwin->w_frame->fr_next;
1267 else /* Swap last window in row/col with previous */
1268 frp = curwin->w_frame->fr_prev;
1269
1270 /* We can only exchange a window with another window, not with a frame
1271 * containing windows. */
1272 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1273 return;
1274 wp = frp->fr_win;
1275
1276/*
1277 * 1. remove curwin from the list. Remember after which window it was in wp2
1278 * 2. insert curwin before wp in the list
1279 * if wp != wp2
1280 * 3. remove wp from the list
1281 * 4. insert wp after wp2
1282 * 5. exchange the status line height and vsep width.
1283 */
1284 wp2 = curwin->w_prev;
1285 frp2 = curwin->w_frame->fr_prev;
1286 if (wp->w_prev != curwin)
1287 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001288 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 frame_remove(curwin->w_frame);
1290 win_append(wp->w_prev, curwin);
1291 frame_insert(frp, curwin->w_frame);
1292 }
1293 if (wp != wp2)
1294 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001295 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 frame_remove(wp->w_frame);
1297 win_append(wp2, wp);
1298 if (frp2 == NULL)
1299 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1300 else
1301 frame_append(frp2, wp->w_frame);
1302 }
1303 temp = curwin->w_status_height;
1304 curwin->w_status_height = wp->w_status_height;
1305 wp->w_status_height = temp;
1306#ifdef FEAT_VERTSPLIT
1307 temp = curwin->w_vsep_width;
1308 curwin->w_vsep_width = wp->w_vsep_width;
1309 wp->w_vsep_width = temp;
1310
1311 /* If the windows are not in the same frame, exchange the sizes to avoid
1312 * messing up the window layout. Otherwise fix the frame sizes. */
1313 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1314 {
1315 temp = curwin->w_height;
1316 curwin->w_height = wp->w_height;
1317 wp->w_height = temp;
1318 temp = curwin->w_width;
1319 curwin->w_width = wp->w_width;
1320 wp->w_width = temp;
1321 }
1322 else
1323 {
1324 frame_fix_height(curwin);
1325 frame_fix_height(wp);
1326 frame_fix_width(curwin);
1327 frame_fix_width(wp);
1328 }
1329#endif
1330
1331 (void)win_comp_pos(); /* recompute window positions */
1332
1333 win_enter(wp, TRUE);
1334 redraw_later(CLEAR);
1335}
1336
1337/*
1338 * rotate windows: if upwards TRUE the second window becomes the first one
1339 * if upwards FALSE the first window becomes the second one
1340 */
1341 static void
1342win_rotate(upwards, count)
1343 int upwards;
1344 int count;
1345{
1346 win_T *wp1;
1347 win_T *wp2;
1348 frame_T *frp;
1349 int n;
1350
1351 if (firstwin == lastwin) /* nothing to do */
1352 {
1353 beep_flush();
1354 return;
1355 }
1356
1357#ifdef FEAT_GUI
1358 need_mouse_correct = TRUE;
1359#endif
1360
1361#ifdef FEAT_VERTSPLIT
1362 /* Check if all frames in this row/col have one window. */
1363 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1364 frp = frp->fr_next)
1365 if (frp->fr_win == NULL)
1366 {
1367 EMSG(_("E443: Cannot rotate when another window is split"));
1368 return;
1369 }
1370#endif
1371
1372 while (count--)
1373 {
1374 if (upwards) /* first window becomes last window */
1375 {
1376 /* remove first window/frame from the list */
1377 frp = curwin->w_frame->fr_parent->fr_child;
1378 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001379 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 frame_remove(frp);
1381
1382 /* find last frame and append removed window/frame after it */
1383 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1384 ;
1385 win_append(frp->fr_win, wp1);
1386 frame_append(frp, wp1->w_frame);
1387
1388 wp2 = frp->fr_win; /* previously last window */
1389 }
1390 else /* last window becomes first window */
1391 {
1392 /* find last window/frame in the list and remove it */
1393 for (frp = curwin->w_frame; frp->fr_next != NULL;
1394 frp = frp->fr_next)
1395 ;
1396 wp1 = frp->fr_win;
1397 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001398 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 frame_remove(frp);
1400
1401 /* append the removed window/frame before the first in the list */
1402 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1403 frame_insert(frp->fr_parent->fr_child, frp);
1404 }
1405
1406 /* exchange status height and vsep width of old and new last window */
1407 n = wp2->w_status_height;
1408 wp2->w_status_height = wp1->w_status_height;
1409 wp1->w_status_height = n;
1410 frame_fix_height(wp1);
1411 frame_fix_height(wp2);
1412#ifdef FEAT_VERTSPLIT
1413 n = wp2->w_vsep_width;
1414 wp2->w_vsep_width = wp1->w_vsep_width;
1415 wp1->w_vsep_width = n;
1416 frame_fix_width(wp1);
1417 frame_fix_width(wp2);
1418#endif
1419
1420 /* recompute w_winrow and w_wincol for all windows */
1421 (void)win_comp_pos();
1422 }
1423
1424 redraw_later(CLEAR);
1425}
1426
1427/*
1428 * Move the current window to the very top/bottom/left/right of the screen.
1429 */
1430 static void
1431win_totop(size, flags)
1432 int size;
1433 int flags;
1434{
1435 int dir;
1436 int height = curwin->w_height;
1437
1438 if (lastwin == firstwin)
1439 {
1440 beep_flush();
1441 return;
1442 }
1443
1444 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001445 (void)winframe_remove(curwin, &dir, NULL);
1446 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 last_status(FALSE); /* may need to remove last status line */
1448 (void)win_comp_pos(); /* recompute window positions */
1449
1450 /* Split a window on the desired side and put the window there. */
1451 (void)win_split_ins(size, flags, curwin, dir);
1452 if (!(flags & WSP_VERT))
1453 {
1454 win_setheight(height);
1455 if (p_ea)
1456 win_equal(curwin, TRUE, 'v');
1457 }
1458
1459#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1460 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1461 * scrollbars. Have to update them anyway. */
1462 if (gui.in_use)
1463 {
1464 out_flush();
1465 gui_init_which_components(NULL);
1466 gui_update_scrollbars(TRUE);
1467 }
1468 need_mouse_correct = TRUE;
1469#endif
1470
1471}
1472
1473/*
1474 * Move window "win1" to below/right of "win2" and make "win1" the current
1475 * window. Only works within the same frame!
1476 */
1477 void
1478win_move_after(win1, win2)
1479 win_T *win1, *win2;
1480{
1481 int height;
1482
1483 /* check if the arguments are reasonable */
1484 if (win1 == win2)
1485 return;
1486
1487 /* check if there is something to do */
1488 if (win2->w_next != win1)
1489 {
1490 /* may need move the status line/vertical separator of the last window
1491 * */
1492 if (win1 == lastwin)
1493 {
1494 height = win1->w_prev->w_status_height;
1495 win1->w_prev->w_status_height = win1->w_status_height;
1496 win1->w_status_height = height;
1497#ifdef FEAT_VERTSPLIT
1498 win1->w_prev->w_vsep_width = 0;
1499 win1->w_vsep_width = 1;
1500#endif
1501 }
1502 else if (win2 == lastwin)
1503 {
1504 height = win1->w_status_height;
1505 win1->w_status_height = win2->w_status_height;
1506 win2->w_status_height = height;
1507#ifdef FEAT_VERTSPLIT
1508 win2->w_vsep_width = 1;
1509 win1->w_vsep_width = 0;
1510#endif
1511 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001512 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 frame_remove(win1->w_frame);
1514 win_append(win2, win1);
1515 frame_append(win2->w_frame, win1->w_frame);
1516
1517 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1518 redraw_later(NOT_VALID);
1519 }
1520 win_enter(win1, FALSE);
1521}
1522
1523/*
1524 * Make all windows the same height.
1525 * 'next_curwin' will soon be the current window, make sure it has enough
1526 * rows.
1527 */
1528 void
1529win_equal(next_curwin, current, dir)
1530 win_T *next_curwin; /* pointer to current window to be or NULL */
1531 int current; /* do only frame with current window */
1532 int dir; /* 'v' for vertically, 'h' for horizontally,
1533 'b' for both, 0 for using p_ead */
1534{
1535 if (dir == 0)
1536#ifdef FEAT_VERTSPLIT
1537 dir = *p_ead;
1538#else
1539 dir = 'b';
1540#endif
1541 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001542 topframe, dir, 0, tabpageline_height(),
1543 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544}
1545
1546/*
1547 * Set a frame to a new position and height, spreading the available room
1548 * equally over contained frames.
1549 * The window "next_curwin" (if not NULL) should at least get the size from
1550 * 'winheight' and 'winwidth' if possible.
1551 */
1552 static void
1553win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1554 win_T *next_curwin; /* pointer to current window to be or NULL */
1555 int current; /* do only frame with current window */
1556 frame_T *topfr; /* frame to set size off */
1557 int dir; /* 'v', 'h' or 'b', see win_equal() */
1558 int col; /* horizontal position for frame */
1559 int row; /* vertical position for frame */
1560 int width; /* new width of frame */
1561 int height; /* new height of frame */
1562{
1563 int n, m;
1564 int extra_sep = 0;
1565 int wincount, totwincount = 0;
1566 frame_T *fr;
1567 int next_curwin_size = 0;
1568 int room = 0;
1569 int new_size;
1570 int has_next_curwin = 0;
1571 int hnc;
1572
1573 if (topfr->fr_layout == FR_LEAF)
1574 {
1575 /* Set the width/height of this frame.
1576 * Redraw when size or position changes */
1577 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1578#ifdef FEAT_VERTSPLIT
1579 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1580#endif
1581 )
1582 {
1583 topfr->fr_win->w_winrow = row;
1584 frame_new_height(topfr, height, FALSE, FALSE);
1585#ifdef FEAT_VERTSPLIT
1586 topfr->fr_win->w_wincol = col;
1587 frame_new_width(topfr, width, FALSE);
1588#endif
1589 redraw_all_later(CLEAR);
1590 }
1591 }
1592#ifdef FEAT_VERTSPLIT
1593 else if (topfr->fr_layout == FR_ROW)
1594 {
1595 topfr->fr_width = width;
1596 topfr->fr_height = height;
1597
1598 if (dir != 'v') /* equalize frame widths */
1599 {
1600 /* Compute the maximum number of windows horizontally in this
1601 * frame. */
1602 n = frame_minwidth(topfr, NOWIN);
1603 /* add one for the rightmost window, it doesn't have a separator */
1604 if (col + width == Columns)
1605 extra_sep = 1;
1606 else
1607 extra_sep = 0;
1608 totwincount = (n + extra_sep) / (p_wmw + 1);
1609
1610 /* Compute room available for windows other than "next_curwin" */
1611 m = frame_minwidth(topfr, next_curwin);
1612 room = width - m;
1613 if (room < 0)
1614 {
1615 next_curwin_size = p_wiw + room;
1616 room = 0;
1617 }
1618 else if (n == m) /* doesn't contain curwin */
1619 next_curwin_size = 0;
1620 else
1621 {
1622 next_curwin_size = (room + p_wiw + (totwincount - 1) * p_wmw
1623 + (totwincount - 1)) / totwincount;
1624 if (next_curwin_size > p_wiw)
1625 room -= next_curwin_size - p_wiw;
1626 else
1627 next_curwin_size = p_wiw;
1628 }
1629 if (n != m)
1630 --totwincount; /* don't count curwin */
1631 }
1632
1633 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1634 {
1635 n = m = 0;
1636 wincount = 1;
1637 if (fr->fr_next == NULL)
1638 /* last frame gets all that remains (avoid roundoff error) */
1639 new_size = width;
1640 else if (dir == 'v')
1641 new_size = fr->fr_width;
1642 else
1643 {
1644 /* Compute the maximum number of windows horiz. in "fr". */
1645 n = frame_minwidth(fr, NOWIN);
1646 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1647 / (p_wmw + 1);
1648 m = frame_minwidth(fr, next_curwin);
1649 if (n != m) /* don't count next_curwin */
1650 --wincount;
1651 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1652 / totwincount;
1653 if (n != m) /* add next_curwin size */
1654 {
1655 next_curwin_size -= p_wiw - (m - n);
1656 new_size += next_curwin_size;
1657 }
1658 }
1659
1660 /* Skip frame that is full height when splitting or closing a
1661 * window, unless equalizing all frames. */
1662 if (!current || dir != 'v' || topfr->fr_parent != NULL
1663 || (new_size != fr->fr_width)
1664 || frame_has_win(fr, next_curwin))
1665 win_equal_rec(next_curwin, current, fr, dir, col, row,
1666 new_size + n, height);
1667 col += new_size + n;
1668 width -= new_size + n;
1669 if (n != m) /* contains curwin */
1670 room -= new_size - next_curwin_size;
1671 else
1672 room -= new_size;
1673 totwincount -= wincount;
1674 }
1675 }
1676#endif
1677 else /* topfr->fr_layout == FR_COL */
1678 {
1679#ifdef FEAT_VERTSPLIT
1680 topfr->fr_width = width;
1681#endif
1682 topfr->fr_height = height;
1683
1684 if (dir != 'h') /* equalize frame heights */
1685 {
1686 /* Compute maximum number of windows vertically in this frame. */
1687 n = frame_minheight(topfr, NOWIN);
1688 /* add one for the bottom window if it doesn't have a statusline */
1689 if (row + height == cmdline_row && p_ls == 0)
1690 extra_sep = 1;
1691 else
1692 extra_sep = 0;
1693 totwincount = (n + extra_sep) / (p_wmh + 1);
1694 has_next_curwin = frame_has_win(topfr, next_curwin);
1695
1696 /*
1697 * Compute height for "next_curwin" window and room available for
1698 * other windows.
1699 * "m" is the minimal height when counting p_wh for "next_curwin".
1700 */
1701 m = frame_minheight(topfr, next_curwin);
1702 room = height - m;
1703 if (room < 0)
1704 {
1705 /* The room is less then 'winheight', use all space for the
1706 * current window. */
1707 next_curwin_size = p_wh + room;
1708 room = 0;
1709 }
1710 else
1711 {
1712 next_curwin_size = -1;
1713 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1714 {
1715 /* If 'winfixheight' set keep the window height if
1716 * possible.
1717 * Watch out for this window being the next_curwin. */
1718 if (frame_fixed_height(fr))
1719 {
1720 n = frame_minheight(fr, NOWIN);
1721 new_size = fr->fr_height;
1722 if (frame_has_win(fr, next_curwin))
1723 {
1724 room += p_wh - p_wmh;
1725 next_curwin_size = 0;
1726 if (new_size < p_wh)
1727 new_size = p_wh;
1728 }
1729 else
1730 /* These windows don't use up room. */
1731 totwincount -= (n + (fr->fr_next == NULL
1732 ? extra_sep : 0)) / (p_wmh + 1);
1733 room -= new_size - n;
1734 if (room < 0)
1735 {
1736 new_size += room;
1737 room = 0;
1738 }
1739 fr->fr_newheight = new_size;
1740 }
1741 }
1742 if (next_curwin_size == -1)
1743 {
1744 if (!has_next_curwin)
1745 next_curwin_size = 0;
1746 else if (totwincount > 1
1747 && (room + (totwincount - 2))
1748 / (totwincount - 1) > p_wh)
1749 {
1750 next_curwin_size = (room + p_wh + totwincount * p_wmh
1751 + (totwincount - 1)) / totwincount;
1752 room -= next_curwin_size - p_wh;
1753 }
1754 else
1755 next_curwin_size = p_wh;
1756 }
1757 }
1758
1759 if (has_next_curwin)
1760 --totwincount; /* don't count curwin */
1761 }
1762
1763 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1764 {
1765 n = m = 0;
1766 wincount = 1;
1767 if (fr->fr_next == NULL)
1768 /* last frame gets all that remains (avoid roundoff error) */
1769 new_size = height;
1770 else if (dir == 'h')
1771 new_size = fr->fr_height;
1772 else if (frame_fixed_height(fr))
1773 {
1774 new_size = fr->fr_newheight;
1775 wincount = 0; /* doesn't count as a sizeable window */
1776 }
1777 else
1778 {
1779 /* Compute the maximum number of windows vert. in "fr". */
1780 n = frame_minheight(fr, NOWIN);
1781 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1782 / (p_wmh + 1);
1783 m = frame_minheight(fr, next_curwin);
1784 if (has_next_curwin)
1785 hnc = frame_has_win(fr, next_curwin);
1786 else
1787 hnc = FALSE;
1788 if (hnc) /* don't count next_curwin */
1789 --wincount;
1790 if (totwincount == 0)
1791 new_size = room;
1792 else
1793 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1794 / totwincount;
1795 if (hnc) /* add next_curwin size */
1796 {
1797 next_curwin_size -= p_wh - (m - n);
1798 new_size += next_curwin_size;
1799 room -= new_size - next_curwin_size;
1800 }
1801 else
1802 room -= new_size;
1803 new_size += n;
1804 }
1805 /* Skip frame that is full width when splitting or closing a
1806 * window, unless equalizing all frames. */
1807 if (!current || dir != 'h' || topfr->fr_parent != NULL
1808 || (new_size != fr->fr_height)
1809 || frame_has_win(fr, next_curwin))
1810 win_equal_rec(next_curwin, current, fr, dir, col, row,
1811 width, new_size);
1812 row += new_size;
1813 height -= new_size;
1814 totwincount -= wincount;
1815 }
1816 }
1817}
1818
1819/*
1820 * close all windows for buffer 'buf'
1821 */
1822 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00001823close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001825 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826{
Bram Moolenaarf740b292006-02-16 22:11:02 +00001827 win_T *wp;
1828 tabpage_T *tp, *nexttp;
1829 int h = tabpageline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830
1831 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001832
1833 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001835 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001837 win_close(wp, FALSE);
1838
1839 /* Start all over, autocommands may change the window layout. */
1840 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
1842 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00001843 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001845
1846 /* Also check windows in other tab pages. */
1847 for (tp = first_tabpage; tp != NULL; tp = nexttp)
1848 {
1849 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001850 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001851 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
1852 if (wp->w_buffer == buf)
1853 {
1854 win_close_othertab(wp, FALSE, tp);
1855
1856 /* Start all over, the tab page may be closed and
1857 * autocommands may change the window layout. */
1858 nexttp = first_tabpage;
1859 break;
1860 }
1861 }
1862
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001864
1865 if (h != tabpageline_height())
1866 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867}
1868
1869/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001870 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001871 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001872 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001873 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001874last_window()
1875{
1876 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
1877}
1878
1879/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00001880 * Close window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 * If "free_buf" is TRUE related buffer may be unloaded.
1882 *
1883 * called by :quit, :close, :xit, :wq and findtag()
1884 */
1885 void
1886win_close(win, free_buf)
1887 win_T *win;
1888 int free_buf;
1889{
1890 win_T *wp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001891 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892#ifdef FEAT_AUTOCMD
1893 int other_buffer = FALSE;
1894#endif
1895 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 int dir;
1897 int help_window = FALSE;
1898
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001899 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
1901 EMSG(_("E444: Cannot close last window"));
1902 return;
1903 }
1904
1905 /* When closing the help window, try restoring a snapshot after closing
1906 * the window. Otherwise clear the snapshot, it's now invalid. */
1907 if (win->w_buffer->b_help)
1908 help_window = TRUE;
1909 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001910 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911
1912#ifdef FEAT_AUTOCMD
1913 if (win == curwin)
1914 {
1915 /*
1916 * Guess which window is going to be the new current window.
1917 * This may change because of the autocommands (sigh).
1918 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001919 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920
1921 /*
1922 * Be careful: If autocommands delete the window, return now.
1923 */
1924 if (wp->w_buffer != curbuf)
1925 {
1926 other_buffer = TRUE;
1927 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001928 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 return;
1930 }
1931 apply_autocmds(EVENT_WINLEAVE, 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# ifdef FEAT_EVAL
1935 /* autocmds may abort script processing */
1936 if (aborting())
1937 return;
1938# endif
1939 }
1940#endif
1941
1942 /*
1943 * Close the link to the buffer.
1944 */
1945 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
1946 /* Autocommands may have closed the window already, or closed the only
1947 * other window. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001948 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 return;
1950
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001951 /* Free the memory used for the window. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001952 wp = win_free_mem(win, &dir, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001954 /* When closing the last window in a tab page go to another tab page. */
1955 if (wp == NULL)
1956 {
1957 tabpage_T *ptp = NULL;
1958 tabpage_T *tp;
1959 tabpage_T *atp = alt_tabpage();
1960
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001961 for (tp = first_tabpage; tp != curtab; tp = tp->tp_next)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001962 ptp = tp;
1963 if (tp == NULL)
1964 {
1965 EMSG2(_(e_intern2), "win_close()");
1966 return;
1967 }
1968 if (ptp == NULL)
1969 first_tabpage = tp->tp_next;
1970 else
1971 ptp->tp_next = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001972 free_tabpage(tp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001973
1974 /* We don't do the window resizing stuff, let enter_tabpage() take
1975 * care of entering a window in another tab page. */
1976 enter_tabpage(atp, old_curbuf);
1977 return;
1978 }
1979
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 /* Make sure curwin isn't invalid. It can cause severe trouble when
1981 * printing an error message. For win_equal() curbuf needs to be valid
1982 * too. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001983 else if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
1985 curwin = wp;
1986#ifdef FEAT_QUICKFIX
1987 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
1988 {
1989 /*
1990 * The cursor goes to the preview or the quickfix window, try
1991 * finding another window to go to.
1992 */
1993 for (;;)
1994 {
1995 if (wp->w_next == NULL)
1996 wp = firstwin;
1997 else
1998 wp = wp->w_next;
1999 if (wp == curwin)
2000 break;
2001 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2002 {
2003 curwin = wp;
2004 break;
2005 }
2006 }
2007 }
2008#endif
2009 curbuf = curwin->w_buffer;
2010 close_curwin = TRUE;
2011 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002012 if (p_ea
2013#ifdef FEAT_VERTSPLIT
2014 && (*p_ead == 'b' || *p_ead == dir)
2015#endif
2016 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 win_equal(curwin, TRUE,
2018#ifdef FEAT_VERTSPLIT
2019 dir
2020#else
2021 0
2022#endif
2023 );
2024 else
2025 win_comp_pos();
2026 if (close_curwin)
2027 {
2028 win_enter_ext(wp, FALSE, TRUE);
2029#ifdef FEAT_AUTOCMD
2030 if (other_buffer)
2031 /* careful: after this wp and win may be invalid! */
2032 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2033#endif
2034 }
2035
2036 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002037 * If last window has a status line now and we don't want one,
2038 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 */
2040 last_status(FALSE);
2041
2042 /* After closing the help window, try restoring the window layout from
2043 * before it was opened. */
2044 if (help_window)
2045 restore_snapshot(close_curwin);
2046
2047#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2048 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2049 if (gui.in_use && !win_hasvertsplit())
2050 gui_init_which_components(NULL);
2051#endif
2052
2053 redraw_all_later(NOT_VALID);
2054}
2055
2056/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002057 * Close window "win" in tab page "tp", which is not the current tab page.
2058 * This may be the last window ih that tab page and result in closing the tab,
2059 * thus "tp" may become invalid!
2060 * Called must check if buffer is hidden.
2061 */
2062 void
2063win_close_othertab(win, free_buf, tp)
2064 win_T *win;
2065 int free_buf;
2066 tabpage_T *tp;
2067{
2068 win_T *wp;
2069 int dir;
2070 tabpage_T *ptp = NULL;
2071
2072 /* Close the link to the buffer. */
2073 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2074
2075 /* Careful: Autocommands may have closed the tab page or made it the
2076 * current tab page. */
2077 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2078 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002079 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002080 return;
2081
2082 /* Autocommands may have closed the window already. */
2083 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2084 ;
2085 if (wp == NULL)
2086 return;
2087
2088 /* Free the memory used for the window. */
2089 wp = win_free_mem(win, &dir, tp);
2090
2091 /* When closing the last window in a tab page remove the tab page. */
2092 if (wp == NULL)
2093 {
2094 if (tp == first_tabpage)
2095 first_tabpage = tp->tp_next;
2096 else
2097 {
2098 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2099 ptp = ptp->tp_next)
2100 ;
2101 if (ptp == NULL)
2102 {
2103 EMSG2(_(e_intern2), "win_close_othertab()");
2104 return;
2105 }
2106 ptp->tp_next = tp->tp_next;
2107 }
2108 vim_free(tp);
2109 }
2110}
2111
2112/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002113 * Free the memory used for a window.
2114 * Returns a pointer to the window that got the freed up space.
2115 */
2116 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002117win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002118 win_T *win;
2119 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002120 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002121{
2122 frame_T *frp;
2123 win_T *wp;
2124
Bram Moolenaarea408852005-06-25 22:49:46 +00002125#ifdef FEAT_FOLDING
2126 clearFolding(win);
2127#endif
2128
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002129 /* reduce the reference count to the argument list. */
2130 alist_unlink(win->w_alist);
2131
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002132 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002133 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002134 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002135 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002136 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002137
2138 return wp;
2139}
2140
2141#if defined(EXITFREE) || defined(PROTO)
2142 void
2143win_free_all()
2144{
2145 int dummy;
2146
Bram Moolenaarf740b292006-02-16 22:11:02 +00002147# ifdef FEAT_WINDOWS
2148 while (first_tabpage->tp_next != NULL)
2149 tabpage_close(TRUE);
2150# endif
2151
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002152 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002153 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002154}
2155#endif
2156
2157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 * Remove a window and its frame from the tree of frames.
2159 * Returns a pointer to the window that got the freed up space.
2160 */
2161/*ARGSUSED*/
2162 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002163winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 win_T *win;
2165 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002166 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167{
2168 frame_T *frp, *frp2, *frp3;
2169 frame_T *frp_close = win->w_frame;
2170 win_T *wp;
2171 int old_height = 0;
2172
2173 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002174 * If there is only one window there is nothing to remove.
2175 */
2176 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2177 return NULL;
2178
2179 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 * Remove the window from its frame.
2181 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002182 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 wp = frame2win(frp2);
2184
2185 /* Remove this frame from the list of frames. */
2186 frame_remove(frp_close);
2187
2188#ifdef FEAT_VERTSPLIT
2189 if (frp_close->fr_parent->fr_layout == FR_COL)
2190 {
2191#endif
2192 /* When 'winfixheight' is set, remember its old size and restore
2193 * it later (it's a simplistic solution...). Don't do this if the
2194 * window will occupy the full height of the screen. */
2195 if (frp2->fr_win != NULL
2196 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2197 && frp2->fr_win->w_p_wfh)
2198 old_height = frp2->fr_win->w_height;
2199 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2200 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2201 if (old_height != 0)
2202 win_setheight_win(old_height, frp2->fr_win);
2203#ifdef FEAT_VERTSPLIT
2204 *dirp = 'v';
2205 }
2206 else
2207 {
2208 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2209 frp2 == frp_close->fr_next ? TRUE : FALSE);
2210 *dirp = 'h';
2211 }
2212#endif
2213
2214 /* If rows/columns go to a window below/right its positions need to be
2215 * updated. Can only be done after the sizes have been updated. */
2216 if (frp2 == frp_close->fr_next)
2217 {
2218 int row = win->w_winrow;
2219 int col = W_WINCOL(win);
2220
2221 frame_comp_pos(frp2, &row, &col);
2222 }
2223
2224 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2225 {
2226 /* There is no other frame in this list, move its info to the parent
2227 * and remove it. */
2228 frp2->fr_parent->fr_layout = frp2->fr_layout;
2229 frp2->fr_parent->fr_child = frp2->fr_child;
2230 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2231 frp->fr_parent = frp2->fr_parent;
2232 frp2->fr_parent->fr_win = frp2->fr_win;
2233 if (frp2->fr_win != NULL)
2234 frp2->fr_win->w_frame = frp2->fr_parent;
2235 frp = frp2->fr_parent;
2236 vim_free(frp2);
2237
2238 frp2 = frp->fr_parent;
2239 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2240 {
2241 /* The frame above the parent has the same layout, have to merge
2242 * the frames into this list. */
2243 if (frp2->fr_child == frp)
2244 frp2->fr_child = frp->fr_child;
2245 frp->fr_child->fr_prev = frp->fr_prev;
2246 if (frp->fr_prev != NULL)
2247 frp->fr_prev->fr_next = frp->fr_child;
2248 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2249 {
2250 frp3->fr_parent = frp2;
2251 if (frp3->fr_next == NULL)
2252 {
2253 frp3->fr_next = frp->fr_next;
2254 if (frp->fr_next != NULL)
2255 frp->fr_next->fr_prev = frp3;
2256 break;
2257 }
2258 }
2259 vim_free(frp);
2260 }
2261 }
2262
2263 return wp;
2264}
2265
2266/*
2267 * Find out which frame is going to get the freed up space when "win" is
2268 * closed.
2269 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2270 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2271 * This makes opening a window and closing it immediately keep the same window
2272 * layout.
2273 */
2274 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002275win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002277 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278{
2279 frame_T *frp;
2280 int b;
2281
Bram Moolenaarf740b292006-02-16 22:11:02 +00002282 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002283 /* Last window in this tab page, will go to next tab page. */
2284 return alt_tabpage()->tp_curwin->w_frame;
2285
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 frp = win->w_frame;
2287#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002288 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 b = p_spr;
2290 else
2291#endif
2292 b = p_sb;
2293 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2294 return frp->fr_next;
2295 return frp->fr_prev;
2296}
2297
2298/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002299 * Return the tabpage that will be used if the current one is closed.
2300 */
2301 static tabpage_T *
2302alt_tabpage()
2303{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002304 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002305
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002306 /* Use the next tab page if we are currently at the first one. */
2307 if (curtab == first_tabpage)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002308 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002309
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002310 /* Find the previous tab page. */
2311 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
2312 if (tp->tp_next == curtab)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002313 break;
2314 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002315}
2316
2317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 * Find the left-upper window in frame "frp".
2319 */
2320 static win_T *
2321frame2win(frp)
2322 frame_T *frp;
2323{
2324 while (frp->fr_win == NULL)
2325 frp = frp->fr_child;
2326 return frp->fr_win;
2327}
2328
2329/*
2330 * Return TRUE if frame "frp" contains window "wp".
2331 */
2332 static int
2333frame_has_win(frp, wp)
2334 frame_T *frp;
2335 win_T *wp;
2336{
2337 frame_T *p;
2338
2339 if (frp->fr_layout == FR_LEAF)
2340 return frp->fr_win == wp;
2341
2342 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2343 if (frame_has_win(p, wp))
2344 return TRUE;
2345 return FALSE;
2346}
2347
2348/*
2349 * Set a new height for a frame. Recursively sets the height for contained
2350 * frames and windows. Caller must take care of positions.
2351 */
2352 static void
2353frame_new_height(topfrp, height, topfirst, wfh)
2354 frame_T *topfrp;
2355 int height;
2356 int topfirst; /* resize topmost contained frame first */
2357 int wfh; /* obey 'winfixheight' when there is a choice;
2358 may cause the height not to be set */
2359{
2360 frame_T *frp;
2361 int extra_lines;
2362 int h;
2363
2364 if (topfrp->fr_win != NULL)
2365 {
2366 /* Simple case: just one window. */
2367 win_new_height(topfrp->fr_win,
2368 height - topfrp->fr_win->w_status_height);
2369 }
2370#ifdef FEAT_VERTSPLIT
2371 else if (topfrp->fr_layout == FR_ROW)
2372 {
2373 do
2374 {
2375 /* All frames in this row get the same new height. */
2376 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2377 {
2378 frame_new_height(frp, height, topfirst, wfh);
2379 if (frp->fr_height > height)
2380 {
2381 /* Could not fit the windows, make the whole row higher. */
2382 height = frp->fr_height;
2383 break;
2384 }
2385 }
2386 }
2387 while (frp != NULL);
2388 }
2389#endif
2390 else
2391 {
2392 /* Complicated case: Resize a column of frames. Resize the bottom
2393 * frame first, frames above that when needed. */
2394
2395 frp = topfrp->fr_child;
2396 if (wfh)
2397 /* Advance past frames with one window with 'wfh' set. */
2398 while (frame_fixed_height(frp))
2399 {
2400 frp = frp->fr_next;
2401 if (frp == NULL)
2402 return; /* no frame without 'wfh', give up */
2403 }
2404 if (!topfirst)
2405 {
2406 /* Find the bottom frame of this column */
2407 while (frp->fr_next != NULL)
2408 frp = frp->fr_next;
2409 if (wfh)
2410 /* Advance back for frames with one window with 'wfh' set. */
2411 while (frame_fixed_height(frp))
2412 frp = frp->fr_prev;
2413 }
2414
2415 extra_lines = height - topfrp->fr_height;
2416 if (extra_lines < 0)
2417 {
2418 /* reduce height of contained frames, bottom or top frame first */
2419 while (frp != NULL)
2420 {
2421 h = frame_minheight(frp, NULL);
2422 if (frp->fr_height + extra_lines < h)
2423 {
2424 extra_lines += frp->fr_height - h;
2425 frame_new_height(frp, h, topfirst, wfh);
2426 }
2427 else
2428 {
2429 frame_new_height(frp, frp->fr_height + extra_lines,
2430 topfirst, wfh);
2431 break;
2432 }
2433 if (topfirst)
2434 {
2435 do
2436 frp = frp->fr_next;
2437 while (wfh && frp != NULL && frame_fixed_height(frp));
2438 }
2439 else
2440 {
2441 do
2442 frp = frp->fr_prev;
2443 while (wfh && frp != NULL && frame_fixed_height(frp));
2444 }
2445 /* Increase "height" if we could not reduce enough frames. */
2446 if (frp == NULL)
2447 height -= extra_lines;
2448 }
2449 }
2450 else if (extra_lines > 0)
2451 {
2452 /* increase height of bottom or top frame */
2453 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2454 }
2455 }
2456 topfrp->fr_height = height;
2457}
2458
2459/*
2460 * Return TRUE if height of frame "frp" should not be changed because of
2461 * the 'winfixheight' option.
2462 */
2463 static int
2464frame_fixed_height(frp)
2465 frame_T *frp;
2466{
2467 /* frame with one window: fixed height if 'winfixheight' set. */
2468 if (frp->fr_win != NULL)
2469 return frp->fr_win->w_p_wfh;
2470
2471 if (frp->fr_layout == FR_ROW)
2472 {
2473 /* The frame is fixed height if one of the frames in the row is fixed
2474 * height. */
2475 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2476 if (frame_fixed_height(frp))
2477 return TRUE;
2478 return FALSE;
2479 }
2480
2481 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2482 * frames in the row are fixed height. */
2483 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2484 if (!frame_fixed_height(frp))
2485 return FALSE;
2486 return TRUE;
2487}
2488
2489#ifdef FEAT_VERTSPLIT
2490/*
2491 * Add a status line to windows at the bottom of "frp".
2492 * Note: Does not check if there is room!
2493 */
2494 static void
2495frame_add_statusline(frp)
2496 frame_T *frp;
2497{
2498 win_T *wp;
2499
2500 if (frp->fr_layout == FR_LEAF)
2501 {
2502 wp = frp->fr_win;
2503 if (wp->w_status_height == 0)
2504 {
2505 if (wp->w_height > 0) /* don't make it negative */
2506 --wp->w_height;
2507 wp->w_status_height = STATUS_HEIGHT;
2508 }
2509 }
2510 else if (frp->fr_layout == FR_ROW)
2511 {
2512 /* Handle all the frames in the row. */
2513 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2514 frame_add_statusline(frp);
2515 }
2516 else /* frp->fr_layout == FR_COL */
2517 {
2518 /* Only need to handle the last frame in the column. */
2519 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2520 ;
2521 frame_add_statusline(frp);
2522 }
2523}
2524
2525/*
2526 * Set width of a frame. Handles recursively going through contained frames.
2527 * May remove separator line for windows at the right side (for win_close()).
2528 */
2529 static void
2530frame_new_width(topfrp, width, leftfirst)
2531 frame_T *topfrp;
2532 int width;
2533 int leftfirst; /* resize leftmost contained frame first */
2534{
2535 frame_T *frp;
2536 int extra_cols;
2537 int w;
2538 win_T *wp;
2539
2540 if (topfrp->fr_layout == FR_LEAF)
2541 {
2542 /* Simple case: just one window. */
2543 wp = topfrp->fr_win;
2544 /* Find out if there are any windows right of this one. */
2545 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2546 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2547 break;
2548 if (frp->fr_parent == NULL)
2549 wp->w_vsep_width = 0;
2550 win_new_width(wp, width - wp->w_vsep_width);
2551 }
2552 else if (topfrp->fr_layout == FR_COL)
2553 {
2554 /* All frames in this column get the same new width. */
2555 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2556 frame_new_width(frp, width, leftfirst);
2557 }
2558 else /* fr_layout == FR_ROW */
2559 {
2560 /* Complicated case: Resize a row of frames. Resize the rightmost
2561 * frame first, frames left of it when needed. */
2562
2563 /* Find the rightmost frame of this row */
2564 frp = topfrp->fr_child;
2565 if (!leftfirst)
2566 while (frp->fr_next != NULL)
2567 frp = frp->fr_next;
2568
2569 extra_cols = width - topfrp->fr_width;
2570 if (extra_cols < 0)
2571 {
2572 /* reduce frame width, rightmost frame first */
2573 while (frp != NULL)
2574 {
2575 w = frame_minwidth(frp, NULL);
2576 if (frp->fr_width + extra_cols < w)
2577 {
2578 extra_cols += frp->fr_width - w;
2579 frame_new_width(frp, w, leftfirst);
2580 }
2581 else
2582 {
2583 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst);
2584 break;
2585 }
2586 if (leftfirst)
2587 frp = frp->fr_next;
2588 else
2589 frp = frp->fr_prev;
2590 }
2591 }
2592 else if (extra_cols > 0)
2593 {
2594 /* increase width of rightmost frame */
2595 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst);
2596 }
2597 }
2598 topfrp->fr_width = width;
2599}
2600
2601/*
2602 * Add the vertical separator to windows at the right side of "frp".
2603 * Note: Does not check if there is room!
2604 */
2605 static void
2606frame_add_vsep(frp)
2607 frame_T *frp;
2608{
2609 win_T *wp;
2610
2611 if (frp->fr_layout == FR_LEAF)
2612 {
2613 wp = frp->fr_win;
2614 if (wp->w_vsep_width == 0)
2615 {
2616 if (wp->w_width > 0) /* don't make it negative */
2617 --wp->w_width;
2618 wp->w_vsep_width = 1;
2619 }
2620 }
2621 else if (frp->fr_layout == FR_COL)
2622 {
2623 /* Handle all the frames in the column. */
2624 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2625 frame_add_vsep(frp);
2626 }
2627 else /* frp->fr_layout == FR_ROW */
2628 {
2629 /* Only need to handle the last frame in the row. */
2630 frp = frp->fr_child;
2631 while (frp->fr_next != NULL)
2632 frp = frp->fr_next;
2633 frame_add_vsep(frp);
2634 }
2635}
2636
2637/*
2638 * Set frame width from the window it contains.
2639 */
2640 static void
2641frame_fix_width(wp)
2642 win_T *wp;
2643{
2644 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2645}
2646#endif
2647
2648/*
2649 * Set frame height from the window it contains.
2650 */
2651 static void
2652frame_fix_height(wp)
2653 win_T *wp;
2654{
2655 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2656}
2657
2658/*
2659 * Compute the minimal height for frame "topfrp".
2660 * Uses the 'winminheight' option.
2661 * When "next_curwin" isn't NULL, use p_wh for this window.
2662 * When "next_curwin" is NOWIN, don't use at least one line for the current
2663 * window.
2664 */
2665 static int
2666frame_minheight(topfrp, next_curwin)
2667 frame_T *topfrp;
2668 win_T *next_curwin;
2669{
2670 frame_T *frp;
2671 int m;
2672#ifdef FEAT_VERTSPLIT
2673 int n;
2674#endif
2675
2676 if (topfrp->fr_win != NULL)
2677 {
2678 if (topfrp->fr_win == next_curwin)
2679 m = p_wh + topfrp->fr_win->w_status_height;
2680 else
2681 {
2682 /* window: minimal height of the window plus status line */
2683 m = p_wmh + topfrp->fr_win->w_status_height;
2684 /* Current window is minimal one line high */
2685 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2686 ++m;
2687 }
2688 }
2689#ifdef FEAT_VERTSPLIT
2690 else if (topfrp->fr_layout == FR_ROW)
2691 {
2692 /* get the minimal height from each frame in this row */
2693 m = 0;
2694 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2695 {
2696 n = frame_minheight(frp, next_curwin);
2697 if (n > m)
2698 m = n;
2699 }
2700 }
2701#endif
2702 else
2703 {
2704 /* Add up the minimal heights for all frames in this column. */
2705 m = 0;
2706 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2707 m += frame_minheight(frp, next_curwin);
2708 }
2709
2710 return m;
2711}
2712
2713#ifdef FEAT_VERTSPLIT
2714/*
2715 * Compute the minimal width for frame "topfrp".
2716 * When "next_curwin" isn't NULL, use p_wiw for this window.
2717 * When "next_curwin" is NOWIN, don't use at least one column for the current
2718 * window.
2719 */
2720 static int
2721frame_minwidth(topfrp, next_curwin)
2722 frame_T *topfrp;
2723 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2724{
2725 frame_T *frp;
2726 int m, n;
2727
2728 if (topfrp->fr_win != NULL)
2729 {
2730 if (topfrp->fr_win == next_curwin)
2731 m = p_wiw + topfrp->fr_win->w_vsep_width;
2732 else
2733 {
2734 /* window: minimal width of the window plus separator column */
2735 m = p_wmw + topfrp->fr_win->w_vsep_width;
2736 /* Current window is minimal one column wide */
2737 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2738 ++m;
2739 }
2740 }
2741 else if (topfrp->fr_layout == FR_COL)
2742 {
2743 /* get the minimal width from each frame in this column */
2744 m = 0;
2745 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2746 {
2747 n = frame_minwidth(frp, next_curwin);
2748 if (n > m)
2749 m = n;
2750 }
2751 }
2752 else
2753 {
2754 /* Add up the minimal widths for all frames in this row. */
2755 m = 0;
2756 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2757 m += frame_minwidth(frp, next_curwin);
2758 }
2759
2760 return m;
2761}
2762#endif
2763
2764
2765/*
2766 * Try to close all windows except current one.
2767 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2768 * used and the buffer was modified.
2769 *
2770 * Used by ":bdel" and ":only".
2771 */
2772 void
2773close_others(message, forceit)
2774 int message;
2775 int forceit; /* always hide all other windows */
2776{
2777 win_T *wp;
2778 win_T *nextwp;
2779 int r;
2780
2781 if (lastwin == firstwin)
2782 {
2783 if (message
2784#ifdef FEAT_AUTOCMD
2785 && !autocmd_busy
2786#endif
2787 )
2788 MSG(_("Already only one window"));
2789 return;
2790 }
2791
2792 /* Be very careful here: autocommands may change the window layout. */
2793 for (wp = firstwin; win_valid(wp); wp = nextwp)
2794 {
2795 nextwp = wp->w_next;
2796 if (wp != curwin) /* don't close current window */
2797 {
2798
2799 /* Check if it's allowed to abandon this window */
2800 r = can_abandon(wp->w_buffer, forceit);
2801#ifdef FEAT_AUTOCMD
2802 if (!win_valid(wp)) /* autocommands messed wp up */
2803 {
2804 nextwp = firstwin;
2805 continue;
2806 }
2807#endif
2808 if (!r)
2809 {
2810#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2811 if (message && (p_confirm || cmdmod.confirm) && p_write)
2812 {
2813 dialog_changed(wp->w_buffer, FALSE);
2814# ifdef FEAT_AUTOCMD
2815 if (!win_valid(wp)) /* autocommands messed wp up */
2816 {
2817 nextwp = firstwin;
2818 continue;
2819 }
2820# endif
2821 }
2822 if (bufIsChanged(wp->w_buffer))
2823#endif
2824 continue;
2825 }
2826 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
2827 }
2828 }
2829
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002830 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 EMSG(_("E445: Other window contains changes"));
2832}
2833
2834#endif /* FEAT_WINDOWS */
2835
2836/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002837 * Init the current window "curwin".
2838 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 */
2840 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002841curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002843 redraw_win_later(curwin, NOT_VALID);
2844 curwin->w_lines_valid = 0;
2845 curwin->w_cursor.lnum = 1;
2846 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002848 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002850 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
2851 curwin->w_pcmark.col = 0;
2852 curwin->w_prev_pcmark.lnum = 0;
2853 curwin->w_prev_pcmark.col = 0;
2854 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002856 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002858 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859#ifdef FEAT_FKMAP
2860 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002861 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002863 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864#endif
2865}
2866
2867/*
2868 * Allocate the first window and put an empty buffer in it.
2869 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002870 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002872 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873win_alloc_first()
2874{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002875 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002876 return FAIL;
2877
2878#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002879 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002880 if (first_tabpage == NULL)
2881 return FAIL;
2882 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002883 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002884#endif
2885 return OK;
2886}
2887
2888/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002889 * Allocate the first window or the first window in a new tab page.
2890 * When "oldwin" is NULL create an empty buffer for it.
2891 * When "oldwin" is not NULL copy info from it to the new window (only with
2892 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002893 * Return FAIL when something goes wrong (out of memory).
2894 */
2895 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002896win_alloc_firstwin(oldwin)
2897 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002898{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002900 if (oldwin == NULL)
2901 {
2902 /* Very first window, need to create an empty buffer for it and
2903 * initialize from scratch. */
2904 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
2905 if (curwin == NULL || curbuf == NULL)
2906 return FAIL;
2907 curwin->w_buffer = curbuf;
2908 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002910 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002912 curwin_init(); /* init current window */
2913 }
2914#ifdef FEAT_WINDOWS
2915 else
2916 {
2917 /* First window in new tab page, initialize it from "oldwin". */
2918 win_init(curwin, oldwin);
2919
2920# ifdef FEAT_SCROLLBIND
2921 /* We don't want scroll-binding in the first window. */
2922 curwin->w_p_scb = FALSE;
2923# endif
2924 }
2925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
2927 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
2928 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002929 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 topframe->fr_layout = FR_LEAF;
2931#ifdef FEAT_VERTSPLIT
2932 topframe->fr_width = Columns;
2933#endif
2934 topframe->fr_height = Rows - p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002935#ifdef FEAT_WINDOWS
2936 p_ch_used = p_ch;
2937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 topframe->fr_win = curwin;
2939 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002940
2941 return OK;
2942}
2943
2944/*
2945 * Initialize the window and frame size to the maximum.
2946 */
2947 void
2948win_init_size()
2949{
2950 firstwin->w_height = ROWS_AVAIL;
2951 topframe->fr_height = ROWS_AVAIL;
2952#ifdef FEAT_VERTSPLIT
2953 firstwin->w_width = Columns;
2954 topframe->fr_width = Columns;
2955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956}
2957
2958#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002959
2960/*
2961 * Allocate a new tabpage_T and init the values.
2962 * Returns NULL when out of memory.
2963 */
2964 static tabpage_T *
2965alloc_tabpage()
2966{
2967 tabpage_T *tp;
2968
2969 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
2970 if (tp != NULL)
2971 {
2972# ifdef FEAT_DIFF
2973 tp->tp_diff_invalid = TRUE;
2974# endif
2975 }
2976 return tp;
2977}
2978
2979 static void
2980free_tabpage(tp)
2981 tabpage_T *tp;
2982{
2983# ifdef FEAT_DIFF
2984 diff_clear(tp);
2985# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002986 clear_snapshot(tp);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002987 vim_free(tp);
2988}
2989
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002990/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002991 * Create a new Tab page with one window.
2992 * It will edit the current buffer, like after ":split".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002993 * Put it just after the current Tab page.
2994 * Return FAIL or OK.
2995 */
2996 int
2997win_new_tabpage()
2998{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002999 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003000 tabpage_T *newtp;
3001
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003002 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003003 if (newtp == NULL)
3004 return FAIL;
3005
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003006 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003007 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003008 {
3009 vim_free(newtp);
3010 return FAIL;
3011 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003012 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003013
3014 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003015 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003016 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003017 /* Make the new Tab page the new topframe. */
3018 newtp->tp_next = tp->tp_next;
3019 tp->tp_next = newtp;
3020 win_init_size();
3021 firstwin->w_winrow = tabpageline_height();
3022
3023 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003024 last_status(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003025 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003026#ifdef FEAT_AUTOCMD
3027 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3028 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3029#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003030 return OK;
3031 }
3032
3033 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003034 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003035 return FAIL;
3036}
3037
3038/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003039 * Create up to "maxcount" tabpages with empty windows.
3040 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003041 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003042 int
3043make_tabpages(maxcount)
3044 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003045{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003046 int count = maxcount;
3047 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003048
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003049 /* Limit to 10 tabs. */
3050 if (count > 10)
3051 count = 10;
3052
3053#ifdef FEAT_AUTOCMD
3054 /*
3055 * Don't execute autocommands while creating the tab pages. Must do that
3056 * when putting the buffers in the windows.
3057 */
3058 ++autocmd_block;
3059#endif
3060
3061 for (todo = count - 1; todo > 0; --todo)
3062 if (win_new_tabpage() == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003063 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003064
3065#ifdef FEAT_AUTOCMD
3066 --autocmd_block;
3067#endif
3068
3069 /* return actual number of tab pages */
3070 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003071}
3072
3073/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003074 * Return TRUE when "tpc" points to a valid tab page.
3075 */
3076 int
3077valid_tabpage(tpc)
3078 tabpage_T *tpc;
3079{
3080 tabpage_T *tp;
3081
3082 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3083 if (tp == tpc)
3084 return TRUE;
3085 return FALSE;
3086}
3087
3088/*
3089 * Find tab page "n" (first one is 1). Returns NULL when not found.
3090 */
3091 tabpage_T *
3092find_tabpage(n)
3093 int n;
3094{
3095 tabpage_T *tp;
3096 int i = 1;
3097
3098 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3099 ++i;
3100 return tp;
3101}
3102
3103/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003104 * Prepare for leaving the current tab page.
3105 * When autocomands change "curtab" we don't leave the tab page and return
3106 * FAIL.
3107 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003108 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003109/*ARGSUSED*/
3110 static int
3111leave_tabpage(new_curbuf)
3112 buf_T *new_curbuf; /* what is going to be the new curbuf,
3113 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003114{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003115 tabpage_T *tp = curtab;
3116
3117#ifdef FEAT_AUTOCMD
3118 if (new_curbuf != curbuf)
3119 {
3120 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3121 if (curtab != tp)
3122 return FAIL;
3123 }
3124 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3125 if (curtab != tp)
3126 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003127 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003128 if (curtab != tp)
3129 return FAIL;
3130#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003131#if defined(FEAT_GUI)
3132 /* Remove the scrollbars. They may be added back later. */
3133 if (gui.in_use)
3134 gui_remove_scrollbars();
3135#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003136 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003137 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003138 tp->tp_firstwin = firstwin;
3139 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003140 tp->tp_old_Rows = Rows;
3141 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003142 firstwin = NULL;
3143 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003144 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003145}
3146
3147/*
3148 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003149 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003150 */
3151/*ARGSUSED*/
3152 static void
3153enter_tabpage(tp, old_curbuf)
3154 tabpage_T *tp;
3155 buf_T *old_curbuf;
3156{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003157 int old_off = tp->tp_firstwin->w_winrow;
3158
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003159 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003160 firstwin = tp->tp_firstwin;
3161 lastwin = tp->tp_lastwin;
3162 topframe = tp->tp_topframe;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003163#ifdef FEAT_AUTOCMD
3164 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3165#endif
3166
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003167 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003168 prevwin = tp->tp_prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003169
3170#ifdef FEAT_AUTOCMD
3171 if (old_curbuf != curbuf)
3172 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3173#endif
3174
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003175 last_status(FALSE); /* status line may appear or disappear */
3176 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003177 must_redraw = CLEAR; /* need to redraw everything */
3178#ifdef FEAT_DIFF
3179 diff_need_scrollbind = TRUE;
3180#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003181
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003182 /* The tabpage line may have appeared or disappeared, may need to resize
3183 * the frames for that. When the Vim window was resized need to update
3184 * frame sizes too. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003185 if (curtab->tp_old_Rows != Rows || old_off != firstwin->w_winrow)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003186 shell_new_rows();
3187#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003188 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003189 shell_new_columns(); /* update window widths */
3190#endif
3191
3192#if defined(FEAT_GUI)
3193 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3194 * scrollbars. Have to update them anyway. */
3195 if (gui.in_use)
3196 {
3197 out_flush();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003198 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003199 gui_update_scrollbars(TRUE);
3200 }
3201 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003202#endif
3203
3204 redraw_all_later(CLEAR);
3205}
3206
3207/*
3208 * Go to tab page "n". For ":tab N" and "Ngt".
3209 */
3210 void
3211goto_tabpage(n)
3212 int n;
3213{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003214 tabpage_T *tp;
3215 int i;
3216
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003217 /* If there is only one it can't work. */
3218 if (first_tabpage->tp_next == NULL)
3219 {
3220 if (n > 1)
3221 beep_flush();
3222 return;
3223 }
3224
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003225 if (n == 0)
3226 {
3227 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003228 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003229 tp = first_tabpage;
3230 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003231 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003232 }
3233 else
3234 {
3235 /* Go to tab page "n". */
3236 i = 0;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003237 for (tp = first_tabpage; ++i != n && tp != NULL; tp = tp->tp_next)
3238 ;
3239 if (tp == NULL)
3240 {
3241 beep_flush();
3242 return;
3243 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003244 }
3245
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003246 if (leave_tabpage(tp->tp_curwin->w_buffer) == OK)
3247 {
3248 if (valid_tabpage(tp))
3249 enter_tabpage(tp, curbuf);
3250 else
3251 enter_tabpage(curtab, curbuf);
3252 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003253}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
3255/*
3256 * Go to another window.
3257 * When jumping to another buffer, stop Visual mode. Do this before
3258 * changing windows so we can yank the selection into the '*' register.
3259 * When jumping to another window on the same buffer, adjust its cursor
3260 * position to keep the same Visual area.
3261 */
3262 void
3263win_goto(wp)
3264 win_T *wp;
3265{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003266 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 {
3268 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003269 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 return;
3271 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003272
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273#ifdef FEAT_VISUAL
3274 if (wp->w_buffer != curbuf)
3275 reset_VIsual_and_resel();
3276 else if (VIsual_active)
3277 wp->w_cursor = curwin->w_cursor;
3278#endif
3279
3280#ifdef FEAT_GUI
3281 need_mouse_correct = TRUE;
3282#endif
3283 win_enter(wp, TRUE);
3284}
3285
3286#if defined(FEAT_PERL) || defined(PROTO)
3287/*
3288 * Find window number "winnr" (counting top to bottom).
3289 */
3290 win_T *
3291win_find_nr(winnr)
3292 int winnr;
3293{
3294 win_T *wp;
3295
3296# ifdef FEAT_WINDOWS
3297 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3298 if (--winnr == 0)
3299 break;
3300 return wp;
3301# else
3302 return curwin;
3303# endif
3304}
3305#endif
3306
3307#ifdef FEAT_VERTSPLIT
3308/*
3309 * Move to window above or below "count" times.
3310 */
3311 static void
3312win_goto_ver(up, count)
3313 int up; /* TRUE to go to win above */
3314 long count;
3315{
3316 frame_T *fr;
3317 frame_T *nfr;
3318 frame_T *foundfr;
3319
3320 foundfr = curwin->w_frame;
3321 while (count--)
3322 {
3323 /*
3324 * First go upwards in the tree of frames until we find a upwards or
3325 * downwards neighbor.
3326 */
3327 fr = foundfr;
3328 for (;;)
3329 {
3330 if (fr == topframe)
3331 goto end;
3332 if (up)
3333 nfr = fr->fr_prev;
3334 else
3335 nfr = fr->fr_next;
3336 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3337 break;
3338 fr = fr->fr_parent;
3339 }
3340
3341 /*
3342 * Now go downwards to find the bottom or top frame in it.
3343 */
3344 for (;;)
3345 {
3346 if (nfr->fr_layout == FR_LEAF)
3347 {
3348 foundfr = nfr;
3349 break;
3350 }
3351 fr = nfr->fr_child;
3352 if (nfr->fr_layout == FR_ROW)
3353 {
3354 /* Find the frame at the cursor row. */
3355 while (fr->fr_next != NULL
3356 && frame2win(fr)->w_wincol + fr->fr_width
3357 <= curwin->w_wincol + curwin->w_wcol)
3358 fr = fr->fr_next;
3359 }
3360 if (nfr->fr_layout == FR_COL && up)
3361 while (fr->fr_next != NULL)
3362 fr = fr->fr_next;
3363 nfr = fr;
3364 }
3365 }
3366end:
3367 if (foundfr != NULL)
3368 win_goto(foundfr->fr_win);
3369}
3370
3371/*
3372 * Move to left or right window.
3373 */
3374 static void
3375win_goto_hor(left, count)
3376 int left; /* TRUE to go to left win */
3377 long count;
3378{
3379 frame_T *fr;
3380 frame_T *nfr;
3381 frame_T *foundfr;
3382
3383 foundfr = curwin->w_frame;
3384 while (count--)
3385 {
3386 /*
3387 * First go upwards in the tree of frames until we find a left or
3388 * right neighbor.
3389 */
3390 fr = foundfr;
3391 for (;;)
3392 {
3393 if (fr == topframe)
3394 goto end;
3395 if (left)
3396 nfr = fr->fr_prev;
3397 else
3398 nfr = fr->fr_next;
3399 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3400 break;
3401 fr = fr->fr_parent;
3402 }
3403
3404 /*
3405 * Now go downwards to find the leftmost or rightmost frame in it.
3406 */
3407 for (;;)
3408 {
3409 if (nfr->fr_layout == FR_LEAF)
3410 {
3411 foundfr = nfr;
3412 break;
3413 }
3414 fr = nfr->fr_child;
3415 if (nfr->fr_layout == FR_COL)
3416 {
3417 /* Find the frame at the cursor row. */
3418 while (fr->fr_next != NULL
3419 && frame2win(fr)->w_winrow + fr->fr_height
3420 <= curwin->w_winrow + curwin->w_wrow)
3421 fr = fr->fr_next;
3422 }
3423 if (nfr->fr_layout == FR_ROW && left)
3424 while (fr->fr_next != NULL)
3425 fr = fr->fr_next;
3426 nfr = fr;
3427 }
3428 }
3429end:
3430 if (foundfr != NULL)
3431 win_goto(foundfr->fr_win);
3432}
3433#endif
3434
3435/*
3436 * Make window "wp" the current window.
3437 */
3438 void
3439win_enter(wp, undo_sync)
3440 win_T *wp;
3441 int undo_sync;
3442{
3443 win_enter_ext(wp, undo_sync, FALSE);
3444}
3445
3446/*
3447 * Make window wp the current window.
3448 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3449 * been closed and isn't valid.
3450 */
3451 static void
3452win_enter_ext(wp, undo_sync, curwin_invalid)
3453 win_T *wp;
3454 int undo_sync;
3455 int curwin_invalid;
3456{
3457#ifdef FEAT_AUTOCMD
3458 int other_buffer = FALSE;
3459#endif
3460
3461 if (wp == curwin && !curwin_invalid) /* nothing to do */
3462 return;
3463
3464#ifdef FEAT_AUTOCMD
3465 if (!curwin_invalid)
3466 {
3467 /*
3468 * Be careful: If autocommands delete the window, return now.
3469 */
3470 if (wp->w_buffer != curbuf)
3471 {
3472 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3473 other_buffer = TRUE;
3474 if (!win_valid(wp))
3475 return;
3476 }
3477 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3478 if (!win_valid(wp))
3479 return;
3480# ifdef FEAT_EVAL
3481 /* autocmds may abort script processing */
3482 if (aborting())
3483 return;
3484# endif
3485 }
3486#endif
3487
3488 /* sync undo before leaving the current buffer */
3489 if (undo_sync && curbuf != wp->w_buffer)
3490 u_sync();
3491 /* may have to copy the buffer options when 'cpo' contains 'S' */
3492 if (wp->w_buffer != curbuf)
3493 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3494 if (!curwin_invalid)
3495 {
3496 prevwin = curwin; /* remember for CTRL-W p */
3497 curwin->w_redr_status = TRUE;
3498 }
3499 curwin = wp;
3500 curbuf = wp->w_buffer;
3501 check_cursor();
3502#ifdef FEAT_VIRTUALEDIT
3503 if (!virtual_active())
3504 curwin->w_cursor.coladd = 0;
3505#endif
3506 changed_line_abv_curs(); /* assume cursor position needs updating */
3507
3508 if (curwin->w_localdir != NULL)
3509 {
3510 /* Window has a local directory: Save current directory as global
3511 * directory (unless that was done already) and change to the local
3512 * directory. */
3513 if (globaldir == NULL)
3514 {
3515 char_u cwd[MAXPATHL];
3516
3517 if (mch_dirname(cwd, MAXPATHL) == OK)
3518 globaldir = vim_strsave(cwd);
3519 }
3520 mch_chdir((char *)curwin->w_localdir);
3521 shorten_fnames(TRUE);
3522 }
3523 else if (globaldir != NULL)
3524 {
3525 /* Window doesn't have a local directory and we are not in the global
3526 * directory: Change to the global directory. */
3527 mch_chdir((char *)globaldir);
3528 vim_free(globaldir);
3529 globaldir = NULL;
3530 shorten_fnames(TRUE);
3531 }
3532
3533#ifdef FEAT_AUTOCMD
3534 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3535 if (other_buffer)
3536 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3537#endif
3538
3539#ifdef FEAT_TITLE
3540 maketitle();
3541#endif
3542 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003543 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 if (restart_edit)
3545 redraw_later(VALID); /* causes status line redraw */
3546
3547 /* set window height to desired minimal value */
3548 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3549 win_setheight((int)p_wh);
3550 else if (curwin->w_height == 0)
3551 win_setheight(1);
3552
3553#ifdef FEAT_VERTSPLIT
3554 /* set window width to desired minimal value */
3555 if (curwin->w_width < p_wiw)
3556 win_setwidth((int)p_wiw);
3557#endif
3558
3559#ifdef FEAT_MOUSE
3560 setmouse(); /* in case jumped to/from help buffer */
3561#endif
3562
3563#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)
3564 /* Change directories when the acd option is set on and after
3565 * switching windows. */
3566 if (p_acd && curbuf->b_ffname != NULL
3567 && vim_chdirfile(curbuf->b_ffname) == OK)
3568 shorten_fnames(TRUE);
3569#endif
3570}
3571
3572#endif /* FEAT_WINDOWS */
3573
3574#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3575/*
3576 * Jump to the first open window that contains buffer buf if one exists
3577 * TODO: Alternatively jump to last open window? Dependent from 'splitbelow'?
3578 * Returns pointer to window if it exists, otherwise NULL.
3579 */
3580 win_T *
3581buf_jump_open_win(buf)
3582 buf_T *buf;
3583{
3584# ifdef FEAT_WINDOWS
3585 win_T *wp;
3586
3587 for (wp = firstwin; wp; wp = wp->w_next)
3588 if (wp->w_buffer == buf)
3589 break;
3590 if (wp != NULL)
3591 win_enter(wp, FALSE);
3592 return wp;
3593# else
3594 if (curwin->w_buffer == buf)
3595 return curwin;
3596 return NULL;
3597# endif
3598}
3599#endif
3600
3601/*
3602 * allocate a window structure and link it in the window list
3603 */
3604/*ARGSUSED*/
3605 static win_T *
3606win_alloc(after)
3607 win_T *after;
3608{
3609 win_T *newwin;
3610
3611 /*
3612 * allocate window structure and linesizes arrays
3613 */
3614 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
3615 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
3616 {
3617 vim_free(newwin);
3618 newwin = NULL;
3619 }
3620
3621 if (newwin != NULL)
3622 {
3623 /*
3624 * link the window in the window list
3625 */
3626#ifdef FEAT_WINDOWS
3627 win_append(after, newwin);
3628#endif
3629#ifdef FEAT_VERTSPLIT
3630 newwin->w_wincol = 0;
3631 newwin->w_width = Columns;
3632#endif
3633
3634 /* position the display and the cursor at the top of the file. */
3635 newwin->w_topline = 1;
3636#ifdef FEAT_DIFF
3637 newwin->w_topfill = 0;
3638#endif
3639 newwin->w_botline = 2;
3640 newwin->w_cursor.lnum = 1;
3641#ifdef FEAT_SCROLLBIND
3642 newwin->w_scbind_pos = 1;
3643#endif
3644
3645 /* We won't calculate w_fraction until resizing the window */
3646 newwin->w_fraction = 0;
3647 newwin->w_prev_fraction_row = -1;
3648
3649#ifdef FEAT_GUI
3650 if (gui.in_use)
3651 {
3652 out_flush();
3653 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
3654 SBAR_LEFT, newwin);
3655 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
3656 SBAR_RIGHT, newwin);
3657 }
3658#endif
3659#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003660 /* init w: variables */
3661 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662#endif
3663#ifdef FEAT_FOLDING
3664 foldInitWin(newwin);
3665#endif
3666 }
3667 return newwin;
3668}
3669
3670#if defined(FEAT_WINDOWS) || defined(PROTO)
3671
3672/*
3673 * remove window 'wp' from the window list and free the structure
3674 */
3675 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00003676win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003678 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679{
3680 int i;
3681
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003682#ifdef FEAT_MZSCHEME
3683 mzscheme_window_free(wp);
3684#endif
3685
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686#ifdef FEAT_PERL
3687 perl_win_free(wp);
3688#endif
3689
3690#ifdef FEAT_PYTHON
3691 python_window_free(wp);
3692#endif
3693
3694#ifdef FEAT_TCL
3695 tcl_window_free(wp);
3696#endif
3697
3698#ifdef FEAT_RUBY
3699 ruby_window_free(wp);
3700#endif
3701
3702 clear_winopt(&wp->w_onebuf_opt);
3703 clear_winopt(&wp->w_allbuf_opt);
3704
3705#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003706 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707#endif
3708
3709 if (prevwin == wp)
3710 prevwin = NULL;
3711 win_free_lsize(wp);
3712
3713 for (i = 0; i < wp->w_tagstacklen; ++i)
3714 vim_free(wp->w_tagstack[i].tagname);
3715
3716 vim_free(wp->w_localdir);
3717#ifdef FEAT_SEARCH_EXTRA
3718 vim_free(wp->w_match.regprog);
3719#endif
3720#ifdef FEAT_JUMPLIST
3721 free_jumplist(wp);
3722#endif
3723
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003724#ifdef FEAT_QUICKFIX
3725 qf_free_all(wp);
3726#endif
3727
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728#ifdef FEAT_GUI
3729 if (gui.in_use)
3730 {
3731 out_flush();
3732 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
3733 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
3734 }
3735#endif /* FEAT_GUI */
3736
Bram Moolenaarf740b292006-02-16 22:11:02 +00003737 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 vim_free(wp);
3739}
3740
3741/*
3742 * Append window "wp" in the window list after window "after".
3743 */
3744 static void
3745win_append(after, wp)
3746 win_T *after, *wp;
3747{
3748 win_T *before;
3749
3750 if (after == NULL) /* after NULL is in front of the first */
3751 before = firstwin;
3752 else
3753 before = after->w_next;
3754
3755 wp->w_next = before;
3756 wp->w_prev = after;
3757 if (after == NULL)
3758 firstwin = wp;
3759 else
3760 after->w_next = wp;
3761 if (before == NULL)
3762 lastwin = wp;
3763 else
3764 before->w_prev = wp;
3765}
3766
3767/*
3768 * Remove a window from the window list.
3769 */
3770 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00003771win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003773 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774{
3775 if (wp->w_prev != NULL)
3776 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003777 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003779 else
3780 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 if (wp->w_next != NULL)
3782 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003783 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003785 else
3786 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787}
3788
3789/*
3790 * Append frame "frp" in a frame list after frame "after".
3791 */
3792 static void
3793frame_append(after, frp)
3794 frame_T *after, *frp;
3795{
3796 frp->fr_next = after->fr_next;
3797 after->fr_next = frp;
3798 if (frp->fr_next != NULL)
3799 frp->fr_next->fr_prev = frp;
3800 frp->fr_prev = after;
3801}
3802
3803/*
3804 * Insert frame "frp" in a frame list before frame "before".
3805 */
3806 static void
3807frame_insert(before, frp)
3808 frame_T *before, *frp;
3809{
3810 frp->fr_next = before;
3811 frp->fr_prev = before->fr_prev;
3812 before->fr_prev = frp;
3813 if (frp->fr_prev != NULL)
3814 frp->fr_prev->fr_next = frp;
3815 else
3816 frp->fr_parent->fr_child = frp;
3817}
3818
3819/*
3820 * Remove a frame from a frame list.
3821 */
3822 static void
3823frame_remove(frp)
3824 frame_T *frp;
3825{
3826 if (frp->fr_prev != NULL)
3827 frp->fr_prev->fr_next = frp->fr_next;
3828 else
3829 frp->fr_parent->fr_child = frp->fr_next;
3830 if (frp->fr_next != NULL)
3831 frp->fr_next->fr_prev = frp->fr_prev;
3832}
3833
3834#endif /* FEAT_WINDOWS */
3835
3836/*
3837 * Allocate w_lines[] for window "wp".
3838 * Return FAIL for failure, OK for success.
3839 */
3840 int
3841win_alloc_lines(wp)
3842 win_T *wp;
3843{
3844 wp->w_lines_valid = 0;
3845 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
3846 if (wp->w_lines == NULL)
3847 return FAIL;
3848 return OK;
3849}
3850
3851/*
3852 * free lsize arrays for a window
3853 */
3854 void
3855win_free_lsize(wp)
3856 win_T *wp;
3857{
3858 vim_free(wp->w_lines);
3859 wp->w_lines = NULL;
3860}
3861
3862/*
3863 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00003864 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 */
3866 void
3867shell_new_rows()
3868{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003869 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870
3871 if (firstwin == NULL) /* not initialized yet */
3872 return;
3873#ifdef FEAT_WINDOWS
3874 if (h < frame_minheight(topframe, NULL))
3875 h = frame_minheight(topframe, NULL);
3876 /* First try setting the heights of windows without 'winfixheight'. If
3877 * that doesn't result in the right height, forget about that option. */
3878 frame_new_height(topframe, h, FALSE, TRUE);
3879 if (topframe->fr_height != h)
3880 frame_new_height(topframe, h, FALSE, FALSE);
3881
3882 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
3883#else
3884 if (h < 1)
3885 h = 1;
3886 win_new_height(firstwin, h);
3887#endif
3888 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00003889#ifdef FEAT_WINDOWS
3890 p_ch_used = p_ch;
3891#endif
3892
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893#if 0
3894 /* Disabled: don't want making the screen smaller make a window larger. */
3895 if (p_ea)
3896 win_equal(curwin, FALSE, 'v');
3897#endif
3898}
3899
3900#if defined(FEAT_VERTSPLIT) || defined(PROTO)
3901/*
3902 * Called from win_new_shellsize() after Columns changed.
3903 */
3904 void
3905shell_new_columns()
3906{
3907 if (firstwin == NULL) /* not initialized yet */
3908 return;
3909 frame_new_width(topframe, (int)Columns, FALSE);
3910 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
3911#if 0
3912 /* Disabled: don't want making the screen smaller make a window larger. */
3913 if (p_ea)
3914 win_equal(curwin, FALSE, 'h');
3915#endif
3916}
3917#endif
3918
3919#if defined(FEAT_CMDWIN) || defined(PROTO)
3920/*
3921 * Save the size of all windows in "gap".
3922 */
3923 void
3924win_size_save(gap)
3925 garray_T *gap;
3926
3927{
3928 win_T *wp;
3929
3930 ga_init2(gap, (int)sizeof(int), 1);
3931 if (ga_grow(gap, win_count() * 2) == OK)
3932 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3933 {
3934 ((int *)gap->ga_data)[gap->ga_len++] =
3935 wp->w_width + wp->w_vsep_width;
3936 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
3937 }
3938}
3939
3940/*
3941 * Restore window sizes, but only if the number of windows is still the same.
3942 * Does not free the growarray.
3943 */
3944 void
3945win_size_restore(gap)
3946 garray_T *gap;
3947{
3948 win_T *wp;
3949 int i;
3950
3951 if (win_count() * 2 == gap->ga_len)
3952 {
3953 i = 0;
3954 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3955 {
3956 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
3957 win_setheight_win(((int *)gap->ga_data)[i++], wp);
3958 }
3959 /* recompute the window positions */
3960 (void)win_comp_pos();
3961 }
3962}
3963#endif /* FEAT_CMDWIN */
3964
3965#if defined(FEAT_WINDOWS) || defined(PROTO)
3966/*
3967 * Update the position for all windows, using the width and height of the
3968 * frames.
3969 * Returns the row just after the last window.
3970 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003971 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972win_comp_pos()
3973{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003974 int row = tabpageline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 int col = 0;
3976
3977 frame_comp_pos(topframe, &row, &col);
3978 return row;
3979}
3980
3981/*
3982 * Update the position of the windows in frame "topfrp", using the width and
3983 * height of the frames.
3984 * "*row" and "*col" are the top-left position of the frame. They are updated
3985 * to the bottom-right position plus one.
3986 */
3987 static void
3988frame_comp_pos(topfrp, row, col)
3989 frame_T *topfrp;
3990 int *row;
3991 int *col;
3992{
3993 win_T *wp;
3994 frame_T *frp;
3995#ifdef FEAT_VERTSPLIT
3996 int startcol;
3997 int startrow;
3998#endif
3999
4000 wp = topfrp->fr_win;
4001 if (wp != NULL)
4002 {
4003 if (wp->w_winrow != *row
4004#ifdef FEAT_VERTSPLIT
4005 || wp->w_wincol != *col
4006#endif
4007 )
4008 {
4009 /* position changed, redraw */
4010 wp->w_winrow = *row;
4011#ifdef FEAT_VERTSPLIT
4012 wp->w_wincol = *col;
4013#endif
4014 redraw_win_later(wp, NOT_VALID);
4015 wp->w_redr_status = TRUE;
4016 }
4017 *row += wp->w_height + wp->w_status_height;
4018#ifdef FEAT_VERTSPLIT
4019 *col += wp->w_width + wp->w_vsep_width;
4020#endif
4021 }
4022 else
4023 {
4024#ifdef FEAT_VERTSPLIT
4025 startrow = *row;
4026 startcol = *col;
4027#endif
4028 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4029 {
4030#ifdef FEAT_VERTSPLIT
4031 if (topfrp->fr_layout == FR_ROW)
4032 *row = startrow; /* all frames are at the same row */
4033 else
4034 *col = startcol; /* all frames are at the same col */
4035#endif
4036 frame_comp_pos(frp, row, col);
4037 }
4038 }
4039}
4040
4041#endif /* FEAT_WINDOWS */
4042
4043/*
4044 * Set current window height and take care of repositioning other windows to
4045 * fit around it.
4046 */
4047 void
4048win_setheight(height)
4049 int height;
4050{
4051 win_setheight_win(height, curwin);
4052}
4053
4054/*
4055 * Set the window height of window "win" and take care of repositioning other
4056 * windows to fit around it.
4057 */
4058 void
4059win_setheight_win(height, win)
4060 int height;
4061 win_T *win;
4062{
4063 int row;
4064
4065 if (win == curwin)
4066 {
4067 /* Always keep current window at least one line high, even when
4068 * 'winminheight' is zero. */
4069#ifdef FEAT_WINDOWS
4070 if (height < p_wmh)
4071 height = p_wmh;
4072#endif
4073 if (height == 0)
4074 height = 1;
4075 }
4076
4077#ifdef FEAT_WINDOWS
4078 frame_setheight(win->w_frame, height + win->w_status_height);
4079
4080 /* recompute the window positions */
4081 row = win_comp_pos();
4082#else
4083 if (height > topframe->fr_height)
4084 height = topframe->fr_height;
4085 win->w_height = height;
4086 row = height;
4087#endif
4088
4089 /*
4090 * If there is extra space created between the last window and the command
4091 * line, clear it.
4092 */
4093 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4094 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4095 cmdline_row = row;
4096 msg_row = row;
4097 msg_col = 0;
4098
4099 redraw_all_later(NOT_VALID);
4100}
4101
4102#if defined(FEAT_WINDOWS) || defined(PROTO)
4103
4104/*
4105 * Set the height of a frame to "height" and take care that all frames and
4106 * windows inside it are resized. Also resize frames on the left and right if
4107 * the are in the same FR_ROW frame.
4108 *
4109 * Strategy:
4110 * If the frame is part of a FR_COL frame, try fitting the frame in that
4111 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4112 * go to containing frames to resize them and make room.
4113 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4114 * Check for the minimal height of the FR_ROW frame.
4115 * At the top level we can also use change the command line height.
4116 */
4117 static void
4118frame_setheight(curfrp, height)
4119 frame_T *curfrp;
4120 int height;
4121{
4122 int room; /* total number of lines available */
4123 int take; /* number of lines taken from other windows */
4124 int room_cmdline; /* lines available from cmdline */
4125 int run;
4126 frame_T *frp;
4127 int h;
4128 int room_reserved;
4129
4130 /* If the height already is the desired value, nothing to do. */
4131 if (curfrp->fr_height == height)
4132 return;
4133
4134 if (curfrp->fr_parent == NULL)
4135 {
4136 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004137 if (height > ROWS_AVAIL)
4138 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 if (height > 0)
4140 frame_new_height(curfrp, height, FALSE, FALSE);
4141 }
4142 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4143 {
4144 /* Row of frames: Also need to resize frames left and right of this
4145 * one. First check for the minimal height of these. */
4146 h = frame_minheight(curfrp->fr_parent, NULL);
4147 if (height < h)
4148 height = h;
4149 frame_setheight(curfrp->fr_parent, height);
4150 }
4151 else
4152 {
4153 /*
4154 * Column of frames: try to change only frames in this column.
4155 */
4156#ifdef FEAT_VERTSPLIT
4157 /*
4158 * Do this twice:
4159 * 1: compute room available, if it's not enough try resizing the
4160 * containing frame.
4161 * 2: compute the room available and adjust the height to it.
4162 * Try not to reduce the height of a window with 'winfixheight' set.
4163 */
4164 for (run = 1; run <= 2; ++run)
4165#else
4166 for (;;)
4167#endif
4168 {
4169 room = 0;
4170 room_reserved = 0;
4171 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4172 frp = frp->fr_next)
4173 {
4174 if (frp != curfrp
4175 && frp->fr_win != NULL
4176 && frp->fr_win->w_p_wfh)
4177 room_reserved += frp->fr_height;
4178 room += frp->fr_height;
4179 if (frp != curfrp)
4180 room -= frame_minheight(frp, NULL);
4181 }
4182#ifdef FEAT_VERTSPLIT
4183 if (curfrp->fr_width != Columns)
4184 room_cmdline = 0;
4185 else
4186#endif
4187 {
4188 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4189 + lastwin->w_height + lastwin->w_status_height);
4190 if (room_cmdline < 0)
4191 room_cmdline = 0;
4192 }
4193
4194 if (height <= room + room_cmdline)
4195 break;
4196#ifdef FEAT_VERTSPLIT
4197 if (run == 2 || curfrp->fr_width == Columns)
4198#endif
4199 {
4200 if (height > room + room_cmdline)
4201 height = room + room_cmdline;
4202 break;
4203 }
4204#ifdef FEAT_VERTSPLIT
4205 frame_setheight(curfrp->fr_parent, height
4206 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4207#endif
4208 /*NOTREACHED*/
4209 }
4210
4211 /*
4212 * Compute the number of lines we will take from others frames (can be
4213 * negative!).
4214 */
4215 take = height - curfrp->fr_height;
4216
4217 /* If there is not enough room, also reduce the height of a window
4218 * with 'winfixheight' set. */
4219 if (height > room + room_cmdline - room_reserved)
4220 room_reserved = room + room_cmdline - height;
4221 /* If there is only a 'winfixheight' window and making the
4222 * window smaller, need to make the other window taller. */
4223 if (take < 0 && room - curfrp->fr_height < room_reserved)
4224 room_reserved = 0;
4225
4226 if (take > 0 && room_cmdline > 0)
4227 {
4228 /* use lines from cmdline first */
4229 if (take < room_cmdline)
4230 room_cmdline = take;
4231 take -= room_cmdline;
4232 topframe->fr_height += room_cmdline;
4233 }
4234
4235 /*
4236 * set the current frame to the new height
4237 */
4238 frame_new_height(curfrp, height, FALSE, FALSE);
4239
4240 /*
4241 * First take lines from the frames after the current frame. If
4242 * that is not enough, takes lines from frames above the current
4243 * frame.
4244 */
4245 for (run = 0; run < 2; ++run)
4246 {
4247 if (run == 0)
4248 frp = curfrp->fr_next; /* 1st run: start with next window */
4249 else
4250 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4251 while (frp != NULL && take != 0)
4252 {
4253 h = frame_minheight(frp, NULL);
4254 if (room_reserved > 0
4255 && frp->fr_win != NULL
4256 && frp->fr_win->w_p_wfh)
4257 {
4258 if (room_reserved >= frp->fr_height)
4259 room_reserved -= frp->fr_height;
4260 else
4261 {
4262 if (frp->fr_height - room_reserved > take)
4263 room_reserved = frp->fr_height - take;
4264 take -= frp->fr_height - room_reserved;
4265 frame_new_height(frp, room_reserved, FALSE, FALSE);
4266 room_reserved = 0;
4267 }
4268 }
4269 else
4270 {
4271 if (frp->fr_height - take < h)
4272 {
4273 take -= frp->fr_height - h;
4274 frame_new_height(frp, h, FALSE, FALSE);
4275 }
4276 else
4277 {
4278 frame_new_height(frp, frp->fr_height - take,
4279 FALSE, FALSE);
4280 take = 0;
4281 }
4282 }
4283 if (run == 0)
4284 frp = frp->fr_next;
4285 else
4286 frp = frp->fr_prev;
4287 }
4288 }
4289 }
4290}
4291
4292#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4293/*
4294 * Set current window width and take care of repositioning other windows to
4295 * fit around it.
4296 */
4297 void
4298win_setwidth(width)
4299 int width;
4300{
4301 win_setwidth_win(width, curwin);
4302}
4303
4304 void
4305win_setwidth_win(width, wp)
4306 int width;
4307 win_T *wp;
4308{
4309 /* Always keep current window at least one column wide, even when
4310 * 'winminwidth' is zero. */
4311 if (wp == curwin)
4312 {
4313 if (width < p_wmw)
4314 width = p_wmw;
4315 if (width == 0)
4316 width = 1;
4317 }
4318
4319 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4320
4321 /* recompute the window positions */
4322 (void)win_comp_pos();
4323
4324 redraw_all_later(NOT_VALID);
4325}
4326
4327/*
4328 * Set the width of a frame to "width" and take care that all frames and
4329 * windows inside it are resized. Also resize frames above and below if the
4330 * are in the same FR_ROW frame.
4331 *
4332 * Strategy is similar to frame_setheight().
4333 */
4334 static void
4335frame_setwidth(curfrp, width)
4336 frame_T *curfrp;
4337 int width;
4338{
4339 int room; /* total number of lines available */
4340 int take; /* number of lines taken from other windows */
4341 int run;
4342 frame_T *frp;
4343 int w;
4344
4345 /* If the width already is the desired value, nothing to do. */
4346 if (curfrp->fr_width == width)
4347 return;
4348
4349 if (curfrp->fr_parent == NULL)
4350 /* topframe: can't change width */
4351 return;
4352
4353 if (curfrp->fr_parent->fr_layout == FR_COL)
4354 {
4355 /* Column of frames: Also need to resize frames above and below of
4356 * this one. First check for the minimal width of these. */
4357 w = frame_minwidth(curfrp->fr_parent, NULL);
4358 if (width < w)
4359 width = w;
4360 frame_setwidth(curfrp->fr_parent, width);
4361 }
4362 else
4363 {
4364 /*
4365 * Row of frames: try to change only frames in this row.
4366 *
4367 * Do this twice:
4368 * 1: compute room available, if it's not enough try resizing the
4369 * containing frame.
4370 * 2: compute the room available and adjust the width to it.
4371 */
4372 for (run = 1; run <= 2; ++run)
4373 {
4374 room = 0;
4375 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4376 frp = frp->fr_next)
4377 {
4378 room += frp->fr_width;
4379 if (frp != curfrp)
4380 room -= frame_minwidth(frp, NULL);
4381 }
4382
4383 if (width <= room)
4384 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004385 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 {
4387 if (width > room)
4388 width = room;
4389 break;
4390 }
4391 frame_setwidth(curfrp->fr_parent, width
4392 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4393 }
4394
4395
4396 /*
4397 * Compute the number of lines we will take from others frames (can be
4398 * negative!).
4399 */
4400 take = width - curfrp->fr_width;
4401
4402 /*
4403 * set the current frame to the new width
4404 */
4405 frame_new_width(curfrp, width, FALSE);
4406
4407 /*
4408 * First take lines from the frames right of the current frame. If
4409 * that is not enough, takes lines from frames left of the current
4410 * frame.
4411 */
4412 for (run = 0; run < 2; ++run)
4413 {
4414 if (run == 0)
4415 frp = curfrp->fr_next; /* 1st run: start with next window */
4416 else
4417 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4418 while (frp != NULL && take != 0)
4419 {
4420 w = frame_minwidth(frp, NULL);
4421 if (frp->fr_width - take < w)
4422 {
4423 take -= frp->fr_width - w;
4424 frame_new_width(frp, w, FALSE);
4425 }
4426 else
4427 {
4428 frame_new_width(frp, frp->fr_width - take, FALSE);
4429 take = 0;
4430 }
4431 if (run == 0)
4432 frp = frp->fr_next;
4433 else
4434 frp = frp->fr_prev;
4435 }
4436 }
4437 }
4438}
4439#endif /* FEAT_VERTSPLIT */
4440
4441/*
4442 * Check 'winminheight' for a valid value.
4443 */
4444 void
4445win_setminheight()
4446{
4447 int room;
4448 int first = TRUE;
4449 win_T *wp;
4450
4451 /* loop until there is a 'winminheight' that is possible */
4452 while (p_wmh > 0)
4453 {
4454 /* TODO: handle vertical splits */
4455 room = -p_wh;
4456 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4457 room += wp->w_height - p_wmh;
4458 if (room >= 0)
4459 break;
4460 --p_wmh;
4461 if (first)
4462 {
4463 EMSG(_(e_noroom));
4464 first = FALSE;
4465 }
4466 }
4467}
4468
4469#ifdef FEAT_MOUSE
4470
4471/*
4472 * Status line of dragwin is dragged "offset" lines down (negative is up).
4473 */
4474 void
4475win_drag_status_line(dragwin, offset)
4476 win_T *dragwin;
4477 int offset;
4478{
4479 frame_T *curfr;
4480 frame_T *fr;
4481 int room;
4482 int row;
4483 int up; /* if TRUE, drag status line up, otherwise down */
4484 int n;
4485
4486 fr = dragwin->w_frame;
4487 curfr = fr;
4488 if (fr != topframe) /* more than one window */
4489 {
4490 fr = fr->fr_parent;
4491 /* When the parent frame is not a column of frames, its parent should
4492 * be. */
4493 if (fr->fr_layout != FR_COL)
4494 {
4495 curfr = fr;
4496 if (fr != topframe) /* only a row of windows, may drag statusline */
4497 fr = fr->fr_parent;
4498 }
4499 }
4500
4501 /* If this is the last frame in a column, may want to resize the parent
4502 * frame instead (go two up to skip a row of frames). */
4503 while (curfr != topframe && curfr->fr_next == NULL)
4504 {
4505 if (fr != topframe)
4506 fr = fr->fr_parent;
4507 curfr = fr;
4508 if (fr != topframe)
4509 fr = fr->fr_parent;
4510 }
4511
4512 if (offset < 0) /* drag up */
4513 {
4514 up = TRUE;
4515 offset = -offset;
4516 /* sum up the room of the current frame and above it */
4517 if (fr == curfr)
4518 {
4519 /* only one window */
4520 room = fr->fr_height - frame_minheight(fr, NULL);
4521 }
4522 else
4523 {
4524 room = 0;
4525 for (fr = fr->fr_child; ; fr = fr->fr_next)
4526 {
4527 room += fr->fr_height - frame_minheight(fr, NULL);
4528 if (fr == curfr)
4529 break;
4530 }
4531 }
4532 fr = curfr->fr_next; /* put fr at frame that grows */
4533 }
4534 else /* drag down */
4535 {
4536 up = FALSE;
4537 /*
4538 * Only dragging the last status line can reduce p_ch.
4539 */
4540 room = Rows - cmdline_row;
4541 if (curfr->fr_next == NULL)
4542 room -= 1;
4543 else
4544 room -= p_ch;
4545 if (room < 0)
4546 room = 0;
4547 /* sum up the room of frames below of the current one */
4548 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4549 room += fr->fr_height - frame_minheight(fr, NULL);
4550 fr = curfr; /* put fr at window that grows */
4551 }
4552
4553 if (room < offset) /* Not enough room */
4554 offset = room; /* Move as far as we can */
4555 if (offset <= 0)
4556 return;
4557
4558 /*
4559 * Grow frame fr by "offset" lines.
4560 * Doesn't happen when dragging the last status line up.
4561 */
4562 if (fr != NULL)
4563 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
4564
4565 if (up)
4566 fr = curfr; /* current frame gets smaller */
4567 else
4568 fr = curfr->fr_next; /* next frame gets smaller */
4569
4570 /*
4571 * Now make the other frames smaller.
4572 */
4573 while (fr != NULL && offset > 0)
4574 {
4575 n = frame_minheight(fr, NULL);
4576 if (fr->fr_height - offset <= n)
4577 {
4578 offset -= fr->fr_height - n;
4579 frame_new_height(fr, n, !up, FALSE);
4580 }
4581 else
4582 {
4583 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
4584 break;
4585 }
4586 if (up)
4587 fr = fr->fr_prev;
4588 else
4589 fr = fr->fr_next;
4590 }
4591 row = win_comp_pos();
4592 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4593 cmdline_row = row;
4594 p_ch = Rows - cmdline_row;
4595 if (p_ch < 1)
4596 p_ch = 1;
4597 redraw_all_later(NOT_VALID);
4598 showmode();
4599}
4600
4601#ifdef FEAT_VERTSPLIT
4602/*
4603 * Separator line of dragwin is dragged "offset" lines right (negative is left).
4604 */
4605 void
4606win_drag_vsep_line(dragwin, offset)
4607 win_T *dragwin;
4608 int offset;
4609{
4610 frame_T *curfr;
4611 frame_T *fr;
4612 int room;
4613 int left; /* if TRUE, drag separator line left, otherwise right */
4614 int n;
4615
4616 fr = dragwin->w_frame;
4617 if (fr == topframe) /* only one window (cannot happe?) */
4618 return;
4619 curfr = fr;
4620 fr = fr->fr_parent;
4621 /* When the parent frame is not a row of frames, its parent should be. */
4622 if (fr->fr_layout != FR_ROW)
4623 {
4624 if (fr == topframe) /* only a column of windows (cannot happen?) */
4625 return;
4626 curfr = fr;
4627 fr = fr->fr_parent;
4628 }
4629
4630 /* If this is the last frame in a row, may want to resize a parent
4631 * frame instead. */
4632 while (curfr->fr_next == NULL)
4633 {
4634 if (fr == topframe)
4635 break;
4636 curfr = fr;
4637 fr = fr->fr_parent;
4638 if (fr != topframe)
4639 {
4640 curfr = fr;
4641 fr = fr->fr_parent;
4642 }
4643 }
4644
4645 if (offset < 0) /* drag left */
4646 {
4647 left = TRUE;
4648 offset = -offset;
4649 /* sum up the room of the current frame and left of it */
4650 room = 0;
4651 for (fr = fr->fr_child; ; fr = fr->fr_next)
4652 {
4653 room += fr->fr_width - frame_minwidth(fr, NULL);
4654 if (fr == curfr)
4655 break;
4656 }
4657 fr = curfr->fr_next; /* put fr at frame that grows */
4658 }
4659 else /* drag right */
4660 {
4661 left = FALSE;
4662 /* sum up the room of frames right of the current one */
4663 room = 0;
4664 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4665 room += fr->fr_width - frame_minwidth(fr, NULL);
4666 fr = curfr; /* put fr at window that grows */
4667 }
4668
4669 if (room < offset) /* Not enough room */
4670 offset = room; /* Move as far as we can */
4671 if (offset <= 0) /* No room at all, quit. */
4672 return;
4673
4674 /* grow frame fr by offset lines */
4675 frame_new_width(fr, fr->fr_width + offset, left);
4676
4677 /* shrink other frames: current and at the left or at the right */
4678 if (left)
4679 fr = curfr; /* current frame gets smaller */
4680 else
4681 fr = curfr->fr_next; /* next frame gets smaller */
4682
4683 while (fr != NULL && offset > 0)
4684 {
4685 n = frame_minwidth(fr, NULL);
4686 if (fr->fr_width - offset <= n)
4687 {
4688 offset -= fr->fr_width - n;
4689 frame_new_width(fr, n, !left);
4690 }
4691 else
4692 {
4693 frame_new_width(fr, fr->fr_width - offset, !left);
4694 break;
4695 }
4696 if (left)
4697 fr = fr->fr_prev;
4698 else
4699 fr = fr->fr_next;
4700 }
4701 (void)win_comp_pos();
4702 redraw_all_later(NOT_VALID);
4703}
4704#endif /* FEAT_VERTSPLIT */
4705#endif /* FEAT_MOUSE */
4706
4707#endif /* FEAT_WINDOWS */
4708
4709/*
4710 * Set the height of a window.
4711 * This takes care of the things inside the window, not what happens to the
4712 * window position, the frame or to other windows.
4713 */
4714 static void
4715win_new_height(wp, height)
4716 win_T *wp;
4717 int height;
4718{
4719 linenr_T lnum;
Bram Moolenaar34114692005-01-02 11:28:13 +00004720 linenr_T bot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 int sline, line_size;
Bram Moolenaar34114692005-01-02 11:28:13 +00004722 int space;
4723 int did_below = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724#define FRACTION_MULT 16384L
4725
4726 /* Don't want a negative height. Happens when splitting a tiny window.
4727 * Will equalize heights soon to fix it. */
4728 if (height < 0)
4729 height = 0;
4730
4731 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
4732 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
4733 + FRACTION_MULT / 2) / (long)wp->w_height;
4734
4735 wp->w_height = height;
4736 wp->w_skipcol = 0;
4737
4738 /* Don't change w_topline when height is zero. Don't set w_topline when
4739 * 'scrollbind' is set and this isn't the current window. */
4740 if (height > 0
4741#ifdef FEAT_SCROLLBIND
4742 && (!wp->w_p_scb || wp == curwin)
4743#endif
4744 )
4745 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004746 /*
4747 * Find a value for w_topline that shows the cursor at the same
4748 * relative position in the window as before (more or less).
4749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 lnum = wp->w_cursor.lnum;
4751 if (lnum < 1) /* can happen when starting up */
4752 lnum = 1;
4753 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
4754 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
4755 sline = wp->w_wrow - line_size;
4756 if (sline < 0)
4757 {
4758 /*
4759 * Cursor line would go off top of screen if w_wrow was this high.
4760 */
4761 wp->w_wrow = line_size;
4762 }
4763 else
4764 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004765 space = height;
4766 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004768 space -= line_size;
4769 if (space > 0 && sline <= 0 && !did_below)
4770 {
4771 /* Try to use "~" lines below the text to avoid that text
4772 * is above the window while there are empty lines.
4773 * Subtract the rows below the cursor from "space" and
4774 * give the rest to "sline". */
4775 did_below = TRUE;
4776 bot = wp->w_cursor.lnum;
4777 while (space > 0)
4778 {
4779 if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
4780 space = 0;
4781 else
4782 {
4783#ifdef FEAT_FOLDING
4784 hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
4785#endif
4786 if (bot >= wp->w_buffer->b_ml.ml_line_count)
4787 break;
4788 ++bot;
4789 space -= plines_win(wp, bot, TRUE);
4790 }
4791 }
4792 if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
4793 sline += space;
4794 }
4795 if (sline <= 0)
4796 break;
4797
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798#ifdef FEAT_FOLDING
4799 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
4800 if (lnum == 1)
4801 {
4802 /* first line in buffer is folded */
4803 line_size = 1;
4804 --sline;
4805 break;
4806 }
4807#endif
4808 --lnum;
4809#ifdef FEAT_DIFF
4810 if (lnum == wp->w_topline)
4811 line_size = plines_win_nofill(wp, lnum, TRUE)
4812 + wp->w_topfill;
4813 else
4814#endif
4815 line_size = plines_win(wp, lnum, TRUE);
4816 sline -= line_size;
4817 }
Bram Moolenaar34114692005-01-02 11:28:13 +00004818
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 if (sline < 0)
4820 {
4821 /*
4822 * Line we want at top would go off top of screen. Use next
4823 * line instead.
4824 */
4825#ifdef FEAT_FOLDING
4826 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
4827#endif
4828 lnum++;
4829 wp->w_wrow -= line_size + sline;
4830 }
4831 else if (sline > 0)
4832 {
4833 /* First line of file reached, use that as topline. */
4834 lnum = 1;
4835 wp->w_wrow -= sline;
4836 }
4837 }
4838 set_topline(wp, lnum);
4839 }
4840
4841 if (wp == curwin)
4842 {
4843 if (p_so)
4844 update_topline();
4845 curs_columns(FALSE); /* validate w_wrow */
4846 }
4847 wp->w_prev_fraction_row = wp->w_wrow;
4848
4849 win_comp_scroll(wp);
4850 redraw_win_later(wp, NOT_VALID);
4851#ifdef FEAT_WINDOWS
4852 wp->w_redr_status = TRUE;
4853#endif
4854 invalidate_botline_win(wp);
4855}
4856
4857#ifdef FEAT_VERTSPLIT
4858/*
4859 * Set the width of a window.
4860 */
4861 static void
4862win_new_width(wp, width)
4863 win_T *wp;
4864 int width;
4865{
4866 wp->w_width = width;
4867 wp->w_lines_valid = 0;
4868 changed_line_abv_curs_win(wp);
4869 invalidate_botline_win(wp);
4870 if (wp == curwin)
4871 {
4872 update_topline();
4873 curs_columns(TRUE); /* validate w_wrow */
4874 }
4875 redraw_win_later(wp, NOT_VALID);
4876 wp->w_redr_status = TRUE;
4877}
4878#endif
4879
4880 void
4881win_comp_scroll(wp)
4882 win_T *wp;
4883{
4884 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
4885 if (wp->w_p_scr == 0)
4886 wp->w_p_scr = 1;
4887}
4888
4889/*
4890 * command_height: called whenever p_ch has been changed
4891 */
4892 void
4893command_height(old_p_ch)
4894 long old_p_ch;
4895{
4896#ifdef FEAT_WINDOWS
4897 int h;
4898 frame_T *frp;
4899
Bram Moolenaar05159a02005-02-26 23:04:13 +00004900 /* When passed a negative value use the value of p_ch that we remembered.
4901 * This is needed for when the GUI starts up, we can't be sure in what
4902 * order things happen. */
4903 if (old_p_ch < 0)
4904 old_p_ch = p_ch_used;
4905 p_ch_used = p_ch;
4906
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 /* Find bottom frame with width of screen. */
4908 frp = lastwin->w_frame;
4909# ifdef FEAT_VERTSPLIT
4910 while (frp->fr_width != Columns && frp->fr_parent != NULL)
4911 frp = frp->fr_parent;
4912# endif
4913
4914 /* Avoid changing the height of a window with 'winfixheight' set. */
4915 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
4916 && frp->fr_win->w_p_wfh)
4917 frp = frp->fr_prev;
4918
4919 if (starting != NO_SCREEN)
4920 {
4921 cmdline_row = Rows - p_ch;
4922
4923 if (p_ch > old_p_ch) /* p_ch got bigger */
4924 {
4925 while (p_ch > old_p_ch)
4926 {
4927 if (frp == NULL)
4928 {
4929 EMSG(_(e_noroom));
4930 p_ch = old_p_ch;
4931 cmdline_row = Rows - p_ch;
4932 break;
4933 }
4934 h = frp->fr_height - frame_minheight(frp, NULL);
4935 if (h > p_ch - old_p_ch)
4936 h = p_ch - old_p_ch;
4937 old_p_ch += h;
4938 frame_add_height(frp, -h);
4939 frp = frp->fr_prev;
4940 }
4941
4942 /* Recompute window positions. */
4943 (void)win_comp_pos();
4944
4945 /* clear the lines added to cmdline */
4946 if (full_screen)
4947 screen_fill((int)(cmdline_row), (int)Rows, 0,
4948 (int)Columns, ' ', ' ', 0);
4949 msg_row = cmdline_row;
4950 redraw_cmdline = TRUE;
4951 return;
4952 }
4953
4954 if (msg_row < cmdline_row)
4955 msg_row = cmdline_row;
4956 redraw_cmdline = TRUE;
4957 }
4958 frame_add_height(frp, (int)(old_p_ch - p_ch));
4959
4960 /* Recompute window positions. */
4961 if (frp != lastwin->w_frame)
4962 (void)win_comp_pos();
4963#else
4964 win_setheight((int)(firstwin->w_height + old_p_ch - p_ch));
4965 cmdline_row = Rows - p_ch;
4966#endif
4967}
4968
4969#if defined(FEAT_WINDOWS) || defined(PROTO)
4970/*
4971 * Resize frame "frp" to be "n" lines higher (negative for less high).
4972 * Also resize the frames it is contained in.
4973 */
4974 static void
4975frame_add_height(frp, n)
4976 frame_T *frp;
4977 int n;
4978{
4979 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
4980 for (;;)
4981 {
4982 frp = frp->fr_parent;
4983 if (frp == NULL)
4984 break;
4985 frp->fr_height += n;
4986 }
4987}
4988
4989/*
4990 * Add or remove a status line for the bottom window(s), according to the
4991 * value of 'laststatus'.
4992 */
4993 void
4994last_status(morewin)
4995 int morewin; /* pretend there are two or more windows */
4996{
4997 /* Don't make a difference between horizontal or vertical split. */
4998 last_status_rec(topframe, (p_ls == 2
4999 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5000}
5001
5002 static void
5003last_status_rec(fr, statusline)
5004 frame_T *fr;
5005 int statusline;
5006{
5007 frame_T *fp;
5008 win_T *wp;
5009
5010 if (fr->fr_layout == FR_LEAF)
5011 {
5012 wp = fr->fr_win;
5013 if (wp->w_status_height != 0 && !statusline)
5014 {
5015 /* remove status line */
5016 win_new_height(wp, wp->w_height + 1);
5017 wp->w_status_height = 0;
5018 comp_col();
5019 }
5020 else if (wp->w_status_height == 0 && statusline)
5021 {
5022 /* Find a frame to take a line from. */
5023 fp = fr;
5024 while (fp->fr_height <= frame_minheight(fp, NULL))
5025 {
5026 if (fp == topframe)
5027 {
5028 EMSG(_(e_noroom));
5029 return;
5030 }
5031 /* In a column of frames: go to frame above. If already at
5032 * the top or in a row of frames: go to parent. */
5033 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5034 fp = fp->fr_prev;
5035 else
5036 fp = fp->fr_parent;
5037 }
5038 wp->w_status_height = 1;
5039 if (fp != fr)
5040 {
5041 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5042 frame_fix_height(wp);
5043 (void)win_comp_pos();
5044 }
5045 else
5046 win_new_height(wp, wp->w_height - 1);
5047 comp_col();
5048 redraw_all_later(NOT_VALID);
5049 }
5050 }
5051#ifdef FEAT_VERTSPLIT
5052 else if (fr->fr_layout == FR_ROW)
5053 {
5054 /* vertically split windows, set status line for each one */
5055 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5056 last_status_rec(fp, statusline);
5057 }
5058#endif
5059 else
5060 {
5061 /* horizontally split window, set status line for last one */
5062 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5063 ;
5064 last_status_rec(fp, statusline);
5065 }
5066}
5067
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005068/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005069 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005070 */
5071 int
5072tabpageline_height()
5073{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005074 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005075 {
5076 case 0: return 0;
5077 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5078 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005079 return 1;
5080}
5081
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082#endif /* FEAT_WINDOWS */
5083
5084#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5085/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005086 * Get the file name at the cursor.
5087 * If Visual mode is active, use the selected text if it's in one line.
5088 * Returns the name in allocated memory, NULL for failure.
5089 */
5090 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005091grab_file_name(count, file_lnum)
5092 long count;
5093 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005094{
5095# ifdef FEAT_VISUAL
5096 if (VIsual_active)
5097 {
5098 int len;
5099 char_u *ptr;
5100
5101 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5102 return NULL;
5103 return find_file_name_in_path(ptr, len,
5104 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5105 }
5106# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005107 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5108 file_lnum);
5109
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005110}
5111
5112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 * Return the file name under or after the cursor.
5114 *
5115 * The 'path' option is searched if the file name is not absolute.
5116 * The string returned has been alloc'ed and should be freed by the caller.
5117 * NULL is returned if the file name or file is not found.
5118 *
5119 * options:
5120 * FNAME_MESS give error messages
5121 * FNAME_EXP expand to path
5122 * FNAME_HYP check for hypertext link
5123 * FNAME_INCL apply "includeexpr"
5124 */
5125 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005126file_name_at_cursor(options, count, file_lnum)
5127 int options;
5128 long count;
5129 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130{
5131 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005132 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5133 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134}
5135
5136/*
5137 * Return the name of the file under or after ptr[col].
5138 * Otherwise like file_name_at_cursor().
5139 */
5140 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005141file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 char_u *line;
5143 int col;
5144 int options;
5145 long count;
5146 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005147 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148{
5149 char_u *ptr;
5150 int len;
5151
5152 /*
5153 * search forward for what could be the start of a file name
5154 */
5155 ptr = line + col;
5156 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005157 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if (*ptr == NUL) /* nothing found */
5159 {
5160 if (options & FNAME_MESS)
5161 EMSG(_("E446: No file name under cursor"));
5162 return NULL;
5163 }
5164
5165 /*
5166 * Search backward for first char of the file name.
5167 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5168 */
5169 while (ptr > line)
5170 {
5171#ifdef FEAT_MBYTE
5172 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5173 ptr -= len + 1;
5174 else
5175#endif
5176 if (vim_isfilec(ptr[-1])
5177 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5178 --ptr;
5179 else
5180 break;
5181 }
5182
5183 /*
5184 * Search forward for the last char of the file name.
5185 * Also allow "://" when ':' is not in 'isfname'.
5186 */
5187 len = 0;
5188 while (vim_isfilec(ptr[len])
5189 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5190#ifdef FEAT_MBYTE
5191 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005192 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 else
5194#endif
5195 ++len;
5196
5197 /*
5198 * If there is trailing punctuation, remove it.
5199 * But don't remove "..", could be a directory name.
5200 */
5201 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5202 && ptr[len - 2] != '.')
5203 --len;
5204
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005205 if (file_lnum != NULL)
5206 {
5207 char_u *p;
5208
5209 /* Get the number after the file name and a separator character */
5210 p = ptr + len;
5211 p = skipwhite(p);
5212 if (*p != NUL)
5213 {
5214 if (!isdigit(*p))
5215 ++p; /* skip the separator */
5216 p = skipwhite(p);
5217 if (isdigit(*p))
5218 *file_lnum = (int)getdigits(&p);
5219 }
5220 }
5221
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5223}
5224
5225# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5226static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5227
5228 static char_u *
5229eval_includeexpr(ptr, len)
5230 char_u *ptr;
5231 int len;
5232{
5233 char_u *res;
5234
5235 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005236 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005237 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 set_vim_var_string(VV_FNAME, NULL, 0);
5239 return res;
5240}
5241#endif
5242
5243/*
5244 * Return the name of the file ptr[len] in 'path'.
5245 * Otherwise like file_name_at_cursor().
5246 */
5247 char_u *
5248find_file_name_in_path(ptr, len, options, count, rel_fname)
5249 char_u *ptr;
5250 int len;
5251 int options;
5252 long count;
5253 char_u *rel_fname; /* file we are searching relative to */
5254{
5255 char_u *file_name;
5256 int c;
5257# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5258 char_u *tofree = NULL;
5259
5260 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5261 {
5262 tofree = eval_includeexpr(ptr, len);
5263 if (tofree != NULL)
5264 {
5265 ptr = tofree;
5266 len = (int)STRLEN(ptr);
5267 }
5268 }
5269# endif
5270
5271 if (options & FNAME_EXP)
5272 {
5273 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5274 TRUE, rel_fname);
5275
5276# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5277 /*
5278 * If the file could not be found in a normal way, try applying
5279 * 'includeexpr' (unless done already).
5280 */
5281 if (file_name == NULL
5282 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5283 {
5284 tofree = eval_includeexpr(ptr, len);
5285 if (tofree != NULL)
5286 {
5287 ptr = tofree;
5288 len = (int)STRLEN(ptr);
5289 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5290 TRUE, rel_fname);
5291 }
5292 }
5293# endif
5294 if (file_name == NULL && (options & FNAME_MESS))
5295 {
5296 c = ptr[len];
5297 ptr[len] = NUL;
5298 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5299 ptr[len] = c;
5300 }
5301
5302 /* Repeat finding the file "count" times. This matters when it
5303 * appears several times in the path. */
5304 while (file_name != NULL && --count > 0)
5305 {
5306 vim_free(file_name);
5307 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5308 }
5309 }
5310 else
5311 file_name = vim_strnsave(ptr, len);
5312
5313# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5314 vim_free(tofree);
5315# endif
5316
5317 return file_name;
5318}
5319#endif /* FEAT_SEARCHPATH */
5320
5321/*
5322 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5323 * Also check for ":\\", which MS Internet Explorer accepts, return
5324 * URL_BACKSLASH.
5325 */
5326 static int
5327path_is_url(p)
5328 char_u *p;
5329{
5330 if (STRNCMP(p, "://", (size_t)3) == 0)
5331 return URL_SLASH;
5332 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5333 return URL_BACKSLASH;
5334 return 0;
5335}
5336
5337/*
5338 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5339 * Return URL_BACKSLASH for "name:\\".
5340 * Return zero otherwise.
5341 */
5342 int
5343path_with_url(fname)
5344 char_u *fname;
5345{
5346 char_u *p;
5347
5348 for (p = fname; isalpha(*p); ++p)
5349 ;
5350 return path_is_url(p);
5351}
5352
5353/*
5354 * Return TRUE if "name" is a full (absolute) path name or URL.
5355 */
5356 int
5357vim_isAbsName(name)
5358 char_u *name;
5359{
5360 return (path_with_url(name) != 0 || mch_isFullName(name));
5361}
5362
5363/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005364 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 *
5366 * return FAIL for failure, OK otherwise
5367 */
5368 int
5369vim_FullName(fname, buf, len, force)
5370 char_u *fname, *buf;
5371 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005372 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373{
5374 int retval = OK;
5375 int url;
5376
5377 *buf = NUL;
5378 if (fname == NULL)
5379 return FAIL;
5380
5381 url = path_with_url(fname);
5382 if (!url)
5383 retval = mch_FullName(fname, buf, len, force);
5384 if (url || retval == FAIL)
5385 {
5386 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005387 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
5389#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5390 slash_adjust(buf);
5391#endif
5392 return retval;
5393}
5394
5395/*
5396 * Return the minimal number of rows that is needed on the screen to display
5397 * the current number of windows.
5398 */
5399 int
5400min_rows()
5401{
5402 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005403#ifdef FEAT_WINDOWS
5404 tabpage_T *tp;
5405 int n;
5406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407
5408 if (firstwin == NULL) /* not initialized yet */
5409 return MIN_LINES;
5410
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005412 total = 0;
5413 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5414 {
5415 n = frame_minheight(tp->tp_topframe, NULL);
5416 if (total < n)
5417 total = n;
5418 }
5419 total += tabpageline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005421 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005423 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 return total;
5425}
5426
5427/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005428 * Return TRUE if there is only one window (in the current tab page), not
5429 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 */
5431 int
5432only_one_window()
5433{
5434#ifdef FEAT_WINDOWS
5435 int count = 0;
5436 win_T *wp;
5437
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005438 /* If there is another tab page there always is another window. */
5439 if (first_tabpage->tp_next != NULL)
5440 return FALSE;
5441
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005443 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444# ifdef FEAT_QUICKFIX
5445 || wp->w_p_pvw
5446# endif
5447 ) || wp == curwin)
5448 ++count;
5449 return (count <= 1);
5450#else
5451 return TRUE;
5452#endif
5453}
5454
5455#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5456/*
5457 * Correct the cursor line number in other windows. Used after changing the
5458 * current buffer, and before applying autocommands.
5459 * When "do_curwin" is TRUE, also check current window.
5460 */
5461 void
5462check_lnums(do_curwin)
5463 int do_curwin;
5464{
5465 win_T *wp;
5466
5467#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005468 tabpage_T *tp;
5469
5470 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5472#else
5473 wp = curwin;
5474 if (do_curwin)
5475#endif
5476 {
5477 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5478 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5479 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5480 wp->w_topline = curbuf->b_ml.ml_line_count;
5481 }
5482}
5483#endif
5484
5485#if defined(FEAT_WINDOWS) || defined(PROTO)
5486
5487/*
5488 * A snapshot of the window sizes, to restore them after closing the help
5489 * window.
5490 * Only these fields are used:
5491 * fr_layout
5492 * fr_width
5493 * fr_height
5494 * fr_next
5495 * fr_child
5496 * fr_win (only valid for the old curwin, NULL otherwise)
5497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498
5499/*
5500 * Create a snapshot of the current frame sizes.
5501 */
5502 static void
5503make_snapshot()
5504{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005505 clear_snapshot(curtab);
5506 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507}
5508
5509 static void
5510make_snapshot_rec(fr, frp)
5511 frame_T *fr;
5512 frame_T **frp;
5513{
5514 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5515 if (*frp == NULL)
5516 return;
5517 (*frp)->fr_layout = fr->fr_layout;
5518# ifdef FEAT_VERTSPLIT
5519 (*frp)->fr_width = fr->fr_width;
5520# endif
5521 (*frp)->fr_height = fr->fr_height;
5522 if (fr->fr_next != NULL)
5523 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
5524 if (fr->fr_child != NULL)
5525 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
5526 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
5527 (*frp)->fr_win = curwin;
5528}
5529
5530/*
5531 * Remove any existing snapshot.
5532 */
5533 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005534clear_snapshot(tp)
5535 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005537 clear_snapshot_rec(tp->tp_snapshot);
5538 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539}
5540
5541 static void
5542clear_snapshot_rec(fr)
5543 frame_T *fr;
5544{
5545 if (fr != NULL)
5546 {
5547 clear_snapshot_rec(fr->fr_next);
5548 clear_snapshot_rec(fr->fr_child);
5549 vim_free(fr);
5550 }
5551}
5552
5553/*
5554 * Restore a previously created snapshot, if there is any.
5555 * This is only done if the screen size didn't change and the window layout is
5556 * still the same.
5557 */
5558 static void
5559restore_snapshot(close_curwin)
5560 int close_curwin; /* closing current window */
5561{
5562 win_T *wp;
5563
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005564 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005566 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005568 && curtab->tp_snapshot->fr_height == topframe->fr_height
5569 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005571 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 win_comp_pos();
5573 if (wp != NULL && close_curwin)
5574 win_goto(wp);
5575 redraw_all_later(CLEAR);
5576 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005577 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578}
5579
5580/*
5581 * Check if frames "sn" and "fr" have the same layout, same following frames
5582 * and same children.
5583 */
5584 static int
5585check_snapshot_rec(sn, fr)
5586 frame_T *sn;
5587 frame_T *fr;
5588{
5589 if (sn->fr_layout != fr->fr_layout
5590 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
5591 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
5592 || (sn->fr_next != NULL
5593 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
5594 || (sn->fr_child != NULL
5595 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
5596 return FAIL;
5597 return OK;
5598}
5599
5600/*
5601 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
5602 * following frames and children.
5603 * Returns a pointer to the old current window, or NULL.
5604 */
5605 static win_T *
5606restore_snapshot_rec(sn, fr)
5607 frame_T *sn;
5608 frame_T *fr;
5609{
5610 win_T *wp = NULL;
5611 win_T *wp2;
5612
5613 fr->fr_height = sn->fr_height;
5614# ifdef FEAT_VERTSPLIT
5615 fr->fr_width = sn->fr_width;
5616# endif
5617 if (fr->fr_layout == FR_LEAF)
5618 {
5619 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
5620# ifdef FEAT_VERTSPLIT
5621 frame_new_width(fr, fr->fr_width, FALSE);
5622# endif
5623 wp = sn->fr_win;
5624 }
5625 if (sn->fr_next != NULL)
5626 {
5627 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
5628 if (wp2 != NULL)
5629 wp = wp2;
5630 }
5631 if (sn->fr_child != NULL)
5632 {
5633 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
5634 if (wp2 != NULL)
5635 wp = wp2;
5636 }
5637 return wp;
5638}
5639
5640#endif
5641
5642#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5643/*
5644 * Return TRUE if there is any vertically split window.
5645 */
5646 int
5647win_hasvertsplit()
5648{
5649 frame_T *fr;
5650
5651 if (topframe->fr_layout == FR_ROW)
5652 return TRUE;
5653
5654 if (topframe->fr_layout == FR_COL)
5655 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
5656 if (fr->fr_layout == FR_ROW)
5657 return TRUE;
5658
5659 return FALSE;
5660}
5661#endif