Skip to content

Commit

Permalink
feat: savable scratch buffers (issue: #90)
Browse files Browse the repository at this point in the history
Code commands such as GpEnew as well as hooks which
generate new buffer will start as listed scratch buffers
to ease neovim exit.

BufWritePre callback will change it to a normal buffer if
user decides to save given buf.
  • Loading branch information
Robitx committed Jan 14, 2024
1 parent 76ad40a commit 76751e3
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lua/gp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2718,14 +2718,25 @@ M.Prompt = function(params, target, prompt, model, template, system_template, wh
vim.cmd("tabnew")
win = vim.api.nvim_get_current_win()
end
-- create a new buffer
buf = vim.api.nvim_create_buf(true, false)
-- set the created buffer as the current buffer

buf = vim.api.nvim_create_buf(true, true)
vim.api.nvim_set_current_buf(buf)
-- set the filetype

local group = M._H.create_augroup("GpScratchSave" .. _H.uuid(), { clear = true })
vim.api.nvim_create_autocmd({ "BufWritePre" }, {
buffer = buf,
group = group,
callback = function(ctx)
vim.api.nvim_buf_set_option(ctx.buf, "buftype", "")
vim.api.nvim_buf_set_name(ctx.buf, ctx.file)
vim.api.nvim_command("w!")
vim.api.nvim_del_augroup_by_id(ctx.group)
end,
})

local ft = target.filetype or filetype
vim.api.nvim_buf_set_option(buf, "filetype", ft)
-- prepare handler

handler = M.create_handler(buf, win, 0, false, "", cursor)
end

Expand Down

0 comments on commit 76751e3

Please sign in to comment.