Skip to content

Commit 215dd10

Browse files
fix(#628): Handle absolute paths for org_archive_location
Fixes #628
1 parent e9c08d5 commit 215dd10

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

lua/orgmode/config/init.lua

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local instance = {}
22
local utils = require('orgmode.utils')
3+
local fs = require('orgmode.utils.fs')
34
local defaults = require('orgmode.config.defaults')
45
---@type table<string, MapEntry>
56
local mappings = require('orgmode.config.mappings')
@@ -154,8 +155,10 @@ end
154155
function Config:get_all_files()
155156
local all_filenames = {}
156157
if self.opts.org_default_notes_file and self.opts.org_default_notes_file ~= '' then
157-
local default_full_path = vim.fn.resolve(vim.fn.expand(self.opts.org_default_notes_file, ':p'))
158-
table.insert(all_filenames, default_full_path)
158+
local default_full_path = vim.fn.resolve(vim.fn.fnamemodify(self.opts.org_default_notes_file, ':p'))
159+
if vim.loop.fs_stat(default_full_path) then
160+
table.insert(all_filenames, default_full_path)
161+
end
159162
end
160163
local files = self.opts.org_agenda_files
161164
if not files or files == '' or (type(files) == 'table' and vim.tbl_isempty(files)) then
@@ -317,13 +320,23 @@ function Config:parse_archive_location(file, archive_loc)
317320
-- TODO: Support archive to headline
318321
local parts = vim.split(archive_loc, '::')
319322
local archive_location = vim.trim(parts[1])
320-
if archive_location:find('%%s') then
321-
local file_path = vim.fn.fnamemodify(file, ':p:h')
322-
local file_name = vim.fn.fnamemodify(file, ':t')
323-
local archive_filename = string.format(archive_location, file_name)
323+
if not archive_location:find('%%s') then
324+
return vim.fn.fnamemodify(archive_location, ':p')
325+
end
326+
327+
local file_path = vim.fn.fnamemodify(file, ':p:h')
328+
local file_name = vim.fn.fnamemodify(file, ':t')
329+
local archive_filename = string.format(archive_location, file_name)
330+
331+
-- If org_archive_location is defined as relative path (example: "archive/%s_archive")
332+
-- then we need to prepend the file path to it
333+
local is_full_path = fs.substitute_path(archive_filename)
334+
335+
if not is_full_path then
324336
return string.format('%s/%s', file_path, archive_filename)
325337
end
326-
return vim.fn.fnamemodify(archive_location, ':p')
338+
339+
return vim.fn.fnamemodify(archive_filename, ':p')
327340
end
328341

329342
function Config:is_archive_file(file)

lua/orgmode/utils/fs.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ function M.substitute_path(path_str)
1616
elseif path_str:match('^%.%./') then
1717
local base = vim.fn.fnamemodify(utils.current_file_path(), ':p:h')
1818
return base .. '/' .. path_str
19-
else
20-
return false
2119
end
20+
return false
2221
end
2322

2423
---@param filepath string

tests/plenary/config/config_spec.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
local orgmode = require('orgmode')
2+
3+
describe('Config', function()
4+
local refile_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/refile.org'
5+
6+
it('should parse an absolute archive location for a file', function()
7+
local org = orgmode.setup({
8+
org_agenda_files = vim.fn.getcwd() .. '/tests/plenary/fixtures/*',
9+
org_default_notes_file = refile_file,
10+
org_archive_location = vim.fn.getcwd() .. '/tests/plenary/fixtures/archive/%s_archive::',
11+
})
12+
local config = require('orgmode.config')
13+
assert.are.same(
14+
config:parse_archive_location(refile_file),
15+
vim.fn.getcwd() .. '/tests/plenary/fixtures/archive/refile.org_archive'
16+
)
17+
end)
18+
19+
it('should parse a relative archive location for a file', function()
20+
local org = orgmode.setup({
21+
org_agenda_files = vim.fn.getcwd() .. '/tests/plenary/fixtures/*',
22+
org_default_notes_file = refile_file,
23+
org_archive_location = 'archives_relative/%s_archive::',
24+
})
25+
local config = require('orgmode.config')
26+
assert.are.same(
27+
config:parse_archive_location(refile_file),
28+
vim.fn.getcwd() .. '/tests/plenary/fixtures/archives_relative/refile.org_archive'
29+
)
30+
end)
31+
end)

0 commit comments

Comments
 (0)