blob: 927c7a15e48b88d92729d3bb5c6ce800d6991bb1 [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 Moolenaarf740b292006-02-16 22:11:02 +000028static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
29static win_T *winframe_remove __ARGS((win_T *win, int *dirp, tabpage_T *tp));
30static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
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));
Bram Moolenaarf740b292006-02-16 22:11:02 +000052static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000053static void win_append __ARGS((win_T *, win_T *));
Bram Moolenaarf740b292006-02-16 22:11:02 +000054static void win_remove __ARGS((win_T *, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000055static 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 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001265 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 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 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001272 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 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;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001356 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 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 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001375 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 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. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001422 (void)winframe_remove(curwin, &dir, NULL);
1423 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 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 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001489 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 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
Bram Moolenaarf740b292006-02-16 22:11:02 +00001800close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001802 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803{
Bram Moolenaarf740b292006-02-16 22:11:02 +00001804 win_T *wp;
1805 tabpage_T *tp, *nexttp;
1806 int h = tabpageline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
1808 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001809
1810 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001812 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001814 win_close(wp, FALSE);
1815
1816 /* Start all over, autocommands may change the window layout. */
1817 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 }
1819 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00001820 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001822
1823 /* Also check windows in other tab pages. */
1824 for (tp = first_tabpage; tp != NULL; tp = nexttp)
1825 {
1826 nexttp = tp->tp_next;
1827 if (tp->tp_topframe != topframe)
1828 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
1829 if (wp->w_buffer == buf)
1830 {
1831 win_close_othertab(wp, FALSE, tp);
1832
1833 /* Start all over, the tab page may be closed and
1834 * autocommands may change the window layout. */
1835 nexttp = first_tabpage;
1836 break;
1837 }
1838 }
1839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001841
1842 if (h != tabpageline_height())
1843 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844}
1845
1846/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001847 * Return TRUE if the current window is the only window that exists.
1848 * Returns FALSE if there is a window in another tab page.
1849 */
1850 int
1851last_window()
1852{
1853 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
1854}
1855
1856/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00001857 * Close window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 * If "free_buf" is TRUE related buffer may be unloaded.
1859 *
1860 * called by :quit, :close, :xit, :wq and findtag()
1861 */
1862 void
1863win_close(win, free_buf)
1864 win_T *win;
1865 int free_buf;
1866{
1867 win_T *wp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001868 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869#ifdef FEAT_AUTOCMD
1870 int other_buffer = FALSE;
1871#endif
1872 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 int dir;
1874 int help_window = FALSE;
1875
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001876 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
1878 EMSG(_("E444: Cannot close last window"));
1879 return;
1880 }
1881
1882 /* When closing the help window, try restoring a snapshot after closing
1883 * the window. Otherwise clear the snapshot, it's now invalid. */
1884 if (win->w_buffer->b_help)
1885 help_window = TRUE;
1886 else
1887 clear_snapshot();
1888
1889#ifdef FEAT_AUTOCMD
1890 if (win == curwin)
1891 {
1892 /*
1893 * Guess which window is going to be the new current window.
1894 * This may change because of the autocommands (sigh).
1895 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001896 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897
1898 /*
1899 * Be careful: If autocommands delete the window, return now.
1900 */
1901 if (wp->w_buffer != curbuf)
1902 {
1903 other_buffer = TRUE;
1904 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001905 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 return;
1907 }
1908 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001909 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 return;
1911# ifdef FEAT_EVAL
1912 /* autocmds may abort script processing */
1913 if (aborting())
1914 return;
1915# endif
1916 }
1917#endif
1918
1919 /*
1920 * Close the link to the buffer.
1921 */
1922 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
1923 /* Autocommands may have closed the window already, or closed the only
1924 * other window. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001925 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 return;
1927
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001928 /* Free the memory used for the window. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001929 wp = win_free_mem(win, &dir, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001931 /* When closing the last window in a tab page go to another tab page. */
1932 if (wp == NULL)
1933 {
1934 tabpage_T *ptp = NULL;
1935 tabpage_T *tp;
1936 tabpage_T *atp = alt_tabpage();
1937
1938 for (tp = first_tabpage; tp->tp_topframe != topframe; tp = tp->tp_next)
1939 ptp = tp;
1940 if (tp == NULL)
1941 {
1942 EMSG2(_(e_intern2), "win_close()");
1943 return;
1944 }
1945 if (ptp == NULL)
1946 first_tabpage = tp->tp_next;
1947 else
1948 ptp->tp_next = tp->tp_next;
1949 vim_free(tp);
1950
1951 /* We don't do the window resizing stuff, let enter_tabpage() take
1952 * care of entering a window in another tab page. */
1953 enter_tabpage(atp, old_curbuf);
1954 return;
1955 }
1956
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 /* Make sure curwin isn't invalid. It can cause severe trouble when
1958 * printing an error message. For win_equal() curbuf needs to be valid
1959 * too. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001960 else if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 {
1962 curwin = wp;
1963#ifdef FEAT_QUICKFIX
1964 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
1965 {
1966 /*
1967 * The cursor goes to the preview or the quickfix window, try
1968 * finding another window to go to.
1969 */
1970 for (;;)
1971 {
1972 if (wp->w_next == NULL)
1973 wp = firstwin;
1974 else
1975 wp = wp->w_next;
1976 if (wp == curwin)
1977 break;
1978 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
1979 {
1980 curwin = wp;
1981 break;
1982 }
1983 }
1984 }
1985#endif
1986 curbuf = curwin->w_buffer;
1987 close_curwin = TRUE;
1988 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001989 if (p_ea
1990#ifdef FEAT_VERTSPLIT
1991 && (*p_ead == 'b' || *p_ead == dir)
1992#endif
1993 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 win_equal(curwin, TRUE,
1995#ifdef FEAT_VERTSPLIT
1996 dir
1997#else
1998 0
1999#endif
2000 );
2001 else
2002 win_comp_pos();
2003 if (close_curwin)
2004 {
2005 win_enter_ext(wp, FALSE, TRUE);
2006#ifdef FEAT_AUTOCMD
2007 if (other_buffer)
2008 /* careful: after this wp and win may be invalid! */
2009 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2010#endif
2011 }
2012
2013 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002014 * If last window has a status line now and we don't want one,
2015 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 */
2017 last_status(FALSE);
2018
2019 /* After closing the help window, try restoring the window layout from
2020 * before it was opened. */
2021 if (help_window)
2022 restore_snapshot(close_curwin);
2023
2024#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2025 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2026 if (gui.in_use && !win_hasvertsplit())
2027 gui_init_which_components(NULL);
2028#endif
2029
2030 redraw_all_later(NOT_VALID);
2031}
2032
2033/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002034 * Close window "win" in tab page "tp", which is not the current tab page.
2035 * This may be the last window ih that tab page and result in closing the tab,
2036 * thus "tp" may become invalid!
2037 * Called must check if buffer is hidden.
2038 */
2039 void
2040win_close_othertab(win, free_buf, tp)
2041 win_T *win;
2042 int free_buf;
2043 tabpage_T *tp;
2044{
2045 win_T *wp;
2046 int dir;
2047 tabpage_T *ptp = NULL;
2048
2049 /* Close the link to the buffer. */
2050 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2051
2052 /* Careful: Autocommands may have closed the tab page or made it the
2053 * current tab page. */
2054 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2055 ;
2056 if (ptp == NULL || tp->tp_topframe == topframe)
2057 return;
2058
2059 /* Autocommands may have closed the window already. */
2060 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2061 ;
2062 if (wp == NULL)
2063 return;
2064
2065 /* Free the memory used for the window. */
2066 wp = win_free_mem(win, &dir, tp);
2067
2068 /* When closing the last window in a tab page remove the tab page. */
2069 if (wp == NULL)
2070 {
2071 if (tp == first_tabpage)
2072 first_tabpage = tp->tp_next;
2073 else
2074 {
2075 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2076 ptp = ptp->tp_next)
2077 ;
2078 if (ptp == NULL)
2079 {
2080 EMSG2(_(e_intern2), "win_close_othertab()");
2081 return;
2082 }
2083 ptp->tp_next = tp->tp_next;
2084 }
2085 vim_free(tp);
2086 }
2087}
2088
2089/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002090 * Free the memory used for a window.
2091 * Returns a pointer to the window that got the freed up space.
2092 */
2093 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002094win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002095 win_T *win;
2096 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002097 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002098{
2099 frame_T *frp;
2100 win_T *wp;
2101
Bram Moolenaarea408852005-06-25 22:49:46 +00002102#ifdef FEAT_FOLDING
2103 clearFolding(win);
2104#endif
2105
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002106 /* reduce the reference count to the argument list. */
2107 alist_unlink(win->w_alist);
2108
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002109 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002110 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002111 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002112 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002113 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002114
2115 return wp;
2116}
2117
2118#if defined(EXITFREE) || defined(PROTO)
2119 void
2120win_free_all()
2121{
2122 int dummy;
2123
Bram Moolenaarf740b292006-02-16 22:11:02 +00002124# ifdef FEAT_WINDOWS
2125 while (first_tabpage->tp_next != NULL)
2126 tabpage_close(TRUE);
2127# endif
2128
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002129 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002130 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002131}
2132#endif
2133
2134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 * Remove a window and its frame from the tree of frames.
2136 * Returns a pointer to the window that got the freed up space.
2137 */
2138/*ARGSUSED*/
2139 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002140winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 win_T *win;
2142 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002143 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144{
2145 frame_T *frp, *frp2, *frp3;
2146 frame_T *frp_close = win->w_frame;
2147 win_T *wp;
2148 int old_height = 0;
2149
2150 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002151 * If there is only one window there is nothing to remove.
2152 */
2153 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2154 return NULL;
2155
2156 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 * Remove the window from its frame.
2158 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002159 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 wp = frame2win(frp2);
2161
2162 /* Remove this frame from the list of frames. */
2163 frame_remove(frp_close);
2164
2165#ifdef FEAT_VERTSPLIT
2166 if (frp_close->fr_parent->fr_layout == FR_COL)
2167 {
2168#endif
2169 /* When 'winfixheight' is set, remember its old size and restore
2170 * it later (it's a simplistic solution...). Don't do this if the
2171 * window will occupy the full height of the screen. */
2172 if (frp2->fr_win != NULL
2173 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2174 && frp2->fr_win->w_p_wfh)
2175 old_height = frp2->fr_win->w_height;
2176 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2177 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2178 if (old_height != 0)
2179 win_setheight_win(old_height, frp2->fr_win);
2180#ifdef FEAT_VERTSPLIT
2181 *dirp = 'v';
2182 }
2183 else
2184 {
2185 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2186 frp2 == frp_close->fr_next ? TRUE : FALSE);
2187 *dirp = 'h';
2188 }
2189#endif
2190
2191 /* If rows/columns go to a window below/right its positions need to be
2192 * updated. Can only be done after the sizes have been updated. */
2193 if (frp2 == frp_close->fr_next)
2194 {
2195 int row = win->w_winrow;
2196 int col = W_WINCOL(win);
2197
2198 frame_comp_pos(frp2, &row, &col);
2199 }
2200
2201 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2202 {
2203 /* There is no other frame in this list, move its info to the parent
2204 * and remove it. */
2205 frp2->fr_parent->fr_layout = frp2->fr_layout;
2206 frp2->fr_parent->fr_child = frp2->fr_child;
2207 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2208 frp->fr_parent = frp2->fr_parent;
2209 frp2->fr_parent->fr_win = frp2->fr_win;
2210 if (frp2->fr_win != NULL)
2211 frp2->fr_win->w_frame = frp2->fr_parent;
2212 frp = frp2->fr_parent;
2213 vim_free(frp2);
2214
2215 frp2 = frp->fr_parent;
2216 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2217 {
2218 /* The frame above the parent has the same layout, have to merge
2219 * the frames into this list. */
2220 if (frp2->fr_child == frp)
2221 frp2->fr_child = frp->fr_child;
2222 frp->fr_child->fr_prev = frp->fr_prev;
2223 if (frp->fr_prev != NULL)
2224 frp->fr_prev->fr_next = frp->fr_child;
2225 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2226 {
2227 frp3->fr_parent = frp2;
2228 if (frp3->fr_next == NULL)
2229 {
2230 frp3->fr_next = frp->fr_next;
2231 if (frp->fr_next != NULL)
2232 frp->fr_next->fr_prev = frp3;
2233 break;
2234 }
2235 }
2236 vim_free(frp);
2237 }
2238 }
2239
2240 return wp;
2241}
2242
2243/*
2244 * Find out which frame is going to get the freed up space when "win" is
2245 * closed.
2246 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2247 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2248 * This makes opening a window and closing it immediately keep the same window
2249 * layout.
2250 */
2251 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002252win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002254 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255{
2256 frame_T *frp;
2257 int b;
2258
Bram Moolenaarf740b292006-02-16 22:11:02 +00002259 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002260 /* Last window in this tab page, will go to next tab page. */
2261 return alt_tabpage()->tp_curwin->w_frame;
2262
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 frp = win->w_frame;
2264#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002265 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 b = p_spr;
2267 else
2268#endif
2269 b = p_sb;
2270 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2271 return frp->fr_next;
2272 return frp->fr_prev;
2273}
2274
2275/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002276 * Return the tabpage that will be used if the current one is closed.
2277 */
2278 static tabpage_T *
2279alt_tabpage()
2280{
2281 tabpage_T *tp = current_tabpage();
2282
2283 if (tp != NULL)
2284 {
2285 /* Use the next tab page if it exists. */
2286 if (tp->tp_next != NULL)
2287 return tp->tp_next;
2288
2289 /* Find the previous tab page. */
2290 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
2291 if (tp->tp_next == current_tabpage())
2292 return tp;
2293 }
2294 return first_tabpage;
2295}
2296
2297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 * Find the left-upper window in frame "frp".
2299 */
2300 static win_T *
2301frame2win(frp)
2302 frame_T *frp;
2303{
2304 while (frp->fr_win == NULL)
2305 frp = frp->fr_child;
2306 return frp->fr_win;
2307}
2308
2309/*
2310 * Return TRUE if frame "frp" contains window "wp".
2311 */
2312 static int
2313frame_has_win(frp, wp)
2314 frame_T *frp;
2315 win_T *wp;
2316{
2317 frame_T *p;
2318
2319 if (frp->fr_layout == FR_LEAF)
2320 return frp->fr_win == wp;
2321
2322 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2323 if (frame_has_win(p, wp))
2324 return TRUE;
2325 return FALSE;
2326}
2327
2328/*
2329 * Set a new height for a frame. Recursively sets the height for contained
2330 * frames and windows. Caller must take care of positions.
2331 */
2332 static void
2333frame_new_height(topfrp, height, topfirst, wfh)
2334 frame_T *topfrp;
2335 int height;
2336 int topfirst; /* resize topmost contained frame first */
2337 int wfh; /* obey 'winfixheight' when there is a choice;
2338 may cause the height not to be set */
2339{
2340 frame_T *frp;
2341 int extra_lines;
2342 int h;
2343
2344 if (topfrp->fr_win != NULL)
2345 {
2346 /* Simple case: just one window. */
2347 win_new_height(topfrp->fr_win,
2348 height - topfrp->fr_win->w_status_height);
2349 }
2350#ifdef FEAT_VERTSPLIT
2351 else if (topfrp->fr_layout == FR_ROW)
2352 {
2353 do
2354 {
2355 /* All frames in this row get the same new height. */
2356 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2357 {
2358 frame_new_height(frp, height, topfirst, wfh);
2359 if (frp->fr_height > height)
2360 {
2361 /* Could not fit the windows, make the whole row higher. */
2362 height = frp->fr_height;
2363 break;
2364 }
2365 }
2366 }
2367 while (frp != NULL);
2368 }
2369#endif
2370 else
2371 {
2372 /* Complicated case: Resize a column of frames. Resize the bottom
2373 * frame first, frames above that when needed. */
2374
2375 frp = topfrp->fr_child;
2376 if (wfh)
2377 /* Advance past frames with one window with 'wfh' set. */
2378 while (frame_fixed_height(frp))
2379 {
2380 frp = frp->fr_next;
2381 if (frp == NULL)
2382 return; /* no frame without 'wfh', give up */
2383 }
2384 if (!topfirst)
2385 {
2386 /* Find the bottom frame of this column */
2387 while (frp->fr_next != NULL)
2388 frp = frp->fr_next;
2389 if (wfh)
2390 /* Advance back for frames with one window with 'wfh' set. */
2391 while (frame_fixed_height(frp))
2392 frp = frp->fr_prev;
2393 }
2394
2395 extra_lines = height - topfrp->fr_height;
2396 if (extra_lines < 0)
2397 {
2398 /* reduce height of contained frames, bottom or top frame first */
2399 while (frp != NULL)
2400 {
2401 h = frame_minheight(frp, NULL);
2402 if (frp->fr_height + extra_lines < h)
2403 {
2404 extra_lines += frp->fr_height - h;
2405 frame_new_height(frp, h, topfirst, wfh);
2406 }
2407 else
2408 {
2409 frame_new_height(frp, frp->fr_height + extra_lines,
2410 topfirst, wfh);
2411 break;
2412 }
2413 if (topfirst)
2414 {
2415 do
2416 frp = frp->fr_next;
2417 while (wfh && frp != NULL && frame_fixed_height(frp));
2418 }
2419 else
2420 {
2421 do
2422 frp = frp->fr_prev;
2423 while (wfh && frp != NULL && frame_fixed_height(frp));
2424 }
2425 /* Increase "height" if we could not reduce enough frames. */
2426 if (frp == NULL)
2427 height -= extra_lines;
2428 }
2429 }
2430 else if (extra_lines > 0)
2431 {
2432 /* increase height of bottom or top frame */
2433 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2434 }
2435 }
2436 topfrp->fr_height = height;
2437}
2438
2439/*
2440 * Return TRUE if height of frame "frp" should not be changed because of
2441 * the 'winfixheight' option.
2442 */
2443 static int
2444frame_fixed_height(frp)
2445 frame_T *frp;
2446{
2447 /* frame with one window: fixed height if 'winfixheight' set. */
2448 if (frp->fr_win != NULL)
2449 return frp->fr_win->w_p_wfh;
2450
2451 if (frp->fr_layout == FR_ROW)
2452 {
2453 /* The frame is fixed height if one of the frames in the row is fixed
2454 * height. */
2455 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2456 if (frame_fixed_height(frp))
2457 return TRUE;
2458 return FALSE;
2459 }
2460
2461 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2462 * frames in the row are fixed height. */
2463 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2464 if (!frame_fixed_height(frp))
2465 return FALSE;
2466 return TRUE;
2467}
2468
2469#ifdef FEAT_VERTSPLIT
2470/*
2471 * Add a status line to windows at the bottom of "frp".
2472 * Note: Does not check if there is room!
2473 */
2474 static void
2475frame_add_statusline(frp)
2476 frame_T *frp;
2477{
2478 win_T *wp;
2479
2480 if (frp->fr_layout == FR_LEAF)
2481 {
2482 wp = frp->fr_win;
2483 if (wp->w_status_height == 0)
2484 {
2485 if (wp->w_height > 0) /* don't make it negative */
2486 --wp->w_height;
2487 wp->w_status_height = STATUS_HEIGHT;
2488 }
2489 }
2490 else if (frp->fr_layout == FR_ROW)
2491 {
2492 /* Handle all the frames in the row. */
2493 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2494 frame_add_statusline(frp);
2495 }
2496 else /* frp->fr_layout == FR_COL */
2497 {
2498 /* Only need to handle the last frame in the column. */
2499 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2500 ;
2501 frame_add_statusline(frp);
2502 }
2503}
2504
2505/*
2506 * Set width of a frame. Handles recursively going through contained frames.
2507 * May remove separator line for windows at the right side (for win_close()).
2508 */
2509 static void
2510frame_new_width(topfrp, width, leftfirst)
2511 frame_T *topfrp;
2512 int width;
2513 int leftfirst; /* resize leftmost contained frame first */
2514{
2515 frame_T *frp;
2516 int extra_cols;
2517 int w;
2518 win_T *wp;
2519
2520 if (topfrp->fr_layout == FR_LEAF)
2521 {
2522 /* Simple case: just one window. */
2523 wp = topfrp->fr_win;
2524 /* Find out if there are any windows right of this one. */
2525 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2526 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2527 break;
2528 if (frp->fr_parent == NULL)
2529 wp->w_vsep_width = 0;
2530 win_new_width(wp, width - wp->w_vsep_width);
2531 }
2532 else if (topfrp->fr_layout == FR_COL)
2533 {
2534 /* All frames in this column get the same new width. */
2535 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2536 frame_new_width(frp, width, leftfirst);
2537 }
2538 else /* fr_layout == FR_ROW */
2539 {
2540 /* Complicated case: Resize a row of frames. Resize the rightmost
2541 * frame first, frames left of it when needed. */
2542
2543 /* Find the rightmost frame of this row */
2544 frp = topfrp->fr_child;
2545 if (!leftfirst)
2546 while (frp->fr_next != NULL)
2547 frp = frp->fr_next;
2548
2549 extra_cols = width - topfrp->fr_width;
2550 if (extra_cols < 0)
2551 {
2552 /* reduce frame width, rightmost frame first */
2553 while (frp != NULL)
2554 {
2555 w = frame_minwidth(frp, NULL);
2556 if (frp->fr_width + extra_cols < w)
2557 {
2558 extra_cols += frp->fr_width - w;
2559 frame_new_width(frp, w, leftfirst);
2560 }
2561 else
2562 {
2563 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst);
2564 break;
2565 }
2566 if (leftfirst)
2567 frp = frp->fr_next;
2568 else
2569 frp = frp->fr_prev;
2570 }
2571 }
2572 else if (extra_cols > 0)
2573 {
2574 /* increase width of rightmost frame */
2575 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst);
2576 }
2577 }
2578 topfrp->fr_width = width;
2579}
2580
2581/*
2582 * Add the vertical separator to windows at the right side of "frp".
2583 * Note: Does not check if there is room!
2584 */
2585 static void
2586frame_add_vsep(frp)
2587 frame_T *frp;
2588{
2589 win_T *wp;
2590
2591 if (frp->fr_layout == FR_LEAF)
2592 {
2593 wp = frp->fr_win;
2594 if (wp->w_vsep_width == 0)
2595 {
2596 if (wp->w_width > 0) /* don't make it negative */
2597 --wp->w_width;
2598 wp->w_vsep_width = 1;
2599 }
2600 }
2601 else if (frp->fr_layout == FR_COL)
2602 {
2603 /* Handle all the frames in the column. */
2604 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2605 frame_add_vsep(frp);
2606 }
2607 else /* frp->fr_layout == FR_ROW */
2608 {
2609 /* Only need to handle the last frame in the row. */
2610 frp = frp->fr_child;
2611 while (frp->fr_next != NULL)
2612 frp = frp->fr_next;
2613 frame_add_vsep(frp);
2614 }
2615}
2616
2617/*
2618 * Set frame width from the window it contains.
2619 */
2620 static void
2621frame_fix_width(wp)
2622 win_T *wp;
2623{
2624 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2625}
2626#endif
2627
2628/*
2629 * Set frame height from the window it contains.
2630 */
2631 static void
2632frame_fix_height(wp)
2633 win_T *wp;
2634{
2635 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2636}
2637
2638/*
2639 * Compute the minimal height for frame "topfrp".
2640 * Uses the 'winminheight' option.
2641 * When "next_curwin" isn't NULL, use p_wh for this window.
2642 * When "next_curwin" is NOWIN, don't use at least one line for the current
2643 * window.
2644 */
2645 static int
2646frame_minheight(topfrp, next_curwin)
2647 frame_T *topfrp;
2648 win_T *next_curwin;
2649{
2650 frame_T *frp;
2651 int m;
2652#ifdef FEAT_VERTSPLIT
2653 int n;
2654#endif
2655
2656 if (topfrp->fr_win != NULL)
2657 {
2658 if (topfrp->fr_win == next_curwin)
2659 m = p_wh + topfrp->fr_win->w_status_height;
2660 else
2661 {
2662 /* window: minimal height of the window plus status line */
2663 m = p_wmh + topfrp->fr_win->w_status_height;
2664 /* Current window is minimal one line high */
2665 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2666 ++m;
2667 }
2668 }
2669#ifdef FEAT_VERTSPLIT
2670 else if (topfrp->fr_layout == FR_ROW)
2671 {
2672 /* get the minimal height from each frame in this row */
2673 m = 0;
2674 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2675 {
2676 n = frame_minheight(frp, next_curwin);
2677 if (n > m)
2678 m = n;
2679 }
2680 }
2681#endif
2682 else
2683 {
2684 /* Add up the minimal heights for all frames in this column. */
2685 m = 0;
2686 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2687 m += frame_minheight(frp, next_curwin);
2688 }
2689
2690 return m;
2691}
2692
2693#ifdef FEAT_VERTSPLIT
2694/*
2695 * Compute the minimal width for frame "topfrp".
2696 * When "next_curwin" isn't NULL, use p_wiw for this window.
2697 * When "next_curwin" is NOWIN, don't use at least one column for the current
2698 * window.
2699 */
2700 static int
2701frame_minwidth(topfrp, next_curwin)
2702 frame_T *topfrp;
2703 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2704{
2705 frame_T *frp;
2706 int m, n;
2707
2708 if (topfrp->fr_win != NULL)
2709 {
2710 if (topfrp->fr_win == next_curwin)
2711 m = p_wiw + topfrp->fr_win->w_vsep_width;
2712 else
2713 {
2714 /* window: minimal width of the window plus separator column */
2715 m = p_wmw + topfrp->fr_win->w_vsep_width;
2716 /* Current window is minimal one column wide */
2717 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2718 ++m;
2719 }
2720 }
2721 else if (topfrp->fr_layout == FR_COL)
2722 {
2723 /* get the minimal width from each frame in this column */
2724 m = 0;
2725 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2726 {
2727 n = frame_minwidth(frp, next_curwin);
2728 if (n > m)
2729 m = n;
2730 }
2731 }
2732 else
2733 {
2734 /* Add up the minimal widths for all frames in this row. */
2735 m = 0;
2736 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2737 m += frame_minwidth(frp, next_curwin);
2738 }
2739
2740 return m;
2741}
2742#endif
2743
2744
2745/*
2746 * Try to close all windows except current one.
2747 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2748 * used and the buffer was modified.
2749 *
2750 * Used by ":bdel" and ":only".
2751 */
2752 void
2753close_others(message, forceit)
2754 int message;
2755 int forceit; /* always hide all other windows */
2756{
2757 win_T *wp;
2758 win_T *nextwp;
2759 int r;
2760
2761 if (lastwin == firstwin)
2762 {
2763 if (message
2764#ifdef FEAT_AUTOCMD
2765 && !autocmd_busy
2766#endif
2767 )
2768 MSG(_("Already only one window"));
2769 return;
2770 }
2771
2772 /* Be very careful here: autocommands may change the window layout. */
2773 for (wp = firstwin; win_valid(wp); wp = nextwp)
2774 {
2775 nextwp = wp->w_next;
2776 if (wp != curwin) /* don't close current window */
2777 {
2778
2779 /* Check if it's allowed to abandon this window */
2780 r = can_abandon(wp->w_buffer, forceit);
2781#ifdef FEAT_AUTOCMD
2782 if (!win_valid(wp)) /* autocommands messed wp up */
2783 {
2784 nextwp = firstwin;
2785 continue;
2786 }
2787#endif
2788 if (!r)
2789 {
2790#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2791 if (message && (p_confirm || cmdmod.confirm) && p_write)
2792 {
2793 dialog_changed(wp->w_buffer, FALSE);
2794# ifdef FEAT_AUTOCMD
2795 if (!win_valid(wp)) /* autocommands messed wp up */
2796 {
2797 nextwp = firstwin;
2798 continue;
2799 }
2800# endif
2801 }
2802 if (bufIsChanged(wp->w_buffer))
2803#endif
2804 continue;
2805 }
2806 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
2807 }
2808 }
2809
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002810 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 EMSG(_("E445: Other window contains changes"));
2812}
2813
2814#endif /* FEAT_WINDOWS */
2815
2816/*
2817 * init the cursor in the window
2818 *
2819 * called when a new file is being edited
2820 */
2821 void
2822win_init(wp)
2823 win_T *wp;
2824{
2825 redraw_win_later(wp, NOT_VALID);
2826 wp->w_lines_valid = 0;
2827 wp->w_cursor.lnum = 1;
2828 wp->w_curswant = wp->w_cursor.col = 0;
2829#ifdef FEAT_VIRTUALEDIT
2830 wp->w_cursor.coladd = 0;
2831#endif
2832 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
2833 wp->w_pcmark.col = 0;
2834 wp->w_prev_pcmark.lnum = 0;
2835 wp->w_prev_pcmark.col = 0;
2836 wp->w_topline = 1;
2837#ifdef FEAT_DIFF
2838 wp->w_topfill = 0;
2839#endif
2840 wp->w_botline = 2;
2841#ifdef FEAT_FKMAP
2842 if (curwin->w_p_rl)
2843 wp->w_farsi = W_CONV + W_R_L;
2844 else
2845 wp->w_farsi = W_CONV;
2846#endif
2847}
2848
2849/*
2850 * Allocate the first window and put an empty buffer in it.
2851 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002852 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002854 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855win_alloc_first()
2856{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002857 if (win_alloc_firstwin() == FAIL)
2858 return FAIL;
2859
2860#ifdef FEAT_WINDOWS
2861 first_tabpage = (tabpage_T *)alloc((unsigned)sizeof(tabpage_T));
2862 if (first_tabpage == NULL)
2863 return FAIL;
2864 first_tabpage->tp_topframe = topframe;
2865 first_tabpage->tp_next = NULL;
2866#endif
2867 return OK;
2868}
2869
2870/*
2871 * Allocate one window and put an empty buffer in it.
2872 * Called to create the first window in a new tab page.
2873 * Return FAIL when something goes wrong (out of memory).
2874 */
2875 static int
2876win_alloc_firstwin()
2877{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 curwin = win_alloc(NULL);
2879 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
2880 if (curwin == NULL || curbuf == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002881 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 curwin->w_buffer = curbuf;
2883 curbuf->b_nwindows = 1; /* there is one window */
2884#ifdef FEAT_WINDOWS
2885 curwin->w_alist = &global_alist;
2886#endif
2887 win_init(curwin); /* init current window */
2888
2889 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
2890 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002891 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 topframe->fr_layout = FR_LEAF;
2893#ifdef FEAT_VERTSPLIT
2894 topframe->fr_width = Columns;
2895#endif
2896 topframe->fr_height = Rows - p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002897#ifdef FEAT_WINDOWS
2898 p_ch_used = p_ch;
2899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 topframe->fr_win = curwin;
2901 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002902
2903 return OK;
2904}
2905
2906/*
2907 * Initialize the window and frame size to the maximum.
2908 */
2909 void
2910win_init_size()
2911{
2912 firstwin->w_height = ROWS_AVAIL;
2913 topframe->fr_height = ROWS_AVAIL;
2914#ifdef FEAT_VERTSPLIT
2915 firstwin->w_width = Columns;
2916 topframe->fr_width = Columns;
2917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918}
2919
2920#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002921/*
2922 * Create a new Tab page with one empty window.
2923 * Put it just after the current Tab page.
2924 * Return FAIL or OK.
2925 */
2926 int
2927win_new_tabpage()
2928{
2929 tabpage_T *tp;
2930 tabpage_T *newtp;
2931
2932 newtp = (tabpage_T *)alloc((unsigned)sizeof(tabpage_T));
2933 if (newtp == NULL)
2934 return FAIL;
2935
2936 tp = current_tabpage();
2937
2938 /* Remember the current windows in this Tab page. */
2939 leave_tabpage(tp);
2940
2941 /* Create a new empty window. */
2942 if (win_alloc_firstwin() == OK)
2943 {
2944 /* copy options from previous to new curwin */
2945 win_copy_options(tp->tp_curwin, curwin);
2946
2947 /* Make the new Tab page the new topframe. */
2948 newtp->tp_next = tp->tp_next;
2949 tp->tp_next = newtp;
2950 win_init_size();
2951 firstwin->w_winrow = tabpageline_height();
2952
2953 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002954 last_status(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002955 redraw_all_later(CLEAR);
2956 return OK;
2957 }
2958
2959 /* Failed, get back the previous Tab page */
2960 topframe = tp->tp_topframe;
2961 curwin = tp->tp_curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002962 prevwin = tp->tp_prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002963 firstwin = tp->tp_firstwin;
2964 lastwin = tp->tp_lastwin;
2965 return FAIL;
2966}
2967
2968/*
2969 * Return a pointer to the current tab page.
2970 */
2971 static tabpage_T *
2972current_tabpage()
2973{
2974 tabpage_T *tp;
2975
2976 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
2977 if (tp->tp_topframe == topframe)
2978 break;
2979 if (tp == NULL)
2980 EMSG2(_(e_intern2), "current_tabpage()");
2981 return tp;
2982}
2983
2984/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002985 * Return TRUE when "tpc" points to a valid tab page.
2986 */
2987 int
2988valid_tabpage(tpc)
2989 tabpage_T *tpc;
2990{
2991 tabpage_T *tp;
2992
2993 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
2994 if (tp == tpc)
2995 return TRUE;
2996 return FALSE;
2997}
2998
2999/*
3000 * Find tab page "n" (first one is 1). Returns NULL when not found.
3001 */
3002 tabpage_T *
3003find_tabpage(n)
3004 int n;
3005{
3006 tabpage_T *tp;
3007 int i = 1;
3008
3009 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3010 ++i;
3011 return tp;
3012}
3013
3014/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003015 * Prepare for leaving the current tab page "tp".
3016 */
3017 static void
3018leave_tabpage(tp)
3019 tabpage_T *tp;
3020{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003021#if defined(FEAT_GUI)
3022 /* Remove the scrollbars. They may be added back later. */
3023 if (gui.in_use)
3024 gui_remove_scrollbars();
3025#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003026 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003027 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003028 tp->tp_firstwin = firstwin;
3029 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003030 tp->tp_old_Rows = Rows;
3031 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003032 firstwin = NULL;
3033 lastwin = NULL;
3034}
3035
3036/*
3037 * Start using tab page "tp".
3038 */
3039/*ARGSUSED*/
3040 static void
3041enter_tabpage(tp, old_curbuf)
3042 tabpage_T *tp;
3043 buf_T *old_curbuf;
3044{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003045 int old_off = tp->tp_firstwin->w_winrow;
3046
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003047 firstwin = tp->tp_firstwin;
3048 lastwin = tp->tp_lastwin;
3049 topframe = tp->tp_topframe;
3050 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003051 prevwin = tp->tp_prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003052
3053#ifdef FEAT_AUTOCMD
3054 if (old_curbuf != curbuf)
3055 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3056#endif
3057
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003058 last_status(FALSE); /* status line may appear or disappear */
3059 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003060
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003061 /* The tabpage line may have appeared or disappeared, may need to resize
3062 * the frames for that. When the Vim window was resized need to update
3063 * frame sizes too. */
3064 if (tp->tp_old_Rows != Rows || old_off != firstwin->w_winrow)
3065 shell_new_rows();
3066#ifdef FEAT_VERTSPLIT
3067 if (tp->tp_old_Columns != Columns)
3068 shell_new_columns(); /* update window widths */
3069#endif
3070
3071#if defined(FEAT_GUI)
3072 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3073 * scrollbars. Have to update them anyway. */
3074 if (gui.in_use)
3075 {
3076 out_flush();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003077 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003078 gui_update_scrollbars(TRUE);
3079 }
3080 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003081#endif
3082
3083 redraw_all_later(CLEAR);
3084}
3085
3086/*
3087 * Go to tab page "n". For ":tab N" and "Ngt".
3088 */
3089 void
3090goto_tabpage(n)
3091 int n;
3092{
3093 tabpage_T *otp = current_tabpage();
3094 tabpage_T *tp;
3095 int i;
3096
3097 if (otp == NULL)
3098 return;
3099
3100 if (n == 0)
3101 {
3102 /* No count, go to next tab page, wrap around end. */
3103 if (otp->tp_next == NULL)
3104 tp = first_tabpage;
3105 else
3106 tp = otp->tp_next;
3107 }
3108 else
3109 {
3110 /* Go to tab page "n". */
3111 i = 0;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003112 for (tp = first_tabpage; ++i != n && tp != NULL; tp = tp->tp_next)
3113 ;
3114 if (tp == NULL)
3115 {
3116 beep_flush();
3117 return;
3118 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003119 }
3120
3121 leave_tabpage(otp);
3122 enter_tabpage(tp, curbuf);
3123}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124
3125/*
3126 * Go to another window.
3127 * When jumping to another buffer, stop Visual mode. Do this before
3128 * changing windows so we can yank the selection into the '*' register.
3129 * When jumping to another window on the same buffer, adjust its cursor
3130 * position to keep the same Visual area.
3131 */
3132 void
3133win_goto(wp)
3134 win_T *wp;
3135{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003136 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 {
3138 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003139 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 return;
3141 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003142
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143#ifdef FEAT_VISUAL
3144 if (wp->w_buffer != curbuf)
3145 reset_VIsual_and_resel();
3146 else if (VIsual_active)
3147 wp->w_cursor = curwin->w_cursor;
3148#endif
3149
3150#ifdef FEAT_GUI
3151 need_mouse_correct = TRUE;
3152#endif
3153 win_enter(wp, TRUE);
3154}
3155
3156#if defined(FEAT_PERL) || defined(PROTO)
3157/*
3158 * Find window number "winnr" (counting top to bottom).
3159 */
3160 win_T *
3161win_find_nr(winnr)
3162 int winnr;
3163{
3164 win_T *wp;
3165
3166# ifdef FEAT_WINDOWS
3167 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3168 if (--winnr == 0)
3169 break;
3170 return wp;
3171# else
3172 return curwin;
3173# endif
3174}
3175#endif
3176
3177#ifdef FEAT_VERTSPLIT
3178/*
3179 * Move to window above or below "count" times.
3180 */
3181 static void
3182win_goto_ver(up, count)
3183 int up; /* TRUE to go to win above */
3184 long count;
3185{
3186 frame_T *fr;
3187 frame_T *nfr;
3188 frame_T *foundfr;
3189
3190 foundfr = curwin->w_frame;
3191 while (count--)
3192 {
3193 /*
3194 * First go upwards in the tree of frames until we find a upwards or
3195 * downwards neighbor.
3196 */
3197 fr = foundfr;
3198 for (;;)
3199 {
3200 if (fr == topframe)
3201 goto end;
3202 if (up)
3203 nfr = fr->fr_prev;
3204 else
3205 nfr = fr->fr_next;
3206 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3207 break;
3208 fr = fr->fr_parent;
3209 }
3210
3211 /*
3212 * Now go downwards to find the bottom or top frame in it.
3213 */
3214 for (;;)
3215 {
3216 if (nfr->fr_layout == FR_LEAF)
3217 {
3218 foundfr = nfr;
3219 break;
3220 }
3221 fr = nfr->fr_child;
3222 if (nfr->fr_layout == FR_ROW)
3223 {
3224 /* Find the frame at the cursor row. */
3225 while (fr->fr_next != NULL
3226 && frame2win(fr)->w_wincol + fr->fr_width
3227 <= curwin->w_wincol + curwin->w_wcol)
3228 fr = fr->fr_next;
3229 }
3230 if (nfr->fr_layout == FR_COL && up)
3231 while (fr->fr_next != NULL)
3232 fr = fr->fr_next;
3233 nfr = fr;
3234 }
3235 }
3236end:
3237 if (foundfr != NULL)
3238 win_goto(foundfr->fr_win);
3239}
3240
3241/*
3242 * Move to left or right window.
3243 */
3244 static void
3245win_goto_hor(left, count)
3246 int left; /* TRUE to go to left win */
3247 long count;
3248{
3249 frame_T *fr;
3250 frame_T *nfr;
3251 frame_T *foundfr;
3252
3253 foundfr = curwin->w_frame;
3254 while (count--)
3255 {
3256 /*
3257 * First go upwards in the tree of frames until we find a left or
3258 * right neighbor.
3259 */
3260 fr = foundfr;
3261 for (;;)
3262 {
3263 if (fr == topframe)
3264 goto end;
3265 if (left)
3266 nfr = fr->fr_prev;
3267 else
3268 nfr = fr->fr_next;
3269 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3270 break;
3271 fr = fr->fr_parent;
3272 }
3273
3274 /*
3275 * Now go downwards to find the leftmost or rightmost frame in it.
3276 */
3277 for (;;)
3278 {
3279 if (nfr->fr_layout == FR_LEAF)
3280 {
3281 foundfr = nfr;
3282 break;
3283 }
3284 fr = nfr->fr_child;
3285 if (nfr->fr_layout == FR_COL)
3286 {
3287 /* Find the frame at the cursor row. */
3288 while (fr->fr_next != NULL
3289 && frame2win(fr)->w_winrow + fr->fr_height
3290 <= curwin->w_winrow + curwin->w_wrow)
3291 fr = fr->fr_next;
3292 }
3293 if (nfr->fr_layout == FR_ROW && left)
3294 while (fr->fr_next != NULL)
3295 fr = fr->fr_next;
3296 nfr = fr;
3297 }
3298 }
3299end:
3300 if (foundfr != NULL)
3301 win_goto(foundfr->fr_win);
3302}
3303#endif
3304
3305/*
3306 * Make window "wp" the current window.
3307 */
3308 void
3309win_enter(wp, undo_sync)
3310 win_T *wp;
3311 int undo_sync;
3312{
3313 win_enter_ext(wp, undo_sync, FALSE);
3314}
3315
3316/*
3317 * Make window wp the current window.
3318 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3319 * been closed and isn't valid.
3320 */
3321 static void
3322win_enter_ext(wp, undo_sync, curwin_invalid)
3323 win_T *wp;
3324 int undo_sync;
3325 int curwin_invalid;
3326{
3327#ifdef FEAT_AUTOCMD
3328 int other_buffer = FALSE;
3329#endif
3330
3331 if (wp == curwin && !curwin_invalid) /* nothing to do */
3332 return;
3333
3334#ifdef FEAT_AUTOCMD
3335 if (!curwin_invalid)
3336 {
3337 /*
3338 * Be careful: If autocommands delete the window, return now.
3339 */
3340 if (wp->w_buffer != curbuf)
3341 {
3342 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3343 other_buffer = TRUE;
3344 if (!win_valid(wp))
3345 return;
3346 }
3347 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3348 if (!win_valid(wp))
3349 return;
3350# ifdef FEAT_EVAL
3351 /* autocmds may abort script processing */
3352 if (aborting())
3353 return;
3354# endif
3355 }
3356#endif
3357
3358 /* sync undo before leaving the current buffer */
3359 if (undo_sync && curbuf != wp->w_buffer)
3360 u_sync();
3361 /* may have to copy the buffer options when 'cpo' contains 'S' */
3362 if (wp->w_buffer != curbuf)
3363 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3364 if (!curwin_invalid)
3365 {
3366 prevwin = curwin; /* remember for CTRL-W p */
3367 curwin->w_redr_status = TRUE;
3368 }
3369 curwin = wp;
3370 curbuf = wp->w_buffer;
3371 check_cursor();
3372#ifdef FEAT_VIRTUALEDIT
3373 if (!virtual_active())
3374 curwin->w_cursor.coladd = 0;
3375#endif
3376 changed_line_abv_curs(); /* assume cursor position needs updating */
3377
3378 if (curwin->w_localdir != NULL)
3379 {
3380 /* Window has a local directory: Save current directory as global
3381 * directory (unless that was done already) and change to the local
3382 * directory. */
3383 if (globaldir == NULL)
3384 {
3385 char_u cwd[MAXPATHL];
3386
3387 if (mch_dirname(cwd, MAXPATHL) == OK)
3388 globaldir = vim_strsave(cwd);
3389 }
3390 mch_chdir((char *)curwin->w_localdir);
3391 shorten_fnames(TRUE);
3392 }
3393 else if (globaldir != NULL)
3394 {
3395 /* Window doesn't have a local directory and we are not in the global
3396 * directory: Change to the global directory. */
3397 mch_chdir((char *)globaldir);
3398 vim_free(globaldir);
3399 globaldir = NULL;
3400 shorten_fnames(TRUE);
3401 }
3402
3403#ifdef FEAT_AUTOCMD
3404 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3405 if (other_buffer)
3406 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3407#endif
3408
3409#ifdef FEAT_TITLE
3410 maketitle();
3411#endif
3412 curwin->w_redr_status = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003413 redraw_tabpage = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (restart_edit)
3415 redraw_later(VALID); /* causes status line redraw */
3416
3417 /* set window height to desired minimal value */
3418 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3419 win_setheight((int)p_wh);
3420 else if (curwin->w_height == 0)
3421 win_setheight(1);
3422
3423#ifdef FEAT_VERTSPLIT
3424 /* set window width to desired minimal value */
3425 if (curwin->w_width < p_wiw)
3426 win_setwidth((int)p_wiw);
3427#endif
3428
3429#ifdef FEAT_MOUSE
3430 setmouse(); /* in case jumped to/from help buffer */
3431#endif
3432
3433#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)
3434 /* Change directories when the acd option is set on and after
3435 * switching windows. */
3436 if (p_acd && curbuf->b_ffname != NULL
3437 && vim_chdirfile(curbuf->b_ffname) == OK)
3438 shorten_fnames(TRUE);
3439#endif
3440}
3441
3442#endif /* FEAT_WINDOWS */
3443
3444#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3445/*
3446 * Jump to the first open window that contains buffer buf if one exists
3447 * TODO: Alternatively jump to last open window? Dependent from 'splitbelow'?
3448 * Returns pointer to window if it exists, otherwise NULL.
3449 */
3450 win_T *
3451buf_jump_open_win(buf)
3452 buf_T *buf;
3453{
3454# ifdef FEAT_WINDOWS
3455 win_T *wp;
3456
3457 for (wp = firstwin; wp; wp = wp->w_next)
3458 if (wp->w_buffer == buf)
3459 break;
3460 if (wp != NULL)
3461 win_enter(wp, FALSE);
3462 return wp;
3463# else
3464 if (curwin->w_buffer == buf)
3465 return curwin;
3466 return NULL;
3467# endif
3468}
3469#endif
3470
3471/*
3472 * allocate a window structure and link it in the window list
3473 */
3474/*ARGSUSED*/
3475 static win_T *
3476win_alloc(after)
3477 win_T *after;
3478{
3479 win_T *newwin;
3480
3481 /*
3482 * allocate window structure and linesizes arrays
3483 */
3484 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
3485 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
3486 {
3487 vim_free(newwin);
3488 newwin = NULL;
3489 }
3490
3491 if (newwin != NULL)
3492 {
3493 /*
3494 * link the window in the window list
3495 */
3496#ifdef FEAT_WINDOWS
3497 win_append(after, newwin);
3498#endif
3499#ifdef FEAT_VERTSPLIT
3500 newwin->w_wincol = 0;
3501 newwin->w_width = Columns;
3502#endif
3503
3504 /* position the display and the cursor at the top of the file. */
3505 newwin->w_topline = 1;
3506#ifdef FEAT_DIFF
3507 newwin->w_topfill = 0;
3508#endif
3509 newwin->w_botline = 2;
3510 newwin->w_cursor.lnum = 1;
3511#ifdef FEAT_SCROLLBIND
3512 newwin->w_scbind_pos = 1;
3513#endif
3514
3515 /* We won't calculate w_fraction until resizing the window */
3516 newwin->w_fraction = 0;
3517 newwin->w_prev_fraction_row = -1;
3518
3519#ifdef FEAT_GUI
3520 if (gui.in_use)
3521 {
3522 out_flush();
3523 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
3524 SBAR_LEFT, newwin);
3525 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
3526 SBAR_RIGHT, newwin);
3527 }
3528#endif
3529#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003530 /* init w: variables */
3531 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532#endif
3533#ifdef FEAT_FOLDING
3534 foldInitWin(newwin);
3535#endif
3536 }
3537 return newwin;
3538}
3539
3540#if defined(FEAT_WINDOWS) || defined(PROTO)
3541
3542/*
3543 * remove window 'wp' from the window list and free the structure
3544 */
3545 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00003546win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003548 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549{
3550 int i;
3551
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003552#ifdef FEAT_MZSCHEME
3553 mzscheme_window_free(wp);
3554#endif
3555
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556#ifdef FEAT_PERL
3557 perl_win_free(wp);
3558#endif
3559
3560#ifdef FEAT_PYTHON
3561 python_window_free(wp);
3562#endif
3563
3564#ifdef FEAT_TCL
3565 tcl_window_free(wp);
3566#endif
3567
3568#ifdef FEAT_RUBY
3569 ruby_window_free(wp);
3570#endif
3571
3572 clear_winopt(&wp->w_onebuf_opt);
3573 clear_winopt(&wp->w_allbuf_opt);
3574
3575#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003576 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577#endif
3578
3579 if (prevwin == wp)
3580 prevwin = NULL;
3581 win_free_lsize(wp);
3582
3583 for (i = 0; i < wp->w_tagstacklen; ++i)
3584 vim_free(wp->w_tagstack[i].tagname);
3585
3586 vim_free(wp->w_localdir);
3587#ifdef FEAT_SEARCH_EXTRA
3588 vim_free(wp->w_match.regprog);
3589#endif
3590#ifdef FEAT_JUMPLIST
3591 free_jumplist(wp);
3592#endif
3593
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003594#ifdef FEAT_QUICKFIX
3595 qf_free_all(wp);
3596#endif
3597
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598#ifdef FEAT_GUI
3599 if (gui.in_use)
3600 {
3601 out_flush();
3602 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
3603 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
3604 }
3605#endif /* FEAT_GUI */
3606
Bram Moolenaarf740b292006-02-16 22:11:02 +00003607 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 vim_free(wp);
3609}
3610
3611/*
3612 * Append window "wp" in the window list after window "after".
3613 */
3614 static void
3615win_append(after, wp)
3616 win_T *after, *wp;
3617{
3618 win_T *before;
3619
3620 if (after == NULL) /* after NULL is in front of the first */
3621 before = firstwin;
3622 else
3623 before = after->w_next;
3624
3625 wp->w_next = before;
3626 wp->w_prev = after;
3627 if (after == NULL)
3628 firstwin = wp;
3629 else
3630 after->w_next = wp;
3631 if (before == NULL)
3632 lastwin = wp;
3633 else
3634 before->w_prev = wp;
3635}
3636
3637/*
3638 * Remove a window from the window list.
3639 */
3640 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00003641win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003643 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644{
3645 if (wp->w_prev != NULL)
3646 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003647 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003649 else
3650 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 if (wp->w_next != NULL)
3652 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003653 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003655 else
3656 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657}
3658
3659/*
3660 * Append frame "frp" in a frame list after frame "after".
3661 */
3662 static void
3663frame_append(after, frp)
3664 frame_T *after, *frp;
3665{
3666 frp->fr_next = after->fr_next;
3667 after->fr_next = frp;
3668 if (frp->fr_next != NULL)
3669 frp->fr_next->fr_prev = frp;
3670 frp->fr_prev = after;
3671}
3672
3673/*
3674 * Insert frame "frp" in a frame list before frame "before".
3675 */
3676 static void
3677frame_insert(before, frp)
3678 frame_T *before, *frp;
3679{
3680 frp->fr_next = before;
3681 frp->fr_prev = before->fr_prev;
3682 before->fr_prev = frp;
3683 if (frp->fr_prev != NULL)
3684 frp->fr_prev->fr_next = frp;
3685 else
3686 frp->fr_parent->fr_child = frp;
3687}
3688
3689/*
3690 * Remove a frame from a frame list.
3691 */
3692 static void
3693frame_remove(frp)
3694 frame_T *frp;
3695{
3696 if (frp->fr_prev != NULL)
3697 frp->fr_prev->fr_next = frp->fr_next;
3698 else
3699 frp->fr_parent->fr_child = frp->fr_next;
3700 if (frp->fr_next != NULL)
3701 frp->fr_next->fr_prev = frp->fr_prev;
3702}
3703
3704#endif /* FEAT_WINDOWS */
3705
3706/*
3707 * Allocate w_lines[] for window "wp".
3708 * Return FAIL for failure, OK for success.
3709 */
3710 int
3711win_alloc_lines(wp)
3712 win_T *wp;
3713{
3714 wp->w_lines_valid = 0;
3715 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
3716 if (wp->w_lines == NULL)
3717 return FAIL;
3718 return OK;
3719}
3720
3721/*
3722 * free lsize arrays for a window
3723 */
3724 void
3725win_free_lsize(wp)
3726 win_T *wp;
3727{
3728 vim_free(wp->w_lines);
3729 wp->w_lines = NULL;
3730}
3731
3732/*
3733 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00003734 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 */
3736 void
3737shell_new_rows()
3738{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003739 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740
3741 if (firstwin == NULL) /* not initialized yet */
3742 return;
3743#ifdef FEAT_WINDOWS
3744 if (h < frame_minheight(topframe, NULL))
3745 h = frame_minheight(topframe, NULL);
3746 /* First try setting the heights of windows without 'winfixheight'. If
3747 * that doesn't result in the right height, forget about that option. */
3748 frame_new_height(topframe, h, FALSE, TRUE);
3749 if (topframe->fr_height != h)
3750 frame_new_height(topframe, h, FALSE, FALSE);
3751
3752 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
3753#else
3754 if (h < 1)
3755 h = 1;
3756 win_new_height(firstwin, h);
3757#endif
3758 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00003759#ifdef FEAT_WINDOWS
3760 p_ch_used = p_ch;
3761#endif
3762
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763#if 0
3764 /* Disabled: don't want making the screen smaller make a window larger. */
3765 if (p_ea)
3766 win_equal(curwin, FALSE, 'v');
3767#endif
3768}
3769
3770#if defined(FEAT_VERTSPLIT) || defined(PROTO)
3771/*
3772 * Called from win_new_shellsize() after Columns changed.
3773 */
3774 void
3775shell_new_columns()
3776{
3777 if (firstwin == NULL) /* not initialized yet */
3778 return;
3779 frame_new_width(topframe, (int)Columns, FALSE);
3780 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
3781#if 0
3782 /* Disabled: don't want making the screen smaller make a window larger. */
3783 if (p_ea)
3784 win_equal(curwin, FALSE, 'h');
3785#endif
3786}
3787#endif
3788
3789#if defined(FEAT_CMDWIN) || defined(PROTO)
3790/*
3791 * Save the size of all windows in "gap".
3792 */
3793 void
3794win_size_save(gap)
3795 garray_T *gap;
3796
3797{
3798 win_T *wp;
3799
3800 ga_init2(gap, (int)sizeof(int), 1);
3801 if (ga_grow(gap, win_count() * 2) == OK)
3802 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3803 {
3804 ((int *)gap->ga_data)[gap->ga_len++] =
3805 wp->w_width + wp->w_vsep_width;
3806 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
3807 }
3808}
3809
3810/*
3811 * Restore window sizes, but only if the number of windows is still the same.
3812 * Does not free the growarray.
3813 */
3814 void
3815win_size_restore(gap)
3816 garray_T *gap;
3817{
3818 win_T *wp;
3819 int i;
3820
3821 if (win_count() * 2 == gap->ga_len)
3822 {
3823 i = 0;
3824 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3825 {
3826 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
3827 win_setheight_win(((int *)gap->ga_data)[i++], wp);
3828 }
3829 /* recompute the window positions */
3830 (void)win_comp_pos();
3831 }
3832}
3833#endif /* FEAT_CMDWIN */
3834
3835#if defined(FEAT_WINDOWS) || defined(PROTO)
3836/*
3837 * Update the position for all windows, using the width and height of the
3838 * frames.
3839 * Returns the row just after the last window.
3840 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003841 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842win_comp_pos()
3843{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003844 int row = tabpageline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 int col = 0;
3846
3847 frame_comp_pos(topframe, &row, &col);
3848 return row;
3849}
3850
3851/*
3852 * Update the position of the windows in frame "topfrp", using the width and
3853 * height of the frames.
3854 * "*row" and "*col" are the top-left position of the frame. They are updated
3855 * to the bottom-right position plus one.
3856 */
3857 static void
3858frame_comp_pos(topfrp, row, col)
3859 frame_T *topfrp;
3860 int *row;
3861 int *col;
3862{
3863 win_T *wp;
3864 frame_T *frp;
3865#ifdef FEAT_VERTSPLIT
3866 int startcol;
3867 int startrow;
3868#endif
3869
3870 wp = topfrp->fr_win;
3871 if (wp != NULL)
3872 {
3873 if (wp->w_winrow != *row
3874#ifdef FEAT_VERTSPLIT
3875 || wp->w_wincol != *col
3876#endif
3877 )
3878 {
3879 /* position changed, redraw */
3880 wp->w_winrow = *row;
3881#ifdef FEAT_VERTSPLIT
3882 wp->w_wincol = *col;
3883#endif
3884 redraw_win_later(wp, NOT_VALID);
3885 wp->w_redr_status = TRUE;
3886 }
3887 *row += wp->w_height + wp->w_status_height;
3888#ifdef FEAT_VERTSPLIT
3889 *col += wp->w_width + wp->w_vsep_width;
3890#endif
3891 }
3892 else
3893 {
3894#ifdef FEAT_VERTSPLIT
3895 startrow = *row;
3896 startcol = *col;
3897#endif
3898 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3899 {
3900#ifdef FEAT_VERTSPLIT
3901 if (topfrp->fr_layout == FR_ROW)
3902 *row = startrow; /* all frames are at the same row */
3903 else
3904 *col = startcol; /* all frames are at the same col */
3905#endif
3906 frame_comp_pos(frp, row, col);
3907 }
3908 }
3909}
3910
3911#endif /* FEAT_WINDOWS */
3912
3913/*
3914 * Set current window height and take care of repositioning other windows to
3915 * fit around it.
3916 */
3917 void
3918win_setheight(height)
3919 int height;
3920{
3921 win_setheight_win(height, curwin);
3922}
3923
3924/*
3925 * Set the window height of window "win" and take care of repositioning other
3926 * windows to fit around it.
3927 */
3928 void
3929win_setheight_win(height, win)
3930 int height;
3931 win_T *win;
3932{
3933 int row;
3934
3935 if (win == curwin)
3936 {
3937 /* Always keep current window at least one line high, even when
3938 * 'winminheight' is zero. */
3939#ifdef FEAT_WINDOWS
3940 if (height < p_wmh)
3941 height = p_wmh;
3942#endif
3943 if (height == 0)
3944 height = 1;
3945 }
3946
3947#ifdef FEAT_WINDOWS
3948 frame_setheight(win->w_frame, height + win->w_status_height);
3949
3950 /* recompute the window positions */
3951 row = win_comp_pos();
3952#else
3953 if (height > topframe->fr_height)
3954 height = topframe->fr_height;
3955 win->w_height = height;
3956 row = height;
3957#endif
3958
3959 /*
3960 * If there is extra space created between the last window and the command
3961 * line, clear it.
3962 */
3963 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
3964 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
3965 cmdline_row = row;
3966 msg_row = row;
3967 msg_col = 0;
3968
3969 redraw_all_later(NOT_VALID);
3970}
3971
3972#if defined(FEAT_WINDOWS) || defined(PROTO)
3973
3974/*
3975 * Set the height of a frame to "height" and take care that all frames and
3976 * windows inside it are resized. Also resize frames on the left and right if
3977 * the are in the same FR_ROW frame.
3978 *
3979 * Strategy:
3980 * If the frame is part of a FR_COL frame, try fitting the frame in that
3981 * frame. If that doesn't work (the FR_COL frame is too small), recursively
3982 * go to containing frames to resize them and make room.
3983 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
3984 * Check for the minimal height of the FR_ROW frame.
3985 * At the top level we can also use change the command line height.
3986 */
3987 static void
3988frame_setheight(curfrp, height)
3989 frame_T *curfrp;
3990 int height;
3991{
3992 int room; /* total number of lines available */
3993 int take; /* number of lines taken from other windows */
3994 int room_cmdline; /* lines available from cmdline */
3995 int run;
3996 frame_T *frp;
3997 int h;
3998 int room_reserved;
3999
4000 /* If the height already is the desired value, nothing to do. */
4001 if (curfrp->fr_height == height)
4002 return;
4003
4004 if (curfrp->fr_parent == NULL)
4005 {
4006 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004007 if (height > ROWS_AVAIL)
4008 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 if (height > 0)
4010 frame_new_height(curfrp, height, FALSE, FALSE);
4011 }
4012 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4013 {
4014 /* Row of frames: Also need to resize frames left and right of this
4015 * one. First check for the minimal height of these. */
4016 h = frame_minheight(curfrp->fr_parent, NULL);
4017 if (height < h)
4018 height = h;
4019 frame_setheight(curfrp->fr_parent, height);
4020 }
4021 else
4022 {
4023 /*
4024 * Column of frames: try to change only frames in this column.
4025 */
4026#ifdef FEAT_VERTSPLIT
4027 /*
4028 * Do this twice:
4029 * 1: compute room available, if it's not enough try resizing the
4030 * containing frame.
4031 * 2: compute the room available and adjust the height to it.
4032 * Try not to reduce the height of a window with 'winfixheight' set.
4033 */
4034 for (run = 1; run <= 2; ++run)
4035#else
4036 for (;;)
4037#endif
4038 {
4039 room = 0;
4040 room_reserved = 0;
4041 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4042 frp = frp->fr_next)
4043 {
4044 if (frp != curfrp
4045 && frp->fr_win != NULL
4046 && frp->fr_win->w_p_wfh)
4047 room_reserved += frp->fr_height;
4048 room += frp->fr_height;
4049 if (frp != curfrp)
4050 room -= frame_minheight(frp, NULL);
4051 }
4052#ifdef FEAT_VERTSPLIT
4053 if (curfrp->fr_width != Columns)
4054 room_cmdline = 0;
4055 else
4056#endif
4057 {
4058 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4059 + lastwin->w_height + lastwin->w_status_height);
4060 if (room_cmdline < 0)
4061 room_cmdline = 0;
4062 }
4063
4064 if (height <= room + room_cmdline)
4065 break;
4066#ifdef FEAT_VERTSPLIT
4067 if (run == 2 || curfrp->fr_width == Columns)
4068#endif
4069 {
4070 if (height > room + room_cmdline)
4071 height = room + room_cmdline;
4072 break;
4073 }
4074#ifdef FEAT_VERTSPLIT
4075 frame_setheight(curfrp->fr_parent, height
4076 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4077#endif
4078 /*NOTREACHED*/
4079 }
4080
4081 /*
4082 * Compute the number of lines we will take from others frames (can be
4083 * negative!).
4084 */
4085 take = height - curfrp->fr_height;
4086
4087 /* If there is not enough room, also reduce the height of a window
4088 * with 'winfixheight' set. */
4089 if (height > room + room_cmdline - room_reserved)
4090 room_reserved = room + room_cmdline - height;
4091 /* If there is only a 'winfixheight' window and making the
4092 * window smaller, need to make the other window taller. */
4093 if (take < 0 && room - curfrp->fr_height < room_reserved)
4094 room_reserved = 0;
4095
4096 if (take > 0 && room_cmdline > 0)
4097 {
4098 /* use lines from cmdline first */
4099 if (take < room_cmdline)
4100 room_cmdline = take;
4101 take -= room_cmdline;
4102 topframe->fr_height += room_cmdline;
4103 }
4104
4105 /*
4106 * set the current frame to the new height
4107 */
4108 frame_new_height(curfrp, height, FALSE, FALSE);
4109
4110 /*
4111 * First take lines from the frames after the current frame. If
4112 * that is not enough, takes lines from frames above the current
4113 * frame.
4114 */
4115 for (run = 0; run < 2; ++run)
4116 {
4117 if (run == 0)
4118 frp = curfrp->fr_next; /* 1st run: start with next window */
4119 else
4120 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4121 while (frp != NULL && take != 0)
4122 {
4123 h = frame_minheight(frp, NULL);
4124 if (room_reserved > 0
4125 && frp->fr_win != NULL
4126 && frp->fr_win->w_p_wfh)
4127 {
4128 if (room_reserved >= frp->fr_height)
4129 room_reserved -= frp->fr_height;
4130 else
4131 {
4132 if (frp->fr_height - room_reserved > take)
4133 room_reserved = frp->fr_height - take;
4134 take -= frp->fr_height - room_reserved;
4135 frame_new_height(frp, room_reserved, FALSE, FALSE);
4136 room_reserved = 0;
4137 }
4138 }
4139 else
4140 {
4141 if (frp->fr_height - take < h)
4142 {
4143 take -= frp->fr_height - h;
4144 frame_new_height(frp, h, FALSE, FALSE);
4145 }
4146 else
4147 {
4148 frame_new_height(frp, frp->fr_height - take,
4149 FALSE, FALSE);
4150 take = 0;
4151 }
4152 }
4153 if (run == 0)
4154 frp = frp->fr_next;
4155 else
4156 frp = frp->fr_prev;
4157 }
4158 }
4159 }
4160}
4161
4162#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4163/*
4164 * Set current window width and take care of repositioning other windows to
4165 * fit around it.
4166 */
4167 void
4168win_setwidth(width)
4169 int width;
4170{
4171 win_setwidth_win(width, curwin);
4172}
4173
4174 void
4175win_setwidth_win(width, wp)
4176 int width;
4177 win_T *wp;
4178{
4179 /* Always keep current window at least one column wide, even when
4180 * 'winminwidth' is zero. */
4181 if (wp == curwin)
4182 {
4183 if (width < p_wmw)
4184 width = p_wmw;
4185 if (width == 0)
4186 width = 1;
4187 }
4188
4189 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4190
4191 /* recompute the window positions */
4192 (void)win_comp_pos();
4193
4194 redraw_all_later(NOT_VALID);
4195}
4196
4197/*
4198 * Set the width of a frame to "width" and take care that all frames and
4199 * windows inside it are resized. Also resize frames above and below if the
4200 * are in the same FR_ROW frame.
4201 *
4202 * Strategy is similar to frame_setheight().
4203 */
4204 static void
4205frame_setwidth(curfrp, width)
4206 frame_T *curfrp;
4207 int width;
4208{
4209 int room; /* total number of lines available */
4210 int take; /* number of lines taken from other windows */
4211 int run;
4212 frame_T *frp;
4213 int w;
4214
4215 /* If the width already is the desired value, nothing to do. */
4216 if (curfrp->fr_width == width)
4217 return;
4218
4219 if (curfrp->fr_parent == NULL)
4220 /* topframe: can't change width */
4221 return;
4222
4223 if (curfrp->fr_parent->fr_layout == FR_COL)
4224 {
4225 /* Column of frames: Also need to resize frames above and below of
4226 * this one. First check for the minimal width of these. */
4227 w = frame_minwidth(curfrp->fr_parent, NULL);
4228 if (width < w)
4229 width = w;
4230 frame_setwidth(curfrp->fr_parent, width);
4231 }
4232 else
4233 {
4234 /*
4235 * Row of frames: try to change only frames in this row.
4236 *
4237 * Do this twice:
4238 * 1: compute room available, if it's not enough try resizing the
4239 * containing frame.
4240 * 2: compute the room available and adjust the width to it.
4241 */
4242 for (run = 1; run <= 2; ++run)
4243 {
4244 room = 0;
4245 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4246 frp = frp->fr_next)
4247 {
4248 room += frp->fr_width;
4249 if (frp != curfrp)
4250 room -= frame_minwidth(frp, NULL);
4251 }
4252
4253 if (width <= room)
4254 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004255 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 {
4257 if (width > room)
4258 width = room;
4259 break;
4260 }
4261 frame_setwidth(curfrp->fr_parent, width
4262 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4263 }
4264
4265
4266 /*
4267 * Compute the number of lines we will take from others frames (can be
4268 * negative!).
4269 */
4270 take = width - curfrp->fr_width;
4271
4272 /*
4273 * set the current frame to the new width
4274 */
4275 frame_new_width(curfrp, width, FALSE);
4276
4277 /*
4278 * First take lines from the frames right of the current frame. If
4279 * that is not enough, takes lines from frames left of the current
4280 * frame.
4281 */
4282 for (run = 0; run < 2; ++run)
4283 {
4284 if (run == 0)
4285 frp = curfrp->fr_next; /* 1st run: start with next window */
4286 else
4287 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4288 while (frp != NULL && take != 0)
4289 {
4290 w = frame_minwidth(frp, NULL);
4291 if (frp->fr_width - take < w)
4292 {
4293 take -= frp->fr_width - w;
4294 frame_new_width(frp, w, FALSE);
4295 }
4296 else
4297 {
4298 frame_new_width(frp, frp->fr_width - take, FALSE);
4299 take = 0;
4300 }
4301 if (run == 0)
4302 frp = frp->fr_next;
4303 else
4304 frp = frp->fr_prev;
4305 }
4306 }
4307 }
4308}
4309#endif /* FEAT_VERTSPLIT */
4310
4311/*
4312 * Check 'winminheight' for a valid value.
4313 */
4314 void
4315win_setminheight()
4316{
4317 int room;
4318 int first = TRUE;
4319 win_T *wp;
4320
4321 /* loop until there is a 'winminheight' that is possible */
4322 while (p_wmh > 0)
4323 {
4324 /* TODO: handle vertical splits */
4325 room = -p_wh;
4326 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4327 room += wp->w_height - p_wmh;
4328 if (room >= 0)
4329 break;
4330 --p_wmh;
4331 if (first)
4332 {
4333 EMSG(_(e_noroom));
4334 first = FALSE;
4335 }
4336 }
4337}
4338
4339#ifdef FEAT_MOUSE
4340
4341/*
4342 * Status line of dragwin is dragged "offset" lines down (negative is up).
4343 */
4344 void
4345win_drag_status_line(dragwin, offset)
4346 win_T *dragwin;
4347 int offset;
4348{
4349 frame_T *curfr;
4350 frame_T *fr;
4351 int room;
4352 int row;
4353 int up; /* if TRUE, drag status line up, otherwise down */
4354 int n;
4355
4356 fr = dragwin->w_frame;
4357 curfr = fr;
4358 if (fr != topframe) /* more than one window */
4359 {
4360 fr = fr->fr_parent;
4361 /* When the parent frame is not a column of frames, its parent should
4362 * be. */
4363 if (fr->fr_layout != FR_COL)
4364 {
4365 curfr = fr;
4366 if (fr != topframe) /* only a row of windows, may drag statusline */
4367 fr = fr->fr_parent;
4368 }
4369 }
4370
4371 /* If this is the last frame in a column, may want to resize the parent
4372 * frame instead (go two up to skip a row of frames). */
4373 while (curfr != topframe && curfr->fr_next == NULL)
4374 {
4375 if (fr != topframe)
4376 fr = fr->fr_parent;
4377 curfr = fr;
4378 if (fr != topframe)
4379 fr = fr->fr_parent;
4380 }
4381
4382 if (offset < 0) /* drag up */
4383 {
4384 up = TRUE;
4385 offset = -offset;
4386 /* sum up the room of the current frame and above it */
4387 if (fr == curfr)
4388 {
4389 /* only one window */
4390 room = fr->fr_height - frame_minheight(fr, NULL);
4391 }
4392 else
4393 {
4394 room = 0;
4395 for (fr = fr->fr_child; ; fr = fr->fr_next)
4396 {
4397 room += fr->fr_height - frame_minheight(fr, NULL);
4398 if (fr == curfr)
4399 break;
4400 }
4401 }
4402 fr = curfr->fr_next; /* put fr at frame that grows */
4403 }
4404 else /* drag down */
4405 {
4406 up = FALSE;
4407 /*
4408 * Only dragging the last status line can reduce p_ch.
4409 */
4410 room = Rows - cmdline_row;
4411 if (curfr->fr_next == NULL)
4412 room -= 1;
4413 else
4414 room -= p_ch;
4415 if (room < 0)
4416 room = 0;
4417 /* sum up the room of frames below of the current one */
4418 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4419 room += fr->fr_height - frame_minheight(fr, NULL);
4420 fr = curfr; /* put fr at window that grows */
4421 }
4422
4423 if (room < offset) /* Not enough room */
4424 offset = room; /* Move as far as we can */
4425 if (offset <= 0)
4426 return;
4427
4428 /*
4429 * Grow frame fr by "offset" lines.
4430 * Doesn't happen when dragging the last status line up.
4431 */
4432 if (fr != NULL)
4433 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
4434
4435 if (up)
4436 fr = curfr; /* current frame gets smaller */
4437 else
4438 fr = curfr->fr_next; /* next frame gets smaller */
4439
4440 /*
4441 * Now make the other frames smaller.
4442 */
4443 while (fr != NULL && offset > 0)
4444 {
4445 n = frame_minheight(fr, NULL);
4446 if (fr->fr_height - offset <= n)
4447 {
4448 offset -= fr->fr_height - n;
4449 frame_new_height(fr, n, !up, FALSE);
4450 }
4451 else
4452 {
4453 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
4454 break;
4455 }
4456 if (up)
4457 fr = fr->fr_prev;
4458 else
4459 fr = fr->fr_next;
4460 }
4461 row = win_comp_pos();
4462 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4463 cmdline_row = row;
4464 p_ch = Rows - cmdline_row;
4465 if (p_ch < 1)
4466 p_ch = 1;
4467 redraw_all_later(NOT_VALID);
4468 showmode();
4469}
4470
4471#ifdef FEAT_VERTSPLIT
4472/*
4473 * Separator line of dragwin is dragged "offset" lines right (negative is left).
4474 */
4475 void
4476win_drag_vsep_line(dragwin, offset)
4477 win_T *dragwin;
4478 int offset;
4479{
4480 frame_T *curfr;
4481 frame_T *fr;
4482 int room;
4483 int left; /* if TRUE, drag separator line left, otherwise right */
4484 int n;
4485
4486 fr = dragwin->w_frame;
4487 if (fr == topframe) /* only one window (cannot happe?) */
4488 return;
4489 curfr = fr;
4490 fr = fr->fr_parent;
4491 /* When the parent frame is not a row of frames, its parent should be. */
4492 if (fr->fr_layout != FR_ROW)
4493 {
4494 if (fr == topframe) /* only a column of windows (cannot happen?) */
4495 return;
4496 curfr = fr;
4497 fr = fr->fr_parent;
4498 }
4499
4500 /* If this is the last frame in a row, may want to resize a parent
4501 * frame instead. */
4502 while (curfr->fr_next == NULL)
4503 {
4504 if (fr == topframe)
4505 break;
4506 curfr = fr;
4507 fr = fr->fr_parent;
4508 if (fr != topframe)
4509 {
4510 curfr = fr;
4511 fr = fr->fr_parent;
4512 }
4513 }
4514
4515 if (offset < 0) /* drag left */
4516 {
4517 left = TRUE;
4518 offset = -offset;
4519 /* sum up the room of the current frame and left of it */
4520 room = 0;
4521 for (fr = fr->fr_child; ; fr = fr->fr_next)
4522 {
4523 room += fr->fr_width - frame_minwidth(fr, NULL);
4524 if (fr == curfr)
4525 break;
4526 }
4527 fr = curfr->fr_next; /* put fr at frame that grows */
4528 }
4529 else /* drag right */
4530 {
4531 left = FALSE;
4532 /* sum up the room of frames right of the current one */
4533 room = 0;
4534 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4535 room += fr->fr_width - frame_minwidth(fr, NULL);
4536 fr = curfr; /* put fr at window that grows */
4537 }
4538
4539 if (room < offset) /* Not enough room */
4540 offset = room; /* Move as far as we can */
4541 if (offset <= 0) /* No room at all, quit. */
4542 return;
4543
4544 /* grow frame fr by offset lines */
4545 frame_new_width(fr, fr->fr_width + offset, left);
4546
4547 /* shrink other frames: current and at the left or at the right */
4548 if (left)
4549 fr = curfr; /* current frame gets smaller */
4550 else
4551 fr = curfr->fr_next; /* next frame gets smaller */
4552
4553 while (fr != NULL && offset > 0)
4554 {
4555 n = frame_minwidth(fr, NULL);
4556 if (fr->fr_width - offset <= n)
4557 {
4558 offset -= fr->fr_width - n;
4559 frame_new_width(fr, n, !left);
4560 }
4561 else
4562 {
4563 frame_new_width(fr, fr->fr_width - offset, !left);
4564 break;
4565 }
4566 if (left)
4567 fr = fr->fr_prev;
4568 else
4569 fr = fr->fr_next;
4570 }
4571 (void)win_comp_pos();
4572 redraw_all_later(NOT_VALID);
4573}
4574#endif /* FEAT_VERTSPLIT */
4575#endif /* FEAT_MOUSE */
4576
4577#endif /* FEAT_WINDOWS */
4578
4579/*
4580 * Set the height of a window.
4581 * This takes care of the things inside the window, not what happens to the
4582 * window position, the frame or to other windows.
4583 */
4584 static void
4585win_new_height(wp, height)
4586 win_T *wp;
4587 int height;
4588{
4589 linenr_T lnum;
Bram Moolenaar34114692005-01-02 11:28:13 +00004590 linenr_T bot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 int sline, line_size;
Bram Moolenaar34114692005-01-02 11:28:13 +00004592 int space;
4593 int did_below = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594#define FRACTION_MULT 16384L
4595
4596 /* Don't want a negative height. Happens when splitting a tiny window.
4597 * Will equalize heights soon to fix it. */
4598 if (height < 0)
4599 height = 0;
4600
4601 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
4602 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
4603 + FRACTION_MULT / 2) / (long)wp->w_height;
4604
4605 wp->w_height = height;
4606 wp->w_skipcol = 0;
4607
4608 /* Don't change w_topline when height is zero. Don't set w_topline when
4609 * 'scrollbind' is set and this isn't the current window. */
4610 if (height > 0
4611#ifdef FEAT_SCROLLBIND
4612 && (!wp->w_p_scb || wp == curwin)
4613#endif
4614 )
4615 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004616 /*
4617 * Find a value for w_topline that shows the cursor at the same
4618 * relative position in the window as before (more or less).
4619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 lnum = wp->w_cursor.lnum;
4621 if (lnum < 1) /* can happen when starting up */
4622 lnum = 1;
4623 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
4624 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
4625 sline = wp->w_wrow - line_size;
4626 if (sline < 0)
4627 {
4628 /*
4629 * Cursor line would go off top of screen if w_wrow was this high.
4630 */
4631 wp->w_wrow = line_size;
4632 }
4633 else
4634 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004635 space = height;
4636 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004638 space -= line_size;
4639 if (space > 0 && sline <= 0 && !did_below)
4640 {
4641 /* Try to use "~" lines below the text to avoid that text
4642 * is above the window while there are empty lines.
4643 * Subtract the rows below the cursor from "space" and
4644 * give the rest to "sline". */
4645 did_below = TRUE;
4646 bot = wp->w_cursor.lnum;
4647 while (space > 0)
4648 {
4649 if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
4650 space = 0;
4651 else
4652 {
4653#ifdef FEAT_FOLDING
4654 hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
4655#endif
4656 if (bot >= wp->w_buffer->b_ml.ml_line_count)
4657 break;
4658 ++bot;
4659 space -= plines_win(wp, bot, TRUE);
4660 }
4661 }
4662 if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
4663 sline += space;
4664 }
4665 if (sline <= 0)
4666 break;
4667
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668#ifdef FEAT_FOLDING
4669 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
4670 if (lnum == 1)
4671 {
4672 /* first line in buffer is folded */
4673 line_size = 1;
4674 --sline;
4675 break;
4676 }
4677#endif
4678 --lnum;
4679#ifdef FEAT_DIFF
4680 if (lnum == wp->w_topline)
4681 line_size = plines_win_nofill(wp, lnum, TRUE)
4682 + wp->w_topfill;
4683 else
4684#endif
4685 line_size = plines_win(wp, lnum, TRUE);
4686 sline -= line_size;
4687 }
Bram Moolenaar34114692005-01-02 11:28:13 +00004688
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 if (sline < 0)
4690 {
4691 /*
4692 * Line we want at top would go off top of screen. Use next
4693 * line instead.
4694 */
4695#ifdef FEAT_FOLDING
4696 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
4697#endif
4698 lnum++;
4699 wp->w_wrow -= line_size + sline;
4700 }
4701 else if (sline > 0)
4702 {
4703 /* First line of file reached, use that as topline. */
4704 lnum = 1;
4705 wp->w_wrow -= sline;
4706 }
4707 }
4708 set_topline(wp, lnum);
4709 }
4710
4711 if (wp == curwin)
4712 {
4713 if (p_so)
4714 update_topline();
4715 curs_columns(FALSE); /* validate w_wrow */
4716 }
4717 wp->w_prev_fraction_row = wp->w_wrow;
4718
4719 win_comp_scroll(wp);
4720 redraw_win_later(wp, NOT_VALID);
4721#ifdef FEAT_WINDOWS
4722 wp->w_redr_status = TRUE;
4723#endif
4724 invalidate_botline_win(wp);
4725}
4726
4727#ifdef FEAT_VERTSPLIT
4728/*
4729 * Set the width of a window.
4730 */
4731 static void
4732win_new_width(wp, width)
4733 win_T *wp;
4734 int width;
4735{
4736 wp->w_width = width;
4737 wp->w_lines_valid = 0;
4738 changed_line_abv_curs_win(wp);
4739 invalidate_botline_win(wp);
4740 if (wp == curwin)
4741 {
4742 update_topline();
4743 curs_columns(TRUE); /* validate w_wrow */
4744 }
4745 redraw_win_later(wp, NOT_VALID);
4746 wp->w_redr_status = TRUE;
4747}
4748#endif
4749
4750 void
4751win_comp_scroll(wp)
4752 win_T *wp;
4753{
4754 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
4755 if (wp->w_p_scr == 0)
4756 wp->w_p_scr = 1;
4757}
4758
4759/*
4760 * command_height: called whenever p_ch has been changed
4761 */
4762 void
4763command_height(old_p_ch)
4764 long old_p_ch;
4765{
4766#ifdef FEAT_WINDOWS
4767 int h;
4768 frame_T *frp;
4769
Bram Moolenaar05159a02005-02-26 23:04:13 +00004770 /* When passed a negative value use the value of p_ch that we remembered.
4771 * This is needed for when the GUI starts up, we can't be sure in what
4772 * order things happen. */
4773 if (old_p_ch < 0)
4774 old_p_ch = p_ch_used;
4775 p_ch_used = p_ch;
4776
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 /* Find bottom frame with width of screen. */
4778 frp = lastwin->w_frame;
4779# ifdef FEAT_VERTSPLIT
4780 while (frp->fr_width != Columns && frp->fr_parent != NULL)
4781 frp = frp->fr_parent;
4782# endif
4783
4784 /* Avoid changing the height of a window with 'winfixheight' set. */
4785 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
4786 && frp->fr_win->w_p_wfh)
4787 frp = frp->fr_prev;
4788
4789 if (starting != NO_SCREEN)
4790 {
4791 cmdline_row = Rows - p_ch;
4792
4793 if (p_ch > old_p_ch) /* p_ch got bigger */
4794 {
4795 while (p_ch > old_p_ch)
4796 {
4797 if (frp == NULL)
4798 {
4799 EMSG(_(e_noroom));
4800 p_ch = old_p_ch;
4801 cmdline_row = Rows - p_ch;
4802 break;
4803 }
4804 h = frp->fr_height - frame_minheight(frp, NULL);
4805 if (h > p_ch - old_p_ch)
4806 h = p_ch - old_p_ch;
4807 old_p_ch += h;
4808 frame_add_height(frp, -h);
4809 frp = frp->fr_prev;
4810 }
4811
4812 /* Recompute window positions. */
4813 (void)win_comp_pos();
4814
4815 /* clear the lines added to cmdline */
4816 if (full_screen)
4817 screen_fill((int)(cmdline_row), (int)Rows, 0,
4818 (int)Columns, ' ', ' ', 0);
4819 msg_row = cmdline_row;
4820 redraw_cmdline = TRUE;
4821 return;
4822 }
4823
4824 if (msg_row < cmdline_row)
4825 msg_row = cmdline_row;
4826 redraw_cmdline = TRUE;
4827 }
4828 frame_add_height(frp, (int)(old_p_ch - p_ch));
4829
4830 /* Recompute window positions. */
4831 if (frp != lastwin->w_frame)
4832 (void)win_comp_pos();
4833#else
4834 win_setheight((int)(firstwin->w_height + old_p_ch - p_ch));
4835 cmdline_row = Rows - p_ch;
4836#endif
4837}
4838
4839#if defined(FEAT_WINDOWS) || defined(PROTO)
4840/*
4841 * Resize frame "frp" to be "n" lines higher (negative for less high).
4842 * Also resize the frames it is contained in.
4843 */
4844 static void
4845frame_add_height(frp, n)
4846 frame_T *frp;
4847 int n;
4848{
4849 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
4850 for (;;)
4851 {
4852 frp = frp->fr_parent;
4853 if (frp == NULL)
4854 break;
4855 frp->fr_height += n;
4856 }
4857}
4858
4859/*
4860 * Add or remove a status line for the bottom window(s), according to the
4861 * value of 'laststatus'.
4862 */
4863 void
4864last_status(morewin)
4865 int morewin; /* pretend there are two or more windows */
4866{
4867 /* Don't make a difference between horizontal or vertical split. */
4868 last_status_rec(topframe, (p_ls == 2
4869 || (p_ls == 1 && (morewin || lastwin != firstwin))));
4870}
4871
4872 static void
4873last_status_rec(fr, statusline)
4874 frame_T *fr;
4875 int statusline;
4876{
4877 frame_T *fp;
4878 win_T *wp;
4879
4880 if (fr->fr_layout == FR_LEAF)
4881 {
4882 wp = fr->fr_win;
4883 if (wp->w_status_height != 0 && !statusline)
4884 {
4885 /* remove status line */
4886 win_new_height(wp, wp->w_height + 1);
4887 wp->w_status_height = 0;
4888 comp_col();
4889 }
4890 else if (wp->w_status_height == 0 && statusline)
4891 {
4892 /* Find a frame to take a line from. */
4893 fp = fr;
4894 while (fp->fr_height <= frame_minheight(fp, NULL))
4895 {
4896 if (fp == topframe)
4897 {
4898 EMSG(_(e_noroom));
4899 return;
4900 }
4901 /* In a column of frames: go to frame above. If already at
4902 * the top or in a row of frames: go to parent. */
4903 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
4904 fp = fp->fr_prev;
4905 else
4906 fp = fp->fr_parent;
4907 }
4908 wp->w_status_height = 1;
4909 if (fp != fr)
4910 {
4911 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
4912 frame_fix_height(wp);
4913 (void)win_comp_pos();
4914 }
4915 else
4916 win_new_height(wp, wp->w_height - 1);
4917 comp_col();
4918 redraw_all_later(NOT_VALID);
4919 }
4920 }
4921#ifdef FEAT_VERTSPLIT
4922 else if (fr->fr_layout == FR_ROW)
4923 {
4924 /* vertically split windows, set status line for each one */
4925 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
4926 last_status_rec(fp, statusline);
4927 }
4928#endif
4929 else
4930 {
4931 /* horizontally split window, set status line for last one */
4932 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
4933 ;
4934 last_status_rec(fp, statusline);
4935 }
4936}
4937
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004938/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004939 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004940 */
4941 int
4942tabpageline_height()
4943{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004944 switch (p_tal)
4945 {
4946 case 0: return 0;
4947 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
4948 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004949 return 1;
4950}
4951
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952#endif /* FEAT_WINDOWS */
4953
4954#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4955/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004956 * Get the file name at the cursor.
4957 * If Visual mode is active, use the selected text if it's in one line.
4958 * Returns the name in allocated memory, NULL for failure.
4959 */
4960 char_u *
4961grab_file_name(count)
4962 long count;
4963{
4964# ifdef FEAT_VISUAL
4965 if (VIsual_active)
4966 {
4967 int len;
4968 char_u *ptr;
4969
4970 if (get_visual_text(NULL, &ptr, &len) == FAIL)
4971 return NULL;
4972 return find_file_name_in_path(ptr, len,
4973 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
4974 }
4975# endif
4976 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count);
4977}
4978
4979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 * Return the file name under or after the cursor.
4981 *
4982 * The 'path' option is searched if the file name is not absolute.
4983 * The string returned has been alloc'ed and should be freed by the caller.
4984 * NULL is returned if the file name or file is not found.
4985 *
4986 * options:
4987 * FNAME_MESS give error messages
4988 * FNAME_EXP expand to path
4989 * FNAME_HYP check for hypertext link
4990 * FNAME_INCL apply "includeexpr"
4991 */
4992 char_u *
4993file_name_at_cursor(options, count)
4994 int options;
4995 long count;
4996{
4997 return file_name_in_line(ml_get_curline(),
4998 curwin->w_cursor.col, options, count, curbuf->b_ffname);
4999}
5000
5001/*
5002 * Return the name of the file under or after ptr[col].
5003 * Otherwise like file_name_at_cursor().
5004 */
5005 char_u *
5006file_name_in_line(line, col, options, count, rel_fname)
5007 char_u *line;
5008 int col;
5009 int options;
5010 long count;
5011 char_u *rel_fname; /* file we are searching relative to */
5012{
5013 char_u *ptr;
5014 int len;
5015
5016 /*
5017 * search forward for what could be the start of a file name
5018 */
5019 ptr = line + col;
5020 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005021 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 if (*ptr == NUL) /* nothing found */
5023 {
5024 if (options & FNAME_MESS)
5025 EMSG(_("E446: No file name under cursor"));
5026 return NULL;
5027 }
5028
5029 /*
5030 * Search backward for first char of the file name.
5031 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5032 */
5033 while (ptr > line)
5034 {
5035#ifdef FEAT_MBYTE
5036 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5037 ptr -= len + 1;
5038 else
5039#endif
5040 if (vim_isfilec(ptr[-1])
5041 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5042 --ptr;
5043 else
5044 break;
5045 }
5046
5047 /*
5048 * Search forward for the last char of the file name.
5049 * Also allow "://" when ':' is not in 'isfname'.
5050 */
5051 len = 0;
5052 while (vim_isfilec(ptr[len])
5053 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5054#ifdef FEAT_MBYTE
5055 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005056 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 else
5058#endif
5059 ++len;
5060
5061 /*
5062 * If there is trailing punctuation, remove it.
5063 * But don't remove "..", could be a directory name.
5064 */
5065 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5066 && ptr[len - 2] != '.')
5067 --len;
5068
5069 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5070}
5071
5072# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5073static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5074
5075 static char_u *
5076eval_includeexpr(ptr, len)
5077 char_u *ptr;
5078 int len;
5079{
5080 char_u *res;
5081
5082 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005083 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
5084 was_set_insecurely((char_u *)"includeexpr"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 set_vim_var_string(VV_FNAME, NULL, 0);
5086 return res;
5087}
5088#endif
5089
5090/*
5091 * Return the name of the file ptr[len] in 'path'.
5092 * Otherwise like file_name_at_cursor().
5093 */
5094 char_u *
5095find_file_name_in_path(ptr, len, options, count, rel_fname)
5096 char_u *ptr;
5097 int len;
5098 int options;
5099 long count;
5100 char_u *rel_fname; /* file we are searching relative to */
5101{
5102 char_u *file_name;
5103 int c;
5104# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5105 char_u *tofree = NULL;
5106
5107 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5108 {
5109 tofree = eval_includeexpr(ptr, len);
5110 if (tofree != NULL)
5111 {
5112 ptr = tofree;
5113 len = (int)STRLEN(ptr);
5114 }
5115 }
5116# endif
5117
5118 if (options & FNAME_EXP)
5119 {
5120 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5121 TRUE, rel_fname);
5122
5123# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5124 /*
5125 * If the file could not be found in a normal way, try applying
5126 * 'includeexpr' (unless done already).
5127 */
5128 if (file_name == NULL
5129 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5130 {
5131 tofree = eval_includeexpr(ptr, len);
5132 if (tofree != NULL)
5133 {
5134 ptr = tofree;
5135 len = (int)STRLEN(ptr);
5136 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5137 TRUE, rel_fname);
5138 }
5139 }
5140# endif
5141 if (file_name == NULL && (options & FNAME_MESS))
5142 {
5143 c = ptr[len];
5144 ptr[len] = NUL;
5145 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5146 ptr[len] = c;
5147 }
5148
5149 /* Repeat finding the file "count" times. This matters when it
5150 * appears several times in the path. */
5151 while (file_name != NULL && --count > 0)
5152 {
5153 vim_free(file_name);
5154 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5155 }
5156 }
5157 else
5158 file_name = vim_strnsave(ptr, len);
5159
5160# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5161 vim_free(tofree);
5162# endif
5163
5164 return file_name;
5165}
5166#endif /* FEAT_SEARCHPATH */
5167
5168/*
5169 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5170 * Also check for ":\\", which MS Internet Explorer accepts, return
5171 * URL_BACKSLASH.
5172 */
5173 static int
5174path_is_url(p)
5175 char_u *p;
5176{
5177 if (STRNCMP(p, "://", (size_t)3) == 0)
5178 return URL_SLASH;
5179 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5180 return URL_BACKSLASH;
5181 return 0;
5182}
5183
5184/*
5185 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5186 * Return URL_BACKSLASH for "name:\\".
5187 * Return zero otherwise.
5188 */
5189 int
5190path_with_url(fname)
5191 char_u *fname;
5192{
5193 char_u *p;
5194
5195 for (p = fname; isalpha(*p); ++p)
5196 ;
5197 return path_is_url(p);
5198}
5199
5200/*
5201 * Return TRUE if "name" is a full (absolute) path name or URL.
5202 */
5203 int
5204vim_isAbsName(name)
5205 char_u *name;
5206{
5207 return (path_with_url(name) != 0 || mch_isFullName(name));
5208}
5209
5210/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005211 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 *
5213 * return FAIL for failure, OK otherwise
5214 */
5215 int
5216vim_FullName(fname, buf, len, force)
5217 char_u *fname, *buf;
5218 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005219 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220{
5221 int retval = OK;
5222 int url;
5223
5224 *buf = NUL;
5225 if (fname == NULL)
5226 return FAIL;
5227
5228 url = path_with_url(fname);
5229 if (!url)
5230 retval = mch_FullName(fname, buf, len, force);
5231 if (url || retval == FAIL)
5232 {
5233 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005234 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 }
5236#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5237 slash_adjust(buf);
5238#endif
5239 return retval;
5240}
5241
5242/*
5243 * Return the minimal number of rows that is needed on the screen to display
5244 * the current number of windows.
5245 */
5246 int
5247min_rows()
5248{
5249 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005250#ifdef FEAT_WINDOWS
5251 tabpage_T *tp;
5252 int n;
5253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254
5255 if (firstwin == NULL) /* not initialized yet */
5256 return MIN_LINES;
5257
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005259 total = 0;
5260 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5261 {
5262 n = frame_minheight(tp->tp_topframe, NULL);
5263 if (total < n)
5264 total = n;
5265 }
5266 total += tabpageline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005268 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005270 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 return total;
5272}
5273
5274/*
5275 * Return TRUE if there is only one window, not counting a help or preview
5276 * window, unless it is the current window.
5277 */
5278 int
5279only_one_window()
5280{
5281#ifdef FEAT_WINDOWS
5282 int count = 0;
5283 win_T *wp;
5284
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005285 /* If there is another tab page there always is another window. */
5286 if (first_tabpage->tp_next != NULL)
5287 return FALSE;
5288
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005290 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291# ifdef FEAT_QUICKFIX
5292 || wp->w_p_pvw
5293# endif
5294 ) || wp == curwin)
5295 ++count;
5296 return (count <= 1);
5297#else
5298 return TRUE;
5299#endif
5300}
5301
5302#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5303/*
5304 * Correct the cursor line number in other windows. Used after changing the
5305 * current buffer, and before applying autocommands.
5306 * When "do_curwin" is TRUE, also check current window.
5307 */
5308 void
5309check_lnums(do_curwin)
5310 int do_curwin;
5311{
5312 win_T *wp;
5313
5314#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005315 tabpage_T *tp;
5316
5317 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5319#else
5320 wp = curwin;
5321 if (do_curwin)
5322#endif
5323 {
5324 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5325 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5326 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5327 wp->w_topline = curbuf->b_ml.ml_line_count;
5328 }
5329}
5330#endif
5331
5332#if defined(FEAT_WINDOWS) || defined(PROTO)
5333
5334/*
5335 * A snapshot of the window sizes, to restore them after closing the help
5336 * window.
5337 * Only these fields are used:
5338 * fr_layout
5339 * fr_width
5340 * fr_height
5341 * fr_next
5342 * fr_child
5343 * fr_win (only valid for the old curwin, NULL otherwise)
5344 */
5345static frame_T *snapshot = NULL;
5346
5347/*
5348 * Create a snapshot of the current frame sizes.
5349 */
5350 static void
5351make_snapshot()
5352{
5353 clear_snapshot();
5354 make_snapshot_rec(topframe, &snapshot);
5355}
5356
5357 static void
5358make_snapshot_rec(fr, frp)
5359 frame_T *fr;
5360 frame_T **frp;
5361{
5362 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5363 if (*frp == NULL)
5364 return;
5365 (*frp)->fr_layout = fr->fr_layout;
5366# ifdef FEAT_VERTSPLIT
5367 (*frp)->fr_width = fr->fr_width;
5368# endif
5369 (*frp)->fr_height = fr->fr_height;
5370 if (fr->fr_next != NULL)
5371 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
5372 if (fr->fr_child != NULL)
5373 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
5374 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
5375 (*frp)->fr_win = curwin;
5376}
5377
5378/*
5379 * Remove any existing snapshot.
5380 */
5381 static void
5382clear_snapshot()
5383{
5384 clear_snapshot_rec(snapshot);
5385 snapshot = NULL;
5386}
5387
5388 static void
5389clear_snapshot_rec(fr)
5390 frame_T *fr;
5391{
5392 if (fr != NULL)
5393 {
5394 clear_snapshot_rec(fr->fr_next);
5395 clear_snapshot_rec(fr->fr_child);
5396 vim_free(fr);
5397 }
5398}
5399
5400/*
5401 * Restore a previously created snapshot, if there is any.
5402 * This is only done if the screen size didn't change and the window layout is
5403 * still the same.
5404 */
5405 static void
5406restore_snapshot(close_curwin)
5407 int close_curwin; /* closing current window */
5408{
5409 win_T *wp;
5410
5411 if (snapshot != NULL
5412# ifdef FEAT_VERTSPLIT
5413 && snapshot->fr_width == topframe->fr_width
5414# endif
5415 && snapshot->fr_height == topframe->fr_height
5416 && check_snapshot_rec(snapshot, topframe) == OK)
5417 {
5418 wp = restore_snapshot_rec(snapshot, topframe);
5419 win_comp_pos();
5420 if (wp != NULL && close_curwin)
5421 win_goto(wp);
5422 redraw_all_later(CLEAR);
5423 }
5424 clear_snapshot();
5425}
5426
5427/*
5428 * Check if frames "sn" and "fr" have the same layout, same following frames
5429 * and same children.
5430 */
5431 static int
5432check_snapshot_rec(sn, fr)
5433 frame_T *sn;
5434 frame_T *fr;
5435{
5436 if (sn->fr_layout != fr->fr_layout
5437 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
5438 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
5439 || (sn->fr_next != NULL
5440 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
5441 || (sn->fr_child != NULL
5442 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
5443 return FAIL;
5444 return OK;
5445}
5446
5447/*
5448 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
5449 * following frames and children.
5450 * Returns a pointer to the old current window, or NULL.
5451 */
5452 static win_T *
5453restore_snapshot_rec(sn, fr)
5454 frame_T *sn;
5455 frame_T *fr;
5456{
5457 win_T *wp = NULL;
5458 win_T *wp2;
5459
5460 fr->fr_height = sn->fr_height;
5461# ifdef FEAT_VERTSPLIT
5462 fr->fr_width = sn->fr_width;
5463# endif
5464 if (fr->fr_layout == FR_LEAF)
5465 {
5466 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
5467# ifdef FEAT_VERTSPLIT
5468 frame_new_width(fr, fr->fr_width, FALSE);
5469# endif
5470 wp = sn->fr_win;
5471 }
5472 if (sn->fr_next != NULL)
5473 {
5474 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
5475 if (wp2 != NULL)
5476 wp = wp2;
5477 }
5478 if (sn->fr_child != NULL)
5479 {
5480 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
5481 if (wp2 != NULL)
5482 wp = wp2;
5483 }
5484 return wp;
5485}
5486
5487#endif
5488
5489#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5490/*
5491 * Return TRUE if there is any vertically split window.
5492 */
5493 int
5494win_hasvertsplit()
5495{
5496 frame_T *fr;
5497
5498 if (topframe->fr_layout == FR_ROW)
5499 return TRUE;
5500
5501 if (topframe->fr_layout == FR_COL)
5502 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
5503 if (fr->fr_layout == FR_ROW)
5504 return TRUE;
5505
5506 return FALSE;
5507}
5508#endif