@@ -21,7 +21,7 @@ local Promise = require('orgmode.utils.promise')
21
21
--- @field on_pre_refile OrgOnCaptureClose
22
22
--- @field on_post_refile OrgOnCaptureClose
23
23
--- @field on_cancel_refile OrgOnCaptureCancel
24
- --- @field _window OrgCaptureWindow
24
+ --- @field private _windows table<number , OrgCaptureWindow>
25
25
local Capture = {}
26
26
Capture .__index = Capture
27
27
@@ -34,6 +34,7 @@ function Capture:new(opts)
34
34
this .on_cancel_refile = opts .on_cancel_refile
35
35
this .templates = opts .templates or Templates :new ()
36
36
this .closing_note = this :_setup_closing_note ()
37
+ this ._windows = {}
37
38
return this
38
39
end
39
40
72
73
--- @param template OrgCaptureTemplate
73
74
--- @return OrgPromise<OrgCaptureWindow>
74
75
function Capture :open_template (template )
75
- self . _window = CaptureWindow :new ({
76
+ local window = CaptureWindow :new ({
76
77
template = template ,
77
- on_open = function ()
78
+ on_open = function (capture_window )
79
+ self ._windows [capture_window .id ] = capture_window
78
80
return self :setup_mappings ()
79
81
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 )
82
84
end ,
83
85
})
84
86
85
- return self . _window :open ()
87
+ return window :open ()
86
88
end
87
89
88
90
--- @param shortcut string
@@ -94,13 +96,13 @@ function Capture:open_template_by_shortcut(shortcut)
94
96
return self :open_template (template )
95
97
end
96
98
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 )
100
102
if not opts then
101
103
return
102
104
end
103
- if is_modified then
105
+ if capture_window : is_modified () then
104
106
local choice =
105
107
vim .fn .confirm (string.format (' Do you want to refile this to %s?' , opts .destination_file .filename ), ' &Yes\n &No' )
106
108
vim .cmd ([[ redraw!]] )
@@ -112,15 +114,15 @@ function Capture:on_refile_close()
112
114
end
113
115
end
114
116
115
- vim .defer_fn (function ()
117
+ vim .schedule (function ()
116
118
self :_refile_from_capture_buffer (opts )
117
- self ._window = nil
118
- end , 0 )
119
+ end )
119
120
end
120
121
121
122
--- Triggered when refiling from capture buffer
122
123
function 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 )
124
126
if not opts then
125
127
return
126
128
end
@@ -132,12 +134,14 @@ end
132
134
function Capture :refile_to_destination ()
133
135
local source_file = self .files :get_current_file ()
134
136
local source_headline = source_file :get_headlines ()[1 ]
137
+ local capture_window = self ._windows [vim .b .org_capture_window_id ]
135
138
return self :get_destination ():next (function (destination )
136
139
if not destination then
137
140
return false
138
141
end
139
142
return self :_refile_from_capture_buffer ({
140
- template = self ._window .template ,
143
+ template = capture_window .template ,
144
+ capture_window = capture_window ,
141
145
source_file = source_file ,
142
146
source_headline = source_headline ,
143
147
destination_file = destination .file ,
@@ -203,7 +207,7 @@ function Capture:_refile_from_capture_buffer(opts)
203
207
self .on_post_refile (self , opts )
204
208
end
205
209
utils .echo_info ((' Wrote %s' ):format (destination_file .filename ))
206
- self :kill ()
210
+ self :kill (false , opts . capture_window . id )
207
211
return true
208
212
end
209
213
@@ -535,22 +539,25 @@ function Capture:build_note_capture(title)
535
539
end
536
540
537
541
--- @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
540
546
if from_mapping and self .on_cancel_refile then
541
547
self .on_cancel_refile (self )
542
548
end
543
- self . _window :kill ()
544
- self ._window = nil
549
+ window :kill ()
550
+ self ._windows [ window . id ] = nil
545
551
end
546
552
end
547
553
548
554
--- @private
555
+ --- @param capture_window OrgCaptureWindow
549
556
--- @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 ()) )
552
559
local source_headline = nil
553
- if not self . _window .template .whole_file then
560
+ if not capture_window .template .whole_file then
554
561
source_headline = source_file :get_headlines ()[1 ]
555
562
end
556
563
@@ -559,7 +566,8 @@ function Capture:_get_refile_vars()
559
566
source_headline = source_headline ,
560
567
destination_file = nil ,
561
568
destination_headline = nil ,
562
- template = self ._window .template ,
569
+ template = capture_window .template ,
570
+ capture_window = capture_window ,
563
571
}
564
572
565
573
if self .on_pre_refile then
0 commit comments