2021-09-20 13:11:41 +00:00
|
|
|
-- Copyright (c) 2020-2021 shadmansaleh
|
|
|
|
-- MIT license, see LICENSE for more details.
|
2021-10-10 18:04:55 +00:00
|
|
|
local require = require('lualine_require').require
|
2022-01-02 11:38:39 +00:00
|
|
|
local Buffer = require('lualine.components.buffers.buffer')
|
2021-09-25 17:15:42 +00:00
|
|
|
local M = require('lualine.component'):extend()
|
2022-01-02 11:38:39 +00:00
|
|
|
local highlight = require('lualine.highlight')
|
2021-09-20 13:11:41 +00:00
|
|
|
|
|
|
|
local default_options = {
|
|
|
|
show_filename_only = true,
|
2022-04-13 04:59:53 +00:00
|
|
|
hide_filename_extension = false,
|
2021-09-20 13:11:41 +00:00
|
|
|
show_modified_status = true,
|
2021-11-13 13:44:35 +00:00
|
|
|
mode = 0,
|
2021-09-20 13:11:41 +00:00
|
|
|
max_length = 0,
|
|
|
|
filetype_names = {
|
|
|
|
TelescopePrompt = 'Telescope',
|
|
|
|
dashboard = 'Dashboard',
|
|
|
|
packer = 'Packer',
|
|
|
|
fzf = 'FZF',
|
2021-09-20 15:47:25 +00:00
|
|
|
alpha = 'Alpha',
|
2021-09-20 13:11:41 +00:00
|
|
|
},
|
2021-09-21 06:26:19 +00:00
|
|
|
buffers_color = {
|
|
|
|
active = nil,
|
|
|
|
inactive = nil,
|
|
|
|
},
|
2022-05-20 00:48:18 +00:00
|
|
|
symbols = {
|
|
|
|
modified = ' ●',
|
|
|
|
alternate_file = '#',
|
2022-05-20 00:48:48 +00:00
|
|
|
directory = '',
|
|
|
|
},
|
2021-09-20 13:11:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
-- This function is duplicated in tabs
|
2022-05-30 14:25:05 +00:00
|
|
|
---returns the proper hl for buffer in section. Used for setting default highlights
|
2021-10-12 14:04:47 +00:00
|
|
|
---@param section string name of section buffers component is in
|
|
|
|
---@param is_active boolean
|
|
|
|
---@return string hl name
|
2021-09-20 13:11:41 +00:00
|
|
|
local function get_hl(section, is_active)
|
|
|
|
local suffix = is_active and '_normal' or '_inactive'
|
|
|
|
local section_redirects = {
|
|
|
|
lualine_x = 'lualine_c',
|
|
|
|
lualine_y = 'lualine_b',
|
|
|
|
lualine_z = 'lualine_a',
|
|
|
|
}
|
|
|
|
if section_redirects[section] then
|
|
|
|
section = highlight.highlight_exists(section .. suffix) and section or section_redirects[section]
|
|
|
|
end
|
|
|
|
return section .. suffix
|
|
|
|
end
|
|
|
|
|
2021-09-25 17:15:42 +00:00
|
|
|
function M:init(options)
|
|
|
|
M.super.init(self, options)
|
2021-09-20 13:11:41 +00:00
|
|
|
default_options.buffers_color = {
|
2022-03-02 13:37:08 +00:00
|
|
|
active = get_hl('lualine_' .. options.self.section, true),
|
|
|
|
inactive = get_hl('lualine_' .. options.self.section, false),
|
2021-09-20 13:11:41 +00:00
|
|
|
}
|
2021-09-25 17:15:42 +00:00
|
|
|
self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options)
|
2022-03-26 13:31:40 +00:00
|
|
|
if self.options.component_name == 'buffers' then
|
|
|
|
self.highlights = {
|
2022-04-14 18:30:47 +00:00
|
|
|
active = self:create_hl(self.options.buffers_color.active, 'active'),
|
|
|
|
inactive = self:create_hl(self.options.buffers_color.inactive, 'inactive'),
|
2022-03-26 13:31:40 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-13 05:01:42 +00:00
|
|
|
function M:new_buffer(bufnr, buf_index)
|
2022-03-26 13:31:40 +00:00
|
|
|
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
2022-04-13 05:01:42 +00:00
|
|
|
buf_index = buf_index or ''
|
2022-03-26 13:31:40 +00:00
|
|
|
return Buffer:new {
|
|
|
|
bufnr = bufnr,
|
2022-04-13 05:01:42 +00:00
|
|
|
buf_index = buf_index,
|
2022-03-26 13:31:40 +00:00
|
|
|
options = self.options,
|
|
|
|
highlights = self.highlights,
|
2021-09-20 13:11:41 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-03-26 13:31:40 +00:00
|
|
|
function M:buffers()
|
2021-09-20 13:11:41 +00:00
|
|
|
local buffers = {}
|
2022-04-13 05:01:42 +00:00
|
|
|
M.bufpos2nr = {}
|
2022-01-02 11:38:39 +00:00
|
|
|
for b = 1, vim.fn.bufnr('$') do
|
2022-01-04 17:30:21 +00:00
|
|
|
if vim.fn.buflisted(b) ~= 0 and vim.api.nvim_buf_get_option(b, 'buftype') ~= 'quickfix' then
|
2022-04-13 05:01:42 +00:00
|
|
|
buffers[#buffers + 1] = self:new_buffer(b, #buffers + 1)
|
|
|
|
M.bufpos2nr[#buffers] = b
|
2021-09-20 13:11:41 +00:00
|
|
|
end
|
|
|
|
end
|
2022-03-26 13:31:40 +00:00
|
|
|
|
|
|
|
return buffers
|
|
|
|
end
|
|
|
|
|
|
|
|
function M:update_status()
|
|
|
|
local data = {}
|
|
|
|
local buffers = self:buffers()
|
2021-09-20 13:11:41 +00:00
|
|
|
local current = -2
|
2021-10-12 14:04:47 +00:00
|
|
|
-- mark the first, last, current, before current, after current buffers
|
|
|
|
-- for rendering
|
2021-09-20 13:11:41 +00:00
|
|
|
if buffers[1] then
|
|
|
|
buffers[1].first = true
|
|
|
|
end
|
|
|
|
if buffers[#buffers] then
|
|
|
|
buffers[#buffers].last = true
|
|
|
|
end
|
|
|
|
for i, buffer in ipairs(buffers) do
|
2022-03-26 13:31:40 +00:00
|
|
|
if buffer:is_current() then
|
2021-09-20 13:11:41 +00:00
|
|
|
buffer.current = true
|
|
|
|
current = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if buffers[current - 1] then
|
|
|
|
buffers[current - 1].beforecurrent = true
|
|
|
|
end
|
|
|
|
if buffers[current + 1] then
|
|
|
|
buffers[current + 1].aftercurrent = true
|
|
|
|
end
|
|
|
|
|
|
|
|
local max_length = self.options.max_length
|
2021-10-29 13:07:11 +00:00
|
|
|
if type(max_length) == 'function' then
|
|
|
|
max_length = max_length(self)
|
|
|
|
end
|
2021-10-29 13:13:17 +00:00
|
|
|
|
2021-09-20 13:11:41 +00:00
|
|
|
if max_length == 0 then
|
|
|
|
max_length = math.floor(2 * vim.o.columns / 3)
|
|
|
|
end
|
|
|
|
local total_length
|
|
|
|
for i, buffer in pairs(buffers) do
|
|
|
|
if buffer.current then
|
|
|
|
current = i
|
|
|
|
end
|
|
|
|
end
|
2021-10-12 14:04:47 +00:00
|
|
|
-- start drawing from current buffer and draw left and right of it until
|
|
|
|
-- all buffers are drawn or max_length has been reached.
|
2021-09-20 13:11:41 +00:00
|
|
|
if current == -2 then
|
2022-03-26 13:31:40 +00:00
|
|
|
local b = self:new_buffer()
|
2021-09-20 13:11:41 +00:00
|
|
|
b.current = true
|
2022-03-02 13:37:08 +00:00
|
|
|
if self.options.self.section < 'x' then
|
2021-09-20 13:11:41 +00:00
|
|
|
b.last = true
|
2021-09-20 15:47:25 +00:00
|
|
|
if #buffers > 0 then
|
|
|
|
buffers[#buffers].last = nil
|
|
|
|
end
|
2021-09-20 13:11:41 +00:00
|
|
|
buffers[#buffers + 1] = b
|
|
|
|
current = #buffers
|
|
|
|
else
|
|
|
|
b.first = true
|
2021-09-20 15:47:25 +00:00
|
|
|
if #buffers > 0 then
|
|
|
|
buffers[1].first = nil
|
|
|
|
end
|
2021-09-20 13:11:41 +00:00
|
|
|
table.insert(buffers, 1, b)
|
|
|
|
current = 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local current_buffer = buffers[current]
|
|
|
|
data[#data + 1] = current_buffer:render()
|
|
|
|
total_length = current_buffer.len
|
|
|
|
local i = 0
|
|
|
|
local before, after
|
|
|
|
while true do
|
|
|
|
i = i + 1
|
|
|
|
before = buffers[current - i]
|
|
|
|
after = buffers[current + i]
|
|
|
|
local rendered_before, rendered_after
|
|
|
|
if before == nil and after == nil then
|
|
|
|
break
|
|
|
|
end
|
2021-10-12 14:04:47 +00:00
|
|
|
-- draw left most undrawn buffer if fits in max_length
|
2021-09-20 13:11:41 +00:00
|
|
|
if before then
|
|
|
|
rendered_before = before:render()
|
|
|
|
total_length = total_length + before.len
|
2021-10-12 14:04:47 +00:00
|
|
|
if total_length > max_length then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
table.insert(data, 1, rendered_before)
|
2021-09-20 13:11:41 +00:00
|
|
|
end
|
2021-10-12 14:04:47 +00:00
|
|
|
-- draw right most undrawn buffer if fits in max_length
|
2021-09-20 13:11:41 +00:00
|
|
|
if after then
|
|
|
|
rendered_after = after:render()
|
|
|
|
total_length = total_length + after.len
|
2021-10-12 14:04:47 +00:00
|
|
|
if total_length > max_length then
|
|
|
|
break
|
|
|
|
end
|
2021-09-20 13:11:41 +00:00
|
|
|
data[#data + 1] = rendered_after
|
|
|
|
end
|
|
|
|
end
|
2022-05-30 14:25:05 +00:00
|
|
|
-- draw ellipsis (...) on relevant sides if all buffers don't fit in max_length
|
2021-09-20 13:11:41 +00:00
|
|
|
if total_length > max_length then
|
|
|
|
if before ~= nil then
|
|
|
|
before.ellipse = true
|
|
|
|
before.first = true
|
|
|
|
table.insert(data, 1, before:render())
|
|
|
|
end
|
|
|
|
if after ~= nil then
|
|
|
|
after.ellipse = true
|
|
|
|
after.last = true
|
|
|
|
data[#data + 1] = after:render()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return table.concat(data)
|
|
|
|
end
|
|
|
|
|
2021-11-18 02:12:33 +00:00
|
|
|
function M:draw()
|
|
|
|
self.status = ''
|
|
|
|
self.applied_separator = ''
|
|
|
|
|
|
|
|
if self.options.cond ~= nil and self.options.cond() ~= true then
|
|
|
|
return self.status
|
|
|
|
end
|
|
|
|
local status = self:update_status()
|
|
|
|
if type(status) == 'string' and #status > 0 then
|
|
|
|
self.status = status
|
|
|
|
self:apply_section_separators()
|
|
|
|
self:apply_separator()
|
|
|
|
end
|
|
|
|
return self.status
|
|
|
|
end
|
|
|
|
|
2022-08-08 15:52:28 +00:00
|
|
|
function M.buffer_jump(buf_pos, bang)
|
2022-04-13 05:01:42 +00:00
|
|
|
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
|
2022-08-08 15:52:28 +00:00
|
|
|
if bang ~= '!' then
|
|
|
|
error('Error: Unable to jump buffer position out of range')
|
|
|
|
else
|
|
|
|
return
|
|
|
|
end
|
2022-04-13 05:01:42 +00:00
|
|
|
end
|
|
|
|
vim.api.nvim_set_current_buf(M.bufpos2nr[buf_pos])
|
|
|
|
end
|
|
|
|
|
2022-01-02 11:38:39 +00:00
|
|
|
vim.cmd([[
|
2021-09-20 13:11:41 +00:00
|
|
|
function! LualineSwitchBuffer(bufnr, mouseclicks, mousebutton, modifiers)
|
|
|
|
execute ":buffer " . a:bufnr
|
|
|
|
endfunction
|
2022-04-13 05:01:42 +00:00
|
|
|
|
2022-08-08 15:52:28 +00:00
|
|
|
command! -nargs=1 -bang LualineBuffersJump call v:lua.require'lualine.components.buffers'.buffer_jump(<f-args>, "<bang>")
|
2022-01-02 11:38:39 +00:00
|
|
|
]])
|
2021-09-20 13:11:41 +00:00
|
|
|
|
2021-09-25 17:15:42 +00:00
|
|
|
return M
|