|
| 1 | +local M = {} |
| 2 | + |
| 3 | +-- wrap url, then paste it |
| 4 | +local paste = function(wrap_cb, url) |
| 5 | + if not url then |
| 6 | + local text = vim.fn.getreg "+" |
| 7 | + if text == nil then return end |
| 8 | + -- TODO: whatever, treat it as url now |
| 9 | + url = text:gsub("\n", "") |
| 10 | + end |
| 11 | + url = wrap_cb(url) |
| 12 | + vim.api.nvim_paste(url, true, 1) |
| 13 | +end |
| 14 | + |
| 15 | +-- return line with wrapped url, or nil if no url in line |
| 16 | +-- TODO: also skip if wrapped url exist |
| 17 | +-- TODO: two or more url |
| 18 | +local wrap_line = function(cb, line) |
| 19 | + -- https://github.com/sportshead/gx.nvim/blob/77241c1508883882c027aa971b98cd0631ff2a10/lua/gx/helper.lua?plain=1#L49-L60 |
| 20 | + local url_patterns = { |
| 21 | + "(https?://[a-zA-Z%d_/%%%-%.~@\\+#=?&:]+)", |
| 22 | + "([a-zA-Z%d_/%-%.~@\\+#]+%.[a-zA-Z%d_/%%%-%.~@\\+#=?&:]+)", |
| 23 | + } |
| 24 | + local s, e |
| 25 | + for _, p in ipairs(url_patterns) do |
| 26 | + s, e = line:find(p) |
| 27 | + if s then break end |
| 28 | + end |
| 29 | + if not s then return nil end |
| 30 | + local url = line:sub(s, e) -- NOTE: may not contain 'https?://' |
| 31 | + url = cb(url) |
| 32 | + line = line:sub(1, s - 1) .. url .. line:sub(e + 1) |
| 33 | + return line |
| 34 | +end |
| 35 | + |
| 36 | +-- normal mode only |
| 37 | +local wrap_or_paste = function(cb) |
| 38 | + local line = vim.api.nvim_get_current_line() |
| 39 | + local wrapped = wrap_line(cb, line) |
| 40 | + if not wrapped then return paste(cb) end |
| 41 | + vim.api.nvim_set_current_line(wrapped) |
| 42 | +end |
| 43 | + |
| 44 | +local wrap_lines = function(cb) |
| 45 | + -- normal: wrap or paste |
| 46 | + local mode = vim.api.nvim_get_mode().mode |
| 47 | + if mode == "n" then return wrap_or_paste(cb) end |
| 48 | + -- visual: linewise wrap |
| 49 | + local vs, ve = require("mder.utils").visual_region() |
| 50 | + local lines = vim.api.nvim_buf_get_lines(0, vs, ve, false) |
| 51 | + lines = vim.tbl_map(function(line) return wrap_line(cb, line) or line end, lines) |
| 52 | + vim.api.nvim_buf_set_lines(0, vs, ve, false, lines) |
| 53 | +end |
| 54 | + |
| 55 | +-- wrap with pos inicator... |
| 56 | +local img_cb = function(url, prefix, name) |
| 57 | + prefix = prefix or "img:" |
| 58 | + name = name or "" |
| 59 | + return (""):format(prefix, name, url), #prefix + 3, #prefix + #name + 5 |
| 60 | +end |
| 61 | + |
| 62 | +local raw_cb = function(url) return "<" .. url .. ">", 2 end |
| 63 | + |
| 64 | +local link_cb = function(url, prefix, name) |
| 65 | + prefix = prefix or "img:" |
| 66 | + name = name or "" |
| 67 | + return ("[%s%s](%s)"):format(prefix, name, url), #prefix + 2, #prefix + #name + 4 |
| 68 | +end |
| 69 | + |
| 70 | +M.wrap_or_paste_img = function() wrap_lines(img_cb) end |
| 71 | +M.wrap_or_paste_raw = function() wrap_lines(raw_cb) end |
| 72 | +M.wrap_or_paste_link = function() wrap_lines(link_cb) end |
| 73 | + |
| 74 | +return M |
0 commit comments