Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions autoload/vimtex/view.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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(<q-args>)

nnoremap <buffer> <plug>(vimtex-view) :VimtexView<cr>

if has('nvim')
call s:nvim_prune_servernames()
endif
endfunction

" }}}1
Expand Down Expand Up @@ -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',
Expand All @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -220,3 +217,6 @@ function! s:focus_vim() abort " {{{1
endfunction

" }}}1


let s:nvim_servernames = vimtex#cache#path('nvim_servernames.log')
35 changes: 13 additions & 22 deletions doc/vimtex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5213,30 +5213,24 @@ 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
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
Expand All @@ -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*

Expand Down Expand Up @@ -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*
Expand Down