From 798cc3cde7c79dac4c65e6a47d6d582a7bad3ef5 Mon Sep 17 00:00:00 2001 From: Kim Gert Nielsen Date: Fri, 31 May 2024 22:05:23 +0200 Subject: [PATCH] feat: close 'no name' buffer when running open_all --- README.md | 12 ++++++++++++ lua/marlin/init.lua | 2 ++ lua/marlin/utils.lua | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/README.md b/README.md index 5cb4bba..e1712aa 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,18 @@ Example using the lazy plugin manager } ``` +If you want to restore the 'session' it can be done via autocmd like + +```lua +vim.api.nvim_create_autocmd("VimEnter", { + group = vim.api.nvim_create_augroup("restore_marlin", { clear = true }), + callback = function() + require("marlin").open_all() + end, + nested = true, +}) +``` + ### Default configuration ```lua diff --git a/lua/marlin/init.lua b/lua/marlin/init.lua index c590576..71a11a6 100644 --- a/lua/marlin/init.lua +++ b/lua/marlin/init.lua @@ -340,6 +340,8 @@ end --- ---@usage `require('marlin').open_all()` marlin.open_all = function(opts) + utils.delete_no_name_buffer() + opts = opts or {} local marlinopen = marlin.open for idx, _ in ipairs(marlin.get_indexes()) do diff --git a/lua/marlin/utils.lua b/lua/marlin/utils.lua index 49b31bd..fcd1e01 100644 --- a/lua/marlin/utils.lua +++ b/lua/marlin/utils.lua @@ -44,4 +44,26 @@ M.load_buffer = function(filename) return bufnr, set_position end +M.is_no_name_buf = function(buf) + local opts = { buf = buf } + return vim.api.nvim_buf_is_loaded(buf) + and vim.api.nvim_buf_get_name(buf) == "" + and vim.api.nvim_get_option_value("buflisted", opts) + and vim.api.nvim_get_option_value("buftype", opts) == "" + and vim.api.nvim_get_option_value("filetype", opts) == "" +end + +M.delete_no_name_buffer = function() + local curbuffers = vim.api.nvim_list_bufs() + + if #curbuffers == 1 then + local bufid = curbuffers[1] + if M.is_no_name_buf(bufid) then + vim.schedule(function() + vim.api.nvim_buf_delete(bufid, { force = true }) + end) + end + end +end + return M