Skip to content

Commit bb4afa8

Browse files
Fix refiling from capture buffer. Closes #224
1 parent ae9d9fd commit bb4afa8

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

lua/orgmode/capture/init.lua

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local utils = require('orgmode.utils')
22
local config = require('orgmode.config')
33
local Files = require('orgmode.parser.files')
4+
local File = require('orgmode.parser.file')
45
local Templates = require('orgmode.capture.templates')
56

67
local capture_augroup = vim.api.nvim_create_augroup('OrgCapture', { clear = true })
@@ -99,9 +100,7 @@ end
99100
---@param confirm? boolean
100101
function Capture:refile(confirm)
101102
local is_modified = vim.bo.modified
102-
local template = vim.api.nvim_buf_get_var(0, 'org_template') or {}
103-
local file = vim.fn.fnamemodify(template.target or config.org_default_notes_file, ':p')
104-
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
103+
local file, lines, item, template = self:_get_refile_vars()
105104
local headline_title = template.headline
106105
if confirm and is_modified then
107106
local choice = vim.fn.confirm(string.format('Do you want to refile this to %s?', file), '&Yes\n&No')
@@ -111,34 +110,37 @@ function Capture:refile(confirm)
111110
end
112111
end
113112
vim.defer_fn(function()
114-
-- TODO: Parse refile content as org file and update refile destination to point to headline or root
115113
if headline_title then
116-
self:refile_to_headline(file, lines, nil, headline_title)
114+
self:refile_to_headline(file, lines, item, headline_title)
117115
else
118-
self:_refile_to_end(file, lines)
116+
self:_refile_to_end(file, lines, item)
119117
end
120118

121-
if self.wipeout_autocmd_id then
122-
vim.api.nvim_del_autocmd(self.wipeout_autocmd_id)
123-
self.wipeout_autocmd_id = nil
119+
if not confirm then
120+
self:kill()
124121
end
125-
vim.cmd('silent! wq')
126122
end, 0)
127123
end
128124

129125
---Triggered when refiling to destination from capture buffer
130126
function Capture:refile_to_destination()
131-
local template = vim.api.nvim_buf_get_var(0, 'org_template')
127+
local file, lines, item = self:_get_refile_vars()
128+
self:_refile_content_with_fallback(lines, file, item)
129+
self:kill()
130+
end
131+
132+
---@private
133+
function Capture:_get_refile_vars()
134+
local template = vim.api.nvim_buf_get_var(0, 'org_template') or {}
135+
local file = vim.fn.fnamemodify(template.target or config.org_default_notes_file, ':p')
132136
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
133-
local default_file = vim.fn.fnamemodify(template.target or config.org_default_notes_file, ':p')
134-
-- TODO: Parse refile content as org file and update refile destination to point to headline or root
135-
self:_refile_content_with_fallback(lines, default_file)
137+
local org_file = File.from_content(lines, 'capture', vim.api.nvim_buf_get_name(0))
138+
local item = nil
139+
if org_file then
140+
item = org_file:get_headlines()[1]
141+
end
136142

137-
vim.api.nvim_create_autocmd('BufWipeout', {
138-
buffer = 0,
139-
group = capture_augroup,
140-
command = 'silent! wq',
141-
})
143+
return file, lines, item, template
142144
end
143145

144146
---Triggered from org file when we want to refile headline
@@ -158,6 +160,7 @@ function Capture:refile_file_headline_to_archive(file, item, archive_file)
158160
return self:_refile_to_end(archive_file, lines, item, string.format('Archived to %s', archive_file))
159161
end
160162

163+
---@private
161164
---@param file string
162165
---@param lines string[]
163166
---@param item? Section
@@ -172,6 +175,7 @@ function Capture:_refile_to_end(file, lines, item, message)
172175
return true
173176
end
174177

178+
---@private
175179
---@param lines string[]
176180
---@param fallback_file string
177181
---@param item? Section
@@ -198,6 +202,10 @@ function Capture:_refile_content_with_fallback(lines, fallback_file, item)
198202
return self:refile_to_headline(destination_file, lines, item, destination[2])
199203
end
200204

205+
---@param destination_file string
206+
---@param lines string[]
207+
---@param item? Section
208+
---@param headline_title? string
201209
function Capture:refile_to_headline(destination_file, lines, item, headline_title)
202210
local agenda_file = Files.get(destination_file)
203211
local headline
@@ -223,6 +231,7 @@ function Capture:refile_to_headline(destination_file, lines, item, headline_titl
223231
return true
224232
end
225233

234+
---@private
226235
---@param file string
227236
---@param lines string[]
228237
---@param item? Section

lua/orgmode/parser/file.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ end
150150
---@param content string|table
151151
---@param category string
152152
---@param filename string
153-
---@param is_archive_file boolean
153+
---@param is_archive_file? boolean
154154
---@return File|nil
155155
function File.from_content(content, category, filename, is_archive_file)
156156
local str_content = table.concat(content, '\n')

lua/orgmode/parser/section.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ function Section:demote(amount, demote_child_sections, dryRun)
474474
break
475475
end
476476
local content_line = string.rep(' ', amount) .. content
477+
table.insert(lines, content_line)
477478
if not dryRun then
478479
vim.api.nvim_call_function('setline', { self.range.start_line + i - 1, content_line })
479480
end

0 commit comments

Comments
 (0)