Skip to content

Commit 70dea91

Browse files
author
jghauser
committed
feat(utils): handle opening multiple notes
1 parent 3c5bb86 commit 70dea91

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

lua/papis/utils.lua

+31-17
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
local NuiLine = require("nui.line")
99
local NuiPopup = require("nui.popup")
10-
local nuiEvent = require("nui.utils.autocmd").event
1110
local Path = require("pathlib")
1211

1312
local new_timer = vim.uv.new_timer
@@ -110,7 +109,18 @@ function M:do_open_attached_files(papis_id)
110109
end
111110
end
112111

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+
114124
---Opens a text file with neovim, asking to select one if there are multiple buf_options
115125
---@param papis_id string #The `papis_id` of the entry
116126
---@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)
129139
log.debug("Opening a note")
130140
if entry.notes then
131141
cmd = string.format("edit %s", entry.notes[1])
142+
popup = nil
143+
self:on_popup_close()
132144
else
133145
local lines_text = {
134146
{ "This entry has no notes.", "WarningMsg" },
@@ -138,8 +150,14 @@ function M:do_open_text_file(papis_id, type)
138150
for _, line in pairs(lines_text) do
139151
width = math.max(width, #line[1])
140152
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({
143161
enter = true,
144162
position = "50%",
145163
size = {
@@ -156,9 +174,8 @@ function M:do_open_text_file(papis_id, type)
156174
},
157175
})
158176

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()
162179
local config = require("papis.config")
163180
local create_new_note_fn = config.create_new_note_fn
164181
local notes_name = db.config:get_conf_value("notes_name")
@@ -184,24 +201,21 @@ function M:do_open_text_file(papis_id, type)
184201
end)
185202
)
186203
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 })
191204

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 })
196210

197211
for k, line in pairs(lines_text) do
198212
local nuiline = NuiLine()
199213
nuiline:append(unpack(line))
200-
nuiline:render(popups[current_popup].bufnr, -1, k)
214+
nuiline:render(popup.bufnr, -1, k)
201215
end
202216

203217
vim.schedule(function()
204-
popups[current_popup]:mount()
218+
popup:mount()
205219
end)
206220
end
207221
elseif type == "info" then

lua/telescope/_extensions/papis/actions.lua

-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ M.open_note = function(prompt_bufnr)
9898
utils:do_open_text_file(papis_id, "note")
9999
else
100100
for _, entry in pairs(multi) do
101-
-- TODO: this only opens one note if a note needs to be created
102101
local papis_id = entry.id.papis_id
103102
utils:do_open_text_file(papis_id, "note")
104103
end

0 commit comments

Comments
 (0)