diff --git a/docs/installation.rst b/docs/installation.rst index 01cf5ec7..e41a8ff4 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -8,15 +8,15 @@ Using pip You can install the package without being root by adding the ``--user`` flag:: - pip2 install neovim - pip3 install neovim + pip2 install --user neovim + pip3 install --user neovim .. note:: If you only use one of python2 or python3, it is enough to install that version. -If you follow Neovim master, +If you follow Neovim HEAD, make sure to upgrade the ``python-client`` when you upgrade Neovim:: pip2 install --upgrade neovim diff --git a/neovim/plugin/script_host.py b/neovim/plugin/script_host.py index 8993cb8d..3c21089e 100644 --- a/neovim/plugin/script_host.py +++ b/neovim/plugin/script_host.py @@ -45,6 +45,14 @@ def __init__(self, nvim): self.legacy_vim = LegacyVim.from_nvim(nvim) sys.modules['vim'] = self.legacy_vim + # Handle DirChanged. #296 + nvim.command( + 'autocmd DirChanged * call rpcrequest({}, "python_chdir", v:event)' + .format(nvim.channel_id), async=True) + # XXX: Avoid race condition. + # https://github.com/neovim/python-client/pull/296#issuecomment-358970531 + os.chdir(nvim.eval('getcwd()', async=False)) + def setup(self, nvim): """Setup import hooks and global streams. @@ -153,6 +161,11 @@ def python_eval(self, expr): """Handle the `pyeval` vim function.""" return eval(expr, self.module.__dict__) + @rpc_export('python_chdir', sync=True) + def python_chdir(self, args): + """Handle working directory changes.""" + os.chdir(args['cwd']) + def _set_current_range(self, start, stop): current = self.legacy_vim.current current.range = current.buffer.range(start, stop) diff --git a/test/test_vim.py b/test/test_vim.py index 40d72512..df51ba37 100644 --- a/test/test_vim.py +++ b/test/test_vim.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -import os, tempfile +import os, sys, tempfile from nose.tools import with_setup, eq_ as eq, ok_ as ok from test_common import vim, cleanup @@ -15,7 +15,7 @@ def source(code): def test_command(): fname = tempfile.mkstemp()[1] vim.command('new') - vim.command('edit %s' % fname) + vim.command('edit {}'.format(fname)) # skip the "press return" state, which does not handle deferred calls vim.input('\r') vim.command('normal itesting\npython\napi') @@ -162,3 +162,20 @@ def test_hash(): eq(d[vim.current.buffer], "alpha") vim.command('winc w') eq(d[vim.current.buffer], "beta") + + +@with_setup(setup=cleanup) +def test_cwd(): + pycmd = 'python' + if sys.version_info >= (3, 0): + pycmd = 'python3' + + vim.command('{} import os'.format(pycmd)) + cwd_before = vim.command_output('{} print(os.getcwd())'.format(pycmd)) + + vim.command('cd test') + cwd_vim = vim.command_output('pwd') + cwd_python = vim.command_output('{} print(os.getcwd())'.format(pycmd)) + eq(cwd_vim, cwd_python) + ok(cwd_python != cwd_before) +