Skip to content

Commit

Permalink
Merge pull request #10 from desdic/20240531noname
Browse files Browse the repository at this point in the history
feat: close 'no name' buffer when running open_all
  • Loading branch information
desdic authored May 31, 2024
2 parents f802e44 + 798cc3c commit edcb41c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
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

0 comments on commit edcb41c

Please sign in to comment.