robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
23.1 KiB910 linesraw
1// eval/window.c: Window related builtin functions
2
3#include <stdbool.h>
4#include <stdint.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8
9#include "nvim/ascii_defs.h"
10#include "nvim/autocmd.h"
11#include "nvim/buffer.h"
12#include "nvim/charset.h"
13#include "nvim/context.h"
14#include "nvim/cursor.h"
15#include "nvim/errors.h"
16#include "nvim/eval/funcs.h"
17#include "nvim/eval/typval.h"
18#include "nvim/eval/typval_defs.h"
19#include "nvim/eval/window.h"
20#include "nvim/ex_getln.h"
21#include "nvim/garray.h"
22#include "nvim/garray_defs.h"
23#include "nvim/gettext_defs.h"
24#include "nvim/globals.h"
25#include "nvim/macros_defs.h"
26#include "nvim/mark_defs.h"
27#include "nvim/memory.h"
28#include "nvim/message.h"
29#include "nvim/move.h"
30#include "nvim/normal.h"
31#include "nvim/option_vars.h"
32#include "nvim/os/fs.h"
33#include "nvim/pos_defs.h"
34#include "nvim/strings.h"
35#include "nvim/types_defs.h"
36#include "nvim/vim_defs.h"
37#include "nvim/window.h"
38
39#include "eval/window.c.generated.h"
40
41static const char e_cannot_resize_window_in_another_tab_page[]
42  = N_("E1308: Cannot resize a window in another tab page");
43
44bool win_has_winnr(win_T *wp, tabpage_T *tp)
45  FUNC_ATTR_NONNULL_ALL
46{
47  return (wp == (tp == curtab ? curwin : tp->tp_curwin))
48         || (!wp->w_config.hide && wp->w_config.focusable);
49}
50
51win_T *win_find_nr(int nr, tabpage_T *tp)
52{
53  if (tp == NULL) {
54    assert(curtab);
55    tp = curtab;
56  }
57  FOR_ALL_WINDOWS_IN_TAB(wp, tp) {
58    if ((nr -= win_has_winnr(wp, tp)) <= 0) {
59      return wp;
60    }
61  }
62  return NULL;
63}
64
65static int win_getid(typval_T *argvars)
66{
67  if (argvars[0].v_type == VAR_UNKNOWN) {
68    return curwin->handle;
69  }
70  int winnr = (int)tv_get_number(&argvars[0]);
71  win_T *wp;
72  if (winnr <= 0) {
73    return 0;
74  }
75
76  tabpage_T *tp = NULL;
77  if (argvars[1].v_type == VAR_UNKNOWN) {
78    tp = curtab;
79    wp = firstwin;
80  } else {
81    int tabnr = (int)tv_get_number(&argvars[1]);
82    FOR_ALL_TABS(tp2) {
83      if (--tabnr == 0) {
84        tp = tp2;
85        break;
86      }
87    }
88    if (tp == NULL) {
89      return -1;
90    }
91    if (tp == curtab) {
92      wp = firstwin;
93    } else {
94      wp = tp->tp_firstwin;
95    }
96  }
97  for (; wp != NULL; wp = wp->w_next) {
98    if ((winnr -= win_has_winnr(wp, tp)) == 0) {
99      return wp->handle;
100    }
101  }
102  return 0;
103}
104
105static void win_id2tabwin(typval_T *const argvars, typval_T *const rettv)
106{
107  handle_T id = (handle_T)tv_get_number(&argvars[0]);
108
109  int winnr = 1;
110  int tabnr = 1;
111  win_get_tabwin(id, &tabnr, &winnr);
112
113  list_T *const list = tv_list_alloc_ret(rettv, 2);
114  tv_list_append_number(list, tabnr);
115  tv_list_append_number(list, winnr);
116}
117
118win_T *win_id2wp(int id)
119{
120  return win_id2wp_tp(id, NULL);
121}
122
123/// Return the window and tab pointer of window "id".
124/// Returns NULL when not found.
125win_T *win_id2wp_tp(int id, tabpage_T **tpp)
126{
127  FOR_ALL_TAB_WINDOWS(tp, wp) {
128    if (wp->handle == id) {
129      if (tpp != NULL) {
130        *tpp = tp;
131      }
132      return wp;
133    }
134  }
135
136  return NULL;
137}
138
139static int win_id2win(typval_T *argvars)
140{
141  int nr = 1;
142  int id = (int)tv_get_number(&argvars[0]);
143
144  FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
145    if (wp->handle == id) {
146      return (win_has_winnr(wp, curtab) ? nr : 0);
147    }
148    nr += win_has_winnr(wp, curtab);
149  }
150  return 0;
151}
152
153void win_findbuf(typval_T *argvars, list_T *list)
154{
155  int bufnr = (int)tv_get_number(&argvars[0]);
156
157  FOR_ALL_TAB_WINDOWS(tp, wp) {
158    if (wp->w_buffer->b_fnum == bufnr) {
159      tv_list_append_number(list, wp->handle);
160    }
161  }
162}
163
164/// Find window specified by "vp" in tabpage "tp".
165///
166/// @param tp  NULL for current tab page
167/// @return  current window if "vp" is number zero.
168///          NULL if not found.
169win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp)
170{
171  int nr = (int)tv_get_number_chk(vp, NULL);
172
173  if (nr < 0) {
174    return NULL;
175  }
176
177  if (nr == 0) {
178    return curwin;
179  }
180
181  // This method accepts NULL as an alias for curtab.
182  if (tp == NULL) {
183    tp = curtab;
184  }
185
186  if (nr >= LOWEST_WIN_ID) {
187    // window id
188    FOR_ALL_WINDOWS_IN_TAB(wp, tp) {
189      if (wp->handle == nr) {
190        return wp;
191      }
192    }
193    return NULL;
194  }
195
196  return win_find_nr(nr, tp);
197}
198
199/// Find a window: When using a Window ID in any tab page, when using a number
200/// in the current tab page.
201/// Returns NULL when not found.
202win_T *find_win_by_nr_or_id(typval_T *vp)
203{
204  int nr = (int)tv_get_number_chk(vp, NULL);
205
206  if (nr >= LOWEST_WIN_ID) {
207    return win_id2wp((int)tv_get_number(vp));
208  }
209
210  return find_win_by_nr(vp, NULL);
211}
212
213/// Find window specified by "wvp" in tabpage "tvp".
214win_T *find_tabwin(typval_T *wvp, typval_T *tvp)
215{
216  win_T *wp = NULL;
217  tabpage_T *tp = NULL;
218
219  if (wvp->v_type != VAR_UNKNOWN) {
220    if (tvp->v_type != VAR_UNKNOWN) {
221      int n = (int)tv_get_number(tvp);
222      if (n >= 0) {
223        tp = find_tabpage(n);
224      }
225    } else {
226      tp = curtab;
227    }
228
229    if (tp != NULL) {
230      wp = find_win_by_nr(wvp, tp);
231    }
232  } else {
233    wp = curwin;
234  }
235
236  return wp;
237}
238
239/// Get the layout of the given tab page for winlayout().
240static void get_framelayout(const frame_T *fr, list_T *l, bool outer)
241{
242  if (fr == NULL) {
243    return;
244  }
245
246  list_T *fr_list;
247  if (outer) {
248    // outermost call from f_winlayout()
249    fr_list = l;
250  } else {
251    fr_list = tv_list_alloc(2);
252    tv_list_append_list(l, fr_list);
253  }
254
255  if (fr->fr_layout == FR_LEAF) {
256    if (fr->fr_win != NULL) {
257      tv_list_append_string(fr_list, S_LEN("leaf"));
258      tv_list_append_number(fr_list, fr->fr_win->handle);
259    }
260  } else {
261    if (fr->fr_layout == FR_ROW) {
262      tv_list_append_string(fr_list, S_LEN("row"));
263    } else {
264      tv_list_append_string(fr_list, S_LEN("col"));
265    }
266
267    list_T *const win_list = tv_list_alloc(kListLenUnknown);
268    tv_list_append_list(fr_list, win_list);
269    const frame_T *child = fr->fr_child;
270    while (child != NULL) {
271      get_framelayout(child, win_list, false);
272      child = child->fr_next;
273    }
274  }
275}
276
277/// Common code for tabpagewinnr() and winnr().
278static int get_winnr(tabpage_T *tp, typval_T *argvar)
279{
280  int nr = 1;
281
282  win_T *twin = (tp == curtab) ? curwin : tp->tp_curwin;
283  if (argvar->v_type != VAR_UNKNOWN) {
284    bool invalid_arg = false;
285    const char *const arg = tv_get_string_chk(argvar);
286    if (arg == NULL) {
287      nr = 0;  // Type error; errmsg already given.
288    } else if (strcmp(arg, "$") == 0) {
289      twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
290    } else if (strcmp(arg, "#") == 0) {
291      twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
292      if (twin == NULL) {
293        nr = 0;
294      }
295    } else {
296      // Extract the window count (if specified). e.g. winnr('3j')
297      char *endp = (char *)arg;
298      int count = getdigits_int(&endp, false, 0);
299      if (count <= 0) {
300        // if count is not specified, default to 1
301        count = 1;
302      }
303      if (endp != NULL && *endp != NUL) {
304        if (strequal(endp, "j")) {
305          twin = win_vert_neighbor(tp, twin, false, count);
306        } else if (strequal(endp, "k")) {
307          twin = win_vert_neighbor(tp, twin, true, count);
308        } else if (strequal(endp, "h")) {
309          twin = win_horz_neighbor(tp, twin, true, count);
310        } else if (strequal(endp, "l")) {
311          twin = win_horz_neighbor(tp, twin, false, count);
312        } else {
313          invalid_arg = true;
314        }
315      } else {
316        invalid_arg = true;
317      }
318    }
319
320    if (invalid_arg) {
321      semsg(_(e_invexpr2), arg);
322      nr = 0;
323    }
324  } else if (!win_has_winnr(twin, tp)) {
325    nr = 0;
326  }
327
328  if (nr <= 0) {
329    return 0;
330  }
331
332  nr = 0;
333  win_T *wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
334  for (; wp != NULL; wp = wp->w_next) {
335    nr += win_has_winnr(wp, tp);
336    if (wp == twin) {
337      break;
338    }
339  }
340  if (wp == NULL) {
341    nr = 0;  // didn't find it in this tabpage
342  }
343  return nr;
344}
345
346/// @return  information about a window as a dictionary.
347static dict_T *get_win_info(win_T *wp, int16_t tpnr, int16_t winnr)
348{
349  dict_T *const dict = tv_dict_alloc();
350
351  // make sure w_botline is valid
352  validate_botline_win(wp);
353
354  tv_dict_add_nr(dict, S_LEN("tabnr"), tpnr);
355  tv_dict_add_nr(dict, S_LEN("winnr"), winnr);
356  tv_dict_add_nr(dict, S_LEN("winid"), wp->handle);
357  tv_dict_add_nr(dict, S_LEN("height"), wp->w_view_height);
358  tv_dict_add_nr(dict, S_LEN("status_height"), wp->w_status_height);
359  tv_dict_add_nr(dict, S_LEN("winrow"), wp->w_winrow + 1);
360  tv_dict_add_nr(dict, S_LEN("topline"), wp->w_topline);
361  tv_dict_add_nr(dict, S_LEN("botline"), wp->w_botline - 1);
362  tv_dict_add_nr(dict, S_LEN("leftcol"), wp->w_leftcol);
363  tv_dict_add_nr(dict, S_LEN("winbar"), wp->w_winbar_height);
364  tv_dict_add_nr(dict, S_LEN("width"), wp->w_view_width);
365  tv_dict_add_nr(dict, S_LEN("bufnr"), wp->w_buffer->b_fnum);
366  tv_dict_add_nr(dict, S_LEN("wincol"), wp->w_wincol + 1);
367  tv_dict_add_nr(dict, S_LEN("textoff"), win_col_off(wp));
368  tv_dict_add_nr(dict, S_LEN("terminal"), bt_terminal(wp->w_buffer));
369  tv_dict_add_nr(dict, S_LEN("quickfix"), bt_quickfix(wp->w_buffer));
370  tv_dict_add_nr(dict, S_LEN("loclist"),
371                 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
372
373  // Add a reference to window variables
374  tv_dict_add_dict(dict, S_LEN("variables"), wp->w_vars);
375
376  return dict;
377}
378
379/// @return  information (variables, options, etc.) about a tab page
380///          as a dictionary.
381static dict_T *get_tabpage_info(tabpage_T *tp, int tp_idx)
382{
383  dict_T *const dict = tv_dict_alloc();
384
385  tv_dict_add_nr(dict, S_LEN("tabnr"), tp_idx);
386
387  list_T *const l = tv_list_alloc(kListLenMayKnow);
388  FOR_ALL_WINDOWS_IN_TAB(wp, tp) {
389    tv_list_append_number(l, (varnumber_T)wp->handle);
390  }
391  tv_dict_add_list(dict, S_LEN("windows"), l);
392
393  // Make a reference to tabpage variables
394  tv_dict_add_dict(dict, S_LEN("variables"), tp->tp_vars);
395
396  return dict;
397}
398
399/// "gettabinfo()" function
400void f_gettabinfo(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
401{
402  tabpage_T *tparg = NULL;
403
404  tv_list_alloc_ret(rettv, (argvars[0].v_type == VAR_UNKNOWN
405                            ? 1
406                            : kListLenMayKnow));
407
408  if (argvars[0].v_type != VAR_UNKNOWN) {
409    // Information about one tab page
410    tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
411    if (tparg == NULL) {
412      return;
413    }
414  }
415
416  // Get information about a specific tab page or all tab pages
417  int tpnr = 0;
418  FOR_ALL_TABS(tp) {
419    tpnr++;
420    if (tparg != NULL && tp != tparg) {
421      continue;
422    }
423    dict_T *const d = get_tabpage_info(tp, tpnr);
424    tv_list_append_dict(rettv->vval.v_list, d);
425    if (tparg != NULL) {
426      return;
427    }
428  }
429}
430
431/// "getwininfo()" function
432void f_getwininfo(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
433{
434  win_T *wparg = NULL;
435
436  tv_list_alloc_ret(rettv, kListLenMayKnow);
437
438  if (argvars[0].v_type != VAR_UNKNOWN) {
439    wparg = win_id2wp((int)tv_get_number(&argvars[0]));
440    if (wparg == NULL) {
441      return;
442    }
443  }
444
445  // Collect information about either all the windows across all the tab
446  // pages or one particular window.
447  int16_t tabnr = 0;
448  FOR_ALL_TABS(tp) {
449    tabnr++;
450    int16_t winnr = 0;
451    FOR_ALL_WINDOWS_IN_TAB(wp, tp) {
452      winnr += win_has_winnr(wp, tp);
453      if (wparg != NULL && wp != wparg) {
454        continue;
455      }
456      dict_T *const d = get_win_info(wp, tabnr, win_has_winnr(wp, tp) ? winnr : 0);
457      tv_list_append_dict(rettv->vval.v_list, d);
458      if (wparg != NULL) {
459        // found information about a specific window
460        return;
461      }
462    }
463  }
464}
465
466/// "getwinpos({timeout})" function
467void f_getwinpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
468{
469  tv_list_alloc_ret(rettv, 2);
470  tv_list_append_number(rettv->vval.v_list, -1);
471  tv_list_append_number(rettv->vval.v_list, -1);
472}
473
474/// "getwinposx()" function
475void f_getwinposx(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
476{
477  rettv->vval.v_number = -1;
478}
479
480/// "getwinposy()" function
481void f_getwinposy(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
482{
483  rettv->vval.v_number = -1;
484}
485
486/// "tabpagenr()" function
487void f_tabpagenr(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
488{
489  int nr = 1;
490
491  if (argvars[0].v_type != VAR_UNKNOWN) {
492    const char *const arg = tv_get_string_chk(&argvars[0]);
493    nr = 0;
494    if (arg != NULL) {
495      if (strcmp(arg, "$") == 0) {
496        nr = tabpage_index(NULL) - 1;
497      } else if (strcmp(arg, "#") == 0) {
498        nr = valid_tabpage(lastused_tabpage) ? tabpage_index(lastused_tabpage) : 0;
499      } else {
500        semsg(_(e_invexpr2), arg);
501      }
502    }
503  } else {
504    nr = tabpage_index(curtab);
505  }
506  rettv->vval.v_number = nr;
507}
508
509/// "tabpagewinnr()" function
510void f_tabpagewinnr(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
511{
512  int nr = 1;
513  tabpage_T *const tp = find_tabpage((int)tv_get_number(&argvars[0]));
514  if (tp == NULL) {
515    nr = 0;
516  } else {
517    nr = get_winnr(tp, &argvars[1]);
518  }
519  rettv->vval.v_number = nr;
520}
521
522/// "win_execute(win_id, command)" function
523void f_win_execute(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
524{
525  // Return an empty string if something fails.
526  rettv->v_type = VAR_STRING;
527  rettv->vval.v_string = NULL;
528
529  int id = (int)tv_get_number(argvars);
530  tabpage_T *tp;
531  win_T *wp = win_id2wp_tp(id, &tp);
532  if (wp == NULL || tp == NULL) {
533    return;
534  }
535
536  CtxSwitch cs = { 0 };
537  if (ctx_switch(&cs, wp, tp, NULL, kCtxNoDisplay | kCtxKeepCwd | kCtxValidate)) {
538    execute_common(argvars, rettv, 1);
539  }
540  ctx_restore(&cs);
541}
542
543/// "win_findbuf()" function
544void f_win_findbuf(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
545{
546  tv_list_alloc_ret(rettv, kListLenMayKnow);
547  win_findbuf(argvars, rettv->vval.v_list);
548}
549
550/// "win_getid()" function
551void f_win_getid(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
552{
553  rettv->vval.v_number = win_getid(argvars);
554}
555
556/// "win_gotoid()" function
557void f_win_gotoid(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
558{
559  int id = (int)tv_get_number(&argvars[0]);
560  if (curwin->handle == id) {
561    // Nothing to do.
562    rettv->vval.v_number = 1;
563    return;
564  }
565
566  if (text_or_buf_locked()) {
567    return;
568  }
569  FOR_ALL_TAB_WINDOWS(tp, wp) {
570    if (wp->handle == id) {
571      // When jumping to another buffer stop Visual mode.
572      if (Visual.active && wp->w_buffer != curbuf) {
573        end_visual_mode();
574      }
575      goto_tabpage_win(tp, wp);
576      rettv->vval.v_number = 1;
577      return;
578    }
579  }
580}
581
582/// "win_id2tabwin()" function
583void f_win_id2tabwin(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
584{
585  win_id2tabwin(argvars, rettv);
586}
587
588/// "win_id2win()" function
589void f_win_id2win(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
590{
591  rettv->vval.v_number = win_id2win(argvars);
592}
593
594/// "win_move_separator()" function
595void f_win_move_separator(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
596{
597  rettv->vval.v_number = false;
598
599  win_T *wp = find_win_by_nr_or_id(&argvars[0]);
600  if (wp == NULL || wp->w_floating) {
601    return;
602  }
603  if (!win_valid(wp)) {
604    emsg(_(e_cannot_resize_window_in_another_tab_page));
605    return;
606  }
607
608  int offset = (int)tv_get_number(&argvars[1]);
609  win_drag_vsep_line(wp, offset);
610  rettv->vval.v_number = true;
611}
612
613/// "win_move_statusline()" function
614void f_win_move_statusline(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
615{
616  win_T *wp;
617  int offset;
618
619  rettv->vval.v_number = false;
620
621  wp = find_win_by_nr_or_id(&argvars[0]);
622  if (wp == NULL || wp->w_floating) {
623    return;
624  }
625  if (!win_valid(wp)) {
626    emsg(_(e_cannot_resize_window_in_another_tab_page));
627    return;
628  }
629
630  offset = (int)tv_get_number(&argvars[1]);
631  win_drag_status_line(wp, offset);
632  rettv->vval.v_number = true;
633}
634
635/// "win_screenpos()" function
636void f_win_screenpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
637{
638  tv_list_alloc_ret(rettv, 2);
639  const win_T *const wp = find_win_by_nr_or_id(&argvars[0]);
640  tv_list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
641  tv_list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
642}
643
644/// "win_splitmove()" function
645void f_win_splitmove(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
646{
647  win_T *wp = find_win_by_nr_or_id(&argvars[0]);
648  win_T *targetwin = find_win_by_nr_or_id(&argvars[1]);
649  win_T *oldwin = curwin;
650
651  rettv->vval.v_number = -1;
652
653  if (wp == NULL || targetwin == NULL || wp == targetwin
654      || !win_valid(wp) || !win_valid(targetwin)
655      || targetwin->w_floating) {
656    emsg(_(e_invalwindow));
657    return;
658  }
659
660  int flags = 0;
661  int size = 0;
662
663  if (argvars[2].v_type != VAR_UNKNOWN) {
664    dict_T *d;
665    dictitem_T *di;
666
667    if (tv_check_for_nonnull_dict_arg(argvars, 2) == FAIL) {
668      return;
669    }
670
671    d = argvars[2].vval.v_dict;
672    if (tv_dict_get_bool(d, "vertical", false)) {
673      flags |= WSP_VERT;
674    }
675    if ((di = tv_dict_find(d, "rightbelow", -1)) != NULL) {
676      flags |= tv_get_bool(&di->di_tv) ? WSP_BELOW : WSP_ABOVE;
677    }
678    size = (int)tv_dict_get_number(d, "size");
679  }
680
681  // Check if we're allowed to continue before we bother switching windows.
682  if (is_ctx_win(wp) || text_or_buf_locked() || check_split_disallowed(wp) == FAIL) {
683    return;
684  }
685
686  if (curwin != targetwin) {
687    win_goto(targetwin);
688  }
689
690  // Autocommands may have sent us elsewhere or closed "wp" or "oldwin".
691  if (curwin == targetwin && win_valid(wp)) {
692    if (win_splitmove(wp, size, flags) == OK) {
693      rettv->vval.v_number = 0;
694    }
695  } else {
696    emsg(_(e_auabort));
697  }
698
699  if (oldwin != curwin && win_valid(oldwin)) {
700    win_goto(oldwin);
701  }
702}
703
704/// "win_gettype(nr)" function
705void f_win_gettype(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
706{
707  win_T *wp = curwin;
708
709  rettv->v_type = VAR_STRING;
710  rettv->vval.v_string = NULL;
711  if (argvars[0].v_type != VAR_UNKNOWN) {
712    wp = find_win_by_nr_or_id(&argvars[0]);
713    if (wp == NULL) {
714      rettv->vval.v_string = xstrdup("unknown");
715      return;
716    }
717  }
718  if (is_ctx_win(wp)) {
719    rettv->vval.v_string = xstrdup("autocmd");
720  } else if (wp->w_p_pvw) {
721    rettv->vval.v_string = xstrdup("preview");
722  } else if (wp->w_floating) {
723    rettv->vval.v_string = xstrdup("popup");
724  } else if (bt_cmdwin(wp->w_buffer)) {
725    rettv->vval.v_string = xstrdup("command");
726  } else if (bt_quickfix(wp->w_buffer)) {
727    rettv->vval.v_string = xstrdup((wp->w_llist_ref != NULL ? "loclist" : "quickfix"));
728  }
729}
730
731/// "getcmdwintype()" function
732void f_getcmdwintype(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
733{
734  rettv->v_type = VAR_STRING;
735  rettv->vval.v_string = NULL;
736  rettv->vval.v_string = xmallocz(1);
737  rettv->vval.v_string[0] = (char)cmdwin_type;
738}
739
740/// "winbufnr(nr)" function
741void f_winbufnr(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
742{
743  win_T *wp = find_win_by_nr_or_id(&argvars[0]);
744  if (wp == NULL) {
745    rettv->vval.v_number = -1;
746  } else {
747    rettv->vval.v_number = wp->w_buffer->b_fnum;
748  }
749}
750
751/// "wincol()" function
752void f_wincol(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
753{
754  validate_cursor(curwin);
755  rettv->vval.v_number = curwin->w_wcol + 1;
756}
757
758/// "winheight(nr)" function
759void f_winheight(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
760{
761  win_T *wp = find_win_by_nr_or_id(&argvars[0]);
762  if (wp == NULL) {
763    rettv->vval.v_number = -1;
764  } else {
765    rettv->vval.v_number = wp->w_view_height;
766  }
767}
768
769/// "winlayout()" function
770void f_winlayout(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
771{
772  tabpage_T *tp;
773
774  tv_list_alloc_ret(rettv, 2);
775
776  if (argvars[0].v_type == VAR_UNKNOWN) {
777    tp = curtab;
778  } else {
779    tp = find_tabpage((int)tv_get_number(&argvars[0]));
780    if (tp == NULL) {
781      return;
782    }
783  }
784
785  get_framelayout(tp->tp_topframe, rettv->vval.v_list, true);
786}
787
788/// "winline()" function
789void f_winline(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
790{
791  validate_cursor(curwin);
792  rettv->vval.v_number = curwin->w_wrow + 1;
793}
794
795/// "winnr()" function
796void f_winnr(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
797{
798  rettv->vval.v_number = get_winnr(curtab, &argvars[0]);
799}
800
801/// "winrestcmd()" function
802void f_winrestcmd(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
803{
804  char buf[50];
805
806  garray_T ga;
807  ga_init(&ga, (int)sizeof(char), 70);
808
809  // Do this twice to handle some window layouts properly.
810  for (int i = 0; i < 2; i++) {
811    int winnr = 1;
812    FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
813      if (!win_has_winnr(wp, curtab)) {
814        continue;
815      }
816      size_t buflen = vim_snprintf_safelen(buf, sizeof(buf),
817                                           ":%dresize %d|", winnr, wp->w_height);
818      ga_concat_len(&ga, buf, buflen);
819      buflen = vim_snprintf_safelen(buf, sizeof(buf),
820                                    "vert :%dresize %d|", winnr, wp->w_width);
821      ga_concat_len(&ga, buf, buflen);
822      winnr++;
823    }
824  }
825  ga_append(&ga, NUL);
826
827  rettv->vval.v_string = ga.ga_data;
828  rettv->v_type = VAR_STRING;
829}
830
831/// "winrestview()" function
832void f_winrestview(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
833{
834  if (tv_check_for_nonnull_dict_arg(argvars, 0) == FAIL) {
835    return;
836  }
837
838  dict_T *dict = argvars[0].vval.v_dict;
839  dictitem_T *di;
840  if ((di = tv_dict_find(dict, S_LEN("lnum"))) != NULL) {
841    curwin->w_cursor.lnum = (linenr_T)tv_get_number(&di->di_tv);
842  }
843  if ((di = tv_dict_find(dict, S_LEN("col"))) != NULL) {
844    curwin->w_cursor.col = (colnr_T)tv_get_number(&di->di_tv);
845  }
846  if ((di = tv_dict_find(dict, S_LEN("coladd"))) != NULL) {
847    curwin->w_cursor.coladd = (colnr_T)tv_get_number(&di->di_tv);
848  }
849  if ((di = tv_dict_find(dict, S_LEN("topline"))) != NULL) {
850    set_topline(curwin, (linenr_T)tv_get_number(&di->di_tv));  // A folded line: its fold start.
851  }
852  viewstate_T vs;
853  save_viewstate(curwin, &vs);  // Keys absent from the dict keep their value.
854  if ((di = tv_dict_find(dict, S_LEN("curswant"))) != NULL) {
855    vs.vs_curswant = (colnr_T)tv_get_number(&di->di_tv);
856    vs.vs_set_curswant = false;
857  }
858  if ((di = tv_dict_find(dict, S_LEN("topfill"))) != NULL) {
859    vs.vs_topfill = (int)tv_get_number(&di->di_tv);
860  }
861  if ((di = tv_dict_find(dict, S_LEN("leftcol"))) != NULL) {
862    vs.vs_leftcol = (colnr_T)tv_get_number(&di->di_tv);
863  }
864  if ((di = tv_dict_find(dict, S_LEN("skipcol"))) != NULL) {
865    vs.vs_skipcol = (colnr_T)tv_get_number(&di->di_tv);
866  }
867  restore_viewstate(curwin, &vs);
868
869  // The dict may name positions beyond the buffer.
870  check_cursor(curwin);
871  changed_window_setting(curwin);
872  if (curwin->w_topline <= 0) {
873    curwin->w_topline = 1;
874  }
875  if (curwin->w_topline > curbuf->b_ml.ml_line_count) {
876    curwin->w_topline = curbuf->b_ml.ml_line_count;
877  }
878  check_topfill(curwin, true);
879}
880
881/// "winsaveview()" function
882void f_winsaveview(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
883{
884  tv_dict_alloc_ret(rettv);
885  dict_T *dict = rettv->vval.v_dict;
886
887  tv_dict_add_nr(dict, S_LEN("lnum"), (varnumber_T)curwin->w_cursor.lnum);
888  tv_dict_add_nr(dict, S_LEN("col"), (varnumber_T)curwin->w_cursor.col);
889  tv_dict_add_nr(dict, S_LEN("coladd"), (varnumber_T)curwin->w_cursor.coladd);
890  update_curswant();
891  viewstate_T vs;
892  save_viewstate(curwin, &vs);
893  tv_dict_add_nr(dict, S_LEN("curswant"), (varnumber_T)vs.vs_curswant);
894
895  tv_dict_add_nr(dict, S_LEN("topline"), (varnumber_T)vs.vs_topline);
896  tv_dict_add_nr(dict, S_LEN("topfill"), (varnumber_T)vs.vs_topfill);
897  tv_dict_add_nr(dict, S_LEN("leftcol"), (varnumber_T)vs.vs_leftcol);
898  tv_dict_add_nr(dict, S_LEN("skipcol"), (varnumber_T)vs.vs_skipcol);
899}
900
901/// "winwidth(nr)" function
902void f_winwidth(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
903{
904  win_T *wp = find_win_by_nr_or_id(&argvars[0]);
905  if (wp == NULL) {
906    rettv->vval.v_number = -1;
907  } else {
908    rettv->vval.v_number = wp->w_view_width;
909  }
910}