neovim

Personal neovim configuration files
git clone git://gtms.dev:neovim
Log | Files | Refs

commit 28de8e4e7c54fdff15498c4ebd7ef225d1f9aab3
parent 52ba5e8c75318aa076916de4913d7013d9a05fab
Author: Tomas Nemec <owl@gtms.dev>
Date:   Wed, 19 Apr 2023 15:36:01 +0200

update

Diffstat:
Mafter/plugin/hbac.lua | 1+
Mlua/tms/p/hbac.lua | 124++++++++++++++++++++++++++++++++++++++-----------------------------------------
2 files changed, 60 insertions(+), 65 deletions(-)

diff --git a/after/plugin/hbac.lua b/after/plugin/hbac.lua @@ -1,3 +1,4 @@ +-- BUG: Closes open windows! 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/p/hbac.lua b/lua/tms/p/hbac.lua @@ -1,87 +1,81 @@ -local id = vim.api.nvim_create_augroup('hbac', { clear = false }) +local id = vim.api.nvim_create_augroup('hbac', {clear = false}) -local M = { persistant_buffers = {} } +local M = {persistant_buffers = {}} M.persist_buffer = function(bufnr) - bufnr = bufnr or vim.api.nvim_get_current_buf() - M.persistant_buffers[bufnr] = true + 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 } + 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, + vim.api.nvim_create_autocmd({'BufRead'}, { + group = id, + pattern = {'*'}, callback = function() - M.persist_buffer() - end, - }) - end, - }) + 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 + 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 + 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 + 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 + 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 num_buffers <= opts.threshold then return end - if min == bufnr then - return - end + if min == bufnr then return end - if min >= 2 ^ 1023 then - return - end + if min >= 2 ^ 1023 then return end - vim.api.nvim_buf_delete(min, {}) - end, - }) + local wins = vim.api.nvim_list_wins() + for _, win in ipairs(wins) do + if vim.api.nvim_win_get_buf(win) == min then + return + end + 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)) + 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 end -return { close_unused = close_unused, setup = M.setup } +return {close_unused = close_unused, setup = M.setup}