Skip to content

Commit

Permalink
fix(#628): Handle absolute paths for org_archive_location
Browse files Browse the repository at this point in the history
Fixes #628
  • Loading branch information
kristijanhusak committed Nov 22, 2023
1 parent e9c08d5 commit 215dd10
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
27 changes: 20 additions & 7 deletions lua/orgmode/config/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local instance = {}
local utils = require('orgmode.utils')
local fs = require('orgmode.utils.fs')
local defaults = require('orgmode.config.defaults')
---@type table<string, MapEntry>
local mappings = require('orgmode.config.mappings')
Expand Down Expand Up @@ -154,8 +155,10 @@ end
function Config:get_all_files()
local all_filenames = {}
if self.opts.org_default_notes_file and self.opts.org_default_notes_file ~= '' then
local default_full_path = vim.fn.resolve(vim.fn.expand(self.opts.org_default_notes_file, ':p'))
table.insert(all_filenames, default_full_path)
local default_full_path = vim.fn.resolve(vim.fn.fnamemodify(self.opts.org_default_notes_file, ':p'))
if vim.loop.fs_stat(default_full_path) then
table.insert(all_filenames, default_full_path)
end
end
local files = self.opts.org_agenda_files
if not files or files == '' or (type(files) == 'table' and vim.tbl_isempty(files)) then
Expand Down Expand Up @@ -317,13 +320,23 @@ function Config:parse_archive_location(file, archive_loc)
-- TODO: Support archive to headline
local parts = vim.split(archive_loc, '::')
local archive_location = vim.trim(parts[1])
if archive_location:find('%%s') then
local file_path = vim.fn.fnamemodify(file, ':p:h')
local file_name = vim.fn.fnamemodify(file, ':t')
local archive_filename = string.format(archive_location, file_name)
if not archive_location:find('%%s') then
return vim.fn.fnamemodify(archive_location, ':p')
end

local file_path = vim.fn.fnamemodify(file, ':p:h')
local file_name = vim.fn.fnamemodify(file, ':t')
local archive_filename = string.format(archive_location, file_name)

-- If org_archive_location is defined as relative path (example: "archive/%s_archive")
-- then we need to prepend the file path to it
local is_full_path = fs.substitute_path(archive_filename)

if not is_full_path then
return string.format('%s/%s', file_path, archive_filename)
end
return vim.fn.fnamemodify(archive_location, ':p')

return vim.fn.fnamemodify(archive_filename, ':p')
end

function Config:is_archive_file(file)
Expand Down
3 changes: 1 addition & 2 deletions lua/orgmode/utils/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ function M.substitute_path(path_str)
elseif path_str:match('^%.%./') then
local base = vim.fn.fnamemodify(utils.current_file_path(), ':p:h')
return base .. '/' .. path_str
else
return false
end
return false
end

---@param filepath string
Expand Down
31 changes: 31 additions & 0 deletions tests/plenary/config/config_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
local orgmode = require('orgmode')

describe('Config', function()
local refile_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/refile.org'

it('should parse an absolute archive location for a file', function()
local org = orgmode.setup({
org_agenda_files = vim.fn.getcwd() .. '/tests/plenary/fixtures/*',
org_default_notes_file = refile_file,
org_archive_location = vim.fn.getcwd() .. '/tests/plenary/fixtures/archive/%s_archive::',
})
local config = require('orgmode.config')
assert.are.same(
config:parse_archive_location(refile_file),
vim.fn.getcwd() .. '/tests/plenary/fixtures/archive/refile.org_archive'
)
end)

it('should parse a relative archive location for a file', function()
local org = orgmode.setup({
org_agenda_files = vim.fn.getcwd() .. '/tests/plenary/fixtures/*',
org_default_notes_file = refile_file,
org_archive_location = 'archives_relative/%s_archive::',
})
local config = require('orgmode.config')
assert.are.same(
config:parse_archive_location(refile_file),
vim.fn.getcwd() .. '/tests/plenary/fixtures/archives_relative/refile.org_archive'
)
end)
end)

0 comments on commit 215dd10

Please sign in to comment.