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