From 4b3e0a189528c284eeee519c4104b682ef80c464 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johann=20Kl=C3=A4hn?= Date: Mon, 25 Feb 2013 10:49:51 +0100 Subject: [PATCH] Add code to ask user for confirmation in vimsupport --- python/vimsupport.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/python/vimsupport.py b/python/vimsupport.py index 6760755f..75e15218 100644 --- a/python/vimsupport.py +++ b/python/vimsupport.py @@ -59,6 +59,34 @@ def PostVimMessage( 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 . Use -1 for no default. + + PresentDialog will return a 0-based index into the list + or -1 if the dialog was dismissed by using , 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 EchoLine( text ): vim.command( "echom '{0}'".format( EscapeForVim( text ) ) )