commit 52ba5e8c75318aa076916de4913d7013d9a05fab
parent cd9b7968249d818cb731d643e961f042ba348817
Author: Tomas Nemec <owl@gtms.dev>
Date: Wed, 19 Apr 2023 07:44:27 +0200
update
Diffstat:
3 files changed, 92 insertions(+), 0 deletions(-)
diff --git a/after/plugin/hbac.lua b/after/plugin/hbac.lua
@@ -0,0 +1,4 @@
+require('tms.p.hbac').setup({
+ autoclose = true, -- set autoclose to false if you want to close manually
+ threshold = 3, -- hbac will start closing unedited buffers once that number is reached
+})
diff --git a/lua/tms/options.lua b/lua/tms/options.lua
@@ -37,3 +37,4 @@ vim.opt.fillchars = { eob = '•' }
vim.opt.scrolloff = 5
vim.opt.sidescrolloff = 5
vim.opt.signcolumn = 'yes:1'
+vim.opt.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
diff --git a/lua/tms/p/hbac.lua b/lua/tms/p/hbac.lua
@@ -0,0 +1,87 @@
+local id = vim.api.nvim_create_augroup('hbac', { clear = false })
+
+local M = { persistant_buffers = {} }
+
+M.persist_buffer = function(bufnr)
+ bufnr = bufnr or vim.api.nvim_get_current_buf()
+ M.persistant_buffers[bufnr] = true
+end
+
+function M.setup(opts)
+ opts = opts or { autoclose = true, threshold = 10 }
+
+ vim.api.nvim_create_autocmd({ 'BufRead' }, {
+ group = id,
+ pattern = { '*' },
+ callback = function()
+ vim.api.nvim_create_autocmd({ 'InsertEnter', 'BufModifiedSet' }, {
+ buffer = 0,
+ once = true,
+ callback = function()
+ M.persist_buffer()
+ end,
+ })
+ end,
+ })
+
+ if not opts.autoclose then
+ return
+ end
+
+ vim.api.nvim_create_autocmd({ 'BufEnter' }, {
+ group = id,
+ pattern = { '*' },
+ callback = function()
+ local bufnr = vim.api.nvim_get_current_buf()
+ local buftype = vim.api.nvim_buf_get_option(bufnr, 'buftype')
+ -- if the buffer is not a file - do nothing
+ if buftype ~= '' then
+ return
+ end
+
+ if M.persistant_buffers[M.last] then
+ return
+ end
+
+ local min = 2 ^ 1023
+ local buffers = vim.api.nvim_list_bufs()
+ local num_buffers = 0
+ for _, buf in ipairs(buffers) do
+ local name = vim.api.nvim_buf_get_name(buf)
+ local listed = vim.api.nvim_buf_get_option(buf, 'buflisted')
+ if name ~= '' and listed then
+ num_buffers = num_buffers + 1
+ if not M.persistant_buffers[buf] then
+ min = math.min(min, buf)
+ end
+ end
+ end
+
+ if num_buffers <= opts.threshold then
+ return
+ end
+
+ if min == bufnr then
+ return
+ end
+
+ if min >= 2 ^ 1023 then
+ return
+ end
+
+ vim.api.nvim_buf_delete(min, {})
+ end,
+ })
+end
+
+local function close_unused()
+ local curbufnr = vim.api.nvim_get_current_buf()
+ local buflist = vim.api.nvim_list_bufs()
+ for _, bufnr in ipairs(buflist) do
+ if vim.bo[bufnr].buflisted and bufnr ~= curbufnr and not M.persistant_buffers[bufnr] then
+ vim.cmd('bd ' .. tostring(bufnr))
+ end
+ end
+end
+
+return { close_unused = close_unused, setup = M.setup }