@@ -30,6 +30,7 @@ popup._borders = {}
30
30
popup ._callback_fn = {}
31
31
32
32
-- 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.
33
34
popup ._result = {}
34
35
35
36
local function dict_default (options , key , default )
120
121
---
121
122
--- @param winnr integer window id of popup window
122
123
--- @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 )
124
125
vim .schedule (function ()
125
126
-- exit if we are in one of ignored buffers
126
127
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)
149
150
vim .api .nvim_create_autocmd (" BufEnter" , {
150
151
group = augroup ,
151
152
callback = function ()
152
- close_window (winnr , bufnrs )
153
+ close_window_for_aucmd (winnr , bufnrs )
153
154
end ,
154
155
})
155
156
@@ -158,7 +159,7 @@ local function close_window_autocmd(events, winnr, bufnrs)
158
159
group = augroup ,
159
160
buffer = bufnrs [2 ],
160
161
callback = function ()
161
- close_window (winnr )
162
+ close_window_for_aucmd (winnr )
162
163
end ,
163
164
})
164
165
end
@@ -188,8 +189,10 @@ function popup.create(what, vim_options)
188
189
bufnr = vim .api .nvim_create_buf (false , true )
189
190
assert (bufnr , " Failed to create buffer" )
190
191
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)
193
196
194
197
-- TODO: Handle list of lines
195
198
if type (what ) == " string" then
@@ -302,10 +305,9 @@ function popup.create(what, vim_options)
302
305
303
306
local win_id
304
307
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
308
309
end
310
+ win_id = vim .api .nvim_open_win (bufnr , false , win_opts )
309
311
310
312
-- Set the default result. Also serves to indicate active popups.
311
313
popup ._result [win_id ] = - 1
@@ -515,6 +517,55 @@ function popup.create(what, vim_options)
515
517
}
516
518
end
517
519
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
+
518
569
-- Move popup with window id {win_id} to the position specified with {vim_options}.
519
570
-- {vim_options} may contain the following items that determine the popup position/size:
520
571
-- - line
0 commit comments