diff --git a/README.md b/README.md index 0c67f6b..24d3826 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,10 @@ A collection of custom keymaps and configurations for Neovim aimed at improved b - Paste from clipboard: `P` (at cursor, or over selection without yanking it) - Paste over selection without yanking replaced text: `p` +### Pastebin + +- Use :Pastebin command to upload the filetext to a server (check lua/pastebin.lua) + ### Diagnostics - `[d` / `]d` — Navigate diagnostics diff --git a/init.lua b/init.lua index c491a02..5b657ef 100644 --- a/init.lua +++ b/init.lua @@ -385,5 +385,8 @@ require('message_auto_clear').setup() -- When a file is deleted externally, rename all its buffers to "[file]: file removed" require 'buffer_deleted' +-- Pastebin +require('pastebin').setup() + -- The line beneath this is called `modeline`. See `:help modeline` -- vim: ts=2 sts=2 sw=2 et diff --git a/lua/pastebin.lua b/lua/pastebin.lua new file mode 100644 index 0000000..cf272ef --- /dev/null +++ b/lua/pastebin.lua @@ -0,0 +1,63 @@ +local M = {} + +M.upload_url = 'https://user:pass@upload.freedoms4.top/index.php' +M.auth = 'user:pass' + +-- Generate random filename +local function random_name(len) + local chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' + local name = {} + math.randomseed(os.time() + vim.loop.hrtime()) + for i = 1, len do + local idx = math.random(#chars) + name[#name + 1] = chars:sub(idx, idx) + end + return table.concat(name) +end + +function M.upload(text) + local fname = random_name(12) .. '.txt' + local form_field = string.format('file=@-;filename=%s', fname) + + local cmd = { + 'curl', + '-s', + '--user', + M.auth, + '--form', + form_field, + M.upload_url, + } + + local output = vim.fn.system(cmd, text) + + if vim.v.shell_error ~= 0 then + vim.notify('[Pastebin] Upload failed:\n' .. output, vim.log.levels.ERROR) + return nil + end + + local url = output:match "https?://[%w%-%._~:/%?#%[%]@%%&+='*,]+" + if not url then + vim.notify('[Pastebin] No URL found.\nServer replied:\n' .. output, vim.log.levels.ERROR) + return nil + end + + -- Clipboard + vim.fn.setreg('+', url) + vim.fn.setreg('*', url) + + vim.notify(string.format('[Pastebin]\nURL copied:\n%s', url), vim.log.levels.INFO) + + return url +end + +function M.pastebin_cmd() + local text = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), '\n') + M.upload(text) +end + +function M.setup() + vim.api.nvim_create_user_command('Pastebin', M.pastebin_cmd, {}) +end + +return M