refactor(nvim): improve mime open autocmd
This commit is contained in:
parent
6d07d72121
commit
3eb9864112
@ -86,8 +86,8 @@ M.setup = function()
|
|||||||
|
|
||||||
---@type string? The file extension if detected
|
---@type string? The file extension if detected
|
||||||
local extension = vim.fn.fnamemodify(path, ":e")
|
local extension = vim.fn.fnamemodify(path, ":e")
|
||||||
---@type string? The filename if detected
|
---@type string The filename if detected
|
||||||
local filename = vim.fn.fnamemodify(path, ":t")
|
local filename = vim.fn.fnamemodify(path, ":t") or "[Unknown Filename]"
|
||||||
|
|
||||||
---Open a given file path in a given program and remove the buffer for the file.
|
---Open a given file path in a given program and remove the buffer for the file.
|
||||||
---@param buf integer The buffer handle for the opening buffer
|
---@param buf integer The buffer handle for the opening buffer
|
||||||
@ -104,35 +104,22 @@ M.setup = function()
|
|||||||
vim.api.nvim_buf_delete(buf, { force = true })
|
vim.api.nvim_buf_delete(buf, { force = true })
|
||||||
end
|
end
|
||||||
|
|
||||||
local extension_callbacks = {
|
local extensions = {
|
||||||
["pdf"] = open_mime,
|
"pdf",
|
||||||
["djvu"] = "pdf",
|
"djvu",
|
||||||
["wav"] = open_mime,
|
"wav",
|
||||||
["png"] = open_mime,
|
"png",
|
||||||
["jpg"] = "png",
|
"jpg",
|
||||||
["docx"] = open_mime,
|
"docx",
|
||||||
["mp4"] = open_mime,
|
"mp4",
|
||||||
["webm"] = open_mime,
|
"webm",
|
||||||
["pptx"] = open_mime,
|
"pptx",
|
||||||
["gif"] = "mp4",
|
"gif",
|
||||||
}
|
}
|
||||||
|
|
||||||
---Get the extension callback for a given extension. Will do a recursive lookup if an extension callback is actually
|
if extension and not extension:match("^%s*$") and intercept_file_open then
|
||||||
---of type string to get the correct extension
|
if vim.list_contains(extensions, extension) then
|
||||||
---@param ext string A file extension. Example: `png`.
|
open_mime(bufnr, path, filename)
|
||||||
---@return fun(bufnr: integer, path: string, filename: string?) extension_callback The extension callback to invoke, expects a buffer handle, file path, and filename.
|
|
||||||
local function extension_lookup(ext)
|
|
||||||
local callback = extension_callbacks[ext]
|
|
||||||
if type(callback) == "string" then
|
|
||||||
callback = extension_lookup(callback)
|
|
||||||
end
|
|
||||||
return callback
|
|
||||||
end
|
|
||||||
|
|
||||||
if extension ~= nil and not extension:match("^%s*$") and intercept_file_open then
|
|
||||||
local callback = extension_lookup(extension)
|
|
||||||
if type(callback) == "function" then
|
|
||||||
callback(bufnr, path, filename)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
Loading…
Reference in New Issue
Block a user