blob: 116a5fc7f9d3600947791a094d09405beeee2bdb [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 Moolenaar071d4272004-06-13 20:20:40 +000019static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
20static void frame_setheight __ARGS((frame_T *curfrp, int height));
21#ifdef FEAT_VERTSPLIT
22static void frame_setwidth __ARGS((frame_T *curfrp, int width));
23#endif
24static void win_exchange __ARGS((long));
25static void win_rotate __ARGS((int, int));
26static void win_totop __ARGS((int size, int flags));
27static 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 Moolenaar0a5fe212005-06-24 23:01:23 +000028static win_T *win_free_mem __ARGS((win_T *win, int *dirp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000029static win_T *winframe_remove __ARGS((win_T *win, int *dirp));
30static frame_T *win_altframe __ARGS((win_T *win));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000031static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000032static win_T *frame2win __ARGS((frame_T *frp));
33static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
34static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
35static int frame_fixed_height __ARGS((frame_T *frp));
36#ifdef FEAT_VERTSPLIT
37static void frame_add_statusline __ARGS((frame_T *frp));
38static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst));
39static void frame_add_vsep __ARGS((frame_T *frp));
40static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
41static void frame_fix_width __ARGS((win_T *wp));
42#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000043#endif
44static int win_alloc_firstwin __ARGS((void));
45#if defined(FEAT_WINDOWS) || defined(PROTO)
46static tabpage_T *current_tabpage __ARGS((void));
47static void leave_tabpage __ARGS((tabpage_T *tp));
48static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000049static void frame_fix_height __ARGS((win_T *wp));
50static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
51static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
52static void win_free __ARGS((win_T *wp));
53static void win_append __ARGS((win_T *, win_T *));
54static void win_remove __ARGS((win_T *));
55static void frame_append __ARGS((frame_T *after, frame_T *frp));
56static void frame_insert __ARGS((frame_T *before, frame_T *frp));
57static void frame_remove __ARGS((frame_T *frp));
58#ifdef FEAT_VERTSPLIT
59static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060static void win_goto_ver __ARGS((int up, long count));
61static void win_goto_hor __ARGS((int left, long count));
62#endif
63static void frame_add_height __ARGS((frame_T *frp, int n));
64static void last_status_rec __ARGS((frame_T *fr, int statusline));
65
66static void make_snapshot __ARGS((void));
67static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
68static void clear_snapshot __ARGS((void));
69static void clear_snapshot_rec __ARGS((frame_T *fr));
70static void restore_snapshot __ARGS((int close_curwin));
71static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
72static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
73
74#endif /* FEAT_WINDOWS */
75static win_T *win_alloc __ARGS((win_T *after));
76static void win_new_height __ARGS((win_T *, int));
77
78#define URL_SLASH 1 /* path_is_url() has found "://" */
79#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
80
81#define NOWIN (win_T *)-1 /* non-exisiting window */
82
Bram Moolenaar05159a02005-02-26 23:04:13 +000083#ifdef FEAT_WINDOWS
84static long p_ch_used = 1L; /* value of 'cmdheight' when frame
85 size was set */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000086# define ROWS_AVAIL (Rows - p_ch - tabpageline_height())
87#else
88# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000089#endif
90
Bram Moolenaar071d4272004-06-13 20:20:40 +000091#if defined(FEAT_WINDOWS) || defined(PROTO)
92/*
93 * all CTRL-W window commands are handled here, called from normal_cmd().
94 */
95 void
96do_window(nchar, Prenum, xchar)
97 int nchar;
98 long Prenum;
99 int xchar; /* extra char from ":wincmd gx" or NUL */
100{
101 long Prenum1;
102 win_T *wp;
103#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
104 char_u *ptr;
105#endif
106#ifdef FEAT_FIND_ID
107 int type = FIND_DEFINE;
108 int len;
109#endif
110 char_u cbuf[40];
111
112 if (Prenum == 0)
113 Prenum1 = 1;
114 else
115 Prenum1 = Prenum;
116
117#ifdef FEAT_CMDWIN
118# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
119#else
120# define CHECK_CMDWIN
121#endif
122
123 switch (nchar)
124 {
125/* split current window in two parts, horizontally */
126 case 'S':
127 case Ctrl_S:
128 case 's':
129 CHECK_CMDWIN
130#ifdef FEAT_VISUAL
131 reset_VIsual_and_resel(); /* stop Visual mode */
132#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000133#ifdef FEAT_QUICKFIX
134 /* When splitting the quickfix window open a new buffer in it,
135 * don't replicate the quickfix buffer. */
136 if (bt_quickfix(curbuf))
137 goto newwindow;
138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#ifdef FEAT_GUI
140 need_mouse_correct = TRUE;
141#endif
142 win_split((int)Prenum, 0);
143 break;
144
145#ifdef FEAT_VERTSPLIT
146/* split current window in two parts, vertically */
147 case Ctrl_V:
148 case 'v':
149 CHECK_CMDWIN
150#ifdef FEAT_VISUAL
151 reset_VIsual_and_resel(); /* stop Visual mode */
152#endif
153#ifdef FEAT_GUI
154 need_mouse_correct = TRUE;
155#endif
156 win_split((int)Prenum, WSP_VERT);
157 break;
158#endif
159
160/* split current window and edit alternate file */
161 case Ctrl_HAT:
162 case '^':
163 CHECK_CMDWIN
164#ifdef FEAT_VISUAL
165 reset_VIsual_and_resel(); /* stop Visual mode */
166#endif
167 STRCPY(cbuf, "split #");
168 if (Prenum)
169 sprintf((char *)cbuf + 7, "%ld", Prenum);
170 do_cmdline_cmd(cbuf);
171 break;
172
173/* open new window */
174 case Ctrl_N:
175 case 'n':
176 CHECK_CMDWIN
177#ifdef FEAT_VISUAL
178 reset_VIsual_and_resel(); /* stop Visual mode */
179#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000180#ifdef FEAT_QUICKFIX
181newwindow:
182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 if (Prenum)
184 sprintf((char *)cbuf, "%ld", Prenum); /* window height */
185 else
186 cbuf[0] = NUL;
187 STRCAT(cbuf, "new");
188 do_cmdline_cmd(cbuf);
189 break;
190
191/* quit current window */
192 case Ctrl_Q:
193 case 'q':
194#ifdef FEAT_VISUAL
195 reset_VIsual_and_resel(); /* stop Visual mode */
196#endif
197 do_cmdline_cmd((char_u *)"quit");
198 break;
199
200/* close current window */
201 case Ctrl_C:
202 case 'c':
203#ifdef FEAT_VISUAL
204 reset_VIsual_and_resel(); /* stop Visual mode */
205#endif
206 do_cmdline_cmd((char_u *)"close");
207 break;
208
209#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
210/* close preview window */
211 case Ctrl_Z:
212 case 'z':
213 CHECK_CMDWIN
214#ifdef FEAT_VISUAL
215 reset_VIsual_and_resel(); /* stop Visual mode */
216#endif
217 do_cmdline_cmd((char_u *)"pclose");
218 break;
219
220/* cursor to preview window */
221 case 'P':
222 for (wp = firstwin; wp != NULL; wp = wp->w_next)
223 if (wp->w_p_pvw)
224 break;
225 if (wp == NULL)
226 EMSG(_("E441: There is no preview window"));
227 else
228 win_goto(wp);
229 break;
230#endif
231
232/* close all but current window */
233 case Ctrl_O:
234 case 'o':
235 CHECK_CMDWIN
236#ifdef FEAT_VISUAL
237 reset_VIsual_and_resel(); /* stop Visual mode */
238#endif
239 do_cmdline_cmd((char_u *)"only");
240 break;
241
242/* cursor to next window with wrap around */
243 case Ctrl_W:
244 case 'w':
245/* cursor to previous window with wrap around */
246 case 'W':
247 CHECK_CMDWIN
248 if (lastwin == firstwin && Prenum != 1) /* just one window */
249 beep_flush();
250 else
251 {
252 if (Prenum) /* go to specified window */
253 {
254 for (wp = firstwin; --Prenum > 0; )
255 {
256 if (wp->w_next == NULL)
257 break;
258 else
259 wp = wp->w_next;
260 }
261 }
262 else
263 {
264 if (nchar == 'W') /* go to previous window */
265 {
266 wp = curwin->w_prev;
267 if (wp == NULL)
268 wp = lastwin; /* wrap around */
269 }
270 else /* go to next window */
271 {
272 wp = curwin->w_next;
273 if (wp == NULL)
274 wp = firstwin; /* wrap around */
275 }
276 }
277 win_goto(wp);
278 }
279 break;
280
281/* cursor to window below */
282 case 'j':
283 case K_DOWN:
284 case Ctrl_J:
285 CHECK_CMDWIN
286#ifdef FEAT_VERTSPLIT
287 win_goto_ver(FALSE, Prenum1);
288#else
289 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
290 wp = wp->w_next)
291 ;
292 win_goto(wp);
293#endif
294 break;
295
296/* cursor to window above */
297 case 'k':
298 case K_UP:
299 case Ctrl_K:
300 CHECK_CMDWIN
301#ifdef FEAT_VERTSPLIT
302 win_goto_ver(TRUE, Prenum1);
303#else
304 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
305 wp = wp->w_prev)
306 ;
307 win_goto(wp);
308#endif
309 break;
310
311#ifdef FEAT_VERTSPLIT
312/* cursor to left window */
313 case 'h':
314 case K_LEFT:
315 case Ctrl_H:
316 case K_BS:
317 CHECK_CMDWIN
318 win_goto_hor(TRUE, Prenum1);
319 break;
320
321/* cursor to right window */
322 case 'l':
323 case K_RIGHT:
324 case Ctrl_L:
325 CHECK_CMDWIN
326 win_goto_hor(FALSE, Prenum1);
327 break;
328#endif
329
330/* cursor to top-left window */
331 case 't':
332 case Ctrl_T:
333 win_goto(firstwin);
334 break;
335
336/* cursor to bottom-right window */
337 case 'b':
338 case Ctrl_B:
339 win_goto(lastwin);
340 break;
341
342/* cursor to last accessed (previous) window */
343 case 'p':
344 case Ctrl_P:
345 if (prevwin == NULL)
346 beep_flush();
347 else
348 win_goto(prevwin);
349 break;
350
351/* exchange current and next window */
352 case 'x':
353 case Ctrl_X:
354 CHECK_CMDWIN
355 win_exchange(Prenum);
356 break;
357
358/* rotate windows downwards */
359 case Ctrl_R:
360 case 'r':
361 CHECK_CMDWIN
362#ifdef FEAT_VISUAL
363 reset_VIsual_and_resel(); /* stop Visual mode */
364#endif
365 win_rotate(FALSE, (int)Prenum1); /* downwards */
366 break;
367
368/* rotate windows upwards */
369 case 'R':
370 CHECK_CMDWIN
371#ifdef FEAT_VISUAL
372 reset_VIsual_and_resel(); /* stop Visual mode */
373#endif
374 win_rotate(TRUE, (int)Prenum1); /* upwards */
375 break;
376
377/* move window to the very top/bottom/left/right */
378 case 'K':
379 case 'J':
380#ifdef FEAT_VERTSPLIT
381 case 'H':
382 case 'L':
383#endif
384 CHECK_CMDWIN
385 win_totop((int)Prenum,
386 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
387 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
388 break;
389
390/* make all windows the same height */
391 case '=':
392#ifdef FEAT_GUI
393 need_mouse_correct = TRUE;
394#endif
395 win_equal(NULL, FALSE, 'b');
396 break;
397
398/* increase current window height */
399 case '+':
400#ifdef FEAT_GUI
401 need_mouse_correct = TRUE;
402#endif
403 win_setheight(curwin->w_height + (int)Prenum1);
404 break;
405
406/* decrease current window height */
407 case '-':
408#ifdef FEAT_GUI
409 need_mouse_correct = TRUE;
410#endif
411 win_setheight(curwin->w_height - (int)Prenum1);
412 break;
413
414/* set current window height */
415 case Ctrl__:
416 case '_':
417#ifdef FEAT_GUI
418 need_mouse_correct = TRUE;
419#endif
420 win_setheight(Prenum ? (int)Prenum : 9999);
421 break;
422
423#ifdef FEAT_VERTSPLIT
424/* increase current window width */
425 case '>':
426#ifdef FEAT_GUI
427 need_mouse_correct = TRUE;
428#endif
429 win_setwidth(curwin->w_width + (int)Prenum1);
430 break;
431
432/* decrease current window width */
433 case '<':
434#ifdef FEAT_GUI
435 need_mouse_correct = TRUE;
436#endif
437 win_setwidth(curwin->w_width - (int)Prenum1);
438 break;
439
440/* set current window width */
441 case '|':
442#ifdef FEAT_GUI
443 need_mouse_correct = TRUE;
444#endif
445 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
446 break;
447#endif
448
449/* jump to tag and split window if tag exists (in preview window) */
450#if defined(FEAT_QUICKFIX)
451 case '}':
452 CHECK_CMDWIN
453 if (Prenum)
454 g_do_tagpreview = Prenum;
455 else
456 g_do_tagpreview = p_pvh;
457 /*FALLTHROUGH*/
458#endif
459 case ']':
460 case Ctrl_RSB:
461 CHECK_CMDWIN
462#ifdef FEAT_VISUAL
463 reset_VIsual_and_resel(); /* stop Visual mode */
464#endif
465 if (Prenum)
466 postponed_split = Prenum;
467 else
468 postponed_split = -1;
469
470 /* Execute the command right here, required when
471 * "wincmd ]" was used in a function. */
472 do_nv_ident(Ctrl_RSB, NUL);
473 break;
474
475#ifdef FEAT_SEARCHPATH
476/* edit file name under cursor in a new window */
477 case 'f':
478 case Ctrl_F:
479 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000480
481 ptr = grab_file_name(Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 if (ptr != NULL)
483 {
484#ifdef FEAT_GUI
485 need_mouse_correct = TRUE;
486#endif
487 setpcmark();
488 if (win_split(0, 0) == OK)
489 {
490# ifdef FEAT_SCROLLBIND
491 curwin->w_p_scb = FALSE;
492# endif
493 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
494 ECMD_HIDE);
495 }
496 vim_free(ptr);
497 }
498 break;
499#endif
500
501#ifdef FEAT_FIND_ID
502/* Go to the first occurence of the identifier under cursor along path in a
503 * new window -- webb
504 */
505 case 'i': /* Go to any match */
506 case Ctrl_I:
507 type = FIND_ANY;
508 /* FALLTHROUGH */
509 case 'd': /* Go to definition, using 'define' */
510 case Ctrl_D:
511 CHECK_CMDWIN
512 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
513 break;
514 find_pattern_in_path(ptr, 0, len, TRUE,
515 Prenum == 0 ? TRUE : FALSE, type,
516 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
517 curwin->w_set_curswant = TRUE;
518 break;
519#endif
520
Bram Moolenaar05159a02005-02-26 23:04:13 +0000521 case K_KENTER:
522 case CAR:
523#if defined(FEAT_QUICKFIX)
524 /*
525 * In a quickfix window a <CR> jumps to the error under the
526 * cursor in a new window.
527 */
528 if (bt_quickfix(curbuf))
529 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000530 sprintf((char *)cbuf, "split +%ld%s",
531 (long)curwin->w_cursor.lnum,
532 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000533 do_cmdline_cmd(cbuf);
534 }
535#endif
536 break;
537
538
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539/* CTRL-W g extended commands */
540 case 'g':
541 case Ctrl_G:
542 CHECK_CMDWIN
543#ifdef USE_ON_FLY_SCROLL
544 dont_scroll = TRUE; /* disallow scrolling here */
545#endif
546 ++no_mapping;
547 ++allow_keys; /* no mapping for xchar, but allow key codes */
548 if (xchar == NUL)
549 xchar = safe_vgetc();
550#ifdef FEAT_LANGMAP
551 LANGMAP_ADJUST(xchar, TRUE);
552#endif
553 --no_mapping;
554 --allow_keys;
555#ifdef FEAT_CMDL_INFO
556 (void)add_to_showcmd(xchar);
557#endif
558 switch (xchar)
559 {
560#if defined(FEAT_QUICKFIX)
561 case '}':
562 xchar = Ctrl_RSB;
563 if (Prenum)
564 g_do_tagpreview = Prenum;
565 else
566 g_do_tagpreview = p_pvh;
567 /*FALLTHROUGH*/
568#endif
569 case ']':
570 case Ctrl_RSB:
571#ifdef FEAT_VISUAL
572 reset_VIsual_and_resel(); /* stop Visual mode */
573#endif
574 if (Prenum)
575 postponed_split = Prenum;
576 else
577 postponed_split = -1;
578
579 /* Execute the command right here, required when
580 * "wincmd g}" was used in a function. */
581 do_nv_ident('g', xchar);
582 break;
583
584 default:
585 beep_flush();
586 break;
587 }
588 break;
589
590 default: beep_flush();
591 break;
592 }
593}
594
595/*
596 * split the current window, implements CTRL-W s and :split
597 *
598 * "size" is the height or width for the new window, 0 to use half of current
599 * height or width.
600 *
601 * "flags":
602 * WSP_ROOM: require enough room for new window
603 * WSP_VERT: vertical split.
604 * WSP_TOP: open window at the top-left of the shell (help window).
605 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
606 * WSP_HELP: creating the help window, keep layout snapshot
607 *
608 * return FAIL for failure, OK otherwise
609 */
610 int
611win_split(size, flags)
612 int size;
613 int flags;
614{
615 /* Add flags from ":vertical", ":topleft" and ":botright". */
616 flags |= cmdmod.split;
617 if ((flags & WSP_TOP) && (flags & WSP_BOT))
618 {
619 EMSG(_("E442: Can't split topleft and botright at the same time"));
620 return FAIL;
621 }
622
623 /* When creating the help window make a snapshot of the window layout.
624 * Otherwise clear the snapshot, it's now invalid. */
625 if (flags & WSP_HELP)
626 make_snapshot();
627 else
628 clear_snapshot();
629
630 return win_split_ins(size, flags, NULL, 0);
631}
632
633/*
634 * When "newwin" is NULL: split a window in two.
635 * When "newwin" is not NULL: insert this window at the far
636 * top/left/right/bottom.
637 * return FAIL for failure, OK otherwise
638 */
639 static int
640win_split_ins(size, flags, newwin, dir)
641 int size;
642 int flags;
643 win_T *newwin;
644 int dir;
645{
646 win_T *wp = newwin;
647 win_T *oldwin;
648 int new_size = size;
649 int i;
650 int need_status = 0;
651 int do_equal = FALSE;
652 int needed;
653 int available;
654 int oldwin_height = 0;
655 int layout;
656 frame_T *frp, *curfrp;
657 int before;
658
659 if (flags & WSP_TOP)
660 oldwin = firstwin;
661 else if (flags & WSP_BOT)
662 oldwin = lastwin;
663 else
664 oldwin = curwin;
665
666 /* add a status line when p_ls == 1 and splitting the first window */
667 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
668 {
669 if (oldwin->w_height <= p_wmh && newwin == NULL)
670 {
671 EMSG(_(e_noroom));
672 return FAIL;
673 }
674 need_status = STATUS_HEIGHT;
675 }
676
677#ifdef FEAT_VERTSPLIT
678 if (flags & WSP_VERT)
679 {
680 layout = FR_ROW;
681 do_equal = (p_ea && new_size == 0 && *p_ead != 'v');
682
683 /*
684 * Check if we are able to split the current window and compute its
685 * width.
686 */
687 needed = p_wmw + 1;
688 if (flags & WSP_ROOM)
689 needed += p_wiw - p_wmw;
690 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
691 {
692 available = topframe->fr_width;
693 needed += frame_minwidth(topframe, NULL);
694 }
695 else
696 available = oldwin->w_width;
697 if (available < needed && newwin == NULL)
698 {
699 EMSG(_(e_noroom));
700 return FAIL;
701 }
702 if (new_size == 0)
703 new_size = oldwin->w_width / 2;
704 if (new_size > oldwin->w_width - p_wmw - 1)
705 new_size = oldwin->w_width - p_wmw - 1;
706 if (new_size < p_wmw)
707 new_size = p_wmw;
708
709 /* if it doesn't fit in the current window, need win_equal() */
710 if (oldwin->w_width - new_size - 1 < p_wmw)
711 do_equal = TRUE;
712 }
713 else
714#endif
715 {
716 layout = FR_COL;
717 do_equal = (p_ea && new_size == 0
718#ifdef FEAT_VERTSPLIT
719 && *p_ead != 'h'
720#endif
721 );
722
723 /*
724 * Check if we are able to split the current window and compute its
725 * height.
726 */
727 needed = p_wmh + STATUS_HEIGHT + need_status;
728 if (flags & WSP_ROOM)
729 needed += p_wh - p_wmh;
730 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
731 {
732 available = topframe->fr_height;
733 needed += frame_minheight(topframe, NULL);
734 }
735 else
736 {
737 available = oldwin->w_height;
738 needed += p_wmh;
739 }
740 if (available < needed && newwin == NULL)
741 {
742 EMSG(_(e_noroom));
743 return FAIL;
744 }
745 oldwin_height = oldwin->w_height;
746 if (need_status)
747 {
748 oldwin->w_status_height = STATUS_HEIGHT;
749 oldwin_height -= STATUS_HEIGHT;
750 }
751 if (new_size == 0)
752 new_size = oldwin_height / 2;
753
754 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
755 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
756 if (new_size < p_wmh)
757 new_size = p_wmh;
758
759 /* if it doesn't fit in the current window, need win_equal() */
760 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
761 do_equal = TRUE;
762
763 /* We don't like to take lines for the new window from a
764 * 'winfixheight' window. Take them from a window above or below
765 * instead, if possible. */
766 if (oldwin->w_p_wfh)
767 {
768 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
769 oldwin);
770 oldwin_height = oldwin->w_height;
771 if (need_status)
772 oldwin_height -= STATUS_HEIGHT;
773 }
774 }
775
776 /*
777 * allocate new window structure and link it in the window list
778 */
779 if ((flags & WSP_TOP) == 0
780 && ((flags & WSP_BOT)
781 || (flags & WSP_BELOW)
782 || (!(flags & WSP_ABOVE)
783 && (
784#ifdef FEAT_VERTSPLIT
785 (flags & WSP_VERT) ? p_spr :
786#endif
787 p_sb))))
788 {
789 /* new window below/right of current one */
790 if (newwin == NULL)
791 wp = win_alloc(oldwin);
792 else
793 win_append(oldwin, wp);
794 }
795 else
796 {
797 if (newwin == NULL)
798 wp = win_alloc(oldwin->w_prev);
799 else
800 win_append(oldwin->w_prev, wp);
801 }
802
803 if (newwin == NULL)
804 {
805 if (wp == NULL)
806 return FAIL;
807
808 /*
809 * make the contents of the new window the same as the current one
810 */
811 wp->w_buffer = curbuf;
812 curbuf->b_nwindows++;
813 wp->w_cursor = curwin->w_cursor;
814 wp->w_valid = 0;
815 wp->w_curswant = curwin->w_curswant;
816 wp->w_set_curswant = curwin->w_set_curswant;
817 wp->w_topline = curwin->w_topline;
818#ifdef FEAT_DIFF
819 wp->w_topfill = curwin->w_topfill;
820#endif
821 wp->w_leftcol = curwin->w_leftcol;
822 wp->w_pcmark = curwin->w_pcmark;
823 wp->w_prev_pcmark = curwin->w_prev_pcmark;
824 wp->w_alt_fnum = curwin->w_alt_fnum;
825 wp->w_fraction = curwin->w_fraction;
826 wp->w_prev_fraction_row = curwin->w_prev_fraction_row;
827#ifdef FEAT_JUMPLIST
828 copy_jumplist(curwin, wp);
829#endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000830#ifdef FEAT_QUICKFIX
831 copy_loclist(curwin, wp);
832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 if (curwin->w_localdir != NULL)
834 wp->w_localdir = vim_strsave(curwin->w_localdir);
835
836 /* Use the same argument list. */
837 wp->w_alist = curwin->w_alist;
838 ++wp->w_alist->al_refcount;
839 wp->w_arg_idx = curwin->w_arg_idx;
840
841 /*
842 * copy tagstack and options from existing window
843 */
844 for (i = 0; i < curwin->w_tagstacklen; i++)
845 {
846 wp->w_tagstack[i] = curwin->w_tagstack[i];
847 if (wp->w_tagstack[i].tagname != NULL)
848 wp->w_tagstack[i].tagname =
849 vim_strsave(wp->w_tagstack[i].tagname);
850 }
851 wp->w_tagstackidx = curwin->w_tagstackidx;
852 wp->w_tagstacklen = curwin->w_tagstacklen;
853 win_copy_options(curwin, wp);
854#ifdef FEAT_FOLDING
855 copyFoldingState(curwin, wp);
856#endif
857 }
858
859 /*
860 * Reorganise the tree of frames to insert the new window.
861 */
862 if (flags & (WSP_TOP | WSP_BOT))
863 {
864#ifdef FEAT_VERTSPLIT
865 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
866 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
867#else
868 if (topframe->fr_layout == FR_COL)
869#endif
870 {
871 curfrp = topframe->fr_child;
872 if (flags & WSP_BOT)
873 while (curfrp->fr_next != NULL)
874 curfrp = curfrp->fr_next;
875 }
876 else
877 curfrp = topframe;
878 before = (flags & WSP_TOP);
879 }
880 else
881 {
882 curfrp = oldwin->w_frame;
883 if (flags & WSP_BELOW)
884 before = FALSE;
885 else if (flags & WSP_ABOVE)
886 before = TRUE;
887 else
888#ifdef FEAT_VERTSPLIT
889 if (flags & WSP_VERT)
890 before = !p_spr;
891 else
892#endif
893 before = !p_sb;
894 }
895 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
896 {
897 /* Need to create a new frame in the tree to make a branch. */
898 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
899 *frp = *curfrp;
900 curfrp->fr_layout = layout;
901 frp->fr_parent = curfrp;
902 frp->fr_next = NULL;
903 frp->fr_prev = NULL;
904 curfrp->fr_child = frp;
905 curfrp->fr_win = NULL;
906 curfrp = frp;
907 if (frp->fr_win != NULL)
908 oldwin->w_frame = frp;
909 else
910 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
911 frp->fr_parent = curfrp;
912 }
913
914 if (newwin == NULL)
915 {
916 /* Create a frame for the new window. */
917 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
918 frp->fr_layout = FR_LEAF;
919 frp->fr_win = wp;
920 wp->w_frame = frp;
921 }
922 else
923 frp = newwin->w_frame;
924 frp->fr_parent = curfrp->fr_parent;
925
926 /* Insert the new frame at the right place in the frame list. */
927 if (before)
928 frame_insert(curfrp, frp);
929 else
930 frame_append(curfrp, frp);
931
932#ifdef FEAT_VERTSPLIT
933 if (flags & WSP_VERT)
934 {
935 wp->w_p_scr = curwin->w_p_scr;
936 if (need_status)
937 {
938 --oldwin->w_height;
939 oldwin->w_status_height = need_status;
940 }
941 if (flags & (WSP_TOP | WSP_BOT))
942 {
943 /* set height and row of new window to full height */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000944 wp->w_winrow = tabpageline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 wp->w_height = curfrp->fr_height - (p_ls > 0);
946 wp->w_status_height = (p_ls > 0);
947 }
948 else
949 {
950 /* height and row of new window is same as current window */
951 wp->w_winrow = oldwin->w_winrow;
952 wp->w_height = oldwin->w_height;
953 wp->w_status_height = oldwin->w_status_height;
954 }
955 frp->fr_height = curfrp->fr_height;
956
957 /* "new_size" of the current window goes to the new window, use
958 * one column for the vertical separator */
959 wp->w_width = new_size;
960 if (before)
961 wp->w_vsep_width = 1;
962 else
963 {
964 wp->w_vsep_width = oldwin->w_vsep_width;
965 oldwin->w_vsep_width = 1;
966 }
967 if (flags & (WSP_TOP | WSP_BOT))
968 {
969 if (flags & WSP_BOT)
970 frame_add_vsep(curfrp);
971 /* Set width of neighbor frame */
972 frame_new_width(curfrp, curfrp->fr_width
973 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP);
974 }
975 else
976 oldwin->w_width -= new_size + 1;
977 if (before) /* new window left of current one */
978 {
979 wp->w_wincol = oldwin->w_wincol;
980 oldwin->w_wincol += new_size + 1;
981 }
982 else /* new window right of current one */
983 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
984 frame_fix_width(oldwin);
985 frame_fix_width(wp);
986 }
987 else
988#endif
989 {
990 /* width and column of new window is same as current window */
991#ifdef FEAT_VERTSPLIT
992 if (flags & (WSP_TOP | WSP_BOT))
993 {
994 wp->w_wincol = 0;
995 wp->w_width = Columns;
996 wp->w_vsep_width = 0;
997 }
998 else
999 {
1000 wp->w_wincol = oldwin->w_wincol;
1001 wp->w_width = oldwin->w_width;
1002 wp->w_vsep_width = oldwin->w_vsep_width;
1003 }
1004 frp->fr_width = curfrp->fr_width;
1005#endif
1006
1007 /* "new_size" of the current window goes to the new window, use
1008 * one row for the status line */
1009 win_new_height(wp, new_size);
1010 if (flags & (WSP_TOP | WSP_BOT))
1011 frame_new_height(curfrp, curfrp->fr_height
1012 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1013 else
1014 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1015 if (before) /* new window above current one */
1016 {
1017 wp->w_winrow = oldwin->w_winrow;
1018 wp->w_status_height = STATUS_HEIGHT;
1019 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1020 }
1021 else /* new window below current one */
1022 {
1023 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1024 wp->w_status_height = oldwin->w_status_height;
1025 oldwin->w_status_height = STATUS_HEIGHT;
1026 }
1027#ifdef FEAT_VERTSPLIT
1028 if (flags & WSP_BOT)
1029 frame_add_statusline(curfrp);
1030#endif
1031 frame_fix_height(wp);
1032 frame_fix_height(oldwin);
1033 }
1034
1035 if (flags & (WSP_TOP | WSP_BOT))
1036 (void)win_comp_pos();
1037
1038 /*
1039 * Both windows need redrawing
1040 */
1041 redraw_win_later(wp, NOT_VALID);
1042 wp->w_redr_status = TRUE;
1043 redraw_win_later(oldwin, NOT_VALID);
1044 oldwin->w_redr_status = TRUE;
1045
1046 if (need_status)
1047 {
1048 msg_row = Rows - 1;
1049 msg_col = sc_col;
1050 msg_clr_eos_force(); /* Old command/ruler may still be there */
1051 comp_col();
1052 msg_row = Rows - 1;
1053 msg_col = 0; /* put position back at start of line */
1054 }
1055
1056 /*
1057 * make the new window the current window and redraw
1058 */
1059 if (do_equal || dir != 0)
1060 win_equal(wp, TRUE,
1061#ifdef FEAT_VERTSPLIT
1062 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1063 : dir == 'h' ? 'b' :
1064#endif
1065 'v');
1066
1067 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1068 * size was given. */
1069#ifdef FEAT_VERTSPLIT
1070 if (flags & WSP_VERT)
1071 {
1072 i = p_wiw;
1073 if (size != 0)
1074 p_wiw = size;
1075
1076# ifdef FEAT_GUI
1077 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1078 if (gui.in_use)
1079 gui_init_which_components(NULL);
1080# endif
1081 }
1082 else
1083#endif
1084 {
1085 i = p_wh;
1086 if (size != 0)
1087 p_wh = size;
1088 }
1089 win_enter(wp, FALSE);
1090#ifdef FEAT_VERTSPLIT
1091 if (flags & WSP_VERT)
1092 p_wiw = i;
1093 else
1094#endif
1095 p_wh = i;
1096
1097 return OK;
1098}
1099
1100#endif /* FEAT_WINDOWS */
1101
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102#if defined(FEAT_WINDOWS) || defined(PROTO)
1103/*
1104 * Check if "win" is a pointer to an existing window.
1105 */
1106 int
1107win_valid(win)
1108 win_T *win;
1109{
1110 win_T *wp;
1111
1112 if (win == NULL)
1113 return FALSE;
1114 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1115 if (wp == win)
1116 return TRUE;
1117 return FALSE;
1118}
1119
1120/*
1121 * Return the number of windows.
1122 */
1123 int
1124win_count()
1125{
1126 win_T *wp;
1127 int count = 0;
1128
1129 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1130 ++count;
1131 return count;
1132}
1133
1134/*
1135 * Make "count" windows on the screen.
1136 * Return actual number of windows on the screen.
1137 * Must be called when there is just one window, filling the whole screen
1138 * (excluding the command line).
1139 */
1140/*ARGSUSED*/
1141 int
1142make_windows(count, vertical)
1143 int count;
1144 int vertical; /* split windows vertically if TRUE */
1145{
1146 int maxcount;
1147 int todo;
1148
1149#ifdef FEAT_VERTSPLIT
1150 if (vertical)
1151 {
1152 /* Each windows needs at least 'winminwidth' lines and a separator
1153 * column. */
1154 maxcount = (curwin->w_width + curwin->w_vsep_width
1155 - (p_wiw - p_wmw)) / (p_wmw + 1);
1156 }
1157 else
1158#endif
1159 {
1160 /* Each window needs at least 'winminheight' lines and a status line. */
1161 maxcount = (curwin->w_height + curwin->w_status_height
1162 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1163 }
1164
1165 if (maxcount < 2)
1166 maxcount = 2;
1167 if (count > maxcount)
1168 count = maxcount;
1169
1170 /*
1171 * add status line now, otherwise first window will be too big
1172 */
1173 if (count > 1)
1174 last_status(TRUE);
1175
1176#ifdef FEAT_AUTOCMD
1177 /*
1178 * Don't execute autocommands while creating the windows. Must do that
1179 * when putting the buffers in the windows.
1180 */
1181 ++autocmd_block;
1182#endif
1183
1184 /* todo is number of windows left to create */
1185 for (todo = count - 1; todo > 0; --todo)
1186#ifdef FEAT_VERTSPLIT
1187 if (vertical)
1188 {
1189 if (win_split(curwin->w_width - (curwin->w_width - todo)
1190 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1191 break;
1192 }
1193 else
1194#endif
1195 {
1196 if (win_split(curwin->w_height - (curwin->w_height - todo
1197 * STATUS_HEIGHT) / (todo + 1)
1198 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1199 break;
1200 }
1201
1202#ifdef FEAT_AUTOCMD
1203 --autocmd_block;
1204#endif
1205
1206 /* return actual number of windows */
1207 return (count - todo);
1208}
1209
1210/*
1211 * Exchange current and next window
1212 */
1213 static void
1214win_exchange(Prenum)
1215 long Prenum;
1216{
1217 frame_T *frp;
1218 frame_T *frp2;
1219 win_T *wp;
1220 win_T *wp2;
1221 int temp;
1222
1223 if (lastwin == firstwin) /* just one window */
1224 {
1225 beep_flush();
1226 return;
1227 }
1228
1229#ifdef FEAT_GUI
1230 need_mouse_correct = TRUE;
1231#endif
1232
1233 /*
1234 * find window to exchange with
1235 */
1236 if (Prenum)
1237 {
1238 frp = curwin->w_frame->fr_parent->fr_child;
1239 while (frp != NULL && --Prenum > 0)
1240 frp = frp->fr_next;
1241 }
1242 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1243 frp = curwin->w_frame->fr_next;
1244 else /* Swap last window in row/col with previous */
1245 frp = curwin->w_frame->fr_prev;
1246
1247 /* We can only exchange a window with another window, not with a frame
1248 * containing windows. */
1249 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1250 return;
1251 wp = frp->fr_win;
1252
1253/*
1254 * 1. remove curwin from the list. Remember after which window it was in wp2
1255 * 2. insert curwin before wp in the list
1256 * if wp != wp2
1257 * 3. remove wp from the list
1258 * 4. insert wp after wp2
1259 * 5. exchange the status line height and vsep width.
1260 */
1261 wp2 = curwin->w_prev;
1262 frp2 = curwin->w_frame->fr_prev;
1263 if (wp->w_prev != curwin)
1264 {
1265 win_remove(curwin);
1266 frame_remove(curwin->w_frame);
1267 win_append(wp->w_prev, curwin);
1268 frame_insert(frp, curwin->w_frame);
1269 }
1270 if (wp != wp2)
1271 {
1272 win_remove(wp);
1273 frame_remove(wp->w_frame);
1274 win_append(wp2, wp);
1275 if (frp2 == NULL)
1276 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1277 else
1278 frame_append(frp2, wp->w_frame);
1279 }
1280 temp = curwin->w_status_height;
1281 curwin->w_status_height = wp->w_status_height;
1282 wp->w_status_height = temp;
1283#ifdef FEAT_VERTSPLIT
1284 temp = curwin->w_vsep_width;
1285 curwin->w_vsep_width = wp->w_vsep_width;
1286 wp->w_vsep_width = temp;
1287
1288 /* If the windows are not in the same frame, exchange the sizes to avoid
1289 * messing up the window layout. Otherwise fix the frame sizes. */
1290 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1291 {
1292 temp = curwin->w_height;
1293 curwin->w_height = wp->w_height;
1294 wp->w_height = temp;
1295 temp = curwin->w_width;
1296 curwin->w_width = wp->w_width;
1297 wp->w_width = temp;
1298 }
1299 else
1300 {
1301 frame_fix_height(curwin);
1302 frame_fix_height(wp);
1303 frame_fix_width(curwin);
1304 frame_fix_width(wp);
1305 }
1306#endif
1307
1308 (void)win_comp_pos(); /* recompute window positions */
1309
1310 win_enter(wp, TRUE);
1311 redraw_later(CLEAR);
1312}
1313
1314/*
1315 * rotate windows: if upwards TRUE the second window becomes the first one
1316 * if upwards FALSE the first window becomes the second one
1317 */
1318 static void
1319win_rotate(upwards, count)
1320 int upwards;
1321 int count;
1322{
1323 win_T *wp1;
1324 win_T *wp2;
1325 frame_T *frp;
1326 int n;
1327
1328 if (firstwin == lastwin) /* nothing to do */
1329 {
1330 beep_flush();
1331 return;
1332 }
1333
1334#ifdef FEAT_GUI
1335 need_mouse_correct = TRUE;
1336#endif
1337
1338#ifdef FEAT_VERTSPLIT
1339 /* Check if all frames in this row/col have one window. */
1340 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1341 frp = frp->fr_next)
1342 if (frp->fr_win == NULL)
1343 {
1344 EMSG(_("E443: Cannot rotate when another window is split"));
1345 return;
1346 }
1347#endif
1348
1349 while (count--)
1350 {
1351 if (upwards) /* first window becomes last window */
1352 {
1353 /* remove first window/frame from the list */
1354 frp = curwin->w_frame->fr_parent->fr_child;
1355 wp1 = frp->fr_win;
1356 win_remove(wp1);
1357 frame_remove(frp);
1358
1359 /* find last frame and append removed window/frame after it */
1360 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1361 ;
1362 win_append(frp->fr_win, wp1);
1363 frame_append(frp, wp1->w_frame);
1364
1365 wp2 = frp->fr_win; /* previously last window */
1366 }
1367 else /* last window becomes first window */
1368 {
1369 /* find last window/frame in the list and remove it */
1370 for (frp = curwin->w_frame; frp->fr_next != NULL;
1371 frp = frp->fr_next)
1372 ;
1373 wp1 = frp->fr_win;
1374 wp2 = wp1->w_prev; /* will become last window */
1375 win_remove(wp1);
1376 frame_remove(frp);
1377
1378 /* append the removed window/frame before the first in the list */
1379 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1380 frame_insert(frp->fr_parent->fr_child, frp);
1381 }
1382
1383 /* exchange status height and vsep width of old and new last window */
1384 n = wp2->w_status_height;
1385 wp2->w_status_height = wp1->w_status_height;
1386 wp1->w_status_height = n;
1387 frame_fix_height(wp1);
1388 frame_fix_height(wp2);
1389#ifdef FEAT_VERTSPLIT
1390 n = wp2->w_vsep_width;
1391 wp2->w_vsep_width = wp1->w_vsep_width;
1392 wp1->w_vsep_width = n;
1393 frame_fix_width(wp1);
1394 frame_fix_width(wp2);
1395#endif
1396
1397 /* recompute w_winrow and w_wincol for all windows */
1398 (void)win_comp_pos();
1399 }
1400
1401 redraw_later(CLEAR);
1402}
1403
1404/*
1405 * Move the current window to the very top/bottom/left/right of the screen.
1406 */
1407 static void
1408win_totop(size, flags)
1409 int size;
1410 int flags;
1411{
1412 int dir;
1413 int height = curwin->w_height;
1414
1415 if (lastwin == firstwin)
1416 {
1417 beep_flush();
1418 return;
1419 }
1420
1421 /* Remove the window and frame from the tree of frames. */
1422 (void)winframe_remove(curwin, &dir);
1423 win_remove(curwin);
1424 last_status(FALSE); /* may need to remove last status line */
1425 (void)win_comp_pos(); /* recompute window positions */
1426
1427 /* Split a window on the desired side and put the window there. */
1428 (void)win_split_ins(size, flags, curwin, dir);
1429 if (!(flags & WSP_VERT))
1430 {
1431 win_setheight(height);
1432 if (p_ea)
1433 win_equal(curwin, TRUE, 'v');
1434 }
1435
1436#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1437 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1438 * scrollbars. Have to update them anyway. */
1439 if (gui.in_use)
1440 {
1441 out_flush();
1442 gui_init_which_components(NULL);
1443 gui_update_scrollbars(TRUE);
1444 }
1445 need_mouse_correct = TRUE;
1446#endif
1447
1448}
1449
1450/*
1451 * Move window "win1" to below/right of "win2" and make "win1" the current
1452 * window. Only works within the same frame!
1453 */
1454 void
1455win_move_after(win1, win2)
1456 win_T *win1, *win2;
1457{
1458 int height;
1459
1460 /* check if the arguments are reasonable */
1461 if (win1 == win2)
1462 return;
1463
1464 /* check if there is something to do */
1465 if (win2->w_next != win1)
1466 {
1467 /* may need move the status line/vertical separator of the last window
1468 * */
1469 if (win1 == lastwin)
1470 {
1471 height = win1->w_prev->w_status_height;
1472 win1->w_prev->w_status_height = win1->w_status_height;
1473 win1->w_status_height = height;
1474#ifdef FEAT_VERTSPLIT
1475 win1->w_prev->w_vsep_width = 0;
1476 win1->w_vsep_width = 1;
1477#endif
1478 }
1479 else if (win2 == lastwin)
1480 {
1481 height = win1->w_status_height;
1482 win1->w_status_height = win2->w_status_height;
1483 win2->w_status_height = height;
1484#ifdef FEAT_VERTSPLIT
1485 win2->w_vsep_width = 1;
1486 win1->w_vsep_width = 0;
1487#endif
1488 }
1489 win_remove(win1);
1490 frame_remove(win1->w_frame);
1491 win_append(win2, win1);
1492 frame_append(win2->w_frame, win1->w_frame);
1493
1494 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1495 redraw_later(NOT_VALID);
1496 }
1497 win_enter(win1, FALSE);
1498}
1499
1500/*
1501 * Make all windows the same height.
1502 * 'next_curwin' will soon be the current window, make sure it has enough
1503 * rows.
1504 */
1505 void
1506win_equal(next_curwin, current, dir)
1507 win_T *next_curwin; /* pointer to current window to be or NULL */
1508 int current; /* do only frame with current window */
1509 int dir; /* 'v' for vertically, 'h' for horizontally,
1510 'b' for both, 0 for using p_ead */
1511{
1512 if (dir == 0)
1513#ifdef FEAT_VERTSPLIT
1514 dir = *p_ead;
1515#else
1516 dir = 'b';
1517#endif
1518 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001519 topframe, dir, 0, tabpageline_height(),
1520 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521}
1522
1523/*
1524 * Set a frame to a new position and height, spreading the available room
1525 * equally over contained frames.
1526 * The window "next_curwin" (if not NULL) should at least get the size from
1527 * 'winheight' and 'winwidth' if possible.
1528 */
1529 static void
1530win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1531 win_T *next_curwin; /* pointer to current window to be or NULL */
1532 int current; /* do only frame with current window */
1533 frame_T *topfr; /* frame to set size off */
1534 int dir; /* 'v', 'h' or 'b', see win_equal() */
1535 int col; /* horizontal position for frame */
1536 int row; /* vertical position for frame */
1537 int width; /* new width of frame */
1538 int height; /* new height of frame */
1539{
1540 int n, m;
1541 int extra_sep = 0;
1542 int wincount, totwincount = 0;
1543 frame_T *fr;
1544 int next_curwin_size = 0;
1545 int room = 0;
1546 int new_size;
1547 int has_next_curwin = 0;
1548 int hnc;
1549
1550 if (topfr->fr_layout == FR_LEAF)
1551 {
1552 /* Set the width/height of this frame.
1553 * Redraw when size or position changes */
1554 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1555#ifdef FEAT_VERTSPLIT
1556 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1557#endif
1558 )
1559 {
1560 topfr->fr_win->w_winrow = row;
1561 frame_new_height(topfr, height, FALSE, FALSE);
1562#ifdef FEAT_VERTSPLIT
1563 topfr->fr_win->w_wincol = col;
1564 frame_new_width(topfr, width, FALSE);
1565#endif
1566 redraw_all_later(CLEAR);
1567 }
1568 }
1569#ifdef FEAT_VERTSPLIT
1570 else if (topfr->fr_layout == FR_ROW)
1571 {
1572 topfr->fr_width = width;
1573 topfr->fr_height = height;
1574
1575 if (dir != 'v') /* equalize frame widths */
1576 {
1577 /* Compute the maximum number of windows horizontally in this
1578 * frame. */
1579 n = frame_minwidth(topfr, NOWIN);
1580 /* add one for the rightmost window, it doesn't have a separator */
1581 if (col + width == Columns)
1582 extra_sep = 1;
1583 else
1584 extra_sep = 0;
1585 totwincount = (n + extra_sep) / (p_wmw + 1);
1586
1587 /* Compute room available for windows other than "next_curwin" */
1588 m = frame_minwidth(topfr, next_curwin);
1589 room = width - m;
1590 if (room < 0)
1591 {
1592 next_curwin_size = p_wiw + room;
1593 room = 0;
1594 }
1595 else if (n == m) /* doesn't contain curwin */
1596 next_curwin_size = 0;
1597 else
1598 {
1599 next_curwin_size = (room + p_wiw + (totwincount - 1) * p_wmw
1600 + (totwincount - 1)) / totwincount;
1601 if (next_curwin_size > p_wiw)
1602 room -= next_curwin_size - p_wiw;
1603 else
1604 next_curwin_size = p_wiw;
1605 }
1606 if (n != m)
1607 --totwincount; /* don't count curwin */
1608 }
1609
1610 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1611 {
1612 n = m = 0;
1613 wincount = 1;
1614 if (fr->fr_next == NULL)
1615 /* last frame gets all that remains (avoid roundoff error) */
1616 new_size = width;
1617 else if (dir == 'v')
1618 new_size = fr->fr_width;
1619 else
1620 {
1621 /* Compute the maximum number of windows horiz. in "fr". */
1622 n = frame_minwidth(fr, NOWIN);
1623 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1624 / (p_wmw + 1);
1625 m = frame_minwidth(fr, next_curwin);
1626 if (n != m) /* don't count next_curwin */
1627 --wincount;
1628 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1629 / totwincount;
1630 if (n != m) /* add next_curwin size */
1631 {
1632 next_curwin_size -= p_wiw - (m - n);
1633 new_size += next_curwin_size;
1634 }
1635 }
1636
1637 /* Skip frame that is full height when splitting or closing a
1638 * window, unless equalizing all frames. */
1639 if (!current || dir != 'v' || topfr->fr_parent != NULL
1640 || (new_size != fr->fr_width)
1641 || frame_has_win(fr, next_curwin))
1642 win_equal_rec(next_curwin, current, fr, dir, col, row,
1643 new_size + n, height);
1644 col += new_size + n;
1645 width -= new_size + n;
1646 if (n != m) /* contains curwin */
1647 room -= new_size - next_curwin_size;
1648 else
1649 room -= new_size;
1650 totwincount -= wincount;
1651 }
1652 }
1653#endif
1654 else /* topfr->fr_layout == FR_COL */
1655 {
1656#ifdef FEAT_VERTSPLIT
1657 topfr->fr_width = width;
1658#endif
1659 topfr->fr_height = height;
1660
1661 if (dir != 'h') /* equalize frame heights */
1662 {
1663 /* Compute maximum number of windows vertically in this frame. */
1664 n = frame_minheight(topfr, NOWIN);
1665 /* add one for the bottom window if it doesn't have a statusline */
1666 if (row + height == cmdline_row && p_ls == 0)
1667 extra_sep = 1;
1668 else
1669 extra_sep = 0;
1670 totwincount = (n + extra_sep) / (p_wmh + 1);
1671 has_next_curwin = frame_has_win(topfr, next_curwin);
1672
1673 /*
1674 * Compute height for "next_curwin" window and room available for
1675 * other windows.
1676 * "m" is the minimal height when counting p_wh for "next_curwin".
1677 */
1678 m = frame_minheight(topfr, next_curwin);
1679 room = height - m;
1680 if (room < 0)
1681 {
1682 /* The room is less then 'winheight', use all space for the
1683 * current window. */
1684 next_curwin_size = p_wh + room;
1685 room = 0;
1686 }
1687 else
1688 {
1689 next_curwin_size = -1;
1690 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1691 {
1692 /* If 'winfixheight' set keep the window height if
1693 * possible.
1694 * Watch out for this window being the next_curwin. */
1695 if (frame_fixed_height(fr))
1696 {
1697 n = frame_minheight(fr, NOWIN);
1698 new_size = fr->fr_height;
1699 if (frame_has_win(fr, next_curwin))
1700 {
1701 room += p_wh - p_wmh;
1702 next_curwin_size = 0;
1703 if (new_size < p_wh)
1704 new_size = p_wh;
1705 }
1706 else
1707 /* These windows don't use up room. */
1708 totwincount -= (n + (fr->fr_next == NULL
1709 ? extra_sep : 0)) / (p_wmh + 1);
1710 room -= new_size - n;
1711 if (room < 0)
1712 {
1713 new_size += room;
1714 room = 0;
1715 }
1716 fr->fr_newheight = new_size;
1717 }
1718 }
1719 if (next_curwin_size == -1)
1720 {
1721 if (!has_next_curwin)
1722 next_curwin_size = 0;
1723 else if (totwincount > 1
1724 && (room + (totwincount - 2))
1725 / (totwincount - 1) > p_wh)
1726 {
1727 next_curwin_size = (room + p_wh + totwincount * p_wmh
1728 + (totwincount - 1)) / totwincount;
1729 room -= next_curwin_size - p_wh;
1730 }
1731 else
1732 next_curwin_size = p_wh;
1733 }
1734 }
1735
1736 if (has_next_curwin)
1737 --totwincount; /* don't count curwin */
1738 }
1739
1740 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1741 {
1742 n = m = 0;
1743 wincount = 1;
1744 if (fr->fr_next == NULL)
1745 /* last frame gets all that remains (avoid roundoff error) */
1746 new_size = height;
1747 else if (dir == 'h')
1748 new_size = fr->fr_height;
1749 else if (frame_fixed_height(fr))
1750 {
1751 new_size = fr->fr_newheight;
1752 wincount = 0; /* doesn't count as a sizeable window */
1753 }
1754 else
1755 {
1756 /* Compute the maximum number of windows vert. in "fr". */
1757 n = frame_minheight(fr, NOWIN);
1758 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1759 / (p_wmh + 1);
1760 m = frame_minheight(fr, next_curwin);
1761 if (has_next_curwin)
1762 hnc = frame_has_win(fr, next_curwin);
1763 else
1764 hnc = FALSE;
1765 if (hnc) /* don't count next_curwin */
1766 --wincount;
1767 if (totwincount == 0)
1768 new_size = room;
1769 else
1770 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1771 / totwincount;
1772 if (hnc) /* add next_curwin size */
1773 {
1774 next_curwin_size -= p_wh - (m - n);
1775 new_size += next_curwin_size;
1776 room -= new_size - next_curwin_size;
1777 }
1778 else
1779 room -= new_size;
1780 new_size += n;
1781 }
1782 /* Skip frame that is full width when splitting or closing a
1783 * window, unless equalizing all frames. */
1784 if (!current || dir != 'h' || topfr->fr_parent != NULL
1785 || (new_size != fr->fr_height)
1786 || frame_has_win(fr, next_curwin))
1787 win_equal_rec(next_curwin, current, fr, dir, col, row,
1788 width, new_size);
1789 row += new_size;
1790 height -= new_size;
1791 totwincount -= wincount;
1792 }
1793 }
1794}
1795
1796/*
1797 * close all windows for buffer 'buf'
1798 */
1799 void
1800close_windows(buf)
1801 buf_T *buf;
1802{
1803 win_T *win;
1804
1805 ++RedrawingDisabled;
1806 for (win = firstwin; win != NULL && lastwin != firstwin; )
1807 {
1808 if (win->w_buffer == buf)
1809 {
1810 win_close(win, FALSE);
1811 win = firstwin; /* go back to the start */
1812 }
1813 else
1814 win = win->w_next;
1815 }
1816 --RedrawingDisabled;
1817}
1818
1819/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001820 * Return TRUE if the current window is the only window that exists.
1821 * Returns FALSE if there is a window in another tab page.
1822 */
1823 int
1824last_window()
1825{
1826 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
1827}
1828
1829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 * close window "win"
1831 * If "free_buf" is TRUE related buffer may be unloaded.
1832 *
1833 * called by :quit, :close, :xit, :wq and findtag()
1834 */
1835 void
1836win_close(win, free_buf)
1837 win_T *win;
1838 int free_buf;
1839{
1840 win_T *wp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001841 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842#ifdef FEAT_AUTOCMD
1843 int other_buffer = FALSE;
1844#endif
1845 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 int dir;
1847 int help_window = FALSE;
1848
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001849 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 {
1851 EMSG(_("E444: Cannot close last window"));
1852 return;
1853 }
1854
1855 /* When closing the help window, try restoring a snapshot after closing
1856 * the window. Otherwise clear the snapshot, it's now invalid. */
1857 if (win->w_buffer->b_help)
1858 help_window = TRUE;
1859 else
1860 clear_snapshot();
1861
1862#ifdef FEAT_AUTOCMD
1863 if (win == curwin)
1864 {
1865 /*
1866 * Guess which window is going to be the new current window.
1867 * This may change because of the autocommands (sigh).
1868 */
1869 wp = frame2win(win_altframe(win));
1870
1871 /*
1872 * Be careful: If autocommands delete the window, return now.
1873 */
1874 if (wp->w_buffer != curbuf)
1875 {
1876 other_buffer = TRUE;
1877 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001878 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 return;
1880 }
1881 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001882 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 return;
1884# ifdef FEAT_EVAL
1885 /* autocmds may abort script processing */
1886 if (aborting())
1887 return;
1888# endif
1889 }
1890#endif
1891
1892 /*
1893 * Close the link to the buffer.
1894 */
1895 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
1896 /* Autocommands may have closed the window already, or closed the only
1897 * other window. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001898 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 return;
1900
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001901 /* Free the memory used for the window. */
1902 wp = win_free_mem(win, &dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001904 /* When closing the last window in a tab page go to another tab page. */
1905 if (wp == NULL)
1906 {
1907 tabpage_T *ptp = NULL;
1908 tabpage_T *tp;
1909 tabpage_T *atp = alt_tabpage();
1910
1911 for (tp = first_tabpage; tp->tp_topframe != topframe; tp = tp->tp_next)
1912 ptp = tp;
1913 if (tp == NULL)
1914 {
1915 EMSG2(_(e_intern2), "win_close()");
1916 return;
1917 }
1918 if (ptp == NULL)
1919 first_tabpage = tp->tp_next;
1920 else
1921 ptp->tp_next = tp->tp_next;
1922 vim_free(tp);
1923
1924 /* We don't do the window resizing stuff, let enter_tabpage() take
1925 * care of entering a window in another tab page. */
1926 enter_tabpage(atp, old_curbuf);
1927 return;
1928 }
1929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 /* Make sure curwin isn't invalid. It can cause severe trouble when
1931 * printing an error message. For win_equal() curbuf needs to be valid
1932 * too. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001933 else if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 {
1935 curwin = wp;
1936#ifdef FEAT_QUICKFIX
1937 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
1938 {
1939 /*
1940 * The cursor goes to the preview or the quickfix window, try
1941 * finding another window to go to.
1942 */
1943 for (;;)
1944 {
1945 if (wp->w_next == NULL)
1946 wp = firstwin;
1947 else
1948 wp = wp->w_next;
1949 if (wp == curwin)
1950 break;
1951 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
1952 {
1953 curwin = wp;
1954 break;
1955 }
1956 }
1957 }
1958#endif
1959 curbuf = curwin->w_buffer;
1960 close_curwin = TRUE;
1961 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001962 if (p_ea
1963#ifdef FEAT_VERTSPLIT
1964 && (*p_ead == 'b' || *p_ead == dir)
1965#endif
1966 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 win_equal(curwin, TRUE,
1968#ifdef FEAT_VERTSPLIT
1969 dir
1970#else
1971 0
1972#endif
1973 );
1974 else
1975 win_comp_pos();
1976 if (close_curwin)
1977 {
1978 win_enter_ext(wp, FALSE, TRUE);
1979#ifdef FEAT_AUTOCMD
1980 if (other_buffer)
1981 /* careful: after this wp and win may be invalid! */
1982 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1983#endif
1984 }
1985
1986 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001987 * If last window has a status line now and we don't want one,
1988 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 */
1990 last_status(FALSE);
1991
1992 /* After closing the help window, try restoring the window layout from
1993 * before it was opened. */
1994 if (help_window)
1995 restore_snapshot(close_curwin);
1996
1997#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1998 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
1999 if (gui.in_use && !win_hasvertsplit())
2000 gui_init_which_components(NULL);
2001#endif
2002
2003 redraw_all_later(NOT_VALID);
2004}
2005
2006/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002007 * Free the memory used for a window.
2008 * Returns a pointer to the window that got the freed up space.
2009 */
2010 static win_T *
2011win_free_mem(win, dirp)
2012 win_T *win;
2013 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
2014{
2015 frame_T *frp;
2016 win_T *wp;
2017
Bram Moolenaarea408852005-06-25 22:49:46 +00002018#ifdef FEAT_FOLDING
2019 clearFolding(win);
2020#endif
2021
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002022 /* reduce the reference count to the argument list. */
2023 alist_unlink(win->w_alist);
2024
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002025 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002026 frp = win->w_frame;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002027 if (firstwin == lastwin)
2028 /* Last window in a tab page. */
2029 wp = NULL;
2030 else
2031 wp = winframe_remove(win, dirp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002032 vim_free(frp);
2033 win_free(win);
2034
2035 return wp;
2036}
2037
2038#if defined(EXITFREE) || defined(PROTO)
2039 void
2040win_free_all()
2041{
2042 int dummy;
2043
2044 while (firstwin != NULL)
2045 (void)win_free_mem(firstwin, &dummy);
2046}
2047#endif
2048
2049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 * Remove a window and its frame from the tree of frames.
2051 * Returns a pointer to the window that got the freed up space.
2052 */
2053/*ARGSUSED*/
2054 static win_T *
2055winframe_remove(win, dirp)
2056 win_T *win;
2057 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
2058{
2059 frame_T *frp, *frp2, *frp3;
2060 frame_T *frp_close = win->w_frame;
2061 win_T *wp;
2062 int old_height = 0;
2063
2064 /*
2065 * Remove the window from its frame.
2066 */
2067 frp2 = win_altframe(win);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002068 if (frp2 == NULL)
2069 return NULL; /* deleted the last frame */
2070
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 wp = frame2win(frp2);
2072
2073 /* Remove this frame from the list of frames. */
2074 frame_remove(frp_close);
2075
2076#ifdef FEAT_VERTSPLIT
2077 if (frp_close->fr_parent->fr_layout == FR_COL)
2078 {
2079#endif
2080 /* When 'winfixheight' is set, remember its old size and restore
2081 * it later (it's a simplistic solution...). Don't do this if the
2082 * window will occupy the full height of the screen. */
2083 if (frp2->fr_win != NULL
2084 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2085 && frp2->fr_win->w_p_wfh)
2086 old_height = frp2->fr_win->w_height;
2087 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2088 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2089 if (old_height != 0)
2090 win_setheight_win(old_height, frp2->fr_win);
2091#ifdef FEAT_VERTSPLIT
2092 *dirp = 'v';
2093 }
2094 else
2095 {
2096 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2097 frp2 == frp_close->fr_next ? TRUE : FALSE);
2098 *dirp = 'h';
2099 }
2100#endif
2101
2102 /* If rows/columns go to a window below/right its positions need to be
2103 * updated. Can only be done after the sizes have been updated. */
2104 if (frp2 == frp_close->fr_next)
2105 {
2106 int row = win->w_winrow;
2107 int col = W_WINCOL(win);
2108
2109 frame_comp_pos(frp2, &row, &col);
2110 }
2111
2112 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2113 {
2114 /* There is no other frame in this list, move its info to the parent
2115 * and remove it. */
2116 frp2->fr_parent->fr_layout = frp2->fr_layout;
2117 frp2->fr_parent->fr_child = frp2->fr_child;
2118 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2119 frp->fr_parent = frp2->fr_parent;
2120 frp2->fr_parent->fr_win = frp2->fr_win;
2121 if (frp2->fr_win != NULL)
2122 frp2->fr_win->w_frame = frp2->fr_parent;
2123 frp = frp2->fr_parent;
2124 vim_free(frp2);
2125
2126 frp2 = frp->fr_parent;
2127 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2128 {
2129 /* The frame above the parent has the same layout, have to merge
2130 * the frames into this list. */
2131 if (frp2->fr_child == frp)
2132 frp2->fr_child = frp->fr_child;
2133 frp->fr_child->fr_prev = frp->fr_prev;
2134 if (frp->fr_prev != NULL)
2135 frp->fr_prev->fr_next = frp->fr_child;
2136 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2137 {
2138 frp3->fr_parent = frp2;
2139 if (frp3->fr_next == NULL)
2140 {
2141 frp3->fr_next = frp->fr_next;
2142 if (frp->fr_next != NULL)
2143 frp->fr_next->fr_prev = frp3;
2144 break;
2145 }
2146 }
2147 vim_free(frp);
2148 }
2149 }
2150
2151 return wp;
2152}
2153
2154/*
2155 * Find out which frame is going to get the freed up space when "win" is
2156 * closed.
2157 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2158 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2159 * This makes opening a window and closing it immediately keep the same window
2160 * layout.
2161 */
2162 static frame_T *
2163win_altframe(win)
2164 win_T *win;
2165{
2166 frame_T *frp;
2167 int b;
2168
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002169 if (firstwin == lastwin)
2170 /* Last window in this tab page, will go to next tab page. */
2171 return alt_tabpage()->tp_curwin->w_frame;
2172
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 frp = win->w_frame;
2174#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002175 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 b = p_spr;
2177 else
2178#endif
2179 b = p_sb;
2180 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2181 return frp->fr_next;
2182 return frp->fr_prev;
2183}
2184
2185/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002186 * Return the tabpage that will be used if the current one is closed.
2187 */
2188 static tabpage_T *
2189alt_tabpage()
2190{
2191 tabpage_T *tp = current_tabpage();
2192
2193 if (tp != NULL)
2194 {
2195 /* Use the next tab page if it exists. */
2196 if (tp->tp_next != NULL)
2197 return tp->tp_next;
2198
2199 /* Find the previous tab page. */
2200 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
2201 if (tp->tp_next == current_tabpage())
2202 return tp;
2203 }
2204 return first_tabpage;
2205}
2206
2207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 * Find the left-upper window in frame "frp".
2209 */
2210 static win_T *
2211frame2win(frp)
2212 frame_T *frp;
2213{
2214 while (frp->fr_win == NULL)
2215 frp = frp->fr_child;
2216 return frp->fr_win;
2217}
2218
2219/*
2220 * Return TRUE if frame "frp" contains window "wp".
2221 */
2222 static int
2223frame_has_win(frp, wp)
2224 frame_T *frp;
2225 win_T *wp;
2226{
2227 frame_T *p;
2228
2229 if (frp->fr_layout == FR_LEAF)
2230 return frp->fr_win == wp;
2231
2232 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2233 if (frame_has_win(p, wp))
2234 return TRUE;
2235 return FALSE;
2236}
2237
2238/*
2239 * Set a new height for a frame. Recursively sets the height for contained
2240 * frames and windows. Caller must take care of positions.
2241 */
2242 static void
2243frame_new_height(topfrp, height, topfirst, wfh)
2244 frame_T *topfrp;
2245 int height;
2246 int topfirst; /* resize topmost contained frame first */
2247 int wfh; /* obey 'winfixheight' when there is a choice;
2248 may cause the height not to be set */
2249{
2250 frame_T *frp;
2251 int extra_lines;
2252 int h;
2253
2254 if (topfrp->fr_win != NULL)
2255 {
2256 /* Simple case: just one window. */
2257 win_new_height(topfrp->fr_win,
2258 height - topfrp->fr_win->w_status_height);
2259 }
2260#ifdef FEAT_VERTSPLIT
2261 else if (topfrp->fr_layout == FR_ROW)
2262 {
2263 do
2264 {
2265 /* All frames in this row get the same new height. */
2266 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2267 {
2268 frame_new_height(frp, height, topfirst, wfh);
2269 if (frp->fr_height > height)
2270 {
2271 /* Could not fit the windows, make the whole row higher. */
2272 height = frp->fr_height;
2273 break;
2274 }
2275 }
2276 }
2277 while (frp != NULL);
2278 }
2279#endif
2280 else
2281 {
2282 /* Complicated case: Resize a column of frames. Resize the bottom
2283 * frame first, frames above that when needed. */
2284
2285 frp = topfrp->fr_child;
2286 if (wfh)
2287 /* Advance past frames with one window with 'wfh' set. */
2288 while (frame_fixed_height(frp))
2289 {
2290 frp = frp->fr_next;
2291 if (frp == NULL)
2292 return; /* no frame without 'wfh', give up */
2293 }
2294 if (!topfirst)
2295 {
2296 /* Find the bottom frame of this column */
2297 while (frp->fr_next != NULL)
2298 frp = frp->fr_next;
2299 if (wfh)
2300 /* Advance back for frames with one window with 'wfh' set. */
2301 while (frame_fixed_height(frp))
2302 frp = frp->fr_prev;
2303 }
2304
2305 extra_lines = height - topfrp->fr_height;
2306 if (extra_lines < 0)
2307 {
2308 /* reduce height of contained frames, bottom or top frame first */
2309 while (frp != NULL)
2310 {
2311 h = frame_minheight(frp, NULL);
2312 if (frp->fr_height + extra_lines < h)
2313 {
2314 extra_lines += frp->fr_height - h;
2315 frame_new_height(frp, h, topfirst, wfh);
2316 }
2317 else
2318 {
2319 frame_new_height(frp, frp->fr_height + extra_lines,
2320 topfirst, wfh);
2321 break;
2322 }
2323 if (topfirst)
2324 {
2325 do
2326 frp = frp->fr_next;
2327 while (wfh && frp != NULL && frame_fixed_height(frp));
2328 }
2329 else
2330 {
2331 do
2332 frp = frp->fr_prev;
2333 while (wfh && frp != NULL && frame_fixed_height(frp));
2334 }
2335 /* Increase "height" if we could not reduce enough frames. */
2336 if (frp == NULL)
2337 height -= extra_lines;
2338 }
2339 }
2340 else if (extra_lines > 0)
2341 {
2342 /* increase height of bottom or top frame */
2343 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2344 }
2345 }
2346 topfrp->fr_height = height;
2347}
2348
2349/*
2350 * Return TRUE if height of frame "frp" should not be changed because of
2351 * the 'winfixheight' option.
2352 */
2353 static int
2354frame_fixed_height(frp)
2355 frame_T *frp;
2356{
2357 /* frame with one window: fixed height if 'winfixheight' set. */
2358 if (frp->fr_win != NULL)
2359 return frp->fr_win->w_p_wfh;
2360
2361 if (frp->fr_layout == FR_ROW)
2362 {
2363 /* The frame is fixed height if one of the frames in the row is fixed
2364 * height. */
2365 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2366 if (frame_fixed_height(frp))
2367 return TRUE;
2368 return FALSE;
2369 }
2370
2371 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2372 * frames in the row are fixed height. */
2373 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2374 if (!frame_fixed_height(frp))
2375 return FALSE;
2376 return TRUE;
2377}
2378
2379#ifdef FEAT_VERTSPLIT
2380/*
2381 * Add a status line to windows at the bottom of "frp".
2382 * Note: Does not check if there is room!
2383 */
2384 static void
2385frame_add_statusline(frp)
2386 frame_T *frp;
2387{
2388 win_T *wp;
2389
2390 if (frp->fr_layout == FR_LEAF)
2391 {
2392 wp = frp->fr_win;
2393 if (wp->w_status_height == 0)
2394 {
2395 if (wp->w_height > 0) /* don't make it negative */
2396 --wp->w_height;
2397 wp->w_status_height = STATUS_HEIGHT;
2398 }
2399 }
2400 else if (frp->fr_layout == FR_ROW)
2401 {
2402 /* Handle all the frames in the row. */
2403 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2404 frame_add_statusline(frp);
2405 }
2406 else /* frp->fr_layout == FR_COL */
2407 {
2408 /* Only need to handle the last frame in the column. */
2409 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2410 ;
2411 frame_add_statusline(frp);
2412 }
2413}
2414
2415/*
2416 * Set width of a frame. Handles recursively going through contained frames.
2417 * May remove separator line for windows at the right side (for win_close()).
2418 */
2419 static void
2420frame_new_width(topfrp, width, leftfirst)
2421 frame_T *topfrp;
2422 int width;
2423 int leftfirst; /* resize leftmost contained frame first */
2424{
2425 frame_T *frp;
2426 int extra_cols;
2427 int w;
2428 win_T *wp;
2429
2430 if (topfrp->fr_layout == FR_LEAF)
2431 {
2432 /* Simple case: just one window. */
2433 wp = topfrp->fr_win;
2434 /* Find out if there are any windows right of this one. */
2435 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2436 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2437 break;
2438 if (frp->fr_parent == NULL)
2439 wp->w_vsep_width = 0;
2440 win_new_width(wp, width - wp->w_vsep_width);
2441 }
2442 else if (topfrp->fr_layout == FR_COL)
2443 {
2444 /* All frames in this column get the same new width. */
2445 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2446 frame_new_width(frp, width, leftfirst);
2447 }
2448 else /* fr_layout == FR_ROW */
2449 {
2450 /* Complicated case: Resize a row of frames. Resize the rightmost
2451 * frame first, frames left of it when needed. */
2452
2453 /* Find the rightmost frame of this row */
2454 frp = topfrp->fr_child;
2455 if (!leftfirst)
2456 while (frp->fr_next != NULL)
2457 frp = frp->fr_next;
2458
2459 extra_cols = width - topfrp->fr_width;
2460 if (extra_cols < 0)
2461 {
2462 /* reduce frame width, rightmost frame first */
2463 while (frp != NULL)
2464 {
2465 w = frame_minwidth(frp, NULL);
2466 if (frp->fr_width + extra_cols < w)
2467 {
2468 extra_cols += frp->fr_width - w;
2469 frame_new_width(frp, w, leftfirst);
2470 }
2471 else
2472 {
2473 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst);
2474 break;
2475 }
2476 if (leftfirst)
2477 frp = frp->fr_next;
2478 else
2479 frp = frp->fr_prev;
2480 }
2481 }
2482 else if (extra_cols > 0)
2483 {
2484 /* increase width of rightmost frame */
2485 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst);
2486 }
2487 }
2488 topfrp->fr_width = width;
2489}
2490
2491/*
2492 * Add the vertical separator to windows at the right side of "frp".
2493 * Note: Does not check if there is room!
2494 */
2495 static void
2496frame_add_vsep(frp)
2497 frame_T *frp;
2498{
2499 win_T *wp;
2500
2501 if (frp->fr_layout == FR_LEAF)
2502 {
2503 wp = frp->fr_win;
2504 if (wp->w_vsep_width == 0)
2505 {
2506 if (wp->w_width > 0) /* don't make it negative */
2507 --wp->w_width;
2508 wp->w_vsep_width = 1;
2509 }
2510 }
2511 else if (frp->fr_layout == FR_COL)
2512 {
2513 /* Handle all the frames in the column. */
2514 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2515 frame_add_vsep(frp);
2516 }
2517 else /* frp->fr_layout == FR_ROW */
2518 {
2519 /* Only need to handle the last frame in the row. */
2520 frp = frp->fr_child;
2521 while (frp->fr_next != NULL)
2522 frp = frp->fr_next;
2523 frame_add_vsep(frp);
2524 }
2525}
2526
2527/*
2528 * Set frame width from the window it contains.
2529 */
2530 static void
2531frame_fix_width(wp)
2532 win_T *wp;
2533{
2534 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2535}
2536#endif
2537
2538/*
2539 * Set frame height from the window it contains.
2540 */
2541 static void
2542frame_fix_height(wp)
2543 win_T *wp;
2544{
2545 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2546}
2547
2548/*
2549 * Compute the minimal height for frame "topfrp".
2550 * Uses the 'winminheight' option.
2551 * When "next_curwin" isn't NULL, use p_wh for this window.
2552 * When "next_curwin" is NOWIN, don't use at least one line for the current
2553 * window.
2554 */
2555 static int
2556frame_minheight(topfrp, next_curwin)
2557 frame_T *topfrp;
2558 win_T *next_curwin;
2559{
2560 frame_T *frp;
2561 int m;
2562#ifdef FEAT_VERTSPLIT
2563 int n;
2564#endif
2565
2566 if (topfrp->fr_win != NULL)
2567 {
2568 if (topfrp->fr_win == next_curwin)
2569 m = p_wh + topfrp->fr_win->w_status_height;
2570 else
2571 {
2572 /* window: minimal height of the window plus status line */
2573 m = p_wmh + topfrp->fr_win->w_status_height;
2574 /* Current window is minimal one line high */
2575 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2576 ++m;
2577 }
2578 }
2579#ifdef FEAT_VERTSPLIT
2580 else if (topfrp->fr_layout == FR_ROW)
2581 {
2582 /* get the minimal height from each frame in this row */
2583 m = 0;
2584 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2585 {
2586 n = frame_minheight(frp, next_curwin);
2587 if (n > m)
2588 m = n;
2589 }
2590 }
2591#endif
2592 else
2593 {
2594 /* Add up the minimal heights for all frames in this column. */
2595 m = 0;
2596 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2597 m += frame_minheight(frp, next_curwin);
2598 }
2599
2600 return m;
2601}
2602
2603#ifdef FEAT_VERTSPLIT
2604/*
2605 * Compute the minimal width for frame "topfrp".
2606 * When "next_curwin" isn't NULL, use p_wiw for this window.
2607 * When "next_curwin" is NOWIN, don't use at least one column for the current
2608 * window.
2609 */
2610 static int
2611frame_minwidth(topfrp, next_curwin)
2612 frame_T *topfrp;
2613 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2614{
2615 frame_T *frp;
2616 int m, n;
2617
2618 if (topfrp->fr_win != NULL)
2619 {
2620 if (topfrp->fr_win == next_curwin)
2621 m = p_wiw + topfrp->fr_win->w_vsep_width;
2622 else
2623 {
2624 /* window: minimal width of the window plus separator column */
2625 m = p_wmw + topfrp->fr_win->w_vsep_width;
2626 /* Current window is minimal one column wide */
2627 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2628 ++m;
2629 }
2630 }
2631 else if (topfrp->fr_layout == FR_COL)
2632 {
2633 /* get the minimal width from each frame in this column */
2634 m = 0;
2635 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2636 {
2637 n = frame_minwidth(frp, next_curwin);
2638 if (n > m)
2639 m = n;
2640 }
2641 }
2642 else
2643 {
2644 /* Add up the minimal widths for all frames in this row. */
2645 m = 0;
2646 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2647 m += frame_minwidth(frp, next_curwin);
2648 }
2649
2650 return m;
2651}
2652#endif
2653
2654
2655/*
2656 * Try to close all windows except current one.
2657 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2658 * used and the buffer was modified.
2659 *
2660 * Used by ":bdel" and ":only".
2661 */
2662 void
2663close_others(message, forceit)
2664 int message;
2665 int forceit; /* always hide all other windows */
2666{
2667 win_T *wp;
2668 win_T *nextwp;
2669 int r;
2670
2671 if (lastwin == firstwin)
2672 {
2673 if (message
2674#ifdef FEAT_AUTOCMD
2675 && !autocmd_busy
2676#endif
2677 )
2678 MSG(_("Already only one window"));
2679 return;
2680 }
2681
2682 /* Be very careful here: autocommands may change the window layout. */
2683 for (wp = firstwin; win_valid(wp); wp = nextwp)
2684 {
2685 nextwp = wp->w_next;
2686 if (wp != curwin) /* don't close current window */
2687 {
2688
2689 /* Check if it's allowed to abandon this window */
2690 r = can_abandon(wp->w_buffer, forceit);
2691#ifdef FEAT_AUTOCMD
2692 if (!win_valid(wp)) /* autocommands messed wp up */
2693 {
2694 nextwp = firstwin;
2695 continue;
2696 }
2697#endif
2698 if (!r)
2699 {
2700#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2701 if (message && (p_confirm || cmdmod.confirm) && p_write)
2702 {
2703 dialog_changed(wp->w_buffer, FALSE);
2704# ifdef FEAT_AUTOCMD
2705 if (!win_valid(wp)) /* autocommands messed wp up */
2706 {
2707 nextwp = firstwin;
2708 continue;
2709 }
2710# endif
2711 }
2712 if (bufIsChanged(wp->w_buffer))
2713#endif
2714 continue;
2715 }
2716 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
2717 }
2718 }
2719
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002720 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 EMSG(_("E445: Other window contains changes"));
2722}
2723
2724#endif /* FEAT_WINDOWS */
2725
2726/*
2727 * init the cursor in the window
2728 *
2729 * called when a new file is being edited
2730 */
2731 void
2732win_init(wp)
2733 win_T *wp;
2734{
2735 redraw_win_later(wp, NOT_VALID);
2736 wp->w_lines_valid = 0;
2737 wp->w_cursor.lnum = 1;
2738 wp->w_curswant = wp->w_cursor.col = 0;
2739#ifdef FEAT_VIRTUALEDIT
2740 wp->w_cursor.coladd = 0;
2741#endif
2742 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
2743 wp->w_pcmark.col = 0;
2744 wp->w_prev_pcmark.lnum = 0;
2745 wp->w_prev_pcmark.col = 0;
2746 wp->w_topline = 1;
2747#ifdef FEAT_DIFF
2748 wp->w_topfill = 0;
2749#endif
2750 wp->w_botline = 2;
2751#ifdef FEAT_FKMAP
2752 if (curwin->w_p_rl)
2753 wp->w_farsi = W_CONV + W_R_L;
2754 else
2755 wp->w_farsi = W_CONV;
2756#endif
2757}
2758
2759/*
2760 * Allocate the first window and put an empty buffer in it.
2761 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002762 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002764 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765win_alloc_first()
2766{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002767 if (win_alloc_firstwin() == FAIL)
2768 return FAIL;
2769
2770#ifdef FEAT_WINDOWS
2771 first_tabpage = (tabpage_T *)alloc((unsigned)sizeof(tabpage_T));
2772 if (first_tabpage == NULL)
2773 return FAIL;
2774 first_tabpage->tp_topframe = topframe;
2775 first_tabpage->tp_next = NULL;
2776#endif
2777 return OK;
2778}
2779
2780/*
2781 * Allocate one window and put an empty buffer in it.
2782 * Called to create the first window in a new tab page.
2783 * Return FAIL when something goes wrong (out of memory).
2784 */
2785 static int
2786win_alloc_firstwin()
2787{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 curwin = win_alloc(NULL);
2789 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
2790 if (curwin == NULL || curbuf == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002791 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 curwin->w_buffer = curbuf;
2793 curbuf->b_nwindows = 1; /* there is one window */
2794#ifdef FEAT_WINDOWS
2795 curwin->w_alist = &global_alist;
2796#endif
2797 win_init(curwin); /* init current window */
2798
2799 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
2800 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002801 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 topframe->fr_layout = FR_LEAF;
2803#ifdef FEAT_VERTSPLIT
2804 topframe->fr_width = Columns;
2805#endif
2806 topframe->fr_height = Rows - p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002807#ifdef FEAT_WINDOWS
2808 p_ch_used = p_ch;
2809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 topframe->fr_win = curwin;
2811 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002812
2813 return OK;
2814}
2815
2816/*
2817 * Initialize the window and frame size to the maximum.
2818 */
2819 void
2820win_init_size()
2821{
2822 firstwin->w_height = ROWS_AVAIL;
2823 topframe->fr_height = ROWS_AVAIL;
2824#ifdef FEAT_VERTSPLIT
2825 firstwin->w_width = Columns;
2826 topframe->fr_width = Columns;
2827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828}
2829
2830#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002831/*
2832 * Create a new Tab page with one empty window.
2833 * Put it just after the current Tab page.
2834 * Return FAIL or OK.
2835 */
2836 int
2837win_new_tabpage()
2838{
2839 tabpage_T *tp;
2840 tabpage_T *newtp;
2841
2842 newtp = (tabpage_T *)alloc((unsigned)sizeof(tabpage_T));
2843 if (newtp == NULL)
2844 return FAIL;
2845
2846 tp = current_tabpage();
2847
2848 /* Remember the current windows in this Tab page. */
2849 leave_tabpage(tp);
2850
2851 /* Create a new empty window. */
2852 if (win_alloc_firstwin() == OK)
2853 {
2854 /* copy options from previous to new curwin */
2855 win_copy_options(tp->tp_curwin, curwin);
2856
2857 /* Make the new Tab page the new topframe. */
2858 newtp->tp_next = tp->tp_next;
2859 tp->tp_next = newtp;
2860 win_init_size();
2861 firstwin->w_winrow = tabpageline_height();
2862
2863 newtp->tp_topframe = topframe;
2864 redraw_all_later(CLEAR);
2865 return OK;
2866 }
2867
2868 /* Failed, get back the previous Tab page */
2869 topframe = tp->tp_topframe;
2870 curwin = tp->tp_curwin;
2871 firstwin = tp->tp_firstwin;
2872 lastwin = tp->tp_lastwin;
2873 return FAIL;
2874}
2875
2876/*
2877 * Return a pointer to the current tab page.
2878 */
2879 static tabpage_T *
2880current_tabpage()
2881{
2882 tabpage_T *tp;
2883
2884 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
2885 if (tp->tp_topframe == topframe)
2886 break;
2887 if (tp == NULL)
2888 EMSG2(_(e_intern2), "current_tabpage()");
2889 return tp;
2890}
2891
2892/*
2893 * Prepare for leaving the current tab page "tp".
2894 */
2895 static void
2896leave_tabpage(tp)
2897 tabpage_T *tp;
2898{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00002899#if defined(FEAT_GUI)
2900 /* Remove the scrollbars. They may be added back later. */
2901 if (gui.in_use)
2902 gui_remove_scrollbars();
2903#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002904 tp->tp_curwin = curwin;
2905 tp->tp_firstwin = firstwin;
2906 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00002907 tp->tp_old_Rows = Rows;
2908 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002909 firstwin = NULL;
2910 lastwin = NULL;
2911}
2912
2913/*
2914 * Start using tab page "tp".
2915 */
2916/*ARGSUSED*/
2917 static void
2918enter_tabpage(tp, old_curbuf)
2919 tabpage_T *tp;
2920 buf_T *old_curbuf;
2921{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00002922 int old_off = tp->tp_firstwin->w_winrow;
2923
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002924 firstwin = tp->tp_firstwin;
2925 lastwin = tp->tp_lastwin;
2926 topframe = tp->tp_topframe;
2927 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
2928
2929#ifdef FEAT_AUTOCMD
2930 if (old_curbuf != curbuf)
2931 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2932#endif
2933
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00002934 last_status(FALSE); /* status line may appear or disappear */
2935 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002936
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00002937 /* The tabpage line may have appeared or disappeared, may need to resize
2938 * the frames for that. When the Vim window was resized need to update
2939 * frame sizes too. */
2940 if (tp->tp_old_Rows != Rows || old_off != firstwin->w_winrow)
2941 shell_new_rows();
2942#ifdef FEAT_VERTSPLIT
2943 if (tp->tp_old_Columns != Columns)
2944 shell_new_columns(); /* update window widths */
2945#endif
2946
2947#if defined(FEAT_GUI)
2948 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
2949 * scrollbars. Have to update them anyway. */
2950 if (gui.in_use)
2951 {
2952 out_flush();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002953 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00002954 gui_update_scrollbars(TRUE);
2955 }
2956 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002957#endif
2958
2959 redraw_all_later(CLEAR);
2960}
2961
2962/*
2963 * Go to tab page "n". For ":tab N" and "Ngt".
2964 */
2965 void
2966goto_tabpage(n)
2967 int n;
2968{
2969 tabpage_T *otp = current_tabpage();
2970 tabpage_T *tp;
2971 int i;
2972
2973 if (otp == NULL)
2974 return;
2975
2976 if (n == 0)
2977 {
2978 /* No count, go to next tab page, wrap around end. */
2979 if (otp->tp_next == NULL)
2980 tp = first_tabpage;
2981 else
2982 tp = otp->tp_next;
2983 }
2984 else
2985 {
2986 /* Go to tab page "n". */
2987 i = 0;
2988 for (tp = first_tabpage; ++i != n; tp = tp->tp_next)
2989 if (tp == NULL)
2990 {
2991 beep_flush();
2992 return;
2993 }
2994 }
2995
2996 leave_tabpage(otp);
2997 enter_tabpage(tp, curbuf);
2998}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999
3000/*
3001 * Go to another window.
3002 * When jumping to another buffer, stop Visual mode. Do this before
3003 * changing windows so we can yank the selection into the '*' register.
3004 * When jumping to another window on the same buffer, adjust its cursor
3005 * position to keep the same Visual area.
3006 */
3007 void
3008win_goto(wp)
3009 win_T *wp;
3010{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003011 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 {
3013 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003014 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 return;
3016 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003017
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018#ifdef FEAT_VISUAL
3019 if (wp->w_buffer != curbuf)
3020 reset_VIsual_and_resel();
3021 else if (VIsual_active)
3022 wp->w_cursor = curwin->w_cursor;
3023#endif
3024
3025#ifdef FEAT_GUI
3026 need_mouse_correct = TRUE;
3027#endif
3028 win_enter(wp, TRUE);
3029}
3030
3031#if defined(FEAT_PERL) || defined(PROTO)
3032/*
3033 * Find window number "winnr" (counting top to bottom).
3034 */
3035 win_T *
3036win_find_nr(winnr)
3037 int winnr;
3038{
3039 win_T *wp;
3040
3041# ifdef FEAT_WINDOWS
3042 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3043 if (--winnr == 0)
3044 break;
3045 return wp;
3046# else
3047 return curwin;
3048# endif
3049}
3050#endif
3051
3052#ifdef FEAT_VERTSPLIT
3053/*
3054 * Move to window above or below "count" times.
3055 */
3056 static void
3057win_goto_ver(up, count)
3058 int up; /* TRUE to go to win above */
3059 long count;
3060{
3061 frame_T *fr;
3062 frame_T *nfr;
3063 frame_T *foundfr;
3064
3065 foundfr = curwin->w_frame;
3066 while (count--)
3067 {
3068 /*
3069 * First go upwards in the tree of frames until we find a upwards or
3070 * downwards neighbor.
3071 */
3072 fr = foundfr;
3073 for (;;)
3074 {
3075 if (fr == topframe)
3076 goto end;
3077 if (up)
3078 nfr = fr->fr_prev;
3079 else
3080 nfr = fr->fr_next;
3081 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3082 break;
3083 fr = fr->fr_parent;
3084 }
3085
3086 /*
3087 * Now go downwards to find the bottom or top frame in it.
3088 */
3089 for (;;)
3090 {
3091 if (nfr->fr_layout == FR_LEAF)
3092 {
3093 foundfr = nfr;
3094 break;
3095 }
3096 fr = nfr->fr_child;
3097 if (nfr->fr_layout == FR_ROW)
3098 {
3099 /* Find the frame at the cursor row. */
3100 while (fr->fr_next != NULL
3101 && frame2win(fr)->w_wincol + fr->fr_width
3102 <= curwin->w_wincol + curwin->w_wcol)
3103 fr = fr->fr_next;
3104 }
3105 if (nfr->fr_layout == FR_COL && up)
3106 while (fr->fr_next != NULL)
3107 fr = fr->fr_next;
3108 nfr = fr;
3109 }
3110 }
3111end:
3112 if (foundfr != NULL)
3113 win_goto(foundfr->fr_win);
3114}
3115
3116/*
3117 * Move to left or right window.
3118 */
3119 static void
3120win_goto_hor(left, count)
3121 int left; /* TRUE to go to left win */
3122 long count;
3123{
3124 frame_T *fr;
3125 frame_T *nfr;
3126 frame_T *foundfr;
3127
3128 foundfr = curwin->w_frame;
3129 while (count--)
3130 {
3131 /*
3132 * First go upwards in the tree of frames until we find a left or
3133 * right neighbor.
3134 */
3135 fr = foundfr;
3136 for (;;)
3137 {
3138 if (fr == topframe)
3139 goto end;
3140 if (left)
3141 nfr = fr->fr_prev;
3142 else
3143 nfr = fr->fr_next;
3144 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3145 break;
3146 fr = fr->fr_parent;
3147 }
3148
3149 /*
3150 * Now go downwards to find the leftmost or rightmost frame in it.
3151 */
3152 for (;;)
3153 {
3154 if (nfr->fr_layout == FR_LEAF)
3155 {
3156 foundfr = nfr;
3157 break;
3158 }
3159 fr = nfr->fr_child;
3160 if (nfr->fr_layout == FR_COL)
3161 {
3162 /* Find the frame at the cursor row. */
3163 while (fr->fr_next != NULL
3164 && frame2win(fr)->w_winrow + fr->fr_height
3165 <= curwin->w_winrow + curwin->w_wrow)
3166 fr = fr->fr_next;
3167 }
3168 if (nfr->fr_layout == FR_ROW && left)
3169 while (fr->fr_next != NULL)
3170 fr = fr->fr_next;
3171 nfr = fr;
3172 }
3173 }
3174end:
3175 if (foundfr != NULL)
3176 win_goto(foundfr->fr_win);
3177}
3178#endif
3179
3180/*
3181 * Make window "wp" the current window.
3182 */
3183 void
3184win_enter(wp, undo_sync)
3185 win_T *wp;
3186 int undo_sync;
3187{
3188 win_enter_ext(wp, undo_sync, FALSE);
3189}
3190
3191/*
3192 * Make window wp the current window.
3193 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3194 * been closed and isn't valid.
3195 */
3196 static void
3197win_enter_ext(wp, undo_sync, curwin_invalid)
3198 win_T *wp;
3199 int undo_sync;
3200 int curwin_invalid;
3201{
3202#ifdef FEAT_AUTOCMD
3203 int other_buffer = FALSE;
3204#endif
3205
3206 if (wp == curwin && !curwin_invalid) /* nothing to do */
3207 return;
3208
3209#ifdef FEAT_AUTOCMD
3210 if (!curwin_invalid)
3211 {
3212 /*
3213 * Be careful: If autocommands delete the window, return now.
3214 */
3215 if (wp->w_buffer != curbuf)
3216 {
3217 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3218 other_buffer = TRUE;
3219 if (!win_valid(wp))
3220 return;
3221 }
3222 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3223 if (!win_valid(wp))
3224 return;
3225# ifdef FEAT_EVAL
3226 /* autocmds may abort script processing */
3227 if (aborting())
3228 return;
3229# endif
3230 }
3231#endif
3232
3233 /* sync undo before leaving the current buffer */
3234 if (undo_sync && curbuf != wp->w_buffer)
3235 u_sync();
3236 /* may have to copy the buffer options when 'cpo' contains 'S' */
3237 if (wp->w_buffer != curbuf)
3238 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3239 if (!curwin_invalid)
3240 {
3241 prevwin = curwin; /* remember for CTRL-W p */
3242 curwin->w_redr_status = TRUE;
3243 }
3244 curwin = wp;
3245 curbuf = wp->w_buffer;
3246 check_cursor();
3247#ifdef FEAT_VIRTUALEDIT
3248 if (!virtual_active())
3249 curwin->w_cursor.coladd = 0;
3250#endif
3251 changed_line_abv_curs(); /* assume cursor position needs updating */
3252
3253 if (curwin->w_localdir != NULL)
3254 {
3255 /* Window has a local directory: Save current directory as global
3256 * directory (unless that was done already) and change to the local
3257 * directory. */
3258 if (globaldir == NULL)
3259 {
3260 char_u cwd[MAXPATHL];
3261
3262 if (mch_dirname(cwd, MAXPATHL) == OK)
3263 globaldir = vim_strsave(cwd);
3264 }
3265 mch_chdir((char *)curwin->w_localdir);
3266 shorten_fnames(TRUE);
3267 }
3268 else if (globaldir != NULL)
3269 {
3270 /* Window doesn't have a local directory and we are not in the global
3271 * directory: Change to the global directory. */
3272 mch_chdir((char *)globaldir);
3273 vim_free(globaldir);
3274 globaldir = NULL;
3275 shorten_fnames(TRUE);
3276 }
3277
3278#ifdef FEAT_AUTOCMD
3279 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3280 if (other_buffer)
3281 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3282#endif
3283
3284#ifdef FEAT_TITLE
3285 maketitle();
3286#endif
3287 curwin->w_redr_status = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003288 redraw_tabpage = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 if (restart_edit)
3290 redraw_later(VALID); /* causes status line redraw */
3291
3292 /* set window height to desired minimal value */
3293 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3294 win_setheight((int)p_wh);
3295 else if (curwin->w_height == 0)
3296 win_setheight(1);
3297
3298#ifdef FEAT_VERTSPLIT
3299 /* set window width to desired minimal value */
3300 if (curwin->w_width < p_wiw)
3301 win_setwidth((int)p_wiw);
3302#endif
3303
3304#ifdef FEAT_MOUSE
3305 setmouse(); /* in case jumped to/from help buffer */
3306#endif
3307
3308#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)
3309 /* Change directories when the acd option is set on and after
3310 * switching windows. */
3311 if (p_acd && curbuf->b_ffname != NULL
3312 && vim_chdirfile(curbuf->b_ffname) == OK)
3313 shorten_fnames(TRUE);
3314#endif
3315}
3316
3317#endif /* FEAT_WINDOWS */
3318
3319#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3320/*
3321 * Jump to the first open window that contains buffer buf if one exists
3322 * TODO: Alternatively jump to last open window? Dependent from 'splitbelow'?
3323 * Returns pointer to window if it exists, otherwise NULL.
3324 */
3325 win_T *
3326buf_jump_open_win(buf)
3327 buf_T *buf;
3328{
3329# ifdef FEAT_WINDOWS
3330 win_T *wp;
3331
3332 for (wp = firstwin; wp; wp = wp->w_next)
3333 if (wp->w_buffer == buf)
3334 break;
3335 if (wp != NULL)
3336 win_enter(wp, FALSE);
3337 return wp;
3338# else
3339 if (curwin->w_buffer == buf)
3340 return curwin;
3341 return NULL;
3342# endif
3343}
3344#endif
3345
3346/*
3347 * allocate a window structure and link it in the window list
3348 */
3349/*ARGSUSED*/
3350 static win_T *
3351win_alloc(after)
3352 win_T *after;
3353{
3354 win_T *newwin;
3355
3356 /*
3357 * allocate window structure and linesizes arrays
3358 */
3359 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
3360 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
3361 {
3362 vim_free(newwin);
3363 newwin = NULL;
3364 }
3365
3366 if (newwin != NULL)
3367 {
3368 /*
3369 * link the window in the window list
3370 */
3371#ifdef FEAT_WINDOWS
3372 win_append(after, newwin);
3373#endif
3374#ifdef FEAT_VERTSPLIT
3375 newwin->w_wincol = 0;
3376 newwin->w_width = Columns;
3377#endif
3378
3379 /* position the display and the cursor at the top of the file. */
3380 newwin->w_topline = 1;
3381#ifdef FEAT_DIFF
3382 newwin->w_topfill = 0;
3383#endif
3384 newwin->w_botline = 2;
3385 newwin->w_cursor.lnum = 1;
3386#ifdef FEAT_SCROLLBIND
3387 newwin->w_scbind_pos = 1;
3388#endif
3389
3390 /* We won't calculate w_fraction until resizing the window */
3391 newwin->w_fraction = 0;
3392 newwin->w_prev_fraction_row = -1;
3393
3394#ifdef FEAT_GUI
3395 if (gui.in_use)
3396 {
3397 out_flush();
3398 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
3399 SBAR_LEFT, newwin);
3400 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
3401 SBAR_RIGHT, newwin);
3402 }
3403#endif
3404#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003405 /* init w: variables */
3406 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407#endif
3408#ifdef FEAT_FOLDING
3409 foldInitWin(newwin);
3410#endif
3411 }
3412 return newwin;
3413}
3414
3415#if defined(FEAT_WINDOWS) || defined(PROTO)
3416
3417/*
3418 * remove window 'wp' from the window list and free the structure
3419 */
3420 static void
3421win_free(wp)
3422 win_T *wp;
3423{
3424 int i;
3425
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003426#ifdef FEAT_MZSCHEME
3427 mzscheme_window_free(wp);
3428#endif
3429
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430#ifdef FEAT_PERL
3431 perl_win_free(wp);
3432#endif
3433
3434#ifdef FEAT_PYTHON
3435 python_window_free(wp);
3436#endif
3437
3438#ifdef FEAT_TCL
3439 tcl_window_free(wp);
3440#endif
3441
3442#ifdef FEAT_RUBY
3443 ruby_window_free(wp);
3444#endif
3445
3446 clear_winopt(&wp->w_onebuf_opt);
3447 clear_winopt(&wp->w_allbuf_opt);
3448
3449#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003450 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451#endif
3452
3453 if (prevwin == wp)
3454 prevwin = NULL;
3455 win_free_lsize(wp);
3456
3457 for (i = 0; i < wp->w_tagstacklen; ++i)
3458 vim_free(wp->w_tagstack[i].tagname);
3459
3460 vim_free(wp->w_localdir);
3461#ifdef FEAT_SEARCH_EXTRA
3462 vim_free(wp->w_match.regprog);
3463#endif
3464#ifdef FEAT_JUMPLIST
3465 free_jumplist(wp);
3466#endif
3467
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003468#ifdef FEAT_QUICKFIX
3469 qf_free_all(wp);
3470#endif
3471
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472#ifdef FEAT_GUI
3473 if (gui.in_use)
3474 {
3475 out_flush();
3476 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
3477 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
3478 }
3479#endif /* FEAT_GUI */
3480
3481 win_remove(wp);
3482 vim_free(wp);
3483}
3484
3485/*
3486 * Append window "wp" in the window list after window "after".
3487 */
3488 static void
3489win_append(after, wp)
3490 win_T *after, *wp;
3491{
3492 win_T *before;
3493
3494 if (after == NULL) /* after NULL is in front of the first */
3495 before = firstwin;
3496 else
3497 before = after->w_next;
3498
3499 wp->w_next = before;
3500 wp->w_prev = after;
3501 if (after == NULL)
3502 firstwin = wp;
3503 else
3504 after->w_next = wp;
3505 if (before == NULL)
3506 lastwin = wp;
3507 else
3508 before->w_prev = wp;
3509}
3510
3511/*
3512 * Remove a window from the window list.
3513 */
3514 static void
3515win_remove(wp)
3516 win_T *wp;
3517{
3518 if (wp->w_prev != NULL)
3519 wp->w_prev->w_next = wp->w_next;
3520 else
3521 firstwin = wp->w_next;
3522 if (wp->w_next != NULL)
3523 wp->w_next->w_prev = wp->w_prev;
3524 else
3525 lastwin = wp->w_prev;
3526}
3527
3528/*
3529 * Append frame "frp" in a frame list after frame "after".
3530 */
3531 static void
3532frame_append(after, frp)
3533 frame_T *after, *frp;
3534{
3535 frp->fr_next = after->fr_next;
3536 after->fr_next = frp;
3537 if (frp->fr_next != NULL)
3538 frp->fr_next->fr_prev = frp;
3539 frp->fr_prev = after;
3540}
3541
3542/*
3543 * Insert frame "frp" in a frame list before frame "before".
3544 */
3545 static void
3546frame_insert(before, frp)
3547 frame_T *before, *frp;
3548{
3549 frp->fr_next = before;
3550 frp->fr_prev = before->fr_prev;
3551 before->fr_prev = frp;
3552 if (frp->fr_prev != NULL)
3553 frp->fr_prev->fr_next = frp;
3554 else
3555 frp->fr_parent->fr_child = frp;
3556}
3557
3558/*
3559 * Remove a frame from a frame list.
3560 */
3561 static void
3562frame_remove(frp)
3563 frame_T *frp;
3564{
3565 if (frp->fr_prev != NULL)
3566 frp->fr_prev->fr_next = frp->fr_next;
3567 else
3568 frp->fr_parent->fr_child = frp->fr_next;
3569 if (frp->fr_next != NULL)
3570 frp->fr_next->fr_prev = frp->fr_prev;
3571}
3572
3573#endif /* FEAT_WINDOWS */
3574
3575/*
3576 * Allocate w_lines[] for window "wp".
3577 * Return FAIL for failure, OK for success.
3578 */
3579 int
3580win_alloc_lines(wp)
3581 win_T *wp;
3582{
3583 wp->w_lines_valid = 0;
3584 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
3585 if (wp->w_lines == NULL)
3586 return FAIL;
3587 return OK;
3588}
3589
3590/*
3591 * free lsize arrays for a window
3592 */
3593 void
3594win_free_lsize(wp)
3595 win_T *wp;
3596{
3597 vim_free(wp->w_lines);
3598 wp->w_lines = NULL;
3599}
3600
3601/*
3602 * Called from win_new_shellsize() after Rows changed.
3603 */
3604 void
3605shell_new_rows()
3606{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003607 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
3609 if (firstwin == NULL) /* not initialized yet */
3610 return;
3611#ifdef FEAT_WINDOWS
3612 if (h < frame_minheight(topframe, NULL))
3613 h = frame_minheight(topframe, NULL);
3614 /* First try setting the heights of windows without 'winfixheight'. If
3615 * that doesn't result in the right height, forget about that option. */
3616 frame_new_height(topframe, h, FALSE, TRUE);
3617 if (topframe->fr_height != h)
3618 frame_new_height(topframe, h, FALSE, FALSE);
3619
3620 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
3621#else
3622 if (h < 1)
3623 h = 1;
3624 win_new_height(firstwin, h);
3625#endif
3626 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00003627#ifdef FEAT_WINDOWS
3628 p_ch_used = p_ch;
3629#endif
3630
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631#if 0
3632 /* Disabled: don't want making the screen smaller make a window larger. */
3633 if (p_ea)
3634 win_equal(curwin, FALSE, 'v');
3635#endif
3636}
3637
3638#if defined(FEAT_VERTSPLIT) || defined(PROTO)
3639/*
3640 * Called from win_new_shellsize() after Columns changed.
3641 */
3642 void
3643shell_new_columns()
3644{
3645 if (firstwin == NULL) /* not initialized yet */
3646 return;
3647 frame_new_width(topframe, (int)Columns, FALSE);
3648 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
3649#if 0
3650 /* Disabled: don't want making the screen smaller make a window larger. */
3651 if (p_ea)
3652 win_equal(curwin, FALSE, 'h');
3653#endif
3654}
3655#endif
3656
3657#if defined(FEAT_CMDWIN) || defined(PROTO)
3658/*
3659 * Save the size of all windows in "gap".
3660 */
3661 void
3662win_size_save(gap)
3663 garray_T *gap;
3664
3665{
3666 win_T *wp;
3667
3668 ga_init2(gap, (int)sizeof(int), 1);
3669 if (ga_grow(gap, win_count() * 2) == OK)
3670 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3671 {
3672 ((int *)gap->ga_data)[gap->ga_len++] =
3673 wp->w_width + wp->w_vsep_width;
3674 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
3675 }
3676}
3677
3678/*
3679 * Restore window sizes, but only if the number of windows is still the same.
3680 * Does not free the growarray.
3681 */
3682 void
3683win_size_restore(gap)
3684 garray_T *gap;
3685{
3686 win_T *wp;
3687 int i;
3688
3689 if (win_count() * 2 == gap->ga_len)
3690 {
3691 i = 0;
3692 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3693 {
3694 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
3695 win_setheight_win(((int *)gap->ga_data)[i++], wp);
3696 }
3697 /* recompute the window positions */
3698 (void)win_comp_pos();
3699 }
3700}
3701#endif /* FEAT_CMDWIN */
3702
3703#if defined(FEAT_WINDOWS) || defined(PROTO)
3704/*
3705 * Update the position for all windows, using the width and height of the
3706 * frames.
3707 * Returns the row just after the last window.
3708 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003709 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710win_comp_pos()
3711{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003712 int row = tabpageline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 int col = 0;
3714
3715 frame_comp_pos(topframe, &row, &col);
3716 return row;
3717}
3718
3719/*
3720 * Update the position of the windows in frame "topfrp", using the width and
3721 * height of the frames.
3722 * "*row" and "*col" are the top-left position of the frame. They are updated
3723 * to the bottom-right position plus one.
3724 */
3725 static void
3726frame_comp_pos(topfrp, row, col)
3727 frame_T *topfrp;
3728 int *row;
3729 int *col;
3730{
3731 win_T *wp;
3732 frame_T *frp;
3733#ifdef FEAT_VERTSPLIT
3734 int startcol;
3735 int startrow;
3736#endif
3737
3738 wp = topfrp->fr_win;
3739 if (wp != NULL)
3740 {
3741 if (wp->w_winrow != *row
3742#ifdef FEAT_VERTSPLIT
3743 || wp->w_wincol != *col
3744#endif
3745 )
3746 {
3747 /* position changed, redraw */
3748 wp->w_winrow = *row;
3749#ifdef FEAT_VERTSPLIT
3750 wp->w_wincol = *col;
3751#endif
3752 redraw_win_later(wp, NOT_VALID);
3753 wp->w_redr_status = TRUE;
3754 }
3755 *row += wp->w_height + wp->w_status_height;
3756#ifdef FEAT_VERTSPLIT
3757 *col += wp->w_width + wp->w_vsep_width;
3758#endif
3759 }
3760 else
3761 {
3762#ifdef FEAT_VERTSPLIT
3763 startrow = *row;
3764 startcol = *col;
3765#endif
3766 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3767 {
3768#ifdef FEAT_VERTSPLIT
3769 if (topfrp->fr_layout == FR_ROW)
3770 *row = startrow; /* all frames are at the same row */
3771 else
3772 *col = startcol; /* all frames are at the same col */
3773#endif
3774 frame_comp_pos(frp, row, col);
3775 }
3776 }
3777}
3778
3779#endif /* FEAT_WINDOWS */
3780
3781/*
3782 * Set current window height and take care of repositioning other windows to
3783 * fit around it.
3784 */
3785 void
3786win_setheight(height)
3787 int height;
3788{
3789 win_setheight_win(height, curwin);
3790}
3791
3792/*
3793 * Set the window height of window "win" and take care of repositioning other
3794 * windows to fit around it.
3795 */
3796 void
3797win_setheight_win(height, win)
3798 int height;
3799 win_T *win;
3800{
3801 int row;
3802
3803 if (win == curwin)
3804 {
3805 /* Always keep current window at least one line high, even when
3806 * 'winminheight' is zero. */
3807#ifdef FEAT_WINDOWS
3808 if (height < p_wmh)
3809 height = p_wmh;
3810#endif
3811 if (height == 0)
3812 height = 1;
3813 }
3814
3815#ifdef FEAT_WINDOWS
3816 frame_setheight(win->w_frame, height + win->w_status_height);
3817
3818 /* recompute the window positions */
3819 row = win_comp_pos();
3820#else
3821 if (height > topframe->fr_height)
3822 height = topframe->fr_height;
3823 win->w_height = height;
3824 row = height;
3825#endif
3826
3827 /*
3828 * If there is extra space created between the last window and the command
3829 * line, clear it.
3830 */
3831 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
3832 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
3833 cmdline_row = row;
3834 msg_row = row;
3835 msg_col = 0;
3836
3837 redraw_all_later(NOT_VALID);
3838}
3839
3840#if defined(FEAT_WINDOWS) || defined(PROTO)
3841
3842/*
3843 * Set the height of a frame to "height" and take care that all frames and
3844 * windows inside it are resized. Also resize frames on the left and right if
3845 * the are in the same FR_ROW frame.
3846 *
3847 * Strategy:
3848 * If the frame is part of a FR_COL frame, try fitting the frame in that
3849 * frame. If that doesn't work (the FR_COL frame is too small), recursively
3850 * go to containing frames to resize them and make room.
3851 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
3852 * Check for the minimal height of the FR_ROW frame.
3853 * At the top level we can also use change the command line height.
3854 */
3855 static void
3856frame_setheight(curfrp, height)
3857 frame_T *curfrp;
3858 int height;
3859{
3860 int room; /* total number of lines available */
3861 int take; /* number of lines taken from other windows */
3862 int room_cmdline; /* lines available from cmdline */
3863 int run;
3864 frame_T *frp;
3865 int h;
3866 int room_reserved;
3867
3868 /* If the height already is the desired value, nothing to do. */
3869 if (curfrp->fr_height == height)
3870 return;
3871
3872 if (curfrp->fr_parent == NULL)
3873 {
3874 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003875 if (height > ROWS_AVAIL)
3876 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 if (height > 0)
3878 frame_new_height(curfrp, height, FALSE, FALSE);
3879 }
3880 else if (curfrp->fr_parent->fr_layout == FR_ROW)
3881 {
3882 /* Row of frames: Also need to resize frames left and right of this
3883 * one. First check for the minimal height of these. */
3884 h = frame_minheight(curfrp->fr_parent, NULL);
3885 if (height < h)
3886 height = h;
3887 frame_setheight(curfrp->fr_parent, height);
3888 }
3889 else
3890 {
3891 /*
3892 * Column of frames: try to change only frames in this column.
3893 */
3894#ifdef FEAT_VERTSPLIT
3895 /*
3896 * Do this twice:
3897 * 1: compute room available, if it's not enough try resizing the
3898 * containing frame.
3899 * 2: compute the room available and adjust the height to it.
3900 * Try not to reduce the height of a window with 'winfixheight' set.
3901 */
3902 for (run = 1; run <= 2; ++run)
3903#else
3904 for (;;)
3905#endif
3906 {
3907 room = 0;
3908 room_reserved = 0;
3909 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
3910 frp = frp->fr_next)
3911 {
3912 if (frp != curfrp
3913 && frp->fr_win != NULL
3914 && frp->fr_win->w_p_wfh)
3915 room_reserved += frp->fr_height;
3916 room += frp->fr_height;
3917 if (frp != curfrp)
3918 room -= frame_minheight(frp, NULL);
3919 }
3920#ifdef FEAT_VERTSPLIT
3921 if (curfrp->fr_width != Columns)
3922 room_cmdline = 0;
3923 else
3924#endif
3925 {
3926 room_cmdline = Rows - p_ch - (lastwin->w_winrow
3927 + lastwin->w_height + lastwin->w_status_height);
3928 if (room_cmdline < 0)
3929 room_cmdline = 0;
3930 }
3931
3932 if (height <= room + room_cmdline)
3933 break;
3934#ifdef FEAT_VERTSPLIT
3935 if (run == 2 || curfrp->fr_width == Columns)
3936#endif
3937 {
3938 if (height > room + room_cmdline)
3939 height = room + room_cmdline;
3940 break;
3941 }
3942#ifdef FEAT_VERTSPLIT
3943 frame_setheight(curfrp->fr_parent, height
3944 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
3945#endif
3946 /*NOTREACHED*/
3947 }
3948
3949 /*
3950 * Compute the number of lines we will take from others frames (can be
3951 * negative!).
3952 */
3953 take = height - curfrp->fr_height;
3954
3955 /* If there is not enough room, also reduce the height of a window
3956 * with 'winfixheight' set. */
3957 if (height > room + room_cmdline - room_reserved)
3958 room_reserved = room + room_cmdline - height;
3959 /* If there is only a 'winfixheight' window and making the
3960 * window smaller, need to make the other window taller. */
3961 if (take < 0 && room - curfrp->fr_height < room_reserved)
3962 room_reserved = 0;
3963
3964 if (take > 0 && room_cmdline > 0)
3965 {
3966 /* use lines from cmdline first */
3967 if (take < room_cmdline)
3968 room_cmdline = take;
3969 take -= room_cmdline;
3970 topframe->fr_height += room_cmdline;
3971 }
3972
3973 /*
3974 * set the current frame to the new height
3975 */
3976 frame_new_height(curfrp, height, FALSE, FALSE);
3977
3978 /*
3979 * First take lines from the frames after the current frame. If
3980 * that is not enough, takes lines from frames above the current
3981 * frame.
3982 */
3983 for (run = 0; run < 2; ++run)
3984 {
3985 if (run == 0)
3986 frp = curfrp->fr_next; /* 1st run: start with next window */
3987 else
3988 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
3989 while (frp != NULL && take != 0)
3990 {
3991 h = frame_minheight(frp, NULL);
3992 if (room_reserved > 0
3993 && frp->fr_win != NULL
3994 && frp->fr_win->w_p_wfh)
3995 {
3996 if (room_reserved >= frp->fr_height)
3997 room_reserved -= frp->fr_height;
3998 else
3999 {
4000 if (frp->fr_height - room_reserved > take)
4001 room_reserved = frp->fr_height - take;
4002 take -= frp->fr_height - room_reserved;
4003 frame_new_height(frp, room_reserved, FALSE, FALSE);
4004 room_reserved = 0;
4005 }
4006 }
4007 else
4008 {
4009 if (frp->fr_height - take < h)
4010 {
4011 take -= frp->fr_height - h;
4012 frame_new_height(frp, h, FALSE, FALSE);
4013 }
4014 else
4015 {
4016 frame_new_height(frp, frp->fr_height - take,
4017 FALSE, FALSE);
4018 take = 0;
4019 }
4020 }
4021 if (run == 0)
4022 frp = frp->fr_next;
4023 else
4024 frp = frp->fr_prev;
4025 }
4026 }
4027 }
4028}
4029
4030#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4031/*
4032 * Set current window width and take care of repositioning other windows to
4033 * fit around it.
4034 */
4035 void
4036win_setwidth(width)
4037 int width;
4038{
4039 win_setwidth_win(width, curwin);
4040}
4041
4042 void
4043win_setwidth_win(width, wp)
4044 int width;
4045 win_T *wp;
4046{
4047 /* Always keep current window at least one column wide, even when
4048 * 'winminwidth' is zero. */
4049 if (wp == curwin)
4050 {
4051 if (width < p_wmw)
4052 width = p_wmw;
4053 if (width == 0)
4054 width = 1;
4055 }
4056
4057 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4058
4059 /* recompute the window positions */
4060 (void)win_comp_pos();
4061
4062 redraw_all_later(NOT_VALID);
4063}
4064
4065/*
4066 * Set the width of a frame to "width" and take care that all frames and
4067 * windows inside it are resized. Also resize frames above and below if the
4068 * are in the same FR_ROW frame.
4069 *
4070 * Strategy is similar to frame_setheight().
4071 */
4072 static void
4073frame_setwidth(curfrp, width)
4074 frame_T *curfrp;
4075 int width;
4076{
4077 int room; /* total number of lines available */
4078 int take; /* number of lines taken from other windows */
4079 int run;
4080 frame_T *frp;
4081 int w;
4082
4083 /* If the width already is the desired value, nothing to do. */
4084 if (curfrp->fr_width == width)
4085 return;
4086
4087 if (curfrp->fr_parent == NULL)
4088 /* topframe: can't change width */
4089 return;
4090
4091 if (curfrp->fr_parent->fr_layout == FR_COL)
4092 {
4093 /* Column of frames: Also need to resize frames above and below of
4094 * this one. First check for the minimal width of these. */
4095 w = frame_minwidth(curfrp->fr_parent, NULL);
4096 if (width < w)
4097 width = w;
4098 frame_setwidth(curfrp->fr_parent, width);
4099 }
4100 else
4101 {
4102 /*
4103 * Row of frames: try to change only frames in this row.
4104 *
4105 * Do this twice:
4106 * 1: compute room available, if it's not enough try resizing the
4107 * containing frame.
4108 * 2: compute the room available and adjust the width to it.
4109 */
4110 for (run = 1; run <= 2; ++run)
4111 {
4112 room = 0;
4113 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4114 frp = frp->fr_next)
4115 {
4116 room += frp->fr_width;
4117 if (frp != curfrp)
4118 room -= frame_minwidth(frp, NULL);
4119 }
4120
4121 if (width <= room)
4122 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004123 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
4125 if (width > room)
4126 width = room;
4127 break;
4128 }
4129 frame_setwidth(curfrp->fr_parent, width
4130 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4131 }
4132
4133
4134 /*
4135 * Compute the number of lines we will take from others frames (can be
4136 * negative!).
4137 */
4138 take = width - curfrp->fr_width;
4139
4140 /*
4141 * set the current frame to the new width
4142 */
4143 frame_new_width(curfrp, width, FALSE);
4144
4145 /*
4146 * First take lines from the frames right of the current frame. If
4147 * that is not enough, takes lines from frames left of the current
4148 * frame.
4149 */
4150 for (run = 0; run < 2; ++run)
4151 {
4152 if (run == 0)
4153 frp = curfrp->fr_next; /* 1st run: start with next window */
4154 else
4155 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4156 while (frp != NULL && take != 0)
4157 {
4158 w = frame_minwidth(frp, NULL);
4159 if (frp->fr_width - take < w)
4160 {
4161 take -= frp->fr_width - w;
4162 frame_new_width(frp, w, FALSE);
4163 }
4164 else
4165 {
4166 frame_new_width(frp, frp->fr_width - take, FALSE);
4167 take = 0;
4168 }
4169 if (run == 0)
4170 frp = frp->fr_next;
4171 else
4172 frp = frp->fr_prev;
4173 }
4174 }
4175 }
4176}
4177#endif /* FEAT_VERTSPLIT */
4178
4179/*
4180 * Check 'winminheight' for a valid value.
4181 */
4182 void
4183win_setminheight()
4184{
4185 int room;
4186 int first = TRUE;
4187 win_T *wp;
4188
4189 /* loop until there is a 'winminheight' that is possible */
4190 while (p_wmh > 0)
4191 {
4192 /* TODO: handle vertical splits */
4193 room = -p_wh;
4194 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4195 room += wp->w_height - p_wmh;
4196 if (room >= 0)
4197 break;
4198 --p_wmh;
4199 if (first)
4200 {
4201 EMSG(_(e_noroom));
4202 first = FALSE;
4203 }
4204 }
4205}
4206
4207#ifdef FEAT_MOUSE
4208
4209/*
4210 * Status line of dragwin is dragged "offset" lines down (negative is up).
4211 */
4212 void
4213win_drag_status_line(dragwin, offset)
4214 win_T *dragwin;
4215 int offset;
4216{
4217 frame_T *curfr;
4218 frame_T *fr;
4219 int room;
4220 int row;
4221 int up; /* if TRUE, drag status line up, otherwise down */
4222 int n;
4223
4224 fr = dragwin->w_frame;
4225 curfr = fr;
4226 if (fr != topframe) /* more than one window */
4227 {
4228 fr = fr->fr_parent;
4229 /* When the parent frame is not a column of frames, its parent should
4230 * be. */
4231 if (fr->fr_layout != FR_COL)
4232 {
4233 curfr = fr;
4234 if (fr != topframe) /* only a row of windows, may drag statusline */
4235 fr = fr->fr_parent;
4236 }
4237 }
4238
4239 /* If this is the last frame in a column, may want to resize the parent
4240 * frame instead (go two up to skip a row of frames). */
4241 while (curfr != topframe && curfr->fr_next == NULL)
4242 {
4243 if (fr != topframe)
4244 fr = fr->fr_parent;
4245 curfr = fr;
4246 if (fr != topframe)
4247 fr = fr->fr_parent;
4248 }
4249
4250 if (offset < 0) /* drag up */
4251 {
4252 up = TRUE;
4253 offset = -offset;
4254 /* sum up the room of the current frame and above it */
4255 if (fr == curfr)
4256 {
4257 /* only one window */
4258 room = fr->fr_height - frame_minheight(fr, NULL);
4259 }
4260 else
4261 {
4262 room = 0;
4263 for (fr = fr->fr_child; ; fr = fr->fr_next)
4264 {
4265 room += fr->fr_height - frame_minheight(fr, NULL);
4266 if (fr == curfr)
4267 break;
4268 }
4269 }
4270 fr = curfr->fr_next; /* put fr at frame that grows */
4271 }
4272 else /* drag down */
4273 {
4274 up = FALSE;
4275 /*
4276 * Only dragging the last status line can reduce p_ch.
4277 */
4278 room = Rows - cmdline_row;
4279 if (curfr->fr_next == NULL)
4280 room -= 1;
4281 else
4282 room -= p_ch;
4283 if (room < 0)
4284 room = 0;
4285 /* sum up the room of frames below of the current one */
4286 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4287 room += fr->fr_height - frame_minheight(fr, NULL);
4288 fr = curfr; /* put fr at window that grows */
4289 }
4290
4291 if (room < offset) /* Not enough room */
4292 offset = room; /* Move as far as we can */
4293 if (offset <= 0)
4294 return;
4295
4296 /*
4297 * Grow frame fr by "offset" lines.
4298 * Doesn't happen when dragging the last status line up.
4299 */
4300 if (fr != NULL)
4301 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
4302
4303 if (up)
4304 fr = curfr; /* current frame gets smaller */
4305 else
4306 fr = curfr->fr_next; /* next frame gets smaller */
4307
4308 /*
4309 * Now make the other frames smaller.
4310 */
4311 while (fr != NULL && offset > 0)
4312 {
4313 n = frame_minheight(fr, NULL);
4314 if (fr->fr_height - offset <= n)
4315 {
4316 offset -= fr->fr_height - n;
4317 frame_new_height(fr, n, !up, FALSE);
4318 }
4319 else
4320 {
4321 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
4322 break;
4323 }
4324 if (up)
4325 fr = fr->fr_prev;
4326 else
4327 fr = fr->fr_next;
4328 }
4329 row = win_comp_pos();
4330 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4331 cmdline_row = row;
4332 p_ch = Rows - cmdline_row;
4333 if (p_ch < 1)
4334 p_ch = 1;
4335 redraw_all_later(NOT_VALID);
4336 showmode();
4337}
4338
4339#ifdef FEAT_VERTSPLIT
4340/*
4341 * Separator line of dragwin is dragged "offset" lines right (negative is left).
4342 */
4343 void
4344win_drag_vsep_line(dragwin, offset)
4345 win_T *dragwin;
4346 int offset;
4347{
4348 frame_T *curfr;
4349 frame_T *fr;
4350 int room;
4351 int left; /* if TRUE, drag separator line left, otherwise right */
4352 int n;
4353
4354 fr = dragwin->w_frame;
4355 if (fr == topframe) /* only one window (cannot happe?) */
4356 return;
4357 curfr = fr;
4358 fr = fr->fr_parent;
4359 /* When the parent frame is not a row of frames, its parent should be. */
4360 if (fr->fr_layout != FR_ROW)
4361 {
4362 if (fr == topframe) /* only a column of windows (cannot happen?) */
4363 return;
4364 curfr = fr;
4365 fr = fr->fr_parent;
4366 }
4367
4368 /* If this is the last frame in a row, may want to resize a parent
4369 * frame instead. */
4370 while (curfr->fr_next == NULL)
4371 {
4372 if (fr == topframe)
4373 break;
4374 curfr = fr;
4375 fr = fr->fr_parent;
4376 if (fr != topframe)
4377 {
4378 curfr = fr;
4379 fr = fr->fr_parent;
4380 }
4381 }
4382
4383 if (offset < 0) /* drag left */
4384 {
4385 left = TRUE;
4386 offset = -offset;
4387 /* sum up the room of the current frame and left of it */
4388 room = 0;
4389 for (fr = fr->fr_child; ; fr = fr->fr_next)
4390 {
4391 room += fr->fr_width - frame_minwidth(fr, NULL);
4392 if (fr == curfr)
4393 break;
4394 }
4395 fr = curfr->fr_next; /* put fr at frame that grows */
4396 }
4397 else /* drag right */
4398 {
4399 left = FALSE;
4400 /* sum up the room of frames right of the current one */
4401 room = 0;
4402 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4403 room += fr->fr_width - frame_minwidth(fr, NULL);
4404 fr = curfr; /* put fr at window that grows */
4405 }
4406
4407 if (room < offset) /* Not enough room */
4408 offset = room; /* Move as far as we can */
4409 if (offset <= 0) /* No room at all, quit. */
4410 return;
4411
4412 /* grow frame fr by offset lines */
4413 frame_new_width(fr, fr->fr_width + offset, left);
4414
4415 /* shrink other frames: current and at the left or at the right */
4416 if (left)
4417 fr = curfr; /* current frame gets smaller */
4418 else
4419 fr = curfr->fr_next; /* next frame gets smaller */
4420
4421 while (fr != NULL && offset > 0)
4422 {
4423 n = frame_minwidth(fr, NULL);
4424 if (fr->fr_width - offset <= n)
4425 {
4426 offset -= fr->fr_width - n;
4427 frame_new_width(fr, n, !left);
4428 }
4429 else
4430 {
4431 frame_new_width(fr, fr->fr_width - offset, !left);
4432 break;
4433 }
4434 if (left)
4435 fr = fr->fr_prev;
4436 else
4437 fr = fr->fr_next;
4438 }
4439 (void)win_comp_pos();
4440 redraw_all_later(NOT_VALID);
4441}
4442#endif /* FEAT_VERTSPLIT */
4443#endif /* FEAT_MOUSE */
4444
4445#endif /* FEAT_WINDOWS */
4446
4447/*
4448 * Set the height of a window.
4449 * This takes care of the things inside the window, not what happens to the
4450 * window position, the frame or to other windows.
4451 */
4452 static void
4453win_new_height(wp, height)
4454 win_T *wp;
4455 int height;
4456{
4457 linenr_T lnum;
Bram Moolenaar34114692005-01-02 11:28:13 +00004458 linenr_T bot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 int sline, line_size;
Bram Moolenaar34114692005-01-02 11:28:13 +00004460 int space;
4461 int did_below = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462#define FRACTION_MULT 16384L
4463
4464 /* Don't want a negative height. Happens when splitting a tiny window.
4465 * Will equalize heights soon to fix it. */
4466 if (height < 0)
4467 height = 0;
4468
4469 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
4470 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
4471 + FRACTION_MULT / 2) / (long)wp->w_height;
4472
4473 wp->w_height = height;
4474 wp->w_skipcol = 0;
4475
4476 /* Don't change w_topline when height is zero. Don't set w_topline when
4477 * 'scrollbind' is set and this isn't the current window. */
4478 if (height > 0
4479#ifdef FEAT_SCROLLBIND
4480 && (!wp->w_p_scb || wp == curwin)
4481#endif
4482 )
4483 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004484 /*
4485 * Find a value for w_topline that shows the cursor at the same
4486 * relative position in the window as before (more or less).
4487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 lnum = wp->w_cursor.lnum;
4489 if (lnum < 1) /* can happen when starting up */
4490 lnum = 1;
4491 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
4492 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
4493 sline = wp->w_wrow - line_size;
4494 if (sline < 0)
4495 {
4496 /*
4497 * Cursor line would go off top of screen if w_wrow was this high.
4498 */
4499 wp->w_wrow = line_size;
4500 }
4501 else
4502 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004503 space = height;
4504 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004506 space -= line_size;
4507 if (space > 0 && sline <= 0 && !did_below)
4508 {
4509 /* Try to use "~" lines below the text to avoid that text
4510 * is above the window while there are empty lines.
4511 * Subtract the rows below the cursor from "space" and
4512 * give the rest to "sline". */
4513 did_below = TRUE;
4514 bot = wp->w_cursor.lnum;
4515 while (space > 0)
4516 {
4517 if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
4518 space = 0;
4519 else
4520 {
4521#ifdef FEAT_FOLDING
4522 hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
4523#endif
4524 if (bot >= wp->w_buffer->b_ml.ml_line_count)
4525 break;
4526 ++bot;
4527 space -= plines_win(wp, bot, TRUE);
4528 }
4529 }
4530 if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
4531 sline += space;
4532 }
4533 if (sline <= 0)
4534 break;
4535
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536#ifdef FEAT_FOLDING
4537 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
4538 if (lnum == 1)
4539 {
4540 /* first line in buffer is folded */
4541 line_size = 1;
4542 --sline;
4543 break;
4544 }
4545#endif
4546 --lnum;
4547#ifdef FEAT_DIFF
4548 if (lnum == wp->w_topline)
4549 line_size = plines_win_nofill(wp, lnum, TRUE)
4550 + wp->w_topfill;
4551 else
4552#endif
4553 line_size = plines_win(wp, lnum, TRUE);
4554 sline -= line_size;
4555 }
Bram Moolenaar34114692005-01-02 11:28:13 +00004556
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (sline < 0)
4558 {
4559 /*
4560 * Line we want at top would go off top of screen. Use next
4561 * line instead.
4562 */
4563#ifdef FEAT_FOLDING
4564 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
4565#endif
4566 lnum++;
4567 wp->w_wrow -= line_size + sline;
4568 }
4569 else if (sline > 0)
4570 {
4571 /* First line of file reached, use that as topline. */
4572 lnum = 1;
4573 wp->w_wrow -= sline;
4574 }
4575 }
4576 set_topline(wp, lnum);
4577 }
4578
4579 if (wp == curwin)
4580 {
4581 if (p_so)
4582 update_topline();
4583 curs_columns(FALSE); /* validate w_wrow */
4584 }
4585 wp->w_prev_fraction_row = wp->w_wrow;
4586
4587 win_comp_scroll(wp);
4588 redraw_win_later(wp, NOT_VALID);
4589#ifdef FEAT_WINDOWS
4590 wp->w_redr_status = TRUE;
4591#endif
4592 invalidate_botline_win(wp);
4593}
4594
4595#ifdef FEAT_VERTSPLIT
4596/*
4597 * Set the width of a window.
4598 */
4599 static void
4600win_new_width(wp, width)
4601 win_T *wp;
4602 int width;
4603{
4604 wp->w_width = width;
4605 wp->w_lines_valid = 0;
4606 changed_line_abv_curs_win(wp);
4607 invalidate_botline_win(wp);
4608 if (wp == curwin)
4609 {
4610 update_topline();
4611 curs_columns(TRUE); /* validate w_wrow */
4612 }
4613 redraw_win_later(wp, NOT_VALID);
4614 wp->w_redr_status = TRUE;
4615}
4616#endif
4617
4618 void
4619win_comp_scroll(wp)
4620 win_T *wp;
4621{
4622 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
4623 if (wp->w_p_scr == 0)
4624 wp->w_p_scr = 1;
4625}
4626
4627/*
4628 * command_height: called whenever p_ch has been changed
4629 */
4630 void
4631command_height(old_p_ch)
4632 long old_p_ch;
4633{
4634#ifdef FEAT_WINDOWS
4635 int h;
4636 frame_T *frp;
4637
Bram Moolenaar05159a02005-02-26 23:04:13 +00004638 /* When passed a negative value use the value of p_ch that we remembered.
4639 * This is needed for when the GUI starts up, we can't be sure in what
4640 * order things happen. */
4641 if (old_p_ch < 0)
4642 old_p_ch = p_ch_used;
4643 p_ch_used = p_ch;
4644
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 /* Find bottom frame with width of screen. */
4646 frp = lastwin->w_frame;
4647# ifdef FEAT_VERTSPLIT
4648 while (frp->fr_width != Columns && frp->fr_parent != NULL)
4649 frp = frp->fr_parent;
4650# endif
4651
4652 /* Avoid changing the height of a window with 'winfixheight' set. */
4653 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
4654 && frp->fr_win->w_p_wfh)
4655 frp = frp->fr_prev;
4656
4657 if (starting != NO_SCREEN)
4658 {
4659 cmdline_row = Rows - p_ch;
4660
4661 if (p_ch > old_p_ch) /* p_ch got bigger */
4662 {
4663 while (p_ch > old_p_ch)
4664 {
4665 if (frp == NULL)
4666 {
4667 EMSG(_(e_noroom));
4668 p_ch = old_p_ch;
4669 cmdline_row = Rows - p_ch;
4670 break;
4671 }
4672 h = frp->fr_height - frame_minheight(frp, NULL);
4673 if (h > p_ch - old_p_ch)
4674 h = p_ch - old_p_ch;
4675 old_p_ch += h;
4676 frame_add_height(frp, -h);
4677 frp = frp->fr_prev;
4678 }
4679
4680 /* Recompute window positions. */
4681 (void)win_comp_pos();
4682
4683 /* clear the lines added to cmdline */
4684 if (full_screen)
4685 screen_fill((int)(cmdline_row), (int)Rows, 0,
4686 (int)Columns, ' ', ' ', 0);
4687 msg_row = cmdline_row;
4688 redraw_cmdline = TRUE;
4689 return;
4690 }
4691
4692 if (msg_row < cmdline_row)
4693 msg_row = cmdline_row;
4694 redraw_cmdline = TRUE;
4695 }
4696 frame_add_height(frp, (int)(old_p_ch - p_ch));
4697
4698 /* Recompute window positions. */
4699 if (frp != lastwin->w_frame)
4700 (void)win_comp_pos();
4701#else
4702 win_setheight((int)(firstwin->w_height + old_p_ch - p_ch));
4703 cmdline_row = Rows - p_ch;
4704#endif
4705}
4706
4707#if defined(FEAT_WINDOWS) || defined(PROTO)
4708/*
4709 * Resize frame "frp" to be "n" lines higher (negative for less high).
4710 * Also resize the frames it is contained in.
4711 */
4712 static void
4713frame_add_height(frp, n)
4714 frame_T *frp;
4715 int n;
4716{
4717 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
4718 for (;;)
4719 {
4720 frp = frp->fr_parent;
4721 if (frp == NULL)
4722 break;
4723 frp->fr_height += n;
4724 }
4725}
4726
4727/*
4728 * Add or remove a status line for the bottom window(s), according to the
4729 * value of 'laststatus'.
4730 */
4731 void
4732last_status(morewin)
4733 int morewin; /* pretend there are two or more windows */
4734{
4735 /* Don't make a difference between horizontal or vertical split. */
4736 last_status_rec(topframe, (p_ls == 2
4737 || (p_ls == 1 && (morewin || lastwin != firstwin))));
4738}
4739
4740 static void
4741last_status_rec(fr, statusline)
4742 frame_T *fr;
4743 int statusline;
4744{
4745 frame_T *fp;
4746 win_T *wp;
4747
4748 if (fr->fr_layout == FR_LEAF)
4749 {
4750 wp = fr->fr_win;
4751 if (wp->w_status_height != 0 && !statusline)
4752 {
4753 /* remove status line */
4754 win_new_height(wp, wp->w_height + 1);
4755 wp->w_status_height = 0;
4756 comp_col();
4757 }
4758 else if (wp->w_status_height == 0 && statusline)
4759 {
4760 /* Find a frame to take a line from. */
4761 fp = fr;
4762 while (fp->fr_height <= frame_minheight(fp, NULL))
4763 {
4764 if (fp == topframe)
4765 {
4766 EMSG(_(e_noroom));
4767 return;
4768 }
4769 /* In a column of frames: go to frame above. If already at
4770 * the top or in a row of frames: go to parent. */
4771 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
4772 fp = fp->fr_prev;
4773 else
4774 fp = fp->fr_parent;
4775 }
4776 wp->w_status_height = 1;
4777 if (fp != fr)
4778 {
4779 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
4780 frame_fix_height(wp);
4781 (void)win_comp_pos();
4782 }
4783 else
4784 win_new_height(wp, wp->w_height - 1);
4785 comp_col();
4786 redraw_all_later(NOT_VALID);
4787 }
4788 }
4789#ifdef FEAT_VERTSPLIT
4790 else if (fr->fr_layout == FR_ROW)
4791 {
4792 /* vertically split windows, set status line for each one */
4793 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
4794 last_status_rec(fp, statusline);
4795 }
4796#endif
4797 else
4798 {
4799 /* horizontally split window, set status line for last one */
4800 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
4801 ;
4802 last_status_rec(fp, statusline);
4803 }
4804}
4805
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004806/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004807 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004808 */
4809 int
4810tabpageline_height()
4811{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004812 switch (p_tal)
4813 {
4814 case 0: return 0;
4815 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
4816 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004817 return 1;
4818}
4819
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820#endif /* FEAT_WINDOWS */
4821
4822#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4823/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004824 * Get the file name at the cursor.
4825 * If Visual mode is active, use the selected text if it's in one line.
4826 * Returns the name in allocated memory, NULL for failure.
4827 */
4828 char_u *
4829grab_file_name(count)
4830 long count;
4831{
4832# ifdef FEAT_VISUAL
4833 if (VIsual_active)
4834 {
4835 int len;
4836 char_u *ptr;
4837
4838 if (get_visual_text(NULL, &ptr, &len) == FAIL)
4839 return NULL;
4840 return find_file_name_in_path(ptr, len,
4841 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
4842 }
4843# endif
4844 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count);
4845}
4846
4847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 * Return the file name under or after the cursor.
4849 *
4850 * The 'path' option is searched if the file name is not absolute.
4851 * The string returned has been alloc'ed and should be freed by the caller.
4852 * NULL is returned if the file name or file is not found.
4853 *
4854 * options:
4855 * FNAME_MESS give error messages
4856 * FNAME_EXP expand to path
4857 * FNAME_HYP check for hypertext link
4858 * FNAME_INCL apply "includeexpr"
4859 */
4860 char_u *
4861file_name_at_cursor(options, count)
4862 int options;
4863 long count;
4864{
4865 return file_name_in_line(ml_get_curline(),
4866 curwin->w_cursor.col, options, count, curbuf->b_ffname);
4867}
4868
4869/*
4870 * Return the name of the file under or after ptr[col].
4871 * Otherwise like file_name_at_cursor().
4872 */
4873 char_u *
4874file_name_in_line(line, col, options, count, rel_fname)
4875 char_u *line;
4876 int col;
4877 int options;
4878 long count;
4879 char_u *rel_fname; /* file we are searching relative to */
4880{
4881 char_u *ptr;
4882 int len;
4883
4884 /*
4885 * search forward for what could be the start of a file name
4886 */
4887 ptr = line + col;
4888 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00004889 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 if (*ptr == NUL) /* nothing found */
4891 {
4892 if (options & FNAME_MESS)
4893 EMSG(_("E446: No file name under cursor"));
4894 return NULL;
4895 }
4896
4897 /*
4898 * Search backward for first char of the file name.
4899 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
4900 */
4901 while (ptr > line)
4902 {
4903#ifdef FEAT_MBYTE
4904 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
4905 ptr -= len + 1;
4906 else
4907#endif
4908 if (vim_isfilec(ptr[-1])
4909 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
4910 --ptr;
4911 else
4912 break;
4913 }
4914
4915 /*
4916 * Search forward for the last char of the file name.
4917 * Also allow "://" when ':' is not in 'isfname'.
4918 */
4919 len = 0;
4920 while (vim_isfilec(ptr[len])
4921 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
4922#ifdef FEAT_MBYTE
4923 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004924 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 else
4926#endif
4927 ++len;
4928
4929 /*
4930 * If there is trailing punctuation, remove it.
4931 * But don't remove "..", could be a directory name.
4932 */
4933 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
4934 && ptr[len - 2] != '.')
4935 --len;
4936
4937 return find_file_name_in_path(ptr, len, options, count, rel_fname);
4938}
4939
4940# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
4941static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
4942
4943 static char_u *
4944eval_includeexpr(ptr, len)
4945 char_u *ptr;
4946 int len;
4947{
4948 char_u *res;
4949
4950 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004951 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
4952 was_set_insecurely((char_u *)"includeexpr"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 set_vim_var_string(VV_FNAME, NULL, 0);
4954 return res;
4955}
4956#endif
4957
4958/*
4959 * Return the name of the file ptr[len] in 'path'.
4960 * Otherwise like file_name_at_cursor().
4961 */
4962 char_u *
4963find_file_name_in_path(ptr, len, options, count, rel_fname)
4964 char_u *ptr;
4965 int len;
4966 int options;
4967 long count;
4968 char_u *rel_fname; /* file we are searching relative to */
4969{
4970 char_u *file_name;
4971 int c;
4972# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
4973 char_u *tofree = NULL;
4974
4975 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
4976 {
4977 tofree = eval_includeexpr(ptr, len);
4978 if (tofree != NULL)
4979 {
4980 ptr = tofree;
4981 len = (int)STRLEN(ptr);
4982 }
4983 }
4984# endif
4985
4986 if (options & FNAME_EXP)
4987 {
4988 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
4989 TRUE, rel_fname);
4990
4991# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
4992 /*
4993 * If the file could not be found in a normal way, try applying
4994 * 'includeexpr' (unless done already).
4995 */
4996 if (file_name == NULL
4997 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
4998 {
4999 tofree = eval_includeexpr(ptr, len);
5000 if (tofree != NULL)
5001 {
5002 ptr = tofree;
5003 len = (int)STRLEN(ptr);
5004 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5005 TRUE, rel_fname);
5006 }
5007 }
5008# endif
5009 if (file_name == NULL && (options & FNAME_MESS))
5010 {
5011 c = ptr[len];
5012 ptr[len] = NUL;
5013 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5014 ptr[len] = c;
5015 }
5016
5017 /* Repeat finding the file "count" times. This matters when it
5018 * appears several times in the path. */
5019 while (file_name != NULL && --count > 0)
5020 {
5021 vim_free(file_name);
5022 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5023 }
5024 }
5025 else
5026 file_name = vim_strnsave(ptr, len);
5027
5028# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5029 vim_free(tofree);
5030# endif
5031
5032 return file_name;
5033}
5034#endif /* FEAT_SEARCHPATH */
5035
5036/*
5037 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5038 * Also check for ":\\", which MS Internet Explorer accepts, return
5039 * URL_BACKSLASH.
5040 */
5041 static int
5042path_is_url(p)
5043 char_u *p;
5044{
5045 if (STRNCMP(p, "://", (size_t)3) == 0)
5046 return URL_SLASH;
5047 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5048 return URL_BACKSLASH;
5049 return 0;
5050}
5051
5052/*
5053 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5054 * Return URL_BACKSLASH for "name:\\".
5055 * Return zero otherwise.
5056 */
5057 int
5058path_with_url(fname)
5059 char_u *fname;
5060{
5061 char_u *p;
5062
5063 for (p = fname; isalpha(*p); ++p)
5064 ;
5065 return path_is_url(p);
5066}
5067
5068/*
5069 * Return TRUE if "name" is a full (absolute) path name or URL.
5070 */
5071 int
5072vim_isAbsName(name)
5073 char_u *name;
5074{
5075 return (path_with_url(name) != 0 || mch_isFullName(name));
5076}
5077
5078/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005079 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 *
5081 * return FAIL for failure, OK otherwise
5082 */
5083 int
5084vim_FullName(fname, buf, len, force)
5085 char_u *fname, *buf;
5086 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005087 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088{
5089 int retval = OK;
5090 int url;
5091
5092 *buf = NUL;
5093 if (fname == NULL)
5094 return FAIL;
5095
5096 url = path_with_url(fname);
5097 if (!url)
5098 retval = mch_FullName(fname, buf, len, force);
5099 if (url || retval == FAIL)
5100 {
5101 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005102 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 }
5104#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5105 slash_adjust(buf);
5106#endif
5107 return retval;
5108}
5109
5110/*
5111 * Return the minimal number of rows that is needed on the screen to display
5112 * the current number of windows.
5113 */
5114 int
5115min_rows()
5116{
5117 int total;
5118
5119 if (firstwin == NULL) /* not initialized yet */
5120 return MIN_LINES;
5121
5122 total = 1; /* count the room for the command line */
5123#ifdef FEAT_WINDOWS
5124 total += frame_minheight(topframe, NULL);
5125#else
5126 total += 1; /* at least one window should have a line! */
5127#endif
5128 return total;
5129}
5130
5131/*
5132 * Return TRUE if there is only one window, not counting a help or preview
5133 * window, unless it is the current window.
5134 */
5135 int
5136only_one_window()
5137{
5138#ifdef FEAT_WINDOWS
5139 int count = 0;
5140 win_T *wp;
5141
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005142 /* If there is another tab page there always is another window. */
5143 if (first_tabpage->tp_next != NULL)
5144 return FALSE;
5145
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005147 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148# ifdef FEAT_QUICKFIX
5149 || wp->w_p_pvw
5150# endif
5151 ) || wp == curwin)
5152 ++count;
5153 return (count <= 1);
5154#else
5155 return TRUE;
5156#endif
5157}
5158
5159#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5160/*
5161 * Correct the cursor line number in other windows. Used after changing the
5162 * current buffer, and before applying autocommands.
5163 * When "do_curwin" is TRUE, also check current window.
5164 */
5165 void
5166check_lnums(do_curwin)
5167 int do_curwin;
5168{
5169 win_T *wp;
5170
5171#ifdef FEAT_WINDOWS
5172 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5173 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5174#else
5175 wp = curwin;
5176 if (do_curwin)
5177#endif
5178 {
5179 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5180 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5181 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5182 wp->w_topline = curbuf->b_ml.ml_line_count;
5183 }
5184}
5185#endif
5186
5187#if defined(FEAT_WINDOWS) || defined(PROTO)
5188
5189/*
5190 * A snapshot of the window sizes, to restore them after closing the help
5191 * window.
5192 * Only these fields are used:
5193 * fr_layout
5194 * fr_width
5195 * fr_height
5196 * fr_next
5197 * fr_child
5198 * fr_win (only valid for the old curwin, NULL otherwise)
5199 */
5200static frame_T *snapshot = NULL;
5201
5202/*
5203 * Create a snapshot of the current frame sizes.
5204 */
5205 static void
5206make_snapshot()
5207{
5208 clear_snapshot();
5209 make_snapshot_rec(topframe, &snapshot);
5210}
5211
5212 static void
5213make_snapshot_rec(fr, frp)
5214 frame_T *fr;
5215 frame_T **frp;
5216{
5217 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5218 if (*frp == NULL)
5219 return;
5220 (*frp)->fr_layout = fr->fr_layout;
5221# ifdef FEAT_VERTSPLIT
5222 (*frp)->fr_width = fr->fr_width;
5223# endif
5224 (*frp)->fr_height = fr->fr_height;
5225 if (fr->fr_next != NULL)
5226 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
5227 if (fr->fr_child != NULL)
5228 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
5229 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
5230 (*frp)->fr_win = curwin;
5231}
5232
5233/*
5234 * Remove any existing snapshot.
5235 */
5236 static void
5237clear_snapshot()
5238{
5239 clear_snapshot_rec(snapshot);
5240 snapshot = NULL;
5241}
5242
5243 static void
5244clear_snapshot_rec(fr)
5245 frame_T *fr;
5246{
5247 if (fr != NULL)
5248 {
5249 clear_snapshot_rec(fr->fr_next);
5250 clear_snapshot_rec(fr->fr_child);
5251 vim_free(fr);
5252 }
5253}
5254
5255/*
5256 * Restore a previously created snapshot, if there is any.
5257 * This is only done if the screen size didn't change and the window layout is
5258 * still the same.
5259 */
5260 static void
5261restore_snapshot(close_curwin)
5262 int close_curwin; /* closing current window */
5263{
5264 win_T *wp;
5265
5266 if (snapshot != NULL
5267# ifdef FEAT_VERTSPLIT
5268 && snapshot->fr_width == topframe->fr_width
5269# endif
5270 && snapshot->fr_height == topframe->fr_height
5271 && check_snapshot_rec(snapshot, topframe) == OK)
5272 {
5273 wp = restore_snapshot_rec(snapshot, topframe);
5274 win_comp_pos();
5275 if (wp != NULL && close_curwin)
5276 win_goto(wp);
5277 redraw_all_later(CLEAR);
5278 }
5279 clear_snapshot();
5280}
5281
5282/*
5283 * Check if frames "sn" and "fr" have the same layout, same following frames
5284 * and same children.
5285 */
5286 static int
5287check_snapshot_rec(sn, fr)
5288 frame_T *sn;
5289 frame_T *fr;
5290{
5291 if (sn->fr_layout != fr->fr_layout
5292 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
5293 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
5294 || (sn->fr_next != NULL
5295 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
5296 || (sn->fr_child != NULL
5297 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
5298 return FAIL;
5299 return OK;
5300}
5301
5302/*
5303 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
5304 * following frames and children.
5305 * Returns a pointer to the old current window, or NULL.
5306 */
5307 static win_T *
5308restore_snapshot_rec(sn, fr)
5309 frame_T *sn;
5310 frame_T *fr;
5311{
5312 win_T *wp = NULL;
5313 win_T *wp2;
5314
5315 fr->fr_height = sn->fr_height;
5316# ifdef FEAT_VERTSPLIT
5317 fr->fr_width = sn->fr_width;
5318# endif
5319 if (fr->fr_layout == FR_LEAF)
5320 {
5321 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
5322# ifdef FEAT_VERTSPLIT
5323 frame_new_width(fr, fr->fr_width, FALSE);
5324# endif
5325 wp = sn->fr_win;
5326 }
5327 if (sn->fr_next != NULL)
5328 {
5329 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
5330 if (wp2 != NULL)
5331 wp = wp2;
5332 }
5333 if (sn->fr_child != NULL)
5334 {
5335 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
5336 if (wp2 != NULL)
5337 wp = wp2;
5338 }
5339 return wp;
5340}
5341
5342#endif
5343
5344#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5345/*
5346 * Return TRUE if there is any vertically split window.
5347 */
5348 int
5349win_hasvertsplit()
5350{
5351 frame_T *fr;
5352
5353 if (topframe->fr_layout == FR_ROW)
5354 return TRUE;
5355
5356 if (topframe->fr_layout == FR_COL)
5357 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
5358 if (fr->fr_layout == FR_ROW)
5359 return TRUE;
5360
5361 return FALSE;
5362}
5363#endif