From 03bcf015d1131b64e13e97344e8f456f2af4e60d Mon Sep 17 00:00:00 2001 From: Plato Date: Mon, 8 Aug 2022 18:52:28 +0300 Subject: [PATCH] More user-friendly LualineBuffersJump (#780) * buffers: user friendly LualineBuffersJump version LualineBuffersJump throws an error when an attempt to jump to a non-existent buffer index is made. It's expected from users to trigger this error by accident while attempting to switch buffers. To deal with this add support to LualineBuffersJump. Signed-off-by: Plato Kiorpelidis * chore: autogen (vimdocs+formating) Co-authored-by: kioplato --- README.md | 4 ++++ doc/lualine.txt | 9 ++++++++- lua/lualine/components/buffers/init.lua | 10 +++++++--- 3 files changed, 19 insertions(+), 4 deletions(-) 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