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