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