popup
popup
1. Introduction |popup-intro|
Window position and size |popup-position|
Closing the popup window |popup-close|
Popup buffer and window |popup-buffer|
Terminal in popup window |popup-terminal|
2. Functions |popup-functions|
Details |popup-function-details|
3. Usage |popup-usage|
popup_create() arguments |popup_create-arguments|
Popup text properties |popup-props|
Position popup with textprop |popup-textprop-pos|
Popup filter |popup-filter|
Popup callback |popup-callback|
Popup scrollbar |popup-scrollbar|
Popup mask |popup-mask|
4. Examples |popup-examples|
==============================================================================
1. Introduction *popup-intro*
We are talking about popup windows here, text that goes on top of the regular
windows and is under control of a plugin. You cannot edit the text in the
popup window like with regular windows.
The default color used is "Pmenu". If you prefer something else use the
"highlight" argument or the 'wincolor' option, e.g.: >
hi MyPopupColor ctermbg=lightblue guibg=lightblue
call setwinvar(winid, '&wincolor', 'MyPopupColor')
A popup window has a window-ID like other windows, but behaves differently.
The size can be up to the whole Vim window and it overlaps other windows.
Popup windows can also overlap each other. The "zindex" property specifies
what goes on top of what.
*E366*
The popup window contains a buffer, and that buffer is always associated with
the popup window. The window cannot be in Normal, Visual or Insert mode, it
does not get keyboard focus. You can use functions like `setbufline()` to
change the text in the buffer. There are more differences from how this
window and buffer behave compared to regular windows and buffers, see
|popup-buffer|.
If this is not what you are looking for, check out other popup functionality:
- popup menu, see |popup-menu|
- balloon, see |balloon-eval|
The height of the window is normally equal to the number of, possibly
wrapping, lines in the buffer. It can be limited with the "maxheight"
property. You can use empty lines to increase the height or the "minheight"
property.
The width of the window is normally equal to the longest visible line in the
buffer. It can be limited with the "maxwidth" property. You can use spaces
to increase the width or use the "minwidth" property.
Vim tries to show the popup in the location you specify. In some cases, e.g.
when the popup would go outside of the Vim window, it will show it somewhere
nearby. E.g. if you use `popup_atcursor()` the popup normally shows just above
the current cursor position, but if the cursor is close to the top of the Vim
window it will be placed below the cursor position.
When the screen scrolls up for output of an Ex command, popups move too, so
that they will not cover the output.
The current cursor position is displayed even when it is under a popup window.
That way you can still see where it is, even though you cannot see the text
that it is in.
Normally the plugin that created the popup window is also in charge of closing
it. If somehow a popup hangs around, you can close all of them with: >
call popup_clear(1)
Some popups, such as notifications, close after a specified time. This can be
set with the "time" property on `popup_create()`.
Otherwise, a popup can be closed by clicking on the X in the top-right corner
or by clicking anywhere inside the popup. This must be enabled with the
"close" property. It is set by default for notifications.
The window does have a cursor position, but the cursor is not displayed. In
fact, the cursor in the underlying window is displayed, as if it peeks through
the popup, so you can see where it is.
To execute a command in the context of the popup window and buffer use
`win_execute()`. Example: >
call win_execute(winid, 'syntax enable')
A special case is running a terminal in a popup window. Many rules are then
different: *E863*
- The popup window always has focus, it is not possible to switch to another
window.
- When the job ends, the popup window shows the buffer in Terminal-Normal
mode. Use `:q` to close it or use "term_finish" value "close".
- The popup window can be closed with `popup_close()`, the terminal buffer
then becomes hidden.
- It is not possible to open a second popup window with a terminal. *E861*
- The default Pmenu color is only used for the border and padding. To change
the color of the terminal itself set the Terminal highlight group before
creating the terminal. Setting 'wincolor' later can work but requires the
program in the terminal to redraw everything.
- The default minimal size is 5 lines of 20 characters; Use the "minwidth" and
"minheight" parameters to set a different value.
- The terminal size will grow if the program running in the terminal writes
text. Set "maxheight" and "maxwidth" to restrict the size.
To run a terminal in a popup window, first create the terminal hidden. Then
pass the buffer number to popup_create(). Example: >
hi link Terminal Search
let buf = term_start(['picker', 'Something'], #{hidden: 1, term_finish:
'close'})
let winid = popup_create(buf, #{minwidth: 50, minheight: 20})
==============================================================================
2. Functions *popup-functions*
Filter functions:
|popup_filter_menu()| select from a list of items
|popup_filter_yesno()| blocks until 'y' or 'n' is pressed
Other:
|popup_getoptions()| get current options for a popup
|popup_getpos()| get actual position and size of a popup
|popup_locate()| find popup window at a screen position
|popup_list()| get list of all popups
DETAILS *popup-function-details*
popup_findecho() *popup_findecho()*
Get the |window-ID| for the popup that shows messages for the
`:echowindow` command. Return zero if there is none.
Mainly useful to hide the popup.
popup_findinfo() *popup_findinfo()*
Get the |window-ID| for the popup info window, as it used by
the popup menu. See |complete-popup|. The info popup is
hidden when not used, it can be deleted with |popup_clear()|
and |popup_close()|. Use |popup_show()| to reposition it to
the item in the popup menu.
Returns zero if there is none.
popup_findpreview() *popup_findpreview()*
Get the |window-ID| for the popup preview window.
Return zero if there is none.
popup_getoptions({id}) *popup_getoptions()*
Return the {options} for popup {id} in a Dict.
A zero value means the option was not set. For "zindex" the
default value is returned, not zero.
"border" and "padding" are not included when all values are
zero. When all values are one then an empty list is included.
popup_getpos({id}) *popup_getpos()*
Return the position and size of popup {id}. Returns a Dict
with these entries:
col screen column of the popup, one-based
line screen line of the popup, one-based
width width of the whole popup in screen cells
height height of the whole popup in screen cells
core_col screen column of the text box
core_line screen line of the text box
core_width width of the text box in screen cells
core_height height of the text box in screen cells
firstline line of the buffer at top (1 unless scrolled)
(not the value of the "firstline" property)
lastline line of the buffer at the bottom (updated when
the popup is redrawn)
scrollbar non-zero if a scrollbar is displayed
visible one if the popup is displayed, zero if hidden
Note that these are the actual screen positions. They differ
from the values in `popup_getoptions()` for the sizing and
positioning mechanism applied.
popup_hide({id}) *popup_hide()*
If {id} is a displayed popup, hide it now. If the popup has a
filter it will not be invoked for so long as the popup is
hidden.
If window {id} does not exist nothing happens. If window {id}
exists but is not a popup window an error is given. *E993*
If popup window {id} contains a terminal an error is given.
popup_list() *popup_list()*
Return a List with the |window-ID| of all existing popups.
popup_show({id}) *popup_show()*
If {id} is a hidden popup, show it now.
For {id} see `popup_hide()`.
If {id} is the info popup it will be positioned next to the
current popup menu item.
==============================================================================
3. Usage *popup-usage*
If you want to create a new buffer yourself use |bufadd()| and pass the buffer
number to popup_create().
Depending on the "zindex" the popup goes under or above other popups. The
completion menu (|popup-menu|) has zindex 100. For messages that occur for a
short time the suggestion is to use zindex 1000.
By default text wraps, which causes a line in {lines} to occupy more than one
screen line. When "wrap" is FALSE then the text outside of the popup or
outside of the Vim window will not be displayed, thus truncated.
By default the popup is positioned at the corner of the text, opposite of the
"pos" specified for the popup. Thus when the popup uses "botleft", the
bottom-left corner of the popup is positioned next to the top-right corner of
the text property:
+----------+
| the text |
+----------+
just some PROPERTY as an example
Here the text property is on "PROPERTY". Move the popup to the left by
passing a negative "col" value to popup_create(). With "col: -5" you get:
+----------+
| the text |
+----------+
just some PROPERTY as an example
If the text property moves out of view then the popup will be hidden.
If the window for which the popup was defined is closed, the popup is closed.
If the popup cannot fit in the desired position, it may show at a nearby
position.
Some hints:
- To avoid collision with other plugins the text property type name has to be
unique. You can also use the "bufnr" item to make it local to a buffer.
- You can leave out the text property ID if there is only ever one text
property visible.
- The popup may be in the way of what the user is doing, making it close with
a click, as in the example above, helps for that.
- If the text property is removed the popup is closed. Use something like
this: >
call prop_remove(#{type: 'popupMarker', id: propId})
A callback that gets any typed keys while a popup is displayed. The filter is
not invoked when the popup is hidden.
The filter can return TRUE to indicate the key has been handled and is to be
discarded, or FALSE to let Vim handle the key as usual in the current state.
In case it returns FALSE and there is another popup window visible, that
filter is also called. The filter of the popup window with the highest zindex
is called first.
The filter function is called with two arguments: the ID of the popup and the
key as a string, e.g.: >
func MyFilter(winid, key)
if a:key == "\<F2>"
" do something
return 1
endif
if a:key == 'x'
call popup_close(a:winid)
return 1
endif
return 0
endfunc
< *popup-filter-mode*
The "filtermode" property can be used to specify in what mode the filter is
invoked. The default is "a": all modes. When using "nvi" Command-line mode
is not included, so that any command typed on the command line is not
filtered. However, to get to Command-line mode the filter must not consume
":". Just like it must not consume "v" to allow for entering Visual mode.
*popup-mapping*
Normally the key is what results after any mapping, since the keys pass on as
normal input if the filter does not use it. If the filter consumes all the
keys, set the "mapping" property to zero so that mappings do not get in the
way. This is default for |popup_menu()| and |popup_dialog()|.
When CTRL-C is pressed the popup is closed, the filter will not be invoked.
Keys coming from a `:normal` command do not pass through the filter. This can
be used to move the cursor in a popup where the "cursorline" option is set: >
call win_execute(winid, 'normal! 10Gzz')
Keys coming from `feedkeys()` are passed through the filter.
Note that "x" is the normal way to close a popup. You may want to use Esc,
but since many keys start with an Esc character, there may be a delay before
Vim recognizes the Esc key. If you do use Esc, it is recommended to set the
'ttimeoutlen' option to 100 and set 'timeout' and/or 'ttimeout'.
*popup-filter-errors*
If the filter function can't be called, e.g. because the name is wrong, then
the popup is closed. If the filter causes an error then it is assumed to
return zero. If this happens three times in a row the popup is closed. If
the popup gives errors fewer than 10% of the calls then it won't be closed.
The callback is invoked with two arguments: the ID of the popup window and the
result, which could be an index in the popup lines, or whatever was passed as
the second argument of `popup_close()`.
If the popup is force-closed, e.g. because the cursor moved or CTRL-C was
pressed, the number -1 is passed to the callback.
Example: >
func SelectedColor(id, result)
echo 'choice made: ' .. a:result
endfunc
If the text does not fit in the popup a scrollbar is displayed on the right of
the window. This can be disabled by setting the "scrollbar" option to zero.
When the scrollbar is displayed mouse scroll events, while the mouse pointer
is on the popup, will cause the text to scroll up or down as you would expect.
A click in the upper half of the scrollbar will scroll the text down one line.
A click in the lower half will scroll the text up one line. However, this is
limited so that the popup does not get smaller.
To minimize the text that the popup covers, parts of it can be made
transparent. This is defined by a "mask" which is a list of lists, where each
list has four numbers:
col start column, positive for counting from the left, 1 for
leftmost, negative for counting from the right, -1 for
rightmost
endcol last column, like "col"
line start line, positive for counting from the top, 1 for top,
negative for counting from the bottom, -1 for bottom
endline end line, like "line"
For example, to make the last 10 columns of the last line transparent:
[[-10, -1, -1, -1]]
*popup_dialog-example*
Prompt the user to press y/Y or n/N: >
popup_dialog('Continue? y/n', {
filter: 'popup_filter_yesno',
callback: (id, result) => {
if result == 1
echomsg "'y' or 'Y' was pressed"
else
echomsg "'y' or 'Y' was NOT pressed"
endif
},
padding: [2, 4, 2, 4],
})
<
*popup_menu-shortcut-example*
Extend popup_filter_menu() with shortcut keys: >
If the text has to be obtained asynchronously return an empty string from the
expression function and call popup_beval() once the text is available. In
this example simulated with a timer callback: >
vim:tw=78:ts=8:noet:ft=help:norl: