Skip to content

Commit 83bf78a

Browse files
authored
Allow dynamic target in capture templates (#591)
1 parent 6b6eb8e commit 83bf78a

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

DOCS.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,15 @@ Journal example:<br />
421421
} }
422422
```
423423

424+
Journal example with dynamic target, i.e. a separate file per month:<br />
425+
```lua
426+
{ J = {
427+
description = 'Journal',
428+
template = '\n*** %<%Y-%m-%d> %<%A>\n**** %U\n\n%?',
429+
target = '~/sync/org/journal/%<%Y-%m>.org'
430+
} }
431+
```
432+
424433
Nested key example:<br />
425434
```lua
426435
{

lua/orgmode/capture/init.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ end
135135
---@private
136136
function Capture:_get_refile_vars()
137137
local template = vim.api.nvim_buf_get_var(0, 'org_template') or {}
138-
local file = vim.fn.resolve(vim.fn.fnamemodify(template.target or config.org_default_notes_file, ':p'))
138+
local target = self.templates:compile_target(template.target or config.org_default_notes_file)
139+
local file = vim.fn.resolve(vim.fn.fnamemodify(target, ':p'))
140+
139141
if vim.fn.filereadable(file) == 0 then
140142
local choice = vim.fn.confirm(('Refile destination %s does not exist. Create now?'):format(file), '&Yes\n&No')
141143
if choice ~= 1 then

lua/orgmode/capture/templates.lua

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,16 @@ function Templates:compile(template)
5555
if type(content) == 'table' then
5656
content = table.concat(content, '\n')
5757
end
58-
content = self:_compile_dates(content)
59-
content = self:_compile_expansions(content)
60-
content = self:_compile_expressions(content)
61-
content = self:_compile_prompts(content)
58+
content = self:_compile(content)
6259
return vim.split(content, '\n', true)
6360
end
6461

62+
---@param target string
63+
---@return string
64+
function Templates:compile_target(target)
65+
return self:_compile(target)
66+
end
67+
6568
function Templates:setup()
6669
local initial_position = vim.fn.search('%?')
6770
local is_at_end_of_line = vim.fn.search('%?$') > 0
@@ -76,6 +79,17 @@ function Templates:setup()
7679
end
7780
end
7881

82+
---@private
83+
---@param content string
84+
---@return string
85+
function Templates:_compile(content)
86+
content = self:_compile_dates(content)
87+
content = self:_compile_expansions(content)
88+
content = self:_compile_expressions(content)
89+
content = self:_compile_prompts(content)
90+
return content
91+
end
92+
7993
---@param content string
8094
---@return string
8195
function Templates:_compile_expansions(content, found_expansions)

0 commit comments

Comments
 (0)