diff --git a/autoload/SpaceVim/api/vim/buffer.vim b/autoload/SpaceVim/api/vim/buffer.vim index a576d24e3..9b2522854 100644 --- a/autoload/SpaceVim/api/vim/buffer.vim +++ b/autoload/SpaceVim/api/vim/buffer.vim @@ -32,6 +32,14 @@ " initfunc: the function which will be call after creating buffer " " cmd: the ex command which will be run after the new buffer is created +" +" get_option(bufnr, name) +" +" Gets a buffer option value. +" +" set_option(buf, opt, value) +" +" Set a buffer option value. let s:self = {} diff --git a/doc/SpaceVim.txt b/doc/SpaceVim.txt index e1f93a73c..612d7a753 100644 --- a/doc/SpaceVim.txt +++ b/doc/SpaceVim.txt @@ -6872,6 +6872,14 @@ is a dict with following keys: cmd: the ex command which will be run after the new buffer is created +get_option(bufnr, name) + +Gets a buffer option value. + +set_option(buf, opt, value) + +Set a buffer option value. + ============================================================================== VIM#COMMAND *SpaceVim-api-vim-command* diff --git a/lua/spacevim/api/vim/buffer.lua b/lua/spacevim/api/vim/buffer.lua index 5ff670ace..c0d0b40e5 100644 --- a/lua/spacevim/api/vim/buffer.lua +++ b/lua/spacevim/api/vim/buffer.lua @@ -13,38 +13,33 @@ function M.create_buf(listed, scratch) end function M.set_lines(bufnr, startindex, endindex, replacement) - if startindex < 0 then - startindex = #vim.buffer(bufnr) + 1 + startindex + if startindex < 0 then + startindex = #vim.buffer(bufnr) + 1 + startindex + end + if endindex < 0 then + endindex = #vim.buffer(bufnr) + 1 + endindex + end + if #replacement == endindex - startindex then + for i = startindex, endindex - 1, 1 do + vim.buffer(bufnr)[i + 1] = replacement[i - startindex] end - if endindex < 0 then - endindex = #vim.buffer(bufnr) + 1 + endindex + else + if endindex < #vim.buffer(bufnr) then + for i = endindex + 1, #vim.buffer(bufnr), 1 do + replacement:add(vim.buffer(bufnr)[i]) + end end - if #replacement == endindex - startindex then - for i = startindex, endindex - 1, 1 - do - vim.buffer(bufnr)[i + 1] = replacement[i - startindex] - end - else - if endindex < #vim.buffer(bufnr) then - for i = endindex + 1, #vim.buffer(bufnr), 1 - do - replacement:add(vim.buffer(bufnr)[i]) - end - end - for i = startindex, #replacement + startindex - 1, 1 - do - if i + 1 > #vim.buffer(bufnr) then - vim.buffer(bufnr):insert(replacement[i - startindex]) - else - vim.buffer(bufnr)[i + 1] = replacement[i - startindex] - end - end - for i = #replacement + startindex + 1, #vim.buffer(bufnr), 1 - do - vim.buffer(bufnr)[#replacement + startindex + 1] = nil - end - + for i = startindex, #replacement + startindex - 1, 1 do + if i + 1 > #vim.buffer(bufnr) then + vim.buffer(bufnr):insert(replacement[i - startindex]) + else + vim.buffer(bufnr)[i + 1] = replacement[i - startindex] + end end + for i = #replacement + startindex + 1, #vim.buffer(bufnr), 1 do + vim.buffer(bufnr)[#replacement + startindex + 1] = nil + end + end end function M.listed_buffers() -- {{{ @@ -53,24 +48,37 @@ end -- }}} function M.resize(size, ...) - local arg = {...} - local cmd = arg[1] or 'vertical' - vim.cmd(cmd .. ' resize ' .. size) + local arg = { ... } + local cmd = arg[1] or 'vertical' + vim.cmd(cmd .. ' resize ' .. size) end function M.open_pos(cmd, filename, line, col) - vim.cmd('silent ' .. cmd .. ' ' .. filename) - vim.fn.cursor(line, col) + vim.cmd('silent ' .. cmd .. ' ' .. filename) + vim.fn.cursor(line, col) end ---@param bufnr number the buffer number ---@param opt string option name ---@param value any option value function M.set_option(bufnr, opt, value) - + if vim.api.nvim_set_option_value then + return vim.api.nvim_set_option_value(opt, value, { + buf = bufnr + }) + end + if vim.api.nvim_buf_set_option then + return vim.api.nvim_buf_set_option(bufnr, opt, value) + end end +function M.get_option(bufnr, name) + if vim.api.nvim_get_option_value then + return vim.api.nvim_get_option_value(name, { buf = bufnr }) + end + if vim.api.nvim_buf_get_option then + return vim.api.nvim_buf_get_option(bufnr, name) + end +end return M - - diff --git a/lua/spacevim/plugin/scrollbar.lua b/lua/spacevim/plugin/scrollbar.lua index ca89a33ad..614223c14 100644 --- a/lua/spacevim/plugin/scrollbar.lua +++ b/lua/spacevim/plugin/scrollbar.lua @@ -9,6 +9,7 @@ local M = {} local win = require('spacevim.api.vim.window') +local buffer = require('spacevim.api.vim.buffer') local default_conf = { max_size = 10, @@ -75,7 +76,7 @@ local function create_scrollbar_buffer(size, lines) if not vim.api.nvim_buf_is_valid(scrollbar_bufnr) then scrollbar_bufnr = vim.api.nvim_create_buf(false, true) end - vim.api.nvim_buf_set_option(scrollbar_bufnr, 'buftype', 'nofile') + buffer.set_option(scrollbar_bufnr, 'buftype', 'nofile') vim.api.nvim_buf_set_lines(scrollbar_bufnr, 0, -1, false, lines) return scrollbar_bufnr end