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