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