Skip to content

Commit 94886d6

Browse files
fix(files): Use full match when finding bufnr for filename
In certain situations, buffer number for an org file was matched on it's archive location (.org_archive extension). For example, if two buffers are opened, one being `/path/to/org/todos.org` and `/path/to/org/todos.org_archive`, doing `vim.fn.bufnr('/path/to/org/todos.org')` could return the archive file because of the partial match. This is now fixed by doing a strict match `vim.fn.bufnr('^/path/to/org/todos.org$')`
1 parent 25b3ee8 commit 94886d6

File tree

4 files changed

+6
-6
lines changed

4 files changed

+6
-6
lines changed

lua/orgmode/api/file.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ end
113113
--- @return string
114114
function OrgFile:get_link()
115115
local filename = self.filename
116-
local bufnr = vim.fn.bufnr(filename)
116+
local bufnr = vim.fn.bufnr('^' .. filename .. '$')
117117

118118
if bufnr == -1 or not vim.api.nvim_buf_is_loaded(bufnr) then
119119
-- do remote edit

lua/orgmode/api/headline.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ end
276276
--- @return string
277277
function OrgHeadline:get_link()
278278
local filename = self.file.filename
279-
local bufnr = vim.fn.bufnr(filename)
279+
local bufnr = vim.fn.bufnr('^' .. filename .. '$')
280280

281281
if bufnr == -1 or not vim.api.nvim_buf_is_loaded(bufnr) then
282282
-- do remote edit

lua/orgmode/api/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function OrgApi.refile(opts)
8181
refile_opts.destination_headline = opts.destination._section
8282
end
8383

84-
local source_bufnr = vim.fn.bufnr(opts.source.file.filename) or -1
84+
local source_bufnr = vim.fn.bufnr('^' .. opts.source.file.filename .. '$') or -1
8585
local is_capture = source_bufnr > -1 and vim.b[source_bufnr].org_capture
8686

8787
if is_capture and orgmode.capture._window then

lua/orgmode/files/file.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ end
6060
---Load the file
6161
---@return OrgPromise<OrgFile | false>
6262
function OrgFile.load(filename)
63-
local bufnr = vim.fn.bufnr(filename) or -1
63+
local bufnr = vim.fn.bufnr('^' .. filename .. '$') or -1
6464

6565
if
6666
bufnr > -1
@@ -534,7 +534,7 @@ end
534534

535535
---@return number
536536
function OrgFile:bufnr()
537-
local bufnr = vim.fn.bufnr(self.filename) or -1
537+
local bufnr = vim.fn.bufnr('^' .. self.filename .. '$') or -1
538538
-- Do not consider unloaded buffers as valid
539539
-- Treesitter is not working in them
540540
if bufnr > -1 and vim.api.nvim_buf_is_loaded(bufnr) then
@@ -546,7 +546,7 @@ end
546546
---Return valid buffer handle or throw an error if it's not valid
547547
---@return number
548548
function OrgFile:get_valid_bufnr()
549-
local bufnr = vim.fn.bufnr(self.filename) or -1
549+
local bufnr = vim.fn.bufnr('^' .. self.filename .. '$') or -1
550550
if bufnr < 0 then
551551
error('[orgmode] No valid buffer for file ' .. self.filename .. ' to edit', 0)
552552
end

0 commit comments

Comments
 (0)