7
7
8
8
local NuiLine = require (" nui.line" )
9
9
local NuiPopup = require (" nui.popup" )
10
- local nuiEvent = require (" nui.utils.autocmd" ).event
11
10
local Path = require (" pathlib" )
12
11
13
12
local new_timer = vim .uv .new_timer
@@ -110,7 +109,18 @@ function M:do_open_attached_files(papis_id)
110
109
end
111
110
end
112
111
113
- local popups = {}
112
+ local popup
113
+ local file_queue = {}
114
+
115
+ -- Function to be called when a popup is closed
116
+ function M :on_popup_close ()
117
+ -- If there are no more active popups, open the files in the queue
118
+ if (not popup ) and (# file_queue > 0 ) then
119
+ self :do_open_text_file (unpack (file_queue [1 ]))
120
+ table.remove (file_queue , 1 )
121
+ end
122
+ end
123
+
114
124
--- Opens a text file with neovim, asking to select one if there are multiple buf_options
115
125
--- @param papis_id string #The `papis_id` of the entry
116
126
--- @param type string #Either "note" or "info", specifying the type of file
@@ -129,6 +139,8 @@ function M:do_open_text_file(papis_id, type)
129
139
log .debug (" Opening a note" )
130
140
if entry .notes then
131
141
cmd = string.format (" edit %s" , entry .notes [1 ])
142
+ popup = nil
143
+ self :on_popup_close ()
132
144
else
133
145
local lines_text = {
134
146
{ " This entry has no notes." , " WarningMsg" },
@@ -138,8 +150,14 @@ function M:do_open_text_file(papis_id, type)
138
150
for _ , line in pairs (lines_text ) do
139
151
width = math.max (width , # line [1 ])
140
152
end
141
- local current_popup = # popups + 1
142
- popups [current_popup ] = NuiPopup ({
153
+
154
+ -- If there are active popups, add the file to the queue and return
155
+ if popup then
156
+ table.insert (file_queue , { papis_id , type })
157
+ return
158
+ end
159
+
160
+ popup = NuiPopup ({
143
161
enter = true ,
144
162
position = " 50%" ,
145
163
size = {
@@ -156,9 +174,8 @@ function M:do_open_text_file(papis_id, type)
156
174
},
157
175
})
158
176
159
- popups [current_popup ]:map (" n" , { " Y" , " y" , " <cr>" }, function (_ )
160
- popups [current_popup ]:unmount ()
161
- popups [current_popup ] = nil
177
+ popup :map (" n" , { " Y" , " y" , " <cr>" }, function (_ )
178
+ popup :unmount ()
162
179
local config = require (" papis.config" )
163
180
local create_new_note_fn = config .create_new_note_fn
164
181
local notes_name = db .config :get_conf_value (" notes_name" )
@@ -184,24 +201,21 @@ function M:do_open_text_file(papis_id, type)
184
201
end )
185
202
)
186
203
end , { noremap = true , nowait = true })
187
- popups [current_popup ]:map (" n" , { " N" , " n" , " <esc>" , " q" }, function (_ )
188
- popups [current_popup ]:unmount ()
189
- popups [current_popup ] = nil
190
- end , { noremap = true , nowait = true })
191
204
192
- popups [current_popup ]:on ({ nuiEvent .BufLeave }, function ()
193
- popups [current_popup ]:unmount ()
194
- popups [current_popup ] = nil
195
- end , { once = true })
205
+ popup :map (" n" , { " N" , " n" , " <esc>" , " q" }, function (_ )
206
+ popup :unmount ()
207
+ popup = nil
208
+ self :on_popup_close ()
209
+ end , { noremap = true , nowait = true })
196
210
197
211
for k , line in pairs (lines_text ) do
198
212
local nuiline = NuiLine ()
199
213
nuiline :append (unpack (line ))
200
- nuiline :render (popups [ current_popup ] .bufnr , - 1 , k )
214
+ nuiline :render (popup .bufnr , - 1 , k )
201
215
end
202
216
203
217
vim .schedule (function ()
204
- popups [ current_popup ] :mount ()
218
+ popup :mount ()
205
219
end )
206
220
end
207
221
elseif type == " info" then
0 commit comments