neovim

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

options.lua (26120B)


      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 
     95 local api = vim.api
     96 
     97 -- TODO(tjdevries): Improve option metadata so that this doesn't have to be hardcoded.
     98 local key_value_options = {
     99  fillchars = true,
    100  fcs = true,
    101  listchars = true,
    102  lcs = true,
    103  winhighlight = true,
    104  winhl = true,
    105 }
    106 
    107 --- @nodoc
    108 --- @class vim._option.Info : vim.api.keyset.get_option_info
    109 --- @field metatype 'boolean'|'string'|'number'|'map'|'array'|'set'
    110 
    111 --- Convert a vimoption_T style dictionary to the correct OptionType associated with it.
    112 ---@return string
    113 local function get_option_metatype(name, info)
    114  if info.type == 'string' then
    115    if info.flaglist then
    116      return 'set'
    117    elseif info.commalist then
    118      if key_value_options[name] then
    119        return 'map'
    120      end
    121      return 'array'
    122    end
    123    return 'string'
    124  end
    125  return info.type
    126 end
    127 
    128 --- @param name string
    129 --- @return vim._option.Info
    130 local function get_options_info(name)
    131  local info = api.nvim_get_option_info2(name, {})
    132  --- @cast info vim._option.Info
    133  info.metatype = get_option_metatype(name, info)
    134  return info
    135 end
    136 
    137 --- Environment variables defined in the editor session.
    138 --- See |expand-env| and |:let-environment| for the Vimscript behavior.
    139 --- Invalid or unset key returns `nil`.
    140 ---
    141 --- Example:
    142 ---
    143 --- ```lua
    144 --- vim.env.FOO = 'bar'
    145 --- print(vim.env.TERM)
    146 --- ```
    147 vim.env = setmetatable({}, {
    148  __index = function(_, k)
    149    local v = vim.fn.getenv(k)
    150    if v == vim.NIL then
    151      return nil
    152    end
    153    return v
    154  end,
    155 
    156  __newindex = function(_, k, v)
    157    vim.fn.setenv(k, v)
    158  end,
    159 })
    160 
    161 local function new_buf_opt_accessor(bufnr)
    162  return setmetatable({}, {
    163    __index = function(_, k)
    164      if bufnr == nil and type(k) == 'number' then
    165        return new_buf_opt_accessor(k)
    166      end
    167      return api.nvim_get_option_value(k, { buf = bufnr or 0 })
    168    end,
    169 
    170    __newindex = function(_, k, v)
    171      return api.nvim_set_option_value(k, v, { buf = bufnr or 0 })
    172    end,
    173  })
    174 end
    175 
    176 local function new_win_opt_accessor(winid, bufnr)
    177  -- TODO(lewis6991): allow passing both buf and win to nvim_get_option_value
    178  if bufnr ~= nil and bufnr ~= 0 then
    179    error('only bufnr=0 is supported')
    180  end
    181 
    182  return setmetatable({}, {
    183    __index = function(_, k)
    184      if bufnr == nil and type(k) == 'number' then
    185        if winid == nil then
    186          return new_win_opt_accessor(k)
    187        else
    188          return new_win_opt_accessor(winid, k)
    189        end
    190      end
    191 
    192      return api.nvim_get_option_value(k, {
    193        scope = bufnr and 'local' or nil,
    194        win = winid or 0,
    195      })
    196    end,
    197 
    198    __newindex = function(_, k, v)
    199      return api.nvim_set_option_value(k, v, {
    200        scope = bufnr and 'local' or nil,
    201        win = winid or 0,
    202      })
    203    end,
    204  })
    205 end
    206 
    207 --- @brief <pre>help
    208 ---                                                                  *lua-options*
    209 ---                                                              *lua-vim-options*
    210 ---                                                                  *lua-vim-set*
    211 ---                                                             *lua-vim-setlocal*
    212 ---
    213 --- Vim options can be accessed through |vim.o|, which behaves like Vimscript
    214 --- |:set|.
    215 ---
    216 ---     Examples: ~
    217 ---
    218 ---     To set a boolean toggle:
    219 ---         Vimscript: `set number`
    220 ---         Lua:       `vim.o.number = true`
    221 ---
    222 ---     To set a string value:
    223 ---         Vimscript: `set wildignore=*.o,*.a,__pycache__`
    224 ---         Lua:       `vim.o.wildignore = '*.o,*.a,__pycache__'`
    225 ---
    226 --- Similarly, there is |vim.bo| and |vim.wo| for setting buffer-scoped and
    227 --- window-scoped options. Note that this must NOT be confused with
    228 --- |local-options| and |:setlocal|. There is also |vim.go| that only accesses the
    229 --- global value of a |global-local| option, see |:setglobal|.
    230 --- </pre>
    231 
    232 --- Get or set |options|. Works like `:set`, so buffer/window-scoped options target the current
    233 --- buffer/window. Invalid key is an error.
    234 ---
    235 --- Example:
    236 ---
    237 --- ```lua
    238 --- vim.o.cmdheight = 4
    239 --- print(vim.o.columns)
    240 --- print(vim.o.foo)     -- error: invalid key
    241 --- ```
    242 vim.o = setmetatable({}, {
    243  __index = function(_, k)
    244    return api.nvim_get_option_value(k, {})
    245  end,
    246  __newindex = function(_, k, v)
    247    return api.nvim_set_option_value(k, v, {})
    248  end,
    249 })
    250 
    251 --- Get or set global |options|. Like `:setglobal`. Invalid key is
    252 --- an error.
    253 ---
    254 --- Note: this is different from |vim.o| because this accesses the global
    255 --- option value and thus is mostly useful for use with |global-local|
    256 --- options.
    257 ---
    258 --- Example:
    259 ---
    260 --- ```lua
    261 --- vim.go.cmdheight = 4
    262 --- print(vim.go.columns)
    263 --- print(vim.go.bar)     -- error: invalid key
    264 --- ```
    265 vim.go = setmetatable({}, {
    266  __index = function(_, k)
    267    return api.nvim_get_option_value(k, { scope = 'global' })
    268  end,
    269  __newindex = function(_, k, v)
    270    return api.nvim_set_option_value(k, v, { scope = 'global' })
    271  end,
    272 })
    273 
    274 --- Get or set buffer-scoped |options| for the buffer with number {bufnr}.
    275 --- Like `:setlocal`. If {bufnr} is omitted then the current buffer is used.
    276 --- Invalid {bufnr} or key is an error.
    277 ---
    278 --- Example:
    279 ---
    280 --- ```lua
    281 --- local bufnr = vim.api.nvim_get_current_buf()
    282 --- vim.bo[bufnr].buflisted = true    -- same as vim.bo.buflisted = true
    283 --- print(vim.bo.comments)
    284 --- print(vim.bo.baz)                 -- error: invalid key
    285 --- ```
    286 vim.bo = new_buf_opt_accessor()
    287 
    288 --- Get or set window-scoped |options| for the window with handle {winid} and
    289 --- buffer with number {bufnr}. Like `:setlocal` if setting a |global-local| option
    290 --- or if {bufnr} is provided, like `:set` otherwise. If {winid} is omitted then
    291 --- the current window is used. Invalid {winid}, {bufnr} or key is an error.
    292 ---
    293 --- Note: only {bufnr} with value `0` (the current buffer in the window) is
    294 --- supported.
    295 ---
    296 --- Example:
    297 ---
    298 --- ```lua
    299 --- local winid = vim.api.nvim_get_current_win()
    300 --- vim.wo[winid].number = true    -- same as vim.wo.number = true
    301 --- print(vim.wo.foldmarker)
    302 --- print(vim.wo.quux)             -- error: invalid key
    303 --- vim.wo[winid][0].spell = false -- like ':setlocal nospell'
    304 --- ```
    305 vim.wo = new_win_opt_accessor()
    306 
    307 --- vim.opt, vim.opt_local and vim.opt_global implementation
    308 ---
    309 --- To be used as helpers for working with options within neovim.
    310 --- For information on how to use, see :help vim.opt
    311 
    312 --- Preserves the order and does not mutate the original list
    313 --- @generic T
    314 --- @param t T[]
    315 --- @return T[]
    316 local function remove_duplicate_values(t)
    317  --- @type table, table<any,true>
    318  local result, seen = {}, {}
    319  for _, v in
    320    ipairs(t --[[@as any[] ]])
    321  do
    322    if not seen[v] then
    323      table.insert(result, v)
    324    end
    325 
    326    seen[v] = true
    327  end
    328 
    329  return result
    330 end
    331 
    332 --- Check whether the OptionTypes is allowed for vim.opt
    333 --- If it does not match, throw an error which indicates which option causes the error.
    334 --- @param name any
    335 --- @param value any
    336 --- @param types string[]
    337 local function assert_valid_value(name, value, types)
    338  local type_of_value = type(value)
    339  for _, valid_type in ipairs(types) do
    340    if valid_type == type_of_value then
    341      return
    342    end
    343  end
    344 
    345  error(
    346    string.format(
    347      "Invalid option type '%s' for '%s', should be %s",
    348      type_of_value,
    349      name,
    350      table.concat(types, ' or ')
    351    )
    352  )
    353 end
    354 
    355 local function passthrough(_, x)
    356  return x
    357 end
    358 
    359 local function tbl_merge(left, right)
    360  return vim.tbl_extend('force', left, right)
    361 end
    362 
    363 --- @param t table<any,any>
    364 --- @param value any|any[]
    365 local function tbl_remove(t, value)
    366  if type(value) == 'string' then
    367    t[value] = nil
    368  else
    369    for _, v in ipairs(value) do
    370      t[v] = nil
    371    end
    372  end
    373 
    374  return t
    375 end
    376 
    377 local valid_types = {
    378  boolean = { 'boolean' },
    379  number = { 'number' },
    380  string = { 'string' },
    381  set = { 'string', 'table' },
    382  array = { 'string', 'table' },
    383  map = { 'string', 'table' },
    384 }
    385 
    386 -- Map of functions to take a Lua style value and convert to vimoption_T style value.
    387 -- Each function takes (info, lua_value) -> vim_value
    388 local to_vim_value = {
    389  boolean = passthrough,
    390  number = passthrough,
    391  string = passthrough,
    392 
    393  --- @param info vim._option.Info
    394  --- @param value string|table<string,true>
    395  set = function(info, value)
    396    if type(value) == 'string' then
    397      return value
    398    end
    399 
    400    if info.flaglist and info.commalist then
    401      local keys = {}
    402      for k, v in pairs(value) do
    403        if v then
    404          table.insert(keys, k)
    405        end
    406      end
    407 
    408      table.sort(keys)
    409      return table.concat(keys, ',')
    410    else
    411      local result = ''
    412      for k, v in pairs(value) do
    413        if v then
    414          result = result .. k
    415        end
    416      end
    417 
    418      return result
    419    end
    420  end,
    421 
    422  --- @param info vim._option.Info
    423  --- @param value string|string[]
    424  array = function(info, value)
    425    if type(value) == 'string' then
    426      return value
    427    end
    428    if not info.allows_duplicates then
    429      value = remove_duplicate_values(value)
    430    end
    431    return table.concat(value, ',')
    432  end,
    433 
    434  --- @param value string|table<string,string>
    435  map = function(_, value)
    436    if type(value) == 'string' then
    437      return value
    438    end
    439 
    440    local result = {}
    441    for opt_key, opt_value in pairs(value) do
    442      table.insert(result, string.format('%s:%s', opt_key, opt_value))
    443    end
    444 
    445    table.sort(result)
    446    return table.concat(result, ',')
    447  end,
    448 }
    449 
    450 --- Convert a Lua value to a vimoption_T value
    451 local function convert_value_to_vim(name, info, value)
    452  if value == nil then
    453    return vim.NIL
    454  end
    455 
    456  assert_valid_value(name, value, valid_types[info.metatype])
    457 
    458  return to_vim_value[info.metatype](info, value)
    459 end
    460 
    461 -- Map of OptionType to functions that take vimoption_T values and convert to Lua values.
    462 -- Each function takes (info, vim_value) -> lua_value
    463 local to_lua_value = {
    464  boolean = passthrough,
    465  number = passthrough,
    466  string = passthrough,
    467 
    468  array = function(info, value)
    469    if type(value) == 'table' then
    470      if not info.allows_duplicates then
    471        value = remove_duplicate_values(value)
    472      end
    473 
    474      return value
    475    end
    476 
    477    -- Empty strings mean that there is nothing there,
    478    -- so empty table should be returned.
    479    if value == '' then
    480      return {}
    481    end
    482 
    483    -- Handles unescaped commas in a list.
    484    if value:find(',,,') then
    485      --- @type string, string
    486      local left, right = unpack(vim.split(value, ',,,'))
    487 
    488      local result = {}
    489      vim.list_extend(result, vim.split(left, ','))
    490      table.insert(result, ',')
    491      vim.list_extend(result, vim.split(right, ','))
    492 
    493      table.sort(result)
    494 
    495      return result
    496    end
    497 
    498    if value:find(',^,,', 1, true) then
    499      --- @type string, string
    500      local left, right = unpack(vim.split(value, ',^,,', { plain = true }))
    501 
    502      local result = {}
    503      vim.list_extend(result, vim.split(left, ','))
    504      table.insert(result, '^,')
    505      vim.list_extend(result, vim.split(right, ','))
    506 
    507      table.sort(result)
    508 
    509      return result
    510    end
    511 
    512    return vim.split(value, ',')
    513  end,
    514 
    515  set = function(info, value)
    516    if type(value) == 'table' then
    517      return value
    518    end
    519 
    520    -- Empty strings mean that there is nothing there,
    521    -- so empty table should be returned.
    522    if value == '' then
    523      return {}
    524    end
    525 
    526    assert(info.flaglist, 'That is the only one I know how to handle')
    527 
    528    local result = {} --- @type table<string,true>
    529 
    530    if info.flaglist and info.commalist then
    531      local split_value = vim.split(value, ',')
    532      for _, v in ipairs(split_value) do
    533        result[v] = true
    534      end
    535    else
    536      for i = 1, #value do
    537        result[value:sub(i, i)] = true
    538      end
    539    end
    540 
    541    return result
    542  end,
    543 
    544  map = function(info, raw_value)
    545    if type(raw_value) == 'table' then
    546      return raw_value
    547    end
    548 
    549    assert(info.commalist, 'Only commas are supported currently')
    550 
    551    local result = {} --- @type table<string,string>
    552 
    553    local comma_split = vim.split(raw_value, ',')
    554    for _, key_value_str in ipairs(comma_split) do
    555      --- @type string, string
    556      local key, value = unpack(vim.split(key_value_str, ':'))
    557      key = vim.trim(key)
    558 
    559      result[key] = value
    560    end
    561 
    562    return result
    563  end,
    564 }
    565 
    566 --- Converts a vimoption_T style value to a Lua value
    567 local function convert_value_to_lua(info, option_value)
    568  return to_lua_value[info.metatype](info, option_value)
    569 end
    570 
    571 local prepend_methods = {
    572  number = function()
    573    error("The '^' operator is not currently supported for")
    574  end,
    575 
    576  string = function(left, right)
    577    return right .. left
    578  end,
    579 
    580  array = function(left, right)
    581    for i = #right, 1, -1 do
    582      table.insert(left, 1, right[i])
    583    end
    584 
    585    return left
    586  end,
    587 
    588  map = tbl_merge,
    589  set = tbl_merge,
    590 }
    591 
    592 --- Handles the '^' operator
    593 local function prepend_value(info, current, new)
    594  return prepend_methods[info.metatype](
    595    convert_value_to_lua(info, current),
    596    convert_value_to_lua(info, new)
    597  )
    598 end
    599 
    600 local add_methods = {
    601  --- @param left integer
    602  --- @param right integer
    603  number = function(left, right)
    604    return left + right
    605  end,
    606 
    607  --- @param left string
    608  --- @param right string
    609  string = function(left, right)
    610    return left .. right
    611  end,
    612 
    613  --- @param left string[]
    614  --- @param right string[]
    615  --- @return string[]
    616  array = function(left, right)
    617    for _, v in ipairs(right) do
    618      table.insert(left, v)
    619    end
    620 
    621    return left
    622  end,
    623 
    624  map = tbl_merge,
    625  set = tbl_merge,
    626 }
    627 
    628 --- Handles the '+' operator
    629 local function add_value(info, current, new)
    630  return add_methods[info.metatype](
    631    convert_value_to_lua(info, current),
    632    convert_value_to_lua(info, new)
    633  )
    634 end
    635 
    636 --- @param t table<any,any>
    637 --- @param val any
    638 local function remove_one_item(t, val)
    639  if vim.islist(t) then
    640    local remove_index = nil
    641    for i, v in ipairs(t) do
    642      if v == val then
    643        remove_index = i
    644      end
    645    end
    646 
    647    if remove_index then
    648      table.remove(t, remove_index)
    649    end
    650  else
    651    t[val] = nil
    652  end
    653 end
    654 
    655 local remove_methods = {
    656  --- @param left integer
    657  --- @param right integer
    658  number = function(left, right)
    659    return left - right
    660  end,
    661 
    662  string = function()
    663    error('Subtraction not supported for strings.')
    664  end,
    665 
    666  --- @param left string[]
    667  --- @param right string[]
    668  --- @return string[]
    669  array = function(left, right)
    670    if type(right) == 'string' then
    671      remove_one_item(left, right)
    672    else
    673      for _, v in ipairs(right) do
    674        remove_one_item(left, v)
    675      end
    676    end
    677 
    678    return left
    679  end,
    680 
    681  map = tbl_remove,
    682  set = tbl_remove,
    683 }
    684 
    685 --- Handles the '-' operator
    686 local function remove_value(info, current, new)
    687  return remove_methods[info.metatype](convert_value_to_lua(info, current), new)
    688 end
    689 
    690 local function create_option_accessor(scope)
    691  --- @diagnostic disable-next-line: no-unknown
    692  local option_mt
    693 
    694  local function make_option(name, value)
    695    local info = assert(get_options_info(name), 'Not a valid option name: ' .. name)
    696 
    697    if type(value) == 'table' and getmetatable(value) == option_mt then
    698      assert(name == value._name, "must be the same value, otherwise that's weird.")
    699 
    700      --- @diagnostic disable-next-line: no-unknown
    701      value = value._value
    702    end
    703 
    704    return setmetatable({
    705      _name = name,
    706      _value = value,
    707      _info = info,
    708    }, option_mt)
    709  end
    710 
    711  option_mt = {
    712    -- To set a value, instead use:
    713    --  opt[my_option] = value
    714    _set = function(self)
    715      local value = convert_value_to_vim(self._name, self._info, self._value)
    716      api.nvim_set_option_value(self._name, value, { scope = scope })
    717    end,
    718 
    719    get = function(self)
    720      return convert_value_to_lua(self._info, self._value)
    721    end,
    722 
    723    append = function(self, right)
    724      --- @diagnostic disable-next-line: no-unknown
    725      self._value = add_value(self._info, self._value, right)
    726      self:_set()
    727    end,
    728 
    729    __add = function(self, right)
    730      return make_option(self._name, add_value(self._info, self._value, right))
    731    end,
    732 
    733    prepend = function(self, right)
    734      --- @diagnostic disable-next-line: no-unknown
    735      self._value = prepend_value(self._info, self._value, right)
    736      self:_set()
    737    end,
    738 
    739    __pow = function(self, right)
    740      return make_option(self._name, prepend_value(self._info, self._value, right))
    741    end,
    742 
    743    remove = function(self, right)
    744      --- @diagnostic disable-next-line: no-unknown
    745      self._value = remove_value(self._info, self._value, right)
    746      self:_set()
    747    end,
    748 
    749    __sub = function(self, right)
    750      return make_option(self._name, remove_value(self._info, self._value, right))
    751    end,
    752  }
    753  option_mt.__index = option_mt
    754 
    755  return setmetatable({}, {
    756    __index = function(_, k)
    757      -- vim.opt_global must get global value only
    758      -- vim.opt_local may fall back to global value like vim.opt
    759      local opts = { scope = scope == 'global' and 'global' or nil }
    760      return make_option(k, api.nvim_get_option_value(k, opts))
    761    end,
    762 
    763    __newindex = function(_, k, v)
    764      make_option(k, v):_set()
    765    end,
    766  })
    767 end
    768 
    769 --- @brief <pre>help
    770 ---                                                                           *vim.opt_local*
    771 ---                                                                        *vim.opt_global*
    772 ---                                                                               *vim.opt*
    773 ---
    774 ---
    775 --- A special interface |vim.opt| exists for conveniently interacting with list-
    776 --- and map-style options from Lua: It allows accessing them as Lua tables and
    777 --- offers object-oriented method for adding and removing entries.
    778 ---
    779 ---     Examples: ~
    780 ---
    781 ---     The following methods of setting a list-style option are equivalent:
    782 ---         In Vimscript: >vim
    783 ---             set wildignore=*.o,*.a,__pycache__
    784 --- <
    785 ---         In Lua using `vim.o`: >lua
    786 ---             vim.o.wildignore = '*.o,*.a,__pycache__'
    787 --- <
    788 ---         In Lua using `vim.opt`: >lua
    789 ---             vim.opt.wildignore = { '*.o', '*.a', '__pycache__' }
    790 --- <
    791 ---     To replicate the behavior of |:set+=|, use: >lua
    792 ---
    793 ---         vim.opt.wildignore:append { "*.pyc", "node_modules" }
    794 --- <
    795 ---     To replicate the behavior of |:set^=|, use: >lua
    796 ---
    797 ---         vim.opt.wildignore:prepend { "new_first_value" }
    798 --- <
    799 ---     To replicate the behavior of |:set-=|, use: >lua
    800 ---
    801 ---         vim.opt.wildignore:remove { "node_modules" }
    802 --- <
    803 ---     The following methods of setting a map-style option are equivalent:
    804 ---         In Vimscript: >vim
    805 ---             set listchars=space:_,tab:>~
    806 --- <
    807 ---         In Lua using `vim.o`: >lua
    808 ---             vim.o.listchars = 'space:_,tab:>~'
    809 --- <
    810 ---         In Lua using `vim.opt`: >lua
    811 ---             vim.opt.listchars = { space = '_', tab = '>~' }
    812 --- <
    813 ---
    814 --- Note that |vim.opt| returns an `Option` object, not the value of the option,
    815 --- which is accessed through |vim.opt:get()|:
    816 ---
    817 ---     Examples: ~
    818 ---
    819 ---     The following methods of getting a list-style option are equivalent:
    820 ---         In Vimscript: >vim
    821 ---             echo wildignore
    822 --- <
    823 ---         In Lua using `vim.o`: >lua
    824 ---             print(vim.o.wildignore)
    825 --- <
    826 ---         In Lua using `vim.opt`: >lua
    827 ---             vim.print(vim.opt.wildignore:get())
    828 --- <
    829 ---
    830 --- In any of the above examples, to replicate the behavior |:setlocal|, use
    831 --- `vim.opt_local`. Additionally, to replicate the behavior of |:setglobal|, use
    832 --- `vim.opt_global`.
    833 --- </pre>
    834 
    835 --- @nodoc
    836 --- @class vim.Option
    837 local Option = {} -- luacheck: no unused
    838 
    839 --- Returns a Lua-representation of the option. Boolean, number and string
    840 --- values will be returned in exactly the same fashion.
    841 ---
    842 --- For values that are comma-separated lists, an array will be returned with
    843 --- the values as entries in the array:
    844 ---
    845 --- ```lua
    846 --- vim.cmd [[set wildignore=*.pyc,*.o]]
    847 ---
    848 --- vim.print(vim.opt.wildignore:get())
    849 --- -- { "*.pyc", "*.o", }
    850 ---
    851 --- for _, ignore_pattern in ipairs(vim.opt.wildignore:get()) do
    852 ---     print("Will ignore:", ignore_pattern)
    853 --- end
    854 --- -- Will ignore: *.pyc
    855 --- -- Will ignore: *.o
    856 --- ```
    857 ---
    858 --- For values that are comma-separated maps, a table will be returned with
    859 --- the names as keys and the values as entries:
    860 ---
    861 --- ```lua
    862 --- vim.cmd [[set listchars=space:_,tab:>~]]
    863 ---
    864 --- vim.print(vim.opt.listchars:get())
    865 --- --  { space = "_", tab = ">~", }
    866 ---
    867 --- for char, representation in pairs(vim.opt.listchars:get()) do
    868 ---     print(char, "=>", representation)
    869 --- end
    870 --- ```
    871 ---
    872 --- For values that are lists of flags, a set will be returned with the flags
    873 --- as keys and `true` as entries.
    874 ---
    875 --- ```lua
    876 --- vim.cmd [[set formatoptions=njtcroql]]
    877 ---
    878 --- vim.print(vim.opt.formatoptions:get())
    879 --- -- { n = true, j = true, c = true, ... }
    880 ---
    881 --- local format_opts = vim.opt.formatoptions:get()
    882 --- if format_opts.j then
    883 ---     print("J is enabled!")
    884 --- end
    885 --- ```
    886 ---@return string|integer|boolean|nil value of option
    887 function Option:get() end
    888 
    889 --- Append a value to string-style options. See |:set+=|
    890 ---
    891 --- These are equivalent:
    892 ---
    893 --- ```lua
    894 --- vim.opt.formatoptions:append('j')
    895 --- vim.opt.formatoptions = vim.opt.formatoptions + 'j'
    896 --- ```
    897 ---@param value string Value to append
    898 ---@diagnostic disable-next-line:unused-local used for gen_vimdoc
    899 function Option:append(value) end -- luacheck: no unused
    900 
    901 --- Prepend a value to string-style options. See |:set^=|
    902 ---
    903 --- These are equivalent:
    904 ---
    905 --- ```lua
    906 --- vim.opt.wildignore:prepend('*.o')
    907 --- vim.opt.wildignore = vim.opt.wildignore ^ '*.o'
    908 --- ```
    909 ---@param value string Value to prepend
    910 ---@diagnostic disable-next-line:unused-local used for gen_vimdoc
    911 function Option:prepend(value) end -- luacheck: no unused
    912 
    913 --- Remove a value from string-style options. See |:set-=|
    914 ---
    915 --- These are equivalent:
    916 ---
    917 --- ```lua
    918 --- vim.opt.wildignore:remove('*.pyc')
    919 --- vim.opt.wildignore = vim.opt.wildignore - '*.pyc'
    920 --- ```
    921 ---@param value string Value to remove
    922 ---@diagnostic disable-next-line:unused-local used for gen_vimdoc
    923 function Option:remove(value) end -- luacheck: no unused
    924 
    925 --- @nodoc
    926 vim.opt = create_option_accessor()
    927 
    928 --- @nodoc
    929 vim.opt_local = create_option_accessor('local')
    930 
    931 --- @nodoc
    932 vim.opt_global = create_option_accessor('global')