1
1
local utils = require (' orgmode.utils' )
2
2
local config = require (' orgmode.config' )
3
3
local Files = require (' orgmode.parser.files' )
4
+ local File = require (' orgmode.parser.file' )
4
5
local Templates = require (' orgmode.capture.templates' )
5
6
6
7
local capture_augroup = vim .api .nvim_create_augroup (' OrgCapture' , { clear = true })
99
100
--- @param confirm ? boolean
100
101
function Capture :refile (confirm )
101
102
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 ()
105
104
local headline_title = template .headline
106
105
if confirm and is_modified then
107
106
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)
111
110
end
112
111
end
113
112
vim .defer_fn (function ()
114
- -- TODO: Parse refile content as org file and update refile destination to point to headline or root
115
113
if headline_title then
116
- self :refile_to_headline (file , lines , nil , headline_title )
114
+ self :refile_to_headline (file , lines , item , headline_title )
117
115
else
118
- self :_refile_to_end (file , lines )
116
+ self :_refile_to_end (file , lines , item )
119
117
end
120
118
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 ()
124
121
end
125
- vim .cmd (' silent! wq' )
126
122
end , 0 )
127
123
end
128
124
129
125
--- Triggered when refiling to destination from capture buffer
130
126
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' )
132
136
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
136
142
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
142
144
end
143
145
144
146
--- 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)
158
160
return self :_refile_to_end (archive_file , lines , item , string.format (' Archived to %s' , archive_file ))
159
161
end
160
162
163
+ --- @private
161
164
--- @param file string
162
165
--- @param lines string[]
163
166
--- @param item ? Section
@@ -172,6 +175,7 @@ function Capture:_refile_to_end(file, lines, item, message)
172
175
return true
173
176
end
174
177
178
+ --- @private
175
179
--- @param lines string[]
176
180
--- @param fallback_file string
177
181
--- @param item ? Section
@@ -198,6 +202,10 @@ function Capture:_refile_content_with_fallback(lines, fallback_file, item)
198
202
return self :refile_to_headline (destination_file , lines , item , destination [2 ])
199
203
end
200
204
205
+ --- @param destination_file string
206
+ --- @param lines string[]
207
+ --- @param item ? Section
208
+ --- @param headline_title ? string
201
209
function Capture :refile_to_headline (destination_file , lines , item , headline_title )
202
210
local agenda_file = Files .get (destination_file )
203
211
local headline
@@ -223,6 +231,7 @@ function Capture:refile_to_headline(destination_file, lines, item, headline_titl
223
231
return true
224
232
end
225
233
234
+ --- @private
226
235
--- @param file string
227
236
--- @param lines string[]
228
237
--- @param item ? Section
0 commit comments