Add code to ask user for confirmation in vimsupport

This commit is contained in:
Johann Klähn 2013-02-25 10:49:51 +01:00
parent 70a2a722fe
commit 4b3e0a1895

View File

@ -59,6 +59,34 @@ def PostVimMessage( message ):
.format( EscapeForVim( message ) ) ) .format( EscapeForVim( message ) ) )
def PresentDialog( message, choices, default_choice_index = 0 ):
"""Presents the user with a dialog where a choice can be made.
This will be a dialog for gvim users or a question in the message buffer
for vim users or if `set guioptions+=c` was used.
choices is list of alternatives.
default_choice_index is the 0-based index of the default element
that will get choosen if the user hits <CR>. Use -1 for no default.
PresentDialog will return a 0-based index into the list
or -1 if the dialog was dismissed by using <Esc>, Ctrl-C, etc.
See also:
:help confirm() in vim (Note that vim uses 1-based indexes)
Example call:
PresentDialog("Is this a nice example?", ["Yes", "No", "May&be"])
Is this a nice example?
[Y]es, (N)o, May(b)e:"""
to_eval = "confirm('{0}', '{1}', {2})".format( EscapeForVim( message ),
EscapeForVim( "\n" .join( choices ) ), default_choice_index + 1 )
return int( vim.eval( to_eval ) ) - 1
def Confirm( message ):
return bool( PresentDialog( message, [ "Ok", "Cancel" ] ) == 0 )
def EchoText( text ): def EchoText( text ):
def EchoLine( text ): def EchoLine( text ):
vim.command( "echom '{0}'".format( EscapeForVim( text ) ) ) vim.command( "echom '{0}'".format( EscapeForVim( text ) ) )