comparison runtime/doc/popup.txt @ 29193:1e9e9d89f0ee

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