blob: f2d9a0b4448d7d9d0dc73c98afc5f4969e68c489 [file] [log] [blame]
Bram Moolenaar261f3462019-09-07 15:45:32 +02001/* 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
19win_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
Bram Moolenaar261f3462019-09-07 15:45:32 +020053 static void
54win_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 *
85win_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 *
94win_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 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100106#ifdef FEAT_PROP_POPUP
Bram Moolenaar261f3462019-09-07 15:45:32 +0200107 // popup windows are in separate lists
108 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200109 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar261f3462019-09-07 15:45:32 +0200110 if (wp->w_id == id)
111 {
112 if (tpp != NULL)
113 *tpp = tp;
114 return wp;
115 }
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200116 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar261f3462019-09-07 15:45:32 +0200117 if (wp->w_id == id)
118 {
119 if (tpp != NULL)
Bram Moolenaar1f42f5a2020-09-03 18:52:24 +0200120 *tpp = curtab; // any tabpage would do
Bram Moolenaar261f3462019-09-07 15:45:32 +0200121 return wp;
122 }
123#endif
124
125 return NULL;
126}
127
128 static int
129win_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
145win_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 *
160find_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 {
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100184#ifdef FEAT_PROP_POPUP
Bram Moolenaar261f3462019-09-07 15:45:32 +0200185 // check tab-local popup windows
Bram Moolenaaree93b732020-01-14 19:05:39 +0100186 for (wp = (tp == NULL ? curtab : tp)->tp_first_popupwin;
187 wp != NULL; wp = wp->w_next)
Bram Moolenaar261f3462019-09-07 15:45:32 +0200188 if (wp->w_id == nr)
189 return wp;
190 // check global popup windows
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200191 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar261f3462019-09-07 15:45:32 +0200192 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.
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100203 * Returns NULL when not found.
Bram Moolenaar261f3462019-09-07 15:45:32 +0200204 */
205 win_T *
206find_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 *
220find_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
265get_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
315get_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;
Bram Moolenaar261f3462019-09-07 15:45:32 +0200335 }
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 }
Bram Moolenaar631ebc42020-02-03 22:15:26 +0100361 if (twin == NULL)
362 nr = 0;
Bram Moolenaar261f3462019-09-07 15:45:32 +0200363
364 if (invalid_arg)
365 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200366 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaar261f3462019-09-07 15:45:32 +0200367 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 *
390get_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);
Bram Moolenaarcdf5fdb2021-11-20 11:14:24 +0000410 dict_add_number(dict, "textoff", win_col_off(wp));
Bram Moolenaar261f3462019-09-07 15:45:32 +0200411 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
412
413#ifdef FEAT_TERMINAL
414 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
415#endif
416#ifdef FEAT_QUICKFIX
417 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
418 dict_add_number(dict, "loclist",
419 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
420#endif
421
422 // Add a reference to window variables
423 dict_add_dict(dict, "variables", wp->w_vars);
424
425 return dict;
426}
427
428/*
429 * Returns information (variables, options, etc.) about a tab page
430 * as a dictionary.
431 */
432 static dict_T *
433get_tabpage_info(tabpage_T *tp, int tp_idx)
434{
435 win_T *wp;
436 dict_T *dict;
437 list_T *l;
438
439 dict = dict_alloc();
440 if (dict == NULL)
441 return NULL;
442
443 dict_add_number(dict, "tabnr", tp_idx);
444
445 l = list_alloc();
446 if (l != NULL)
447 {
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200448 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaar261f3462019-09-07 15:45:32 +0200449 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
463f_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
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200472 if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
473 return;
474
Bram Moolenaar261f3462019-09-07 15:45:32 +0200475 if (argvars[0].v_type != VAR_UNKNOWN)
476 {
477 // Information about one tab page
478 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
479 if (tparg == NULL)
480 return;
481 }
482
483 // Get information about a specific tab page or all tab pages
484 FOR_ALL_TABPAGES(tp)
485 {
486 tpnr++;
487 if (tparg != NULL && tp != tparg)
488 continue;
489 d = get_tabpage_info(tp, tpnr);
490 if (d != NULL)
491 list_append_dict(rettv->vval.v_list, d);
492 if (tparg != NULL)
493 return;
494 }
495}
496
497/*
498 * "getwininfo()" function
499 */
500 void
501f_getwininfo(typval_T *argvars, typval_T *rettv)
502{
503 tabpage_T *tp;
504 win_T *wp = NULL, *wparg = NULL;
505 dict_T *d;
506 short tabnr = 0, winnr;
507
508 if (rettv_list_alloc(rettv) != OK)
509 return;
510
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200511 if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
512 return;
513
Bram Moolenaar261f3462019-09-07 15:45:32 +0200514 if (argvars[0].v_type != VAR_UNKNOWN)
515 {
516 wparg = win_id2wp(tv_get_number(&argvars[0]));
517 if (wparg == NULL)
518 return;
519 }
520
521 // Collect information about either all the windows across all the tab
522 // pages or one particular window.
523 FOR_ALL_TABPAGES(tp)
524 {
525 tabnr++;
526 winnr = 0;
527 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
528 {
529 winnr++;
530 if (wparg != NULL && wp != wparg)
531 continue;
532 d = get_win_info(wp, tabnr, winnr);
533 if (d != NULL)
534 list_append_dict(rettv->vval.v_list, d);
535 if (wparg != NULL)
536 // found information about a specific window
537 return;
538 }
539 }
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200540#ifdef FEAT_PROP_POPUP
541 if (wparg != NULL)
542 {
543 tabnr = 0;
544 FOR_ALL_TABPAGES(tp)
545 {
546 tabnr++;
547 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
548 if (wp == wparg)
549 break;
550 }
551 d = get_win_info(wparg, tp == NULL ? 0 : tabnr, 0);
552 if (d != NULL)
553 list_append_dict(rettv->vval.v_list, d);
554 }
555#endif
Bram Moolenaar261f3462019-09-07 15:45:32 +0200556}
557
558/*
559 * "getwinpos({timeout})" function
560 */
561 void
562f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
563{
564 int x = -1;
565 int y = -1;
566
567 if (rettv_list_alloc(rettv) == FAIL)
568 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200569
570 if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
571 return;
572
Bram Moolenaar261f3462019-09-07 15:45:32 +0200573#if defined(FEAT_GUI) \
574 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
575 || defined(MSWIN)
576 {
577 varnumber_T timeout = 100;
578
579 if (argvars[0].v_type != VAR_UNKNOWN)
580 timeout = tv_get_number(&argvars[0]);
581
582 (void)ui_get_winpos(&x, &y, timeout);
583 }
584#endif
585 list_append_number(rettv->vval.v_list, (varnumber_T)x);
586 list_append_number(rettv->vval.v_list, (varnumber_T)y);
587}
588
589
590/*
591 * "getwinposx()" function
592 */
593 void
594f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
595{
596 rettv->vval.v_number = -1;
597#if defined(FEAT_GUI) \
598 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
599 || defined(MSWIN)
600
601 {
602 int x, y;
603
604 if (ui_get_winpos(&x, &y, 100) == OK)
605 rettv->vval.v_number = x;
606 }
607#endif
608}
609
610/*
611 * "getwinposy()" function
612 */
613 void
614f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
615{
616 rettv->vval.v_number = -1;
617#if defined(FEAT_GUI) \
618 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
619 || defined(MSWIN)
620 {
621 int x, y;
622
623 if (ui_get_winpos(&x, &y, 100) == OK)
624 rettv->vval.v_number = y;
625 }
626#endif
627}
628
629/*
630 * "tabpagenr()" function
631 */
632 void
633f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
634{
635 int nr = 1;
636 char_u *arg;
637
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200638 if (in_vim9script() && check_for_opt_string_arg(argvars, 0) == FAIL)
639 return;
640
Bram Moolenaar261f3462019-09-07 15:45:32 +0200641 if (argvars[0].v_type != VAR_UNKNOWN)
642 {
643 arg = tv_get_string_chk(&argvars[0]);
644 nr = 0;
645 if (arg != NULL)
646 {
647 if (STRCMP(arg, "$") == 0)
648 nr = tabpage_index(NULL) - 1;
Bram Moolenaar62a23252020-08-09 14:04:42 +0200649 else if (STRCMP(arg, "#") == 0)
650 nr = valid_tabpage(lastused_tabpage) ?
651 tabpage_index(lastused_tabpage) : 0;
Bram Moolenaar261f3462019-09-07 15:45:32 +0200652 else
Bram Moolenaar108010a2021-06-27 22:03:33 +0200653 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaar261f3462019-09-07 15:45:32 +0200654 }
655 }
656 else
657 nr = tabpage_index(curtab);
658 rettv->vval.v_number = nr;
659}
660
661/*
662 * "tabpagewinnr()" function
663 */
664 void
665f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
666{
667 int nr = 1;
668 tabpage_T *tp;
669
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200670 if (in_vim9script()
671 && (check_for_number_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200672 || check_for_opt_string_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200673 return;
674
Bram Moolenaar261f3462019-09-07 15:45:32 +0200675 tp = find_tabpage((int)tv_get_number(&argvars[0]));
676 if (tp == NULL)
677 nr = 0;
678 else
679 nr = get_winnr(tp, &argvars[1]);
680 rettv->vval.v_number = nr;
681}
682
683/*
684 * "win_execute()" function
685 */
686 void
687f_win_execute(typval_T *argvars, typval_T *rettv)
688{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200689 int id;
Bram Moolenaar261f3462019-09-07 15:45:32 +0200690 tabpage_T *tp;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200691 win_T *wp;
Bram Moolenaar18f47402022-01-06 13:24:51 +0000692 switchwin_T switchwin;
Bram Moolenaar261f3462019-09-07 15:45:32 +0200693
Bram Moolenaar37487e12021-01-12 22:08:53 +0100694 // Return an empty string if something fails.
695 rettv->v_type = VAR_STRING;
696 rettv->vval.v_string = NULL;
697
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200698 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);
Bram Moolenaar261f3462019-09-07 15:45:32 +0200706 if (wp != NULL && tp != NULL)
707 {
Bram Moolenaar345f28d2019-10-08 22:20:35 +0200708 pos_T curpos = wp->w_cursor;
Bram Moolenaar90c317f2021-12-28 13:15:05 +0000709 char_u cwd[MAXPATHL];
710 int cwd_status;
Bram Moolenaar92f246e2021-12-28 20:03:43 +0000711#ifdef FEAT_AUTOCHDIR
Bram Moolenaar90c317f2021-12-28 13:15:05 +0000712 char_u autocwd[MAXPATHL];
713 int apply_acd = FALSE;
Bram Moolenaar92f246e2021-12-28 20:03:43 +0000714#endif
Bram Moolenaar90c317f2021-12-28 13:15:05 +0000715
716 cwd_status = mch_dirname(cwd, MAXPATHL);
717
Bram Moolenaar92f246e2021-12-28 20:03:43 +0000718#ifdef FEAT_AUTOCHDIR
Bram Moolenaar90c317f2021-12-28 13:15:05 +0000719 // If 'acd' is set, check we are using that directory. If yes, then
720 // apply 'acd' afterwards, otherwise restore the current directory.
721 if (cwd_status == OK && p_acd)
722 {
723 do_autochdir();
724 apply_acd = mch_dirname(autocwd, MAXPATHL) == OK
725 && STRCMP(cwd, autocwd) == 0;
726 }
Bram Moolenaar92f246e2021-12-28 20:03:43 +0000727#endif
Bram Moolenaar345f28d2019-10-08 22:20:35 +0200728
Bram Moolenaar18f47402022-01-06 13:24:51 +0000729 if (switch_win_noblock(&switchwin, wp, tp, TRUE) == OK)
Bram Moolenaar261f3462019-09-07 15:45:32 +0200730 {
731 check_cursor();
732 execute_common(argvars, rettv, 1);
733 }
Bram Moolenaar18f47402022-01-06 13:24:51 +0000734 restore_win_noblock(&switchwin, TRUE);
Bram Moolenaar92f246e2021-12-28 20:03:43 +0000735#ifdef FEAT_AUTOCHDIR
Bram Moolenaar90c317f2021-12-28 13:15:05 +0000736 if (apply_acd)
737 do_autochdir();
Bram Moolenaar92f246e2021-12-28 20:03:43 +0000738 else
739#endif
740 if (cwd_status == OK)
Bram Moolenaar90c317f2021-12-28 13:15:05 +0000741 mch_chdir((char *)cwd);
Bram Moolenaar345f28d2019-10-08 22:20:35 +0200742
743 // Update the status line if the cursor moved.
744 if (win_valid(wp) && !EQUAL_POS(curpos, wp->w_cursor))
745 wp->w_redr_status = TRUE;
Bram Moolenaare664a322022-01-07 14:08:03 +0000746
747 // In case the command moved the cursor or changed the Visual area,
748 // check it is valid.
749 check_cursor();
750 if (VIsual_active)
751 check_pos(curbuf, &VIsual);
Bram Moolenaar261f3462019-09-07 15:45:32 +0200752 }
753}
754
755/*
756 * "win_findbuf()" function
757 */
758 void
759f_win_findbuf(typval_T *argvars, typval_T *rettv)
760{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200761 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
762 return;
763
Bram Moolenaar261f3462019-09-07 15:45:32 +0200764 if (rettv_list_alloc(rettv) != FAIL)
765 win_findbuf(argvars, rettv->vval.v_list);
766}
767
768/*
769 * "win_getid()" function
770 */
771 void
772f_win_getid(typval_T *argvars, typval_T *rettv)
773{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200774 if (in_vim9script()
775 && (check_for_opt_number_arg(argvars, 0) == FAIL
776 || (argvars[0].v_type != VAR_UNKNOWN
777 && check_for_opt_number_arg(argvars, 1) == FAIL)))
778 return;
779
Bram Moolenaar261f3462019-09-07 15:45:32 +0200780 rettv->vval.v_number = win_getid(argvars);
781}
782
783/*
784 * "win_gotoid()" function
785 */
786 void
787f_win_gotoid(typval_T *argvars, typval_T *rettv)
788{
Bram Moolenaara046b372019-09-15 17:26:07 +0200789 win_T *wp;
790 tabpage_T *tp;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200791 int id;
Bram Moolenaara046b372019-09-15 17:26:07 +0200792
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200793 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
794 return;
795
796 id = tv_get_number(&argvars[0]);
Bram Moolenaara046b372019-09-15 17:26:07 +0200797#ifdef FEAT_CMDWIN
798 if (cmdwin_type != 0)
799 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200800 emsg(_(e_invalid_in_cmdline_window));
Bram Moolenaara046b372019-09-15 17:26:07 +0200801 return;
802 }
803#endif
804 FOR_ALL_TAB_WINDOWS(tp, wp)
805 if (wp->w_id == id)
806 {
807 goto_tabpage_win(tp, wp);
808 rettv->vval.v_number = 1;
809 return;
810 }
Bram Moolenaar261f3462019-09-07 15:45:32 +0200811}
812
813/*
814 * "win_id2tabwin()" function
815 */
816 void
817f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
818{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200819 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
820 return;
821
Bram Moolenaar261f3462019-09-07 15:45:32 +0200822 if (rettv_list_alloc(rettv) != FAIL)
823 win_id2tabwin(argvars, rettv->vval.v_list);
824}
825
826/*
827 * "win_id2win()" function
828 */
829 void
830f_win_id2win(typval_T *argvars, typval_T *rettv)
831{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200832 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
833 return;
834
Bram Moolenaar261f3462019-09-07 15:45:32 +0200835 rettv->vval.v_number = win_id2win(argvars);
836}
837
838/*
Daniel Steinbergee630312022-01-10 13:36:34 +0000839 * "win_move_separator()" function
840 */
841 void
842f_win_move_separator(typval_T *argvars, typval_T *rettv)
843{
844 win_T *wp;
845 int offset;
846
847 rettv->vval.v_number = FALSE;
848
849 if (in_vim9script()
850 && (check_for_number_arg(argvars, 0) == FAIL
851 || check_for_number_arg(argvars, 1) == FAIL))
852 return;
853
854 wp = find_win_by_nr_or_id(&argvars[0]);
855 if (wp == NULL || win_valid_popup(wp))
856 return;
857
858 offset = (int)tv_get_number(&argvars[1]);
859 win_drag_vsep_line(wp, offset);
860 rettv->vval.v_number = TRUE;
861}
862
863/*
864 * "win_move_statusline()" function
865 */
866 void
867f_win_move_statusline(typval_T *argvars, typval_T *rettv)
868{
869 win_T *wp;
870 int offset;
871
872 rettv->vval.v_number = FALSE;
873
874 if (in_vim9script()
875 && (check_for_number_arg(argvars, 0) == FAIL
876 || check_for_number_arg(argvars, 1) == FAIL))
877 return;
878
879 wp = find_win_by_nr_or_id(&argvars[0]);
880 if (wp == NULL || win_valid_popup(wp))
881 return;
882
883 offset = (int)tv_get_number(&argvars[1]);
884 win_drag_status_line(wp, offset);
885 rettv->vval.v_number = TRUE;
886}
887
888/*
Bram Moolenaar261f3462019-09-07 15:45:32 +0200889 * "win_screenpos()" function
890 */
891 void
892f_win_screenpos(typval_T *argvars, typval_T *rettv)
893{
894 win_T *wp;
895
896 if (rettv_list_alloc(rettv) == FAIL)
897 return;
898
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200899 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
900 return;
901
Bram Moolenaar261f3462019-09-07 15:45:32 +0200902 wp = find_win_by_nr_or_id(&argvars[0]);
903 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
904 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
905}
906
907/*
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200908 * Move the window wp into a new split of targetwin in a given direction
909 */
910 static void
911win_move_into_split(win_T *wp, win_T *targetwin, int size, int flags)
912{
913 int dir;
914 int height = wp->w_height;
915 win_T *oldwin = curwin;
916
917 if (wp == targetwin)
918 return;
919
920 // Jump to the target window
921 if (curwin != targetwin)
922 win_goto(targetwin);
923
924 // Remove the old window and frame from the tree of frames
925 (void)winframe_remove(wp, &dir, NULL);
926 win_remove(wp, NULL);
927 last_status(FALSE); // may need to remove last status line
928 (void)win_comp_pos(); // recompute window positions
929
930 // Split a window on the desired side and put the old window there
931 (void)win_split_ins(size, flags, wp, dir);
932
933 // If splitting horizontally, try to preserve height
934 if (size == 0 && !(flags & WSP_VERT))
935 {
936 win_setheight_win(height, wp);
937 if (p_ea)
938 win_equal(wp, TRUE, 'v');
939 }
940
941#if defined(FEAT_GUI)
942 // When 'guioptions' includes 'L' or 'R' may have to remove or add
943 // scrollbars. Have to update them anyway.
944 gui_may_update_scrollbars();
945#endif
946
947 if (oldwin != curwin)
948 win_goto(oldwin);
949}
950
951/*
952 * "win_splitmove()" function
953 */
954 void
955f_win_splitmove(typval_T *argvars, typval_T *rettv)
956{
957 win_T *wp;
958 win_T *targetwin;
959 int flags = 0, size = 0;
960
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200961 if (in_vim9script()
962 && (check_for_number_arg(argvars, 0) == FAIL
963 || check_for_number_arg(argvars, 1) == FAIL
964 || check_for_opt_dict_arg(argvars, 2) == FAIL))
965 return;
966
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200967 wp = find_win_by_nr_or_id(&argvars[0]);
968 targetwin = find_win_by_nr_or_id(&argvars[1]);
969
Bram Moolenaar7b94e772020-01-06 21:03:24 +0100970 if (wp == NULL || targetwin == NULL || wp == targetwin
Bram Moolenaar0f1563f2020-03-20 21:15:51 +0100971 || !win_valid(wp) || !win_valid(targetwin)
972 || win_valid_popup(wp) || win_valid_popup(targetwin))
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200973 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +0000974 emsg(_(e_invalid_window_number));
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200975 rettv->vval.v_number = -1;
976 return;
977 }
978
979 if (argvars[2].v_type != VAR_UNKNOWN)
980 {
981 dict_T *d;
982 dictitem_T *di;
983
984 if (argvars[2].v_type != VAR_DICT || argvars[2].vval.v_dict == NULL)
985 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000986 emsg(_(e_invalid_argument));
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200987 return;
988 }
989
990 d = argvars[2].vval.v_dict;
Bram Moolenaar4b9bd692020-09-05 21:57:53 +0200991 if (dict_get_bool(d, (char_u *)"vertical", FALSE))
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200992 flags |= WSP_VERT;
993 if ((di = dict_find(d, (char_u *)"rightbelow", -1)) != NULL)
Bram Moolenaar4b9bd692020-09-05 21:57:53 +0200994 flags |= tv_get_bool(&di->di_tv) ? WSP_BELOW : WSP_ABOVE;
Bram Moolenaard20dcb32019-09-10 21:22:58 +0200995 size = (int)dict_get_number(d, (char_u *)"size");
996 }
997
998 win_move_into_split(wp, targetwin, size, flags);
999}
1000
1001/*
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001002 * "win_gettype(nr)" function
1003 */
1004 void
1005f_win_gettype(typval_T *argvars, typval_T *rettv)
1006{
1007 win_T *wp = curwin;
1008
1009 rettv->v_type = VAR_STRING;
1010 rettv->vval.v_string = NULL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001011
1012 if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
1013 return;
1014
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001015 if (argvars[0].v_type != VAR_UNKNOWN)
1016 {
1017 wp = find_win_by_nr_or_id(&argvars[0]);
1018 if (wp == NULL)
1019 {
1020 rettv->vval.v_string = vim_strsave((char_u *)"unknown");
1021 return;
1022 }
1023 }
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02001024 if (wp == aucmd_win)
Bram Moolenaar40a019f2020-06-17 21:41:35 +02001025 rettv->vval.v_string = vim_strsave((char_u *)"autocmd");
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02001026#if defined(FEAT_QUICKFIX)
1027 else if (wp->w_p_pvw)
1028 rettv->vval.v_string = vim_strsave((char_u *)"preview");
1029#endif
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001030#ifdef FEAT_PROP_POPUP
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02001031 else if (WIN_IS_POPUP(wp))
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001032 rettv->vval.v_string = vim_strsave((char_u *)"popup");
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001033#endif
1034#ifdef FEAT_CMDWIN
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02001035 else if (wp == curwin && cmdwin_type != 0)
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001036 rettv->vval.v_string = vim_strsave((char_u *)"command");
1037#endif
Yegappan Lakshmanan28d84212021-07-31 12:43:23 +02001038#ifdef FEAT_QUICKFIX
1039 else if (bt_quickfix(wp->w_buffer))
1040 rettv->vval.v_string = vim_strsave((char_u *)
1041 (wp->w_llist_ref != NULL ? "loclist" : "quickfix"));
1042#endif
1043
Bram Moolenaar00f3b4e2020-02-14 14:32:22 +01001044}
1045
1046/*
1047 * "getcmdwintype()" function
1048 */
1049 void
1050f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
1051{
1052 rettv->v_type = VAR_STRING;
1053 rettv->vval.v_string = NULL;
1054#ifdef FEAT_CMDWIN
1055 rettv->vval.v_string = alloc(2);
1056 if (rettv->vval.v_string != NULL)
1057 {
1058 rettv->vval.v_string[0] = cmdwin_type;
1059 rettv->vval.v_string[1] = NUL;
1060 }
1061#endif
1062}
1063
1064/*
Bram Moolenaar261f3462019-09-07 15:45:32 +02001065 * "winbufnr(nr)" function
1066 */
1067 void
1068f_winbufnr(typval_T *argvars, typval_T *rettv)
1069{
1070 win_T *wp;
1071
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001072 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
1073 return;
1074
Bram Moolenaar261f3462019-09-07 15:45:32 +02001075 wp = find_win_by_nr_or_id(&argvars[0]);
1076 if (wp == NULL)
1077 rettv->vval.v_number = -1;
1078 else
1079 rettv->vval.v_number = wp->w_buffer->b_fnum;
1080}
1081
1082/*
1083 * "wincol()" function
1084 */
1085 void
1086f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
1087{
1088 validate_cursor();
1089 rettv->vval.v_number = curwin->w_wcol + 1;
1090}
1091
1092/*
1093 * "winheight(nr)" function
1094 */
1095 void
1096f_winheight(typval_T *argvars, typval_T *rettv)
1097{
1098 win_T *wp;
1099
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001100 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
1101 return;
1102
Bram Moolenaar261f3462019-09-07 15:45:32 +02001103 wp = find_win_by_nr_or_id(&argvars[0]);
1104 if (wp == NULL)
1105 rettv->vval.v_number = -1;
1106 else
1107 rettv->vval.v_number = wp->w_height;
1108}
1109
1110/*
1111 * "winlayout()" function
1112 */
1113 void
1114f_winlayout(typval_T *argvars, typval_T *rettv)
1115{
1116 tabpage_T *tp;
1117
1118 if (rettv_list_alloc(rettv) != OK)
1119 return;
1120
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001121 if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
1122 return;
1123
Bram Moolenaar261f3462019-09-07 15:45:32 +02001124 if (argvars[0].v_type == VAR_UNKNOWN)
1125 tp = curtab;
1126 else
1127 {
1128 tp = find_tabpage((int)tv_get_number(&argvars[0]));
1129 if (tp == NULL)
1130 return;
1131 }
1132
1133 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
1134}
1135
1136/*
1137 * "winline()" function
1138 */
1139 void
1140f_winline(typval_T *argvars UNUSED, typval_T *rettv)
1141{
1142 validate_cursor();
1143 rettv->vval.v_number = curwin->w_wrow + 1;
1144}
1145
1146/*
1147 * "winnr()" function
1148 */
1149 void
1150f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
1151{
1152 int nr = 1;
1153
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001154 if (in_vim9script() && check_for_opt_string_arg(argvars, 0) == FAIL)
1155 return;
1156
Bram Moolenaar261f3462019-09-07 15:45:32 +02001157 nr = get_winnr(curtab, &argvars[0]);
1158 rettv->vval.v_number = nr;
1159}
1160
1161/*
1162 * "winrestcmd()" function
1163 */
1164 void
1165f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
1166{
1167 win_T *wp;
Bram Moolenaara0c8aea2021-03-20 19:55:35 +01001168 int i;
1169 int winnr;
Bram Moolenaar261f3462019-09-07 15:45:32 +02001170 garray_T ga;
1171 char_u buf[50];
1172
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001173 ga_init2(&ga, sizeof(char), 70);
Bram Moolenaara0c8aea2021-03-20 19:55:35 +01001174
1175 // Do this twice to handle some window layouts properly.
1176 for (i = 0; i < 2; ++i)
Bram Moolenaar261f3462019-09-07 15:45:32 +02001177 {
Bram Moolenaara0c8aea2021-03-20 19:55:35 +01001178 winnr = 1;
1179 FOR_ALL_WINDOWS(wp)
1180 {
1181 sprintf((char *)buf, ":%dresize %d|", winnr, wp->w_height);
1182 ga_concat(&ga, buf);
1183 sprintf((char *)buf, "vert :%dresize %d|", winnr, wp->w_width);
1184 ga_concat(&ga, buf);
1185 ++winnr;
1186 }
Bram Moolenaar261f3462019-09-07 15:45:32 +02001187 }
1188 ga_append(&ga, NUL);
1189
1190 rettv->vval.v_string = ga.ga_data;
1191 rettv->v_type = VAR_STRING;
1192}
1193
1194/*
1195 * "winrestview()" function
1196 */
1197 void
1198f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
1199{
1200 dict_T *dict;
1201
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001202 if (in_vim9script() && check_for_dict_arg(argvars, 0) == FAIL)
1203 return;
1204
Bram Moolenaar261f3462019-09-07 15:45:32 +02001205 if (argvars[0].v_type != VAR_DICT
1206 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001207 emsg(_(e_invalid_argument));
Bram Moolenaar261f3462019-09-07 15:45:32 +02001208 else
1209 {
1210 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
1211 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
1212 if (dict_find(dict, (char_u *)"col", -1) != NULL)
1213 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
1214 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
1215 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
1216 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
1217 {
1218 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
1219 curwin->w_set_curswant = FALSE;
1220 }
1221
1222 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
1223 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
1224#ifdef FEAT_DIFF
1225 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
1226 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
1227#endif
1228 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
1229 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
1230 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
1231 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
1232
1233 check_cursor();
1234 win_new_height(curwin, curwin->w_height);
1235 win_new_width(curwin, curwin->w_width);
1236 changed_window_setting();
1237
1238 if (curwin->w_topline <= 0)
1239 curwin->w_topline = 1;
1240 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1241 curwin->w_topline = curbuf->b_ml.ml_line_count;
1242#ifdef FEAT_DIFF
1243 check_topfill(curwin, TRUE);
1244#endif
1245 }
1246}
1247
1248/*
1249 * "winsaveview()" function
1250 */
1251 void
1252f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
1253{
1254 dict_T *dict;
1255
1256 if (rettv_dict_alloc(rettv) == FAIL)
1257 return;
1258 dict = rettv->vval.v_dict;
1259
1260 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
1261 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
1262 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
1263 update_curswant();
1264 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
1265
1266 dict_add_number(dict, "topline", (long)curwin->w_topline);
1267#ifdef FEAT_DIFF
1268 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
1269#endif
1270 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
1271 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
1272}
1273
1274/*
1275 * "winwidth(nr)" function
1276 */
1277 void
1278f_winwidth(typval_T *argvars, typval_T *rettv)
1279{
1280 win_T *wp;
1281
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001282 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
1283 return;
1284
Bram Moolenaar261f3462019-09-07 15:45:32 +02001285 wp = find_win_by_nr_or_id(&argvars[0]);
1286 if (wp == NULL)
1287 rettv->vval.v_number = -1;
1288 else
1289 rettv->vval.v_number = wp->w_width;
1290}
1291#endif // FEAT_EVAL
1292
1293#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
1294 || defined(PROTO)
1295/*
1296 * Set "win" to be the curwin and "tp" to be the current tab page.
1297 * restore_win() MUST be called to undo, also when FAIL is returned.
1298 * No autocommands will be executed until restore_win() is called.
1299 * When "no_display" is TRUE the display won't be affected, no redraw is
1300 * triggered, another tabpage access is limited.
1301 * Returns FAIL if switching to "win" failed.
1302 */
1303 int
1304switch_win(
Bram Moolenaar18f47402022-01-06 13:24:51 +00001305 switchwin_T *switchwin,
1306 win_T *win,
1307 tabpage_T *tp,
1308 int no_display)
Bram Moolenaar261f3462019-09-07 15:45:32 +02001309{
1310 block_autocmds();
Bram Moolenaar18f47402022-01-06 13:24:51 +00001311 return switch_win_noblock(switchwin, win, tp, no_display);
Bram Moolenaar261f3462019-09-07 15:45:32 +02001312}
1313
1314/*
1315 * As switch_win() but without blocking autocommands.
1316 */
1317 int
1318switch_win_noblock(
Bram Moolenaar18f47402022-01-06 13:24:51 +00001319 switchwin_T *switchwin,
1320 win_T *win,
1321 tabpage_T *tp,
1322 int no_display)
Bram Moolenaar261f3462019-09-07 15:45:32 +02001323{
Bram Moolenaar18f47402022-01-06 13:24:51 +00001324 CLEAR_POINTER(switchwin);
1325 switchwin->sw_curwin = curwin;
1326 if (win == curwin)
1327 switchwin->sw_same_win = TRUE;
1328 else
1329 {
1330 // Disable Visual selection, because redrawing may fail.
1331 switchwin->sw_visual_active = VIsual_active;
1332 VIsual_active = FALSE;
1333 }
1334
Bram Moolenaar261f3462019-09-07 15:45:32 +02001335 if (tp != NULL)
1336 {
Bram Moolenaar18f47402022-01-06 13:24:51 +00001337 switchwin->sw_curtab = curtab;
Bram Moolenaar261f3462019-09-07 15:45:32 +02001338 if (no_display)
1339 {
1340 curtab->tp_firstwin = firstwin;
1341 curtab->tp_lastwin = lastwin;
Bram Moolenaardab17a02021-12-20 21:35:59 +00001342 curtab->tp_topframe = topframe;
Bram Moolenaar261f3462019-09-07 15:45:32 +02001343 curtab = tp;
1344 firstwin = curtab->tp_firstwin;
1345 lastwin = curtab->tp_lastwin;
Bram Moolenaardab17a02021-12-20 21:35:59 +00001346 topframe = curtab->tp_topframe;
Bram Moolenaar261f3462019-09-07 15:45:32 +02001347 }
1348 else
1349 goto_tabpage_tp(tp, FALSE, FALSE);
1350 }
1351 if (!win_valid(win))
1352 return FAIL;
1353 curwin = win;
1354 curbuf = curwin->w_buffer;
1355 return OK;
1356}
1357
1358/*
1359 * Restore current tabpage and window saved by switch_win(), if still valid.
1360 * When "no_display" is TRUE the display won't be affected, no redraw is
1361 * triggered.
1362 */
1363 void
1364restore_win(
Bram Moolenaar18f47402022-01-06 13:24:51 +00001365 switchwin_T *switchwin,
1366 int no_display)
Bram Moolenaar261f3462019-09-07 15:45:32 +02001367{
Bram Moolenaar18f47402022-01-06 13:24:51 +00001368 restore_win_noblock(switchwin, no_display);
Bram Moolenaar261f3462019-09-07 15:45:32 +02001369 unblock_autocmds();
1370}
1371
1372/*
1373 * As restore_win() but without unblocking autocommands.
1374 */
1375 void
1376restore_win_noblock(
Bram Moolenaar18f47402022-01-06 13:24:51 +00001377 switchwin_T *switchwin,
1378 int no_display)
Bram Moolenaar261f3462019-09-07 15:45:32 +02001379{
Bram Moolenaar18f47402022-01-06 13:24:51 +00001380 if (switchwin->sw_curtab != NULL && valid_tabpage(switchwin->sw_curtab))
Bram Moolenaar261f3462019-09-07 15:45:32 +02001381 {
1382 if (no_display)
1383 {
1384 curtab->tp_firstwin = firstwin;
1385 curtab->tp_lastwin = lastwin;
Bram Moolenaardab17a02021-12-20 21:35:59 +00001386 curtab->tp_topframe = topframe;
Bram Moolenaar18f47402022-01-06 13:24:51 +00001387 curtab = switchwin->sw_curtab;
Bram Moolenaar261f3462019-09-07 15:45:32 +02001388 firstwin = curtab->tp_firstwin;
1389 lastwin = curtab->tp_lastwin;
Bram Moolenaardab17a02021-12-20 21:35:59 +00001390 topframe = curtab->tp_topframe;
Bram Moolenaar261f3462019-09-07 15:45:32 +02001391 }
1392 else
Bram Moolenaar18f47402022-01-06 13:24:51 +00001393 goto_tabpage_tp(switchwin->sw_curtab, FALSE, FALSE);
Bram Moolenaar261f3462019-09-07 15:45:32 +02001394 }
Bram Moolenaar18f47402022-01-06 13:24:51 +00001395
1396 if (!switchwin->sw_same_win)
1397 VIsual_active = switchwin->sw_visual_active;
1398
1399 if (win_valid(switchwin->sw_curwin))
Bram Moolenaar261f3462019-09-07 15:45:32 +02001400 {
Bram Moolenaar18f47402022-01-06 13:24:51 +00001401 curwin = switchwin->sw_curwin;
Bram Moolenaar261f3462019-09-07 15:45:32 +02001402 curbuf = curwin->w_buffer;
1403 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001404# ifdef FEAT_PROP_POPUP
Bram Moolenaar261f3462019-09-07 15:45:32 +02001405 else if (WIN_IS_POPUP(curwin))
1406 // original window was closed and now we're in a popup window: Go
1407 // to the first valid window.
1408 win_goto(firstwin);
1409# endif
Bram Moolenaar261f3462019-09-07 15:45:32 +02001410}
1411#endif