11"""Main Nvim interface."""
22import functools
33import os
4+ import sys
45
56from traceback import format_exc , format_stack
67
@@ -65,9 +66,10 @@ def from_session(cls, session):
6566 def from_nvim (cls , nvim ):
6667 """Create a new Nvim instance from an existing instance."""
6768 return cls (nvim ._session , nvim .channel_id , nvim .metadata ,
68- nvim .types , nvim ._decodehook )
69+ nvim .types , nvim ._decodehook , nvim . _err_cb )
6970
70- def __init__ (self , session , channel_id , metadata , types , decodehook = None ):
71+ def __init__ (self , session , channel_id , metadata , types ,
72+ decodehook = None , err_cb = None ):
7173 """Initialize a new Nvim instance. This method is module-private."""
7274 self ._session = session
7375 self .channel_id = channel_id
@@ -84,6 +86,7 @@ def __init__(self, session, channel_id, metadata, types, decodehook=None):
8486 self .funcs = Funcs (self )
8587 self .error = NvimError
8688 self ._decodehook = decodehook
89+ self ._err_cb = err_cb
8790
8891 def _from_nvim (self , obj ):
8992 if type (obj ) is ExtType :
@@ -132,7 +135,8 @@ def next_message(self):
132135 if msg :
133136 return walk (self ._from_nvim , msg )
134137
135- def run_loop (self , request_cb , notification_cb , setup_cb = None ):
138+ def run_loop (self , request_cb , notification_cb ,
139+ setup_cb = None , err_cb = None ):
136140 """Run the event loop to receive requests and notifications from Nvim.
137141
138142 This should not be called from a plugin running in the host, which
@@ -146,6 +150,10 @@ def filter_request_cb(name, args):
146150 def filter_notification_cb (name , args ):
147151 notification_cb (self ._from_nvim (name ), walk (self ._from_nvim , args ))
148152
153+ if err_cb is None :
154+ err_cb = sys .stderr .write
155+ self ._err_cb = err_cb
156+
149157 self ._session .run (filter_request_cb , filter_notification_cb , setup_cb )
150158
151159 def stop_loop (self ):
@@ -155,7 +163,7 @@ def stop_loop(self):
155163 def with_decodehook (self , hook ):
156164 """Initialize a new Nvim instance."""
157165 return Nvim (self ._session , self .channel_id ,
158- self .metadata , self .types , hook )
166+ self .metadata , self .types , hook , self . _err_cb )
159167
160168 def ui_attach (self , width , height , rgb ):
161169 """Register as a remote UI.
@@ -316,9 +324,9 @@ def handler():
316324 fn (* args , ** kwargs )
317325 except Exception as err :
318326 msg = ("error caught while executing async callback:\n "
319- "{!r}\n {}\n \n the call was requested at\n {}"
327+ "{0 !r}\n {1 }\n \n the call was requested at\n {2 }"
320328 .format (err , format_exc (5 ), call_point ))
321- self .err_write (msg , async = True )
329+ self ._err_cb (msg )
322330 raise
323331 self ._session .threadsafe_call (handler )
324332
0 commit comments