Skip to content

Commit 0f4eb33

Browse files
Check if capture destination file exists and prompt to create one if missing
1 parent 7bc3ea0 commit 0f4eb33

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lua/orgmode/capture/init.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ end
111111
function Capture:refile(confirm)
112112
local is_modified = vim.bo.modified
113113
local file, lines, item, template = self:_get_refile_vars()
114+
if not file then
115+
return
116+
end
114117
local headline_title = template.headline
115118
if confirm and is_modified then
116119
local choice = vim.fn.confirm(string.format('Do you want to refile this to %s?', file), '&Yes\n&No')
@@ -135,6 +138,9 @@ end
135138
---Triggered when refiling to destination from capture buffer
136139
function Capture:refile_to_destination()
137140
local file, lines, item = self:_get_refile_vars()
141+
if not file then
142+
return
143+
end
138144
self:_refile_content_with_fallback(lines, file, item)
139145
self:kill()
140146
end
@@ -143,6 +149,15 @@ end
143149
function Capture:_get_refile_vars()
144150
local template = vim.api.nvim_buf_get_var(0, 'org_template') or {}
145151
local file = vim.fn.resolve(vim.fn.fnamemodify(template.target or config.org_default_notes_file, ':p'))
152+
if vim.fn.filereadable(file) == 0 then
153+
local choice = vim.fn.confirm(('Refile destination %s does not exist. Create now?'):format(file), '&Yes\n&No')
154+
if choice ~= 1 then
155+
utils.echo_error('Cannot proceed without a valid refile destination')
156+
return
157+
end
158+
vim.fn.mkdir(vim.fn.fnamemodify(file, ':h'), 'p')
159+
vim.fn.writefile({}, file)
160+
end
146161
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
147162
local org_file = File.from_content(lines, 'capture', utils.current_file_path())
148163
local item = nil

0 commit comments

Comments
 (0)