Scratchpad utilities
has a title now
3 files changed, +14 -13
+3-3lua/scratch/commands.lua
| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | local scratch = require("scratch.scratch") |
| 2 | -vim.api.nvim_create_user_command("Scratch", function() | |
| 3 | - scratch.relative_scratch() | |
| 4 | -end, {}) | |
| 2 | +vim.api.nvim_create_user_command("Scratch", function(args) | |
| 3 | + scratch.scratch(args.fargs[1]) | |
| 4 | +end, { nargs = "?" }) |
+2-0lua/scratch/init.lua
| @@ -5,4 +5,6 @@ M.setup = function() | ||
| 5 | 5 | require("scratch.scratch").load() |
| 6 | 6 | end |
| 7 | 7 | |
| 8 | +M.open = require("scratch.scratch").scratch | |
| 9 | + | |
| 8 | 10 | return M |
+9-10lua/scratch/scratch.lua
| @@ -1,8 +1,7 @@ | ||
| 1 | 1 | local M = {} |
| 2 | 2 | |
| 3 | -M.files = {} | |
| 4 | - | |
| 5 | 3 | function M.load() |
| 4 | + M.files = {} | |
| 6 | 5 | local cwd = vim.fn.getcwd() |
| 7 | 6 | M.hash = vim.fn.sha256(cwd) |
| 8 | 7 | M.scratchdir = vim.fn.stdpath("data") .. "/scratch" |
| @@ -20,11 +19,14 @@ function M.getfile(ext) | ||
| 20 | 19 | end |
| 21 | 20 | |
| 22 | 21 | ---Opens a buffer for the given file extension |
| 23 | ----@param ext string | |
| 22 | +---@param ext string? | |
| 24 | 23 | function M.scratch(ext) |
| 24 | + if not ext then | |
| 25 | + ext = vim.fn.expand("%:e") | |
| 26 | + end | |
| 25 | 27 | -- open scratch for the ext |
| 26 | 28 | local filename = M.getfile(ext) |
| 27 | - local buf = vim.api.nvim_create_buf(false, true) | |
| 29 | + local buf = vim.api.nvim_create_buf(false, false) | |
| 28 | 30 | |
| 29 | 31 | -- immediately move into floating window |
| 30 | 32 | local width = math.floor(vim.o.columns * 0.8) |
| @@ -39,6 +41,7 @@ function M.scratch(ext) | ||
| 39 | 41 | col = col, |
| 40 | 42 | style = "minimal", |
| 41 | 43 | border = "rounded", |
| 44 | + title = ext .. " Scratchpad: " .. filename, | |
| 42 | 45 | }) |
| 43 | 46 | vim.cmd("edit " .. filename) |
| 44 | 47 | |
| @@ -47,8 +50,8 @@ function M.scratch(ext) | ||
| 47 | 50 | vim.bo[buf].buflisted = false |
| 48 | 51 | |
| 49 | 52 | -- easy close |
| 50 | - vim.keymap.set("n", "q", ":q!<cr>", { buffer = buf, nowait = true }) | |
| 51 | - vim.keymap.set("n", "<C-c>", ":q!<cr>", { buffer = buf, nowait = true }) | |
| 53 | + vim.keymap.set("n", "q", ":q<cr>", { buffer = buf, nowait = true }) | |
| 54 | + vim.keymap.set("n", "<C-c>", "<Esc>:q<cr>", { buffer = buf, nowait = true }) | |
| 52 | 55 | |
| 53 | 56 | -- execute on save |
| 54 | 57 | vim.api.nvim_create_autocmd("BufWritePost", { |
| @@ -59,8 +62,4 @@ function M.scratch(ext) | ||
| 59 | 62 | }) |
| 60 | 63 | end |
| 61 | 64 | |
| 62 | -function M.relative_scratch() | |
| 63 | - M.scratch(vim.fn.expand("%:e")) | |
| 64 | -end | |
| 65 | - | |
| 66 | 65 | return M |