support for IMAGE PASTING!!!!!!!!!!!!!

This commit is contained in:
Rene Schallner
2021-11-26 04:49:51 +01:00
parent ab80db3ec9
commit 97b5d322c9
3 changed files with 140 additions and 8 deletions

View File

@@ -19,6 +19,13 @@ local ZkCfg = {
dailies = home .. "/" .. "daily",
weeklies = home .. "/" .. "weekly",
templates = home .. "/" .. "templates",
-- image subdir for pasting
-- subdir name
-- or nil if pasted images shouldn't go into a special subdir
image_subdir = "img",
-- markdown file extension
extension = ".md",
-- following a link to a non-existing note will create it
@@ -31,6 +38,11 @@ local ZkCfg = {
template_new_daily = home .. "/" .. "templates/daily_tk.md",
template_new_weekly = home .. "/" .. "templates/weekly_tk.md",
-- image link style
-- wiki: ![[image name]]
-- markdown: ![image name](image_subdir/xxxxx.png)
image_link_style = "wiki",
-- integrate with calendar-vim
plug_into_calendar = true,
calendar_opts = {
@@ -43,14 +55,6 @@ local ZkCfg = {
},
}
-- ----------------------------------------------------------------------------
local note_type_templates = {
normal = ZkCfg.template_new_note,
daily = ZkCfg.template_new_daily,
weekly = ZkCfg.template_new_weekly,
}
local function file_exists(fname)
local f = io.open(fname, "r")
-- print("checking for " .. fname)
@@ -62,6 +66,71 @@ local function file_exists(fname)
end
end
-- ----------------------------------------------------------------------------
-- image stuff
local imgFromClipboard = function()
if vim.fn.executable("xclip") == 0 then
print("No xclip installed!")
return
end
-- TODO: check `xclip -selection clipboard -t TARGETS -o` for the occurence of `image/png`
-- using plenary.job::new():sync() with on_stdout(_, data) unfortunately did some weird ASCII translation on the
-- data, so the PNGs were invalid. It seems like 0d 0a and single 0a bytes were stripped by the plenary job:
--
-- plenary job version:
-- $ hexdump -C /tmp/x.png|head
-- 00000000 89 50 4e 47 1a 00 00 00 49 48 44 52 00 00 03 19 |.PNG....IHDR....|
-- 00000010 00 00 01 c1 08 02 00 00 00 8a 73 e1 c3 00 00 00 |..........s.....|
-- 00000020 09 70 48 59 73 00 00 0e c4 00 00 0e c4 01 95 2b |.pHYs..........+|
-- 00000030 0e 1b 00 00 20 00 49 44 41 54 78 9c ec dd 77 58 |.... .IDATx...wX|
-- 00000040 14 d7 fa 07 f0 33 bb b3 4b af 0b 2c 08 22 1d 04 |.....3..K..,."..|
-- 00000050 05 11 10 1b a2 54 c5 1e bb b1 c6 98 c4 68 72 4d |.....T.......hrM|
-- 00000060 e2 cd 35 37 26 b9 49 6e 6e 7e f7 a6 98 98 a8 29 |..57&.Inn~.....)|
-- 00000070 26 6a 8c 51 63 8b bd 00 8a 58 40 b0 81 08 2a 45 |&j.Qc....X@...*E|
-- 00000080 69 52 17 58 ca ee b2 f5 f7 c7 ea 4a 10 66 d7 01 |iR.X.......J.f..|
-- 00000090 b1 e4 fb 79 7c f2 2c e7 cc 39 e7 3d 67 66 b3 2f |...y|.,..9.=gf./|
--
-- OK version
-- $ hexdump -C /tmp/x2.png|head
-- 00000000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 |.PNG........IHDR|
-- 00000010 00 00 03 19 00 00 01 c1 08 02 00 00 00 8a 73 e1 |..............s.|
-- 00000020 c3 00 00 00 09 70 48 59 73 00 00 0e c4 00 00 0e |.....pHYs.......|
-- 00000030 c4 01 95 2b 0e 1b 00 00 20 00 49 44 41 54 78 9c |...+.... .IDATx.|
-- 00000040 ec dd 77 58 14 d7 fa 07 f0 33 bb b3 4b af 0b 2c |..wX.....3..K..,|
-- 00000050 08 22 1d 04 05 11 10 1b a2 54 c5 1e bb b1 c6 98 |.".......T......|
-- 00000060 c4 68 72 4d e2 cd 35 37 26 b9 49 6e 6e 7e f7 a6 |.hrM..57&.Inn~..|
-- 00000070 98 98 a8 29 26 6a 8c 51 63 8b bd 00 8a 58 40 b0 |...)&j.Qc....X@.|
-- 00000080 81 08 2a 45 69 52 17 58 ca ee b2 f5 f7 c7 ea 4a |..*EiR.X.......J|
-- 00000090 10 66 d7 01 b1 e4 fb 79 7c f2 2c e7 cc 39 e7 3d |.f.....y|.,..9.=|
local pngname = "pasted_img_" .. os.date("%Y%m%d%H%M%S") .. ".png"
local pngpath = ZkCfg.home
local relpath = pngname
if ZkCfg.image_subdir then
relpath = ZkCfg.image_subdir .. "/" .. pngname
end
pngpath = pngpath .. "/" .. pngname
os.execute("xclip -selection clipboard -t image/png -o > " .. pngpath)
if file_exists(pngpath) then
if ZkCfg.image_link_style == "markdown" then
vim.api.nvim_put({ "![" .. pngname .. "](" .. relpath .. "]" }, "", false, true)
else
vim.api.nvim_put({ "![[" .. pngname .. "]]" }, "", false, true)
end
end
end
-- end of image stuff
local note_type_templates = {
normal = ZkCfg.template_new_note,
daily = ZkCfg.template_new_daily,
weekly = ZkCfg.template_new_weekly,
}
local function daysuffix(day)
if (day == "1") or (day == "21") or (day == "31") then
return "st"
@@ -592,5 +661,6 @@ local M = {
show_calendar = ShowCalendar,
CalendarSignDay = CalendarSignDay,
CalendarAction = CalendarAction,
paste_img_and_link = imgFromClipboard,
}
return M