robin/laufey

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

Justin M. KeyesMerge #40325 from justinmk/cmdwindd5ebae7ac

master
6.5 KiB256 linesraw
1#include <stdbool.h>
2#include <stdlib.h>
3
4#include "nvim/api/keysets_defs.h"
5#include "nvim/api/private/defs.h"
6#include "nvim/api/private/dispatch.h"
7#include "nvim/api/private/helpers.h"
8#include "nvim/api/tabpage.h"
9#include "nvim/api/vim.h"
10#include "nvim/autocmd.h"
11#include "nvim/buffer.h"
12#include "nvim/buffer_defs.h"
13#include "nvim/errors.h"
14#include "nvim/globals.h"
15#include "nvim/memory_defs.h"
16#include "nvim/types_defs.h"
17#include "nvim/window.h"
18
19#include "api/tabpage.c.generated.h"  // IWYU pragma: keep
20
21/// Gets the windows in a tabpage
22///
23/// @param tabpage  |tab-ID|, or 0 for current tabpage
24/// @param[out] err Error details, if any
25/// @return List of windows in `tabpage`
26ArrayOf(Window) nvim_tabpage_list_wins(Tabpage tabpage, Arena *arena, Error *err)
27  FUNC_API_SINCE(1)
28{
29  Array rv = ARRAY_DICT_INIT;
30  tabpage_T *tab = find_tab_by_handle(tabpage, err);
31
32  if (!tab || !valid_tabpage(tab)) {
33    return rv;
34  }
35
36  size_t n = 0;
37  FOR_ALL_WINDOWS_IN_TAB(wp, tab) {
38    n++;
39  }
40
41  rv = arena_array(arena, n);
42
43  FOR_ALL_WINDOWS_IN_TAB(wp, tab) {
44    ADD_C(rv, WINDOW_OBJ(wp->handle));
45  }
46
47  return rv;
48}
49
50/// Gets a tab-scoped (t:) variable
51///
52/// @param tabpage  |tab-ID|, or 0 for current tabpage
53/// @param name     Variable name
54/// @param[out] err Error details, if any
55/// @return Variable value
56Object nvim_tabpage_get_var(Tabpage tabpage, String name, Arena *arena, Error *err)
57  FUNC_API_SINCE(1)
58{
59  tabpage_T *tab = find_tab_by_handle(tabpage, err);
60
61  if (!tab) {
62    return (Object)OBJECT_INIT;
63  }
64
65  return dict_get_value(tab->tp_vars, name, arena, err);
66}
67
68/// Sets a tab-scoped (t:) variable
69///
70/// @param tabpage  |tab-ID|, or 0 for current tabpage
71/// @param name     Variable name
72/// @param value    Variable value
73/// @param[out] err Error details, if any
74void nvim_tabpage_set_var(Tabpage tabpage, String name, Object value, Error *err)
75  FUNC_API_SINCE(1)
76{
77  tabpage_T *tab = find_tab_by_handle(tabpage, err);
78
79  if (!tab) {
80    return;
81  }
82
83  dict_set_var(tab->tp_vars, name, value, false, false, NULL, err);
84}
85
86/// Removes a tab-scoped (t:) variable
87///
88/// @param tabpage  |tab-ID|, or 0 for current tabpage
89/// @param name     Variable name
90/// @param[out] err Error details, if any
91void nvim_tabpage_del_var(Tabpage tabpage, String name, Error *err)
92  FUNC_API_SINCE(1)
93{
94  tabpage_T *tab = find_tab_by_handle(tabpage, err);
95
96  if (!tab) {
97    return;
98  }
99
100  dict_set_var(tab->tp_vars, name, NIL, true, false, NULL, err);
101}
102
103/// Gets the current window in a tabpage
104///
105/// @param tabpage  |tab-ID|, or 0 for current tabpage
106/// @param[out] err Error details, if any
107/// @return |window-ID|
108Window nvim_tabpage_get_win(Tabpage tabpage, Error *err)
109  FUNC_API_SINCE(1)
110{
111  tabpage_T *tab = find_tab_by_handle(tabpage, err);
112
113  if (!tab || !valid_tabpage(tab)) {
114    return 0;
115  }
116
117  if (tab == curtab) {
118    return nvim_get_current_win();
119  }
120  FOR_ALL_WINDOWS_IN_TAB(wp, tab) {
121    if (wp == tab->tp_curwin) {
122      return wp->handle;
123    }
124  }
125  // There should always be a current window for a tabpage
126  abort();
127}
128
129/// Sets the current window in a tabpage
130///
131/// @param tabpage  |tab-ID|, or 0 for current tabpage
132/// @param win |window-ID|, must already belong to {tabpage}
133/// @param[out] err Error details, if any
134void nvim_tabpage_set_win(Tabpage tabpage, Window win, Error *err)
135  FUNC_API_SINCE(12)
136{
137  tabpage_T *tp = find_tab_by_handle(tabpage, err);
138  if (!tp) {
139    return;
140  }
141
142  win_T *wp = find_window_by_handle(win, err);
143  if (!wp) {
144    return;
145  }
146
147  if (!tabpage_win_valid(tp, wp)) {
148    api_set_error(err, kErrorTypeException, "Window does not belong to tabpage %d", tp->handle);
149    return;
150  }
151
152  if (tp == curtab) {
153    TRY_WRAP(err, {
154      win_goto(wp);
155    });
156  } else if (tp->tp_curwin != wp) {
157    tp->tp_prevwin = tp->tp_curwin;
158    tp->tp_curwin = wp;
159  }
160}
161
162/// Gets the tabpage number
163///
164/// @param tabpage  |tab-ID|, or 0 for current tabpage
165/// @param[out] err Error details, if any
166/// @return Tabpage number
167Integer nvim_tabpage_get_number(Tabpage tabpage, Error *err)
168  FUNC_API_SINCE(1)
169{
170  tabpage_T *tab = find_tab_by_handle(tabpage, err);
171
172  if (!tab) {
173    return 0;
174  }
175
176  return tabpage_index(tab);
177}
178
179/// Checks if a tabpage is valid
180///
181/// @param tabpage  |tab-ID|, or 0 for current tabpage
182/// @return true if the tabpage is valid, false otherwise
183Boolean nvim_tabpage_is_valid(Tabpage tabpage)
184  FUNC_API_SINCE(1)
185{
186  Error stub = ERROR_INIT;
187  Boolean ret = find_tab_by_handle(tabpage, &stub) != NULL;
188  api_clear_error(&stub);
189  return ret;
190}
191
192/// Opens a new tabpage.
193///
194/// @param buf Buffer to open in the first window of the new tabpage.
195///               Use 0 for current buffer.
196/// @param enter  Enter the tabpage (make it the current tabpage).
197/// @param config Configuration for the new tabpage. Keys:
198///   - after: Position to insert tabpage (default: -1; after current).
199///            0 = first, N = after Nth.
200/// @param[out] err Error details, if any
201/// @return |tab-ID| of the new tabpage
202Tabpage nvim_open_tabpage(Buffer buf, Boolean enter, Dict(tabpage_config) *config, Error *err)
203  FUNC_API_SINCE(14) FUNC_API_TEXTLOCK
204{
205#define HAS_KEY_X(d, key) HAS_KEY(d, tabpage_config, key)
206  buf_T *b = find_buffer_by_handle(buf, err);
207  if (b == NULL) {
208    return 0;
209  }
210
211  int after = -1;  // Default to after current tabpage
212  if (HAS_KEY_X(config, after)) {
213    after = (int)config->after;
214  }
215
216  tabpage_T *tp;
217  win_T *wp;
218  TRY_WRAP(err, {
219    tp = win_new_tabpage(after + 1, NULL, enter, &wp);
220  });
221  if (!tp) {
222    if (!ERROR_SET(err)) {  // set error maybe more specific
223      api_set_error(err, kErrorTypeException, "Failed to create new tabpage");
224    }
225    return 0;
226  }
227  if (!valid_tabpage(tp)) {
228    api_clear_error(err);  // maybe set by win_new_tabpage, but wasn't fatal
229    api_set_error(err, kErrorTypeException, "Tabpage was closed immediately");
230    return 0;
231  }
232
233  // Set the buffer in the new window if different from current
234  if (tabpage_win_valid(tp, wp) && wp->w_buffer != b) {
235    // win_set_buf temporarily makes `wp` the curwin to set the buffer.
236    // If not entering `wp`, block Enter and Leave events. (cringe)
237    const bool au_no_enter_leave = curwin != wp;
238    if (au_no_enter_leave) {
239      autocmd_no_enter++;
240      autocmd_no_leave++;
241    }
242    win_set_buf(wp, b, err);
243    if (au_no_enter_leave) {
244      autocmd_no_enter--;
245      autocmd_no_leave--;
246    }
247    if (!valid_tabpage(tp)) {
248      api_clear_error(err);  // maybe set by win_new_tabpage/win_set_buf, but wasn't fatal
249      api_set_error(err, kErrorTypeException, "Tabpage was closed immediately");
250      return 0;
251    }
252  }
253
254  return tp->handle;
255#undef HAS_KEY_X
256}