@@ -46,7 +46,7 @@ local expansions = {
4646--- @field whole_file ? boolean
4747
4848--- @class OrgCaptureTemplate : OrgCaptureTemplateOpts
49- --- @field private _compile_hooks (fun (content : string , type : ' target' | ' content' ): string )[]
49+ --- @field private _compile_hooks (fun (content : string , content_type : ' target' | ' content' ): string | nil )[]
5050local Template = {}
5151
5252--- @param opts OrgCaptureTemplateOpts
@@ -156,25 +156,21 @@ function Template:compile()
156156 if type (content ) == ' table' then
157157 content = table.concat (content , ' \n ' )
158158 end
159- content = self :_compile (content or ' ' , ' content' )
160- return vim .split (content , ' \n ' , { plain = true })
161- end
162-
163- function Template :has_input_prompts ()
164- return self .datetree and type (self .datetree ) == ' table' and self .datetree .time_prompt
165- end
166-
167- function Template :prompt_for_inputs ()
168- if not self :has_input_prompts () then
169- return Promise .resolve (true )
170- end
171- return Calendar .new ({ date = Date .now () }):open ():next (function (date )
172- if date then
173- self .datetree .date = date
174- return true
175- end
176- return false
177- end )
159+ return self
160+ :_compile (self .target , ' target' )
161+ :next (function (target )
162+ if not target then
163+ return nil
164+ end
165+ self .target = target
166+ return self :_compile (content or ' ' , ' content' )
167+ end )
168+ :next (function (compiled_content )
169+ if not compiled_content then
170+ return nil
171+ end
172+ return vim .split (compiled_content , ' \n ' , { plain = true })
173+ end )
178174end
179175
180176--- @return OrgCaptureTemplateDatetreeOpts
189185
190186--- @return string
191187function Template :get_target ()
192- return vim .fn .resolve (vim .fn .fnamemodify (self : _compile ( self .target , ' target ' ) , ' :p' ))
188+ return vim .fn .resolve (vim .fn .fnamemodify (self .target , ' :p' ))
193189end
194190
195191--- @param lines string[]
@@ -210,31 +206,80 @@ end
210206
211207--- @private
212208--- @param content string
213- --- @param type ' target' | ' content'
214- --- @return string
215- function Template :_compile (content , type )
209+ --- @param content_type ' target' | ' content'
210+ --- @return OrgPromise< string | nil>
211+ function Template :_compile (content , content_type )
216212 content = self :_compile_dates (content )
217- content = self :_compile_expansions (content )
218- content = self :_compile_expressions (content )
219213 content = self :_compile_prompts (content )
214+ content = self :_compile_expressions (content )
220215 if self ._compile_hooks then
221216 for _ , hook in ipairs (self ._compile_hooks ) do
222- content = hook (content , type )
217+ content = hook (content , content_type )
218+ if not content then
219+ return Promise .resolve (nil )
220+ end
223221 end
224222 end
225- return content
223+ return self :_compile_datetree (content , content_type ):next (function (compiled_content )
224+ if not compiled_content then
225+ return nil
226+ end
227+ return self :_compile_expansions (compiled_content )
228+ end )
226229end
227230
228231--- @param content string
229- --- @return string
232+ --- @param content_type ' target' | ' content'
233+ --- @return OrgPromise<string | nil>
234+ function Template :_compile_datetree (content , content_type )
235+ if
236+ not self .datetree
237+ or type (self .datetree ) ~= ' table'
238+ or not self .datetree .time_prompt
239+ or content_type ~= ' target'
240+ then
241+ return Promise .resolve (content )
242+ end
243+
244+ return Calendar .new ({ date = Date .now () }):open ():next (function (date )
245+ if date then
246+ self .datetree .date = date
247+ return content
248+ end
249+ return nil
250+ end )
251+ end
252+
253+ --- @param content string
254+ --- @return OrgPromise<string | nil>
230255function Template :_compile_expansions (content , found_expansions )
231256 found_expansions = found_expansions or expansions
257+ local promises = {}
258+ local proceed = true
232259 for expansion , compiler in pairs (found_expansions ) do
233260 if content :match (vim .pesc (expansion )) then
234- content = content :gsub (vim .pesc (expansion ), vim .pesc (compiler ()))
261+ table.insert (
262+ promises ,
263+ Promise .resolve ()
264+ :next (function ()
265+ return compiler ()
266+ end )
267+ :next (function (replacement )
268+ if not proceed or not replacement then
269+ proceed = false
270+ return
271+ end
272+ content = content :gsub (vim .pesc (expansion ), vim .pesc (replacement ))
273+ end )
274+ )
235275 end
236276 end
237- return content
277+ return Promise .all (promises ):next (function ()
278+ if not proceed then
279+ return nil
280+ end
281+ return content
282+ end )
238283end
239284
240285--- @param content string
0 commit comments