From 7775a8efedde1547e6c2f50972bdb98095ed01d7 Mon Sep 17 00:00:00 2001 From: maidlover Date: Sun, 11 Jun 2023 12:39:32 +0200 Subject: [PATCH 1/8] fix: Open file hyperlinks starting with ~ --- lua/orgmode/org/hyperlinks.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lua/orgmode/org/hyperlinks.lua b/lua/orgmode/org/hyperlinks.lua index b15d91842..af87d657f 100644 --- a/lua/orgmode/org/hyperlinks.lua +++ b/lua/orgmode/org/hyperlinks.lua @@ -148,6 +148,9 @@ end function Hyperlinks.get_file_real_path(url_path) local path = url_path path = path:gsub('^file:', '') + if path:match('^~/') then + path = path:gsub('^~', os.getenv('HOME')) + end if path:match('^/') then return path end From 456931185042282ca48b5f2dfafe8304b0455fab Mon Sep 17 00:00:00 2001 From: maidl0ver <117573165+maidl0ver@users.noreply.github.com> Date: Sun, 18 Jun 2023 15:25:18 +0200 Subject: [PATCH 2/8] Add external openers for hyperlinks --- lua/orgmode/org/mappings.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lua/orgmode/org/mappings.lua b/lua/orgmode/org/mappings.lua index a2dc0e4e5..b7901895d 100644 --- a/lua/orgmode/org/mappings.lua +++ b/lua/orgmode/org/mappings.lua @@ -754,6 +754,8 @@ function OrgMappings:open_at_point() local parts = vim.split(link, '][', true) local url = parts[1] local link_ctx = { base = url, skip_add_prefix = true } + local ext_opener = 'xdg-open' + if url:find('^file:') then if url:find(' +', 1, true) then parts = vim.split(url, ' +', true) @@ -766,6 +768,9 @@ function OrgMappings:open_at_point() if url:find('^file:(.-)::') then link_ctx.line = url + elseif vim.filetype.match({ filename = url }) == nil then + local filepath = Hyperlinks.get_file_real_path(url) + vim.fn.jobstart(ext_opener .. ' ' .. vim.fn.shellescape(filepath), { detach = true }) else vim.cmd(string.format('edit %s', Hyperlinks.get_file_real_path(url))) vim.cmd([[normal! zv]]) @@ -783,6 +788,13 @@ function OrgMappings:open_at_point() return vim.cmd(string.format('edit %s', url)) end + if url:find('^custom:') then + url = string.gsub(url, '^custom:', '') + local opener = string.gsub(url, '^(.-):.*', '%1') + local target = string.gsub(url, '^.-:', '') + vim.fn.jobstart(opener .. ' ' .. vim.fn.shellescape(target), { detach = true }) + end + local headlines = Hyperlinks.find_matching_links(link_ctx) local current_headline = Files.get_closest_headline() if current_headline then From 47671f28e66218ada45585ecaf71254b1b929da2 Mon Sep 17 00:00:00 2001 From: maidl0ver <117573165+maidl0ver@users.noreply.github.com> Date: Sun, 18 Jun 2023 16:00:16 +0200 Subject: [PATCH 3/8] Move external opener to the config --- lua/orgmode/config/defaults.lua | 1 + lua/orgmode/org/mappings.lua | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/orgmode/config/defaults.lua b/lua/orgmode/config/defaults.lua index 8b2397fae..28e36c8d2 100644 --- a/lua/orgmode/config/defaults.lua +++ b/lua/orgmode/config/defaults.lua @@ -52,6 +52,7 @@ local DefaultConfig = { deadline_reminder = true, scheduled_reminder = true, }, + org_external_opener = 'xdg-open', mappings = { disable_all = false, prefix = 'o', diff --git a/lua/orgmode/org/mappings.lua b/lua/orgmode/org/mappings.lua index b7901895d..fc4338bcc 100644 --- a/lua/orgmode/org/mappings.lua +++ b/lua/orgmode/org/mappings.lua @@ -754,7 +754,6 @@ function OrgMappings:open_at_point() local parts = vim.split(link, '][', true) local url = parts[1] local link_ctx = { base = url, skip_add_prefix = true } - local ext_opener = 'xdg-open' if url:find('^file:') then if url:find(' +', 1, true) then @@ -770,7 +769,7 @@ function OrgMappings:open_at_point() link_ctx.line = url elseif vim.filetype.match({ filename = url }) == nil then local filepath = Hyperlinks.get_file_real_path(url) - vim.fn.jobstart(ext_opener .. ' ' .. vim.fn.shellescape(filepath), { detach = true }) + vim.fn.jobstart(config.org_external_opener .. ' ' .. vim.fn.shellescape(filepath), { detach = true }) else vim.cmd(string.format('edit %s', Hyperlinks.get_file_real_path(url))) vim.cmd([[normal! zv]]) From 25c3b862ee30ba863a906416ad4fb44fbfce2020 Mon Sep 17 00:00:00 2001 From: maidl0ver <117573165+maidl0ver@users.noreply.github.com> Date: Mon, 19 Jun 2023 21:51:37 +0200 Subject: [PATCH 4/8] Change custom: links to shell: --- lua/orgmode/org/mappings.lua | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lua/orgmode/org/mappings.lua b/lua/orgmode/org/mappings.lua index fc4338bcc..4a467d131 100644 --- a/lua/orgmode/org/mappings.lua +++ b/lua/orgmode/org/mappings.lua @@ -787,11 +787,12 @@ function OrgMappings:open_at_point() return vim.cmd(string.format('edit %s', url)) end - if url:find('^custom:') then - url = string.gsub(url, '^custom:', '') - local opener = string.gsub(url, '^(.-):.*', '%1') - local target = string.gsub(url, '^.-:', '') - vim.fn.jobstart(opener .. ' ' .. vim.fn.shellescape(target), { detach = true }) + if url:find('^shell:') then + url = string.gsub(url, '^shell:', '') + url = vim.fn.shellescape(url) + print(url) + vim.fn.jobstart(url, { detach = true }) + return end local headlines = Hyperlinks.find_matching_links(link_ctx) From cca7f7e02acb273154365fd397c16b7697611514 Mon Sep 17 00:00:00 2001 From: maidl0ver <117573165+maidl0ver@users.noreply.github.com> Date: Mon, 19 Jun 2023 22:18:35 +0200 Subject: [PATCH 5/8] Add a config option for opening file: links externally --- lua/orgmode/config/defaults.lua | 1 + lua/orgmode/org/mappings.lua | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/orgmode/config/defaults.lua b/lua/orgmode/config/defaults.lua index 28e36c8d2..e5a325f63 100644 --- a/lua/orgmode/config/defaults.lua +++ b/lua/orgmode/config/defaults.lua @@ -52,6 +52,7 @@ local DefaultConfig = { deadline_reminder = true, scheduled_reminder = true, }, + org_nontext_hyperlinks = false, org_external_opener = 'xdg-open', mappings = { disable_all = false, diff --git a/lua/orgmode/org/mappings.lua b/lua/orgmode/org/mappings.lua index 4a467d131..f98fb7b13 100644 --- a/lua/orgmode/org/mappings.lua +++ b/lua/orgmode/org/mappings.lua @@ -767,7 +767,7 @@ function OrgMappings:open_at_point() if url:find('^file:(.-)::') then link_ctx.line = url - elseif vim.filetype.match({ filename = url }) == nil then + elseif config.org_nontext_hyperlinks and vim.filetype.match({ filename = url }) == nil then local filepath = Hyperlinks.get_file_real_path(url) vim.fn.jobstart(config.org_external_opener .. ' ' .. vim.fn.shellescape(filepath), { detach = true }) else From f5dccb6dc7be59abd810ab516f094cbb2843569e Mon Sep 17 00:00:00 2001 From: maidl0ver <117573165+maidl0ver@users.noreply.github.com> Date: Mon, 19 Jun 2023 22:58:28 +0200 Subject: [PATCH 6/8] Remove lines used for testing --- lua/orgmode/org/mappings.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/lua/orgmode/org/mappings.lua b/lua/orgmode/org/mappings.lua index f98fb7b13..a3ba1eaad 100644 --- a/lua/orgmode/org/mappings.lua +++ b/lua/orgmode/org/mappings.lua @@ -789,8 +789,6 @@ function OrgMappings:open_at_point() if url:find('^shell:') then url = string.gsub(url, '^shell:', '') - url = vim.fn.shellescape(url) - print(url) vim.fn.jobstart(url, { detach = true }) return end From 3194448e7bd2f2c3f5bdbef2b8224cb6d5620c64 Mon Sep 17 00:00:00 2001 From: maidlover Date: Fri, 30 Jun 2023 13:57:10 +0200 Subject: [PATCH 7/8] Add custom links --- lua/orgmode/config/defaults.lua | 9 +++++++++ lua/orgmode/org/mappings.lua | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/lua/orgmode/config/defaults.lua b/lua/orgmode/config/defaults.lua index e5a325f63..d36e0e735 100644 --- a/lua/orgmode/config/defaults.lua +++ b/lua/orgmode/config/defaults.lua @@ -54,6 +54,15 @@ local DefaultConfig = { }, org_nontext_hyperlinks = false, org_external_opener = 'xdg-open', + org_link_types = { + shell = { + handler = function(url) + url = string.gsub(url, '^shell:', '') + vim.fn.jobstart(url, { detach = true }) + return + end, + }, + }, mappings = { disable_all = false, prefix = 'o', diff --git a/lua/orgmode/org/mappings.lua b/lua/orgmode/org/mappings.lua index a3ba1eaad..8ec7d2eb7 100644 --- a/lua/orgmode/org/mappings.lua +++ b/lua/orgmode/org/mappings.lua @@ -793,6 +793,12 @@ function OrgMappings:open_at_point() return end + pref = string.gsub(url, '^(.-):.*', '%1') + if config.org_link_types[pref] ~= nil then + config.org_link_types[pref].handler(url) + return + end + local headlines = Hyperlinks.find_matching_links(link_ctx) local current_headline = Files.get_closest_headline() if current_headline then From b235e509eed551bce48ed8ea749976c696a724c8 Mon Sep 17 00:00:00 2001 From: maidlover Date: Fri, 30 Jun 2023 14:56:56 +0200 Subject: [PATCH 8/8] Remove hardcoded shell link --- lua/orgmode/org/mappings.lua | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lua/orgmode/org/mappings.lua b/lua/orgmode/org/mappings.lua index 8ec7d2eb7..6325f53d9 100644 --- a/lua/orgmode/org/mappings.lua +++ b/lua/orgmode/org/mappings.lua @@ -787,12 +787,6 @@ function OrgMappings:open_at_point() return vim.cmd(string.format('edit %s', url)) end - if url:find('^shell:') then - url = string.gsub(url, '^shell:', '') - vim.fn.jobstart(url, { detach = true }) - return - end - pref = string.gsub(url, '^(.-):.*', '%1') if config.org_link_types[pref] ~= nil then config.org_link_types[pref].handler(url)