Skip to content

Commit 07ae1a3

Browse files
authored
Merge pull request #1106 from tsuyoshicho/update/20220612/popup
Update popup.{txt,jax}
2 parents be1380f + 2f0b442 commit 07ae1a3

File tree

2 files changed

+151
-144
lines changed

2 files changed

+151
-144
lines changed

doc/popup.jax

Lines changed: 75 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*popup.txt* For Vim バージョン 9.0. Last change: 2022 Apr 04
1+
*popup.txt* For Vim バージョン 9.0. Last change: 2022 Jun 16
22

33

44
VIMリファレンスマニュアル by Bram Moolenaar
@@ -149,7 +149,8 @@ Exコマンドの出力のために画面がスクロールアップすると、
149149
この場合、いくつものルールが異なる: *E863*
150150
- ポップアップウィンドウは常にフォーカスを持ち、別のウィンドウに切り替えること
151151
はできない。
152-
- ジョブが終了すると、ポップアップウィンドウは閉じられる。
152+
- ジョブが終了すると、ポップアップウィンドウは端末ノーマルモードでバッファを表
153+
示する。`:q` を使って閉じるか "term_finish" の値を "close" にして使う。
153154
- ポップアップウィンドウは `popup_close()` で閉じることができ、端末バッファで
154155
あれば隠した状態になる。
155156
- 2つ目の端末のポップアップを開くことは不可能。 *E861*
@@ -985,98 +986,100 @@ Note "x" はポップアップを閉じる通常の方法である。Escを使
985986
==============================================================================
986987
4. 例 *popup-examples*
987988

