xref: /vim-8.2.3635/src/evalwindow.c (revision 53f7fccc)
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_ALL_POPUPWINS_IN_TAB(tp, wp)
110 	     if (wp->w_id == id)
111 	     {
112 		 if (tpp != NULL)
113 		     *tpp = tp;
114 		 return wp;
115 	     }
116     FOR_ALL_POPUPWINS(wp)
117 	if (wp->w_id == id)
118 	{
119 	    if (tpp != NULL)
120 		*tpp = curtab;  // any tabpage would do
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_ALL_POPUPWINS(wp)
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_invalid_expression_str), 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_ALL_WINDOWS_IN_TAB(tp, wp)
448 	    list_append_number(l, (varnumber_T)wp->w_id);
449 	dict_add_list(dict, "windows", l);
450     }
451 
452     // Make a reference to tabpage variables
453     dict_add_dict(dict, "variables", tp->tp_vars);
454 
455     return dict;
456 }
457 
458 /*
459  * "gettabinfo()" function
460  */
461     void
462 f_gettabinfo(typval_T *argvars, typval_T *rettv)
463 {
464     tabpage_T	*tp, *tparg = NULL;
465     dict_T	*d;
466     int		tpnr = 0;
467 
468     if (rettv_list_alloc(rettv) != OK)
469 	return;
470 
471     if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
472 	return;
473 
474     if (argvars[0].v_type != VAR_UNKNOWN)
475     {
476 	// Information about one tab page
477 	tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
478 	if (tparg == NULL)
479 	    return;
480     }
481 
482     // Get information about a specific tab page or all tab pages
483     FOR_ALL_TABPAGES(tp)
484     {
485 	tpnr++;
486 	if (tparg != NULL && tp != tparg)
487 	    continue;
488 	d = get_tabpage_info(tp, tpnr);
489 	if (d != NULL)
490 	    list_append_dict(rettv->vval.v_list, d);
491 	if (tparg != NULL)
492 	    return;
493     }
494 }
495 
496 /*
497  * "getwininfo()" function
498  */
499     void
500 f_getwininfo(typval_T *argvars, typval_T *rettv)
501 {
502     tabpage_T	*tp;
503     win_T	*wp = NULL, *wparg = NULL;
504     dict_T	*d;
505     short	tabnr = 0, winnr;
506 
507     if (rettv_list_alloc(rettv) != OK)
508 	return;
509 
510     if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
511 	return;
512 
513     if (argvars[0].v_type != VAR_UNKNOWN)
514     {
515 	wparg = win_id2wp(tv_get_number(&argvars[0]));
516 	if (wparg == NULL)
517 	    return;
518     }
519 
520     // Collect information about either all the windows across all the tab
521     // pages or one particular window.
522     FOR_ALL_TABPAGES(tp)
523     {
524 	tabnr++;
525 	winnr = 0;
526 	FOR_ALL_WINDOWS_IN_TAB(tp, wp)
527 	{
528 	    winnr++;
529 	    if (wparg != NULL && wp != wparg)
530 		continue;
531 	    d = get_win_info(wp, tabnr, winnr);
532 	    if (d != NULL)
533 		list_append_dict(rettv->vval.v_list, d);
534 	    if (wparg != NULL)
535 		// found information about a specific window
536 		return;
537 	}
538     }
539 #ifdef FEAT_PROP_POPUP
540     if (wparg != NULL)
541     {
542 	tabnr = 0;
543 	FOR_ALL_TABPAGES(tp)
544 	{
545 	    tabnr++;
546 	    FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
547 	    if (wp == wparg)
548 		break;
549 	}
550 	d = get_win_info(wparg, tp == NULL ? 0 : tabnr, 0);
551 	if (d != NULL)
552 	    list_append_dict(rettv->vval.v_list, d);
553     }
554 #endif
555 }
556 
557 /*
558  * "getwinpos({timeout})" function
559  */
560     void
561 f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
562 {
563     int x = -1;
564     int y = -1;
565 
566     if (rettv_list_alloc(rettv) == FAIL)
567 	return;
568 
569     if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
570 	return;
571 
572 #if defined(FEAT_GUI) \
573 	|| (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
574 	|| defined(MSWIN)
575     {
576 	varnumber_T timeout = 100;
577 
578 	if (argvars[0].v_type != VAR_UNKNOWN)
579 	    timeout = tv_get_number(&argvars[0]);
580 
581 	(void)ui_get_winpos(&x, &y, timeout);
582     }
583 #endif
584     list_append_number(rettv->vval.v_list, (varnumber_T)x);
585     list_append_number(rettv->vval.v_list, (varnumber_T)y);
586 }
587 
588 
589 /*
590  * "getwinposx()" function
591  */
592     void
593 f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
594 {
595     rettv->vval.v_number = -1;
596 #if defined(FEAT_GUI) \
597 	|| (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
598 	|| defined(MSWIN)
599 
600     {
601 	int	    x, y;
602 
603 	if (ui_get_winpos(&x, &y, 100) == OK)
604 	    rettv->vval.v_number = x;
605     }
606 #endif
607 }
608 
609 /*
610  * "getwinposy()" function
611  */
612     void
613 f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
614 {
615     rettv->vval.v_number = -1;
616 #if defined(FEAT_GUI) \
617 	|| (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
618 	|| defined(MSWIN)
619     {
620 	int	    x, y;
621 
622 	if (ui_get_winpos(&x, &y, 100) == OK)
623 	    rettv->vval.v_number = y;
624     }
625 #endif
626 }
627 
628 /*
629  * "tabpagenr()" function
630  */
631     void
632 f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
633 {
634     int		nr = 1;
635     char_u	*arg;
636 
637     if (in_vim9script() && check_for_opt_string_arg(argvars, 0) == FAIL)
638 	return;
639 
640     if (argvars[0].v_type != VAR_UNKNOWN)
641     {
642 	arg = tv_get_string_chk(&argvars[0]);
643 	nr = 0;
644 	if (arg != NULL)
645 	{
646 	    if (STRCMP(arg, "$") == 0)
647 		nr = tabpage_index(NULL) - 1;
648 	    else if (STRCMP(arg, "#") == 0)
649 		nr = valid_tabpage(lastused_tabpage) ?
650 					tabpage_index(lastused_tabpage) : 0;
651 	    else
652 		semsg(_(e_invalid_expression_str), arg);
653 	}
654     }
655     else
656 	nr = tabpage_index(curtab);
657     rettv->vval.v_number = nr;
658 }
659 
660 /*
661  * "tabpagewinnr()" function
662  */
663     void
664 f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
665 {
666     int		nr = 1;
667     tabpage_T	*tp;
668 
669     if (in_vim9script()
670 	    && (check_for_number_arg(argvars, 0) == FAIL
671 		|| check_for_opt_string_arg(argvars, 1) == FAIL))
672 	return;
673 
674     tp = find_tabpage((int)tv_get_number(&argvars[0]));
675     if (tp == NULL)
676 	nr = 0;
677     else
678 	nr = get_winnr(tp, &argvars[1]);
679     rettv->vval.v_number = nr;
680 }
681 
682 /*
683  * "win_execute()" function
684  */
685     void
686 f_win_execute(typval_T *argvars, typval_T *rettv)
687 {
688     int		id;
689     tabpage_T	*tp;
690     win_T	*wp;
691     win_T	*save_curwin;
692     tabpage_T	*save_curtab;
693 
694     // Return an empty string if something fails.
695     rettv->v_type = VAR_STRING;
696     rettv->vval.v_string = NULL;
697 
698     if (in_vim9script()
699 	    && (check_for_number_arg(argvars, 0) == FAIL
700 		|| check_for_string_or_list_arg(argvars, 1) == FAIL
701 		|| check_for_opt_string_arg(argvars, 2) == FAIL))
702 	return;
703 
704     id = (int)tv_get_number(argvars);
705     wp = win_id2wp_tp(id, &tp);
706     if (wp != NULL && tp != NULL)
707     {
708 	pos_T	curpos = wp->w_cursor;
709 
710 	if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE) == OK)
711 	{
712 	    check_cursor();
713 	    execute_common(argvars, rettv, 1);
714 	}
715 	restore_win_noblock(save_curwin, save_curtab, TRUE);
716 
717 	// Update the status line if the cursor moved.
718 	if (win_valid(wp) && !EQUAL_POS(curpos, wp->w_cursor))
719 	    wp->w_redr_status = TRUE;
720     }
721 }
722 
723 /*
724  * "win_findbuf()" function
725  */
726     void
727 f_win_findbuf(typval_T *argvars, typval_T *rettv)
728 {
729     if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
730 	return;
731 
732     if (rettv_list_alloc(rettv) != FAIL)
733 	win_findbuf(argvars, rettv->vval.v_list);
734 }
735 
736 /*
737  * "win_getid()" function
738  */
739     void
740 f_win_getid(typval_T *argvars, typval_T *rettv)
741 {
742     if (in_vim9script()
743 	    && (check_for_opt_number_arg(argvars, 0) == FAIL
744 		|| (argvars[0].v_type != VAR_UNKNOWN
745 		    && check_for_opt_number_arg(argvars, 1) == FAIL)))
746 	return;
747 
748     rettv->vval.v_number = win_getid(argvars);
749 }
750 
751 /*
752  * "win_gotoid()" function
753  */
754     void
755 f_win_gotoid(typval_T *argvars, typval_T *rettv)
756 {
757     win_T	*wp;
758     tabpage_T   *tp;
759     int		id;
760 
761     if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
762 	return;
763 
764     id = tv_get_number(&argvars[0]);
765 #ifdef FEAT_CMDWIN
766     if (cmdwin_type != 0)
767     {
768 	emsg(_(e_invalid_in_cmdline_window));
769 	return;
770     }
771 #endif
772     FOR_ALL_TAB_WINDOWS(tp, wp)
773 	if (wp->w_id == id)
774 	{
775 	    goto_tabpage_win(tp, wp);
776 	    rettv->vval.v_number = 1;
777 	    return;
778 	}
779 }
780 
781 /*
782  * "win_id2tabwin()" function
783  */
784     void
785 f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
786 {
787     if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
788 	return;
789 
790     if (rettv_list_alloc(rettv) != FAIL)
791 	win_id2tabwin(argvars, rettv->vval.v_list);
792 }
793 
794 /*
795  * "win_id2win()" function
796  */
797     void
798 f_win_id2win(typval_T *argvars, typval_T *rettv)
799 {
800     if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
801 	return;
802 
803     rettv->vval.v_number = win_id2win(argvars);
804 }
805 
806 /*
807  * "win_screenpos()" function
808  */
809     void
810 f_win_screenpos(typval_T *argvars, typval_T *rettv)
811 {
812     win_T	*wp;
813 
814     if (rettv_list_alloc(rettv) == FAIL)
815 	return;
816 
817     if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
818 	return;
819 
820     wp = find_win_by_nr_or_id(&argvars[0]);
821     list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
822     list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
823 }
824 
825 /*
826  * Move the window wp into a new split of targetwin in a given direction
827  */
828     static void
829 win_move_into_split(win_T *wp, win_T *targetwin, int size, int flags)
830 {
831     int	    dir;
832     int	    height = wp->w_height;
833     win_T   *oldwin = curwin;
834 
835     if (wp == targetwin)
836 	return;
837 
838     // Jump to the target window
839     if (curwin != targetwin)
840 	win_goto(targetwin);
841 
842     // Remove the old window and frame from the tree of frames
843     (void)winframe_remove(wp, &dir, NULL);
844     win_remove(wp, NULL);
845     last_status(FALSE);	    // may need to remove last status line
846     (void)win_comp_pos();   // recompute window positions
847 
848     // Split a window on the desired side and put the old window there
849     (void)win_split_ins(size, flags, wp, dir);
850 
851     // If splitting horizontally, try to preserve height
852     if (size == 0 && !(flags & WSP_VERT))
853     {
854 	win_setheight_win(height, wp);
855 	if (p_ea)
856 	    win_equal(wp, TRUE, 'v');
857     }
858 
859 #if defined(FEAT_GUI)
860     // When 'guioptions' includes 'L' or 'R' may have to remove or add
861     // scrollbars.  Have to update them anyway.
862     gui_may_update_scrollbars();
863 #endif
864 
865     if (oldwin != curwin)
866 	win_goto(oldwin);
867 }
868 
869 /*
870  * "win_splitmove()" function
871  */
872     void
873 f_win_splitmove(typval_T *argvars, typval_T *rettv)
874 {
875     win_T   *wp;
876     win_T   *targetwin;
877     int     flags = 0, size = 0;
878 
879     if (in_vim9script()
880 	    && (check_for_number_arg(argvars, 0) == FAIL
881 		|| check_for_number_arg(argvars, 1) == FAIL
882 		|| check_for_opt_dict_arg(argvars, 2) == FAIL))
883 	return;
884 
885     wp = find_win_by_nr_or_id(&argvars[0]);
886     targetwin = find_win_by_nr_or_id(&argvars[1]);
887 
888     if (wp == NULL || targetwin == NULL || wp == targetwin
889 	    || !win_valid(wp) || !win_valid(targetwin)
890 	    || win_valid_popup(wp) || win_valid_popup(targetwin))
891     {
892         emsg(_(e_invalwindow));
893 	rettv->vval.v_number = -1;
894 	return;
895     }
896 
897     if (argvars[2].v_type != VAR_UNKNOWN)
898     {
899         dict_T      *d;
900         dictitem_T  *di;
901 
902         if (argvars[2].v_type != VAR_DICT || argvars[2].vval.v_dict == NULL)
903         {
904             emsg(_(e_invarg));
905             return;
906         }
907 
908         d = argvars[2].vval.v_dict;
909         if (dict_get_bool(d, (char_u *)"vertical", FALSE))
910             flags |= WSP_VERT;
911         if ((di = dict_find(d, (char_u *)"rightbelow", -1)) != NULL)
912             flags |= tv_get_bool(&di->di_tv) ? WSP_BELOW : WSP_ABOVE;
913         size = (int)dict_get_number(d, (char_u *)"size");
914     }
915 
916     win_move_into_split(wp, targetwin, size, flags);
917 }
918 
919 /*
920  * "win_gettype(nr)" function
921  */
922     void
923 f_win_gettype(typval_T *argvars, typval_T *rettv)
924 {
925     win_T	*wp = curwin;
926 
927     rettv->v_type = VAR_STRING;
928     rettv->vval.v_string = NULL;
929 
930     if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
931 	return;
932 
933     if (argvars[0].v_type != VAR_UNKNOWN)
934     {
935 	wp = find_win_by_nr_or_id(&argvars[0]);
936 	if (wp == NULL)
937 	{
938 	    rettv->vval.v_string = vim_strsave((char_u *)"unknown");
939 	    return;
940 	}
941     }
942     if (wp == aucmd_win)
943 	rettv->vval.v_string = vim_strsave((char_u *)"autocmd");
944 #if defined(FEAT_QUICKFIX)
945     else if (wp->w_p_pvw)
946 	rettv->vval.v_string = vim_strsave((char_u *)"preview");
947 #endif
948 #ifdef FEAT_PROP_POPUP
949     else if (WIN_IS_POPUP(wp))
950 	rettv->vval.v_string = vim_strsave((char_u *)"popup");
951 #endif
952 #ifdef FEAT_CMDWIN
953     else if (wp == curwin && cmdwin_type != 0)
954 	rettv->vval.v_string = vim_strsave((char_u *)"command");
955 #endif
956 }
957 
958 /*
959  * "getcmdwintype()" function
960  */
961     void
962 f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
963 {
964     rettv->v_type = VAR_STRING;
965     rettv->vval.v_string = NULL;
966 #ifdef FEAT_CMDWIN
967     rettv->vval.v_string = alloc(2);
968     if (rettv->vval.v_string != NULL)
969     {
970 	rettv->vval.v_string[0] = cmdwin_type;
971 	rettv->vval.v_string[1] = NUL;
972     }
973 #endif
974 }
975 
976 /*
977  * "winbufnr(nr)" function
978  */
979     void
980 f_winbufnr(typval_T *argvars, typval_T *rettv)
981 {
982     win_T	*wp;
983 
984     if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
985 	return;
986 
987     wp = find_win_by_nr_or_id(&argvars[0]);
988     if (wp == NULL)
989 	rettv->vval.v_number = -1;
990     else
991 	rettv->vval.v_number = wp->w_buffer->b_fnum;
992 }
993 
994 /*
995  * "wincol()" function
996  */
997     void
998 f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
999 {
1000     validate_cursor();
1001     rettv->vval.v_number = curwin->w_wcol + 1;
1002 }
1003 
1004 /*
1005  * "winheight(nr)" function
1006  */
1007     void
1008 f_winheight(typval_T *argvars, typval_T *rettv)
1009 {
1010     win_T	*wp;
1011 
1012     if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
1013 	return;
1014 
1015     wp = find_win_by_nr_or_id(&argvars[0]);
1016     if (wp == NULL)
1017 	rettv->vval.v_number = -1;
1018     else
1019 	rettv->vval.v_number = wp->w_height;
1020 }
1021 
1022 /*
1023  * "winlayout()" function
1024  */
1025     void
1026 f_winlayout(typval_T *argvars, typval_T *rettv)
1027 {
1028     tabpage_T	*tp;
1029 
1030     if (rettv_list_alloc(rettv) != OK)
1031 	return;
1032 
1033     if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
1034 	return;
1035 
1036     if (argvars[0].v_type == VAR_UNKNOWN)
1037 	tp = curtab;
1038     else
1039     {
1040 	tp = find_tabpage((int)tv_get_number(&argvars[0]));
1041 	if (tp == NULL)
1042 	    return;
1043     }
1044 
1045     get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
1046 }
1047 
1048 /*
1049  * "winline()" function
1050  */
1051     void
1052 f_winline(typval_T *argvars UNUSED, typval_T *rettv)
1053 {
1054     validate_cursor();
1055     rettv->vval.v_number = curwin->w_wrow + 1;
1056 }
1057 
1058 /*
1059  * "winnr()" function
1060  */
1061     void
1062 f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
1063 {
1064     int		nr = 1;
1065 
1066     if (in_vim9script() && check_for_opt_string_arg(argvars, 0) == FAIL)
1067 	return;
1068 
1069     nr = get_winnr(curtab, &argvars[0]);
1070     rettv->vval.v_number = nr;
1071 }
1072 
1073 /*
1074  * "winrestcmd()" function
1075  */
1076     void
1077 f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
1078 {
1079     win_T	*wp;
1080     int		i;
1081     int		winnr;
1082     garray_T	ga;
1083     char_u	buf[50];
1084 
1085     ga_init2(&ga, (int)sizeof(char), 70);
1086 
1087     // Do this twice to handle some window layouts properly.
1088     for (i = 0; i < 2; ++i)
1089     {
1090 	winnr = 1;
1091 	FOR_ALL_WINDOWS(wp)
1092 	{
1093 	    sprintf((char *)buf, ":%dresize %d|", winnr, wp->w_height);
1094 	    ga_concat(&ga, buf);
1095 	    sprintf((char *)buf, "vert :%dresize %d|", winnr, wp->w_width);
1096 	    ga_concat(&ga, buf);
1097 	    ++winnr;
1098 	}
1099     }
1100     ga_append(&ga, NUL);
1101 
1102     rettv->vval.v_string = ga.ga_data;
1103     rettv->v_type = VAR_STRING;
1104 }
1105 
1106 /*
1107  * "winrestview()" function
1108  */
1109     void
1110 f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
1111 {
1112     dict_T	*dict;
1113 
1114     if (in_vim9script() && check_for_dict_arg(argvars, 0) == FAIL)
1115 	return;
1116 
1117     if (argvars[0].v_type != VAR_DICT
1118 	    || (dict = argvars[0].vval.v_dict) == NULL)
1119 	emsg(_(e_invarg));
1120     else
1121     {
1122 	if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
1123 	    curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
1124 	if (dict_find(dict, (char_u *)"col", -1) != NULL)
1125 	    curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
1126 	if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
1127 	    curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
1128 	if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
1129 	{
1130 	    curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
1131 	    curwin->w_set_curswant = FALSE;
1132 	}
1133 
1134 	if (dict_find(dict, (char_u *)"topline", -1) != NULL)
1135 	    set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
1136 #ifdef FEAT_DIFF
1137 	if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
1138 	    curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
1139 #endif
1140 	if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
1141 	    curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
1142 	if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
1143 	    curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
1144 
1145 	check_cursor();
1146 	win_new_height(curwin, curwin->w_height);
1147 	win_new_width(curwin, curwin->w_width);
1148 	changed_window_setting();
1149 
1150 	if (curwin->w_topline <= 0)
1151 	    curwin->w_topline = 1;
1152 	if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1153 	    curwin->w_topline = curbuf->b_ml.ml_line_count;
1154 #ifdef FEAT_DIFF
1155 	check_topfill(curwin, TRUE);
1156 #endif
1157     }
1158 }
1159 
1160 /*
1161  * "winsaveview()" function
1162  */
1163     void
1164 f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
1165 {
1166     dict_T	*dict;
1167 
1168     if (rettv_dict_alloc(rettv) == FAIL)
1169 	return;
1170     dict = rettv->vval.v_dict;
1171 
1172     dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
1173     dict_add_number(dict, "col", (long)curwin->w_cursor.col);
1174     dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
1175     update_curswant();
1176     dict_add_number(dict, "curswant", (long)curwin->w_curswant);
1177 
1178     dict_add_number(dict, "topline", (long)curwin->w_topline);
1179 #ifdef FEAT_DIFF
1180     dict_add_number(dict, "topfill", (long)curwin->w_topfill);
1181 #endif
1182     dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
1183     dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
1184 }
1185 
1186 /*
1187  * "winwidth(nr)" function
1188  */
1189     void
1190 f_winwidth(typval_T *argvars, typval_T *rettv)
1191 {
1192     win_T	*wp;
1193 
1194     if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
1195 	return;
1196 
1197     wp = find_win_by_nr_or_id(&argvars[0]);
1198     if (wp == NULL)
1199 	rettv->vval.v_number = -1;
1200     else
1201 	rettv->vval.v_number = wp->w_width;
1202 }
1203 #endif // FEAT_EVAL
1204 
1205 #if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
1206 	|| defined(PROTO)
1207 /*
1208  * Set "win" to be the curwin and "tp" to be the current tab page.
1209  * restore_win() MUST be called to undo, also when FAIL is returned.
1210  * No autocommands will be executed until restore_win() is called.
1211  * When "no_display" is TRUE the display won't be affected, no redraw is
1212  * triggered, another tabpage access is limited.
1213  * Returns FAIL if switching to "win" failed.
1214  */
1215     int
1216 switch_win(
1217     win_T	**save_curwin,
1218     tabpage_T	**save_curtab,
1219     win_T	*win,
1220     tabpage_T	*tp,
1221     int		no_display)
1222 {
1223     block_autocmds();
1224     return switch_win_noblock(save_curwin, save_curtab, win, tp, no_display);
1225 }
1226 
1227 /*
1228  * As switch_win() but without blocking autocommands.
1229  */
1230     int
1231 switch_win_noblock(
1232     win_T	**save_curwin,
1233     tabpage_T	**save_curtab,
1234     win_T	*win,
1235     tabpage_T	*tp,
1236     int		no_display)
1237 {
1238     *save_curwin = curwin;
1239     if (tp != NULL)
1240     {
1241 	*save_curtab = curtab;
1242 	if (no_display)
1243 	{
1244 	    curtab->tp_firstwin = firstwin;
1245 	    curtab->tp_lastwin = lastwin;
1246 	    curtab = tp;
1247 	    firstwin = curtab->tp_firstwin;
1248 	    lastwin = curtab->tp_lastwin;
1249 	}
1250 	else
1251 	    goto_tabpage_tp(tp, FALSE, FALSE);
1252     }
1253     if (!win_valid(win))
1254 	return FAIL;
1255     curwin = win;
1256     curbuf = curwin->w_buffer;
1257     return OK;
1258 }
1259 
1260 /*
1261  * Restore current tabpage and window saved by switch_win(), if still valid.
1262  * When "no_display" is TRUE the display won't be affected, no redraw is
1263  * triggered.
1264  */
1265     void
1266 restore_win(
1267     win_T	*save_curwin,
1268     tabpage_T	*save_curtab,
1269     int		no_display)
1270 {
1271     restore_win_noblock(save_curwin, save_curtab, no_display);
1272     unblock_autocmds();
1273 }
1274 
1275 /*
1276  * As restore_win() but without unblocking autocommands.
1277  */
1278     void
1279 restore_win_noblock(
1280     win_T	*save_curwin,
1281     tabpage_T	*save_curtab,
1282     int		no_display)
1283 {
1284     if (save_curtab != NULL && valid_tabpage(save_curtab))
1285     {
1286 	if (no_display)
1287 	{
1288 	    curtab->tp_firstwin = firstwin;
1289 	    curtab->tp_lastwin = lastwin;
1290 	    curtab = save_curtab;
1291 	    firstwin = curtab->tp_firstwin;
1292 	    lastwin = curtab->tp_lastwin;
1293 	}
1294 	else
1295 	    goto_tabpage_tp(save_curtab, FALSE, FALSE);
1296     }
1297     if (win_valid(save_curwin))
1298     {
1299 	curwin = save_curwin;
1300 	curbuf = curwin->w_buffer;
1301     }
1302 # ifdef FEAT_PROP_POPUP
1303     else if (WIN_IS_POPUP(curwin))
1304 	// original window was closed and now we're in a popup window: Go
1305 	// to the first valid window.
1306 	win_goto(firstwin);
1307 # endif
1308 }
1309 #endif
1310