robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1--- @brief Nvim Lua provides an interface or "bridge" to Vimscript variables and 2--- functions, and editor commands and options. 3--- 4--- Objects passed over this bridge are COPIED (marshalled): there are no 5--- "references". |lua-guide-variables| For example, using `vim.fn.remove()` on 6--- a Lua list copies the list object to Vimscript and does NOT modify the Lua 7--- list: 8--- 9--- ```lua 10--- local list = { 1, 2, 3 } 11--- vim.fn.remove(list, 0) 12--- vim.print(list) --> "{ 1, 2, 3 }" 13--- ``` 14 15--- @brief <pre>help 16--- vim.call({func}, {...}) *vim.call()* 17--- Invokes |vim-function| or |user-function| {func} with arguments {...}. 18--- See also |vim.fn|. 19--- Equivalent to: >lua 20--- vim.fn[func]({...}) 21--- < 22--- vim.cmd({command}) 23--- See |vim.cmd()|. 24--- 25--- vim.fn.{func}({...}) *vim.fn* 26--- Invokes |vim-function| or |user-function| {func} with arguments {...}. 27--- To call autoload functions, use the syntax: >lua 28--- vim.fn['some#function']({...}) 29--- < 30--- Unlike vim.api.|nvim_call_function()| this converts directly between Vim 31--- objects and Lua objects. If the Vim function returns a float, it will be 32--- represented directly as a Lua number. Empty lists and dictionaries both 33--- are represented by an empty table. 34--- 35--- Note: |v:null| values as part of the return value is represented as 36--- |vim.NIL| special value 37--- 38--- Note: vim.fn keys are generated lazily, thus `pairs(vim.fn)` only 39--- enumerates functions that were called at least once. 40--- 41--- Note: The majority of functions cannot run in |api-fast| callbacks with some 42--- undocumented exceptions which are allowed. 43--- 44--- *lua-vim-variables* 45--- The Vim editor global dictionaries |g:| |w:| |b:| |t:| |v:| can be accessed 46--- from Lua conveniently and idiomatically by referencing the `vim.*` Lua tables 47--- described below. In this way you can easily read and modify global Vimscript 48--- variables from Lua. 49--- 50--- Example: >lua 51--- 52--- vim.g.foo = 5 -- Set the g:foo Vimscript variable. 53--- print(vim.g.foo) -- Get and print the g:foo Vimscript variable. 54--- vim.g.foo = nil -- Delete (:unlet) the Vimscript variable. 55--- vim.b[2].foo = 6 -- Set b:foo for buffer 2 56--- < 57--- 58--- Note that setting dictionary fields directly will not write them back into 59--- Nvim. This is because the index into the namespace simply returns a copy. 60--- Instead the whole dictionary must be written as one. This can be achieved by 61--- creating a short-lived temporary. 62--- 63--- Example: >lua 64--- 65--- vim.g.my_dict.field1 = 'value' -- Does not work 66--- 67--- local my_dict = vim.g.my_dict -- 68--- my_dict.field1 = 'value' -- Instead do 69--- vim.g.my_dict = my_dict -- 70--- 71--- vim.g *vim.g* 72--- Global (|g:|) editor variables. 73--- Key with no value returns `nil`. 74--- 75--- vim.b *vim.b* 76--- Buffer-scoped (|b:|) variables for the current buffer. 77--- Invalid or unset key returns `nil`. Can be indexed with 78--- an integer to access variables for a specific buffer. 79--- 80--- vim.w *vim.w* 81--- Window-scoped (|w:|) variables for the current window. 82--- Invalid or unset key returns `nil`. Can be indexed with 83--- an integer to access variables for a specific window. 84--- 85--- vim.t *vim.t* 86--- Tabpage-scoped (|t:|) variables for the current tabpage. 87--- Invalid or unset key returns `nil`. Can be indexed with 88--- an integer to access variables for a specific tabpage. 89--- 90--- vim.v *vim.v* 91--- |v:| variables. 92--- Invalid or unset key returns `nil`. 93--- </pre> 94 95local M ={} 96local api =vim . api 97 98--- Gets or sets environment variables in the current editor process. See |expand-env| and 99--- |:let-environment| for the Vimscript behavior. Invalid or unset key returns `nil`. 100--- 101--- Example: 102--- 103--- ```lua 104--- vim.env.FOO = 'bar' 105--- print(vim.env.TERM) 106--- ``` 107--- @type table<string, string?> 108vim . env =setmetatable ( {} , { 109__index =function ( _ , k ) 110local v =vim . fn . getenv ( k ) 111if v == vim . NIL then 112return nil 113end 114return v 115end , 116 117__newindex =function ( _ , k , v ) 118vim . fn . setenv ( k , v ) 119end , 120} ) 121 122--- @param bufnr? integer 123--- @return vim.bo 124local function new_buf_opt_accessor ( bufnr ) 125return setmetatable ( {} , { 126__index =function ( _ , k ) 127if bufnr == nil and type ( k ) == 'number' then 128--- @cast k integer 129return new_buf_opt_accessor ( k ) 130end 131return api . nvim_get_option_value ( k , { buf =bufnr or 0 } ) 132end , 133 134__newindex =function ( _ , k , v ) 135api . nvim_set_option_value ( k , v , { buf =bufnr or 0 } ) 136end , 137} ) 138end 139 140--- @param winid? integer 141--- @param bufnr? integer 142--- @return vim.wo 143local function new_win_opt_accessor ( winid , bufnr ) 144-- TODO(lewis6991): allow passing both buf and win to nvim_get_option_value 145if bufnr ~= nil and bufnr ~= 0 then 146error ( 'only bufnr=0 is supported' ) 147end 148 149return setmetatable ( {} , { 150__index =function ( _ , k ) 151if bufnr == nil and type ( k ) == 'number' then 152--- @cast k integer 153if winid == nil then 154return new_win_opt_accessor ( k ) 155else 156return new_win_opt_accessor ( winid , k ) 157end 158end 159 160return api . nvim_get_option_value ( k , { 161scope =bufnr and 'local' or nil , 162win =winid or 0 , 163} ) 164end , 165 166__newindex =function ( _ , k , v ) 167api . nvim_set_option_value ( k , v , { 168scope =bufnr and 'local' or nil , 169win =winid or 0 , 170} ) 171end , 172} ) 173end 174 175--- @brief <pre>help 176--- *lua-options* 177--- *lua-vim-options* 178--- *lua-vim-set* 179--- *lua-vim-setlocal* 180--- 181--- Vim options can be accessed through |vim.o|, which behaves like Vimscript 182--- |:set|. 183--- 184--- Examples: ~ 185--- 186--- To set a boolean toggle: 187--- Vimscript: `set number` 188--- Lua: `vim.o.number = true` 189--- 190--- To set a string value: 191--- Vimscript: `set wildignore=*.o,*.a,__pycache__` 192--- Lua: `vim.o.wildignore = '*.o,*.a,__pycache__'` 193--- Lua (alt): `vim.o.wildignore = { '*.o', '*.a', '__pycache__' }` 194--- 195--- To set a key:value type option: 196--- Vimscript: `set listchars=eol:~,space:-` 197--- Lua: `vim.o.listchars = 'eol:~,space:-'` 198--- Lua (alt): `vim.o.listchars = { eol = '~', space = '-' }` 199--- 200--- Similarly, there is |vim.bo| and |vim.wo| for setting buffer-scoped and 201--- window-scoped options. Note that this must NOT be confused with 202--- |local-options| and |:setlocal|. There is also |vim.go| that only accesses the 203--- global value of a |global-local| option, see |:setglobal|. 204--- </pre> 205 206--- Gets or sets |options|. Works like `:set`, so buffer/window-scoped options target the current 207--- buffer/window. Invalid key is an error. 208--- 209--- Example: 210--- 211--- ```lua 212--- vim.o.cmdheight = 4 213--- print(vim.o.columns) 214--- print(vim.o.foo) -- error: invalid key 215--- ``` 216vim . o =setmetatable ( {} , { 217__index =function ( _ , k ) 218return api . nvim_get_option_value ( k ) 219end , 220__newindex =function ( _ , k , v ) 221api . nvim_set_option_value ( k , v ) 222end , 223} ) 224 225--- Gets or sets global |options|. Like `:setglobal`. Invalid key is an error. 226--- 227--- Note: unlike |vim.o|, this accesses the global option value and thus is mostly useful 228--- with |global-local| options. 229--- 230--- Example: 231--- 232--- ```lua 233--- vim.go.cmdheight = 4 234--- print(vim.go.columns) 235--- print(vim.go.bar) -- error: invalid key 236--- ``` 237vim . go =setmetatable ( {} , { 238__index =function ( _ , k ) 239return api . nvim_get_option_value ( k , { scope ='global' } ) 240end , 241__newindex =function ( _ , k , v ) 242api . nvim_set_option_value ( k , v , { scope ='global' } ) 243end , 244} ) 245 246--- Gets or sets buffer-scoped |options| on buffer {bufnr} (or "current buffer" if 0 or omitted). Like 247--- `:setlocal`. Invalid {bufnr} or key is an error. 248--- 249--- Example: 250--- 251--- ```lua 252--- local bufnr = vim.api.nvim_get_current_buf() 253--- vim.bo[bufnr].buflisted = true -- same as vim.bo.buflisted = true 254--- print(vim.bo.comments) 255--- print(vim.bo.baz) -- error: invalid key 256--- ``` 257vim . bo =new_buf_opt_accessor () 258 259--- Gets or sets window-scoped |options| on window {winid} (or "current window" if 0 or omitted) and 260--- buffer {bufnr} (0 for current buffer). Like `:setlocal` if setting a |global-local| option or if 261--- {bufnr} is specified, like `:set` otherwise. Invalid {winid}, {bufnr}, or key is an error. 262--- 263--- Note: only bufnr=0 (current window-buffer) is supported, currently. 264--- 265--- Example: 266--- 267--- ```lua 268--- local winid = vim.api.nvim_get_current_win() 269--- vim.wo[winid].number = true -- same as vim.wo.number = true 270--- print(vim.wo.foldmarker) 271--- print(vim.wo.quux) -- error: invalid key 272--- vim.wo[winid][0].spell = false -- like ':setlocal nospell' 273--- ``` 274vim . wo =new_win_opt_accessor () 275 276--- @param scope? 'local'|'global' 277--- @return table<string,vim.Option> 278local function create_option_accessor ( scope ) 279--- @diagnostic disable-next-line: no-unknown 280local option_mt 281 282--- @generic T 283--- @param name string 284--- @param value T 285--- @param op_count integer 286--- @return vim.Option & { _name: string, _value: T, _op_count: integer } 287local function make_option ( name , value , op_count ) 288if type ( value ) == 'table' and getmetatable ( value ) == option_mt then 289assert ( name == value . _name , "must be the same value, otherwise that's weird." ) 290 291--- @diagnostic disable-next-line: no-unknown 292value =value . _value 293end 294 295return setmetatable ( { 296_name =name , 297_value =value , 298_op_count =op_count , 299} , option_mt ) 300end 301 302option_mt ={ 303get =function ( self ) 304-- `nvim_set_option_value(dry_run)` returns the value in its structured form. 305return api . nvim_set_option_value ( 306self . _name , 307self . _value , 308{ operation ='set' , scope =scope , dry_run =true } 309) 310end , 311 312append =function ( self , right ) 313api . nvim_set_option_value ( self . _name , right , { operation ='append' , scope =scope } ) 314end , 315 316__infix =function ( self , right , operation ) 317-- TODO(kylesower): support multiple infix operations. Right now this 318-- doesn't work because nvim_set_option_value uses get_option_newval to 319-- merge the values, and that always expects a varp pointer that points 320-- to the existing option value. Thus, when a new value is computed with 321-- an infix op, but the option isn't updated, subsequent infix ops still 322-- use the outdated option value. 323-- This could be resolved by updating the option value when computing 324-- the infix ops; however, this would then turn the infix ops into 325-- assignments. The full solution to the problem requires computing the 326-- infix op of two arbitrary values (not just one value compared to an 327-- existing option). 328if self . _op_count > 0 then 329error ( 'Multiple vim.opt infix operations unsupported' ) 330end 331return make_option ( 332self . _name , 333api . nvim_set_option_value ( 334self . _name , 335right , 336{ operation =operation , scope =scope , dry_run =true } 337), 338self . _op_count + 1 339) 340end , 341 342__add =function ( self , right ) 343return self : __infix ( right , 'append' ) 344end , 345 346prepend =function ( self , right ) 347api . nvim_set_option_value ( self . _name , right , { operation ='prepend' , scope =scope } ) 348end , 349 350__pow =function ( self , right ) 351return self : __infix ( right , 'prepend' ) 352end , 353 354remove =function ( self , right ) 355api . nvim_set_option_value ( self . _name , right , { operation ='remove' , scope =scope } ) 356end , 357 358__sub =function ( self , right ) 359return self : __infix ( right , 'remove' ) 360end , 361} 362option_mt . __index =option_mt 363 364return setmetatable ( {} , { 365__index =function ( _ , k ) 366-- vim.opt_global must get global value only 367-- vim.opt_local may fall back to global value like vim.opt 368local opts ={ scope =scope == 'global' and 'global' or nil } 369return make_option ( k , api . nvim_get_option_value ( k , opts ), 0 ) 370end , 371 372__newindex =function ( _ , k , v ) 373local option =make_option ( k , v , 0 ) 374api . nvim_set_option_value ( option . _name , option . _value , { scope =scope } ) 375end , 376} ) 377end 378 379--- @brief <pre>help 380--- *vim.opt_local* 381--- *vim.opt_global* 382--- *vim.opt* 383--- 384--- 385--- A special interface |vim.opt| exists for conveniently interacting with list- 386--- and map-style options from Lua: It allows accessing them as Lua tables and 387--- offers object-oriented method for adding and removing entries. 388--- 389--- Examples: ~ 390--- 391--- The following methods of setting a list-style option are equivalent: 392--- In Vimscript: >vim 393--- set wildignore=*.o,*.a,__pycache__ 394--- < 395--- In Lua using `vim.o`: >lua 396--- vim.o.wildignore = '*.o,*.a,__pycache__' 397--- < 398--- In Lua using `vim.opt`: >lua 399--- vim.opt.wildignore = { '*.o', '*.a', '__pycache__' } 400--- < 401--- To replicate the behavior of |:set+=|, use: >lua 402--- 403--- vim.opt.wildignore:append { "*.pyc", "node_modules" } 404--- < 405--- To replicate the behavior of |:set^=|, use: >lua 406--- 407--- vim.opt.wildignore:prepend { "new_first_value" } 408--- < 409--- To replicate the behavior of |:set-=|, use: >lua 410--- 411--- vim.opt.wildignore:remove { "node_modules" } 412--- < 413--- The following methods of setting a map-style option are equivalent: 414--- In Vimscript: >vim 415--- set listchars=space:_,tab:>~ 416--- < 417--- In Lua using `vim.o`: >lua 418--- vim.o.listchars = 'space:_,tab:>~' 419--- < 420--- In Lua using `vim.opt`: >lua 421--- vim.opt.listchars = { space = '_', tab = '>~' } 422--- < 423--- 424--- Note that |vim.opt| returns an `Option` object, not the value of the option, 425--- which is accessed through |vim.opt:get()|: 426--- 427--- Examples: ~ 428--- 429--- The following methods of getting a list-style option are equivalent: 430--- In Vimscript: >vim 431--- echo wildignore 432--- < 433--- In Lua using `vim.o`: >lua 434--- print(vim.o.wildignore) 435--- < 436--- In Lua using `vim.opt`: >lua 437--- vim.print(vim.opt.wildignore:get()) 438--- < 439--- 440--- In any of the above examples, to replicate the behavior |:setlocal|, use 441--- `vim.opt_local`. Additionally, to replicate the behavior of |:setglobal|, use 442--- `vim.opt_global`. 443--- </pre> 444 445--- @nodoc 446--- @class vim.Option 447local Option ={} -- luacheck: no unused 448 449--- Returns a Lua-representation of the option. Boolean, number and string 450--- values will be returned in exactly the same fashion. 451--- 452--- For values that are comma-separated lists, an array will be returned with 453--- the values as entries in the array: 454--- 455--- ```lua 456--- vim.cmd [[set wildignore=*.pyc,*.o]] 457--- 458--- vim.print(vim.opt.wildignore:get()) 459--- -- { "*.pyc", "*.o", } 460--- 461--- for _, ignore_pattern in ipairs(vim.opt.wildignore:get()) do 462--- print("Will ignore:", ignore_pattern) 463--- end 464--- -- Will ignore: *.pyc 465--- -- Will ignore: *.o 466--- ``` 467--- 468--- For values that are comma-separated maps, a table will be returned with 469--- the names as keys and the values as entries: 470--- 471--- ```lua 472--- vim.cmd [[set listchars=space:_,tab:>~]] 473--- 474--- vim.print(vim.opt.listchars:get()) 475--- -- { space = "_", tab = ">~", } 476--- 477--- for char, representation in pairs(vim.opt.listchars:get()) do 478--- print(char, "=>", representation) 479--- end 480--- ``` 481--- 482--- For values that are lists of flags, a set will be returned with the flags 483--- as keys and `true` as entries. 484--- 485--- ```lua 486--- vim.cmd [[set formatoptions=njtcroql]] 487--- 488--- vim.print(vim.opt.formatoptions:get()) 489--- -- { n = true, j = true, c = true, ... } 490--- 491--- local format_opts = vim.opt.formatoptions:get() 492--- if format_opts.j then 493--- print("J is enabled!") 494--- end 495--- ``` 496---@return string|integer|boolean|string[]|table<string,string|boolean>|nil value of option 497---@diagnostic disable-next-line: unused used for gen_vimdoc 498function Option : get () end 499 500--- Append a value to string-style options. See |:set+=| 501--- 502--- These are equivalent: 503--- 504--- ```lua 505--- vim.opt.formatoptions:append('j') 506--- vim.opt.formatoptions = vim.opt.formatoptions + 'j' 507--- ``` 508---@param value string Value to append 509---@diagnostic disable-next-line:unused used for gen_vimdoc 510function Option : append ( value ) end -- luacheck: no unused 511 512--- Prepend a value to string-style options. See |:set^=| 513--- 514--- These are equivalent: 515--- 516--- ```lua 517--- vim.opt.wildignore:prepend('*.o') 518--- vim.opt.wildignore = vim.opt.wildignore ^ '*.o' 519--- ``` 520---@param value string Value to prepend 521---@diagnostic disable-next-line:unused used for gen_vimdoc 522function Option : prepend ( value ) end -- luacheck: no unused 523 524--- Remove a value from string-style options. See |:set-=| 525--- 526--- These are equivalent: 527--- 528--- ```lua 529--- vim.opt.wildignore:remove('*.pyc') 530--- vim.opt.wildignore = vim.opt.wildignore - '*.pyc' 531--- ``` 532---@param value string Value to remove 533---@diagnostic disable-next-line:unused used for gen_vimdoc 534function Option : remove ( value ) end -- luacheck: no unused 535 536--- @nodoc 537vim . opt =create_option_accessor () 538 539--- @nodoc 540vim . opt_local =create_option_accessor ( 'local' ) 541 542--- @nodoc 543vim . opt_global =create_option_accessor ( 'global' ) 544 545return M