88from msgpack import ExtType
99
1010from .buffer import Buffer
11- from .common import (DecodeHook , Remote , RemoteApi ,
12- RemoteMap , RemoteSequence , walk )
11+ from .common import (Remote , RemoteApi , RemoteMap , RemoteSequence ,
12+ decode_if_bytes , walk )
1313from .tabpage import Tabpage
1414from .window import Window
1515from ..compat import IS_PYTHON3
@@ -33,7 +33,7 @@ class Nvim(object):
3333 from a raw `Session` instance.
3434
3535 Subsequent instances for the same session can be created by calling the
36- `with_decodehook ` instance method to change the decoding behavior or
36+ `with_decode ` instance method to change the decoding behavior or
3737 `SubClass.from_nvim(nvim)` where `SubClass` is a subclass of `Nvim`, which
3838 is useful for having multiple `Nvim` objects that behave differently
3939 without one affecting the other.
@@ -52,7 +52,7 @@ def from_session(cls, session):
5252
5353 if IS_PYTHON3 :
5454 # decode all metadata strings for python3
55- metadata = DecodeHook (). walk (metadata )
55+ metadata = walk (decode_if_bytes , metadata )
5656
5757 types = {
5858 metadata ['types' ]['Buffer' ]['id' ]: Buffer ,
@@ -66,10 +66,10 @@ def from_session(cls, session):
6666 def from_nvim (cls , nvim ):
6767 """Create a new Nvim instance from an existing instance."""
6868 return cls (nvim ._session , nvim .channel_id , nvim .metadata ,
69- nvim .types , nvim ._decodehook , nvim ._err_cb )
69+ nvim .types , nvim ._decode , nvim ._err_cb )
7070
7171 def __init__ (self , session , channel_id , metadata , types ,
72- decodehook = None , err_cb = None ):
72+ decode = False , err_cb = None ):
7373 """Initialize a new Nvim instance. This method is module-private."""
7474 self ._session = session
7575 self .channel_id = channel_id
@@ -85,15 +85,17 @@ def __init__(self, session, channel_id, metadata, types,
8585 self .current = Current (self )
8686 self .funcs = Funcs (self )
8787 self .error = NvimError
88- self ._decodehook = decodehook
88+ self ._decode = decode
8989 self ._err_cb = err_cb
9090
91- def _from_nvim (self , obj ):
91+ def _from_nvim (self , obj , decode = None ):
92+ if decode is None :
93+ decode = self ._decode
9294 if type (obj ) is ExtType :
9395 cls = self .types [obj .code ]
9496 return cls (self , (obj .code , obj .data ))
95- if self . _decodehook is not None :
96- obj = self . _decodehook . decode_if_bytes (obj )
97+ if decode :
98+ obj = decode_if_bytes (obj , decode )
9799 return obj
98100
99101 def _to_nvim (self , obj ):
@@ -121,9 +123,10 @@ def request(self, name, *args, **kwargs):
121123 present and True, a asynchronous notification is sent instead. This
122124 will never block, and the return value or error is ignored.
123125 """
126+ decode = kwargs .pop ('decode' , self ._decode )
124127 args = walk (self ._to_nvim , args )
125128 res = self ._session .request (name , * args , ** kwargs )
126- return walk (self ._from_nvim , res )
129+ return walk (self ._from_nvim , res , decode = decode )
127130
128131 def next_message (self ):
129132 """Block until a message(request or notification) is available.
@@ -160,10 +163,10 @@ def stop_loop(self):
160163 """Stop the event loop being started with `run_loop`."""
161164 self ._session .stop ()
162165
163- def with_decodehook (self , hook ):
166+ def with_decode (self , decode = True ):
164167 """Initialize a new Nvim instance."""
165168 return Nvim (self ._session , self .channel_id ,
166- self .metadata , self .types , hook , self ._err_cb )
169+ self .metadata , self .types , decode , self ._err_cb )
167170
168171 def ui_attach (self , width , height , rgb ):
169172 """Register as a remote UI.
@@ -192,24 +195,20 @@ def unsubscribe(self, event):
192195 """Unsubscribe to a Nvim event."""
193196 return self .request ('vim_unsubscribe' , event )
194197
195- def command (self , string , async = False ):
198+ def command (self , string , ** kwargs ):
196199 """Execute a single ex command."""
197- return self .request ('vim_command' , string , async = async )
200+ return self .request ('vim_command' , string , ** kwargs )
198201
199202 def command_output (self , string ):
200203 """Execute a single ex command and return the output."""
201204 return self .request ('vim_command_output' , string )
202205
203- def eval (self , string , async = False ):
206+ def eval (self , string , ** kwargs ):
204207 """Evaluate a vimscript expression."""
205- return self .request ('vim_eval' , string , async = async )
208+ return self .request ('vim_eval' , string , ** kwargs )
206209
207210 def call (self , name , * args , ** kwargs ):
208211 """Call a vimscript function."""
209- for k in kwargs :
210- if k != "async" :
211- raise TypeError (
212- "call() got an unexpected keyword argument '{}'" .format (k ))
213212 return self .request ('vim_call_function' , name , args , ** kwargs )
214213
215214 def strwidth (self , string ):
@@ -285,9 +284,9 @@ def out_write(self, msg):
285284 """Print `msg` as a normal message."""
286285 return self .request ('vim_out_write' , msg )
287286
288- def err_write (self , msg , async = False ):
287+ def err_write (self , msg , ** kwargs ):
289288 """Print `msg` as an error message."""
290- return self .request ('vim_err_write' , msg , async = async )
289+ return self .request ('vim_err_write' , msg , ** kwargs )
291290
292291 def quit (self , quit_command = 'qa!' ):
293292 """Send a quit command to Nvim.
0 commit comments