@@ -21,7 +21,7 @@ local Promise = require('orgmode.utils.promise')
2121--- @field on_pre_refile OrgOnCaptureClose
2222--- @field on_post_refile OrgOnCaptureClose
2323--- @field on_cancel_refile OrgOnCaptureCancel
24- --- @field _window OrgCaptureWindow
24+ --- @field private _windows table<number , OrgCaptureWindow>
2525local Capture = {}
2626Capture .__index = Capture
2727
@@ -34,6 +34,7 @@ function Capture:new(opts)
3434 this .on_cancel_refile = opts .on_cancel_refile
3535 this .templates = opts .templates or Templates :new ()
3636 this .closing_note = this :_setup_closing_note ()
37+ this ._windows = {}
3738 return this
3839end
3940
7273--- @param template OrgCaptureTemplate
7374--- @return OrgPromise<OrgCaptureWindow>
7475function Capture :open_template (template )
75- self . _window = CaptureWindow :new ({
76+ local window = CaptureWindow :new ({
7677 template = template ,
77- on_open = function ()
78+ on_open = function (capture_window )
79+ self ._windows [capture_window .id ] = capture_window
7880 return self :setup_mappings ()
7981 end ,
80- on_close = function ()
81- return self :on_refile_close ()
82+ on_close = function (capture_window )
83+ return self :on_refile_close (capture_window )
8284 end ,
8385 })
8486
85- return self . _window :open ()
87+ return window :open ()
8688end
8789
8890--- @param shortcut string
@@ -94,13 +96,13 @@ function Capture:open_template_by_shortcut(shortcut)
9496 return self :open_template (template )
9597end
9698
97- function Capture : on_refile_close ()
98- local is_modified = vim . bo . modified
99- local opts = self :_get_refile_vars ()
99+ --- @param capture_window OrgCaptureWindow
100+ function Capture : on_refile_close ( capture_window )
101+ local opts = self :_get_refile_vars (capture_window )
100102 if not opts then
101103 return
102104 end
103- if is_modified then
105+ if capture_window : is_modified () then
104106 local choice =
105107 vim .fn .confirm (string.format (' Do you want to refile this to %s?' , opts .destination_file .filename ), ' &Yes\n &No' )
106108 vim .cmd ([[ redraw!]] )
@@ -112,15 +114,15 @@ function Capture:on_refile_close()
112114 end
113115 end
114116
115- vim .defer_fn (function ()
117+ vim .schedule (function ()
116118 self :_refile_from_capture_buffer (opts )
117- self ._window = nil
118- end , 0 )
119+ end )
119120end
120121
121122--- Triggered when refiling from capture buffer
122123function Capture :refile ()
123- local opts = self :_get_refile_vars ()
124+ local capture_window = self ._windows [vim .b .org_capture_window_id ]
125+ local opts = self :_get_refile_vars (capture_window )
124126 if not opts then
125127 return
126128 end
@@ -132,12 +134,14 @@ end
132134function Capture :refile_to_destination ()
133135 local source_file = self .files :get_current_file ()
134136 local source_headline = source_file :get_headlines ()[1 ]
137+ local capture_window = self ._windows [vim .b .org_capture_window_id ]
135138 return self :get_destination ():next (function (destination )
136139 if not destination then
137140 return false
138141 end
139142 return self :_refile_from_capture_buffer ({
140- template = self ._window .template ,
143+ template = capture_window .template ,
144+ capture_window = capture_window ,
141145 source_file = source_file ,
142146 source_headline = source_headline ,
143147 destination_file = destination .file ,
@@ -203,7 +207,7 @@ function Capture:_refile_from_capture_buffer(opts)
203207 self .on_post_refile (self , opts )
204208 end
205209 utils .echo_info ((' Wrote %s' ):format (destination_file .filename ))
206- self :kill ()
210+ self :kill (false , opts . capture_window . id )
207211 return true
208212end
209213
@@ -535,22 +539,25 @@ function Capture:build_note_capture(title)
535539end
536540
537541--- @param from_mapping ? boolean
538- function Capture :kill (from_mapping )
539- if self ._window then
542+ --- @param window_id ? number
543+ function Capture :kill (from_mapping , window_id )
544+ local window = self ._windows [window_id or vim .b .org_capture_window_id ]
545+ if window then
540546 if from_mapping and self .on_cancel_refile then
541547 self .on_cancel_refile (self )
542548 end
543- self . _window :kill ()
544- self ._window = nil
549+ window :kill ()
550+ self ._windows [ window . id ] = nil
545551 end
546552end
547553
548554--- @private
555+ --- @param capture_window OrgCaptureWindow
549556--- @return OrgProcessCaptureOpts | false
550- function Capture :_get_refile_vars ()
551- local source_file = self .files :get_current_file ( )
557+ function Capture :_get_refile_vars (capture_window )
558+ local source_file = self .files :get ( vim . api . nvim_buf_get_name ( capture_window : get_bufnr ()) )
552559 local source_headline = nil
553- if not self . _window .template .whole_file then
560+ if not capture_window .template .whole_file then
554561 source_headline = source_file :get_headlines ()[1 ]
555562 end
556563
@@ -559,7 +566,8 @@ function Capture:_get_refile_vars()
559566 source_headline = source_headline ,
560567 destination_file = nil ,
561568 destination_headline = nil ,
562- template = self ._window .template ,
569+ template = capture_window .template ,
570+ capture_window = capture_window ,
563571 }
564572
565573 if self .on_pre_refile then
0 commit comments