diff --git a/autoload/vimtex/view.vim b/autoload/vimtex/view.vim index 76ab78683b..aeaae9a49f 100644 --- a/autoload/vimtex/view.vim +++ b/autoload/vimtex/view.vim @@ -7,15 +7,14 @@ function! vimtex#view#init_buffer() abort " {{{1 if !g:vimtex_view_enabled | return | endif - " Store neovim servername for inheritance to inverse search - if has('nvim') && empty($NVIM_LISTEN_ADDRESS_VIMTEX) - let $NVIM_LISTEN_ADDRESS_VIMTEX = v:servername - endif - command! -buffer -nargs=? -complete=file VimtexView \ call vimtex#view#view() nnoremap (vimtex-view) :VimtexView + + if has('nvim') + call s:nvim_prune_servernames() + endif endfunction " }}}1 @@ -136,38 +135,14 @@ endfunction " }}}1 function! s:inverse_search_cmd_nvim(line, filename) abort " {{{1 - " WARNING: The following code is somewhat messy, as it mixes Python with - " Vimscript. Read with care! - if empty($NVIM_LISTEN_ADDRESS_VIMTEX) - if vimtex#util#is_win() - py3 << EOF -import psutil - -sockets = [ - p.environ()["NVIM_LISTEN_ADDRESS_VIMTEX"] - for p in psutil.process_iter(attrs=["name"]) - if ("nvim" in p.info["name"] - and "NVIM_LISTEN_ADDRESS_VIMTEX" in p.environ()) -] -EOF - else - py3 << EOF -import psutil - -sockets = [] -for proc in (p for p in psutil.process_iter(attrs=['name']) - if p.info['name'] in ('nvim', 'nvim.exe', 'nvim-qt.exe')): - sockets += [c.laddr for c in proc.connections('unix') if c.laddr] -EOF - endif - let l:socket_ids = filter(py3eval('sockets'), 'v:val != v:servername') - unsilent echo l:socket_ids - else - let l:socket_ids = [$NVIM_LISTEN_ADDRESS_VIMTEX] - endif + if !filereadable(s:nvim_servernames) | return | endif + + for l:server in readfile(s:nvim_servernames) + try + let l:socket = sockconnect('pipe', l:server, {'rpc': 1}) + catch + endtry - for l:socket_id in l:socket_ids - let l:socket = sockconnect('pipe', l:socket_id, {'rpc': 1}) call rpcnotify(l:socket, \ 'nvim_call_function', \ 'vimtex#view#inverse_search', @@ -176,7 +151,6 @@ EOF endfor endfunction -" }}}1 function! s:inverse_search_cmd_vim(line, filename) abort " {{{1 for l:server in split(serverlist(), "\n") call remote_expr(l:server, @@ -186,6 +160,29 @@ endfunction " }}}1 +function! s:nvim_prune_servernames() abort " {{{1 + " Load servernames from file + let l:servers = filereadable(s:nvim_servernames) + \ ? readfile(s:nvim_servernames) + \ : [] + + " Check which servers are available + let l:available_servernames = [] + for l:server in vimtex#util#uniq_unsorted(l:servers + [v:servername]) + try + let l:socket = sockconnect('pipe', l:server) + call add(l:available_servernames, l:server) + call chanclose(l:socket) + catch + endtry + endfor + + " Write the pruned list to file + call writefile(l:available_servernames, s:nvim_servernames) +endfunction + +" }}}1 + function! s:focus_vim() abort " {{{1 if !executable('pstree') || !executable('xdotool') | return | endif @@ -220,3 +217,6 @@ function! s:focus_vim() abort " {{{1 endfunction " }}}1 + + +let s:nvim_servernames = vimtex#cache#path('nvim_servernames.log') diff --git a/doc/vimtex.txt b/doc/vimtex.txt index 5bce02dd18..07761ffdf6 100644 --- a/doc/vimtex.txt +++ b/doc/vimtex.txt @@ -5213,6 +5213,7 @@ your vim session. See |g:vimtex_view_method| for a list of supported viewers. Inverse search~ *vimtex-synctex-inverse-search* *vimtex-synctex-backward-search* + *:VimtexInverseSearch* In supported viewers, one may set up inverse search, which allows one to go directly from a selected line in the viewer (typically by double clicking with @@ -5220,23 +5221,16 @@ the mouse or something similar) to the corresponding line inside the Vim instance. This is sometimes also called backward search or reverse search. Inverse search relies on communicating with Vim/neovim from the viewer by use -of shell commands executed by the viewer. Each Vim or neovim instance is -(normally) available for communication as a server. Vim users should be aware, -though, that one may need to ensure that the server is really running, see -|vimtex-clientserver|. - -Inverse search is set up within the specific viewer through an option named -something like "inverse search command-line". The option specifies the -necessary shell command to perform the inverse search. The target line and -file are provided as interpolation variables. In the following, we use `%l` -and `%f`, but the interpolation variables may be named different in some -viewers (e.g. |vimtex-view-skim|). - -Common configuration that one would find documented often looks like this: > +of shell commands executed by the viewer. It is usually configured within the +specific viewer through an option named something like "inverse search +command-line". The option specifies the necessary shell command to perform the +inverse search. The target line and file are provided as interpolation +variables. In the following, we use `%l` and `%f`, but the interpolation +variables may be named different in some viewers (e.g. |vimtex-view-skim|). +A typical shell command looks like this: > vim --remote-silent +%l %f -< - *:VimtexInverseSearch* + Luckily, VimTeX provides a convenience function to simplify the viewer configuration. The command `VimtexInverseSearch` will execute |vimtex#view#inverse_search| with the target line and file as arguments inside @@ -5259,6 +5253,9 @@ This may be avoided, or at least reduced, with the following variants: > cmd /c start /min "" nvim --headless -c "VimtexInverseSearch %l '%f'" +Note: Vim users should be aware that one may need to ensure that the server is + really running, see |vimtex-clientserver|. + ============================================================================== LATEX DOCUMENTATION *vimtex-latexdoc* @@ -5464,13 +5461,7 @@ A: Yes. But there are some "gotchas": ------------------------------------------------------------------------------ *vimtex-faq-neovim* Q: Does VimTeX support neovim? -A: Yes, but some people may complain that VimTeX is not written in Lua! In any - case, a couple of requirements should be mentioned: - - * |:VimtexInverseSearch| requires that neovim has a Python 3 provider - available, and the Python environment requires the `psutil` and `pynvim` - packages. See also |python-provider| for more info on how to setup neovim - with Python providers. +A: Yes, but some people may complain that VimTeX is not written in Lua! ------------------------------------------------------------------------------ *vimtex-faq-slow-matchparen*