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