xref: /vim-8.2.3635/src/evalwindow.c (revision 191acfde)
1 /* vi:set ts=8 sts=4 sw=4 noet:
2  *
3  * VIM - Vi IMproved	by Bram Moolenaar
4  *
5  * Do ":help uganda"  in Vim to read copying and usage conditions.
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 /*
11  * evalwindow.c: Window related builtin functions
12  */
13 
14 #include "vim.h"
15 
16 #if defined(FEAT_EVAL) || defined(PROTO)
17 
18     static int
19 win_getid(typval_T *argvars)
20 {
21     int	    winnr;
22     win_T   *wp;
23 
24     if (argvars[0].v_type == VAR_UNKNOWN)
25 	return curwin->w_id;
26     winnr = tv_get_number(&argvars[0]);
27     if (winnr > 0)
28     {
29 	if (argvars[1].v_type == VAR_UNKNOWN)
30 	    wp = firstwin;
31 	else
32 	{
33 	    tabpage_T	*tp;
34 	    int		tabnr = tv_get_number(&argvars[1]);
35 
36 	    FOR_ALL_TABPAGES(tp)
37 		if (--tabnr == 0)
38 		    break;
39 	    if (tp == NULL)
40 		return -1;
41 	    if (tp == curtab)
42 		wp = firstwin;
43 	    else
44 		wp = tp->tp_firstwin;
45 	}
46 	for ( ; wp != NULL; wp = wp->w_next)
47 	    if (--winnr == 0)
48 		return wp->w_id;
49     }
50     return 0;
51 }
52 
53     static void
54 win_id2tabwin(typval_T *argvars, list_T *list)
55 {
56     win_T	*wp;
57     tabpage_T   *tp;
58     int		winnr = 1;
59     int		tabnr = 1;
60     int		id = tv_get_number(&argvars[0]);
61 
62     FOR_ALL_TABPAGES(tp)
63     {
64 	FOR_ALL_WINDOWS_IN_TAB(tp, wp)
65 	{
66 	    if (wp->w_id == id)
67 	    {
68 		list_append_number(list, tabnr);
69 		list_append_number(list, winnr);
70 		return;
71 	    }
72 	    ++winnr;
73 	}
74 	++tabnr;
75 	winnr = 1;
76     }
77     list_append_number(list, 0);
78     list_append_number(list, 0);
79 }
80 
81 /*
82  * Return the window pointer of window "id".
83  */
84     win_T *
85 win_id2wp(int id)
86 {
87     return win_id2wp_tp(id, NULL);
88 }
89 
90 /*
91  * Return the window and tab pointer of window "id".
92  */
93     win_T *
94 win_id2wp_tp(int id, tabpage_T **tpp)
95 {
96     win_T	*wp;
97     tabpage_T   *tp;
98 
99     FOR_ALL_TAB_WINDOWS(tp, wp)
100 	if (wp->w_id == id)
101 	{
102 	    if (tpp != NULL)
103 		*tpp = tp;
104 	    return wp;
105 	}
106 #ifdef FEAT_PROP_POPUP
107     // popup windows are in separate lists
108      FOR_ALL_TABPAGES(tp)
109 	 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
110 	     if (wp->w_id == id)
111 	     {
112 		 if (tpp != NULL)
113 		     *tpp = tp;
114 		 return wp;
115 	     }
116     for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
117 	if (wp->w_id == id)
118 	{
119 	    if (tpp != NULL)
120 		*tpp = tp;
121 	    return wp;
122 	}
123 #endif
124 
125     return NULL;
126 }
127 
128     static int
129 win_id2win(typval_T *argvars)
130 {
131     win_T   *wp;
132     int	    nr = 1;
133     int	    id = tv_get_number(&argvars[0]);
134 
135     FOR_ALL_WINDOWS(wp)
136     {
137 	if (wp->w_id == id)
138 	    return nr;
139 	++nr;
140     }
141     return 0;
142 }
143 
144     void
145 win_findbuf(typval_T *argvars, list_T *list)
146 {
147     win_T	*wp;
148     tabpage_T   *tp;
149     int		bufnr = tv_get_number(&argvars[0]);
150 
151     FOR_ALL_TAB_WINDOWS(tp, wp)
152 	    if (wp->w_buffer->b_fnum == bufnr)
153 		list_append_number(list, wp->w_id);
154 }
155 
156 /*
157  * Find window specified by "vp" in tabpage "tp".
158  */
159     win_T *
160 find_win_by_nr(
161     typval_T	*vp,
162     tabpage_T	*tp)	// NULL for current tab page
163 {
164     win_T	*wp;
165     int		nr = (int)tv_get_number_chk(vp, NULL);
166 
167     if (nr < 0)
168 	return NULL;
169     if (nr == 0)
170 	return curwin;
171 
172     FOR_ALL_WINDOWS_IN_TAB(tp, wp)
173     {
174 	if (nr >= LOWEST_WIN_ID)
175 	{
176 	    if (wp->w_id == nr)
177 		return wp;
178 	}
179 	else if (--nr <= 0)
180 	    break;
181     }
182     if (nr >= LOWEST_WIN_ID)
183     {
184 #ifdef FEAT_PROP_POPUP
185 	// check tab-local popup windows
186 	for (wp = (tp == NULL ? curtab : tp)->tp_first_popupwin;
187 						   wp != NULL; wp = wp->w_next)
188 	    if (wp->w_id == nr)
189 		return wp;
190 	// check global popup windows
191 	for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
192 	    if (wp->w_id == nr)
193 		return wp;
194 #endif
195 	return NULL;
196     }
197     return wp;
198 }
199 
200 /*
201  * Find a window: When using a Window ID in any tab page, when using a number
202  * in the current tab page.
203  * Returns NULL when not found.
204  */
205     win_T *
206 find_win_by_nr_or_id(typval_T *vp)
207 {
208     int	nr = (int)tv_get_number_chk(vp, NULL);
209 
210     if (nr >= LOWEST_WIN_ID)
211 	return win_id2wp(tv_get_number(vp));
212     return find_win_by_nr(vp, NULL);
213 }
214 
215 /*
216  * Find window specified by "wvp" in tabpage "tvp".
217  * Returns the tab page in 'ptp'
218  */
219     win_T *
220 find_tabwin(
221     typval_T	*wvp,	// VAR_UNKNOWN for current window
222     typval_T	*tvp,	// VAR_UNKNOWN for current tab page
223     tabpage_T	**ptp)
224 {
225     win_T	*wp = NULL;
226     tabpage_T	*tp = NULL;
227     long	n;
228 
229     if (wvp->v_type != VAR_UNKNOWN)
230     {
231 	if (tvp->v_type != VAR_UNKNOWN)
232 	{
233 	    n = (long)tv_get_number(tvp);
234 	    if (n >= 0)
235 		tp = find_tabpage(n);
236 	}
237 	else
238 	    tp = curtab;
239 
240 	if (tp != NULL)
241 	{
242 	    wp = find_win_by_nr(wvp, tp);
243 	    if (wp == NULL && wvp->v_type == VAR_NUMBER
244 						&& wvp->vval.v_number != -1)
245 		// A window with the specified number is not found
246 		tp = NULL;
247 	}
248     }
249     else
250     {
251 	wp = curwin;
252 	tp = curtab;
253     }
254 
255     if (ptp != NULL)
256 	*ptp = tp;
257 
258     return wp;
259 }
260 
261 /*
262  * Get the layout of the given tab page for winlayout().
263  */
264     static void
265 get_framelayout(frame_T *fr, list_T *l, int outer)
266 {
267     frame_T	*child;
268     list_T	*fr_list;
269     list_T	*win_list;
270 
271     if (fr == NULL)
272 	return;
273 
274     if (outer)
275 	// outermost call from f_winlayout()
276 	fr_list = l;
277     else
278     {
279 	fr_list = list_alloc();
280 	if (fr_list == NULL)
281 	    return;
282 	list_append_list(l, fr_list);
283     }
284 
285     if (fr->fr_layout == FR_LEAF)
286     {
287 	if (fr->fr_win != NULL)
288 	{
289 	    list_append_string(fr_list, (char_u *)"leaf", -1);
290 	    list_append_number(fr_list, fr->fr_win->w_id);
291 	}
292     }
293     else
294     {
295 	list_append_string(fr_list,
296 	     fr->fr_layout == FR_ROW ?  (char_u *)"row" : (char_u *)"col", -1);
297 
298 	win_list = list_alloc();
299 	if (win_list == NULL)
300 	    return;
301 	list_append_list(fr_list, win_list);
302 	child = fr->fr_child;
303 	while (child != NULL)
304 	{
305 	    get_framelayout(child, win_list, FALSE);
306 	    child = child->fr_next;
307 	}
308     }
309 }
310 
311 /*
312  * Common code for tabpagewinnr() and winnr().
313  */
314     static int
315 get_winnr(tabpage_T *tp, typval_T *argvar)
316 {
317     win_T	*twin;
318     int		nr = 1;
319     win_T	*wp;
320     char_u	*arg;
321 
322     twin = (tp == curtab) ? curwin : tp->tp_curwin;
323     if (argvar->v_type != VAR_UNKNOWN)
324     {
325 	int	invalid_arg = FALSE;
326 
327 	arg = tv_get_string_chk(argvar);
328 	if (arg == NULL)
329 	    nr = 0;		// type error; errmsg already given
330 	else if (STRCMP(arg, "$") == 0)
331 	    twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
332 	else if (STRCMP(arg, "#") == 0)
333 	{
334 	    twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
335 	}
336 	else
337 	{
338 	    long	count;
339 	    char_u	*endp;
340 
341 	    // Extract the window count (if specified). e.g. winnr('3j')
342 	    count = strtol((char *)arg, (char **)&endp, 10);
343 	    if (count <= 0)
344 		count = 1;	// if count is not specified, default to 1
345 	    if (endp != NULL && *endp != '\0')
346 	    {
347 		if (STRCMP(endp, "j") == 0)
348 		    twin = win_vert_neighbor(tp, twin, FALSE, count);
349 		else if (STRCMP(endp, "k") == 0)
350 		    twin = win_vert_neighbor(tp, twin, TRUE, count);
351 		else if (STRCMP(endp, "h") == 0)
352 		    twin = win_horz_neighbor(tp, twin, TRUE, count);
353 		else if (STRCMP(endp, "l") == 0)
354 		    twin = win_horz_neighbor(tp, twin, FALSE, count);
355 		else
356 		    invalid_arg = TRUE;
357 	    }
358 	    else
359 		invalid_arg = TRUE;
360 	}
361 	if (twin == NULL)
362 	    nr = 0;
363 
364 	if (invalid_arg)
365 	{
366 	    semsg(_(e_invexpr2), arg);
367 	    nr = 0;
368 	}
369     }
370 
371     if (nr > 0)
372 	for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
373 					      wp != twin; wp = wp->w_next)
374 	{
375 	    if (wp == NULL)
376 	    {
377 		// didn't find it in this tabpage
378 		nr = 0;
379 		break;
380 	    }
381 	    ++nr;
382 	}
383     return nr;
384 }
385 
386 /*
387  * Returns information about a window as a dictionary.
388  */
389     static dict_T *
390 get_win_info(win_T *wp, short tpnr, short winnr)
391 {
392     dict_T	*dict;
393 
394     dict = dict_alloc();
395     if (dict == NULL)
396 	return NULL;
397 
398     dict_add_number(dict, "tabnr", tpnr);
399     dict_add_number(dict, "winnr", winnr);
400     dict_add_number(dict, "winid", wp->w_id);
401     dict_add_number(dict, "height", wp->w_height);
402     dict_add_number(dict, "winrow", wp->w_winrow + 1);
403     dict_add_number(dict, "topline", wp->w_topline);
404     dict_add_number(dict, "botline", wp->w_botline - 1);
405 #ifdef FEAT_MENU
406     dict_add_number(dict, "winbar", wp->w_winbar_height);
407 #endif
408     dict_add_number(dict, "width", wp->w_width);
409     dict_add_number(dict, "wincol", wp->w_wincol + 1);
410     dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
411 
412 #ifdef FEAT_TERMINAL
413     dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
414 #endif
415 #ifdef FEAT_QUICKFIX
416     dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
417     dict_add_number(dict, "loclist",
418 		      (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
419 #endif
420 
421     // Add a reference to window variables
422     dict_add_dict(dict, "variables", wp->w_vars);
423 
424     return dict;
425 }
426 
427 /*
428  * Returns information (variables, options, etc.) about a tab page
429  * as a dictionary.
430  */
431     static dict_T *
432 get_tabpage_info(tabpage_T *tp, int tp_idx)
433 {
434     win_T	*wp;
435     dict_T	*dict;
436     list_T	*l;
437 
438     dict = dict_alloc();
439     if (dict == NULL)
440 	return NULL;
441 
442     dict_add_number(dict, "tabnr", tp_idx);
443 
444     l = list_alloc();
445     if (l != NULL)
446     {
447 	for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
448 						   wp != NULL; wp = wp->w_next)
449 	    list_append_number(l, (varnumber_T)wp->w_id);
450 	dict_add_list(dict, "windows", l);
451     }
452 
453     // Make a reference to tabpage variables
454     dict_add_dict(dict, "variables", tp->tp_vars);
455 
456     return dict;
457 }
458 
459 /*
460  * "gettabinfo()" function
461  */
462     void
463 f_gettabinfo(typval_T *argvars, typval_T *rettv)
464 {
465     tabpage_T	*tp, *tparg = NULL;
466     dict_T	*d;
467     int		tpnr = 0;
468 
469     if (rettv_list_alloc(rettv) != OK)
470 	return;
471 
472     if (argvars[0].v_type != VAR_UNKNOWN)
473     {
474 	// Information about one tab page
475 	tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
476 	if (tparg == NULL)
477 	    return;
478     }
479 
480     // Get information about a specific tab page or all tab pages
481     FOR_ALL_TABPAGES(tp)
482     {
483 	tpnr++;
484 	if (tparg != NULL && tp != tparg)
485 	    continue;
486 	d = get_tabpage_info(tp, tpnr);
487 	if (d != NULL)
488 	    list_append_dict(rettv->vval.v_list, d);
489 	if (tparg != NULL)
490 	    return;
491     }
492 }
493 
494 /*
495  * "getwininfo()" function
496  */
497     void
498 f_getwininfo(typval_T *argvars, typval_T *rettv)
499 {
500     tabpage_T	*tp;
501     win_T	*wp = NULL, *wparg = NULL;
502     dict_T	*d;
503     short	tabnr = 0, winnr;
504 
505     if (rettv_list_alloc(rettv) != OK)
506 	return;
507 
508     if (argvars[0].v_type != VAR_UNKNOWN)
509     {
510 	wparg = win_id2wp(tv_get_number(&argvars[0]));
511 	if (wparg == NULL)
512 	    return;
513     }
514 
515     // Collect information about either all the windows across all the tab
516     // pages or one particular window.
517     FOR_ALL_TABPAGES(tp)
518     {
519 	tabnr++;
520 	winnr = 0;
521 	FOR_ALL_WINDOWS_IN_TAB(tp, wp)
522 	{
523 	    winnr++;
524 	    if (wparg != NULL && wp != wparg)
525 		continue;
526 	    d = get_win_info(wp, tabnr, winnr);
527 	    if (d != NULL)
528 		list_append_dict(rettv->vval.v_list, d);
529 	    if (wparg != NULL)
530 		// found information about a specific window
531 		return;
532 	}
533     }
534 }
535 
536 /*
537  * "getwinpos({timeout})" function
538  */
539     void
540 f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
541 {
542     int x = -1;
543     int y = -1;
544 
545     if (rettv_list_alloc(rettv) == FAIL)
546 	return;
547 #if defined(FEAT_GUI) \
548 	|| (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
549 	|| defined(MSWIN)
550     {
551 	varnumber_T timeout = 100;
552 
553 	if (argvars[0].v_type != VAR_UNKNOWN)
554 	    timeout = tv_get_number(&argvars[0]);
555 
556 	(void)ui_get_winpos(&x, &y, timeout);
557     }
558 #endif
559     list_append_number(rettv->vval.v_list, (varnumber_T)x);
560     list_append_number(rettv->vval.v_list, (varnumber_T)y);
561 }
562 
563 
564 /*
565  * "getwinposx()" function
566  */
567     void
568 f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
569 {
570     rettv->vval.v_number = -1;
571 #if defined(FEAT_GUI) \
572 	|| (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
573 	|| defined(MSWIN)
574 
575     {
576 	int	    x, y;
577 
578 	if (ui_get_winpos(&x, &y, 100) == OK)
579 	    rettv->vval.v_number = x;
580     }
581 #endif
582 }
583 
584 /*
585  * "getwinposy()" function
586  */
587     void
588 f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
589 {
590     rettv->vval.v_number = -1;
591 #if defined(FEAT_GUI) \
592 	|| (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
593 	|| defined(MSWIN)
594     {
595 	int	    x, y;
596 
597 	if (ui_get_winpos(&x, &y, 100) == OK)
598 	    rettv->vval.v_number = y;
599     }
600 #endif
601 }
602 
603 /*
604  * "tabpagenr()" function
605  */
606     void
607 f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
608 {
609     int		nr = 1;
610     char_u	*arg;
611 
612     if (argvars[0].v_type != VAR_UNKNOWN)
613     {
614 	arg = tv_get_string_chk(&argvars[0]);
615 	nr = 0;
616 	if (arg != NULL)
617 	{
618 	    if (STRCMP(arg, "$") == 0)
619 		nr = tabpage_index(NULL) - 1;
620 	    else
621 		semsg(_(e_invexpr2), arg);
622 	}
623     }
624     else
625 	nr = tabpage_index(curtab);
626     rettv->vval.v_number = nr;
627 }
628 
629 /*
630  * "tabpagewinnr()" function
631  */
632     void
633 f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
634 {
635     int		nr = 1;
636     tabpage_T	*tp;
637 
638     tp = find_tabpage((int)tv_get_number(&argvars[0]));
639     if (tp == NULL)
640 	nr = 0;
641     else
642 	nr = get_winnr(tp, &argvars[1]);
643     rettv->vval.v_number = nr;
644 }
645 
646 /*
647  * "win_execute()" function
648  */
649     void
650 f_win_execute(typval_T *argvars, typval_T *rettv)
651 {
652     int		id = (int)tv_get_number(argvars);
653     tabpage_T	*tp;
654     win_T	*wp = win_id2wp_tp(id, &tp);
655     win_T	*save_curwin;
656     tabpage_T	*save_curtab;
657 
658     if (wp != NULL && tp != NULL)
659     {
660 	pos_T	curpos = wp->w_cursor;
661 
662 	if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE) == OK)
663 	{
664 	    check_cursor();
665 	    execute_common(argvars, rettv, 1);
666 	}
667 	restore_win_noblock(save_curwin, save_curtab, TRUE);
668 
669 	// Update the status line if the cursor moved.
670 	if (win_valid(wp) && !EQUAL_POS(curpos, wp->w_cursor))
671 	    wp->w_redr_status = TRUE;
672     }
673 }
674 
675 /*
676  * "win_findbuf()" function
677  */
678     void
679 f_win_findbuf(typval_T *argvars, typval_T *rettv)
680 {
681     if (rettv_list_alloc(rettv) != FAIL)
682 	win_findbuf(argvars, rettv->vval.v_list);
683 }
684 
685 /*
686  * "win_getid()" function
687  */
688     void
689 f_win_getid(typval_T *argvars, typval_T *rettv)
690 {
691     rettv->vval.v_number = win_getid(argvars);
692 }
693 
694 /*
695  * "win_gotoid()" function
696  */
697     void
698 f_win_gotoid(typval_T *argvars, typval_T *rettv)
699 {
700     win_T	*wp;
701     tabpage_T   *tp;
702     int		id = tv_get_number(&argvars[0]);
703 
704 #ifdef FEAT_CMDWIN
705     if (cmdwin_type != 0)
706     {
707 	emsg(_(e_cmdwin));
708 	return;
709     }
710 #endif
711     FOR_ALL_TAB_WINDOWS(tp, wp)
712 	if (wp->w_id == id)
713 	{
714 	    goto_tabpage_win(tp, wp);
715 	    rettv->vval.v_number = 1;
716 	    return;
717 	}
718 }
719 
720 /*
721  * "win_id2tabwin()" function
722  */
723     void
724 f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
725 {
726     if (rettv_list_alloc(rettv) != FAIL)
727 	win_id2tabwin(argvars, rettv->vval.v_list);
728 }
729 
730 /*
731  * "win_id2win()" function
732  */
733     void
734 f_win_id2win(typval_T *argvars, typval_T *rettv)
735 {
736     rettv->vval.v_number = win_id2win(argvars);
737 }
738 
739 /*
740  * "win_screenpos()" function
741  */
742     void
743 f_win_screenpos(typval_T *argvars, typval_T *rettv)
744 {
745     win_T	*wp;
746 
747     if (rettv_list_alloc(rettv) == FAIL)
748 	return;
749 
750     wp = find_win_by_nr_or_id(&argvars[0]);
751     list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
752     list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
753 }
754 
755 /*
756  * Move the window wp into a new split of targetwin in a given direction
757  */
758     static void
759 win_move_into_split(win_T *wp, win_T *targetwin, int size, int flags)
760 {
761     int	    dir;
762     int	    height = wp->w_height;
763     win_T   *oldwin = curwin;
764 
765     if (wp == targetwin)
766 	return;
767 
768     // Jump to the target window
769     if (curwin != targetwin)
770 	win_goto(targetwin);
771 
772     // Remove the old window and frame from the tree of frames
773     (void)winframe_remove(wp, &dir, NULL);
774     win_remove(wp, NULL);
775     last_status(FALSE);	    // may need to remove last status line
776     (void)win_comp_pos();   // recompute window positions
777 
778     // Split a window on the desired side and put the old window there
779     (void)win_split_ins(size, flags, wp, dir);
780 
781     // If splitting horizontally, try to preserve height
782     if (size == 0 && !(flags & WSP_VERT))
783     {
784 	win_setheight_win(height, wp);
785 	if (p_ea)
786 	    win_equal(wp, TRUE, 'v');
787     }
788 
789 #if defined(FEAT_GUI)
790     // When 'guioptions' includes 'L' or 'R' may have to remove or add
791     // scrollbars.  Have to update them anyway.
792     gui_may_update_scrollbars();
793 #endif
794 
795     if (oldwin != curwin)
796 	win_goto(oldwin);
797 }
798 
799 /*
800  * "win_splitmove()" function
801  */
802     void
803 f_win_splitmove(typval_T *argvars, typval_T *rettv)
804 {
805     win_T   *wp;
806     win_T   *targetwin;
807     int     flags = 0, size = 0;
808 
809     wp = find_win_by_nr_or_id(&argvars[0]);
810     targetwin = find_win_by_nr_or_id(&argvars[1]);
811 
812     if (wp == NULL || targetwin == NULL || wp == targetwin
813 	    || !win_valid(wp) || !win_valid(targetwin)
814 	    || win_valid_popup(wp) || win_valid_popup(targetwin))
815     {
816         emsg(_(e_invalwindow));
817 	rettv->vval.v_number = -1;
818 	return;
819     }
820 
821     if (argvars[2].v_type != VAR_UNKNOWN)
822     {
823         dict_T      *d;
824         dictitem_T  *di;
825 
826         if (argvars[2].v_type != VAR_DICT || argvars[2].vval.v_dict == NULL)
827         {
828             emsg(_(e_invarg));
829             return;
830         }
831 
832         d = argvars[2].vval.v_dict;
833         if (dict_get_number(d, (char_u *)"vertical"))
834             flags |= WSP_VERT;
835         if ((di = dict_find(d, (char_u *)"rightbelow", -1)) != NULL)
836             flags |= tv_get_number(&di->di_tv) ? WSP_BELOW : WSP_ABOVE;
837         size = (int)dict_get_number(d, (char_u *)"size");
838     }
839 
840     win_move_into_split(wp, targetwin, size, flags);
841 }
842 
843 /*
844  * "win_gettype(nr)" function
845  */
846     void
847 f_win_gettype(typval_T *argvars, typval_T *rettv)
848 {
849     win_T	*wp = curwin;
850 
851     rettv->v_type = VAR_STRING;
852     rettv->vval.v_string = NULL;
853     if (argvars[0].v_type != VAR_UNKNOWN)
854     {
855 	wp = find_win_by_nr_or_id(&argvars[0]);
856 	if (wp == NULL)
857 	{
858 	    rettv->vval.v_string = vim_strsave((char_u *)"unknown");
859 	    return;
860 	}
861     }
862 #ifdef FEAT_PROP_POPUP
863     if (WIN_IS_POPUP(wp))
864 	rettv->vval.v_string = vim_strsave((char_u *)"popup");
865     else
866 #endif
867 #ifdef FEAT_CMDWIN
868     if (wp == curwin && cmdwin_type != 0)
869 	rettv->vval.v_string = vim_strsave((char_u *)"command");
870 #endif
871 }
872 
873 /*
874  * "getcmdwintype()" function
875  */
876     void
877 f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
878 {
879     rettv->v_type = VAR_STRING;
880     rettv->vval.v_string = NULL;
881 #ifdef FEAT_CMDWIN
882     rettv->vval.v_string = alloc(2);
883     if (rettv->vval.v_string != NULL)
884     {
885 	rettv->vval.v_string[0] = cmdwin_type;
886 	rettv->vval.v_string[1] = NUL;
887     }
888 #endif
889 }
890 
891 /*
892  * "winbufnr(nr)" function
893  */
894     void
895 f_winbufnr(typval_T *argvars, typval_T *rettv)
896 {
897     win_T	*wp;
898 
899     wp = find_win_by_nr_or_id(&argvars[0]);
900     if (wp == NULL)
901 	rettv->vval.v_number = -1;
902     else
903 	rettv->vval.v_number = wp->w_buffer->b_fnum;
904 }
905 
906 /*
907  * "wincol()" function
908  */
909     void
910 f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
911 {
912     validate_cursor();
913     rettv->vval.v_number = curwin->w_wcol + 1;
914 }
915 
916 /*
917  * "winheight(nr)" function
918  */
919     void
920 f_winheight(typval_T *argvars, typval_T *rettv)
921 {
922     win_T	*wp;
923 
924     wp = find_win_by_nr_or_id(&argvars[0]);
925     if (wp == NULL)
926 	rettv->vval.v_number = -1;
927     else
928 	rettv->vval.v_number = wp->w_height;
929 }
930 
931 /*
932  * "winlayout()" function
933  */
934     void
935 f_winlayout(typval_T *argvars, typval_T *rettv)
936 {
937     tabpage_T	*tp;
938 
939     if (rettv_list_alloc(rettv) != OK)
940 	return;
941 
942     if (argvars[0].v_type == VAR_UNKNOWN)
943 	tp = curtab;
944     else
945     {
946 	tp = find_tabpage((int)tv_get_number(&argvars[0]));
947 	if (tp == NULL)
948 	    return;
949     }
950 
951     get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
952 }
953 
954 /*
955  * "winline()" function
956  */
957     void
958 f_winline(typval_T *argvars UNUSED, typval_T *rettv)
959 {
960     validate_cursor();
961     rettv->vval.v_number = curwin->w_wrow + 1;
962 }
963 
964 /*
965  * "winnr()" function
966  */
967     void
968 f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
969 {
970     int		nr = 1;
971 
972     nr = get_winnr(curtab, &argvars[0]);
973     rettv->vval.v_number = nr;
974 }
975 
976 /*
977  * "winrestcmd()" function
978  */
979     void
980 f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
981 {
982     win_T	*wp;
983     int		winnr = 1;
984     garray_T	ga;
985     char_u	buf[50];
986 
987     ga_init2(&ga, (int)sizeof(char), 70);
988     FOR_ALL_WINDOWS(wp)
989     {
990 	sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
991 	ga_concat(&ga, buf);
992 	sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
993 	ga_concat(&ga, buf);
994 	++winnr;
995     }
996     ga_append(&ga, NUL);
997 
998     rettv->vval.v_string = ga.ga_data;
999     rettv->v_type = VAR_STRING;
1000 }
1001 
1002 /*
1003  * "winrestview()" function
1004  */
1005     void
1006 f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
1007 {
1008     dict_T	*dict;
1009 
1010     if (argvars[0].v_type != VAR_DICT
1011 	    || (dict = argvars[0].vval.v_dict) == NULL)
1012 	emsg(_(e_invarg));
1013     else
1014     {
1015 	if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
1016 	    curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
1017 	if (dict_find(dict, (char_u *)"col", -1) != NULL)
1018 	    curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
1019 	if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
1020 	    curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
1021 	if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
1022 	{
1023 	    curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
1024 	    curwin->w_set_curswant = FALSE;
1025 	}
1026 
1027 	if (dict_find(dict, (char_u *)"topline", -1) != NULL)
1028 	    set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
1029 #ifdef FEAT_DIFF
1030 	if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
1031 	    curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
1032 #endif
1033 	if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
1034 	    curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
1035 	if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
1036 	    curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
1037 
1038 	check_cursor();
1039 	win_new_height(curwin, curwin->w_height);
1040 	win_new_width(curwin, curwin->w_width);
1041 	changed_window_setting();
1042 
1043 	if (curwin->w_topline <= 0)
1044 	    curwin->w_topline = 1;
1045 	if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1046 	    curwin->w_topline = curbuf->b_ml.ml_line_count;
1047 #ifdef FEAT_DIFF
1048 	check_topfill(curwin, TRUE);
1049 #endif
1050     }
1051 }
1052 
1053 /*
1054  * "winsaveview()" function
1055  */
1056     void
1057 f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
1058 {
1059     dict_T	*dict;
1060 
1061     if (rettv_dict_alloc(rettv) == FAIL)
1062 	return;
1063     dict = rettv->vval.v_dict;
1064 
1065     dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
1066     dict_add_number(dict, "col", (long)curwin->w_cursor.col);
1067     dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
1068     update_curswant();
1069     dict_add_number(dict, "curswant", (long)curwin->w_curswant);
1070 
1071     dict_add_number(dict, "topline", (long)curwin->w_topline);
1072 #ifdef FEAT_DIFF
1073     dict_add_number(dict, "topfill", (long)curwin->w_topfill);
1074 #endif
1075     dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
1076     dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
1077 }
1078 
1079 /*
1080  * "winwidth(nr)" function
1081  */
1082     void
1083 f_winwidth(typval_T *argvars, typval_T *rettv)
1084 {
1085     win_T	*wp;
1086 
1087     wp = find_win_by_nr_or_id(&argvars[0]);
1088     if (wp == NULL)
1089 	rettv->vval.v_number = -1;
1090     else
1091 	rettv->vval.v_number = wp->w_width;
1092 }
1093 #endif // FEAT_EVAL
1094 
1095 #if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
1096 	|| defined(PROTO)
1097 /*
1098  * Set "win" to be the curwin and "tp" to be the current tab page.
1099  * restore_win() MUST be called to undo, also when FAIL is returned.
1100  * No autocommands will be executed until restore_win() is called.
1101  * When "no_display" is TRUE the display won't be affected, no redraw is
1102  * triggered, another tabpage access is limited.
1103  * Returns FAIL if switching to "win" failed.
1104  */
1105     int
1106 switch_win(
1107     win_T	**save_curwin,
1108     tabpage_T	**save_curtab,
1109     win_T	*win,
1110     tabpage_T	*tp,
1111     int		no_display)
1112 {
1113     block_autocmds();
1114     return switch_win_noblock(save_curwin, save_curtab, win, tp, no_display);
1115 }
1116 
1117 /*
1118  * As switch_win() but without blocking autocommands.
1119  */
1120     int
1121 switch_win_noblock(
1122     win_T	**save_curwin,
1123     tabpage_T	**save_curtab,
1124     win_T	*win,
1125     tabpage_T	*tp,
1126     int		no_display)
1127 {
1128     *save_curwin = curwin;
1129     if (tp != NULL)
1130     {
1131 	*save_curtab = curtab;
1132 	if (no_display)
1133 	{
1134 	    curtab->tp_firstwin = firstwin;
1135 	    curtab->tp_lastwin = lastwin;
1136 	    curtab = tp;
1137 	    firstwin = curtab->tp_firstwin;
1138 	    lastwin = curtab->tp_lastwin;
1139 	}
1140 	else
1141 	    goto_tabpage_tp(tp, FALSE, FALSE);
1142     }
1143     if (!win_valid(win))
1144 	return FAIL;
1145     curwin = win;
1146     curbuf = curwin->w_buffer;
1147     return OK;
1148 }
1149 
1150 /*
1151  * Restore current tabpage and window saved by switch_win(), if still valid.
1152  * When "no_display" is TRUE the display won't be affected, no redraw is
1153  * triggered.
1154  */
1155     void
1156 restore_win(
1157     win_T	*save_curwin,
1158     tabpage_T	*save_curtab,
1159     int		no_display)
1160 {
1161     restore_win_noblock(save_curwin, save_curtab, no_display);
1162     unblock_autocmds();
1163 }
1164 
1165 /*
1166  * As restore_win() but without unblocking autocommands.
1167  */
1168     void
1169 restore_win_noblock(
1170     win_T	*save_curwin,
1171     tabpage_T	*save_curtab,
1172     int		no_display)
1173 {
1174     if (save_curtab != NULL && valid_tabpage(save_curtab))
1175     {
1176 	if (no_display)
1177 	{
1178 	    curtab->tp_firstwin = firstwin;
1179 	    curtab->tp_lastwin = lastwin;
1180 	    curtab = save_curtab;
1181 	    firstwin = curtab->tp_firstwin;
1182 	    lastwin = curtab->tp_lastwin;
1183 	}
1184 	else
1185 	    goto_tabpage_tp(save_curtab, FALSE, FALSE);
1186     }
1187     if (win_valid(save_curwin))
1188     {
1189 	curwin = save_curwin;
1190 	curbuf = curwin->w_buffer;
1191     }
1192 # ifdef FEAT_PROP_POPUP
1193     else if (WIN_IS_POPUP(curwin))
1194 	// original window was closed and now we're in a popup window: Go
1195 	// to the first valid window.
1196 	win_goto(firstwin);
1197 # endif
1198 }
1199 #endif
1200