988-
TODO: もっと興味深い例
989+
以下の例では |Vim9| script を使う。
990+
989991
*popup_dialog-example*
990992
ユーザーに y/Y か n/N を押すように促す: >
991993
992-
func MyDialogHandler(id, result)
993-
if a:result
994-
" ... 'y' か 'Y' が押された
995-
endif
996-
endfunc
997-
998-
call popup_dialog('Continue? y/n', #{
999-
\ filter: 'popup_filter_yesno',
1000-
\ callback: 'MyDialogHandler',
1001-
\ })
994+
popup_dialog('Continue? y/n', {
995+
filter: 'popup_filter_yesno',
996+
callback: (id, result) => {
997+
if result == 1
998+
echomsg "'y' or 'Y' was pressed"
999+
else
1000+
echomsg "'y' or 'Y' was NOT pressed"
1001+
endif
1002+
},
1003+
padding: [2, 4, 2, 4],
1004+
})
10021005
<
10031006
*popup_menu-shortcut-example*
10041007
popup_filter_menu() をショートカットで拡張できるようにする: >
10051008
1006-
call popup_menu(['Save', 'Cancel', 'Discard'], #{
1007-
\ filter: 'MyMenuFilter',
1008-
\ callback: 'MyMenuHandler',
1009-
\ })
1010-
1011-
func MyMenuFilter(id, key)
1012-
" ショートカットキーをハンドリングする
1013-
if a:key == 'S'
1014-
call popup_close(a:id, 1)
1015-
return 1
1016-
endif
1017-
if a:key == 'C'
1018-
call popup_close(a:id, 2)
1019-
return 1
1020-
endif
1021-
if a:key == 'D'
1022-
call popup_close(a:id, 3)
1023-
return 1
1024-
endif
1025-
1026-
" ショートカットキーではない場合は通常のフィルタに渡す
1027-
return popup_filter_menu(a:id, a:key)
1028-
endfunc
1009+
popup_menu(['Save', 'Cancel', 'Discard'], {
1010+
callback: (_, result) => {
1011+
echo 'dialog result is' result
1012+
},
1013+
filter: (id, key) => {
1014+
# ショートカットキーをハンドリングする
1015+
if key == 'S' || key == 's'
1016+
popup_close(id, 1)
1017+
elseif key == 'C' || key == 'c'
1018+
popup_close(id, 2)
1019+
elseif key == 'D' || key == 'd'
1020+
popup_close(id, 3)
1021+
else
1022+
# ショートカットキーではない場合は通常のフィルタに渡す
1023+
return popup_filter_menu(id, key)
1024+
endif
1025+
return true
1026+
},
1027+
})
10291028
<
10301029
*popup_beval_example*
10311030
'ballooneval' のポップアップウィンドウの使用例: >
10321031
10331032
set ballooneval balloonevalterm
10341033
set balloonexpr=BalloonExpr()
1035-
let s:winid = 0
1036-
let s:last_text = ''
1037-
1038-
func BalloonExpr()
1039-
if s:winid && popup_getpos(s:winid) != {}
1040-
" 以前のポップアップウィンドウがまだ表示されている
1041-
if v:beval_text == s:last_text
1042-
" まだ同じテキスト、既存のポップアップを保持
1043-
return ''
1034+
var winid: number
1035+
var last_text: string
1036+
1037+
def BalloonExpr(): string
1038+
# ここで "v:beval_text" を使用して、興味深いものを検索する
1039+
var text = v:beval_text
1040+
if winid > 0 && popup_getpos(winid) != null_dict
1041+
# 以前のポップアップウィンドウがまだ表示されている
1042+
if text == last_text
1043+
# まだ同じテキスト、既存のポップアップを保持
1044+
return null_string
1045+
endif
1046+
popup_close(winid)
10441047
endif
1045-
call popup_close(s:winid)
1046-
endif
1047-
let s:winid = popup_beval(v:beval_text, #{mousemoved: 'word'})
1048-
let s:last_text = v:beval_text
1049-
return ''
1050-
endfunc
1051-
<
1048+
1049+
winid = popup_beval(text, {})
1050+
last_text = text
1051+
return null_string
1052+
enddef
1053+
10521054
テキストを非同期で取得する必要がある場合は、評価関数から空文字列を返し、テキス
10531055
トが利用可能になったら、popup_beval() を呼び出す。タイマーコールバックでシミュ
10541056
レートされた例: >
10551057
10561058
set ballooneval balloonevalterm
10571059
set balloonexpr=BalloonExpr()
1058-
let s:winid = 0
1059-
let s:balloonText = ''
1060-
1061-
func BalloonExpr()
1062-
if s:winid && popup_getpos(s:winid) != {}
1063-
" 以前のポップアップウィンドウがまだ表示されている
1064-
if v:beval_text == s:balloonText
1065-
" まだ同じテキスト、既存のポップアップを保持
1066-
return ''
1060+
var winid: number
1061+
var last_text: string
1062+
1063+
def BalloonExpr(): string
1064+
var text = v:beval_text
1065+
if winid > 0 && popup_getpos(winid) != null_dict
1066+
# 以前のポップアップウィンドウがまだ表示されている
1067+
if text == last_text
1068+
# まだ同じテキスト、既存のポップアップを保持
1069+
return null_string
1070+
endif
1071+
popup_close(winid)
10671072
endif
1068-
call popup_close(s:winid)
1069-
let s:winid = 0
1070-
endif
1071-
" 表示するテキストの非同期検索をシミュレートする
1072-
let s:balloonText = v:beval_text
1073-
call timer_start(100, 'ShowPopup')
1074-
return ''
1075-
endfunc
10761073
1077-
func ShowPopup(id)
1078-
let s:winid = popup_beval(s:balloonText, #{mousemoved: 'word'})
1079-
endfunc
1074+
# テキストを表示されるまで0.5秒かかる非同期検索をシミュレートする
1075+
last_text = text
1076+
timer_start(500, 'ShowPopup')
1077+
return null_string
1078+
enddef
1079+
1080+
def ShowPopup(timerid: number)
1081+
winid = popup_beval('Result: ' .. last_text, {})
1082+
enddef
10801083
<
10811084

10821085
vim:tw=78:ts=8:noet:ft=help:norl:

en/popup.txt

Lines changed: 76 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*popup.txt* For Vim version 9.0. Last change: 2022 Apr 04
1+
*popup.txt* For Vim version 9.0. Last change: 2022 Jun 16
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -147,7 +147,8 @@ A special case is running a terminal in a popup window. Many rules are then
147147
different: *E863*
148148
- The popup window always has focus, it is not possible to switch to another
149149
window.
150-
- When the job ends, the popup window closes.
150+
- When the job ends, the popup window shows the buffer in Terminal-Normal
151+
mode. Use `:q` to close it or use "term_finish" value "close".
151152
- The popup window can be closed with `popup_close()`, the terminal buffer
152153
then becomes hidden.
153154
- It is not possible to open a second popup window with a terminal. *E861*
@@ -998,98 +999,101 @@ To make the four corners transparent:
998999
==============================================================================
9991000
4. Examples *popup-examples*
10001001

1001-
TODO: more interesting examples
1002+
These examples use |Vim9| script.
1003+
10021004
*popup_dialog-example*
10031005
Prompt the user to press y/Y or n/N: >
10041006
1005-
func MyDialogHandler(id, result)
1006-
if a:result
1007-
" ... 'y' or 'Y' was pressed
1008-
endif
1009-
endfunc
1010-
1011-
call popup_dialog('Continue? y/n', #{
1012-
\ filter: 'popup_filter_yesno',
1013-
\ callback: 'MyDialogHandler',
1014-
\ })
1007+
popup_dialog('Continue? y/n', {
1008+
filter: 'popup_filter_yesno',
1009+
callback: (id, result) => {
1010+
if result == 1
1011+
echomsg "'y' or 'Y' was pressed"
1012+
else
1013+
echomsg "'y' or 'Y' was NOT pressed"
1014+
endif
1015+
},
1016+
padding: [2, 4, 2, 4],
1017+
})
10151018
<
10161019
*popup_menu-shortcut-example*
10171020
Extend popup_filter_menu() with shortcut keys: >
10181021
1019-
call popup_menu(['Save', 'Cancel', 'Discard'], #{
1020-
\ filter: 'MyMenuFilter',
1021-
\ callback: 'MyMenuHandler',
1022-
\ })
1023-
1024-
func MyMenuFilter(id, key)
1025-
" Handle shortcuts
1026-
if a:key == 'S'
1027-
call popup_close(a:id, 1)
1028-
return 1
1029-
endif
1030-
if a:key == 'C'
1031-
call popup_close(a:id, 2)
1032-
return 1
1033-
endif
1034-
if a:key == 'D'
1035-
call popup_close(a:id, 3)
1036-
return 1
1037-
endif
1038-
1039-
" No shortcut, pass to generic filter
1040-
return popup_filter_menu(a:id, a:key)
1041-
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+
})
10421041
<
10431042
*popup_beval_example*
10441043
Example for using a popup window for 'ballooneval': >
10451044
10461045
set ballooneval balloonevalterm
10471046
set balloonexpr=BalloonExpr()
1048-
let s:winid = 0
1049-
let s:last_text = ''
1050-
1051-
func BalloonExpr()
1052-
if s:winid && popup_getpos(s:winid) != {}
1053-
" previous popup window still shows
1054-
if v:beval_text == s:last_text
1055-
" Still the same text, keep the existing popup
1056-
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)
10571060
endif
1058-
call popup_close(s:winid)
1059-
endif
1060-
let s:winid = popup_beval(v:beval_text, #{mousemoved: 'word'})
1061-
let s:last_text = v:beval_text
1062-
return ''
1063-
endfunc
1064-
<
1061+
1062+
winid = popup_beval(text, {})
1063+
last_text = text
1064+
return null_string
1065+
enddef
1066+
10651067
If the text has to be obtained asynchronously return an empty string from the
10661068
expression function and call popup_beval() once the text is available. In
10671069
this example simulated with a timer callback: >
10681070
10691071
set ballooneval balloonevalterm
10701072
set balloonexpr=BalloonExpr()
1071-
let s:winid = 0
1072-
let s:balloonText = ''
1073-
1074-
func BalloonExpr()
1075-
if s:winid && popup_getpos(s:winid) != {}
1076-
" previous popup window still shows
1077-
if v:beval_text == s:balloonText
1078-
" Still the same text, keep the existing popup
1079-
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)
10801085
endif
1081-
call popup_close(s:winid)
1082-
let s:winid = 0
1083-
endif
1084-
" simulate an asynchronous lookup for the text to display
1085-
let s:balloonText = v:beval_text
1086-
call timer_start(100, 'ShowPopup')
1087-
return ''
1088-
endfunc
10891086
1090-
func ShowPopup(id)
1091-
let s:winid = popup_beval(s:balloonText, #{mousemoved: 'word'})
1092-
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
10931097
<
10941098

10951099
vim:tw=78:ts=8:noet:ft=help:norl:

0 commit comments

Comments
 (0)