robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
dd5ebae7ac
master
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 ) 27FUNC_API_SINCE (1 ) 28{ 29Array rv = ARRAY_DICT_INIT ; 30tabpage_T * tab = find_tab_by_handle (tabpage ,err ); 31 32if (!tab || !valid_tabpage (tab )) { 33return rv ; 34 } 35 36size_t n = 0 ; 37FOR_ALL_WINDOWS_IN_TAB (wp ,tab ) { 38n ++ ; 39 } 40 41rv = arena_array (arena ,n ); 42 43FOR_ALL_WINDOWS_IN_TAB (wp ,tab ) { 44ADD_C (rv ,WINDOW_OBJ (wp -> handle )); 45 } 46 47return 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 ) 57FUNC_API_SINCE (1 ) 58{ 59tabpage_T * tab = find_tab_by_handle (tabpage ,err ); 60 61if (!tab ) { 62return (Object )OBJECT_INIT ; 63 } 64 65return 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 ) 75FUNC_API_SINCE (1 ) 76{ 77tabpage_T * tab = find_tab_by_handle (tabpage ,err ); 78 79if (!tab ) { 80return ; 81 } 82 83dict_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 ) 92FUNC_API_SINCE (1 ) 93{ 94tabpage_T * tab = find_tab_by_handle (tabpage ,err ); 95 96if (!tab ) { 97return ; 98 } 99 100dict_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 ) 109FUNC_API_SINCE (1 ) 110{ 111tabpage_T * tab = find_tab_by_handle (tabpage ,err ); 112 113if (!tab || !valid_tabpage (tab )) { 114return 0 ; 115 } 116 117if (tab == curtab ) { 118return nvim_get_current_win (); 119 } 120FOR_ALL_WINDOWS_IN_TAB (wp ,tab ) { 121if (wp == tab -> tp_curwin ) { 122return wp -> handle ; 123 } 124 } 125// There should always be a current window for a tabpage 126abort (); 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 ) 135FUNC_API_SINCE (12 ) 136{ 137tabpage_T * tp = find_tab_by_handle (tabpage ,err ); 138if (!tp ) { 139return ; 140 } 141 142win_T * wp = find_window_by_handle (win ,err ); 143if (!wp ) { 144return ; 145 } 146 147if (!tabpage_win_valid (tp ,wp )) { 148api_set_error (err ,kErrorTypeException ,"Window does not belong to tabpage %d" ,tp -> handle ); 149return ; 150 } 151 152if (tp == curtab ) { 153TRY_WRAP (err , { 154win_goto (wp ); 155 }); 156 }else if (tp -> tp_curwin != wp ) { 157tp -> tp_prevwin = tp -> tp_curwin ; 158tp -> 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 ) 168FUNC_API_SINCE (1 ) 169{ 170tabpage_T * tab = find_tab_by_handle (tabpage ,err ); 171 172if (!tab ) { 173return 0 ; 174 } 175 176return 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 ) 184FUNC_API_SINCE (1 ) 185{ 186Error stub = ERROR_INIT ; 187Boolean ret = find_tab_by_handle (tabpage ,& stub )!= NULL ; 188api_clear_error (& stub ); 189return 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 ) 203FUNC_API_SINCE (14 )FUNC_API_TEXTLOCK 204{ 205#define HAS_KEY_X (d ,key ) HAS_KEY(d, tabpage_config, key) 206buf_T * b = find_buffer_by_handle (buf ,err ); 207if (b == NULL ) { 208return 0 ; 209 } 210 211int after = -1 ;// Default to after current tabpage 212if (HAS_KEY_X (config ,after )) { 213after = (int )config -> after ; 214 } 215 216tabpage_T * tp ; 217win_T * wp ; 218TRY_WRAP (err , { 219tp = win_new_tabpage (after + 1 ,NULL ,enter ,& wp ); 220 }); 221if (!tp ) { 222if (!ERROR_SET (err )) {// set error maybe more specific 223api_set_error (err ,kErrorTypeException ,"Failed to create new tabpage" ); 224 } 225return 0 ; 226 } 227if (!valid_tabpage (tp )) { 228api_clear_error (err );// maybe set by win_new_tabpage, but wasn't fatal 229api_set_error (err ,kErrorTypeException ,"Tabpage was closed immediately" ); 230return 0 ; 231 } 232 233// Set the buffer in the new window if different from current 234if (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) 237const bool au_no_enter_leave = curwin != wp ; 238if (au_no_enter_leave ) { 239autocmd_no_enter ++ ; 240autocmd_no_leave ++ ; 241 } 242win_set_buf (wp ,b ,err ); 243if (au_no_enter_leave ) { 244autocmd_no_enter -- ; 245autocmd_no_leave -- ; 246 } 247if (!valid_tabpage (tp )) { 248api_clear_error (err );// maybe set by win_new_tabpage/win_set_buf, but wasn't fatal 249api_set_error (err ,kErrorTypeException ,"Tabpage was closed immediately" ); 250return 0 ; 251 } 252 } 253 254return tp -> handle ; 255#undef HAS_KEY_X 256}