From ab9241f20c301b3381ff8dc674e429f995a579f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Sat, 6 Nov 2021 14:09:20 +0100 Subject: [PATCH 1/5] feat: simplify neovim servernames for inverse search --- autoload/vimtex/view.vim | 58 +++++++++++++++------------------------- 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/autoload/vimtex/view.vim b/autoload/vimtex/view.vim index 76ab78683b..8133520977 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 writefile([v:servername], s:nvim_servernames, 'a') + endif endfunction " }}}1 @@ -136,46 +135,33 @@ 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:socket_id in l:socket_ids - let l:socket = sockconnect('pipe', l:socket_id, {'rpc': 1}) + let l:servers = readfile(s:nvim_servernames) + let l:servers_connected = [] + + for l:server in l:servers + try + let l:socket = sockconnect('pipe', l:server, {'rpc': 1}) + catch + continue + endtry + + call add(l:servers_connected, l:server) call rpcnotify(l:socket, \ 'nvim_call_function', \ 'vimtex#view#inverse_search', \ [a:line, a:filename]) call chanclose(l:socket) endfor + + if len(l:servers_connected) < len(l:servers) + call writefile(l:servers_connected, s:nvim_servernames) + endif endfunction +let s:nvim_servernames = vimtex#cache#path('nvim_servernames.log') + " }}}1 function! s:inverse_search_cmd_vim(line, filename) abort " {{{1 for l:server in split(serverlist(), "\n") From 4bd7e32f09807bb90ea7afc31aad06e988bbb154 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Sat, 6 Nov 2021 14:21:56 +0100 Subject: [PATCH 2/5] doc: simplify --- doc/vimtex.txt | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/doc/vimtex.txt b/doc/vimtex.txt index 5bce02dd18..36c39c3f76 100644 --- a/doc/vimtex.txt +++ b/doc/vimtex.txt @@ -5464,13 +5464,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* From a8f64828586d8951db00db538b07ab346e343d38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Sat, 6 Nov 2021 18:19:04 +0100 Subject: [PATCH 3/5] fix: ensure unique list --- autoload/vimtex/view.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/vimtex/view.vim b/autoload/vimtex/view.vim index 8133520977..40215aaa95 100644 --- a/autoload/vimtex/view.vim +++ b/autoload/vimtex/view.vim @@ -137,7 +137,7 @@ endfunction function! s:inverse_search_cmd_nvim(line, filename) abort " {{{1 if !filereadable(s:nvim_servernames) | return | endif - let l:servers = readfile(s:nvim_servernames) + let l:servers = vimtex#util#uniq_unsorted(readfile(s:nvim_servernames)) let l:servers_connected = [] for l:server in l:servers From d8f42329cc6e1213c7c76baa1fc0a13e559c995e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Sat, 6 Nov 2021 23:44:37 +0100 Subject: [PATCH 4/5] doc: improved documentation --- doc/vimtex.txt | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/doc/vimtex.txt b/doc/vimtex.txt index 36c39c3f76..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* From f5a74301d46a5808e20bb9089fa02b3bc5d3b73d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Sun, 7 Nov 2021 21:59:51 +0100 Subject: [PATCH 5/5] feat: prune servernames in VimTeX init refer: #2233 --- autoload/vimtex/view.vim | 42 ++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/autoload/vimtex/view.vim b/autoload/vimtex/view.vim index 40215aaa95..aeaae9a49f 100644 --- a/autoload/vimtex/view.vim +++ b/autoload/vimtex/view.vim @@ -13,7 +13,7 @@ function! vimtex#view#init_buffer() abort " {{{1 nnoremap (vimtex-view) :VimtexView if has('nvim') - call writefile([v:servername], s:nvim_servernames, 'a') + call s:nvim_prune_servernames() endif endfunction @@ -137,32 +137,20 @@ endfunction function! s:inverse_search_cmd_nvim(line, filename) abort " {{{1 if !filereadable(s:nvim_servernames) | return | endif - let l:servers = vimtex#util#uniq_unsorted(readfile(s:nvim_servernames)) - let l:servers_connected = [] - - for l:server in l:servers + for l:server in readfile(s:nvim_servernames) try let l:socket = sockconnect('pipe', l:server, {'rpc': 1}) catch - continue endtry - call add(l:servers_connected, l:server) call rpcnotify(l:socket, \ 'nvim_call_function', \ 'vimtex#view#inverse_search', \ [a:line, a:filename]) call chanclose(l:socket) endfor - - if len(l:servers_connected) < len(l:servers) - call writefile(l:servers_connected, s:nvim_servernames) - endif endfunction -let s:nvim_servernames = vimtex#cache#path('nvim_servernames.log') - -" }}}1 function! s:inverse_search_cmd_vim(line, filename) abort " {{{1 for l:server in split(serverlist(), "\n") call remote_expr(l:server, @@ -172,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 @@ -206,3 +217,6 @@ function! s:focus_vim() abort " {{{1 endfunction " }}}1 + + +let s:nvim_servernames = vimtex#cache#path('nvim_servernames.log')