neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

tabpage.c (4412B)


      1 #include <stdbool.h>
      2 #include <stdlib.h>
      3 
      4 #include "nvim/api/private/defs.h"
      5 #include "nvim/api/private/helpers.h"
      6 #include "nvim/api/tabpage.h"
      7 #include "nvim/api/vim.h"
      8 #include "nvim/buffer_defs.h"
      9 #include "nvim/globals.h"
     10 #include "nvim/memory_defs.h"
     11 #include "nvim/types_defs.h"
     12 #include "nvim/window.h"
     13 
     14 #include "api/tabpage.c.generated.h"  // IWYU pragma: keep
     15 
     16 /// Gets the windows in a tabpage
     17 ///
     18 /// @param tabpage  |tab-ID|, or 0 for current tabpage
     19 /// @param[out] err Error details, if any
     20 /// @return List of windows in `tabpage`
     21 ArrayOf(Window) nvim_tabpage_list_wins(Tabpage tabpage, Arena *arena, Error *err)
     22  FUNC_API_SINCE(1)
     23 {
     24  Array rv = ARRAY_DICT_INIT;
     25  tabpage_T *tab = find_tab_by_handle(tabpage, err);
     26 
     27  if (!tab || !valid_tabpage(tab)) {
     28    return rv;
     29  }
     30 
     31  size_t n = 0;
     32  FOR_ALL_WINDOWS_IN_TAB(wp, tab) {
     33    n++;
     34  }
     35 
     36  rv = arena_array(arena, n);
     37 
     38  FOR_ALL_WINDOWS_IN_TAB(wp, tab) {
     39    ADD_C(rv, WINDOW_OBJ(wp->handle));
     40  }
     41 
     42  return rv;
     43 }
     44 
     45 /// Gets a tab-scoped (t:) variable
     46 ///
     47 /// @param tabpage  |tab-ID|, or 0 for current tabpage
     48 /// @param name     Variable name
     49 /// @param[out] err Error details, if any
     50 /// @return Variable value
     51 Object nvim_tabpage_get_var(Tabpage tabpage, String name, Arena *arena, Error *err)
     52  FUNC_API_SINCE(1)
     53 {
     54  tabpage_T *tab = find_tab_by_handle(tabpage, err);
     55 
     56  if (!tab) {
     57    return (Object)OBJECT_INIT;
     58  }
     59 
     60  return dict_get_value(tab->tp_vars, name, arena, err);
     61 }
     62 
     63 /// Sets a tab-scoped (t:) variable
     64 ///
     65 /// @param tabpage  |tab-ID|, or 0 for current tabpage
     66 /// @param name     Variable name
     67 /// @param value    Variable value
     68 /// @param[out] err Error details, if any
     69 void nvim_tabpage_set_var(Tabpage tabpage, String name, Object value, Error *err)
     70  FUNC_API_SINCE(1)
     71 {
     72  tabpage_T *tab = find_tab_by_handle(tabpage, err);
     73 
     74  if (!tab) {
     75    return;
     76  }
     77 
     78  dict_set_var(tab->tp_vars, name, value, false, false, NULL, err);
     79 }
     80 
     81 /// Removes a tab-scoped (t:) variable
     82 ///
     83 /// @param tabpage  |tab-ID|, or 0 for current tabpage
     84 /// @param name     Variable name
     85 /// @param[out] err Error details, if any
     86 void nvim_tabpage_del_var(Tabpage tabpage, String name, Error *err)
     87  FUNC_API_SINCE(1)
     88 {
     89  tabpage_T *tab = find_tab_by_handle(tabpage, err);
     90 
     91  if (!tab) {
     92    return;
     93  }
     94 
     95  dict_set_var(tab->tp_vars, name, NIL, true, false, NULL, err);
     96 }
     97 
     98 /// Gets the current window in a tabpage
     99 ///
    100 /// @param tabpage  |tab-ID|, or 0 for current tabpage
    101 /// @param[out] err Error details, if any
    102 /// @return |window-ID|
    103 Window nvim_tabpage_get_win(Tabpage tabpage, Error *err)
    104  FUNC_API_SINCE(1)
    105 {
    106  tabpage_T *tab = find_tab_by_handle(tabpage, err);
    107 
    108  if (!tab || !valid_tabpage(tab)) {
    109    return 0;
    110  }
    111 
    112  if (tab == curtab) {
    113    return nvim_get_current_win();
    114  }
    115  FOR_ALL_WINDOWS_IN_TAB(wp, tab) {
    116    if (wp == tab->tp_curwin) {
    117      return wp->handle;
    118    }
    119  }
    120  // There should always be a current window for a tabpage
    121  abort();
    122 }
    123 
    124 /// Sets the current window in a tabpage
    125 ///
    126 /// @param tabpage  |tab-ID|, or 0 for current tabpage
    127 /// @param win |window-ID|, must already belong to {tabpage}
    128 /// @param[out] err Error details, if any
    129 void nvim_tabpage_set_win(Tabpage tabpage, Window win, Error *err)
    130  FUNC_API_SINCE(12)
    131 {
    132  tabpage_T *tp = find_tab_by_handle(tabpage, err);
    133  if (!tp) {
    134    return;
    135  }
    136 
    137  win_T *wp = find_window_by_handle(win, err);
    138  if (!wp) {
    139    return;
    140  }
    141 
    142  if (!tabpage_win_valid(tp, wp)) {
    143    api_set_error(err, kErrorTypeException, "Window does not belong to tabpage %d", tp->handle);
    144    return;
    145  }
    146 
    147  if (tp == curtab) {
    148    TRY_WRAP(err, {
    149      win_goto(wp);
    150    });
    151  } else if (tp->tp_curwin != wp) {
    152    tp->tp_prevwin = tp->tp_curwin;
    153    tp->tp_curwin = wp;
    154  }
    155 }
    156 
    157 /// Gets the tabpage number
    158 ///
    159 /// @param tabpage  |tab-ID|, or 0 for current tabpage
    160 /// @param[out] err Error details, if any
    161 /// @return Tabpage number
    162 Integer nvim_tabpage_get_number(Tabpage tabpage, Error *err)
    163  FUNC_API_SINCE(1)
    164 {
    165  tabpage_T *tab = find_tab_by_handle(tabpage, err);
    166 
    167  if (!tab) {
    168    return 0;
    169  }
    170 
    171  return tabpage_index(tab);
    172 }
    173 
    174 /// Checks if a tabpage is valid
    175 ///
    176 /// @param tabpage  |tab-ID|, or 0 for current tabpage
    177 /// @return true if the tabpage is valid, false otherwise
    178 Boolean nvim_tabpage_is_valid(Tabpage tabpage)
    179  FUNC_API_SINCE(1)
    180 {
    181  Error stub = ERROR_INIT;
    182  Boolean ret = find_tab_by_handle(tabpage, &stub) != NULL;
    183  api_clear_error(&stub);
    184  return ret;
    185 }