From c2165540a8cb22261709e07d86237c2362aa6c99 Mon Sep 17 00:00:00 2001 From: Jinq Qian <1261043829@qq.com> Date: Wed, 13 Apr 2022 13:01:42 +0800 Subject: [PATCH] feat: show index in buffers component instead of bufnr (#628) Co-authored-by: shadmansaleh <13149513+shadmansaleh@users.noreply.github.com> --- README.md | 14 ++++++++++---- doc/lualine.txt | 17 +++++++++++++---- lua/lualine/components/buffers/buffer.lua | 5 +++-- lua/lualine/components/buffers/init.lua | 23 ++++++++++++++++++++--- 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 7a4a298..8c63aa6 100644 --- a/README.md +++ b/README.md @@ -438,8 +438,8 @@ sections = { show_modified_status = true, -- Shows indicator when the buffer is modified. mode = 0, -- 0: Shows buffer name - -- 1: Shows buffer index (bufnr) - -- 2: Shows buffer name + buffer index (bufnr) + -- 1: Shows buffer index + -- 2: Shows buffer name + buffer index max_length = vim.o.columns * 2 / 3, -- Maximum width of buffers component, -- it can also be a function that returns @@ -610,8 +610,8 @@ sections = { show_modified_status = true, -- Shows indicator when the window is modified. mode = 0, -- 0: Shows window name - -- 1: Shows window index (bufnr) - -- 2: Shows window name + window index (bufnr) + -- 1: Shows window index + -- 2: Shows window name + window index max_length = vim.o.columns * 2 / 3, -- Maximum width of windows component, -- it can also be a function that returns @@ -673,6 +673,12 @@ tabline = { Shows currently open buffers. Like bufferline . See [buffers options](#buffers-component-options) for all builtin behaviors of buffers component. +You can use `:LualineBuffersJump` to jump to buffer based on index +of buffer in buffers component. +```vim + :LualineBuffersJump 2 " Jumps to 2nd buffer in buffers component. + :LualineBuffersJump $ " Jumps to last buffer in buffers component. +``` #### Tabs Shows currently open tab. Like usual tabline. See diff --git a/doc/lualine.txt b/doc/lualine.txt index a8c68ab..5cdae68 100644 --- a/doc/lualine.txt +++ b/doc/lualine.txt @@ -455,8 +455,8 @@ Component specific options These are options that are available on show_modified_status = true, -- Shows indicator when the buffer is modified. mode = 0, -- 0: Shows buffer name - -- 1: Shows buffer index (bufnr) - -- 2: Shows buffer name + buffer index (bufnr) + -- 1: Shows buffer index + -- 2: Shows buffer name + buffer index max_length = vim.o.columns * 2 / 3, -- Maximum width of buffers component, -- it can also be a function that returns @@ -634,8 +634,8 @@ Component specific options These are options that are available on show_modified_status = true, -- Shows indicator when the window is modified. mode = 0, -- 0: Shows window name - -- 1: Shows window index (bufnr) - -- 2: Shows window name + window index (bufnr) + -- 1: Shows window index + -- 2: Shows window name + window index max_length = vim.o.columns * 2 / 3, -- Maximum width of windows component, -- it can also be a function that returns @@ -704,6 +704,15 @@ Buffers Shows currently open buffers. Like bufferline. See |lualine-buffers-options| for all builtin behaviors of buffers component. + You can use `:LualineBuffersJump` to + jump to buffer based on index of buffer + in buffers component. + + +> + :LualineBuffersJump 2 " Jumps to 2nd buffer in buffers component. + :LualineBuffersJump $ " Jumps to last buffer in buffers component. +< *lualine-Tabs* diff --git a/lua/lualine/components/buffers/buffer.lua b/lua/lualine/components/buffers/buffer.lua index 0fbe7f0..3f7268c 100644 --- a/lua/lualine/components/buffers/buffer.lua +++ b/lua/lualine/components/buffers/buffer.lua @@ -10,6 +10,7 @@ local modules = require('lualine_require').lazy_require { function Buffer:init(opts) assert(opts.bufnr, 'Cannot create Buffer without bufnr') self.bufnr = opts.bufnr + self.buf_index = opts.buf_index self.options = opts.options self.highlights = opts.highlights self:get_props() @@ -162,10 +163,10 @@ function Buffer:apply_mode(name) end if self.options.mode == 1 then - return string.format('%s %s%s', self.bufnr, self.icon, self.modified_icon) + return string.format('%s %s%s', self.buf_index or '', self.icon, self.modified_icon) end - return string.format('%s %s%s%s', self.bufnr, self.icon, name, self.modified_icon) + return string.format('%s %s%s%s', self.buf_index or '', self.icon, name, self.modified_icon) end return Buffer diff --git a/lua/lualine/components/buffers/init.lua b/lua/lualine/components/buffers/init.lua index 4506269..a634941 100644 --- a/lua/lualine/components/buffers/init.lua +++ b/lua/lualine/components/buffers/init.lua @@ -67,11 +67,12 @@ function M:init(options) end end -function M:new_buffer(bufnr) +function M:new_buffer(bufnr, buf_index) bufnr = bufnr or vim.api.nvim_get_current_buf() - + buf_index = buf_index or '' return Buffer:new { bufnr = bufnr, + buf_index = buf_index, options = self.options, highlights = self.highlights, } @@ -79,9 +80,11 @@ end function M:buffers() local buffers = {} + M.bufpos2nr = {} for b = 1, vim.fn.bufnr('$') do if vim.fn.buflisted(b) ~= 0 and vim.api.nvim_buf_get_option(b, 'buftype') ~= 'quickfix' then - buffers[#buffers + 1] = self:new_buffer(b) + buffers[#buffers + 1] = self:new_buffer(b, #buffers + 1) + M.bufpos2nr[#buffers] = b end end @@ -213,10 +216,24 @@ function M:draw() return self.status end +function M.buffer_jump(buf_pos) + 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') + end + vim.api.nvim_set_current_buf(M.bufpos2nr[buf_pos]) +end + vim.cmd([[ function! LualineSwitchBuffer(bufnr, mouseclicks, mousebutton, modifiers) execute ":buffer " . a:bufnr endfunction + + command! -nargs=1 LualineBuffersJump call v:lua.require'lualine.components.buffers'.buffer_jump() ]]) return M