Skip to content

Commit

Permalink
wip: merge reverse_goto and inverse_search
Browse files Browse the repository at this point in the history
  • Loading branch information
lervag committed Oct 25, 2021
1 parent e84c055 commit b307ce5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 75 deletions.
2 changes: 1 addition & 1 deletion autoload/health/vimtex.vim
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function! s:check_view() abort " {{{1

if executable('xdotool') && !executable('pstree')
call health#report_warn('pstree is not available',
\ 'vimtex#view#reverse_goto is better if pstree is available.')
\ 'vimtex#view#inverse_search is better if pstree is available.')
endif
endfunction

Expand Down
85 changes: 19 additions & 66 deletions autoload/vimtex/view.vim
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,23 @@ endfunction

" }}}1

function! vimtex#view#reverse_goto(line, filename) abort " {{{1
function! vimtex#view#inverse_search_comm(line, filename) abort " {{{1
try
if has('nvim')
call s:inverse_search_comm_nvim(a:line, a:filename)
else
call s:inverse_search_comm_vim(a:line, a:filename)
endif
catch
endtry
quitall!
endfunction

" }}}1
function! vimtex#view#inverse_search(line, filename) abort " {{{1
if !exists('b:vimtex') | return | endif
" If a:fliename not in b:vimtex.sources: return

if mode() ==# 'i' | stopinsert | endif

let l:file = resolve(a:filename)
Expand Down Expand Up @@ -100,28 +116,15 @@ endfunction

" }}}1

function! vimtex#view#send(line, filename) abort " {{{1
try
if has('nvim')
call vimtex#view#send_nvim(a:line, a:filename)
else
call vimtex#view#send_vim(a:line, a:filename)
endif
catch
endtry
quitall!
endfunction

" }}}1
function! vimtex#view#send_vim(line, filename) abort " {{{1
function! s:inverse_search_comm_vim(line, filename) abort " {{{1
for l:server in split(serverlist(), "\n")
call remote_expr(l:server,
\ printf("vimtex#view#receive(%d, '%s')", a:line, a:filename))
endfor
endfunction

" }}}1
function! vimtex#view#send_nvim(line, filename) abort " {{{1
function! s:inverse_search_comm_nvim(line, filename) abort " {{{1
py3 <<EOF
import psutil

Expand All @@ -143,56 +146,6 @@ endfunction

" }}}1

function! vimtex#view#receive(line, filename) abort " {{{1
if !exists('b:vimtex') | return | endif

if mode() ==# 'i' | stopinsert | endif

let l:file = resolve(a:filename)

" Open file if necessary
if !bufloaded(l:file)
if filereadable(l:file)
try
execute g:vimtex_view_reverse_search_edit_cmd l:file
catch
call vimtex#log#warning([
\ 'Reverse goto failed!',
\ printf('Command error: %s %s',
\ g:vimtex_view_reverse_search_edit_cmd, l:file)])
return
endtry
else
call vimtex#log#warning([
\ 'Reverse goto failed!',
\ printf('File not readable: "%s"', l:file)])
return
endif
endif

" Get buffer, window, and tab numbers
" * If tab/window exists, switch to it/them
let l:bufnr = bufnr(l:file)
try
let [l:winid] = win_findbuf(l:bufnr)
let [l:tabnr, l:winnr] = win_id2tabwin(l:winid)
execute l:tabnr . 'tabnext'
execute l:winnr . 'wincmd w'
catch
execute g:vimtex_view_reverse_search_edit_cmd l:file
endtry

execute 'normal!' a:line . 'G'
redraw
call s:focus_vim()

if exists('#User#VimtexEventViewReverse')
doautocmd <nomodeline> User VimtexEventViewReverse
endif
endfunction

" }}}1

function! s:focus_vim() abort " {{{1
if !executable('pstree') || !executable('xdotool') | return | endif

Expand Down
4 changes: 2 additions & 2 deletions autoload/vimtex/view/zathura.vim
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function! s:zathura.start(outfile) dict abort " {{{1
let l:cmd .= ' -x "' . g:vimtex_compiler_progname
\ . ' --servername ' . v:servername
\ . ' --remote-expr '
\ . '\"vimtex#view#reverse_goto(%{line}, ''%{input}'')\""'
\ . '\"vimtex#view#inverse_search(%{line}, ''%{input}'')\""'
if g:vimtex_view_forward_search_on_start
let l:cmd .= ' --synctex-forward '
\ . line('.')
Expand Down Expand Up @@ -83,7 +83,7 @@ function! s:zathura.latexmk_append_argument() dict abort " {{{1
if self.has_synctex
let zathura .= ' -x \"' . g:vimtex_compiler_progname
\ . ' --servername ' . v:servername
\ . ' --remote-expr \"\\\"\"vimtex#view#reverse_goto(\%{line}, ''"''"''\%{input}''"''"'')\"\\\"\"\" \%S'
\ . ' --remote-expr \"\\\"\"vimtex#view#inverse_search(\%{line}, ''"''"''\%{input}''"''"'')\"\\\"\"\" \%S'
endif

let cmd = vimtex#compiler#latexmk#wrap_option('new_viewer_always', '0')
Expand Down
12 changes: 6 additions & 6 deletions doc/vimtex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3490,7 +3490,7 @@ further customization.
been performed by the command |:VimtexView| or the related mapping.

*VimtexEventViewReverse*
This event is triggered at the end of the |vimtex#view#reverse_goto|
This event is triggered at the end of the |vimtex#view#inverse_search|
function, which can be used as the callback function for reverse goto from
a PDF viewer.

Expand Down Expand Up @@ -5085,7 +5085,7 @@ Configuration: >
let g:vimtex_view_general_viewer = 'sioyek'
let g:vimtex_view_general_options = '--reuse-instance '
\ . '--inverse-search '
\ . '"nvr --remote-expr \"vimtex#view#reverse_goto(%2, ''%1'')\"" '
\ . '"nvr --remote-expr \"vimtex#view#inverse_search(%2, ''%1'')\"" '
\ . '--forward-search-file @tex --forward-search-line @line @pdf'
let g:vimtex_view_general_options_latexmk = '--reuse-instance'
Expand Down Expand Up @@ -5256,13 +5256,13 @@ a desired shell command where the target line and file are interpolated,
usually as `%l` and `%f` (but this may be different in some viewers, e.g.
|vimtex-view-okular|). The following are some simple examples of possible
configurations for Vim and neovim (`nvr`). This also shows how one may use the
utility function |vimtex#view#reverse_goto|. >
utility function |vimtex#view#inverse_search|. >
vim --remote-silent +%l '%f'
nvr --remote-silent +%l '%f'
vim --remote-expr vimtex#view#reverse_goto(%l, '%f')
nvr --remote-expr vimtex#view#reverse_goto(%l, '%f')
vim --remote-expr vimtex#view#inverse_search(%l, '%f')
nvr --remote-expr vimtex#view#inverse_search(%l, '%f')
vim --servername MYSERVER --remote-silent +%l '%f'
nvr --servername /tmp/mynvimserver --remote-silent +%l "%f"
Expand Down Expand Up @@ -5473,7 +5473,7 @@ Note: This API is currently a work in progress!
arguments, the position refers to the cursor position. Else must be called
with two arguments: the line number and column number.

*vimtex#view#reverse_goto*
*vimtex#view#inverse_search*
Utility function for reverse search from pdf viewer. Takes two arguments:
the line number and a filename. The function runs the event
|VimtexEventViewReverse| at the end, which allows some user flexibility in
Expand Down

0 comments on commit b307ce5

Please sign in to comment.