Skip to content

Commit

Permalink
add setup smart_view.custom argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Junker committed Jul 11, 2022
1 parent 3b0e45f commit a1c015c
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ local open_document_mime_types = {

local pager = "less -R"
local open_custom_commands = {}
local smart_custom_commands = {}
local run_executables = true
local show_line_numbers = false

Expand Down Expand Up @@ -158,6 +159,13 @@ local function exec_paging_html(command, node, ...)
end
end

local function exec_custom(command, node)
command = command:gsub("{}", '"' .. node.absolute_path .. '"')

return {{ BashExec = command }}
end


local function program_exists(p)
return (os.execute("type " .. p .. " > /dev/null 2>&1") == 0)
end
Expand Down Expand Up @@ -218,11 +226,6 @@ local function open_executable(node)
return {{ BashExec = node.absolute_path .. ' ; read -p "[enter to continue]"' }}
end

local function open_custom(node, command)
command = command:gsub("{}", '"' .. node.absolute_path .. '"')

return {{ BashExec = command }}
end

local function open(ctx)
local node = ctx.focused_node
Expand All @@ -237,13 +240,13 @@ local function open(ctx)
local command = entry["command"]
if command ~= nil then
if get_node_extension(node) == entry["extension"] then
return open_custom(node, command)
return exec_custom(command, node)
end
if node_mime == entry["mime"] then
return open_custom(node, command)
return exec_custom(command, node)
end
if entry["mime_regex"] ~= nil and node_mime:match(entry["mime_regex"]) then
return open_custom(node, command)
return exec_custom(command, node)
end
end
end
Expand Down Expand Up @@ -471,6 +474,22 @@ local function smart_view(ctx)
local node_mime = node.mime_essence

if node.is_dir == false or (node.is_symlink and node.symlink.is_dir == false) then

for _,entry in ipairs(smart_custom_commands) do
local command = entry["command"]
if command ~= nil then
if get_node_extension(node) == entry["extension"] then
return exec_custom(command, node)
end
if node_mime == entry["mime"] then
return exec_custom(command, node)
end
if entry["mime_regex"] ~= nil and node_mime:match(entry["mime_regex"]) then
return exec_custom(command, node)
end
end
end

local cases = {
["application/x-troff-man"] = function() return smart_view_man(node) or view_node(node) end,
["application/pdf"] = function() return smart_view_pdf(node) end,
Expand Down Expand Up @@ -542,6 +561,9 @@ local function setup(args)
end
end

if args.smart_view then
if args.smart_view.custom then
smart_custom_commands = args.smart_view.custom
end
end
end
Expand Down

0 comments on commit a1c015c

Please sign in to comment.