Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: close 'no name' buffer when running open_all #10

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lua/marlin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions lua/marlin/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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