blob: 2891972254063b383ae591ad7933ac2991a2c139 [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));
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000019static void win_init __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020static 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 Moolenaar49d7bf12006-02-17 21:45:41 +000029static int last_window __ARGS((void));
Bram Moolenaarf740b292006-02-16 22:11:02 +000030static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
31static win_T *winframe_remove __ARGS((win_T *win, int *dirp, tabpage_T *tp));
32static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000033static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000034static win_T *frame2win __ARGS((frame_T *frp));
35static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
36static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
37static int frame_fixed_height __ARGS((frame_T *frp));
38#ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000039static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000040static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000041static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000042static void frame_add_vsep __ARGS((frame_T *frp));
43static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
44static void frame_fix_width __ARGS((win_T *wp));
45#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000046#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000047static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000048#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000049static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000050static int leave_tabpage __ARGS((buf_T *new_curbuf));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000051static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000052static void frame_fix_height __ARGS((win_T *wp));
53static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
54static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
Bram Moolenaarf740b292006-02-16 22:11:02 +000055static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000056static void win_append __ARGS((win_T *, win_T *));
Bram Moolenaarf740b292006-02-16 22:11:02 +000057static void win_remove __ARGS((win_T *, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static void frame_append __ARGS((frame_T *after, frame_T *frp));
59static void frame_insert __ARGS((frame_T *before, frame_T *frp));
60static void frame_remove __ARGS((frame_T *frp));
61#ifdef FEAT_VERTSPLIT
62static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000063static void win_goto_ver __ARGS((int up, long count));
64static void win_goto_hor __ARGS((int left, long count));
65#endif
66static void frame_add_height __ARGS((frame_T *frp, int n));
67static void last_status_rec __ARGS((frame_T *fr, int statusline));
68
69static void make_snapshot __ARGS((void));
70static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000071static void clear_snapshot __ARGS((tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000072static void clear_snapshot_rec __ARGS((frame_T *fr));
73static void restore_snapshot __ARGS((int close_curwin));
74static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
75static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
76
77#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000078
Bram Moolenaar071d4272004-06-13 20:20:40 +000079static win_T *win_alloc __ARGS((win_T *after));
80static void win_new_height __ARGS((win_T *, int));
81
82#define URL_SLASH 1 /* path_is_url() has found "://" */
83#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
84
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000085#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086
Bram Moolenaar05159a02005-02-26 23:04:13 +000087#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000088# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000089#else
90# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000091#endif
92
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000094
95static char *m_onlyone = N_("Already only one window");
96
Bram Moolenaar071d4272004-06-13 20:20:40 +000097/*
98 * all CTRL-W window commands are handled here, called from normal_cmd().
99 */
100 void
101do_window(nchar, Prenum, xchar)
102 int nchar;
103 long Prenum;
104 int xchar; /* extra char from ":wincmd gx" or NUL */
105{
106 long Prenum1;
107 win_T *wp;
108#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
109 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000110 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111#endif
112#ifdef FEAT_FIND_ID
113 int type = FIND_DEFINE;
114 int len;
115#endif
116 char_u cbuf[40];
117
118 if (Prenum == 0)
119 Prenum1 = 1;
120 else
121 Prenum1 = Prenum;
122
123#ifdef FEAT_CMDWIN
124# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
125#else
126# define CHECK_CMDWIN
127#endif
128
129 switch (nchar)
130 {
131/* split current window in two parts, horizontally */
132 case 'S':
133 case Ctrl_S:
134 case 's':
135 CHECK_CMDWIN
136#ifdef FEAT_VISUAL
137 reset_VIsual_and_resel(); /* stop Visual mode */
138#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000139#ifdef FEAT_QUICKFIX
140 /* When splitting the quickfix window open a new buffer in it,
141 * don't replicate the quickfix buffer. */
142 if (bt_quickfix(curbuf))
143 goto newwindow;
144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#ifdef FEAT_GUI
146 need_mouse_correct = TRUE;
147#endif
148 win_split((int)Prenum, 0);
149 break;
150
151#ifdef FEAT_VERTSPLIT
152/* split current window in two parts, vertically */
153 case Ctrl_V:
154 case 'v':
155 CHECK_CMDWIN
156#ifdef FEAT_VISUAL
157 reset_VIsual_and_resel(); /* stop Visual mode */
158#endif
159#ifdef FEAT_GUI
160 need_mouse_correct = TRUE;
161#endif
162 win_split((int)Prenum, WSP_VERT);
163 break;
164#endif
165
166/* split current window and edit alternate file */
167 case Ctrl_HAT:
168 case '^':
169 CHECK_CMDWIN
170#ifdef FEAT_VISUAL
171 reset_VIsual_and_resel(); /* stop Visual mode */
172#endif
173 STRCPY(cbuf, "split #");
174 if (Prenum)
175 sprintf((char *)cbuf + 7, "%ld", Prenum);
176 do_cmdline_cmd(cbuf);
177 break;
178
179/* open new window */
180 case Ctrl_N:
181 case 'n':
182 CHECK_CMDWIN
183#ifdef FEAT_VISUAL
184 reset_VIsual_and_resel(); /* stop Visual mode */
185#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000186#ifdef FEAT_QUICKFIX
187newwindow:
188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 if (Prenum)
190 sprintf((char *)cbuf, "%ld", Prenum); /* window height */
191 else
192 cbuf[0] = NUL;
193 STRCAT(cbuf, "new");
194 do_cmdline_cmd(cbuf);
195 break;
196
197/* quit current window */
198 case Ctrl_Q:
199 case 'q':
200#ifdef FEAT_VISUAL
201 reset_VIsual_and_resel(); /* stop Visual mode */
202#endif
203 do_cmdline_cmd((char_u *)"quit");
204 break;
205
206/* close current window */
207 case Ctrl_C:
208 case 'c':
209#ifdef FEAT_VISUAL
210 reset_VIsual_and_resel(); /* stop Visual mode */
211#endif
212 do_cmdline_cmd((char_u *)"close");
213 break;
214
215#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
216/* close preview window */
217 case Ctrl_Z:
218 case 'z':
219 CHECK_CMDWIN
220#ifdef FEAT_VISUAL
221 reset_VIsual_and_resel(); /* stop Visual mode */
222#endif
223 do_cmdline_cmd((char_u *)"pclose");
224 break;
225
226/* cursor to preview window */
227 case 'P':
228 for (wp = firstwin; wp != NULL; wp = wp->w_next)
229 if (wp->w_p_pvw)
230 break;
231 if (wp == NULL)
232 EMSG(_("E441: There is no preview window"));
233 else
234 win_goto(wp);
235 break;
236#endif
237
238/* close all but current window */
239 case Ctrl_O:
240 case 'o':
241 CHECK_CMDWIN
242#ifdef FEAT_VISUAL
243 reset_VIsual_and_resel(); /* stop Visual mode */
244#endif
245 do_cmdline_cmd((char_u *)"only");
246 break;
247
248/* cursor to next window with wrap around */
249 case Ctrl_W:
250 case 'w':
251/* cursor to previous window with wrap around */
252 case 'W':
253 CHECK_CMDWIN
254 if (lastwin == firstwin && Prenum != 1) /* just one window */
255 beep_flush();
256 else
257 {
258 if (Prenum) /* go to specified window */
259 {
260 for (wp = firstwin; --Prenum > 0; )
261 {
262 if (wp->w_next == NULL)
263 break;
264 else
265 wp = wp->w_next;
266 }
267 }
268 else
269 {
270 if (nchar == 'W') /* go to previous window */
271 {
272 wp = curwin->w_prev;
273 if (wp == NULL)
274 wp = lastwin; /* wrap around */
275 }
276 else /* go to next window */
277 {
278 wp = curwin->w_next;
279 if (wp == NULL)
280 wp = firstwin; /* wrap around */
281 }
282 }
283 win_goto(wp);
284 }
285 break;
286
287/* cursor to window below */
288 case 'j':
289 case K_DOWN:
290 case Ctrl_J:
291 CHECK_CMDWIN
292#ifdef FEAT_VERTSPLIT
293 win_goto_ver(FALSE, Prenum1);
294#else
295 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
296 wp = wp->w_next)
297 ;
298 win_goto(wp);
299#endif
300 break;
301
302/* cursor to window above */
303 case 'k':
304 case K_UP:
305 case Ctrl_K:
306 CHECK_CMDWIN
307#ifdef FEAT_VERTSPLIT
308 win_goto_ver(TRUE, Prenum1);
309#else
310 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
311 wp = wp->w_prev)
312 ;
313 win_goto(wp);
314#endif
315 break;
316
317#ifdef FEAT_VERTSPLIT
318/* cursor to left window */
319 case 'h':
320 case K_LEFT:
321 case Ctrl_H:
322 case K_BS:
323 CHECK_CMDWIN
324 win_goto_hor(TRUE, Prenum1);
325 break;
326
327/* cursor to right window */
328 case 'l':
329 case K_RIGHT:
330 case Ctrl_L:
331 CHECK_CMDWIN
332 win_goto_hor(FALSE, Prenum1);
333 break;
334#endif
335
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000336/* move window to new tab page */
337 case 'T':
338 if (firstwin == lastwin)
339 MSG(_(m_onlyone));
340 else
341 {
342 tabpage_T *oldtab = curtab;
343 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000344
345 /* First create a new tab with the window, then go back to
346 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000347 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000348 if (win_new_tabpage((int)Prenum) == OK
349 && valid_tabpage(oldtab))
350 {
351 newtab = curtab;
352 goto_tabpage_tp(oldtab);
353 if (curwin == wp)
354 win_close(curwin, FALSE);
355 if (valid_tabpage(newtab))
356 goto_tabpage_tp(newtab);
357 }
358 }
359 break;
360
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361/* cursor to top-left window */
362 case 't':
363 case Ctrl_T:
364 win_goto(firstwin);
365 break;
366
367/* cursor to bottom-right window */
368 case 'b':
369 case Ctrl_B:
370 win_goto(lastwin);
371 break;
372
373/* cursor to last accessed (previous) window */
374 case 'p':
375 case Ctrl_P:
376 if (prevwin == NULL)
377 beep_flush();
378 else
379 win_goto(prevwin);
380 break;
381
382/* exchange current and next window */
383 case 'x':
384 case Ctrl_X:
385 CHECK_CMDWIN
386 win_exchange(Prenum);
387 break;
388
389/* rotate windows downwards */
390 case Ctrl_R:
391 case 'r':
392 CHECK_CMDWIN
393#ifdef FEAT_VISUAL
394 reset_VIsual_and_resel(); /* stop Visual mode */
395#endif
396 win_rotate(FALSE, (int)Prenum1); /* downwards */
397 break;
398
399/* rotate windows upwards */
400 case 'R':
401 CHECK_CMDWIN
402#ifdef FEAT_VISUAL
403 reset_VIsual_and_resel(); /* stop Visual mode */
404#endif
405 win_rotate(TRUE, (int)Prenum1); /* upwards */
406 break;
407
408/* move window to the very top/bottom/left/right */
409 case 'K':
410 case 'J':
411#ifdef FEAT_VERTSPLIT
412 case 'H':
413 case 'L':
414#endif
415 CHECK_CMDWIN
416 win_totop((int)Prenum,
417 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
418 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
419 break;
420
421/* make all windows the same height */
422 case '=':
423#ifdef FEAT_GUI
424 need_mouse_correct = TRUE;
425#endif
426 win_equal(NULL, FALSE, 'b');
427 break;
428
429/* increase current window height */
430 case '+':
431#ifdef FEAT_GUI
432 need_mouse_correct = TRUE;
433#endif
434 win_setheight(curwin->w_height + (int)Prenum1);
435 break;
436
437/* decrease current window height */
438 case '-':
439#ifdef FEAT_GUI
440 need_mouse_correct = TRUE;
441#endif
442 win_setheight(curwin->w_height - (int)Prenum1);
443 break;
444
445/* set current window height */
446 case Ctrl__:
447 case '_':
448#ifdef FEAT_GUI
449 need_mouse_correct = TRUE;
450#endif
451 win_setheight(Prenum ? (int)Prenum : 9999);
452 break;
453
454#ifdef FEAT_VERTSPLIT
455/* increase current window width */
456 case '>':
457#ifdef FEAT_GUI
458 need_mouse_correct = TRUE;
459#endif
460 win_setwidth(curwin->w_width + (int)Prenum1);
461 break;
462
463/* decrease current window width */
464 case '<':
465#ifdef FEAT_GUI
466 need_mouse_correct = TRUE;
467#endif
468 win_setwidth(curwin->w_width - (int)Prenum1);
469 break;
470
471/* set current window width */
472 case '|':
473#ifdef FEAT_GUI
474 need_mouse_correct = TRUE;
475#endif
476 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
477 break;
478#endif
479
480/* jump to tag and split window if tag exists (in preview window) */
481#if defined(FEAT_QUICKFIX)
482 case '}':
483 CHECK_CMDWIN
484 if (Prenum)
485 g_do_tagpreview = Prenum;
486 else
487 g_do_tagpreview = p_pvh;
488 /*FALLTHROUGH*/
489#endif
490 case ']':
491 case Ctrl_RSB:
492 CHECK_CMDWIN
493#ifdef FEAT_VISUAL
494 reset_VIsual_and_resel(); /* stop Visual mode */
495#endif
496 if (Prenum)
497 postponed_split = Prenum;
498 else
499 postponed_split = -1;
500
501 /* Execute the command right here, required when
502 * "wincmd ]" was used in a function. */
503 do_nv_ident(Ctrl_RSB, NUL);
504 break;
505
506#ifdef FEAT_SEARCHPATH
507/* edit file name under cursor in a new window */
508 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000509 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000511wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000513
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000514 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 if (ptr != NULL)
516 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000517# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000519# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 setpcmark();
521 if (win_split(0, 0) == OK)
522 {
523# ifdef FEAT_SCROLLBIND
524 curwin->w_p_scb = FALSE;
525# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000526 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE);
527 if (nchar == 'F' && lnum >= 0)
528 {
529 curwin->w_cursor.lnum = lnum;
530 check_cursor_lnum();
531 beginline(BL_SOL | BL_FIX);
532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 }
534 vim_free(ptr);
535 }
536 break;
537#endif
538
539#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000540/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 * new window -- webb
542 */
543 case 'i': /* Go to any match */
544 case Ctrl_I:
545 type = FIND_ANY;
546 /* FALLTHROUGH */
547 case 'd': /* Go to definition, using 'define' */
548 case Ctrl_D:
549 CHECK_CMDWIN
550 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
551 break;
552 find_pattern_in_path(ptr, 0, len, TRUE,
553 Prenum == 0 ? TRUE : FALSE, type,
554 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
555 curwin->w_set_curswant = TRUE;
556 break;
557#endif
558
Bram Moolenaar05159a02005-02-26 23:04:13 +0000559 case K_KENTER:
560 case CAR:
561#if defined(FEAT_QUICKFIX)
562 /*
563 * In a quickfix window a <CR> jumps to the error under the
564 * cursor in a new window.
565 */
566 if (bt_quickfix(curbuf))
567 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000568 sprintf((char *)cbuf, "split +%ld%s",
569 (long)curwin->w_cursor.lnum,
570 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000571 do_cmdline_cmd(cbuf);
572 }
573#endif
574 break;
575
576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577/* CTRL-W g extended commands */
578 case 'g':
579 case Ctrl_G:
580 CHECK_CMDWIN
581#ifdef USE_ON_FLY_SCROLL
582 dont_scroll = TRUE; /* disallow scrolling here */
583#endif
584 ++no_mapping;
585 ++allow_keys; /* no mapping for xchar, but allow key codes */
586 if (xchar == NUL)
587 xchar = safe_vgetc();
588#ifdef FEAT_LANGMAP
589 LANGMAP_ADJUST(xchar, TRUE);
590#endif
591 --no_mapping;
592 --allow_keys;
593#ifdef FEAT_CMDL_INFO
594 (void)add_to_showcmd(xchar);
595#endif
596 switch (xchar)
597 {
598#if defined(FEAT_QUICKFIX)
599 case '}':
600 xchar = Ctrl_RSB;
601 if (Prenum)
602 g_do_tagpreview = Prenum;
603 else
604 g_do_tagpreview = p_pvh;
605 /*FALLTHROUGH*/
606#endif
607 case ']':
608 case Ctrl_RSB:
609#ifdef FEAT_VISUAL
610 reset_VIsual_and_resel(); /* stop Visual mode */
611#endif
612 if (Prenum)
613 postponed_split = Prenum;
614 else
615 postponed_split = -1;
616
617 /* Execute the command right here, required when
618 * "wincmd g}" was used in a function. */
619 do_nv_ident('g', xchar);
620 break;
621
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000622#ifdef FEAT_SEARCHPATH
623 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000624 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000625 cmdmod.tab = TRUE;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000626 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000627 goto wingotofile;
628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 default:
630 beep_flush();
631 break;
632 }
633 break;
634
635 default: beep_flush();
636 break;
637 }
638}
639
640/*
641 * split the current window, implements CTRL-W s and :split
642 *
643 * "size" is the height or width for the new window, 0 to use half of current
644 * height or width.
645 *
646 * "flags":
647 * WSP_ROOM: require enough room for new window
648 * WSP_VERT: vertical split.
649 * WSP_TOP: open window at the top-left of the shell (help window).
650 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
651 * WSP_HELP: creating the help window, keep layout snapshot
652 *
653 * return FAIL for failure, OK otherwise
654 */
655 int
656win_split(size, flags)
657 int size;
658 int flags;
659{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000660 /* When the ":tab" modifier was used open a new tab page instead. */
661 if (may_open_tabpage() == OK)
662 return OK;
663
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 /* Add flags from ":vertical", ":topleft" and ":botright". */
665 flags |= cmdmod.split;
666 if ((flags & WSP_TOP) && (flags & WSP_BOT))
667 {
668 EMSG(_("E442: Can't split topleft and botright at the same time"));
669 return FAIL;
670 }
671
672 /* When creating the help window make a snapshot of the window layout.
673 * Otherwise clear the snapshot, it's now invalid. */
674 if (flags & WSP_HELP)
675 make_snapshot();
676 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000677 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679 return win_split_ins(size, flags, NULL, 0);
680}
681
682/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000683 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 * When "newwin" is not NULL: insert this window at the far
685 * top/left/right/bottom.
686 * return FAIL for failure, OK otherwise
687 */
688 static int
689win_split_ins(size, flags, newwin, dir)
690 int size;
691 int flags;
692 win_T *newwin;
693 int dir;
694{
695 win_T *wp = newwin;
696 win_T *oldwin;
697 int new_size = size;
698 int i;
699 int need_status = 0;
700 int do_equal = FALSE;
701 int needed;
702 int available;
703 int oldwin_height = 0;
704 int layout;
705 frame_T *frp, *curfrp;
706 int before;
707
708 if (flags & WSP_TOP)
709 oldwin = firstwin;
710 else if (flags & WSP_BOT)
711 oldwin = lastwin;
712 else
713 oldwin = curwin;
714
715 /* add a status line when p_ls == 1 and splitting the first window */
716 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
717 {
718 if (oldwin->w_height <= p_wmh && newwin == NULL)
719 {
720 EMSG(_(e_noroom));
721 return FAIL;
722 }
723 need_status = STATUS_HEIGHT;
724 }
725
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000726#ifdef FEAT_GUI
727 /* May be needed for the scrollbars that are going to change. */
728 if (gui.in_use)
729 out_flush();
730#endif
731
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732#ifdef FEAT_VERTSPLIT
733 if (flags & WSP_VERT)
734 {
735 layout = FR_ROW;
736 do_equal = (p_ea && new_size == 0 && *p_ead != 'v');
737
738 /*
739 * Check if we are able to split the current window and compute its
740 * width.
741 */
742 needed = p_wmw + 1;
743 if (flags & WSP_ROOM)
744 needed += p_wiw - p_wmw;
745 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
746 {
747 available = topframe->fr_width;
748 needed += frame_minwidth(topframe, NULL);
749 }
750 else
751 available = oldwin->w_width;
752 if (available < needed && newwin == NULL)
753 {
754 EMSG(_(e_noroom));
755 return FAIL;
756 }
757 if (new_size == 0)
758 new_size = oldwin->w_width / 2;
759 if (new_size > oldwin->w_width - p_wmw - 1)
760 new_size = oldwin->w_width - p_wmw - 1;
761 if (new_size < p_wmw)
762 new_size = p_wmw;
763
764 /* if it doesn't fit in the current window, need win_equal() */
765 if (oldwin->w_width - new_size - 1 < p_wmw)
766 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000767
768 /* We don't like to take lines for the new window from a
769 * 'winfixwidth' window. Take them from a window to the left or right
770 * instead, if possible. */
771 if (oldwin->w_p_wfw)
772 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 }
774 else
775#endif
776 {
777 layout = FR_COL;
778 do_equal = (p_ea && new_size == 0
779#ifdef FEAT_VERTSPLIT
780 && *p_ead != 'h'
781#endif
782 );
783
784 /*
785 * Check if we are able to split the current window and compute its
786 * height.
787 */
788 needed = p_wmh + STATUS_HEIGHT + need_status;
789 if (flags & WSP_ROOM)
790 needed += p_wh - p_wmh;
791 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
792 {
793 available = topframe->fr_height;
794 needed += frame_minheight(topframe, NULL);
795 }
796 else
797 {
798 available = oldwin->w_height;
799 needed += p_wmh;
800 }
801 if (available < needed && newwin == NULL)
802 {
803 EMSG(_(e_noroom));
804 return FAIL;
805 }
806 oldwin_height = oldwin->w_height;
807 if (need_status)
808 {
809 oldwin->w_status_height = STATUS_HEIGHT;
810 oldwin_height -= STATUS_HEIGHT;
811 }
812 if (new_size == 0)
813 new_size = oldwin_height / 2;
814
815 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
816 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
817 if (new_size < p_wmh)
818 new_size = p_wmh;
819
820 /* if it doesn't fit in the current window, need win_equal() */
821 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
822 do_equal = TRUE;
823
824 /* We don't like to take lines for the new window from a
825 * 'winfixheight' window. Take them from a window above or below
826 * instead, if possible. */
827 if (oldwin->w_p_wfh)
828 {
829 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
830 oldwin);
831 oldwin_height = oldwin->w_height;
832 if (need_status)
833 oldwin_height -= STATUS_HEIGHT;
834 }
835 }
836
837 /*
838 * allocate new window structure and link it in the window list
839 */
840 if ((flags & WSP_TOP) == 0
841 && ((flags & WSP_BOT)
842 || (flags & WSP_BELOW)
843 || (!(flags & WSP_ABOVE)
844 && (
845#ifdef FEAT_VERTSPLIT
846 (flags & WSP_VERT) ? p_spr :
847#endif
848 p_sb))))
849 {
850 /* new window below/right of current one */
851 if (newwin == NULL)
852 wp = win_alloc(oldwin);
853 else
854 win_append(oldwin, wp);
855 }
856 else
857 {
858 if (newwin == NULL)
859 wp = win_alloc(oldwin->w_prev);
860 else
861 win_append(oldwin->w_prev, wp);
862 }
863
864 if (newwin == NULL)
865 {
866 if (wp == NULL)
867 return FAIL;
868
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000869 /* make the contents of the new window the same as the current one */
870 win_init(wp, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 }
872
873 /*
874 * Reorganise the tree of frames to insert the new window.
875 */
876 if (flags & (WSP_TOP | WSP_BOT))
877 {
878#ifdef FEAT_VERTSPLIT
879 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
880 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
881#else
882 if (topframe->fr_layout == FR_COL)
883#endif
884 {
885 curfrp = topframe->fr_child;
886 if (flags & WSP_BOT)
887 while (curfrp->fr_next != NULL)
888 curfrp = curfrp->fr_next;
889 }
890 else
891 curfrp = topframe;
892 before = (flags & WSP_TOP);
893 }
894 else
895 {
896 curfrp = oldwin->w_frame;
897 if (flags & WSP_BELOW)
898 before = FALSE;
899 else if (flags & WSP_ABOVE)
900 before = TRUE;
901 else
902#ifdef FEAT_VERTSPLIT
903 if (flags & WSP_VERT)
904 before = !p_spr;
905 else
906#endif
907 before = !p_sb;
908 }
909 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
910 {
911 /* Need to create a new frame in the tree to make a branch. */
912 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
913 *frp = *curfrp;
914 curfrp->fr_layout = layout;
915 frp->fr_parent = curfrp;
916 frp->fr_next = NULL;
917 frp->fr_prev = NULL;
918 curfrp->fr_child = frp;
919 curfrp->fr_win = NULL;
920 curfrp = frp;
921 if (frp->fr_win != NULL)
922 oldwin->w_frame = frp;
923 else
924 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
925 frp->fr_parent = curfrp;
926 }
927
928 if (newwin == NULL)
929 {
930 /* Create a frame for the new window. */
931 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
932 frp->fr_layout = FR_LEAF;
933 frp->fr_win = wp;
934 wp->w_frame = frp;
935 }
936 else
937 frp = newwin->w_frame;
938 frp->fr_parent = curfrp->fr_parent;
939
940 /* Insert the new frame at the right place in the frame list. */
941 if (before)
942 frame_insert(curfrp, frp);
943 else
944 frame_append(curfrp, frp);
945
946#ifdef FEAT_VERTSPLIT
947 if (flags & WSP_VERT)
948 {
949 wp->w_p_scr = curwin->w_p_scr;
950 if (need_status)
951 {
952 --oldwin->w_height;
953 oldwin->w_status_height = need_status;
954 }
955 if (flags & (WSP_TOP | WSP_BOT))
956 {
957 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000958 wp->w_winrow = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 wp->w_height = curfrp->fr_height - (p_ls > 0);
960 wp->w_status_height = (p_ls > 0);
961 }
962 else
963 {
964 /* height and row of new window is same as current window */
965 wp->w_winrow = oldwin->w_winrow;
966 wp->w_height = oldwin->w_height;
967 wp->w_status_height = oldwin->w_status_height;
968 }
969 frp->fr_height = curfrp->fr_height;
970
971 /* "new_size" of the current window goes to the new window, use
972 * one column for the vertical separator */
973 wp->w_width = new_size;
974 if (before)
975 wp->w_vsep_width = 1;
976 else
977 {
978 wp->w_vsep_width = oldwin->w_vsep_width;
979 oldwin->w_vsep_width = 1;
980 }
981 if (flags & (WSP_TOP | WSP_BOT))
982 {
983 if (flags & WSP_BOT)
984 frame_add_vsep(curfrp);
985 /* Set width of neighbor frame */
986 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000987 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
988 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
990 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000991 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (before) /* new window left of current one */
993 {
994 wp->w_wincol = oldwin->w_wincol;
995 oldwin->w_wincol += new_size + 1;
996 }
997 else /* new window right of current one */
998 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
999 frame_fix_width(oldwin);
1000 frame_fix_width(wp);
1001 }
1002 else
1003#endif
1004 {
1005 /* width and column of new window is same as current window */
1006#ifdef FEAT_VERTSPLIT
1007 if (flags & (WSP_TOP | WSP_BOT))
1008 {
1009 wp->w_wincol = 0;
1010 wp->w_width = Columns;
1011 wp->w_vsep_width = 0;
1012 }
1013 else
1014 {
1015 wp->w_wincol = oldwin->w_wincol;
1016 wp->w_width = oldwin->w_width;
1017 wp->w_vsep_width = oldwin->w_vsep_width;
1018 }
1019 frp->fr_width = curfrp->fr_width;
1020#endif
1021
1022 /* "new_size" of the current window goes to the new window, use
1023 * one row for the status line */
1024 win_new_height(wp, new_size);
1025 if (flags & (WSP_TOP | WSP_BOT))
1026 frame_new_height(curfrp, curfrp->fr_height
1027 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1028 else
1029 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1030 if (before) /* new window above current one */
1031 {
1032 wp->w_winrow = oldwin->w_winrow;
1033 wp->w_status_height = STATUS_HEIGHT;
1034 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1035 }
1036 else /* new window below current one */
1037 {
1038 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1039 wp->w_status_height = oldwin->w_status_height;
1040 oldwin->w_status_height = STATUS_HEIGHT;
1041 }
1042#ifdef FEAT_VERTSPLIT
1043 if (flags & WSP_BOT)
1044 frame_add_statusline(curfrp);
1045#endif
1046 frame_fix_height(wp);
1047 frame_fix_height(oldwin);
1048 }
1049
1050 if (flags & (WSP_TOP | WSP_BOT))
1051 (void)win_comp_pos();
1052
1053 /*
1054 * Both windows need redrawing
1055 */
1056 redraw_win_later(wp, NOT_VALID);
1057 wp->w_redr_status = TRUE;
1058 redraw_win_later(oldwin, NOT_VALID);
1059 oldwin->w_redr_status = TRUE;
1060
1061 if (need_status)
1062 {
1063 msg_row = Rows - 1;
1064 msg_col = sc_col;
1065 msg_clr_eos_force(); /* Old command/ruler may still be there */
1066 comp_col();
1067 msg_row = Rows - 1;
1068 msg_col = 0; /* put position back at start of line */
1069 }
1070
1071 /*
1072 * make the new window the current window and redraw
1073 */
1074 if (do_equal || dir != 0)
1075 win_equal(wp, TRUE,
1076#ifdef FEAT_VERTSPLIT
1077 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1078 : dir == 'h' ? 'b' :
1079#endif
1080 'v');
1081
1082 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1083 * size was given. */
1084#ifdef FEAT_VERTSPLIT
1085 if (flags & WSP_VERT)
1086 {
1087 i = p_wiw;
1088 if (size != 0)
1089 p_wiw = size;
1090
1091# ifdef FEAT_GUI
1092 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1093 if (gui.in_use)
1094 gui_init_which_components(NULL);
1095# endif
1096 }
1097 else
1098#endif
1099 {
1100 i = p_wh;
1101 if (size != 0)
1102 p_wh = size;
1103 }
1104 win_enter(wp, FALSE);
1105#ifdef FEAT_VERTSPLIT
1106 if (flags & WSP_VERT)
1107 p_wiw = i;
1108 else
1109#endif
1110 p_wh = i;
1111
1112 return OK;
1113}
1114
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001115/*
1116 * Initialize window "newp" from window "oldp".
1117 * Used when splitting a window and when creating a new tab page.
1118 * The windows will both edit the same buffer.
1119 */
1120 static void
1121win_init(newp, oldp)
1122 win_T *newp;
1123 win_T *oldp;
1124{
1125 int i;
1126
1127 newp->w_buffer = oldp->w_buffer;
1128 oldp->w_buffer->b_nwindows++;
1129 newp->w_cursor = oldp->w_cursor;
1130 newp->w_valid = 0;
1131 newp->w_curswant = oldp->w_curswant;
1132 newp->w_set_curswant = oldp->w_set_curswant;
1133 newp->w_topline = oldp->w_topline;
1134#ifdef FEAT_DIFF
1135 newp->w_topfill = oldp->w_topfill;
1136#endif
1137 newp->w_leftcol = oldp->w_leftcol;
1138 newp->w_pcmark = oldp->w_pcmark;
1139 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1140 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001141 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001142 newp->w_fraction = oldp->w_fraction;
1143 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1144#ifdef FEAT_JUMPLIST
1145 copy_jumplist(oldp, newp);
1146#endif
1147#ifdef FEAT_QUICKFIX
1148 copy_loclist(oldp, newp);
1149#endif
1150 if (oldp->w_localdir != NULL)
1151 newp->w_localdir = vim_strsave(oldp->w_localdir);
1152
1153 /* Use the same argument list. */
1154 newp->w_alist = oldp->w_alist;
1155 ++newp->w_alist->al_refcount;
1156 newp->w_arg_idx = oldp->w_arg_idx;
1157
1158 /*
1159 * copy tagstack and options from existing window
1160 */
1161 for (i = 0; i < oldp->w_tagstacklen; i++)
1162 {
1163 newp->w_tagstack[i] = oldp->w_tagstack[i];
1164 if (newp->w_tagstack[i].tagname != NULL)
1165 newp->w_tagstack[i].tagname =
1166 vim_strsave(newp->w_tagstack[i].tagname);
1167 }
1168 newp->w_tagstackidx = oldp->w_tagstackidx;
1169 newp->w_tagstacklen = oldp->w_tagstacklen;
1170 win_copy_options(oldp, newp);
1171# ifdef FEAT_FOLDING
1172 copyFoldingState(oldp, newp);
1173# endif
1174}
1175
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176#endif /* FEAT_WINDOWS */
1177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178#if defined(FEAT_WINDOWS) || defined(PROTO)
1179/*
1180 * Check if "win" is a pointer to an existing window.
1181 */
1182 int
1183win_valid(win)
1184 win_T *win;
1185{
1186 win_T *wp;
1187
1188 if (win == NULL)
1189 return FALSE;
1190 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1191 if (wp == win)
1192 return TRUE;
1193 return FALSE;
1194}
1195
1196/*
1197 * Return the number of windows.
1198 */
1199 int
1200win_count()
1201{
1202 win_T *wp;
1203 int count = 0;
1204
1205 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1206 ++count;
1207 return count;
1208}
1209
1210/*
1211 * Make "count" windows on the screen.
1212 * Return actual number of windows on the screen.
1213 * Must be called when there is just one window, filling the whole screen
1214 * (excluding the command line).
1215 */
1216/*ARGSUSED*/
1217 int
1218make_windows(count, vertical)
1219 int count;
1220 int vertical; /* split windows vertically if TRUE */
1221{
1222 int maxcount;
1223 int todo;
1224
1225#ifdef FEAT_VERTSPLIT
1226 if (vertical)
1227 {
1228 /* Each windows needs at least 'winminwidth' lines and a separator
1229 * column. */
1230 maxcount = (curwin->w_width + curwin->w_vsep_width
1231 - (p_wiw - p_wmw)) / (p_wmw + 1);
1232 }
1233 else
1234#endif
1235 {
1236 /* Each window needs at least 'winminheight' lines and a status line. */
1237 maxcount = (curwin->w_height + curwin->w_status_height
1238 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1239 }
1240
1241 if (maxcount < 2)
1242 maxcount = 2;
1243 if (count > maxcount)
1244 count = maxcount;
1245
1246 /*
1247 * add status line now, otherwise first window will be too big
1248 */
1249 if (count > 1)
1250 last_status(TRUE);
1251
1252#ifdef FEAT_AUTOCMD
1253 /*
1254 * Don't execute autocommands while creating the windows. Must do that
1255 * when putting the buffers in the windows.
1256 */
1257 ++autocmd_block;
1258#endif
1259
1260 /* todo is number of windows left to create */
1261 for (todo = count - 1; todo > 0; --todo)
1262#ifdef FEAT_VERTSPLIT
1263 if (vertical)
1264 {
1265 if (win_split(curwin->w_width - (curwin->w_width - todo)
1266 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1267 break;
1268 }
1269 else
1270#endif
1271 {
1272 if (win_split(curwin->w_height - (curwin->w_height - todo
1273 * STATUS_HEIGHT) / (todo + 1)
1274 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1275 break;
1276 }
1277
1278#ifdef FEAT_AUTOCMD
1279 --autocmd_block;
1280#endif
1281
1282 /* return actual number of windows */
1283 return (count - todo);
1284}
1285
1286/*
1287 * Exchange current and next window
1288 */
1289 static void
1290win_exchange(Prenum)
1291 long Prenum;
1292{
1293 frame_T *frp;
1294 frame_T *frp2;
1295 win_T *wp;
1296 win_T *wp2;
1297 int temp;
1298
1299 if (lastwin == firstwin) /* just one window */
1300 {
1301 beep_flush();
1302 return;
1303 }
1304
1305#ifdef FEAT_GUI
1306 need_mouse_correct = TRUE;
1307#endif
1308
1309 /*
1310 * find window to exchange with
1311 */
1312 if (Prenum)
1313 {
1314 frp = curwin->w_frame->fr_parent->fr_child;
1315 while (frp != NULL && --Prenum > 0)
1316 frp = frp->fr_next;
1317 }
1318 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1319 frp = curwin->w_frame->fr_next;
1320 else /* Swap last window in row/col with previous */
1321 frp = curwin->w_frame->fr_prev;
1322
1323 /* We can only exchange a window with another window, not with a frame
1324 * containing windows. */
1325 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1326 return;
1327 wp = frp->fr_win;
1328
1329/*
1330 * 1. remove curwin from the list. Remember after which window it was in wp2
1331 * 2. insert curwin before wp in the list
1332 * if wp != wp2
1333 * 3. remove wp from the list
1334 * 4. insert wp after wp2
1335 * 5. exchange the status line height and vsep width.
1336 */
1337 wp2 = curwin->w_prev;
1338 frp2 = curwin->w_frame->fr_prev;
1339 if (wp->w_prev != curwin)
1340 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001341 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 frame_remove(curwin->w_frame);
1343 win_append(wp->w_prev, curwin);
1344 frame_insert(frp, curwin->w_frame);
1345 }
1346 if (wp != wp2)
1347 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001348 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 frame_remove(wp->w_frame);
1350 win_append(wp2, wp);
1351 if (frp2 == NULL)
1352 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1353 else
1354 frame_append(frp2, wp->w_frame);
1355 }
1356 temp = curwin->w_status_height;
1357 curwin->w_status_height = wp->w_status_height;
1358 wp->w_status_height = temp;
1359#ifdef FEAT_VERTSPLIT
1360 temp = curwin->w_vsep_width;
1361 curwin->w_vsep_width = wp->w_vsep_width;
1362 wp->w_vsep_width = temp;
1363
1364 /* If the windows are not in the same frame, exchange the sizes to avoid
1365 * messing up the window layout. Otherwise fix the frame sizes. */
1366 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1367 {
1368 temp = curwin->w_height;
1369 curwin->w_height = wp->w_height;
1370 wp->w_height = temp;
1371 temp = curwin->w_width;
1372 curwin->w_width = wp->w_width;
1373 wp->w_width = temp;
1374 }
1375 else
1376 {
1377 frame_fix_height(curwin);
1378 frame_fix_height(wp);
1379 frame_fix_width(curwin);
1380 frame_fix_width(wp);
1381 }
1382#endif
1383
1384 (void)win_comp_pos(); /* recompute window positions */
1385
1386 win_enter(wp, TRUE);
1387 redraw_later(CLEAR);
1388}
1389
1390/*
1391 * rotate windows: if upwards TRUE the second window becomes the first one
1392 * if upwards FALSE the first window becomes the second one
1393 */
1394 static void
1395win_rotate(upwards, count)
1396 int upwards;
1397 int count;
1398{
1399 win_T *wp1;
1400 win_T *wp2;
1401 frame_T *frp;
1402 int n;
1403
1404 if (firstwin == lastwin) /* nothing to do */
1405 {
1406 beep_flush();
1407 return;
1408 }
1409
1410#ifdef FEAT_GUI
1411 need_mouse_correct = TRUE;
1412#endif
1413
1414#ifdef FEAT_VERTSPLIT
1415 /* Check if all frames in this row/col have one window. */
1416 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1417 frp = frp->fr_next)
1418 if (frp->fr_win == NULL)
1419 {
1420 EMSG(_("E443: Cannot rotate when another window is split"));
1421 return;
1422 }
1423#endif
1424
1425 while (count--)
1426 {
1427 if (upwards) /* first window becomes last window */
1428 {
1429 /* remove first window/frame from the list */
1430 frp = curwin->w_frame->fr_parent->fr_child;
1431 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001432 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 frame_remove(frp);
1434
1435 /* find last frame and append removed window/frame after it */
1436 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1437 ;
1438 win_append(frp->fr_win, wp1);
1439 frame_append(frp, wp1->w_frame);
1440
1441 wp2 = frp->fr_win; /* previously last window */
1442 }
1443 else /* last window becomes first window */
1444 {
1445 /* find last window/frame in the list and remove it */
1446 for (frp = curwin->w_frame; frp->fr_next != NULL;
1447 frp = frp->fr_next)
1448 ;
1449 wp1 = frp->fr_win;
1450 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001451 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 frame_remove(frp);
1453
1454 /* append the removed window/frame before the first in the list */
1455 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1456 frame_insert(frp->fr_parent->fr_child, frp);
1457 }
1458
1459 /* exchange status height and vsep width of old and new last window */
1460 n = wp2->w_status_height;
1461 wp2->w_status_height = wp1->w_status_height;
1462 wp1->w_status_height = n;
1463 frame_fix_height(wp1);
1464 frame_fix_height(wp2);
1465#ifdef FEAT_VERTSPLIT
1466 n = wp2->w_vsep_width;
1467 wp2->w_vsep_width = wp1->w_vsep_width;
1468 wp1->w_vsep_width = n;
1469 frame_fix_width(wp1);
1470 frame_fix_width(wp2);
1471#endif
1472
1473 /* recompute w_winrow and w_wincol for all windows */
1474 (void)win_comp_pos();
1475 }
1476
1477 redraw_later(CLEAR);
1478}
1479
1480/*
1481 * Move the current window to the very top/bottom/left/right of the screen.
1482 */
1483 static void
1484win_totop(size, flags)
1485 int size;
1486 int flags;
1487{
1488 int dir;
1489 int height = curwin->w_height;
1490
1491 if (lastwin == firstwin)
1492 {
1493 beep_flush();
1494 return;
1495 }
1496
1497 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001498 (void)winframe_remove(curwin, &dir, NULL);
1499 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 last_status(FALSE); /* may need to remove last status line */
1501 (void)win_comp_pos(); /* recompute window positions */
1502
1503 /* Split a window on the desired side and put the window there. */
1504 (void)win_split_ins(size, flags, curwin, dir);
1505 if (!(flags & WSP_VERT))
1506 {
1507 win_setheight(height);
1508 if (p_ea)
1509 win_equal(curwin, TRUE, 'v');
1510 }
1511
1512#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1513 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1514 * scrollbars. Have to update them anyway. */
1515 if (gui.in_use)
1516 {
1517 out_flush();
1518 gui_init_which_components(NULL);
1519 gui_update_scrollbars(TRUE);
1520 }
1521 need_mouse_correct = TRUE;
1522#endif
1523
1524}
1525
1526/*
1527 * Move window "win1" to below/right of "win2" and make "win1" the current
1528 * window. Only works within the same frame!
1529 */
1530 void
1531win_move_after(win1, win2)
1532 win_T *win1, *win2;
1533{
1534 int height;
1535
1536 /* check if the arguments are reasonable */
1537 if (win1 == win2)
1538 return;
1539
1540 /* check if there is something to do */
1541 if (win2->w_next != win1)
1542 {
1543 /* may need move the status line/vertical separator of the last window
1544 * */
1545 if (win1 == lastwin)
1546 {
1547 height = win1->w_prev->w_status_height;
1548 win1->w_prev->w_status_height = win1->w_status_height;
1549 win1->w_status_height = height;
1550#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001551 if (win1->w_prev->w_vsep_width == 1)
1552 {
1553 /* Remove the vertical separator from the last-but-one window,
1554 * add it to the last window. Adjust the frame widths. */
1555 win1->w_prev->w_vsep_width = 0;
1556 win1->w_prev->w_frame->fr_width -= 1;
1557 win1->w_vsep_width = 1;
1558 win1->w_frame->fr_width += 1;
1559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560#endif
1561 }
1562 else if (win2 == lastwin)
1563 {
1564 height = win1->w_status_height;
1565 win1->w_status_height = win2->w_status_height;
1566 win2->w_status_height = height;
1567#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001568 if (win1->w_vsep_width == 1)
1569 {
1570 /* Remove the vertical separator from win1, add it to the last
1571 * window, win2. Adjust the frame widths. */
1572 win2->w_vsep_width = 1;
1573 win2->w_frame->fr_width += 1;
1574 win1->w_vsep_width = 0;
1575 win1->w_frame->fr_width -= 1;
1576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577#endif
1578 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001579 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 frame_remove(win1->w_frame);
1581 win_append(win2, win1);
1582 frame_append(win2->w_frame, win1->w_frame);
1583
1584 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1585 redraw_later(NOT_VALID);
1586 }
1587 win_enter(win1, FALSE);
1588}
1589
1590/*
1591 * Make all windows the same height.
1592 * 'next_curwin' will soon be the current window, make sure it has enough
1593 * rows.
1594 */
1595 void
1596win_equal(next_curwin, current, dir)
1597 win_T *next_curwin; /* pointer to current window to be or NULL */
1598 int current; /* do only frame with current window */
1599 int dir; /* 'v' for vertically, 'h' for horizontally,
1600 'b' for both, 0 for using p_ead */
1601{
1602 if (dir == 0)
1603#ifdef FEAT_VERTSPLIT
1604 dir = *p_ead;
1605#else
1606 dir = 'b';
1607#endif
1608 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001609 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001610 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611}
1612
1613/*
1614 * Set a frame to a new position and height, spreading the available room
1615 * equally over contained frames.
1616 * The window "next_curwin" (if not NULL) should at least get the size from
1617 * 'winheight' and 'winwidth' if possible.
1618 */
1619 static void
1620win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1621 win_T *next_curwin; /* pointer to current window to be or NULL */
1622 int current; /* do only frame with current window */
1623 frame_T *topfr; /* frame to set size off */
1624 int dir; /* 'v', 'h' or 'b', see win_equal() */
1625 int col; /* horizontal position for frame */
1626 int row; /* vertical position for frame */
1627 int width; /* new width of frame */
1628 int height; /* new height of frame */
1629{
1630 int n, m;
1631 int extra_sep = 0;
1632 int wincount, totwincount = 0;
1633 frame_T *fr;
1634 int next_curwin_size = 0;
1635 int room = 0;
1636 int new_size;
1637 int has_next_curwin = 0;
1638 int hnc;
1639
1640 if (topfr->fr_layout == FR_LEAF)
1641 {
1642 /* Set the width/height of this frame.
1643 * Redraw when size or position changes */
1644 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1645#ifdef FEAT_VERTSPLIT
1646 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1647#endif
1648 )
1649 {
1650 topfr->fr_win->w_winrow = row;
1651 frame_new_height(topfr, height, FALSE, FALSE);
1652#ifdef FEAT_VERTSPLIT
1653 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001654 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655#endif
1656 redraw_all_later(CLEAR);
1657 }
1658 }
1659#ifdef FEAT_VERTSPLIT
1660 else if (topfr->fr_layout == FR_ROW)
1661 {
1662 topfr->fr_width = width;
1663 topfr->fr_height = height;
1664
1665 if (dir != 'v') /* equalize frame widths */
1666 {
1667 /* Compute the maximum number of windows horizontally in this
1668 * frame. */
1669 n = frame_minwidth(topfr, NOWIN);
1670 /* add one for the rightmost window, it doesn't have a separator */
1671 if (col + width == Columns)
1672 extra_sep = 1;
1673 else
1674 extra_sep = 0;
1675 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001676 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001678 /*
1679 * Compute width for "next_curwin" window and room available for
1680 * other windows.
1681 * "m" is the minimal width when counting p_wiw for "next_curwin".
1682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 m = frame_minwidth(topfr, next_curwin);
1684 room = width - m;
1685 if (room < 0)
1686 {
1687 next_curwin_size = p_wiw + room;
1688 room = 0;
1689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 else
1691 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001692 next_curwin_size = -1;
1693 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1694 {
1695 /* If 'winfixwidth' set keep the window width if
1696 * possible.
1697 * Watch out for this window being the next_curwin. */
1698 if (frame_fixed_width(fr))
1699 {
1700 n = frame_minwidth(fr, NOWIN);
1701 new_size = fr->fr_width;
1702 if (frame_has_win(fr, next_curwin))
1703 {
1704 room += p_wiw - p_wmw;
1705 next_curwin_size = 0;
1706 if (new_size < p_wiw)
1707 new_size = p_wiw;
1708 }
1709 else
1710 /* These windows don't use up room. */
1711 totwincount -= (n + (fr->fr_next == NULL
1712 ? extra_sep : 0)) / (p_wmw + 1);
1713 room -= new_size - n;
1714 if (room < 0)
1715 {
1716 new_size += room;
1717 room = 0;
1718 }
1719 fr->fr_newwidth = new_size;
1720 }
1721 }
1722 if (next_curwin_size == -1)
1723 {
1724 if (!has_next_curwin)
1725 next_curwin_size = 0;
1726 else if (totwincount > 1
1727 && (room + (totwincount - 2))
1728 / (totwincount - 1) > p_wiw)
1729 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001730 /* Can make all windows wider than 'winwidth', spread
1731 * the room equally. */
1732 next_curwin_size = (room + p_wiw
1733 + (totwincount - 1) * p_wmw
1734 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001735 room -= next_curwin_size - p_wiw;
1736 }
1737 else
1738 next_curwin_size = p_wiw;
1739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001741
1742 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 --totwincount; /* don't count curwin */
1744 }
1745
1746 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1747 {
1748 n = m = 0;
1749 wincount = 1;
1750 if (fr->fr_next == NULL)
1751 /* last frame gets all that remains (avoid roundoff error) */
1752 new_size = width;
1753 else if (dir == 'v')
1754 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001755 else if (frame_fixed_width(fr))
1756 {
1757 new_size = fr->fr_newwidth;
1758 wincount = 0; /* doesn't count as a sizeable window */
1759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 else
1761 {
1762 /* Compute the maximum number of windows horiz. in "fr". */
1763 n = frame_minwidth(fr, NOWIN);
1764 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1765 / (p_wmw + 1);
1766 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001767 if (has_next_curwin)
1768 hnc = frame_has_win(fr, next_curwin);
1769 else
1770 hnc = FALSE;
1771 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001773 if (totwincount == 0)
1774 new_size = room;
1775 else
1776 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001778 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 {
1780 next_curwin_size -= p_wiw - (m - n);
1781 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001782 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001784 else
1785 room -= new_size;
1786 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 }
1788
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001789 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 * window, unless equalizing all frames. */
1791 if (!current || dir != 'v' || topfr->fr_parent != NULL
1792 || (new_size != fr->fr_width)
1793 || frame_has_win(fr, next_curwin))
1794 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001795 new_size, height);
1796 col += new_size;
1797 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 totwincount -= wincount;
1799 }
1800 }
1801#endif
1802 else /* topfr->fr_layout == FR_COL */
1803 {
1804#ifdef FEAT_VERTSPLIT
1805 topfr->fr_width = width;
1806#endif
1807 topfr->fr_height = height;
1808
1809 if (dir != 'h') /* equalize frame heights */
1810 {
1811 /* Compute maximum number of windows vertically in this frame. */
1812 n = frame_minheight(topfr, NOWIN);
1813 /* add one for the bottom window if it doesn't have a statusline */
1814 if (row + height == cmdline_row && p_ls == 0)
1815 extra_sep = 1;
1816 else
1817 extra_sep = 0;
1818 totwincount = (n + extra_sep) / (p_wmh + 1);
1819 has_next_curwin = frame_has_win(topfr, next_curwin);
1820
1821 /*
1822 * Compute height for "next_curwin" window and room available for
1823 * other windows.
1824 * "m" is the minimal height when counting p_wh for "next_curwin".
1825 */
1826 m = frame_minheight(topfr, next_curwin);
1827 room = height - m;
1828 if (room < 0)
1829 {
1830 /* The room is less then 'winheight', use all space for the
1831 * current window. */
1832 next_curwin_size = p_wh + room;
1833 room = 0;
1834 }
1835 else
1836 {
1837 next_curwin_size = -1;
1838 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1839 {
1840 /* If 'winfixheight' set keep the window height if
1841 * possible.
1842 * Watch out for this window being the next_curwin. */
1843 if (frame_fixed_height(fr))
1844 {
1845 n = frame_minheight(fr, NOWIN);
1846 new_size = fr->fr_height;
1847 if (frame_has_win(fr, next_curwin))
1848 {
1849 room += p_wh - p_wmh;
1850 next_curwin_size = 0;
1851 if (new_size < p_wh)
1852 new_size = p_wh;
1853 }
1854 else
1855 /* These windows don't use up room. */
1856 totwincount -= (n + (fr->fr_next == NULL
1857 ? extra_sep : 0)) / (p_wmh + 1);
1858 room -= new_size - n;
1859 if (room < 0)
1860 {
1861 new_size += room;
1862 room = 0;
1863 }
1864 fr->fr_newheight = new_size;
1865 }
1866 }
1867 if (next_curwin_size == -1)
1868 {
1869 if (!has_next_curwin)
1870 next_curwin_size = 0;
1871 else if (totwincount > 1
1872 && (room + (totwincount - 2))
1873 / (totwincount - 1) > p_wh)
1874 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001875 /* can make all windows higher than 'winheight',
1876 * spread the room equally. */
1877 next_curwin_size = (room + p_wh
1878 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 + (totwincount - 1)) / totwincount;
1880 room -= next_curwin_size - p_wh;
1881 }
1882 else
1883 next_curwin_size = p_wh;
1884 }
1885 }
1886
1887 if (has_next_curwin)
1888 --totwincount; /* don't count curwin */
1889 }
1890
1891 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1892 {
1893 n = m = 0;
1894 wincount = 1;
1895 if (fr->fr_next == NULL)
1896 /* last frame gets all that remains (avoid roundoff error) */
1897 new_size = height;
1898 else if (dir == 'h')
1899 new_size = fr->fr_height;
1900 else if (frame_fixed_height(fr))
1901 {
1902 new_size = fr->fr_newheight;
1903 wincount = 0; /* doesn't count as a sizeable window */
1904 }
1905 else
1906 {
1907 /* Compute the maximum number of windows vert. in "fr". */
1908 n = frame_minheight(fr, NOWIN);
1909 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1910 / (p_wmh + 1);
1911 m = frame_minheight(fr, next_curwin);
1912 if (has_next_curwin)
1913 hnc = frame_has_win(fr, next_curwin);
1914 else
1915 hnc = FALSE;
1916 if (hnc) /* don't count next_curwin */
1917 --wincount;
1918 if (totwincount == 0)
1919 new_size = room;
1920 else
1921 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1922 / totwincount;
1923 if (hnc) /* add next_curwin size */
1924 {
1925 next_curwin_size -= p_wh - (m - n);
1926 new_size += next_curwin_size;
1927 room -= new_size - next_curwin_size;
1928 }
1929 else
1930 room -= new_size;
1931 new_size += n;
1932 }
1933 /* Skip frame that is full width when splitting or closing a
1934 * window, unless equalizing all frames. */
1935 if (!current || dir != 'h' || topfr->fr_parent != NULL
1936 || (new_size != fr->fr_height)
1937 || frame_has_win(fr, next_curwin))
1938 win_equal_rec(next_curwin, current, fr, dir, col, row,
1939 width, new_size);
1940 row += new_size;
1941 height -= new_size;
1942 totwincount -= wincount;
1943 }
1944 }
1945}
1946
1947/*
1948 * close all windows for buffer 'buf'
1949 */
1950 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00001951close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001953 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954{
Bram Moolenaarf740b292006-02-16 22:11:02 +00001955 win_T *wp;
1956 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001957 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958
1959 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001960
1961 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001963 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001965 win_close(wp, FALSE);
1966
1967 /* Start all over, autocommands may change the window layout. */
1968 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 }
1970 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00001971 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001973
1974 /* Also check windows in other tab pages. */
1975 for (tp = first_tabpage; tp != NULL; tp = nexttp)
1976 {
1977 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001978 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001979 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
1980 if (wp->w_buffer == buf)
1981 {
1982 win_close_othertab(wp, FALSE, tp);
1983
1984 /* Start all over, the tab page may be closed and
1985 * autocommands may change the window layout. */
1986 nexttp = first_tabpage;
1987 break;
1988 }
1989 }
1990
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001992
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001993 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00001994 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995}
1996
1997/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001998 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001999 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002000 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002001 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002002last_window()
2003{
2004 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
2005}
2006
2007/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002008 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 * If "free_buf" is TRUE related buffer may be unloaded.
2010 *
2011 * called by :quit, :close, :xit, :wq and findtag()
2012 */
2013 void
2014win_close(win, free_buf)
2015 win_T *win;
2016 int free_buf;
2017{
2018 win_T *wp;
2019#ifdef FEAT_AUTOCMD
2020 int other_buffer = FALSE;
2021#endif
2022 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 int dir;
2024 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002025 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002027 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 {
2029 EMSG(_("E444: Cannot close last window"));
2030 return;
2031 }
2032
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002033 /*
2034 * When closing the last window in a tab page first go to another tab
2035 * page and then close the window and the tab page. This avoids that
2036 * curwin and curtab are not invalid while we are freeing memory, they may
2037 * be used in GUI events.
2038 */
2039 if (firstwin == lastwin)
2040 {
2041 goto_tabpage_tp(alt_tabpage());
2042 redraw_tabline = TRUE;
2043
2044 /* Safety check: Autocommands may have closed the window when jumping
2045 * to the other tab page. */
2046 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2047 {
2048 int h = tabline_height();
2049
2050 win_close_othertab(win, free_buf, prev_curtab);
2051 if (h != tabline_height())
2052 shell_new_rows();
2053 }
2054 return;
2055 }
2056
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 /* When closing the help window, try restoring a snapshot after closing
2058 * the window. Otherwise clear the snapshot, it's now invalid. */
2059 if (win->w_buffer->b_help)
2060 help_window = TRUE;
2061 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002062 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
2064#ifdef FEAT_AUTOCMD
2065 if (win == curwin)
2066 {
2067 /*
2068 * Guess which window is going to be the new current window.
2069 * This may change because of the autocommands (sigh).
2070 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002071 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072
2073 /*
2074 * Be careful: If autocommands delete the window, return now.
2075 */
2076 if (wp->w_buffer != curbuf)
2077 {
2078 other_buffer = TRUE;
2079 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002080 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 return;
2082 }
2083 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002084 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 return;
2086# ifdef FEAT_EVAL
2087 /* autocmds may abort script processing */
2088 if (aborting())
2089 return;
2090# endif
2091 }
2092#endif
2093
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002094#ifdef FEAT_GUI
2095 /* Avoid trouble with scrollbars that are going to be deleted in
2096 * win_free(). */
2097 if (gui.in_use)
2098 out_flush();
2099#endif
2100
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 /*
2102 * Close the link to the buffer.
2103 */
2104 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002105
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002107 * other window or moved to another tab page. */
2108 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 return;
2110
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002111 /* Free the memory used for the window. */
2112 wp = win_free_mem(win, &dir, NULL);
2113
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 /* Make sure curwin isn't invalid. It can cause severe trouble when
2115 * printing an error message. For win_equal() curbuf needs to be valid
2116 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002117 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 {
2119 curwin = wp;
2120#ifdef FEAT_QUICKFIX
2121 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2122 {
2123 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002124 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 * finding another window to go to.
2126 */
2127 for (;;)
2128 {
2129 if (wp->w_next == NULL)
2130 wp = firstwin;
2131 else
2132 wp = wp->w_next;
2133 if (wp == curwin)
2134 break;
2135 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2136 {
2137 curwin = wp;
2138 break;
2139 }
2140 }
2141 }
2142#endif
2143 curbuf = curwin->w_buffer;
2144 close_curwin = TRUE;
2145 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002146 if (p_ea
2147#ifdef FEAT_VERTSPLIT
2148 && (*p_ead == 'b' || *p_ead == dir)
2149#endif
2150 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 win_equal(curwin, TRUE,
2152#ifdef FEAT_VERTSPLIT
2153 dir
2154#else
2155 0
2156#endif
2157 );
2158 else
2159 win_comp_pos();
2160 if (close_curwin)
2161 {
2162 win_enter_ext(wp, FALSE, TRUE);
2163#ifdef FEAT_AUTOCMD
2164 if (other_buffer)
2165 /* careful: after this wp and win may be invalid! */
2166 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2167#endif
2168 }
2169
2170 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002171 * If last window has a status line now and we don't want one,
2172 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 */
2174 last_status(FALSE);
2175
2176 /* After closing the help window, try restoring the window layout from
2177 * before it was opened. */
2178 if (help_window)
2179 restore_snapshot(close_curwin);
2180
2181#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2182 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2183 if (gui.in_use && !win_hasvertsplit())
2184 gui_init_which_components(NULL);
2185#endif
2186
2187 redraw_all_later(NOT_VALID);
2188}
2189
2190/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002191 * Close window "win" in tab page "tp", which is not the current tab page.
2192 * This may be the last window ih that tab page and result in closing the tab,
2193 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002194 * Caller must check if buffer is hidden and whether the tabline needs to be
2195 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002196 */
2197 void
2198win_close_othertab(win, free_buf, tp)
2199 win_T *win;
2200 int free_buf;
2201 tabpage_T *tp;
2202{
2203 win_T *wp;
2204 int dir;
2205 tabpage_T *ptp = NULL;
2206
2207 /* Close the link to the buffer. */
2208 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2209
2210 /* Careful: Autocommands may have closed the tab page or made it the
2211 * current tab page. */
2212 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2213 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002214 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002215 return;
2216
2217 /* Autocommands may have closed the window already. */
2218 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2219 ;
2220 if (wp == NULL)
2221 return;
2222
2223 /* Free the memory used for the window. */
2224 wp = win_free_mem(win, &dir, tp);
2225
2226 /* When closing the last window in a tab page remove the tab page. */
2227 if (wp == NULL)
2228 {
2229 if (tp == first_tabpage)
2230 first_tabpage = tp->tp_next;
2231 else
2232 {
2233 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2234 ptp = ptp->tp_next)
2235 ;
2236 if (ptp == NULL)
2237 {
2238 EMSG2(_(e_intern2), "win_close_othertab()");
2239 return;
2240 }
2241 ptp->tp_next = tp->tp_next;
2242 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002243 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002244 }
2245}
2246
2247/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002248 * Free the memory used for a window.
2249 * Returns a pointer to the window that got the freed up space.
2250 */
2251 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002252win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002253 win_T *win;
2254 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002255 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002256{
2257 frame_T *frp;
2258 win_T *wp;
2259
Bram Moolenaarea408852005-06-25 22:49:46 +00002260#ifdef FEAT_FOLDING
2261 clearFolding(win);
2262#endif
2263
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002264 /* reduce the reference count to the argument list. */
2265 alist_unlink(win->w_alist);
2266
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002267 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002268 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002269 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002270 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002271 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002272
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002273 /* When deleting the current window of another tab page select a new
2274 * current window. */
2275 if (tp != NULL && win == tp->tp_curwin)
2276 tp->tp_curwin = wp;
2277
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002278 return wp;
2279}
2280
2281#if defined(EXITFREE) || defined(PROTO)
2282 void
2283win_free_all()
2284{
2285 int dummy;
2286
Bram Moolenaarf740b292006-02-16 22:11:02 +00002287# ifdef FEAT_WINDOWS
2288 while (first_tabpage->tp_next != NULL)
2289 tabpage_close(TRUE);
2290# endif
2291
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002292 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002293 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002294}
2295#endif
2296
2297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 * Remove a window and its frame from the tree of frames.
2299 * Returns a pointer to the window that got the freed up space.
2300 */
2301/*ARGSUSED*/
2302 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002303winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 win_T *win;
2305 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002306 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307{
2308 frame_T *frp, *frp2, *frp3;
2309 frame_T *frp_close = win->w_frame;
2310 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311
2312 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002313 * If there is only one window there is nothing to remove.
2314 */
2315 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2316 return NULL;
2317
2318 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 * Remove the window from its frame.
2320 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002321 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 wp = frame2win(frp2);
2323
2324 /* Remove this frame from the list of frames. */
2325 frame_remove(frp_close);
2326
2327#ifdef FEAT_VERTSPLIT
2328 if (frp_close->fr_parent->fr_layout == FR_COL)
2329 {
2330#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002331 /* When 'winfixheight' is set, try to find another frame in the column
2332 * (as close to the closed frame as possible) to distribute the height
2333 * to. */
2334 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2335 {
2336 frp = frp_close->fr_prev;
2337 frp3 = frp_close->fr_next;
2338 while (frp != NULL || frp3 != NULL)
2339 {
2340 if (frp != NULL)
2341 {
2342 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2343 {
2344 frp2 = frp;
2345 wp = frp->fr_win;
2346 break;
2347 }
2348 frp = frp->fr_prev;
2349 }
2350 if (frp3 != NULL)
2351 {
2352 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2353 {
2354 frp2 = frp3;
2355 wp = frp3->fr_win;
2356 break;
2357 }
2358 frp3 = frp3->fr_next;
2359 }
2360 }
2361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2363 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364#ifdef FEAT_VERTSPLIT
2365 *dirp = 'v';
2366 }
2367 else
2368 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002369 /* When 'winfixwidth' is set, try to find another frame in the column
2370 * (as close to the closed frame as possible) to distribute the width
2371 * to. */
2372 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2373 {
2374 frp = frp_close->fr_prev;
2375 frp3 = frp_close->fr_next;
2376 while (frp != NULL || frp3 != NULL)
2377 {
2378 if (frp != NULL)
2379 {
2380 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2381 {
2382 frp2 = frp;
2383 wp = frp->fr_win;
2384 break;
2385 }
2386 frp = frp->fr_prev;
2387 }
2388 if (frp3 != NULL)
2389 {
2390 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2391 {
2392 frp2 = frp3;
2393 wp = frp3->fr_win;
2394 break;
2395 }
2396 frp3 = frp3->fr_next;
2397 }
2398 }
2399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002401 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 *dirp = 'h';
2403 }
2404#endif
2405
2406 /* If rows/columns go to a window below/right its positions need to be
2407 * updated. Can only be done after the sizes have been updated. */
2408 if (frp2 == frp_close->fr_next)
2409 {
2410 int row = win->w_winrow;
2411 int col = W_WINCOL(win);
2412
2413 frame_comp_pos(frp2, &row, &col);
2414 }
2415
2416 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2417 {
2418 /* There is no other frame in this list, move its info to the parent
2419 * and remove it. */
2420 frp2->fr_parent->fr_layout = frp2->fr_layout;
2421 frp2->fr_parent->fr_child = frp2->fr_child;
2422 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2423 frp->fr_parent = frp2->fr_parent;
2424 frp2->fr_parent->fr_win = frp2->fr_win;
2425 if (frp2->fr_win != NULL)
2426 frp2->fr_win->w_frame = frp2->fr_parent;
2427 frp = frp2->fr_parent;
2428 vim_free(frp2);
2429
2430 frp2 = frp->fr_parent;
2431 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2432 {
2433 /* The frame above the parent has the same layout, have to merge
2434 * the frames into this list. */
2435 if (frp2->fr_child == frp)
2436 frp2->fr_child = frp->fr_child;
2437 frp->fr_child->fr_prev = frp->fr_prev;
2438 if (frp->fr_prev != NULL)
2439 frp->fr_prev->fr_next = frp->fr_child;
2440 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2441 {
2442 frp3->fr_parent = frp2;
2443 if (frp3->fr_next == NULL)
2444 {
2445 frp3->fr_next = frp->fr_next;
2446 if (frp->fr_next != NULL)
2447 frp->fr_next->fr_prev = frp3;
2448 break;
2449 }
2450 }
2451 vim_free(frp);
2452 }
2453 }
2454
2455 return wp;
2456}
2457
2458/*
2459 * Find out which frame is going to get the freed up space when "win" is
2460 * closed.
2461 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2462 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2463 * This makes opening a window and closing it immediately keep the same window
2464 * layout.
2465 */
2466 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002467win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002469 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470{
2471 frame_T *frp;
2472 int b;
2473
Bram Moolenaarf740b292006-02-16 22:11:02 +00002474 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002475 /* Last window in this tab page, will go to next tab page. */
2476 return alt_tabpage()->tp_curwin->w_frame;
2477
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 frp = win->w_frame;
2479#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002480 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 b = p_spr;
2482 else
2483#endif
2484 b = p_sb;
2485 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2486 return frp->fr_next;
2487 return frp->fr_prev;
2488}
2489
2490/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002491 * Return the tabpage that will be used if the current one is closed.
2492 */
2493 static tabpage_T *
2494alt_tabpage()
2495{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002496 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002497
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002498 /* Use the next tab page if possible. */
2499 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002500 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002501
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002502 /* Find the last but one tab page. */
2503 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2504 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002505 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002506}
2507
2508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 * Find the left-upper window in frame "frp".
2510 */
2511 static win_T *
2512frame2win(frp)
2513 frame_T *frp;
2514{
2515 while (frp->fr_win == NULL)
2516 frp = frp->fr_child;
2517 return frp->fr_win;
2518}
2519
2520/*
2521 * Return TRUE if frame "frp" contains window "wp".
2522 */
2523 static int
2524frame_has_win(frp, wp)
2525 frame_T *frp;
2526 win_T *wp;
2527{
2528 frame_T *p;
2529
2530 if (frp->fr_layout == FR_LEAF)
2531 return frp->fr_win == wp;
2532
2533 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2534 if (frame_has_win(p, wp))
2535 return TRUE;
2536 return FALSE;
2537}
2538
2539/*
2540 * Set a new height for a frame. Recursively sets the height for contained
2541 * frames and windows. Caller must take care of positions.
2542 */
2543 static void
2544frame_new_height(topfrp, height, topfirst, wfh)
2545 frame_T *topfrp;
2546 int height;
2547 int topfirst; /* resize topmost contained frame first */
2548 int wfh; /* obey 'winfixheight' when there is a choice;
2549 may cause the height not to be set */
2550{
2551 frame_T *frp;
2552 int extra_lines;
2553 int h;
2554
2555 if (topfrp->fr_win != NULL)
2556 {
2557 /* Simple case: just one window. */
2558 win_new_height(topfrp->fr_win,
2559 height - topfrp->fr_win->w_status_height);
2560 }
2561#ifdef FEAT_VERTSPLIT
2562 else if (topfrp->fr_layout == FR_ROW)
2563 {
2564 do
2565 {
2566 /* All frames in this row get the same new height. */
2567 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2568 {
2569 frame_new_height(frp, height, topfirst, wfh);
2570 if (frp->fr_height > height)
2571 {
2572 /* Could not fit the windows, make the whole row higher. */
2573 height = frp->fr_height;
2574 break;
2575 }
2576 }
2577 }
2578 while (frp != NULL);
2579 }
2580#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002581 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 {
2583 /* Complicated case: Resize a column of frames. Resize the bottom
2584 * frame first, frames above that when needed. */
2585
2586 frp = topfrp->fr_child;
2587 if (wfh)
2588 /* Advance past frames with one window with 'wfh' set. */
2589 while (frame_fixed_height(frp))
2590 {
2591 frp = frp->fr_next;
2592 if (frp == NULL)
2593 return; /* no frame without 'wfh', give up */
2594 }
2595 if (!topfirst)
2596 {
2597 /* Find the bottom frame of this column */
2598 while (frp->fr_next != NULL)
2599 frp = frp->fr_next;
2600 if (wfh)
2601 /* Advance back for frames with one window with 'wfh' set. */
2602 while (frame_fixed_height(frp))
2603 frp = frp->fr_prev;
2604 }
2605
2606 extra_lines = height - topfrp->fr_height;
2607 if (extra_lines < 0)
2608 {
2609 /* reduce height of contained frames, bottom or top frame first */
2610 while (frp != NULL)
2611 {
2612 h = frame_minheight(frp, NULL);
2613 if (frp->fr_height + extra_lines < h)
2614 {
2615 extra_lines += frp->fr_height - h;
2616 frame_new_height(frp, h, topfirst, wfh);
2617 }
2618 else
2619 {
2620 frame_new_height(frp, frp->fr_height + extra_lines,
2621 topfirst, wfh);
2622 break;
2623 }
2624 if (topfirst)
2625 {
2626 do
2627 frp = frp->fr_next;
2628 while (wfh && frp != NULL && frame_fixed_height(frp));
2629 }
2630 else
2631 {
2632 do
2633 frp = frp->fr_prev;
2634 while (wfh && frp != NULL && frame_fixed_height(frp));
2635 }
2636 /* Increase "height" if we could not reduce enough frames. */
2637 if (frp == NULL)
2638 height -= extra_lines;
2639 }
2640 }
2641 else if (extra_lines > 0)
2642 {
2643 /* increase height of bottom or top frame */
2644 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2645 }
2646 }
2647 topfrp->fr_height = height;
2648}
2649
2650/*
2651 * Return TRUE if height of frame "frp" should not be changed because of
2652 * the 'winfixheight' option.
2653 */
2654 static int
2655frame_fixed_height(frp)
2656 frame_T *frp;
2657{
2658 /* frame with one window: fixed height if 'winfixheight' set. */
2659 if (frp->fr_win != NULL)
2660 return frp->fr_win->w_p_wfh;
2661
2662 if (frp->fr_layout == FR_ROW)
2663 {
2664 /* The frame is fixed height if one of the frames in the row is fixed
2665 * height. */
2666 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2667 if (frame_fixed_height(frp))
2668 return TRUE;
2669 return FALSE;
2670 }
2671
2672 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2673 * frames in the row are fixed height. */
2674 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2675 if (!frame_fixed_height(frp))
2676 return FALSE;
2677 return TRUE;
2678}
2679
2680#ifdef FEAT_VERTSPLIT
2681/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002682 * Return TRUE if width of frame "frp" should not be changed because of
2683 * the 'winfixwidth' option.
2684 */
2685 static int
2686frame_fixed_width(frp)
2687 frame_T *frp;
2688{
2689 /* frame with one window: fixed width if 'winfixwidth' set. */
2690 if (frp->fr_win != NULL)
2691 return frp->fr_win->w_p_wfw;
2692
2693 if (frp->fr_layout == FR_COL)
2694 {
2695 /* The frame is fixed width if one of the frames in the row is fixed
2696 * width. */
2697 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2698 if (frame_fixed_width(frp))
2699 return TRUE;
2700 return FALSE;
2701 }
2702
2703 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2704 * frames in the row are fixed width. */
2705 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2706 if (!frame_fixed_width(frp))
2707 return FALSE;
2708 return TRUE;
2709}
2710
2711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 * Add a status line to windows at the bottom of "frp".
2713 * Note: Does not check if there is room!
2714 */
2715 static void
2716frame_add_statusline(frp)
2717 frame_T *frp;
2718{
2719 win_T *wp;
2720
2721 if (frp->fr_layout == FR_LEAF)
2722 {
2723 wp = frp->fr_win;
2724 if (wp->w_status_height == 0)
2725 {
2726 if (wp->w_height > 0) /* don't make it negative */
2727 --wp->w_height;
2728 wp->w_status_height = STATUS_HEIGHT;
2729 }
2730 }
2731 else if (frp->fr_layout == FR_ROW)
2732 {
2733 /* Handle all the frames in the row. */
2734 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2735 frame_add_statusline(frp);
2736 }
2737 else /* frp->fr_layout == FR_COL */
2738 {
2739 /* Only need to handle the last frame in the column. */
2740 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2741 ;
2742 frame_add_statusline(frp);
2743 }
2744}
2745
2746/*
2747 * Set width of a frame. Handles recursively going through contained frames.
2748 * May remove separator line for windows at the right side (for win_close()).
2749 */
2750 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002751frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 frame_T *topfrp;
2753 int width;
2754 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002755 int wfw; /* obey 'winfixwidth' when there is a choice;
2756 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757{
2758 frame_T *frp;
2759 int extra_cols;
2760 int w;
2761 win_T *wp;
2762
2763 if (topfrp->fr_layout == FR_LEAF)
2764 {
2765 /* Simple case: just one window. */
2766 wp = topfrp->fr_win;
2767 /* Find out if there are any windows right of this one. */
2768 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2769 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2770 break;
2771 if (frp->fr_parent == NULL)
2772 wp->w_vsep_width = 0;
2773 win_new_width(wp, width - wp->w_vsep_width);
2774 }
2775 else if (topfrp->fr_layout == FR_COL)
2776 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002777 do
2778 {
2779 /* All frames in this column get the same new width. */
2780 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2781 {
2782 frame_new_width(frp, width, leftfirst, wfw);
2783 if (frp->fr_width > width)
2784 {
2785 /* Could not fit the windows, make whole column wider. */
2786 width = frp->fr_width;
2787 break;
2788 }
2789 }
2790 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
2792 else /* fr_layout == FR_ROW */
2793 {
2794 /* Complicated case: Resize a row of frames. Resize the rightmost
2795 * frame first, frames left of it when needed. */
2796
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002798 if (wfw)
2799 /* Advance past frames with one window with 'wfw' set. */
2800 while (frame_fixed_width(frp))
2801 {
2802 frp = frp->fr_next;
2803 if (frp == NULL)
2804 return; /* no frame without 'wfw', give up */
2805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002807 {
2808 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 while (frp->fr_next != NULL)
2810 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002811 if (wfw)
2812 /* Advance back for frames with one window with 'wfw' set. */
2813 while (frame_fixed_width(frp))
2814 frp = frp->fr_prev;
2815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816
2817 extra_cols = width - topfrp->fr_width;
2818 if (extra_cols < 0)
2819 {
2820 /* reduce frame width, rightmost frame first */
2821 while (frp != NULL)
2822 {
2823 w = frame_minwidth(frp, NULL);
2824 if (frp->fr_width + extra_cols < w)
2825 {
2826 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002827 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 }
2829 else
2830 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002831 frame_new_width(frp, frp->fr_width + extra_cols,
2832 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 break;
2834 }
2835 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002836 {
2837 do
2838 frp = frp->fr_next;
2839 while (wfw && frp != NULL && frame_fixed_width(frp));
2840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002842 {
2843 do
2844 frp = frp->fr_prev;
2845 while (wfw && frp != NULL && frame_fixed_width(frp));
2846 }
2847 /* Increase "width" if we could not reduce enough frames. */
2848 if (frp == NULL)
2849 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 }
2851 }
2852 else if (extra_cols > 0)
2853 {
2854 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002855 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
2857 }
2858 topfrp->fr_width = width;
2859}
2860
2861/*
2862 * Add the vertical separator to windows at the right side of "frp".
2863 * Note: Does not check if there is room!
2864 */
2865 static void
2866frame_add_vsep(frp)
2867 frame_T *frp;
2868{
2869 win_T *wp;
2870
2871 if (frp->fr_layout == FR_LEAF)
2872 {
2873 wp = frp->fr_win;
2874 if (wp->w_vsep_width == 0)
2875 {
2876 if (wp->w_width > 0) /* don't make it negative */
2877 --wp->w_width;
2878 wp->w_vsep_width = 1;
2879 }
2880 }
2881 else if (frp->fr_layout == FR_COL)
2882 {
2883 /* Handle all the frames in the column. */
2884 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2885 frame_add_vsep(frp);
2886 }
2887 else /* frp->fr_layout == FR_ROW */
2888 {
2889 /* Only need to handle the last frame in the row. */
2890 frp = frp->fr_child;
2891 while (frp->fr_next != NULL)
2892 frp = frp->fr_next;
2893 frame_add_vsep(frp);
2894 }
2895}
2896
2897/*
2898 * Set frame width from the window it contains.
2899 */
2900 static void
2901frame_fix_width(wp)
2902 win_T *wp;
2903{
2904 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2905}
2906#endif
2907
2908/*
2909 * Set frame height from the window it contains.
2910 */
2911 static void
2912frame_fix_height(wp)
2913 win_T *wp;
2914{
2915 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2916}
2917
2918/*
2919 * Compute the minimal height for frame "topfrp".
2920 * Uses the 'winminheight' option.
2921 * When "next_curwin" isn't NULL, use p_wh for this window.
2922 * When "next_curwin" is NOWIN, don't use at least one line for the current
2923 * window.
2924 */
2925 static int
2926frame_minheight(topfrp, next_curwin)
2927 frame_T *topfrp;
2928 win_T *next_curwin;
2929{
2930 frame_T *frp;
2931 int m;
2932#ifdef FEAT_VERTSPLIT
2933 int n;
2934#endif
2935
2936 if (topfrp->fr_win != NULL)
2937 {
2938 if (topfrp->fr_win == next_curwin)
2939 m = p_wh + topfrp->fr_win->w_status_height;
2940 else
2941 {
2942 /* window: minimal height of the window plus status line */
2943 m = p_wmh + topfrp->fr_win->w_status_height;
2944 /* Current window is minimal one line high */
2945 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2946 ++m;
2947 }
2948 }
2949#ifdef FEAT_VERTSPLIT
2950 else if (topfrp->fr_layout == FR_ROW)
2951 {
2952 /* get the minimal height from each frame in this row */
2953 m = 0;
2954 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2955 {
2956 n = frame_minheight(frp, next_curwin);
2957 if (n > m)
2958 m = n;
2959 }
2960 }
2961#endif
2962 else
2963 {
2964 /* Add up the minimal heights for all frames in this column. */
2965 m = 0;
2966 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2967 m += frame_minheight(frp, next_curwin);
2968 }
2969
2970 return m;
2971}
2972
2973#ifdef FEAT_VERTSPLIT
2974/*
2975 * Compute the minimal width for frame "topfrp".
2976 * When "next_curwin" isn't NULL, use p_wiw for this window.
2977 * When "next_curwin" is NOWIN, don't use at least one column for the current
2978 * window.
2979 */
2980 static int
2981frame_minwidth(topfrp, next_curwin)
2982 frame_T *topfrp;
2983 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2984{
2985 frame_T *frp;
2986 int m, n;
2987
2988 if (topfrp->fr_win != NULL)
2989 {
2990 if (topfrp->fr_win == next_curwin)
2991 m = p_wiw + topfrp->fr_win->w_vsep_width;
2992 else
2993 {
2994 /* window: minimal width of the window plus separator column */
2995 m = p_wmw + topfrp->fr_win->w_vsep_width;
2996 /* Current window is minimal one column wide */
2997 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2998 ++m;
2999 }
3000 }
3001 else if (topfrp->fr_layout == FR_COL)
3002 {
3003 /* get the minimal width from each frame in this column */
3004 m = 0;
3005 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3006 {
3007 n = frame_minwidth(frp, next_curwin);
3008 if (n > m)
3009 m = n;
3010 }
3011 }
3012 else
3013 {
3014 /* Add up the minimal widths for all frames in this row. */
3015 m = 0;
3016 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3017 m += frame_minwidth(frp, next_curwin);
3018 }
3019
3020 return m;
3021}
3022#endif
3023
3024
3025/*
3026 * Try to close all windows except current one.
3027 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3028 * used and the buffer was modified.
3029 *
3030 * Used by ":bdel" and ":only".
3031 */
3032 void
3033close_others(message, forceit)
3034 int message;
3035 int forceit; /* always hide all other windows */
3036{
3037 win_T *wp;
3038 win_T *nextwp;
3039 int r;
3040
3041 if (lastwin == firstwin)
3042 {
3043 if (message
3044#ifdef FEAT_AUTOCMD
3045 && !autocmd_busy
3046#endif
3047 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003048 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 return;
3050 }
3051
3052 /* Be very careful here: autocommands may change the window layout. */
3053 for (wp = firstwin; win_valid(wp); wp = nextwp)
3054 {
3055 nextwp = wp->w_next;
3056 if (wp != curwin) /* don't close current window */
3057 {
3058
3059 /* Check if it's allowed to abandon this window */
3060 r = can_abandon(wp->w_buffer, forceit);
3061#ifdef FEAT_AUTOCMD
3062 if (!win_valid(wp)) /* autocommands messed wp up */
3063 {
3064 nextwp = firstwin;
3065 continue;
3066 }
3067#endif
3068 if (!r)
3069 {
3070#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3071 if (message && (p_confirm || cmdmod.confirm) && p_write)
3072 {
3073 dialog_changed(wp->w_buffer, FALSE);
3074# ifdef FEAT_AUTOCMD
3075 if (!win_valid(wp)) /* autocommands messed wp up */
3076 {
3077 nextwp = firstwin;
3078 continue;
3079 }
3080# endif
3081 }
3082 if (bufIsChanged(wp->w_buffer))
3083#endif
3084 continue;
3085 }
3086 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3087 }
3088 }
3089
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003090 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 EMSG(_("E445: Other window contains changes"));
3092}
3093
3094#endif /* FEAT_WINDOWS */
3095
3096/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003097 * Init the current window "curwin".
3098 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 */
3100 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003101curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003103 redraw_win_later(curwin, NOT_VALID);
3104 curwin->w_lines_valid = 0;
3105 curwin->w_cursor.lnum = 1;
3106 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003108 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003110 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3111 curwin->w_pcmark.col = 0;
3112 curwin->w_prev_pcmark.lnum = 0;
3113 curwin->w_prev_pcmark.col = 0;
3114 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003116 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003118 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119#ifdef FEAT_FKMAP
3120 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003121 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003123 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124#endif
3125}
3126
3127/*
3128 * Allocate the first window and put an empty buffer in it.
3129 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003130 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003132 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133win_alloc_first()
3134{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003135 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003136 return FAIL;
3137
3138#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003139 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003140 if (first_tabpage == NULL)
3141 return FAIL;
3142 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003143 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003144#endif
3145 return OK;
3146}
3147
3148/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003149 * Allocate the first window or the first window in a new tab page.
3150 * When "oldwin" is NULL create an empty buffer for it.
3151 * When "oldwin" is not NULL copy info from it to the new window (only with
3152 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003153 * Return FAIL when something goes wrong (out of memory).
3154 */
3155 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003156win_alloc_firstwin(oldwin)
3157 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003158{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003160 if (oldwin == NULL)
3161 {
3162 /* Very first window, need to create an empty buffer for it and
3163 * initialize from scratch. */
3164 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3165 if (curwin == NULL || curbuf == NULL)
3166 return FAIL;
3167 curwin->w_buffer = curbuf;
3168 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003170 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003172 curwin_init(); /* init current window */
3173 }
3174#ifdef FEAT_WINDOWS
3175 else
3176 {
3177 /* First window in new tab page, initialize it from "oldwin". */
3178 win_init(curwin, oldwin);
3179
3180# ifdef FEAT_SCROLLBIND
3181 /* We don't want scroll-binding in the first window. */
3182 curwin->w_p_scb = FALSE;
3183# endif
3184 }
3185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
3187 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3188 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003189 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 topframe->fr_layout = FR_LEAF;
3191#ifdef FEAT_VERTSPLIT
3192 topframe->fr_width = Columns;
3193#endif
3194 topframe->fr_height = Rows - p_ch;
3195 topframe->fr_win = curwin;
3196 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003197
3198 return OK;
3199}
3200
3201/*
3202 * Initialize the window and frame size to the maximum.
3203 */
3204 void
3205win_init_size()
3206{
3207 firstwin->w_height = ROWS_AVAIL;
3208 topframe->fr_height = ROWS_AVAIL;
3209#ifdef FEAT_VERTSPLIT
3210 firstwin->w_width = Columns;
3211 topframe->fr_width = Columns;
3212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213}
3214
3215#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003216
3217/*
3218 * Allocate a new tabpage_T and init the values.
3219 * Returns NULL when out of memory.
3220 */
3221 static tabpage_T *
3222alloc_tabpage()
3223{
3224 tabpage_T *tp;
3225
3226 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3227 if (tp != NULL)
3228 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003229# ifdef FEAT_GUI
3230 int i;
3231
3232 for (i = 0; i < 3; i++)
3233 tp->tp_prev_which_scrollbars[i] = -1;
3234# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003235# ifdef FEAT_DIFF
3236 tp->tp_diff_invalid = TRUE;
3237# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003238#ifdef FEAT_EVAL
3239 /* init t: variables */
3240 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3241#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003242 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003243 }
3244 return tp;
3245}
3246
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003247 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003248free_tabpage(tp)
3249 tabpage_T *tp;
3250{
3251# ifdef FEAT_DIFF
3252 diff_clear(tp);
3253# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003254 clear_snapshot(tp);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003255#ifdef FEAT_EVAL
3256 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3257#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003258 vim_free(tp);
3259}
3260
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003261/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003262 * Create a new Tab page with one window.
3263 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003264 * When "after" is 0 put it just after the current Tab page.
3265 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003266 * Return FAIL or OK.
3267 */
3268 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003269win_new_tabpage(after)
3270 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003271{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003272 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003273 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003274 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003275
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003276 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003277 if (newtp == NULL)
3278 return FAIL;
3279
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003280 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003281 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003282 {
3283 vim_free(newtp);
3284 return FAIL;
3285 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003286 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003287
3288 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003289 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003290 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003291 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003292 if (after == 1)
3293 {
3294 /* New tab page becomes the first one. */
3295 newtp->tp_next = first_tabpage;
3296 first_tabpage = newtp;
3297 }
3298 else
3299 {
3300 if (after > 0)
3301 {
3302 /* Put new tab page before tab page "after". */
3303 n = 2;
3304 for (tp = first_tabpage; tp->tp_next != NULL
3305 && n < after; tp = tp->tp_next)
3306 ++n;
3307 }
3308 newtp->tp_next = tp->tp_next;
3309 tp->tp_next = newtp;
3310 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003311 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003312 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003313 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003314
3315 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003316 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003317
3318#if defined(FEAT_GUI)
3319 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3320 * scrollbars. Have to update them anyway. */
3321 if (gui.in_use && starting == 0)
3322 {
3323 gui_init_which_components(NULL);
3324 gui_update_scrollbars(TRUE);
3325 }
3326 need_mouse_correct = TRUE;
3327#endif
3328
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003329 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003330#ifdef FEAT_AUTOCMD
3331 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3332 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3333#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003334 return OK;
3335 }
3336
3337 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003338 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003339 return FAIL;
3340}
3341
3342/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003343 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3344 * like with ":split".
3345 * Returns OK if a new tab page was created, FAIL otherwise.
3346 */
3347 int
3348may_open_tabpage()
3349{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003350 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003351
Bram Moolenaard326ce82007-03-11 14:48:29 +00003352 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003353 {
3354 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003355 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003356 return win_new_tabpage(n);
3357 }
3358 return FAIL;
3359}
3360
3361/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003362 * Create up to "maxcount" tabpages with empty windows.
3363 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003364 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003365 int
3366make_tabpages(maxcount)
3367 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003368{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003369 int count = maxcount;
3370 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003371
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003372 /* Limit to 'tabpagemax' tabs. */
3373 if (count > p_tpm)
3374 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003375
3376#ifdef FEAT_AUTOCMD
3377 /*
3378 * Don't execute autocommands while creating the tab pages. Must do that
3379 * when putting the buffers in the windows.
3380 */
3381 ++autocmd_block;
3382#endif
3383
3384 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003385 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003386 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003387
3388#ifdef FEAT_AUTOCMD
3389 --autocmd_block;
3390#endif
3391
3392 /* return actual number of tab pages */
3393 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003394}
3395
3396/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003397 * Return TRUE when "tpc" points to a valid tab page.
3398 */
3399 int
3400valid_tabpage(tpc)
3401 tabpage_T *tpc;
3402{
3403 tabpage_T *tp;
3404
3405 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3406 if (tp == tpc)
3407 return TRUE;
3408 return FALSE;
3409}
3410
3411/*
3412 * Find tab page "n" (first one is 1). Returns NULL when not found.
3413 */
3414 tabpage_T *
3415find_tabpage(n)
3416 int n;
3417{
3418 tabpage_T *tp;
3419 int i = 1;
3420
3421 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3422 ++i;
3423 return tp;
3424}
3425
3426/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003427 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003428 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003429 */
3430 int
3431tabpage_index(ftp)
3432 tabpage_T *ftp;
3433{
3434 int i = 1;
3435 tabpage_T *tp;
3436
3437 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3438 ++i;
3439 return i;
3440}
3441
3442/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003443 * Prepare for leaving the current tab page.
3444 * When autocomands change "curtab" we don't leave the tab page and return
3445 * FAIL.
3446 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003447 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003448/*ARGSUSED*/
3449 static int
3450leave_tabpage(new_curbuf)
3451 buf_T *new_curbuf; /* what is going to be the new curbuf,
3452 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003453{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003454 tabpage_T *tp = curtab;
3455
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003456#ifdef FEAT_VISUAL
3457 reset_VIsual_and_resel(); /* stop Visual mode */
3458#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003459#ifdef FEAT_AUTOCMD
3460 if (new_curbuf != curbuf)
3461 {
3462 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3463 if (curtab != tp)
3464 return FAIL;
3465 }
3466 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3467 if (curtab != tp)
3468 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003469 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003470 if (curtab != tp)
3471 return FAIL;
3472#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003473#if defined(FEAT_GUI)
3474 /* Remove the scrollbars. They may be added back later. */
3475 if (gui.in_use)
3476 gui_remove_scrollbars();
3477#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003478 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003479 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003480 tp->tp_firstwin = firstwin;
3481 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003482 tp->tp_old_Rows = Rows;
3483 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003484 firstwin = NULL;
3485 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003486 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003487}
3488
3489/*
3490 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003491 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003492 */
3493/*ARGSUSED*/
3494 static void
3495enter_tabpage(tp, old_curbuf)
3496 tabpage_T *tp;
3497 buf_T *old_curbuf;
3498{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003499 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003500 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003501
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003502 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003503 firstwin = tp->tp_firstwin;
3504 lastwin = tp->tp_lastwin;
3505 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003506
3507 /* We would like doing the TabEnter event first, but we don't have a
3508 * valid current window yet, which may break some commands.
3509 * This triggers autocommands, thus may make "tp" invalid. */
3510 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3511 prevwin = next_prevwin;
3512
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003513#ifdef FEAT_AUTOCMD
3514 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003515
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003516 if (old_curbuf != curbuf)
3517 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3518#endif
3519
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003520 last_status(FALSE); /* status line may appear or disappear */
3521 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003522 must_redraw = CLEAR; /* need to redraw everything */
3523#ifdef FEAT_DIFF
3524 diff_need_scrollbind = TRUE;
3525#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003526
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003527 /* The tabpage line may have appeared or disappeared, may need to resize
3528 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003529 * frame sizes too. Use the stored value of p_ch, so that it can be
3530 * different for each tab page. */
3531 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003532 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3533#ifdef FEAT_GUI_TABLINE
3534 && !gui_use_tabline()
3535#endif
3536 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003537 shell_new_rows();
3538#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003539 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003540 shell_new_columns(); /* update window widths */
3541#endif
3542
3543#if defined(FEAT_GUI)
3544 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3545 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003546 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003547 {
3548 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003549 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003550 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003551 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003552#endif
3553
3554 redraw_all_later(CLEAR);
3555}
3556
3557/*
3558 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003559 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003560 */
3561 void
3562goto_tabpage(n)
3563 int n;
3564{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003565 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003566 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003567 int i;
3568
Bram Moolenaard68071d2006-05-02 22:08:30 +00003569 if (text_locked())
3570 {
3571 /* Not allowed when editing the command line. */
3572#ifdef FEAT_CMDWIN
3573 if (cmdwin_type != 0)
3574 EMSG(_(e_cmdwin));
3575 else
3576#endif
3577 EMSG(_(e_secure));
3578 return;
3579 }
3580
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003581 /* If there is only one it can't work. */
3582 if (first_tabpage->tp_next == NULL)
3583 {
3584 if (n > 1)
3585 beep_flush();
3586 return;
3587 }
3588
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003589 if (n == 0)
3590 {
3591 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003592 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003593 tp = first_tabpage;
3594 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003595 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003596 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003597 else if (n < 0)
3598 {
3599 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3600 * this N times. */
3601 ttp = curtab;
3602 for (i = n; i < 0; ++i)
3603 {
3604 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3605 tp = tp->tp_next)
3606 ;
3607 ttp = tp;
3608 }
3609 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003610 else if (n == 9999)
3611 {
3612 /* Go to last tab page. */
3613 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3614 ;
3615 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003616 else
3617 {
3618 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003619 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003620 if (tp == NULL)
3621 {
3622 beep_flush();
3623 return;
3624 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003625 }
3626
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003627 goto_tabpage_tp(tp);
3628
3629#ifdef FEAT_GUI_TABLINE
3630 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003631 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003632#endif
3633}
3634
3635/*
3636 * Go to tabpage "tp".
3637 * Note: doesn't update the GUI tab.
3638 */
3639 void
3640goto_tabpage_tp(tp)
3641 tabpage_T *tp;
3642{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003643 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003644 {
3645 if (valid_tabpage(tp))
3646 enter_tabpage(tp, curbuf);
3647 else
3648 enter_tabpage(curtab, curbuf);
3649 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003650}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
3652/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003653 * Enter window "wp" in tab page "tp".
3654 * Also updates the GUI tab.
3655 */
3656 void
3657goto_tabpage_win(tp, wp)
3658 tabpage_T *tp;
3659 win_T *wp;
3660{
3661 goto_tabpage_tp(tp);
3662 if (curtab == tp && win_valid(wp))
3663 {
3664 win_enter(wp, TRUE);
3665# ifdef FEAT_GUI_TABLINE
3666 if (gui_use_tabline())
3667 gui_mch_set_curtab(tabpage_index(curtab));
3668# endif
3669 }
3670}
3671
3672/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003673 * Move the current tab page to before tab page "nr".
3674 */
3675 void
3676tabpage_move(nr)
3677 int nr;
3678{
3679 int n = nr;
3680 tabpage_T *tp;
3681
3682 if (first_tabpage->tp_next == NULL)
3683 return;
3684
3685 /* Remove the current tab page from the list of tab pages. */
3686 if (curtab == first_tabpage)
3687 first_tabpage = curtab->tp_next;
3688 else
3689 {
3690 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3691 if (tp->tp_next == curtab)
3692 break;
3693 if (tp == NULL) /* "cannot happen" */
3694 return;
3695 tp->tp_next = curtab->tp_next;
3696 }
3697
3698 /* Re-insert it at the specified position. */
3699 if (n == 0)
3700 {
3701 curtab->tp_next = first_tabpage;
3702 first_tabpage = curtab;
3703 }
3704 else
3705 {
3706 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3707 --n;
3708 curtab->tp_next = tp->tp_next;
3709 tp->tp_next = curtab;
3710 }
3711
3712 /* Need to redraw the tabline. Tab page contents doesn't change. */
3713 redraw_tabline = TRUE;
3714}
3715
3716
3717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 * Go to another window.
3719 * When jumping to another buffer, stop Visual mode. Do this before
3720 * changing windows so we can yank the selection into the '*' register.
3721 * When jumping to another window on the same buffer, adjust its cursor
3722 * position to keep the same Visual area.
3723 */
3724 void
3725win_goto(wp)
3726 win_T *wp;
3727{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003728 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 {
3730 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003731 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 return;
3733 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003734#ifdef FEAT_AUTOCMD
3735 if (curbuf_locked())
3736 return;
3737#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003738
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739#ifdef FEAT_VISUAL
3740 if (wp->w_buffer != curbuf)
3741 reset_VIsual_and_resel();
3742 else if (VIsual_active)
3743 wp->w_cursor = curwin->w_cursor;
3744#endif
3745
3746#ifdef FEAT_GUI
3747 need_mouse_correct = TRUE;
3748#endif
3749 win_enter(wp, TRUE);
3750}
3751
3752#if defined(FEAT_PERL) || defined(PROTO)
3753/*
3754 * Find window number "winnr" (counting top to bottom).
3755 */
3756 win_T *
3757win_find_nr(winnr)
3758 int winnr;
3759{
3760 win_T *wp;
3761
3762# ifdef FEAT_WINDOWS
3763 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3764 if (--winnr == 0)
3765 break;
3766 return wp;
3767# else
3768 return curwin;
3769# endif
3770}
3771#endif
3772
3773#ifdef FEAT_VERTSPLIT
3774/*
3775 * Move to window above or below "count" times.
3776 */
3777 static void
3778win_goto_ver(up, count)
3779 int up; /* TRUE to go to win above */
3780 long count;
3781{
3782 frame_T *fr;
3783 frame_T *nfr;
3784 frame_T *foundfr;
3785
3786 foundfr = curwin->w_frame;
3787 while (count--)
3788 {
3789 /*
3790 * First go upwards in the tree of frames until we find a upwards or
3791 * downwards neighbor.
3792 */
3793 fr = foundfr;
3794 for (;;)
3795 {
3796 if (fr == topframe)
3797 goto end;
3798 if (up)
3799 nfr = fr->fr_prev;
3800 else
3801 nfr = fr->fr_next;
3802 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3803 break;
3804 fr = fr->fr_parent;
3805 }
3806
3807 /*
3808 * Now go downwards to find the bottom or top frame in it.
3809 */
3810 for (;;)
3811 {
3812 if (nfr->fr_layout == FR_LEAF)
3813 {
3814 foundfr = nfr;
3815 break;
3816 }
3817 fr = nfr->fr_child;
3818 if (nfr->fr_layout == FR_ROW)
3819 {
3820 /* Find the frame at the cursor row. */
3821 while (fr->fr_next != NULL
3822 && frame2win(fr)->w_wincol + fr->fr_width
3823 <= curwin->w_wincol + curwin->w_wcol)
3824 fr = fr->fr_next;
3825 }
3826 if (nfr->fr_layout == FR_COL && up)
3827 while (fr->fr_next != NULL)
3828 fr = fr->fr_next;
3829 nfr = fr;
3830 }
3831 }
3832end:
3833 if (foundfr != NULL)
3834 win_goto(foundfr->fr_win);
3835}
3836
3837/*
3838 * Move to left or right window.
3839 */
3840 static void
3841win_goto_hor(left, count)
3842 int left; /* TRUE to go to left win */
3843 long count;
3844{
3845 frame_T *fr;
3846 frame_T *nfr;
3847 frame_T *foundfr;
3848
3849 foundfr = curwin->w_frame;
3850 while (count--)
3851 {
3852 /*
3853 * First go upwards in the tree of frames until we find a left or
3854 * right neighbor.
3855 */
3856 fr = foundfr;
3857 for (;;)
3858 {
3859 if (fr == topframe)
3860 goto end;
3861 if (left)
3862 nfr = fr->fr_prev;
3863 else
3864 nfr = fr->fr_next;
3865 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3866 break;
3867 fr = fr->fr_parent;
3868 }
3869
3870 /*
3871 * Now go downwards to find the leftmost or rightmost frame in it.
3872 */
3873 for (;;)
3874 {
3875 if (nfr->fr_layout == FR_LEAF)
3876 {
3877 foundfr = nfr;
3878 break;
3879 }
3880 fr = nfr->fr_child;
3881 if (nfr->fr_layout == FR_COL)
3882 {
3883 /* Find the frame at the cursor row. */
3884 while (fr->fr_next != NULL
3885 && frame2win(fr)->w_winrow + fr->fr_height
3886 <= curwin->w_winrow + curwin->w_wrow)
3887 fr = fr->fr_next;
3888 }
3889 if (nfr->fr_layout == FR_ROW && left)
3890 while (fr->fr_next != NULL)
3891 fr = fr->fr_next;
3892 nfr = fr;
3893 }
3894 }
3895end:
3896 if (foundfr != NULL)
3897 win_goto(foundfr->fr_win);
3898}
3899#endif
3900
3901/*
3902 * Make window "wp" the current window.
3903 */
3904 void
3905win_enter(wp, undo_sync)
3906 win_T *wp;
3907 int undo_sync;
3908{
3909 win_enter_ext(wp, undo_sync, FALSE);
3910}
3911
3912/*
3913 * Make window wp the current window.
3914 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3915 * been closed and isn't valid.
3916 */
3917 static void
3918win_enter_ext(wp, undo_sync, curwin_invalid)
3919 win_T *wp;
3920 int undo_sync;
3921 int curwin_invalid;
3922{
3923#ifdef FEAT_AUTOCMD
3924 int other_buffer = FALSE;
3925#endif
3926
3927 if (wp == curwin && !curwin_invalid) /* nothing to do */
3928 return;
3929
3930#ifdef FEAT_AUTOCMD
3931 if (!curwin_invalid)
3932 {
3933 /*
3934 * Be careful: If autocommands delete the window, return now.
3935 */
3936 if (wp->w_buffer != curbuf)
3937 {
3938 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3939 other_buffer = TRUE;
3940 if (!win_valid(wp))
3941 return;
3942 }
3943 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3944 if (!win_valid(wp))
3945 return;
3946# ifdef FEAT_EVAL
3947 /* autocmds may abort script processing */
3948 if (aborting())
3949 return;
3950# endif
3951 }
3952#endif
3953
3954 /* sync undo before leaving the current buffer */
3955 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003956 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 /* may have to copy the buffer options when 'cpo' contains 'S' */
3958 if (wp->w_buffer != curbuf)
3959 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3960 if (!curwin_invalid)
3961 {
3962 prevwin = curwin; /* remember for CTRL-W p */
3963 curwin->w_redr_status = TRUE;
3964 }
3965 curwin = wp;
3966 curbuf = wp->w_buffer;
3967 check_cursor();
3968#ifdef FEAT_VIRTUALEDIT
3969 if (!virtual_active())
3970 curwin->w_cursor.coladd = 0;
3971#endif
3972 changed_line_abv_curs(); /* assume cursor position needs updating */
3973
3974 if (curwin->w_localdir != NULL)
3975 {
3976 /* Window has a local directory: Save current directory as global
3977 * directory (unless that was done already) and change to the local
3978 * directory. */
3979 if (globaldir == NULL)
3980 {
3981 char_u cwd[MAXPATHL];
3982
3983 if (mch_dirname(cwd, MAXPATHL) == OK)
3984 globaldir = vim_strsave(cwd);
3985 }
3986 mch_chdir((char *)curwin->w_localdir);
3987 shorten_fnames(TRUE);
3988 }
3989 else if (globaldir != NULL)
3990 {
3991 /* Window doesn't have a local directory and we are not in the global
3992 * directory: Change to the global directory. */
3993 mch_chdir((char *)globaldir);
3994 vim_free(globaldir);
3995 globaldir = NULL;
3996 shorten_fnames(TRUE);
3997 }
3998
3999#ifdef FEAT_AUTOCMD
4000 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4001 if (other_buffer)
4002 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4003#endif
4004
4005#ifdef FEAT_TITLE
4006 maketitle();
4007#endif
4008 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004009 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 if (restart_edit)
4011 redraw_later(VALID); /* causes status line redraw */
4012
4013 /* set window height to desired minimal value */
4014 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4015 win_setheight((int)p_wh);
4016 else if (curwin->w_height == 0)
4017 win_setheight(1);
4018
4019#ifdef FEAT_VERTSPLIT
4020 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004021 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 win_setwidth((int)p_wiw);
4023#endif
4024
4025#ifdef FEAT_MOUSE
4026 setmouse(); /* in case jumped to/from help buffer */
4027#endif
4028
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004029 /* Change directories when the 'acd' option is set. */
4030 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031}
4032
4033#endif /* FEAT_WINDOWS */
4034
4035#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4036/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004037 * Jump to the first open window that contains buffer "buf", if one exists.
4038 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 */
4040 win_T *
4041buf_jump_open_win(buf)
4042 buf_T *buf;
4043{
4044# ifdef FEAT_WINDOWS
4045 win_T *wp;
4046
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004047 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 if (wp->w_buffer == buf)
4049 break;
4050 if (wp != NULL)
4051 win_enter(wp, FALSE);
4052 return wp;
4053# else
4054 if (curwin->w_buffer == buf)
4055 return curwin;
4056 return NULL;
4057# endif
4058}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004059
4060/*
4061 * Jump to the first open window in any tab page that contains buffer "buf",
4062 * if one exists.
4063 * Returns a pointer to the window found, otherwise NULL.
4064 */
4065 win_T *
4066buf_jump_open_tab(buf)
4067 buf_T *buf;
4068{
4069# ifdef FEAT_WINDOWS
4070 win_T *wp;
4071 tabpage_T *tp;
4072
4073 /* First try the current tab page. */
4074 wp = buf_jump_open_win(buf);
4075 if (wp != NULL)
4076 return wp;
4077
4078 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4079 if (tp != curtab)
4080 {
4081 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4082 if (wp->w_buffer == buf)
4083 break;
4084 if (wp != NULL)
4085 {
4086 goto_tabpage_win(tp, wp);
4087 if (curwin != wp)
4088 wp = NULL; /* something went wrong */
4089 break;
4090 }
4091 }
4092
4093 return wp;
4094# else
4095 if (curwin->w_buffer == buf)
4096 return curwin;
4097 return NULL;
4098# endif
4099}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100#endif
4101
4102/*
4103 * allocate a window structure and link it in the window list
4104 */
4105/*ARGSUSED*/
4106 static win_T *
4107win_alloc(after)
4108 win_T *after;
4109{
4110 win_T *newwin;
4111
4112 /*
4113 * allocate window structure and linesizes arrays
4114 */
4115 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4116 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4117 {
4118 vim_free(newwin);
4119 newwin = NULL;
4120 }
4121
4122 if (newwin != NULL)
4123 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004124#ifdef FEAT_AUTOCMD
4125 /* Don't execute autocommands while the window is not properly
4126 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4127 * event. */
4128 ++autocmd_block;
4129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 /*
4131 * link the window in the window list
4132 */
4133#ifdef FEAT_WINDOWS
4134 win_append(after, newwin);
4135#endif
4136#ifdef FEAT_VERTSPLIT
4137 newwin->w_wincol = 0;
4138 newwin->w_width = Columns;
4139#endif
4140
4141 /* position the display and the cursor at the top of the file. */
4142 newwin->w_topline = 1;
4143#ifdef FEAT_DIFF
4144 newwin->w_topfill = 0;
4145#endif
4146 newwin->w_botline = 2;
4147 newwin->w_cursor.lnum = 1;
4148#ifdef FEAT_SCROLLBIND
4149 newwin->w_scbind_pos = 1;
4150#endif
4151
4152 /* We won't calculate w_fraction until resizing the window */
4153 newwin->w_fraction = 0;
4154 newwin->w_prev_fraction_row = -1;
4155
4156#ifdef FEAT_GUI
4157 if (gui.in_use)
4158 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4160 SBAR_LEFT, newwin);
4161 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4162 SBAR_RIGHT, newwin);
4163 }
4164#endif
4165#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004166 /* init w: variables */
4167 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168#endif
4169#ifdef FEAT_FOLDING
4170 foldInitWin(newwin);
4171#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004172#ifdef FEAT_AUTOCMD
4173 --autocmd_block;
4174#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004175#ifdef FEAT_SEARCH_EXTRA
4176 newwin->w_match_head = NULL;
4177 newwin->w_next_match_id = 4;
4178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
4180 return newwin;
4181}
4182
4183#if defined(FEAT_WINDOWS) || defined(PROTO)
4184
4185/*
4186 * remove window 'wp' from the window list and free the structure
4187 */
4188 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004189win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004191 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192{
4193 int i;
4194
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004195#ifdef FEAT_AUTOCMD
4196 /* Don't execute autocommands while the window is halfway being deleted.
4197 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
4198 ++autocmd_block;
4199#endif
4200
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004201#ifdef FEAT_MZSCHEME
4202 mzscheme_window_free(wp);
4203#endif
4204
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205#ifdef FEAT_PERL
4206 perl_win_free(wp);
4207#endif
4208
4209#ifdef FEAT_PYTHON
4210 python_window_free(wp);
4211#endif
4212
4213#ifdef FEAT_TCL
4214 tcl_window_free(wp);
4215#endif
4216
4217#ifdef FEAT_RUBY
4218 ruby_window_free(wp);
4219#endif
4220
4221 clear_winopt(&wp->w_onebuf_opt);
4222 clear_winopt(&wp->w_allbuf_opt);
4223
4224#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004225 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226#endif
4227
4228 if (prevwin == wp)
4229 prevwin = NULL;
4230 win_free_lsize(wp);
4231
4232 for (i = 0; i < wp->w_tagstacklen; ++i)
4233 vim_free(wp->w_tagstack[i].tagname);
4234
4235 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004236
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004238 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004240
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241#ifdef FEAT_JUMPLIST
4242 free_jumplist(wp);
4243#endif
4244
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004245#ifdef FEAT_QUICKFIX
4246 qf_free_all(wp);
4247#endif
4248
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249#ifdef FEAT_GUI
4250 if (gui.in_use)
4251 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4253 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4254 }
4255#endif /* FEAT_GUI */
4256
Bram Moolenaarf740b292006-02-16 22:11:02 +00004257 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004259
4260#ifdef FEAT_AUTOCMD
4261 --autocmd_block;
4262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263}
4264
4265/*
4266 * Append window "wp" in the window list after window "after".
4267 */
4268 static void
4269win_append(after, wp)
4270 win_T *after, *wp;
4271{
4272 win_T *before;
4273
4274 if (after == NULL) /* after NULL is in front of the first */
4275 before = firstwin;
4276 else
4277 before = after->w_next;
4278
4279 wp->w_next = before;
4280 wp->w_prev = after;
4281 if (after == NULL)
4282 firstwin = wp;
4283 else
4284 after->w_next = wp;
4285 if (before == NULL)
4286 lastwin = wp;
4287 else
4288 before->w_prev = wp;
4289}
4290
4291/*
4292 * Remove a window from the window list.
4293 */
4294 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004295win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004297 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298{
4299 if (wp->w_prev != NULL)
4300 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004301 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004303 else
4304 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 if (wp->w_next != NULL)
4306 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004307 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004309 else
4310 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311}
4312
4313/*
4314 * Append frame "frp" in a frame list after frame "after".
4315 */
4316 static void
4317frame_append(after, frp)
4318 frame_T *after, *frp;
4319{
4320 frp->fr_next = after->fr_next;
4321 after->fr_next = frp;
4322 if (frp->fr_next != NULL)
4323 frp->fr_next->fr_prev = frp;
4324 frp->fr_prev = after;
4325}
4326
4327/*
4328 * Insert frame "frp" in a frame list before frame "before".
4329 */
4330 static void
4331frame_insert(before, frp)
4332 frame_T *before, *frp;
4333{
4334 frp->fr_next = before;
4335 frp->fr_prev = before->fr_prev;
4336 before->fr_prev = frp;
4337 if (frp->fr_prev != NULL)
4338 frp->fr_prev->fr_next = frp;
4339 else
4340 frp->fr_parent->fr_child = frp;
4341}
4342
4343/*
4344 * Remove a frame from a frame list.
4345 */
4346 static void
4347frame_remove(frp)
4348 frame_T *frp;
4349{
4350 if (frp->fr_prev != NULL)
4351 frp->fr_prev->fr_next = frp->fr_next;
4352 else
4353 frp->fr_parent->fr_child = frp->fr_next;
4354 if (frp->fr_next != NULL)
4355 frp->fr_next->fr_prev = frp->fr_prev;
4356}
4357
4358#endif /* FEAT_WINDOWS */
4359
4360/*
4361 * Allocate w_lines[] for window "wp".
4362 * Return FAIL for failure, OK for success.
4363 */
4364 int
4365win_alloc_lines(wp)
4366 win_T *wp;
4367{
4368 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004369 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 if (wp->w_lines == NULL)
4371 return FAIL;
4372 return OK;
4373}
4374
4375/*
4376 * free lsize arrays for a window
4377 */
4378 void
4379win_free_lsize(wp)
4380 win_T *wp;
4381{
4382 vim_free(wp->w_lines);
4383 wp->w_lines = NULL;
4384}
4385
4386/*
4387 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004388 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 */
4390 void
4391shell_new_rows()
4392{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004393 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394
4395 if (firstwin == NULL) /* not initialized yet */
4396 return;
4397#ifdef FEAT_WINDOWS
4398 if (h < frame_minheight(topframe, NULL))
4399 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004400
4401 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 * that doesn't result in the right height, forget about that option. */
4403 frame_new_height(topframe, h, FALSE, TRUE);
4404 if (topframe->fr_height != h)
4405 frame_new_height(topframe, h, FALSE, FALSE);
4406
4407 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4408#else
4409 if (h < 1)
4410 h = 1;
4411 win_new_height(firstwin, h);
4412#endif
4413 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004414#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004415 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004416#endif
4417
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418#if 0
4419 /* Disabled: don't want making the screen smaller make a window larger. */
4420 if (p_ea)
4421 win_equal(curwin, FALSE, 'v');
4422#endif
4423}
4424
4425#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4426/*
4427 * Called from win_new_shellsize() after Columns changed.
4428 */
4429 void
4430shell_new_columns()
4431{
4432 if (firstwin == NULL) /* not initialized yet */
4433 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004434
4435 /* First try setting the widths of windows with 'winfixwidth'. If that
4436 * doesn't result in the right width, forget about that option. */
4437 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4438 if (topframe->fr_width != Columns)
4439 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4440
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4442#if 0
4443 /* Disabled: don't want making the screen smaller make a window larger. */
4444 if (p_ea)
4445 win_equal(curwin, FALSE, 'h');
4446#endif
4447}
4448#endif
4449
4450#if defined(FEAT_CMDWIN) || defined(PROTO)
4451/*
4452 * Save the size of all windows in "gap".
4453 */
4454 void
4455win_size_save(gap)
4456 garray_T *gap;
4457
4458{
4459 win_T *wp;
4460
4461 ga_init2(gap, (int)sizeof(int), 1);
4462 if (ga_grow(gap, win_count() * 2) == OK)
4463 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4464 {
4465 ((int *)gap->ga_data)[gap->ga_len++] =
4466 wp->w_width + wp->w_vsep_width;
4467 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4468 }
4469}
4470
4471/*
4472 * Restore window sizes, but only if the number of windows is still the same.
4473 * Does not free the growarray.
4474 */
4475 void
4476win_size_restore(gap)
4477 garray_T *gap;
4478{
4479 win_T *wp;
4480 int i;
4481
4482 if (win_count() * 2 == gap->ga_len)
4483 {
4484 i = 0;
4485 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4486 {
4487 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4488 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4489 }
4490 /* recompute the window positions */
4491 (void)win_comp_pos();
4492 }
4493}
4494#endif /* FEAT_CMDWIN */
4495
4496#if defined(FEAT_WINDOWS) || defined(PROTO)
4497/*
4498 * Update the position for all windows, using the width and height of the
4499 * frames.
4500 * Returns the row just after the last window.
4501 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004502 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503win_comp_pos()
4504{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004505 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 int col = 0;
4507
4508 frame_comp_pos(topframe, &row, &col);
4509 return row;
4510}
4511
4512/*
4513 * Update the position of the windows in frame "topfrp", using the width and
4514 * height of the frames.
4515 * "*row" and "*col" are the top-left position of the frame. They are updated
4516 * to the bottom-right position plus one.
4517 */
4518 static void
4519frame_comp_pos(topfrp, row, col)
4520 frame_T *topfrp;
4521 int *row;
4522 int *col;
4523{
4524 win_T *wp;
4525 frame_T *frp;
4526#ifdef FEAT_VERTSPLIT
4527 int startcol;
4528 int startrow;
4529#endif
4530
4531 wp = topfrp->fr_win;
4532 if (wp != NULL)
4533 {
4534 if (wp->w_winrow != *row
4535#ifdef FEAT_VERTSPLIT
4536 || wp->w_wincol != *col
4537#endif
4538 )
4539 {
4540 /* position changed, redraw */
4541 wp->w_winrow = *row;
4542#ifdef FEAT_VERTSPLIT
4543 wp->w_wincol = *col;
4544#endif
4545 redraw_win_later(wp, NOT_VALID);
4546 wp->w_redr_status = TRUE;
4547 }
4548 *row += wp->w_height + wp->w_status_height;
4549#ifdef FEAT_VERTSPLIT
4550 *col += wp->w_width + wp->w_vsep_width;
4551#endif
4552 }
4553 else
4554 {
4555#ifdef FEAT_VERTSPLIT
4556 startrow = *row;
4557 startcol = *col;
4558#endif
4559 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4560 {
4561#ifdef FEAT_VERTSPLIT
4562 if (topfrp->fr_layout == FR_ROW)
4563 *row = startrow; /* all frames are at the same row */
4564 else
4565 *col = startcol; /* all frames are at the same col */
4566#endif
4567 frame_comp_pos(frp, row, col);
4568 }
4569 }
4570}
4571
4572#endif /* FEAT_WINDOWS */
4573
4574/*
4575 * Set current window height and take care of repositioning other windows to
4576 * fit around it.
4577 */
4578 void
4579win_setheight(height)
4580 int height;
4581{
4582 win_setheight_win(height, curwin);
4583}
4584
4585/*
4586 * Set the window height of window "win" and take care of repositioning other
4587 * windows to fit around it.
4588 */
4589 void
4590win_setheight_win(height, win)
4591 int height;
4592 win_T *win;
4593{
4594 int row;
4595
4596 if (win == curwin)
4597 {
4598 /* Always keep current window at least one line high, even when
4599 * 'winminheight' is zero. */
4600#ifdef FEAT_WINDOWS
4601 if (height < p_wmh)
4602 height = p_wmh;
4603#endif
4604 if (height == 0)
4605 height = 1;
4606 }
4607
4608#ifdef FEAT_WINDOWS
4609 frame_setheight(win->w_frame, height + win->w_status_height);
4610
4611 /* recompute the window positions */
4612 row = win_comp_pos();
4613#else
4614 if (height > topframe->fr_height)
4615 height = topframe->fr_height;
4616 win->w_height = height;
4617 row = height;
4618#endif
4619
4620 /*
4621 * If there is extra space created between the last window and the command
4622 * line, clear it.
4623 */
4624 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4625 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4626 cmdline_row = row;
4627 msg_row = row;
4628 msg_col = 0;
4629
4630 redraw_all_later(NOT_VALID);
4631}
4632
4633#if defined(FEAT_WINDOWS) || defined(PROTO)
4634
4635/*
4636 * Set the height of a frame to "height" and take care that all frames and
4637 * windows inside it are resized. Also resize frames on the left and right if
4638 * the are in the same FR_ROW frame.
4639 *
4640 * Strategy:
4641 * If the frame is part of a FR_COL frame, try fitting the frame in that
4642 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4643 * go to containing frames to resize them and make room.
4644 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4645 * Check for the minimal height of the FR_ROW frame.
4646 * At the top level we can also use change the command line height.
4647 */
4648 static void
4649frame_setheight(curfrp, height)
4650 frame_T *curfrp;
4651 int height;
4652{
4653 int room; /* total number of lines available */
4654 int take; /* number of lines taken from other windows */
4655 int room_cmdline; /* lines available from cmdline */
4656 int run;
4657 frame_T *frp;
4658 int h;
4659 int room_reserved;
4660
4661 /* If the height already is the desired value, nothing to do. */
4662 if (curfrp->fr_height == height)
4663 return;
4664
4665 if (curfrp->fr_parent == NULL)
4666 {
4667 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004668 if (height > ROWS_AVAIL)
4669 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 if (height > 0)
4671 frame_new_height(curfrp, height, FALSE, FALSE);
4672 }
4673 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4674 {
4675 /* Row of frames: Also need to resize frames left and right of this
4676 * one. First check for the minimal height of these. */
4677 h = frame_minheight(curfrp->fr_parent, NULL);
4678 if (height < h)
4679 height = h;
4680 frame_setheight(curfrp->fr_parent, height);
4681 }
4682 else
4683 {
4684 /*
4685 * Column of frames: try to change only frames in this column.
4686 */
4687#ifdef FEAT_VERTSPLIT
4688 /*
4689 * Do this twice:
4690 * 1: compute room available, if it's not enough try resizing the
4691 * containing frame.
4692 * 2: compute the room available and adjust the height to it.
4693 * Try not to reduce the height of a window with 'winfixheight' set.
4694 */
4695 for (run = 1; run <= 2; ++run)
4696#else
4697 for (;;)
4698#endif
4699 {
4700 room = 0;
4701 room_reserved = 0;
4702 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4703 frp = frp->fr_next)
4704 {
4705 if (frp != curfrp
4706 && frp->fr_win != NULL
4707 && frp->fr_win->w_p_wfh)
4708 room_reserved += frp->fr_height;
4709 room += frp->fr_height;
4710 if (frp != curfrp)
4711 room -= frame_minheight(frp, NULL);
4712 }
4713#ifdef FEAT_VERTSPLIT
4714 if (curfrp->fr_width != Columns)
4715 room_cmdline = 0;
4716 else
4717#endif
4718 {
4719 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4720 + lastwin->w_height + lastwin->w_status_height);
4721 if (room_cmdline < 0)
4722 room_cmdline = 0;
4723 }
4724
4725 if (height <= room + room_cmdline)
4726 break;
4727#ifdef FEAT_VERTSPLIT
4728 if (run == 2 || curfrp->fr_width == Columns)
4729#endif
4730 {
4731 if (height > room + room_cmdline)
4732 height = room + room_cmdline;
4733 break;
4734 }
4735#ifdef FEAT_VERTSPLIT
4736 frame_setheight(curfrp->fr_parent, height
4737 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4738#endif
4739 /*NOTREACHED*/
4740 }
4741
4742 /*
4743 * Compute the number of lines we will take from others frames (can be
4744 * negative!).
4745 */
4746 take = height - curfrp->fr_height;
4747
4748 /* If there is not enough room, also reduce the height of a window
4749 * with 'winfixheight' set. */
4750 if (height > room + room_cmdline - room_reserved)
4751 room_reserved = room + room_cmdline - height;
4752 /* If there is only a 'winfixheight' window and making the
4753 * window smaller, need to make the other window taller. */
4754 if (take < 0 && room - curfrp->fr_height < room_reserved)
4755 room_reserved = 0;
4756
4757 if (take > 0 && room_cmdline > 0)
4758 {
4759 /* use lines from cmdline first */
4760 if (take < room_cmdline)
4761 room_cmdline = take;
4762 take -= room_cmdline;
4763 topframe->fr_height += room_cmdline;
4764 }
4765
4766 /*
4767 * set the current frame to the new height
4768 */
4769 frame_new_height(curfrp, height, FALSE, FALSE);
4770
4771 /*
4772 * First take lines from the frames after the current frame. If
4773 * that is not enough, takes lines from frames above the current
4774 * frame.
4775 */
4776 for (run = 0; run < 2; ++run)
4777 {
4778 if (run == 0)
4779 frp = curfrp->fr_next; /* 1st run: start with next window */
4780 else
4781 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4782 while (frp != NULL && take != 0)
4783 {
4784 h = frame_minheight(frp, NULL);
4785 if (room_reserved > 0
4786 && frp->fr_win != NULL
4787 && frp->fr_win->w_p_wfh)
4788 {
4789 if (room_reserved >= frp->fr_height)
4790 room_reserved -= frp->fr_height;
4791 else
4792 {
4793 if (frp->fr_height - room_reserved > take)
4794 room_reserved = frp->fr_height - take;
4795 take -= frp->fr_height - room_reserved;
4796 frame_new_height(frp, room_reserved, FALSE, FALSE);
4797 room_reserved = 0;
4798 }
4799 }
4800 else
4801 {
4802 if (frp->fr_height - take < h)
4803 {
4804 take -= frp->fr_height - h;
4805 frame_new_height(frp, h, FALSE, FALSE);
4806 }
4807 else
4808 {
4809 frame_new_height(frp, frp->fr_height - take,
4810 FALSE, FALSE);
4811 take = 0;
4812 }
4813 }
4814 if (run == 0)
4815 frp = frp->fr_next;
4816 else
4817 frp = frp->fr_prev;
4818 }
4819 }
4820 }
4821}
4822
4823#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4824/*
4825 * Set current window width and take care of repositioning other windows to
4826 * fit around it.
4827 */
4828 void
4829win_setwidth(width)
4830 int width;
4831{
4832 win_setwidth_win(width, curwin);
4833}
4834
4835 void
4836win_setwidth_win(width, wp)
4837 int width;
4838 win_T *wp;
4839{
4840 /* Always keep current window at least one column wide, even when
4841 * 'winminwidth' is zero. */
4842 if (wp == curwin)
4843 {
4844 if (width < p_wmw)
4845 width = p_wmw;
4846 if (width == 0)
4847 width = 1;
4848 }
4849
4850 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4851
4852 /* recompute the window positions */
4853 (void)win_comp_pos();
4854
4855 redraw_all_later(NOT_VALID);
4856}
4857
4858/*
4859 * Set the width of a frame to "width" and take care that all frames and
4860 * windows inside it are resized. Also resize frames above and below if the
4861 * are in the same FR_ROW frame.
4862 *
4863 * Strategy is similar to frame_setheight().
4864 */
4865 static void
4866frame_setwidth(curfrp, width)
4867 frame_T *curfrp;
4868 int width;
4869{
4870 int room; /* total number of lines available */
4871 int take; /* number of lines taken from other windows */
4872 int run;
4873 frame_T *frp;
4874 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004875 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
4877 /* If the width already is the desired value, nothing to do. */
4878 if (curfrp->fr_width == width)
4879 return;
4880
4881 if (curfrp->fr_parent == NULL)
4882 /* topframe: can't change width */
4883 return;
4884
4885 if (curfrp->fr_parent->fr_layout == FR_COL)
4886 {
4887 /* Column of frames: Also need to resize frames above and below of
4888 * this one. First check for the minimal width of these. */
4889 w = frame_minwidth(curfrp->fr_parent, NULL);
4890 if (width < w)
4891 width = w;
4892 frame_setwidth(curfrp->fr_parent, width);
4893 }
4894 else
4895 {
4896 /*
4897 * Row of frames: try to change only frames in this row.
4898 *
4899 * Do this twice:
4900 * 1: compute room available, if it's not enough try resizing the
4901 * containing frame.
4902 * 2: compute the room available and adjust the width to it.
4903 */
4904 for (run = 1; run <= 2; ++run)
4905 {
4906 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004907 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4909 frp = frp->fr_next)
4910 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004911 if (frp != curfrp
4912 && frp->fr_win != NULL
4913 && frp->fr_win->w_p_wfw)
4914 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 room += frp->fr_width;
4916 if (frp != curfrp)
4917 room -= frame_minwidth(frp, NULL);
4918 }
4919
4920 if (width <= room)
4921 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004922 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
4924 if (width > room)
4925 width = room;
4926 break;
4927 }
4928 frame_setwidth(curfrp->fr_parent, width
4929 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4930 }
4931
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 /*
4933 * Compute the number of lines we will take from others frames (can be
4934 * negative!).
4935 */
4936 take = width - curfrp->fr_width;
4937
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004938 /* If there is not enough room, also reduce the width of a window
4939 * with 'winfixwidth' set. */
4940 if (width > room - room_reserved)
4941 room_reserved = room - width;
4942 /* If there is only a 'winfixwidth' window and making the
4943 * window smaller, need to make the other window narrower. */
4944 if (take < 0 && room - curfrp->fr_width < room_reserved)
4945 room_reserved = 0;
4946
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 /*
4948 * set the current frame to the new width
4949 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004950 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951
4952 /*
4953 * First take lines from the frames right of the current frame. If
4954 * that is not enough, takes lines from frames left of the current
4955 * frame.
4956 */
4957 for (run = 0; run < 2; ++run)
4958 {
4959 if (run == 0)
4960 frp = curfrp->fr_next; /* 1st run: start with next window */
4961 else
4962 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4963 while (frp != NULL && take != 0)
4964 {
4965 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004966 if (room_reserved > 0
4967 && frp->fr_win != NULL
4968 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004970 if (room_reserved >= frp->fr_width)
4971 room_reserved -= frp->fr_width;
4972 else
4973 {
4974 if (frp->fr_width - room_reserved > take)
4975 room_reserved = frp->fr_width - take;
4976 take -= frp->fr_width - room_reserved;
4977 frame_new_width(frp, room_reserved, FALSE, FALSE);
4978 room_reserved = 0;
4979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
4981 else
4982 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004983 if (frp->fr_width - take < w)
4984 {
4985 take -= frp->fr_width - w;
4986 frame_new_width(frp, w, FALSE, FALSE);
4987 }
4988 else
4989 {
4990 frame_new_width(frp, frp->fr_width - take,
4991 FALSE, FALSE);
4992 take = 0;
4993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 }
4995 if (run == 0)
4996 frp = frp->fr_next;
4997 else
4998 frp = frp->fr_prev;
4999 }
5000 }
5001 }
5002}
5003#endif /* FEAT_VERTSPLIT */
5004
5005/*
5006 * Check 'winminheight' for a valid value.
5007 */
5008 void
5009win_setminheight()
5010{
5011 int room;
5012 int first = TRUE;
5013 win_T *wp;
5014
5015 /* loop until there is a 'winminheight' that is possible */
5016 while (p_wmh > 0)
5017 {
5018 /* TODO: handle vertical splits */
5019 room = -p_wh;
5020 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5021 room += wp->w_height - p_wmh;
5022 if (room >= 0)
5023 break;
5024 --p_wmh;
5025 if (first)
5026 {
5027 EMSG(_(e_noroom));
5028 first = FALSE;
5029 }
5030 }
5031}
5032
5033#ifdef FEAT_MOUSE
5034
5035/*
5036 * Status line of dragwin is dragged "offset" lines down (negative is up).
5037 */
5038 void
5039win_drag_status_line(dragwin, offset)
5040 win_T *dragwin;
5041 int offset;
5042{
5043 frame_T *curfr;
5044 frame_T *fr;
5045 int room;
5046 int row;
5047 int up; /* if TRUE, drag status line up, otherwise down */
5048 int n;
5049
5050 fr = dragwin->w_frame;
5051 curfr = fr;
5052 if (fr != topframe) /* more than one window */
5053 {
5054 fr = fr->fr_parent;
5055 /* When the parent frame is not a column of frames, its parent should
5056 * be. */
5057 if (fr->fr_layout != FR_COL)
5058 {
5059 curfr = fr;
5060 if (fr != topframe) /* only a row of windows, may drag statusline */
5061 fr = fr->fr_parent;
5062 }
5063 }
5064
5065 /* If this is the last frame in a column, may want to resize the parent
5066 * frame instead (go two up to skip a row of frames). */
5067 while (curfr != topframe && curfr->fr_next == NULL)
5068 {
5069 if (fr != topframe)
5070 fr = fr->fr_parent;
5071 curfr = fr;
5072 if (fr != topframe)
5073 fr = fr->fr_parent;
5074 }
5075
5076 if (offset < 0) /* drag up */
5077 {
5078 up = TRUE;
5079 offset = -offset;
5080 /* sum up the room of the current frame and above it */
5081 if (fr == curfr)
5082 {
5083 /* only one window */
5084 room = fr->fr_height - frame_minheight(fr, NULL);
5085 }
5086 else
5087 {
5088 room = 0;
5089 for (fr = fr->fr_child; ; fr = fr->fr_next)
5090 {
5091 room += fr->fr_height - frame_minheight(fr, NULL);
5092 if (fr == curfr)
5093 break;
5094 }
5095 }
5096 fr = curfr->fr_next; /* put fr at frame that grows */
5097 }
5098 else /* drag down */
5099 {
5100 up = FALSE;
5101 /*
5102 * Only dragging the last status line can reduce p_ch.
5103 */
5104 room = Rows - cmdline_row;
5105 if (curfr->fr_next == NULL)
5106 room -= 1;
5107 else
5108 room -= p_ch;
5109 if (room < 0)
5110 room = 0;
5111 /* sum up the room of frames below of the current one */
5112 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5113 room += fr->fr_height - frame_minheight(fr, NULL);
5114 fr = curfr; /* put fr at window that grows */
5115 }
5116
5117 if (room < offset) /* Not enough room */
5118 offset = room; /* Move as far as we can */
5119 if (offset <= 0)
5120 return;
5121
5122 /*
5123 * Grow frame fr by "offset" lines.
5124 * Doesn't happen when dragging the last status line up.
5125 */
5126 if (fr != NULL)
5127 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5128
5129 if (up)
5130 fr = curfr; /* current frame gets smaller */
5131 else
5132 fr = curfr->fr_next; /* next frame gets smaller */
5133
5134 /*
5135 * Now make the other frames smaller.
5136 */
5137 while (fr != NULL && offset > 0)
5138 {
5139 n = frame_minheight(fr, NULL);
5140 if (fr->fr_height - offset <= n)
5141 {
5142 offset -= fr->fr_height - n;
5143 frame_new_height(fr, n, !up, FALSE);
5144 }
5145 else
5146 {
5147 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5148 break;
5149 }
5150 if (up)
5151 fr = fr->fr_prev;
5152 else
5153 fr = fr->fr_next;
5154 }
5155 row = win_comp_pos();
5156 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5157 cmdline_row = row;
5158 p_ch = Rows - cmdline_row;
5159 if (p_ch < 1)
5160 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005161 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005162 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 showmode();
5164}
5165
5166#ifdef FEAT_VERTSPLIT
5167/*
5168 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5169 */
5170 void
5171win_drag_vsep_line(dragwin, offset)
5172 win_T *dragwin;
5173 int offset;
5174{
5175 frame_T *curfr;
5176 frame_T *fr;
5177 int room;
5178 int left; /* if TRUE, drag separator line left, otherwise right */
5179 int n;
5180
5181 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005182 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 return;
5184 curfr = fr;
5185 fr = fr->fr_parent;
5186 /* When the parent frame is not a row of frames, its parent should be. */
5187 if (fr->fr_layout != FR_ROW)
5188 {
5189 if (fr == topframe) /* only a column of windows (cannot happen?) */
5190 return;
5191 curfr = fr;
5192 fr = fr->fr_parent;
5193 }
5194
5195 /* If this is the last frame in a row, may want to resize a parent
5196 * frame instead. */
5197 while (curfr->fr_next == NULL)
5198 {
5199 if (fr == topframe)
5200 break;
5201 curfr = fr;
5202 fr = fr->fr_parent;
5203 if (fr != topframe)
5204 {
5205 curfr = fr;
5206 fr = fr->fr_parent;
5207 }
5208 }
5209
5210 if (offset < 0) /* drag left */
5211 {
5212 left = TRUE;
5213 offset = -offset;
5214 /* sum up the room of the current frame and left of it */
5215 room = 0;
5216 for (fr = fr->fr_child; ; fr = fr->fr_next)
5217 {
5218 room += fr->fr_width - frame_minwidth(fr, NULL);
5219 if (fr == curfr)
5220 break;
5221 }
5222 fr = curfr->fr_next; /* put fr at frame that grows */
5223 }
5224 else /* drag right */
5225 {
5226 left = FALSE;
5227 /* sum up the room of frames right of the current one */
5228 room = 0;
5229 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5230 room += fr->fr_width - frame_minwidth(fr, NULL);
5231 fr = curfr; /* put fr at window that grows */
5232 }
5233
5234 if (room < offset) /* Not enough room */
5235 offset = room; /* Move as far as we can */
5236 if (offset <= 0) /* No room at all, quit. */
5237 return;
5238
5239 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005240 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241
5242 /* shrink other frames: current and at the left or at the right */
5243 if (left)
5244 fr = curfr; /* current frame gets smaller */
5245 else
5246 fr = curfr->fr_next; /* next frame gets smaller */
5247
5248 while (fr != NULL && offset > 0)
5249 {
5250 n = frame_minwidth(fr, NULL);
5251 if (fr->fr_width - offset <= n)
5252 {
5253 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005254 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 }
5256 else
5257 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005258 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 break;
5260 }
5261 if (left)
5262 fr = fr->fr_prev;
5263 else
5264 fr = fr->fr_next;
5265 }
5266 (void)win_comp_pos();
5267 redraw_all_later(NOT_VALID);
5268}
5269#endif /* FEAT_VERTSPLIT */
5270#endif /* FEAT_MOUSE */
5271
5272#endif /* FEAT_WINDOWS */
5273
5274/*
5275 * Set the height of a window.
5276 * This takes care of the things inside the window, not what happens to the
5277 * window position, the frame or to other windows.
5278 */
5279 static void
5280win_new_height(wp, height)
5281 win_T *wp;
5282 int height;
5283{
5284 linenr_T lnum;
5285 int sline, line_size;
5286#define FRACTION_MULT 16384L
5287
5288 /* Don't want a negative height. Happens when splitting a tiny window.
5289 * Will equalize heights soon to fix it. */
5290 if (height < 0)
5291 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005292 if (wp->w_height == height)
5293 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294
5295 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5296 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5297 + FRACTION_MULT / 2) / (long)wp->w_height;
5298
5299 wp->w_height = height;
5300 wp->w_skipcol = 0;
5301
5302 /* Don't change w_topline when height is zero. Don't set w_topline when
5303 * 'scrollbind' is set and this isn't the current window. */
5304 if (height > 0
5305#ifdef FEAT_SCROLLBIND
5306 && (!wp->w_p_scb || wp == curwin)
5307#endif
5308 )
5309 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005310 /*
5311 * Find a value for w_topline that shows the cursor at the same
5312 * relative position in the window as before (more or less).
5313 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 lnum = wp->w_cursor.lnum;
5315 if (lnum < 1) /* can happen when starting up */
5316 lnum = 1;
5317 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5318 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5319 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005320
5321 if (sline >= 0)
5322 {
5323 /* Make sure the whole cursor line is visible, if possible. */
5324 int rows = plines_win(wp, lnum, FALSE);
5325
5326 if (sline > wp->w_height - rows)
5327 {
5328 sline = wp->w_height - rows;
5329 wp->w_wrow -= rows - line_size;
5330 }
5331 }
5332
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 if (sline < 0)
5334 {
5335 /*
5336 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005337 * Make cursor line the first line in the window. If not enough
5338 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 */
5340 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005341 if (wp->w_wrow >= wp->w_height
5342 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5343 {
5344 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5345 --wp->w_wrow;
5346 while (wp->w_wrow >= wp->w_height)
5347 {
5348 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5349 + win_col_off2(wp);
5350 --wp->w_wrow;
5351 }
5352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 }
5354 else
5355 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005356 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 {
5358#ifdef FEAT_FOLDING
5359 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5360 if (lnum == 1)
5361 {
5362 /* first line in buffer is folded */
5363 line_size = 1;
5364 --sline;
5365 break;
5366 }
5367#endif
5368 --lnum;
5369#ifdef FEAT_DIFF
5370 if (lnum == wp->w_topline)
5371 line_size = plines_win_nofill(wp, lnum, TRUE)
5372 + wp->w_topfill;
5373 else
5374#endif
5375 line_size = plines_win(wp, lnum, TRUE);
5376 sline -= line_size;
5377 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005378
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 if (sline < 0)
5380 {
5381 /*
5382 * Line we want at top would go off top of screen. Use next
5383 * line instead.
5384 */
5385#ifdef FEAT_FOLDING
5386 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5387#endif
5388 lnum++;
5389 wp->w_wrow -= line_size + sline;
5390 }
5391 else if (sline > 0)
5392 {
5393 /* First line of file reached, use that as topline. */
5394 lnum = 1;
5395 wp->w_wrow -= sline;
5396 }
5397 }
5398 set_topline(wp, lnum);
5399 }
5400
5401 if (wp == curwin)
5402 {
5403 if (p_so)
5404 update_topline();
5405 curs_columns(FALSE); /* validate w_wrow */
5406 }
5407 wp->w_prev_fraction_row = wp->w_wrow;
5408
5409 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005410 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411#ifdef FEAT_WINDOWS
5412 wp->w_redr_status = TRUE;
5413#endif
5414 invalidate_botline_win(wp);
5415}
5416
5417#ifdef FEAT_VERTSPLIT
5418/*
5419 * Set the width of a window.
5420 */
5421 static void
5422win_new_width(wp, width)
5423 win_T *wp;
5424 int width;
5425{
5426 wp->w_width = width;
5427 wp->w_lines_valid = 0;
5428 changed_line_abv_curs_win(wp);
5429 invalidate_botline_win(wp);
5430 if (wp == curwin)
5431 {
5432 update_topline();
5433 curs_columns(TRUE); /* validate w_wrow */
5434 }
5435 redraw_win_later(wp, NOT_VALID);
5436 wp->w_redr_status = TRUE;
5437}
5438#endif
5439
5440 void
5441win_comp_scroll(wp)
5442 win_T *wp;
5443{
5444 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5445 if (wp->w_p_scr == 0)
5446 wp->w_p_scr = 1;
5447}
5448
5449/*
5450 * command_height: called whenever p_ch has been changed
5451 */
5452 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005453command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454{
5455#ifdef FEAT_WINDOWS
5456 int h;
5457 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005458 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005460 /* Use the value of p_ch that we remembered. This is needed for when the
5461 * GUI starts up, we can't be sure in what order things happen. And when
5462 * p_ch was changed in another tab page. */
5463 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005464
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 /* Find bottom frame with width of screen. */
5466 frp = lastwin->w_frame;
5467# ifdef FEAT_VERTSPLIT
5468 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5469 frp = frp->fr_parent;
5470# endif
5471
5472 /* Avoid changing the height of a window with 'winfixheight' set. */
5473 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5474 && frp->fr_win->w_p_wfh)
5475 frp = frp->fr_prev;
5476
5477 if (starting != NO_SCREEN)
5478 {
5479 cmdline_row = Rows - p_ch;
5480
5481 if (p_ch > old_p_ch) /* p_ch got bigger */
5482 {
5483 while (p_ch > old_p_ch)
5484 {
5485 if (frp == NULL)
5486 {
5487 EMSG(_(e_noroom));
5488 p_ch = old_p_ch;
5489 cmdline_row = Rows - p_ch;
5490 break;
5491 }
5492 h = frp->fr_height - frame_minheight(frp, NULL);
5493 if (h > p_ch - old_p_ch)
5494 h = p_ch - old_p_ch;
5495 old_p_ch += h;
5496 frame_add_height(frp, -h);
5497 frp = frp->fr_prev;
5498 }
5499
5500 /* Recompute window positions. */
5501 (void)win_comp_pos();
5502
5503 /* clear the lines added to cmdline */
5504 if (full_screen)
5505 screen_fill((int)(cmdline_row), (int)Rows, 0,
5506 (int)Columns, ' ', ' ', 0);
5507 msg_row = cmdline_row;
5508 redraw_cmdline = TRUE;
5509 return;
5510 }
5511
5512 if (msg_row < cmdline_row)
5513 msg_row = cmdline_row;
5514 redraw_cmdline = TRUE;
5515 }
5516 frame_add_height(frp, (int)(old_p_ch - p_ch));
5517
5518 /* Recompute window positions. */
5519 if (frp != lastwin->w_frame)
5520 (void)win_comp_pos();
5521#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005523 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524#endif
5525}
5526
5527#if defined(FEAT_WINDOWS) || defined(PROTO)
5528/*
5529 * Resize frame "frp" to be "n" lines higher (negative for less high).
5530 * Also resize the frames it is contained in.
5531 */
5532 static void
5533frame_add_height(frp, n)
5534 frame_T *frp;
5535 int n;
5536{
5537 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5538 for (;;)
5539 {
5540 frp = frp->fr_parent;
5541 if (frp == NULL)
5542 break;
5543 frp->fr_height += n;
5544 }
5545}
5546
5547/*
5548 * Add or remove a status line for the bottom window(s), according to the
5549 * value of 'laststatus'.
5550 */
5551 void
5552last_status(morewin)
5553 int morewin; /* pretend there are two or more windows */
5554{
5555 /* Don't make a difference between horizontal or vertical split. */
5556 last_status_rec(topframe, (p_ls == 2
5557 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5558}
5559
5560 static void
5561last_status_rec(fr, statusline)
5562 frame_T *fr;
5563 int statusline;
5564{
5565 frame_T *fp;
5566 win_T *wp;
5567
5568 if (fr->fr_layout == FR_LEAF)
5569 {
5570 wp = fr->fr_win;
5571 if (wp->w_status_height != 0 && !statusline)
5572 {
5573 /* remove status line */
5574 win_new_height(wp, wp->w_height + 1);
5575 wp->w_status_height = 0;
5576 comp_col();
5577 }
5578 else if (wp->w_status_height == 0 && statusline)
5579 {
5580 /* Find a frame to take a line from. */
5581 fp = fr;
5582 while (fp->fr_height <= frame_minheight(fp, NULL))
5583 {
5584 if (fp == topframe)
5585 {
5586 EMSG(_(e_noroom));
5587 return;
5588 }
5589 /* In a column of frames: go to frame above. If already at
5590 * the top or in a row of frames: go to parent. */
5591 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5592 fp = fp->fr_prev;
5593 else
5594 fp = fp->fr_parent;
5595 }
5596 wp->w_status_height = 1;
5597 if (fp != fr)
5598 {
5599 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5600 frame_fix_height(wp);
5601 (void)win_comp_pos();
5602 }
5603 else
5604 win_new_height(wp, wp->w_height - 1);
5605 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005606 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 }
5608 }
5609#ifdef FEAT_VERTSPLIT
5610 else if (fr->fr_layout == FR_ROW)
5611 {
5612 /* vertically split windows, set status line for each one */
5613 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5614 last_status_rec(fp, statusline);
5615 }
5616#endif
5617 else
5618 {
5619 /* horizontally split window, set status line for last one */
5620 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5621 ;
5622 last_status_rec(fp, statusline);
5623 }
5624}
5625
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005626/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005627 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005628 */
5629 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005630tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005631{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005632#ifdef FEAT_GUI_TABLINE
5633 /* When the GUI has the tabline then this always returns zero. */
5634 if (gui_use_tabline())
5635 return 0;
5636#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005637 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005638 {
5639 case 0: return 0;
5640 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5641 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005642 return 1;
5643}
5644
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645#endif /* FEAT_WINDOWS */
5646
5647#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5648/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005649 * Get the file name at the cursor.
5650 * If Visual mode is active, use the selected text if it's in one line.
5651 * Returns the name in allocated memory, NULL for failure.
5652 */
5653 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005654grab_file_name(count, file_lnum)
5655 long count;
5656 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005657{
5658# ifdef FEAT_VISUAL
5659 if (VIsual_active)
5660 {
5661 int len;
5662 char_u *ptr;
5663
5664 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5665 return NULL;
5666 return find_file_name_in_path(ptr, len,
5667 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5668 }
5669# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005670 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5671 file_lnum);
5672
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005673}
5674
5675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 * Return the file name under or after the cursor.
5677 *
5678 * The 'path' option is searched if the file name is not absolute.
5679 * The string returned has been alloc'ed and should be freed by the caller.
5680 * NULL is returned if the file name or file is not found.
5681 *
5682 * options:
5683 * FNAME_MESS give error messages
5684 * FNAME_EXP expand to path
5685 * FNAME_HYP check for hypertext link
5686 * FNAME_INCL apply "includeexpr"
5687 */
5688 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005689file_name_at_cursor(options, count, file_lnum)
5690 int options;
5691 long count;
5692 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693{
5694 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005695 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5696 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697}
5698
5699/*
5700 * Return the name of the file under or after ptr[col].
5701 * Otherwise like file_name_at_cursor().
5702 */
5703 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005704file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 char_u *line;
5706 int col;
5707 int options;
5708 long count;
5709 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005710 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711{
5712 char_u *ptr;
5713 int len;
5714
5715 /*
5716 * search forward for what could be the start of a file name
5717 */
5718 ptr = line + col;
5719 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005720 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 if (*ptr == NUL) /* nothing found */
5722 {
5723 if (options & FNAME_MESS)
5724 EMSG(_("E446: No file name under cursor"));
5725 return NULL;
5726 }
5727
5728 /*
5729 * Search backward for first char of the file name.
5730 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5731 */
5732 while (ptr > line)
5733 {
5734#ifdef FEAT_MBYTE
5735 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5736 ptr -= len + 1;
5737 else
5738#endif
5739 if (vim_isfilec(ptr[-1])
5740 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5741 --ptr;
5742 else
5743 break;
5744 }
5745
5746 /*
5747 * Search forward for the last char of the file name.
5748 * Also allow "://" when ':' is not in 'isfname'.
5749 */
5750 len = 0;
5751 while (vim_isfilec(ptr[len])
5752 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5753#ifdef FEAT_MBYTE
5754 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005755 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 else
5757#endif
5758 ++len;
5759
5760 /*
5761 * If there is trailing punctuation, remove it.
5762 * But don't remove "..", could be a directory name.
5763 */
5764 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5765 && ptr[len - 2] != '.')
5766 --len;
5767
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005768 if (file_lnum != NULL)
5769 {
5770 char_u *p;
5771
5772 /* Get the number after the file name and a separator character */
5773 p = ptr + len;
5774 p = skipwhite(p);
5775 if (*p != NUL)
5776 {
5777 if (!isdigit(*p))
5778 ++p; /* skip the separator */
5779 p = skipwhite(p);
5780 if (isdigit(*p))
5781 *file_lnum = (int)getdigits(&p);
5782 }
5783 }
5784
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5786}
5787
5788# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5789static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5790
5791 static char_u *
5792eval_includeexpr(ptr, len)
5793 char_u *ptr;
5794 int len;
5795{
5796 char_u *res;
5797
5798 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005799 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005800 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 set_vim_var_string(VV_FNAME, NULL, 0);
5802 return res;
5803}
5804#endif
5805
5806/*
5807 * Return the name of the file ptr[len] in 'path'.
5808 * Otherwise like file_name_at_cursor().
5809 */
5810 char_u *
5811find_file_name_in_path(ptr, len, options, count, rel_fname)
5812 char_u *ptr;
5813 int len;
5814 int options;
5815 long count;
5816 char_u *rel_fname; /* file we are searching relative to */
5817{
5818 char_u *file_name;
5819 int c;
5820# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5821 char_u *tofree = NULL;
5822
5823 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5824 {
5825 tofree = eval_includeexpr(ptr, len);
5826 if (tofree != NULL)
5827 {
5828 ptr = tofree;
5829 len = (int)STRLEN(ptr);
5830 }
5831 }
5832# endif
5833
5834 if (options & FNAME_EXP)
5835 {
5836 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5837 TRUE, rel_fname);
5838
5839# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5840 /*
5841 * If the file could not be found in a normal way, try applying
5842 * 'includeexpr' (unless done already).
5843 */
5844 if (file_name == NULL
5845 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5846 {
5847 tofree = eval_includeexpr(ptr, len);
5848 if (tofree != NULL)
5849 {
5850 ptr = tofree;
5851 len = (int)STRLEN(ptr);
5852 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5853 TRUE, rel_fname);
5854 }
5855 }
5856# endif
5857 if (file_name == NULL && (options & FNAME_MESS))
5858 {
5859 c = ptr[len];
5860 ptr[len] = NUL;
5861 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5862 ptr[len] = c;
5863 }
5864
5865 /* Repeat finding the file "count" times. This matters when it
5866 * appears several times in the path. */
5867 while (file_name != NULL && --count > 0)
5868 {
5869 vim_free(file_name);
5870 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5871 }
5872 }
5873 else
5874 file_name = vim_strnsave(ptr, len);
5875
5876# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5877 vim_free(tofree);
5878# endif
5879
5880 return file_name;
5881}
5882#endif /* FEAT_SEARCHPATH */
5883
5884/*
5885 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5886 * Also check for ":\\", which MS Internet Explorer accepts, return
5887 * URL_BACKSLASH.
5888 */
5889 static int
5890path_is_url(p)
5891 char_u *p;
5892{
5893 if (STRNCMP(p, "://", (size_t)3) == 0)
5894 return URL_SLASH;
5895 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5896 return URL_BACKSLASH;
5897 return 0;
5898}
5899
5900/*
5901 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5902 * Return URL_BACKSLASH for "name:\\".
5903 * Return zero otherwise.
5904 */
5905 int
5906path_with_url(fname)
5907 char_u *fname;
5908{
5909 char_u *p;
5910
5911 for (p = fname; isalpha(*p); ++p)
5912 ;
5913 return path_is_url(p);
5914}
5915
5916/*
5917 * Return TRUE if "name" is a full (absolute) path name or URL.
5918 */
5919 int
5920vim_isAbsName(name)
5921 char_u *name;
5922{
5923 return (path_with_url(name) != 0 || mch_isFullName(name));
5924}
5925
5926/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005927 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 *
5929 * return FAIL for failure, OK otherwise
5930 */
5931 int
5932vim_FullName(fname, buf, len, force)
5933 char_u *fname, *buf;
5934 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005935 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
5937 int retval = OK;
5938 int url;
5939
5940 *buf = NUL;
5941 if (fname == NULL)
5942 return FAIL;
5943
5944 url = path_with_url(fname);
5945 if (!url)
5946 retval = mch_FullName(fname, buf, len, force);
5947 if (url || retval == FAIL)
5948 {
5949 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005950 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 }
5952#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5953 slash_adjust(buf);
5954#endif
5955 return retval;
5956}
5957
5958/*
5959 * Return the minimal number of rows that is needed on the screen to display
5960 * the current number of windows.
5961 */
5962 int
5963min_rows()
5964{
5965 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005966#ifdef FEAT_WINDOWS
5967 tabpage_T *tp;
5968 int n;
5969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970
5971 if (firstwin == NULL) /* not initialized yet */
5972 return MIN_LINES;
5973
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005975 total = 0;
5976 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5977 {
5978 n = frame_minheight(tp->tp_topframe, NULL);
5979 if (total < n)
5980 total = n;
5981 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005982 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005984 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005986 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 return total;
5988}
5989
5990/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005991 * Return TRUE if there is only one window (in the current tab page), not
5992 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 */
5994 int
5995only_one_window()
5996{
5997#ifdef FEAT_WINDOWS
5998 int count = 0;
5999 win_T *wp;
6000
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006001 /* If there is another tab page there always is another window. */
6002 if (first_tabpage->tp_next != NULL)
6003 return FALSE;
6004
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00006006 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007# ifdef FEAT_QUICKFIX
6008 || wp->w_p_pvw
6009# endif
6010 ) || wp == curwin)
6011 ++count;
6012 return (count <= 1);
6013#else
6014 return TRUE;
6015#endif
6016}
6017
6018#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6019/*
6020 * Correct the cursor line number in other windows. Used after changing the
6021 * current buffer, and before applying autocommands.
6022 * When "do_curwin" is TRUE, also check current window.
6023 */
6024 void
6025check_lnums(do_curwin)
6026 int do_curwin;
6027{
6028 win_T *wp;
6029
6030#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006031 tabpage_T *tp;
6032
6033 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6035#else
6036 wp = curwin;
6037 if (do_curwin)
6038#endif
6039 {
6040 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6041 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6042 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6043 wp->w_topline = curbuf->b_ml.ml_line_count;
6044 }
6045}
6046#endif
6047
6048#if defined(FEAT_WINDOWS) || defined(PROTO)
6049
6050/*
6051 * A snapshot of the window sizes, to restore them after closing the help
6052 * window.
6053 * Only these fields are used:
6054 * fr_layout
6055 * fr_width
6056 * fr_height
6057 * fr_next
6058 * fr_child
6059 * fr_win (only valid for the old curwin, NULL otherwise)
6060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061
6062/*
6063 * Create a snapshot of the current frame sizes.
6064 */
6065 static void
6066make_snapshot()
6067{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006068 clear_snapshot(curtab);
6069 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070}
6071
6072 static void
6073make_snapshot_rec(fr, frp)
6074 frame_T *fr;
6075 frame_T **frp;
6076{
6077 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6078 if (*frp == NULL)
6079 return;
6080 (*frp)->fr_layout = fr->fr_layout;
6081# ifdef FEAT_VERTSPLIT
6082 (*frp)->fr_width = fr->fr_width;
6083# endif
6084 (*frp)->fr_height = fr->fr_height;
6085 if (fr->fr_next != NULL)
6086 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6087 if (fr->fr_child != NULL)
6088 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6089 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6090 (*frp)->fr_win = curwin;
6091}
6092
6093/*
6094 * Remove any existing snapshot.
6095 */
6096 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006097clear_snapshot(tp)
6098 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006100 clear_snapshot_rec(tp->tp_snapshot);
6101 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102}
6103
6104 static void
6105clear_snapshot_rec(fr)
6106 frame_T *fr;
6107{
6108 if (fr != NULL)
6109 {
6110 clear_snapshot_rec(fr->fr_next);
6111 clear_snapshot_rec(fr->fr_child);
6112 vim_free(fr);
6113 }
6114}
6115
6116/*
6117 * Restore a previously created snapshot, if there is any.
6118 * This is only done if the screen size didn't change and the window layout is
6119 * still the same.
6120 */
6121 static void
6122restore_snapshot(close_curwin)
6123 int close_curwin; /* closing current window */
6124{
6125 win_T *wp;
6126
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006127 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006129 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006131 && curtab->tp_snapshot->fr_height == topframe->fr_height
6132 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006134 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 win_comp_pos();
6136 if (wp != NULL && close_curwin)
6137 win_goto(wp);
6138 redraw_all_later(CLEAR);
6139 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006140 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141}
6142
6143/*
6144 * Check if frames "sn" and "fr" have the same layout, same following frames
6145 * and same children.
6146 */
6147 static int
6148check_snapshot_rec(sn, fr)
6149 frame_T *sn;
6150 frame_T *fr;
6151{
6152 if (sn->fr_layout != fr->fr_layout
6153 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6154 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6155 || (sn->fr_next != NULL
6156 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6157 || (sn->fr_child != NULL
6158 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6159 return FAIL;
6160 return OK;
6161}
6162
6163/*
6164 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6165 * following frames and children.
6166 * Returns a pointer to the old current window, or NULL.
6167 */
6168 static win_T *
6169restore_snapshot_rec(sn, fr)
6170 frame_T *sn;
6171 frame_T *fr;
6172{
6173 win_T *wp = NULL;
6174 win_T *wp2;
6175
6176 fr->fr_height = sn->fr_height;
6177# ifdef FEAT_VERTSPLIT
6178 fr->fr_width = sn->fr_width;
6179# endif
6180 if (fr->fr_layout == FR_LEAF)
6181 {
6182 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6183# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006184 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185# endif
6186 wp = sn->fr_win;
6187 }
6188 if (sn->fr_next != NULL)
6189 {
6190 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6191 if (wp2 != NULL)
6192 wp = wp2;
6193 }
6194 if (sn->fr_child != NULL)
6195 {
6196 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6197 if (wp2 != NULL)
6198 wp = wp2;
6199 }
6200 return wp;
6201}
6202
6203#endif
6204
6205#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6206/*
6207 * Return TRUE if there is any vertically split window.
6208 */
6209 int
6210win_hasvertsplit()
6211{
6212 frame_T *fr;
6213
6214 if (topframe->fr_layout == FR_ROW)
6215 return TRUE;
6216
6217 if (topframe->fr_layout == FR_COL)
6218 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6219 if (fr->fr_layout == FR_ROW)
6220 return TRUE;
6221
6222 return FALSE;
6223}
6224#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006225
6226#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6227/*
6228 * Add match to the match list of window 'wp'. The pattern 'pat' will be
6229 * highligted with the group 'grp' with priority 'prio'.
6230 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6231 * If no particular ID is desired, -1 must be specified for 'id'.
6232 * Return ID of added match, -1 on failure.
6233 */
6234 int
6235match_add(wp, grp, pat, prio, id)
6236 win_T *wp;
6237 char_u *grp;
6238 char_u *pat;
6239 int prio;
6240 int id;
6241{
6242 matchitem_T *cur;
6243 matchitem_T *prev;
6244 matchitem_T *m;
6245 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006246 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006247
6248 if (*grp == NUL || *pat == NUL)
6249 return -1;
6250 if (id < -1 || id == 0)
6251 {
6252 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6253 return -1;
6254 }
6255 if (id != -1)
6256 {
6257 cur = wp->w_match_head;
6258 while (cur != NULL)
6259 {
6260 if (cur->id == id)
6261 {
6262 EMSGN("E801: ID already taken: %ld", id);
6263 return -1;
6264 }
6265 cur = cur->next;
6266 }
6267 }
6268 if ((hlg_id = syn_namen2id(grp, STRLEN(grp))) == 0)
6269 {
6270 EMSG2(_(e_nogroup), grp);
6271 return -1;
6272 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006273 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006274 {
6275 EMSG2(_(e_invarg2), pat);
6276 return -1;
6277 }
6278
6279 /* Find available match ID. */
6280 while (id == -1)
6281 {
6282 cur = wp->w_match_head;
6283 while (cur != NULL && cur->id != wp->w_next_match_id)
6284 cur = cur->next;
6285 if (cur == NULL)
6286 id = wp->w_next_match_id;
6287 wp->w_next_match_id++;
6288 }
6289
6290 /* Build new match. */
6291 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6292 m->id = id;
6293 m->priority = prio;
6294 m->pattern = vim_strsave(pat);
6295 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006296 m->match.regprog = regprog;
6297 m->match.rmm_ic = FALSE;
6298 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006299
6300 /* Insert new match. The match list is in ascending order with regard to
6301 * the match priorities. */
6302 cur = wp->w_match_head;
6303 prev = cur;
6304 while (cur != NULL && prio >= cur->priority)
6305 {
6306 prev = cur;
6307 cur = cur->next;
6308 }
6309 if (cur == prev)
6310 wp->w_match_head = m;
6311 else
6312 prev->next = m;
6313 m->next = cur;
6314
6315 redraw_later(SOME_VALID);
6316 return id;
6317}
6318
6319/*
6320 * Delete match with ID 'id' in the match list of window 'wp'.
6321 * Print error messages if 'perr' is TRUE.
6322 */
6323 int
6324match_delete(wp, id, perr)
6325 win_T *wp;
6326 int id;
6327 int perr;
6328{
6329 matchitem_T *cur = wp->w_match_head;
6330 matchitem_T *prev = cur;
6331
6332 if (id < 1)
6333 {
6334 if (perr == TRUE)
6335 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6336 id);
6337 return -1;
6338 }
6339 while (cur != NULL && cur->id != id)
6340 {
6341 prev = cur;
6342 cur = cur->next;
6343 }
6344 if (cur == NULL)
6345 {
6346 if (perr == TRUE)
6347 EMSGN("E803: ID not found: %ld", id);
6348 return -1;
6349 }
6350 if (cur == prev)
6351 wp->w_match_head = cur->next;
6352 else
6353 prev->next = cur->next;
6354 vim_free(cur->match.regprog);
6355 vim_free(cur->pattern);
6356 vim_free(cur);
6357 redraw_later(SOME_VALID);
6358 return 0;
6359}
6360
6361/*
6362 * Delete all matches in the match list of window 'wp'.
6363 */
6364 void
6365clear_matches(wp)
6366 win_T *wp;
6367{
6368 matchitem_T *m;
6369
6370 while (wp->w_match_head != NULL)
6371 {
6372 m = wp->w_match_head->next;
6373 vim_free(wp->w_match_head->match.regprog);
6374 vim_free(wp->w_match_head->pattern);
6375 vim_free(wp->w_match_head);
6376 wp->w_match_head = m;
6377 }
6378 redraw_later(SOME_VALID);
6379}
6380
6381/*
6382 * Get match from ID 'id' in window 'wp'.
6383 * Return NULL if match not found.
6384 */
6385 matchitem_T *
6386get_match(wp, id)
6387 win_T *wp;
6388 int id;
6389{
6390 matchitem_T *cur = wp->w_match_head;
6391
6392 while (cur != NULL && cur->id != id)
6393 cur = cur->next;
6394 return cur;
6395}
6396#endif