diff --git a/README.md b/README.md index 99ae41f..3f5d6d9 100644 --- a/README.md +++ b/README.md @@ -759,10 +759,14 @@ Shows currently open buffers. Like bufferline . See for all builtin behaviors of buffers component. You can use `:LualineBuffersJump` to jump to buffer based on index of buffer in buffers component. +Jumping to non-existent buffer indices generates an error. To avoid these errors +`LualineBuffersJump` provides `` support, meaning that you can call +`:LualineBufferJump!` to ignore these errors. ```vim :LualineBuffersJump 2 " Jumps to 2nd buffer in buffers component. :LualineBuffersJump $ " Jumps to last buffer in buffers component. + :LualineBuffersJump! 3 " Attempts to jump to 3rd buffer, if it exists. ``` #### Tabs diff --git a/doc/lualine.txt b/doc/lualine.txt index 8aedd05..5de0552 100644 --- a/doc/lualine.txt +++ b/doc/lualine.txt @@ -793,12 +793,19 @@ Buffers Shows currently open buffers. Like builtin behaviors of buffers component. You can use `:LualineBuffersJump` to jump to buffer based on index of buffer - in buffers component. + in buffers component. Jumping to + non-existent buffer indices generates an + error. To avoid these errors + `LualineBuffersJump` provides `` + support, meaning that you can call + `:LualineBufferJump!` to ignore these + errors. > :LualineBuffersJump 2 " Jumps to 2nd buffer in buffers component. :LualineBuffersJump $ " Jumps to last buffer in buffers component. + :LualineBuffersJump! 3 " Attempts to jump to 3rd buffer, if it exists. < diff --git a/lua/lualine/components/buffers/init.lua b/lua/lualine/components/buffers/init.lua index 3c1ab7b..4a5eb03 100644 --- a/lua/lualine/components/buffers/init.lua +++ b/lua/lualine/components/buffers/init.lua @@ -211,14 +211,18 @@ function M:draw() return self.status end -function M.buffer_jump(buf_pos) +function M.buffer_jump(buf_pos, bang) if buf_pos == '$' then buf_pos = #M.bufpos2nr else buf_pos = tonumber(buf_pos) end if buf_pos < 1 or buf_pos > #M.bufpos2nr then - error('Error: Unable to jump buffer position out of range') + if bang ~= '!' then + error('Error: Unable to jump buffer position out of range') + else + return + end end vim.api.nvim_set_current_buf(M.bufpos2nr[buf_pos]) end @@ -228,7 +232,7 @@ vim.cmd([[ execute ":buffer " . a:bufnr endfunction - command! -nargs=1 LualineBuffersJump call v:lua.require'lualine.components.buffers'.buffer_jump() + command! -nargs=1 -bang LualineBuffersJump call v:lua.require'lualine.components.buffers'.buffer_jump(, "") ]]) return M