Skip to content

Commit 3e64044

Browse files
committed
Add popup.close(), popup.list(), popup.hide(), popup.show()
1 parent 41c4d03 commit 3e64044

File tree

1 file changed

+59
-8
lines changed

1 file changed

+59
-8
lines changed

lua/plenary/popup/init.lua

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ popup._borders = {}
3030
popup._callback_fn = {}
3131

3232
-- Result is passed to the callback. Indexed by win_id. See popup_win_closed.
33+
-- Only active popups are in table; used to check if a win_id is an active popup.
3334
popup._result = {}
3435

3536
local function dict_default(options, key, default)
@@ -120,7 +121,7 @@ end
120121
---
121122
---@param winnr integer window id of popup window
122123
---@param bufnrs table|nil optional list of ignored buffers
123-
local function close_window(winnr, bufnrs)
124+
local function close_window_for_aucmd(winnr, bufnrs)
124125
vim.schedule(function()
125126
-- exit if we are in one of ignored buffers
126127
if bufnrs and vim.list_contains(bufnrs, vim.api.nvim_get_current_buf()) then
@@ -149,7 +150,7 @@ local function close_window_autocmd(events, winnr, bufnrs)
149150
vim.api.nvim_create_autocmd("BufEnter", {
150151
group = augroup,
151152
callback = function()
152-
close_window(winnr, bufnrs)
153+
close_window_for_aucmd(winnr, bufnrs)
153154
end,
154155
})
155156

@@ -158,7 +159,7 @@ local function close_window_autocmd(events, winnr, bufnrs)
158159
group = augroup,
159160
buffer = bufnrs[2],
160161
callback = function()
161-
close_window(winnr)
162+
close_window_for_aucmd(winnr)
162163
end,
163164
})
164165
end
@@ -188,8 +189,10 @@ function popup.create(what, vim_options)
188189
bufnr = vim.api.nvim_create_buf(false, true)
189190
assert(bufnr, "Failed to create buffer")
190191

191-
vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
192-
vim.api.nvim_buf_set_option(bufnr, "modifiable", true)
192+
vim.api.nvim_set_option_value("bufhidden", "wipe", {buf = bufnr})
193+
vim.api.nvim_set_option_value("modifiable", true, {buf = bufnr})
194+
-- vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
195+
-- vim.api.nvim_buf_set_option(bufnr, "modifiable", true)
193196

194197
-- TODO: Handle list of lines
195198
if type(what) == "string" then
@@ -302,10 +305,9 @@ function popup.create(what, vim_options)
302305

303306
local win_id
304307
if vim_options.hidden then
305-
assert(false, "hidden: not implemented yet and don't know how")
306-
else
307-
win_id = vim.api.nvim_open_win(bufnr, false, win_opts)
308+
win_opts.hide = vim_options.hidden
308309
end
310+
win_id = vim.api.nvim_open_win(bufnr, false, win_opts)
309311

310312
-- Set the default result. Also serves to indicate active popups.
311313
popup._result[win_id] = -1
@@ -515,6 +517,55 @@ function popup.create(what, vim_options)
515517
}
516518
end
517519

520+
--- Close the specified popup window; the "result" is available through callback.
521+
---
522+
---@param win_id integer window id of popup window
523+
---@param result any? value to return in a callback
524+
function popup.close(win_id, result)
525+
-- Only save the result if there is a popup with that window id.
526+
assert(popup._result[win_id] ~= nil, "popup.close: no such popup window")
527+
-- update the result as specified
528+
if result == nil then
529+
result = 0
530+
end
531+
popup._result[win_id] = result
532+
Window.try_close(win_id, true)
533+
end
534+
535+
--- Return a list of the window id of existing popups
536+
---
537+
---@return integer[]
538+
function popup.list()
539+
local ids = {}
540+
for k, _ in pairs(popup._result) do
541+
if type(k) == 'number' then
542+
ids[#ids+1] = k
543+
end
544+
end
545+
return ids
546+
end
547+
548+
--- Hide the popup.
549+
---
550+
---@param win_id integer window id of popup window
551+
function popup.hide(win_id)
552+
if not vim.api.nvim_win_is_valid(win_id) then
553+
return
554+
end
555+
assert(popup._result[win_id] ~= nil, "popup.hide: not a popup window")
556+
vim.api.nvim_win_set_config(win_id, { hide = true })
557+
end
558+
559+
--- Show the popup.
560+
---
561+
---@param win_id integer window id of popup window
562+
function popup.show(win_id)
563+
if not vim.api.nvim_win_is_valid(win_id) or not popup._result[win_id] then
564+
return
565+
end
566+
vim.api.nvim_win_set_config(win_id, { hide = false })
567+
end
568+
518569
-- Move popup with window id {win_id} to the position specified with {vim_options}.
519570
-- {vim_options} may contain the following items that determine the popup position/size:
520571
-- - line

0 commit comments

Comments
 (0)