diff --git a/lua/orgmode/org/links/types/headline_search.lua b/lua/orgmode/org/links/types/headline_search.lua index e1d517a27..5878dc53e 100644 --- a/lua/orgmode/org/links/types/headline_search.lua +++ b/lua/orgmode/org/links/types/headline_search.lua @@ -1,4 +1,5 @@ local utils = require('orgmode.utils') +local fs = require('orgmode.utils.fs') local OrgLinkUrl = require('orgmode.org.links.url') local link_utils = require('orgmode.org.links.utils') @@ -74,6 +75,19 @@ function OrgLinkHeadlineSearch:autocomplete(link) f = f:gsub('^' .. opts.file_path, opts.link_url.path) table.insert(valid_filenames, f) end + + local real_path = opts.link_url:get_real_path() + + if not real_path then + local substitute_path = fs.substitute_path(opts.file_path) + if substitute_path then + local full_path = vim.fn.fnamemodify(substitute_path, ':p') + if f:find('^' .. full_path) then + f = f:gsub('^' .. full_path, opts.link_url.path) + table.insert(valid_filenames, f) + end + end + end end local prefix = opts.link_url:get_protocol() == 'file' and 'file:' or '' diff --git a/lua/orgmode/org/links/url.lua b/lua/orgmode/org/links/url.lua index e188c82c9..b5d3d5ef4 100644 --- a/lua/orgmode/org/links/url.lua +++ b/lua/orgmode/org/links/url.lua @@ -20,20 +20,20 @@ end ---@return string | nil function OrgLinkUrl:get_file_path() if self.protocol == 'file' then - return self:_get_real_path() + return self:get_real_path() or self.path end local first_char = self.path:sub(1, 1) if first_char == '/' then - return self:_get_real_path() + return self:get_real_path() or self.path end if (first_char == '.' and (self.path:sub(1, 3) == '../' or self.path:sub(1, 2) == './')) or (first_char == '~' and self.path:sub(2, 2) == '/') then - return self:_get_real_path() + return self:get_real_path() or self.path end return nil @@ -76,10 +76,8 @@ function OrgLinkUrl:get_id() return self.path end ----@private ----@return string -function OrgLinkUrl:_get_real_path() - return fs.get_real_path(self.path) or self.path +function OrgLinkUrl:get_real_path() + return fs.get_real_path(self.path) or nil end ---@return string