96 lines
2.4 KiB
Lua
96 lines
2.4 KiB
Lua
local set = require("rabbit.plugins.util")
|
|
|
|
---@class Rabbit.Plugin.Buffers.Options
|
|
---@field public ignore_unlisted? boolean If true, will ignore unlisted buffers (like Oil)
|
|
|
|
---@class Rabbit.Plugin.Buffers: Rabbit.Plugin
|
|
local M = { ---@type Rabbit.Plugin
|
|
color = "#d08e95",
|
|
name = "buffers",
|
|
func = {},
|
|
switch = "r",
|
|
listing = {},
|
|
empty_msg = "No opened buffers",
|
|
skip_same = true,
|
|
keys = {},
|
|
evt = {},
|
|
|
|
---@param p Rabbit.Plugin.Buffers
|
|
init = function(p)
|
|
p.listing[0] = {}
|
|
p.listing.paths = {}
|
|
-- p.listing.persist = {} --set.clean(set.read(p.memory))
|
|
-- p.listing.opened = {}
|
|
-- p.listing.collections = {}
|
|
-- p.listing.recursive = nil
|
|
end,
|
|
|
|
---@type Rabbit.Plugin.Buffers.Options
|
|
opts = {
|
|
ignore_unlisted = true,
|
|
},
|
|
}
|
|
|
|
function M.evt.RabbitEnter(_, _)
|
|
M.listing.paths = nil
|
|
M.listing[0] = nil
|
|
local bufs = vim.api.nvim_list_bufs()
|
|
for _, b in ipairs(bufs) do
|
|
local path = vim.api.nvim_buf_get_name(b)
|
|
set.add(M.listing.paths, path)
|
|
set.add(M.listing[0], path)
|
|
end
|
|
end
|
|
-- function M.evt.BufEnter(evt, winid)
|
|
-- local is_listed = vim.api.nvim_get_option_value("buflisted", { buf = evt.buf })
|
|
-- if M.opts.ignore_unlisted and not is_listed then
|
|
-- return
|
|
-- end
|
|
-- set.add(M.listing[winid], evt.buf)
|
|
-- set.add(M.listing.opened, evt.buf)
|
|
-- end
|
|
|
|
-- function M.evt.BufDelete(evt, winid)
|
|
-- set.sub(M.listing[winid], evt.buf)
|
|
-- set.sub(M.listing.opened, evt.buf)
|
|
-- end
|
|
|
|
function M.evt.WinClosed(_, winid)
|
|
if M.listing[winid] ~= nil and #M.listing[winid] > 0 then
|
|
M.listing.last_closed = M.listing[winid]
|
|
end
|
|
M.listing[winid] = nil
|
|
end
|
|
|
|
---@param n integer
|
|
function M.func.select(n)
|
|
local rabbit = require("rabbit")
|
|
if M.listing[0] == nil or n ~= 1 then
|
|
return rabbit.func.select(n)
|
|
end
|
|
|
|
M.listing[0] = rabbit.ctx.listing
|
|
table.remove(M.listing[0], 1)
|
|
M.listing[rabbit.user.win] = M.listing[0]
|
|
vim.api.nvim_win_set_buf(rabbit.user.win, tonumber(M.listing[0][1]) or 0)
|
|
M.listing[0] = nil
|
|
|
|
rabbit.func.close()
|
|
end
|
|
|
|
---@param n integer
|
|
function M.func.file_del(n)
|
|
local rabbit = require("rabbit")
|
|
M.listing[rabbit.user.win] = rabbit.ctx.listing
|
|
table.remove(M.listing[rabbit.user.win], n)
|
|
table.insert(M.listing[rabbit.user.win], 1, rabbit.user.buf)
|
|
if M.listing[0] ~= nil then
|
|
table.remove(M.listing[rabbit.user.win], 1)
|
|
M.listing[0] = vim.deepcopy(M.listing[rabbit.user.win])
|
|
table.insert(M.listing[0], 1, "rabbitmsg://Restore full history")
|
|
end
|
|
require("rabbit").Redraw()
|
|
end
|
|
|
|
return M
|