aboutsummaryrefslogtreecommitdiff
path: root/lua/muwiki/links.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/muwiki/links.lua')
-rw-r--r--lua/muwiki/links.lua146
1 files changed, 55 insertions, 91 deletions
diff --git a/lua/muwiki/links.lua b/lua/muwiki/links.lua
index 55ce692..1a681fa 100644
--- a/lua/muwiki/links.lua
+++ b/lua/muwiki/links.lua
@@ -1,7 +1,4 @@
-local config = require('muwiki.config')
-local paths = require('muwiki.paths')
local utils = require('muwiki.utils')
-local external = require('muwiki.external')
local M = {}
@@ -15,6 +12,12 @@ local function get_link_type(target)
return 'wiki'
end
+local function resolve_file_url(url)
+ local path = url:gsub('^file://', '')
+ local resolved = utils.resolve(path, nil)
+ return 'file://' .. resolved
+end
+
function M.get_link()
local cursor = vim.api.nvim_win_get_cursor(0)
@@ -60,20 +63,8 @@ function M.get_link()
}
end
-local function resolve_file_url(url)
- local path = paths.strip_file_protocol(url)
- local path_type = paths.get_path_type(path)
-
- if path_type == 'relative' then
- local current_dir = vim.fs.dirname(vim.api.nvim_buf_get_name(0))
- path = vim.fs.joinpath(current_dir, path)
- end
-
- return 'file://' .. vim.fs.normalize(path)
-end
-
local function get_wiki_root_or_notify()
- local wiki_root = config.wiki_root(0)
+ local wiki_root = utils.wiki_root(0)
if not wiki_root then
vim.notify('Not in a wiki buffer', vim.log.levels.ERROR)
return nil
@@ -88,10 +79,9 @@ function M.open_link()
return
end
+ -- Web links - open with xdg-open
if link.type == 'web' then
- if config.options.use_external_handlers then
- external.open(link.target)
- end
+ vim.system({'xdg-open', link.target}, {detach = true})
return
end
@@ -100,107 +90,89 @@ function M.open_link()
return
end
+ -- File links - use xdg-open
if link.type == 'file' then
if vim.startswith(link.target, 'file://') then
- if config.options.use_external_handlers then
- local absolute_url = resolve_file_url(link.target)
- external.open(absolute_url)
- end
+ -- file:// URLs - open with xdg-open
+ vim.system({'xdg-open', resolve_file_url(link.target)}, {detach = true})
else
+ -- Local file paths - open with xdg-open
local ok, file_path = pcall(utils.resolve, link.target, wiki_root)
if not ok then
vim.notify(string.format('Cannot resolve path: %s', link.target), vim.log.levels.ERROR)
return
end
- if not utils.file_exists(file_path) then
- vim.notify(string.format('File not found: %s', file_path), vim.log.levels.ERROR)
- return
- end
-
- local ext = file_path:match('%.([^%.]+)$') or ''
- if not utils.is_text_file(ext) then
- if config.options.use_external_handlers then
- external.open(file_path)
- end
- return
- end
-
- utils.open_in_buffer(file_path)
+ vim.system({'xdg-open', file_path}, {detach = true})
end
return
end
- -- wiki link
+ -- Wiki links - open in buffer
local file_path = utils.resolve(link.target, wiki_root)
- utils.open_wiki_file(file_path)
+ utils.open_in_buffer(file_path)
end
-function M.open_link_with()
- local link = M.get_link()
+function M.open_with_menu(handlers, link)
+ -- Get link automatically if not provided
+ link = link or M.get_link()
if not link then
- vim.notify('No link found under cursor', vim.log.levels.WARN)
- return
- end
-
- if link.type == 'wiki' then
- vim.notify('Menu not available for wiki links', vim.log.levels.WARN)
+ vim.notify('No link under cursor', vim.log.levels.WARN)
return
end
- local url = link.target
- if link.type == 'file' then
- local wiki_root = get_wiki_root_or_notify()
- if not wiki_root then
- return
- end
- url = utils.resolve(url, wiki_root)
- end
-
- -- Find matching handlers for URL
- local function handler_matches(handler, url)
- local pattern = handler.pattern
- if pattern == nil then
- return true
- end
- if type(pattern) == 'string' then
- return url:match(pattern) ~= nil
- end
- for _, p in ipairs(pattern) do
- if url:match(p) then
- return true
- end
- end
- return false
- end
+ -- Get file extension
+ local ext = link.target:match('%.([^%.]+)$')
+ ext = ext and ext:lower() or nil
+ -- Filter handlers by extension
local matching_handlers = {}
- for _, handler in ipairs(config.options.external_handlers) do
- if handler_matches(handler, url) then
+ for _, handler in ipairs(handlers) do
+ -- Check if handler matches this extension
+ if handler.exts then
+ for _, handler_ext in ipairs(handler.exts) do
+ if handler_ext:lower() == ext then
+ table.insert(matching_handlers, handler)
+ break
+ end
+ end
+ else
+ -- No exts specified - matches all
table.insert(matching_handlers, handler)
end
end
if #matching_handlers == 0 then
- vim.notify('No handlers available for this URL', vim.log.levels.WARN)
+ vim.notify('No handlers for file type: ' .. (ext or 'unknown'), vim.log.levels.WARN)
return
end
- if #matching_handlers == 1 then
- external.execute(matching_handlers[1], url)
- return
+ -- Resolve the path
+ local url = link.target
+ if link.type == 'file' and not vim.startswith(url, 'file://') then
+ local wiki_root = get_wiki_root_or_notify()
+ if wiki_root then
+ url = utils.resolve(url, wiki_root)
+ end
end
+ -- Build menu items
local handler_names = {}
for _, handler in ipairs(matching_handlers) do
table.insert(handler_names, handler.name)
end
+ -- Show menu with link text as title
vim.ui.select(handler_names, {
- prompt = 'Open with:',
+ prompt = 'Open "' .. link.text .. '" with:',
}, function(choice, idx)
if choice and idx then
- external.execute(matching_handlers[idx], url)
+ local handler = matching_handlers[idx]
+ if type(handler.cmd) == 'function' then
+ handler.cmd(url)
+ else
+ vim.system({ handler.cmd, url }, { detach = true })
+ end
end
end)
end
@@ -254,17 +226,17 @@ function M.create_link()
vim.api.nvim_buf_set_text(0, start_row, start_col, end_row, end_col, { link_text })
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Esc>', true, false, true), 'n', false)
- local wiki_root = config.wiki_root(0)
+ local wiki_root = utils.wiki_root(0)
if not wiki_root then
vim.notify('Not in a wiki buffer', vim.log.levels.ERROR)
return
end
local target_path = utils.resolve(link_target, wiki_root)
- utils.open_wiki_file(target_path)
+ utils.open_in_buffer(target_path)
end
-local function jump_link(direction)
+function M.jump_link(direction)
local flags = direction == 'next' and 'w' or 'bw'
local msg = direction == 'next' and 'No more links' or 'No previous links'
@@ -273,12 +245,4 @@ local function jump_link(direction)
end
end
-function M.next_link()
- jump_link('next')
-end
-
-function M.prev_link()
- jump_link('prev')
-end
-
return M