1
- *popup.txt* For Vim version 8.2. Last change: 2022 Jun 06
1
+ *popup.txt* For Vim version 8.2. Last change: 2022 Jun 16
2
2
3
3
4
4
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1001,8 +1001,6 @@ To make the four corners transparent:
1001
1001
1002
1002
These examples use | Vim9 | script.
1003
1003
1004
- TODO: more interesting examples
1005
-
1006
1004
*popup_dialog-example*
1007
1005
Prompt the user to press y/Y or n/N: >
1008
1006
@@ -1015,89 +1013,87 @@ Prompt the user to press y/Y or n/N: >
1015
1013
echomsg "'y' or 'Y' was NOT pressed"
1016
1014
endif
1017
1015
},
1016
+ padding: [2, 4, 2, 4],
1018
1017
})
1019
1018
<
1020
1019
*popup_menu-shortcut-example*
1021
1020
Extend popup_filter_menu() with shortcut keys: >
1022
1021
1023
- call popup_menu(['Save', 'Cancel', 'Discard'], #{
1024
- \ filter: 'MyMenuFilter',
1025
- \ callback: 'MyMenuHandler',
1026
- \ })
1027
-
1028
- func MyMenuFilter(id, key)
1029
- " Handle shortcuts
1030
- if a:key == 'S'
1031
- call popup_close(a:id, 1)
1032
- return 1
1033
- endif
1034
- if a:key == 'C'
1035
- call popup_close(a:id, 2)
1036
- return 1
1037
- endif
1038
- if a:key == 'D'
1039
- call popup_close(a:id, 3)
1040
- return 1
1041
- endif
1042
-
1043
- " No shortcut, pass to generic filter
1044
- return popup_filter_menu(a:id, a:key)
1045
- endfunc
1046
-
1047
- func MyMenuHandler(id, result)
1048
- echo $'Result: {a:result}'
1049
- endfunc
1022
+ popup_menu(['Save', 'Cancel', 'Discard'], {
1023
+ callback: (_, result) => {
1024
+ echo 'dialog result is' result
1025
+ },
1026
+ filter: (id, key) => {
1027
+ # Handle shortcuts
1028
+ if key == 'S' || key == 's'
1029
+ popup_close(id, 1)
1030
+ elseif key == 'C' || key == 'c'
1031
+ popup_close(id, 2)
1032
+ elseif key == 'D' || key == 'd'
1033
+ popup_close(id, 3)
1034
+ else
1035
+ # No shortcut, pass to generic filter
1036
+ return popup_filter_menu(id, key)
1037
+ endif
1038
+ return true
1039
+ },
1040
+ })
1050
1041
<
1051
1042
*popup_beval_example*
1052
1043
Example for using a popup window for 'ballooneval' : >
1053
1044
1054
1045
set ballooneval balloonevalterm
1055
1046
set balloonexpr=BalloonExpr()
1056
- let s:winid = 0
1057
- let s:last_text = ''
1058
-
1059
- func BalloonExpr()
1060
- if s:winid && popup_getpos(s:winid) != {}
1061
- " previous popup window still shows
1062
- if v:beval_text == s:last_text
1063
- " Still the same text, keep the existing popup
1064
- return ''
1047
+ var winid: number
1048
+ var last_text: string
1049
+
1050
+ def BalloonExpr(): string
1051
+ # here you would use "v:beval_text" to lookup something interesting
1052
+ var text = v:beval_text
1053
+ if winid > 0 && popup_getpos(winid) != null_dict
1054
+ # previous popup window still shows
1055
+ if text == last_text
1056
+ # still the same text, keep the existing popup
1057
+ return null_string
1058
+ endif
1059
+ popup_close(winid)
1065
1060
endif
1066
- call popup_close(s:winid)
1067
- endif
1068
- let s:winid = popup_beval(v:beval_text, #{mousemoved: 'word'})
1069
- let s:last_text = v:beval_text
1070
- return ''
1071
- endfunc
1072
- <
1061
+
1062
+ winid = popup_beval(text, {})
1063
+ last_text = text
1064
+ return null_string
1065
+ enddef
1066
+
1073
1067
If the text has to be obtained asynchronously return an empty string from the
1074
1068
expression function and call popup_beval() once the text is available. In
1075
1069
this example simulated with a timer callback: >
1076
1070
1077
1071
set ballooneval balloonevalterm
1078
1072
set balloonexpr=BalloonExpr()
1079
- let s:winid = 0
1080
- let s:balloonText = ''
1081
-
1082
- func BalloonExpr()
1083
- if s:winid && popup_getpos(s:winid) != {}
1084
- " previous popup window still shows
1085
- if v:beval_text == s:balloonText
1086
- " Still the same text, keep the existing popup
1087
- return ''
1073
+ var winid: number
1074
+ var last_text: string
1075
+
1076
+ def BalloonExpr(): string
1077
+ var text = v:beval_text
1078
+ if winid > 0 && popup_getpos(winid) != null_dict
1079
+ # previous popup window still shows
1080
+ if text == last_text
1081
+ # still the same text, keep the existing popup
1082
+ return null_string
1083
+ endif
1084
+ popup_close(winid)
1088
1085
endif
1089
- call popup_close(s:winid)
1090
- let s:winid = 0
1091
- endif
1092
- " simulate an asynchronous lookup for the text to display
1093
- let s:balloonText = v:beval_text
1094
- call timer_start(100, 'ShowPopup')
1095
- return ''
1096
- endfunc
1097
1086
1098
- func ShowPopup(id)
1099
- let s:winid = popup_beval(s:balloonText, #{mousemoved: 'word'})
1100
- endfunc
1087
+ # Simulate an asynchronous lookup that takes half a second for the
1088
+ # text to display.
1089
+ last_text = text
1090
+ timer_start(500, 'ShowPopup')
1091
+ return null_string
1092
+ enddef
1093
+
1094
+ def ShowPopup(timerid: number)
1095
+ winid = popup_beval('Result: ' .. last_text, {})
1096
+ enddef
1101
1097
<
1102
1098
1103
1099
vim:tw=78:ts=8:noet:ft=help:norl:
0 commit comments