local M = {} function M.load() M.files = {} local cwd = vim.fn.getcwd() M.hash = vim.fn.sha256(cwd) M.scratchdir = vim.fn.stdpath("data") .. "/scratch" if vim.fn.isdirectory(M.scratchdir) == 0 then vim.fn.mkdir(M.scratchdir, "p") return end end ---Gets the name of the file that lives in the scratch directory ---@param ext string The file extension ---@return string function M.getfile(ext) return M.scratchdir .. "/" .. M.hash .. "." .. ext end ---Opens a buffer for the given file extension ---@param ext string? function M.scratch(ext) if not ext then ext = vim.fn.expand("%:e") end -- open scratch for the ext local filename = M.getfile(ext) local buf = vim.api.nvim_create_buf(false, false) -- immediately move into floating window local width = math.floor(vim.o.columns * 0.8) local height = math.floor(vim.o.lines * 0.8) local row = math.floor((vim.o.lines - height) / 2) local col = math.floor((vim.o.columns - width) / 2) vim.api.nvim_open_win(buf, true, { relative = "editor", width = width, height = height, row = row, col = col, style = "minimal", border = "rounded", title = " " .. require("mini.icons").get("filetype", ext) .. " Scratchpad: " .. filename .. " ", }) vim.cmd("edit " .. filename) -- mark hidden vim.bo[buf].bufhidden = "wipe" vim.bo[buf].buflisted = false -- easy close vim.keymap.set("n", "q", ":q", { buffer = buf, nowait = true }) vim.keymap.set("n", "", ":q", { buffer = buf, nowait = true }) -- execute on save vim.api.nvim_create_autocmd("BufWritePost", { buffer = buf, callback = function() vim.cmd("luafile %") end, }) end return M