8
8
from msgpack import ExtType
9
9
10
10
from .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 )
13
13
from .tabpage import Tabpage
14
14
from .window import Window
15
15
from ..compat import IS_PYTHON3
@@ -33,7 +33,7 @@ class Nvim(object):
33
33
from a raw `Session` instance.
34
34
35
35
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
37
37
`SubClass.from_nvim(nvim)` where `SubClass` is a subclass of `Nvim`, which
38
38
is useful for having multiple `Nvim` objects that behave differently
39
39
without one affecting the other.
@@ -52,7 +52,7 @@ def from_session(cls, session):
52
52
53
53
if IS_PYTHON3 :
54
54
# decode all metadata strings for python3
55
- metadata = DecodeHook (). walk (metadata )
55
+ metadata = walk (decode_if_bytes , metadata )
56
56
57
57
types = {
58
58
metadata ['types' ]['Buffer' ]['id' ]: Buffer ,
@@ -66,10 +66,10 @@ def from_session(cls, session):
66
66
def from_nvim (cls , nvim ):
67
67
"""Create a new Nvim instance from an existing instance."""
68
68
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 )
70
70
71
71
def __init__ (self , session , channel_id , metadata , types ,
72
- decodehook = None , err_cb = None ):
72
+ decode = False , err_cb = None ):
73
73
"""Initialize a new Nvim instance. This method is module-private."""
74
74
self ._session = session
75
75
self .channel_id = channel_id
@@ -85,15 +85,17 @@ def __init__(self, session, channel_id, metadata, types,
85
85
self .current = Current (self )
86
86
self .funcs = Funcs (self )
87
87
self .error = NvimError
88
- self ._decodehook = decodehook
88
+ self ._decode = decode
89
89
self ._err_cb = err_cb
90
90
91
- def _from_nvim (self , obj ):
91
+ def _from_nvim (self , obj , decode = None ):
92
+ if decode is None :
93
+ decode = self ._decode
92
94
if type (obj ) is ExtType :
93
95
cls = self .types [obj .code ]
94
96
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 )
97
99
return obj
98
100
99
101
def _to_nvim (self , obj ):
@@ -121,9 +123,10 @@ def request(self, name, *args, **kwargs):
121
123
present and True, a asynchronous notification is sent instead. This
122
124
will never block, and the return value or error is ignored.
123
125
"""
126
+ decode = kwargs .pop ('decode' , self ._decode )
124
127
args = walk (self ._to_nvim , args )
125
128
res = self ._session .request (name , * args , ** kwargs )
126
- return walk (self ._from_nvim , res )
129
+ return walk (self ._from_nvim , res , decode = decode )
127
130
128
131
def next_message (self ):
129
132
"""Block until a message(request or notification) is available.
@@ -160,10 +163,10 @@ def stop_loop(self):
160
163
"""Stop the event loop being started with `run_loop`."""
161
164
self ._session .stop ()
162
165
163
- def with_decodehook (self , hook ):
166
+ def with_decode (self , decode = True ):
164
167
"""Initialize a new Nvim instance."""
165
168
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 )
167
170
168
171
def ui_attach (self , width , height , rgb ):
169
172
"""Register as a remote UI.
@@ -192,24 +195,20 @@ def unsubscribe(self, event):
192
195
"""Unsubscribe to a Nvim event."""
193
196
return self .request ('vim_unsubscribe' , event )
194
197
195
- def command (self , string , async = False ):
198
+ def command (self , string , ** kwargs ):
196
199
"""Execute a single ex command."""
197
- return self .request ('vim_command' , string , async = async )
200
+ return self .request ('vim_command' , string , ** kwargs )
198
201
199
202
def command_output (self , string ):
200
203
"""Execute a single ex command and return the output."""
201
204
return self .request ('vim_command_output' , string )
202
205
203
- def eval (self , string , async = False ):
206
+ def eval (self , string , ** kwargs ):
204
207
"""Evaluate a vimscript expression."""
205
- return self .request ('vim_eval' , string , async = async )
208
+ return self .request ('vim_eval' , string , ** kwargs )
206
209
207
210
def call (self , name , * args , ** kwargs ):
208
211
"""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 ))
213
212
return self .request ('vim_call_function' , name , args , ** kwargs )
214
213
215
214
def strwidth (self , string ):
@@ -285,9 +284,9 @@ def out_write(self, msg):
285
284
"""Print `msg` as a normal message."""
286
285
return self .request ('vim_out_write' , msg )
287
286
288
- def err_write (self , msg , async = False ):
287
+ def err_write (self , msg , ** kwargs ):
289
288
"""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 )
291
290
292
291
def quit (self , quit_command = 'qa!' ):
293
292
"""Send a quit command to Nvim.
0 commit comments