popup.txt 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. *popup.txt* For Vim version 9.0. Last change: 2022 Oct 07
  2. VIM REFERENCE MANUAL by Bram Moolenaar
  3. Displaying text in a floating window. *popup* *popup-window* *popupwin*
  4. 1. Introduction |popup-intro|
  5. Window position and size |popup-position|
  6. Closing the popup window |popup-close|
  7. Popup buffer and window |popup-buffer|
  8. Terminal in popup window |popup-terminal|
  9. 2. Functions |popup-functions|
  10. Details |popup-function-details|
  11. 3. Usage |popup-usage|
  12. popup_create() arguments |popup_create-arguments|
  13. Popup text properties |popup-props|
  14. Position popup with textprop |popup-textprop-pos|
  15. Popup filter |popup-filter|
  16. Popup callback |popup-callback|
  17. Popup scrollbar |popup-scrollbar|
  18. Popup mask |popup-mask|
  19. 4. Examples |popup-examples|
  20. {not available if the |+popupwin| feature was disabled at compile time}
  21. ==============================================================================
  22. 1. Introduction *popup-intro*
  23. We are talking about popup windows here, text that goes on top of the regular
  24. windows and is under control of a plugin. You cannot edit the text in the
  25. popup window like with regular windows.
  26. A popup window can be used for such things as:
  27. - briefly show a message without overwriting the command line
  28. - prompt the user with a dialog
  29. - display contextual information while typing
  30. - give extra information for auto-completion
  31. The text in the popup window can be colored with |text-properties|. It is
  32. also possible to use syntax highlighting.
  33. The default color used is "Pmenu". If you prefer something else use the
  34. "highlight" argument or the 'wincolor' option, e.g.: >
  35. hi MyPopupColor ctermbg=lightblue guibg=lightblue
  36. call setwinvar(winid, '&wincolor', 'MyPopupColor')
  37. 'hlsearch' highlighting is not displayed in a popup window.
  38. A popup window has a window-ID like other windows, but behaves differently.
  39. The size can be up to the whole Vim window and it overlaps other windows.
  40. Popup windows can also overlap each other. The "zindex" property specifies
  41. what goes on top of what.
  42. *E366*
  43. The popup window contains a buffer, and that buffer is always associated with
  44. the popup window. The window cannot be in Normal, Visual or Insert mode, it
  45. does not get keyboard focus. You can use functions like `setbufline()` to
  46. change the text in the buffer. There are more differences from how this
  47. window and buffer behave compared to regular windows and buffers, see
  48. |popup-buffer|.
  49. If this is not what you are looking for, check out other popup functionality:
  50. - popup menu, see |popup-menu|
  51. - balloon, see |balloon-eval|
  52. WINDOW POSITION AND SIZE *popup-position*
  53. The height of the window is normally equal to the number of, possibly
  54. wrapping, lines in the buffer. It can be limited with the "maxheight"
  55. property. You can use empty lines to increase the height or the "minheight"
  56. property.
  57. The width of the window is normally equal to the longest visible line in the
  58. buffer. It can be limited with the "maxwidth" property. You can use spaces
  59. to increase the width or use the "minwidth" property.
  60. By default the 'wrap' option is set, so that no text disappears. Otherwise,
  61. if there is not enough space then the window is shifted left in order to
  62. display more text. When right-aligned the window is shifted right to display
  63. more text. The shifting can be disabled with the "fixed" property.
  64. Vim tries to show the popup in the location you specify. In some cases, e.g.
  65. when the popup would go outside of the Vim window, it will show it somewhere
  66. nearby. E.g. if you use `popup_atcursor()` the popup normally shows just above
  67. the current cursor position, but if the cursor is close to the top of the Vim
  68. window it will be placed below the cursor position.
  69. When the screen scrolls up for output of an Ex command, popups move too, so
  70. that they will not cover the output.
  71. The current cursor position is displayed even when it is under a popup window.
  72. That way you can still see where it is, even though you cannot see the text
  73. that it is in.
  74. CLOSING THE POPUP WINDOW *popup-close*
  75. Normally the plugin that created the popup window is also in charge of closing
  76. it. If somehow a popup hangs around, you can close all of them with: >
  77. call popup_clear(1)
  78. Some popups, such as notifications, close after a specified time. This can be
  79. set with the "time" property on `popup_create()`.
  80. Otherwise, a popup can be closed by clicking on the X in the top-right corner
  81. or by clicking anywhere inside the popup. This must be enabled with the
  82. "close" property. It is set by default for notifications.
  83. POPUP BUFFER AND WINDOW *popup-buffer*
  84. If a popup function is called to create a popup from text, a new buffer is
  85. created to hold the text and text properties of the popup window. The buffer
  86. is always associated with the popup window and manipulation is restricted:
  87. - the buffer has no name
  88. - 'buftype' is "popup"
  89. - 'swapfile' is off
  90. - 'bufhidden' is "hide"
  91. - 'buflisted' is off
  92. - 'undolevels' is -1: no undo at all
  93. - all other buffer-local and window-local options are set to their Vim default
  94. value.
  95. It is possible to change the specifically mentioned options, but anything
  96. might break then, so better leave them alone.
  97. The window does have a cursor position, but the cursor is not displayed. In
  98. fact, the cursor in the underlying window is displayed, as if it peeks through
  99. the popup, so you can see where it is.
  100. To execute a command in the context of the popup window and buffer use
  101. `win_execute()`. Example: >
  102. call win_execute(winid, 'syntax enable')
  103. Options can be set on the window with `setwinvar()`, e.g.: >
  104. call setwinvar(winid, '&wrap', 0)
  105. And options can be set on the buffer with `setbufvar()`, e.g.: >
  106. call setbufvar(winbufnr(winid), '&filetype', 'java')
  107. You can also use `win_execute()` with a ":setlocal" command.
  108. TERMINAL IN POPUP WINDOW *popup-terminal*
  109. A special case is running a terminal in a popup window. Many rules are then
  110. different: *E863*
  111. - The popup window always has focus, it is not possible to switch to another
  112. window.
  113. - When the job ends, the popup window shows the buffer in Terminal-Normal
  114. mode. Use `:q` to close it or use "term_finish" value "close".
  115. - The popup window can be closed with `popup_close()`, the terminal buffer
  116. then becomes hidden.
  117. - It is not possible to open a second popup window with a terminal. *E861*
  118. - The default Pmenu color is only used for the border and padding. To change
  119. the color of the terminal itself set the Terminal highlight group before
  120. creating the terminal. Setting 'wincolor' later can work but requires the
  121. program in the terminal to redraw everything.
  122. - The default minimal size is 5 lines of 20 characters; Use the "minwidth" and
  123. "minheight" parameters to set a different value.
  124. - The terminal size will grow if the program running in the terminal writes
  125. text. Set "maxheight" and "maxwidth" to restrict the size.
  126. To run a terminal in a popup window, first create the terminal hidden. Then
  127. pass the buffer number to popup_create(). Example: >
  128. hi link Terminal Search
  129. let buf = term_start(['picker', 'Something'], #{hidden: 1, term_finish: 'close'})
  130. let winid = popup_create(buf, #{minwidth: 50, minheight: 20})
  131. ==============================================================================
  132. 2. Functions *popup-functions*
  133. Creating a popup window:
  134. |popup_create()| centered in the screen
  135. |popup_atcursor()| just above the cursor position, closes when
  136. the cursor moves away
  137. |popup_beval()| at the position indicated by v:beval_
  138. variables, closes when the mouse moves away
  139. |popup_notification()| show a notification for three seconds
  140. |popup_dialog()| centered with padding and border
  141. |popup_menu()| prompt for selecting an item from a list
  142. Manipulating a popup window:
  143. |popup_hide()| hide a popup temporarily
  144. |popup_show()| show a previously hidden popup
  145. |popup_move()| change the position and size of a popup
  146. |popup_setoptions()| override options of a popup
  147. |popup_settext()| replace the popup buffer contents
  148. Closing popup windows:
  149. |popup_close()| close one popup
  150. |popup_clear()| close all popups
  151. Filter functions:
  152. |popup_filter_menu()| select from a list of items
  153. |popup_filter_yesno()| blocks until 'y' or 'n' is pressed
  154. Other:
  155. |popup_getoptions()| get current options for a popup
  156. |popup_getpos()| get actual position and size of a popup
  157. |popup_locate()| find popup window at a screen position
  158. |popup_list()| get list of all popups
  159. DETAILS *popup-function-details*
  160. popup_atcursor({what}, {options}) *popup_atcursor()*
  161. Show the {what} above the cursor, and close it when the cursor
  162. moves. This works like: >
  163. call popup_create({what}, #{
  164. \ pos: 'botleft',
  165. \ line: 'cursor-1',
  166. \ col: 'cursor',
  167. \ moved: 'WORD',
  168. \ })
  169. < Use {options} to change the properties.
  170. If "pos" is passed as "topleft" then the default for "line"
  171. becomes "cursor+1".
  172. Can also be used as a |method|: >
  173. GetText()->popup_atcursor({})
  174. popup_beval({what}, {options}) *popup_beval()*
  175. Show the {what} above the position from 'ballooneval' and
  176. close it when the mouse moves. This works like: >
  177. let pos = screenpos(v:beval_winnr, v:beval_lnum, v:beval_col)
  178. call popup_create({what}, #{
  179. \ pos: 'botleft',
  180. \ line: pos.row - 1,
  181. \ col: pos.col,
  182. \ mousemoved: 'WORD',
  183. \ })
  184. < Use {options} to change the properties.
  185. See |popup_beval_example| for an example.
  186. Can also be used as a |method|: >
  187. GetText()->popup_beval({})
  188. <
  189. *popup_clear()*
  190. popup_clear([{force}])
  191. Emergency solution to a misbehaving plugin: close all popup
  192. windows for the current tab and global popups.
  193. Close callbacks are not invoked.
  194. When {force} is not present this will fail if the current
  195. window is a popup.
  196. When {force} is present and |TRUE| the popup is also closed
  197. when it is the current window. If a terminal is running in a
  198. popup it is killed.
  199. popup_close({id} [, {result}]) *popup_close()*
  200. Close popup {id}. The window and the associated buffer will
  201. be deleted.
  202. If the popup has a callback it will be called just before the
  203. popup window is deleted. If the optional {result} is present
  204. it will be passed as the second argument of the callback.
  205. Otherwise zero is passed to the callback.
  206. Can also be used as a |method|: >
  207. GetPopup()->popup_close()
  208. popup_create({what}, {options}) *popup_create()*
  209. Open a popup window showing {what}, which is either: *E450*
  210. - a buffer number
  211. - a string
  212. - a list of strings
  213. - a list of text lines with text properties
  214. When {what} is not a buffer number, a buffer is created with
  215. 'buftype' set to "popup". That buffer will be wiped out once
  216. the popup closes.
  217. if {what} is a buffer number and loading the buffer runs into
  218. an existing swap file, it is silently opened read-only, as if
  219. a |SwapExists| autocommand had set |v:swapchoice| to 'o'.
  220. This is because we assume the buffer is only used for viewing.
  221. {options} is a dictionary with many possible entries.
  222. See |popup_create-arguments| for details.
  223. Returns a window-ID, which can be used with other popup
  224. functions. Use `winbufnr()` to get the number of the buffer
  225. in the window: >
  226. let winid = popup_create('hello', {})
  227. let bufnr = winbufnr(winid)
  228. call setbufline(bufnr, 2, 'second line')
  229. < In case of failure zero is returned.
  230. Can also be used as a |method|: >
  231. GetText()->popup_create({})
  232. popup_dialog({what}, {options}) *popup_dialog()*
  233. Just like |popup_create()| but with these default options: >
  234. call popup_create({what}, #{
  235. \ pos: 'center',
  236. \ zindex: 200,
  237. \ drag: 1,
  238. \ border: [],
  239. \ padding: [],
  240. \ mapping: 0,
  241. \})
  242. < Use {options} to change the properties. E.g. add a 'filter'
  243. option with value 'popup_filter_yesno'. Example: >
  244. call popup_create('do you want to quit (Yes/no)?', #{
  245. \ filter: 'popup_filter_yesno',
  246. \ callback: 'QuitCallback',
  247. \ })
  248. < By default the dialog can be dragged, so that text below it
  249. can be read if needed.
  250. Can also be used as a |method|: >
  251. GetText()->popup_dialog({})
  252. popup_filter_menu({id}, {key}) *popup_filter_menu()*
  253. Filter that can be used for a popup. These keys can be used:
  254. j <Down> <C-N> select item below
  255. k <Up> <C-P> select item above
  256. <Space> <Enter> accept current selection
  257. x Esc CTRL-C cancel the menu
  258. Other keys are ignored.
  259. Always returns |v:true|.
  260. A match is set on that line to highlight it, see
  261. |popup_menu()|.
  262. When the current selection is accepted the "callback" of the
  263. popup menu is invoked with the index of the selected line as
  264. the second argument. The first entry has index one.
  265. Cancelling the menu invokes the callback with -1.
  266. To add shortcut keys, see the example here:
  267. |popup_menu-shortcut-example|
  268. popup_filter_yesno({id}, {key}) *popup_filter_yesno()*
  269. Filter that can be used for a popup. It handles only the keys
  270. 'y', 'Y' and 'n' or 'N'. Invokes the "callback" of the
  271. popup menu with the 1 for 'y' or 'Y' and zero for 'n' or 'N'
  272. as the second argument. Pressing Esc and 'x' works like
  273. pressing 'n'. CTRL-C invokes the callback with -1. Other
  274. keys are ignored.
  275. See the example here: |popup_dialog-example|
  276. popup_findecho() *popup_findecho()*
  277. Get the |window-ID| for the popup that shows messages for the
  278. `:echowindow` command. Return zero if there is none.
  279. Mainly useful to hide the popup.
  280. popup_findinfo() *popup_findinfo()*
  281. Get the |window-ID| for the popup info window, as it used by
  282. the popup menu. See |complete-popup|. The info popup is
  283. hidden when not used, it can be deleted with |popup_clear()|
  284. and |popup_close()|. Use |popup_show()| to reposition it to
  285. the item in the popup menu.
  286. Returns zero if there is none.
  287. popup_findpreview() *popup_findpreview()*
  288. Get the |window-ID| for the popup preview window.
  289. Return zero if there is none.
  290. popup_getoptions({id}) *popup_getoptions()*
  291. Return the {options} for popup {id} in a Dict.
  292. A zero value means the option was not set. For "zindex" the
  293. default value is returned, not zero.
  294. The "moved" entry is a list with line number, minimum and
  295. maximum column, [0, 0, 0] when not set.
  296. The "mousemoved" entry is a list with screen row, minimum and
  297. maximum screen column, [0, 0, 0] when not set.
  298. "firstline" is the property set on the popup, unlike the
  299. "firstline" obtained with |popup_getpos()| which is the actual
  300. buffer line at the top of the popup window.
  301. "border" and "padding" are not included when all values are
  302. zero. When all values are one then an empty list is included.
  303. "borderhighlight" is not included when all values are empty.
  304. "scrollbarhighlight" and "thumbhighlight" are only included
  305. when set.
  306. "tabpage" will be -1 for a global popup, zero for a popup on
  307. the current tabpage and a positive number for a popup on
  308. another tabpage.
  309. "textprop", "textpropid" and "textpropwin" are only present
  310. when "textprop" was set.
  311. If popup window {id} is not found an empty Dict is returned.
  312. Can also be used as a |method|: >
  313. GetPopup()->popup_getoptions()
  314. popup_getpos({id}) *popup_getpos()*
  315. Return the position and size of popup {id}. Returns a Dict
  316. with these entries:
  317. col screen column of the popup, one-based
  318. line screen line of the popup, one-based
  319. width width of the whole popup in screen cells
  320. height height of the whole popup in screen cells
  321. core_col screen column of the text box
  322. core_line screen line of the text box
  323. core_width width of the text box in screen cells
  324. core_height height of the text box in screen cells
  325. firstline line of the buffer at top (1 unless scrolled)
  326. (not the value of the "firstline" property)
  327. lastline line of the buffer at the bottom (updated when
  328. the popup is redrawn)
  329. scrollbar non-zero if a scrollbar is displayed
  330. visible one if the popup is displayed, zero if hidden
  331. Note that these are the actual screen positions. They differ
  332. from the values in `popup_getoptions()` for the sizing and
  333. positioning mechanism applied.
  334. The "core_" values exclude the padding and border.
  335. If popup window {id} is not found an empty Dict is returned.
  336. Can also be used as a |method|: >
  337. GetPopup()->popup_getpos()
  338. popup_hide({id}) *popup_hide()*
  339. If {id} is a displayed popup, hide it now. If the popup has a
  340. filter it will not be invoked for so long as the popup is
  341. hidden.
  342. If window {id} does not exist nothing happens. If window {id}
  343. exists but is not a popup window an error is given. *E993*
  344. If popup window {id} contains a terminal an error is given.
  345. Can also be used as a |method|: >
  346. GetPopup()->popup_hide()
  347. popup_list() *popup_list()*
  348. Return a List with the |window-ID| of all existing popups.
  349. popup_locate({row}, {col}) *popup_locate()*
  350. Return the |window-ID| of the popup at screen position {row}
  351. and {col}. If there are multiple popups the one with the
  352. highest zindex is returned. If there are no popups at this
  353. position then zero is returned.
  354. popup_menu({what}, {options}) *popup_menu()*
  355. Show the {what} near the cursor, handle selecting one of the
  356. items with cursorkeys, and close it an item is selected with
  357. Space or Enter. {what} should have multiple lines to make this
  358. useful. This works like: >
  359. call popup_create({what}, #{
  360. \ pos: 'center',
  361. \ zindex: 200,
  362. \ drag: 1,
  363. \ wrap: 0,
  364. \ border: [],
  365. \ cursorline: 1,
  366. \ padding: [0,1,0,1],
  367. \ filter: 'popup_filter_menu',
  368. \ mapping: 0,
  369. \ })
  370. < The current line is highlighted with a match using
  371. "PopupSelected", or "PmenuSel" if that is not defined.
  372. Use {options} to change the properties. Should at least set
  373. "callback" to a function that handles the selected item.
  374. Example: >
  375. func ColorSelected(id, result)
  376. " use a:result
  377. endfunc
  378. call popup_menu(['red', 'green', 'blue'], #{
  379. \ callback: 'ColorSelected',
  380. \ })
  381. < Can also be used as a |method|: >
  382. GetChoices()->popup_menu({})
  383. popup_move({id}, {options}) *popup_move()*
  384. Move popup {id} to the position specified with {options}.
  385. {options} may contain the items from |popup_create()| that
  386. specify the popup position:
  387. line
  388. col
  389. pos
  390. maxheight
  391. minheight
  392. maxwidth
  393. minwidth
  394. fixed
  395. For {id} see `popup_hide()`.
  396. For other options see |popup_setoptions()|.
  397. Can also be used as a |method|: >
  398. GetPopup()->popup_move(options)
  399. popup_notification({what}, {options}) *popup_notification()*
  400. Show the {what} for 3 seconds at the top of the Vim window.
  401. This works like: >
  402. call popup_create({what}, #{
  403. \ line: 1,
  404. \ col: 10,
  405. \ minwidth: 20,
  406. \ time: 3000,
  407. \ tabpage: -1,
  408. \ zindex: 300,
  409. \ drag: 1,
  410. \ highlight: 'WarningMsg',
  411. \ border: [],
  412. \ close: 'click',
  413. \ padding: [0,1,0,1],
  414. \ })
  415. < The PopupNotification highlight group is used instead of
  416. WarningMsg if it is defined.
  417. Without the |+timers| feature the popup will not disappear
  418. automatically, the user has to click in it.
  419. The position will be adjusted to avoid overlap with other
  420. notifications.
  421. Use {options} to change the properties.
  422. Can also be used as a |method|: >
  423. GetText()->popup_notification({})
  424. popup_setoptions({id}, {options}) *popup_setoptions()*
  425. Override options in popup {id} with entries in {options}.
  426. These options can be set:
  427. border
  428. borderchars
  429. borderhighlight
  430. callback
  431. close
  432. cursorline
  433. drag
  434. filter
  435. firstline
  436. flip
  437. highlight
  438. mapping
  439. mask
  440. moved
  441. padding
  442. resize
  443. scrollbar
  444. scrollbarhighlight
  445. thumbhighlight
  446. time
  447. title
  448. wrap
  449. zindex
  450. The options from |popup_move()| can also be used.
  451. Generally, setting an option to zero or an empty string resets
  452. it to the default value, but there are exceptions.
  453. For "hidden" use |popup_hide()| and |popup_show()|.
  454. "tabpage" cannot be changed.
  455. Can also be used as a |method|: >
  456. GetPopup()->popup_setoptions(options)
  457. popup_settext({id}, {text}) *popup_settext()*
  458. Set the text of the buffer in popup win {id}. {text} is the
  459. same as supplied to |popup_create()|, except that a buffer
  460. number is not allowed.
  461. Does not change the window size or position, other than caused
  462. by the different text.
  463. Can also be used as a |method|: >
  464. GetPopup()->popup_settext('hello')
  465. popup_show({id}) *popup_show()*
  466. If {id} is a hidden popup, show it now.
  467. For {id} see `popup_hide()`.
  468. If {id} is the info popup it will be positioned next to the
  469. current popup menu item.
  470. ==============================================================================
  471. 3. Usage *popup-usage*
  472. POPUP_CREATE() ARGUMENTS *popup_create-arguments*
  473. The first argument of |popup_create()| (and the second argument to
  474. |popup_settext()|) specifies the text to be displayed, and optionally text
  475. properties. It is in one of four forms: *E1284*
  476. - a buffer number
  477. - a string
  478. - a list of strings
  479. - a list of dictionaries, where each dictionary has these entries:
  480. text String with the text to display.
  481. props A list of text properties. Optional.
  482. Each entry is a dictionary, like the third argument of
  483. |prop_add()|, but specifying the column in the
  484. dictionary with a "col" entry, see below:
  485. |popup-props|.
  486. If you want to create a new buffer yourself use |bufadd()| and pass the buffer
  487. number to popup_create().
  488. The second argument of |popup_create()| is a dictionary with options:
  489. line Screen line where to position the popup. Can use a
  490. number or "cursor", "cursor+1" or "cursor-1" to use
  491. the line of the cursor and add or subtract a number of
  492. lines. If omitted or zero the popup is vertically
  493. centered. The first line is 1.
  494. When using "textprop" the number is relative to the
  495. text property and can be negative.
  496. col Screen column where to position the popup. Can use a
  497. number or "cursor" to use the column of the cursor,
  498. "cursor+9" or "cursor-9" to add or subtract a number
  499. of columns. If omitted or zero the popup is
  500. horizontally centered. The first column is 1.
  501. When using "textprop" the number is relative to the
  502. text property and can be negative.
  503. pos "topleft", "topright", "botleft" or "botright":
  504. defines what corner of the popup "line" and "col" are
  505. used for. When not set "topleft" is used.
  506. Alternatively "center" can be used to position the
  507. popup in the center of the Vim window, in which case
  508. "line" and "col" are ignored.
  509. posinvert When FALSE the value of "pos" is always used. When
  510. TRUE (the default) and the popup does not fit
  511. vertically and there is more space on the other side
  512. then the popup is placed on the other side of the
  513. position indicated by "line".
  514. textprop When present the popup is positioned next to a text
  515. property with this name and will move when the text
  516. property moves. Use an empty string to remove. See
  517. |popup-textprop-pos|.
  518. textpropwin What window to search for the text property. When
  519. omitted or invalid the current window is used. Used
  520. when "textprop" is present.
  521. textpropid Used to identify the text property when "textprop" is
  522. present. Use zero to reset.
  523. fixed When FALSE (the default), and:
  524. - "pos" is "botleft" or "topleft", and
  525. - "wrap" is off, and
  526. - the popup would be truncated at the right edge of
  527. the screen, then
  528. the popup is moved to the left so as to fit the
  529. contents on the screen. Set to TRUE to disable this.
  530. flip When TRUE (the default) and the position is relative
  531. to the cursor, flip to below or above the cursor to
  532. avoid overlap with the |popupmenu-completion| or
  533. another popup with a higher "zindex". When there is
  534. no space above/below the cursor then show the popup to
  535. the side of the popup or popup menu.
  536. {not implemented yet}
  537. maxheight Maximum height of the contents, excluding border and
  538. padding.
  539. minheight Minimum height of the contents, excluding border and
  540. padding.
  541. maxwidth Maximum width of the contents, excluding border,
  542. padding and scrollbar.
  543. minwidth Minimum width of the contents, excluding border,
  544. padding and scrollbar.
  545. firstline First buffer line to display. When larger than one it
  546. looks like the text scrolled up. When out of range
  547. the last buffer line will at the top of the window.
  548. Set to zero to leave the position as set by commands.
  549. Also see "scrollbar".
  550. hidden When TRUE the popup exists but is not displayed; use
  551. `popup_show()` to unhide it.
  552. tabpage When -1: display the popup on all tab pages.
  553. When 0 (the default): display the popup on the current
  554. tab page.
  555. Otherwise the number of the tab page the popup is
  556. displayed on; when invalid the popup is not created
  557. and an error is given. *E997*
  558. title Text to be displayed above the first item in the
  559. popup, on top of any border. If there is no top
  560. border one line of padding is added to put the title
  561. on. You might want to add one or more spaces at the
  562. start and end as padding.
  563. wrap TRUE to make the lines wrap (default TRUE).
  564. drag TRUE to allow the popup to be dragged with the mouse
  565. by grabbing at the border. Has no effect if the
  566. popup does not have a border. As soon as dragging
  567. starts and "pos" is "center" it is changed to
  568. "topleft".
  569. dragall TRUE to allow the popup to be dragged from every
  570. position. Makes it very difficult to select text in
  571. the popup.
  572. resize TRUE to allow the popup to be resized with the mouse
  573. by grabbing at the bottom right corner. Has no effect
  574. if the popup does not have a border.
  575. close When "button" an X is displayed in the top-right, on
  576. top of any border, padding or text. When clicked on
  577. the X the popup will close. Any callback is invoked
  578. with the value -2.
  579. When "click" any mouse click in the popup will close
  580. it.
  581. When "none" (the default) mouse clicks do not close
  582. the popup window.
  583. highlight Highlight group name to use for the text, stored in
  584. the 'wincolor' option.
  585. padding List with numbers, defining the padding
  586. above/right/below/left of the popup (similar to CSS).
  587. An empty list uses a padding of 1 all around. The
  588. padding goes around the text, inside any border.
  589. Padding uses the 'wincolor' highlight.
  590. Example: [1, 2, 1, 3] has 1 line of padding above, 2
  591. columns on the right, 1 line below and 3 columns on
  592. the left.
  593. border List with numbers, defining the border thickness
  594. above/right/below/left of the popup (similar to CSS).
  595. Only values of zero and non-zero are currently
  596. recognized. An empty list uses a border all around.
  597. borderhighlight List of highlight group names to use for the border.
  598. When one entry it is used for all borders, otherwise
  599. the highlight for the top/right/bottom/left border.
  600. Example: ['TopColor', 'RightColor', 'BottomColor,
  601. 'LeftColor']
  602. borderchars List with characters, defining the character to use
  603. for the top/right/bottom/left border. Optionally
  604. followed by the character to use for the
  605. topleft/topright/botright/botleft corner.
  606. Example: ['-', '|', '-', '|', '┌', '┐', '┘', '└']
  607. When the list has one character it is used for all.
  608. When the list has two characters the first is used for
  609. the border lines, the second for the corners.
  610. By default a double line is used all around when
  611. 'encoding' is "utf-8" and 'ambiwidth' is "single",
  612. otherwise ASCII characters are used.
  613. scrollbar 1 or true: show a scrollbar when the text doesn't fit.
  614. zero: do not show a scrollbar. Default is non-zero.
  615. Also see |popup-scrollbar|.
  616. scrollbarhighlight Highlight group name for the scrollbar. The
  617. background color is what matters. When not given then
  618. PmenuSbar is used.
  619. thumbhighlight Highlight group name for the scrollbar thumb. The
  620. background color is what matters. When not given then
  621. PmenuThumb is used.
  622. zindex Priority for the popup, default 50. Minimum value is
  623. 1, maximum value is 32000.
  624. mask A list of lists with coordinates, defining parts of
  625. the popup that are transparent. See |popup-mask|.
  626. time Time in milliseconds after which the popup will close.
  627. When omitted |popup_close()| must be used.
  628. moved Specifies to close the popup if the cursor moved:
  629. - "any": if the cursor moved at all
  630. - "word": if the cursor moved outside |<cword>|
  631. - "WORD": if the cursor moved outside |<cWORD>|
  632. - "expr": if the cursor moved outside |<cexpr>|
  633. - [{start}, {end}]: if the cursor moved before column
  634. {start} or after {end}
  635. - [{lnum}, {start}, {end}]: if the cursor moved away
  636. from line {lnum}, before column {start} or after
  637. {end}
  638. - [0, 0, 0] do not close the popup when the cursor
  639. moves
  640. The popup also closes if the cursor moves to another
  641. line or to another window.
  642. mousemoved Like "moved" but referring to the mouse pointer
  643. position
  644. cursorline TRUE: Highlight the cursor line. Also scrolls the
  645. text to show this line (only works properly
  646. when 'wrap' is off).
  647. zero: Do not highlight the cursor line.
  648. Default is zero, except for |popup_menu()|.
  649. filter A callback that can filter typed characters, see
  650. |popup-filter|.
  651. mapping Allow for key mapping. When FALSE and the popup is
  652. visible and has a filter callback key mapping is
  653. disabled. Default value is TRUE.
  654. filtermode In which modes the filter is used (same flags as with
  655. |hasmapto()| plus "a"):
  656. n Normal mode
  657. v Visual and Select mode
  658. x Visual mode
  659. s Select mode
  660. o Operator-pending mode
  661. i Insert mode
  662. l Language-Argument ("r", "f", "t", etc.)
  663. c Command-line mode
  664. a all modes
  665. The default value is "a".
  666. callback A callback that is called when the popup closes, e.g.
  667. when using |popup_filter_menu()|, see |popup-callback|.
  668. Depending on the "zindex" the popup goes under or above other popups. The
  669. completion menu (|popup-menu|) has zindex 100. For messages that occur for a
  670. short time the suggestion is to use zindex 1000.
  671. By default text wraps, which causes a line in {lines} to occupy more than one
  672. screen line. When "wrap" is FALSE then the text outside of the popup or
  673. outside of the Vim window will not be displayed, thus truncated.
  674. POPUP TEXT PROPERTIES *popup-props*
  675. These are similar to the third argument of |prop_add()| except:
  676. - "lnum" is always the current line in the list
  677. - "bufnr" is always the buffer of the popup
  678. - "col" is in the Dict instead of a separate argument
  679. So we get:
  680. col starting column, counted in bytes, use one for the
  681. first column.
  682. length length of text in bytes; can be zero
  683. end_lnum line number for the end of the text
  684. end_col column just after the text; not used when "length" is
  685. present; when {col} and "end_col" are equal, this is a
  686. zero-width text property
  687. id user defined ID for the property; when omitted zero is
  688. used
  689. type name of the text property type, as added with
  690. |prop_type_add()|
  691. POSITION POPUP WITH TEXTPROP *popup-textprop-pos*
  692. Positioning a popup next to a text property causes the popup to move when text
  693. is inserted or deleted. The popup functions like a tooltip.
  694. These steps are needed to make this work:
  695. - Define a text property type, it defines the name. >
  696. call prop_type_add('popupMarker', {})
  697. - Place a text property at the desired text: >
  698. let lnum = {line of the text}
  699. let col = {start column of the text}
  700. let len = {length of the text}
  701. let propId = {arbitrary but unique number}
  702. call prop_add(lnum, col, #{
  703. \ length: len,
  704. \ type: 'popupMarker',
  705. \ id: propId,
  706. \ })
  707. - Create a popup: >
  708. let winid = popup_create('the text', #{
  709. \ pos: 'botleft',
  710. \ textprop: 'popupMarker',
  711. \ textpropid: propId,
  712. \ border: [],
  713. \ padding: [0,1,0,1],
  714. \ close: 'click',
  715. \ })
  716. By default the popup is positioned at the corner of the text, opposite of the
  717. "pos" specified for the popup. Thus when the popup uses "botleft", the
  718. bottom-left corner of the popup is positioned next to the top-right corner of
  719. the text property:
  720. +----------+
  721. | the text |
  722. +----------+
  723. just some PROPERTY as an example
  724. Here the text property is on "PROPERTY". Move the popup to the left by
  725. passing a negative "col" value to popup_create(). With "col: -5" you get:
  726. +----------+
  727. | the text |
  728. +----------+
  729. just some PROPERTY as an example
  730. If the text property moves out of view then the popup will be hidden.
  731. If the window for which the popup was defined is closed, the popup is closed.
  732. If the popup cannot fit in the desired position, it may show at a nearby
  733. position.
  734. Some hints:
  735. - To avoid collision with other plugins the text property type name has to be
  736. unique. You can also use the "bufnr" item to make it local to a buffer.
  737. - You can leave out the text property ID if there is only ever one text
  738. property visible.
  739. - The popup may be in the way of what the user is doing, making it close with
  740. a click, as in the example above, helps for that.
  741. - If the text property is removed the popup is closed. Use something like
  742. this: >
  743. call prop_remove(#{type: 'popupMarker', id: propId})
  744. POPUP FILTER *popup-filter*
  745. A callback that gets any typed keys while a popup is displayed. The filter is
  746. not invoked when the popup is hidden.
  747. The filter can return TRUE to indicate the key has been handled and is to be
  748. discarded, or FALSE to let Vim handle the key as usual in the current state.
  749. In case it returns FALSE and there is another popup window visible, that
  750. filter is also called. The filter of the popup window with the highest zindex
  751. is called first.
  752. The filter function is called with two arguments: the ID of the popup and the
  753. key as a string, e.g.: >
  754. func MyFilter(winid, key)
  755. if a:key == "\<F2>"
  756. " do something
  757. return 1
  758. endif
  759. if a:key == 'x'
  760. call popup_close(a:winid)
  761. return 1
  762. endif
  763. return 0
  764. endfunc
  765. < *popup-filter-mode*
  766. The "filtermode" property can be used to specify in what mode the filter is
  767. invoked. The default is "a": all modes. When using "nvi" Command-line mode
  768. is not included, so that any command typed on the command line is not
  769. filtered. However, to get to Command-line mode the filter must not consume
  770. ":". Just like it must not consume "v" to allow for entering Visual mode.
  771. *popup-mapping*
  772. Normally the key is what results after any mapping, since the keys pass on as
  773. normal input if the filter does not use it. If the filter consumes all the
  774. keys, set the "mapping" property to zero so that mappings do not get in the
  775. way. This is default for |popup_menu()| and |popup_dialog()|.
  776. Some recommended key actions:
  777. x close the popup (see note below)
  778. cursor keys select another entry
  779. Tab accept current suggestion
  780. When CTRL-C is pressed the popup is closed, the filter will not be invoked.
  781. A mouse click arrives as <LeftMouse>. The coordinates can be obtained with
  782. |getmousepos()|.
  783. Vim provides standard filters |popup_filter_menu()| and
  784. |popup_filter_yesno()|.
  785. Keys coming from a `:normal` command do not pass through the filter. This can
  786. be used to move the cursor in a popup where the "cursorline" option is set: >
  787. call win_execute(winid, 'normal! 10Gzz')
  788. Keys coming from `feedkeys()` are passed through the filter.
  789. Note that "x" is the normal way to close a popup. You may want to use Esc,
  790. but since many keys start with an Esc character, there may be a delay before
  791. Vim recognizes the Esc key. If you do use Esc, it is recommended to set the
  792. 'ttimeoutlen' option to 100 and set 'timeout' and/or 'ttimeout'.
  793. *popup-filter-errors*
  794. If the filter function can't be called, e.g. because the name is wrong, then
  795. the popup is closed. If the filter causes an error then it is assumed to
  796. return zero. If this happens three times in a row the popup is closed. If
  797. the popup gives errors fewer than 10% of the calls then it won't be closed.
  798. POPUP CALLBACK *popup-callback*
  799. A callback that is invoked when the popup closes.
  800. The callback is invoked with two arguments: the ID of the popup window and the
  801. result, which could be an index in the popup lines, or whatever was passed as
  802. the second argument of `popup_close()`.
  803. If the popup is force-closed, e.g. because the cursor moved or CTRL-C was
  804. pressed, the number -1 is passed to the callback.
  805. Example: >
  806. func SelectedColor(id, result)
  807. echo 'choice made: ' .. a:result
  808. endfunc
  809. POPUP SCROLLBAR *popup-scrollbar*
  810. If the text does not fit in the popup a scrollbar is displayed on the right of
  811. the window. This can be disabled by setting the "scrollbar" option to zero.
  812. When the scrollbar is displayed mouse scroll events, while the mouse pointer
  813. is on the popup, will cause the text to scroll up or down as you would expect.
  814. A click in the upper half of the scrollbar will scroll the text down one line.
  815. A click in the lower half will scroll the text up one line. However, this is
  816. limited so that the popup does not get smaller.
  817. POPUP MASK *popup-mask*
  818. To minimize the text that the popup covers, parts of it can be made
  819. transparent. This is defined by a "mask" which is a list of lists, where each
  820. list has four numbers:
  821. col start column, positive for counting from the left, 1 for
  822. leftmost, negative for counting from the right, -1 for
  823. rightmost
  824. endcol last column, like "col"
  825. line start line, positive for counting from the top, 1 for top,
  826. negative for counting from the bottom, -1 for bottom
  827. endline end line, like "line"
  828. For example, to make the last 10 columns of the last line transparent:
  829. [[-10, -1, -1, -1]]
  830. To make the four corners transparent:
  831. [[1, 1, 1, 1], [-1, -1, 1, 1], [1, 1, -1, -1], [-1, -1, -1, -1]]
  832. ==============================================================================
  833. 4. Examples *popup-examples*
  834. These examples use |Vim9| script.
  835. *popup_dialog-example*
  836. Prompt the user to press y/Y or n/N: >
  837. popup_dialog('Continue? y/n', {
  838. filter: 'popup_filter_yesno',
  839. callback: (id, result) => {
  840. if result == 1
  841. echomsg "'y' or 'Y' was pressed"
  842. else
  843. echomsg "'y' or 'Y' was NOT pressed"
  844. endif
  845. },
  846. padding: [2, 4, 2, 4],
  847. })
  848. <
  849. *popup_menu-shortcut-example*
  850. Extend popup_filter_menu() with shortcut keys: >
  851. popup_menu(['Save', 'Cancel', 'Discard'], {
  852. callback: (_, result) => {
  853. echo 'dialog result is' result
  854. },
  855. filter: (id, key) => {
  856. # Handle shortcuts
  857. if key == 'S' || key == 's'
  858. popup_close(id, 1)
  859. elseif key == 'C' || key == 'c'
  860. popup_close(id, 2)
  861. elseif key == 'D' || key == 'd'
  862. popup_close(id, 3)
  863. else
  864. # No shortcut, pass to generic filter
  865. return popup_filter_menu(id, key)
  866. endif
  867. return true
  868. },
  869. })
  870. <
  871. *popup_beval_example*
  872. Example for using a popup window for 'ballooneval': >
  873. set ballooneval balloonevalterm
  874. set balloonexpr=BalloonExpr()
  875. var winid: number
  876. var last_text: string
  877. def BalloonExpr(): string
  878. # here you would use "v:beval_text" to lookup something interesting
  879. var text = v:beval_text
  880. if winid > 0 && popup_getpos(winid) != null_dict
  881. # previous popup window still shows
  882. if text == last_text
  883. # still the same text, keep the existing popup
  884. return null_string
  885. endif
  886. popup_close(winid)
  887. endif
  888. winid = popup_beval(text, {})
  889. last_text = text
  890. return null_string
  891. enddef
  892. If the text has to be obtained asynchronously return an empty string from the
  893. expression function and call popup_beval() once the text is available. In
  894. this example simulated with a timer callback: >
  895. set ballooneval balloonevalterm
  896. set balloonexpr=BalloonExpr()
  897. var winid: number
  898. var last_text: string
  899. def BalloonExpr(): string
  900. var text = v:beval_text
  901. if winid > 0 && popup_getpos(winid) != null_dict
  902. # previous popup window still shows
  903. if text == last_text
  904. # still the same text, keep the existing popup
  905. return null_string
  906. endif
  907. popup_close(winid)
  908. endif
  909. # Simulate an asynchronous lookup that takes half a second for the
  910. # text to display.
  911. last_text = text
  912. timer_start(500, 'ShowPopup')
  913. return null_string
  914. enddef
  915. def ShowPopup(timerid: number)
  916. winid = popup_beval('Result: ' .. last_text, {})
  917. enddef
  918. <
  919. vim:tw=78:ts=8:noet:ft=help:norl: