1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
return {
"HakonHarnes/img-clip.nvim",
event = "VeryLazy",
opts = {
-- add options here
-- or leave it empty to use the default settings
},
config = function(_, opts)
require("img-clip").setup(opts)
vim.keymap.set({ "n", "v", "i" }, "<M-i>", function()
local pasted_image = require("img-clip").paste_image()
if pasted_image then
-- "Update" saves only if the buffer has been modified since the last save
vim.cmd("silent! update")
-- Get the current line
local line = vim.api.nvim_get_current_line()
-- Move cursor to end of line
vim.api.nvim_win_set_cursor(0, { vim.api.nvim_win_get_cursor(0)[1], #line })
-- I reload the file, otherwise I cannot view the image after pasted
vim.cmd("edit!")
end
end, { desc = "Paste image from clipboard" })
vim.keymap.set("n", "<leader>oi", function()
local function get_image_path()
-- Get the current line
local line = vim.api.nvim_get_current_line()
-- Patterns to match image paths
local patterns = {
"%[.-%]%((.-)%)", -- Markdown style: 
"%[%[(.-)%]%]", -- Double square brackets: [[image_path]]
}
for _, pattern in ipairs(patterns) do
local _, _, image_path = string.find(line, pattern)
if image_path then
return image_path
end
end
return nil -- Return nil if no pattern matches
end
local function file_exists(filepath)
-- Check if the file exists
local f = io.open(filepath, "r")
if f then
f:close()
return true
end
return false
end
-- Get the image path
local image_path = get_image_path()
if image_path then
-- Check if the image path starts with "http" or "https"
if string.sub(image_path, 1, 4) == "http" then
print("URL image, use 'gx' to open it in the default browser.")
else
-- Construct absolute image path
local current_file_path = vim.fn.expand("%:p:h")
local absolute_image_path = current_file_path .. "/" .. image_path
-- Check if the image exists in the current path
if not file_exists(absolute_image_path) then
-- If not found, search ../Screenshots/
local fallback_path = vim.fn.fnamemodify(current_file_path, ":h")
.. "/Screenshots/"
.. image_path
if file_exists(fallback_path) then
absolute_image_path = fallback_path
else
print("Image not found in either current directory or ../Screenshots/")
return
end
end
-- Construct command to open image in Preview
local command = "nsxiv -aiop " .. vim.fn.shellescape(absolute_image_path)
-- Execute the command
local success = os.execute(command)
if success then
print("Opened image: " .. absolute_image_path)
else
print("Failed to open image: " .. absolute_image_path)
end
end
else
print("No image found under the cursor")
end
end, { desc = "Open image under cursor" })
vim.keymap.set("n", "<leader>di", function()
local function get_image_path()
local line = vim.api.nvim_get_current_line()
-- Patterns to match image paths
local patterns = {
"%[.-%]%((.-)%)", -- Markdown style: 
"%[%[(.-)%]%]", -- Double square brackets: [[image_path]]
}
for _, pattern in ipairs(patterns) do
local _, _, image_path = string.find(line, pattern)
if image_path then
return image_path
end
end
return nil -- Return nil if no pattern matches
end
local function file_exists(filepath)
local f = io.open(filepath, "r")
if f then
f:close()
return true
end
return false
end
local image_path = get_image_path()
if image_path then
if string.sub(image_path, 1, 4) == "http" then
vim.api.nvim_echo({ { "URL image cannot be deleted from disk.", "WarningMsg" } }, false, {})
return
end
local current_file_path = vim.fn.expand("%:p:h")
local absolute_image_path = current_file_path .. "/" .. image_path
-- Check if file exists
if not file_exists(absolute_image_path) then
-- Search fallback directory
local fallback_path = vim.fn.fnamemodify(current_file_path, ":h") .. "/Screenshots/" .. image_path
if file_exists(fallback_path) then
absolute_image_path = fallback_path
else
print("Image not found in either current directory or ../Screenshots/")
return
end
end
-- Verify if trash utility exists
if vim.fn.executable("trash") == 0 then
vim.api.nvim_echo({
{ "- Trash utility not installed. Install `trash-cli`.\n", "ErrorMsg" },
}, false, {})
return
end
vim.ui.select({ "yes", "no" }, { prompt = "Delete image file? " }, function(choice)
if choice == "yes" then
-- Attempt to delete using trash
local command = { "trash", absolute_image_path }
local success, err = pcall(vim.fn.system, command)
-- Debug message for troubleshooting
print("Debug: Trash command -", table.concat(command, " "))
if success and vim.fn.filereadable(absolute_image_path) == 0 then
vim.api.nvim_echo({
{ "Image file deleted using trash:\n", "Normal" },
{ absolute_image_path, "Normal" },
}, false, {})
require("image").clear()
vim.cmd("edit!")
vim.cmd("normal! dd")
else
-- If trash fails, log the error
vim.api.nvim_echo({
{ "Trash deletion failed. Error:\n", "ErrorMsg" },
{ err or "Unknown error", "ErrorMsg" },
}, false, {})
end
else
vim.api.nvim_echo({ { "Image deletion canceled.", "Normal" } }, false, {})
end
end)
else
print("No image found under the cursor")
end
end, { desc = "Delete image file under cursor" })
-- Refresh the images in the current buffer
-- Useful if you delete an actual image file and want to see the changes
-- without having to re-open neovim
vim.keymap.set("n", "<leader>ir", function()
-- First I clear the images
require("image").clear()
-- I'm using [[ ]] to escape the special characters in a command
-- vim.cmd([[lua require("image").clear()]])
-- Reloads the file to reflect the changes
vim.cmd("edit!")
print("Images refreshed")
end, { desc = "Refresh images" })
-- Set up a keymap to clear all images in the current buffer
vim.keymap.set("n", "<leader>ic", function()
-- This is the command that clears the images
require("image").clear()
-- I'm using [[ ]] to escape the special characters in a command
-- vim.cmd([[lua require("image").clear()]])
print("Images cleared")
end, { desc = "Clear images" })
end,
keys = {
-- suggested keymap
{ "<leader>pi", "<cmd>PasteImage<cr>", desc = "Paste image from clipboard" },
},
}
|