Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to SyncTeX backward search #2135

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions autoload/vimtex/options.vim
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ function! vimtex#options#init() abort " {{{1
call s:init_option('vimtex_view_method', 'general')
call s:init_option('vimtex_view_use_temp_files', 0)
call s:init_option('vimtex_view_forward_search_on_start', 1)
call s:init_option('vimtex_view_reverse_search_edit_cmd', 'edit')

" OS dependent defaults
let l:os = vimtex#util#get_os()
Expand Down
19 changes: 13 additions & 6 deletions autoload/vimtex/view.vim
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ function! vimtex#view#reverse_goto(line, filename) abort " {{{1
let l:file = resolve(a:filename)

" Open file if necessary
if !bufexists(l:file)
if !bufloaded(l:file)
if filereadable(l:file)
try
execute 'edit' l:file
execute g:vimtex_view_reverse_search_edit_cmd l:file
catch
call vimtex#log#warning("Reverse goto failed")
return
Expand All @@ -87,11 +87,18 @@ function! vimtex#view#reverse_goto(line, filename) abort " {{{1
endif

" Go to correct buffer and line

" Get buffer number
let l:bufnr = bufnr(l:file)
let l:winnr = bufwinnr(l:file)
execute l:winnr >= 0
\ ? l:winnr . 'wincmd w'
\ : 'buffer ' . l:bufnr
" Get window and tab numbers
try
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would the following code fail? During tabnext? Or wincmd?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would actually fail in line 95; if the buffer does not exist, then win_findbuf returns an empty list and the unpacking fails.

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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get why we need the second execute ... (which is the same as line 78)? Is it because of the if !bufloaded?

Copy link
Contributor Author

@psvenk psvenk Aug 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think so. I included this to replicate the behavior of the old line 94, which used buffer to load the buffer if it exists but is not loaded. With the change from bufexists to bufloaded, this would load the buffer if it is loaded but for whatever reason win_findbuf does not find it. From my limited knowledge, it seems that this should not happen, but I think I found a counterexample in my testing (though I don't remember what the situation was).

I changed buffer to the value of g:vimtex_view_reverse_search_edit_cmd to avoid usurping the current buffer in this case (if that is the user's preference). With default settings, edit is used instead of buffer and (as far as I know) the user experience is largely unchanged.

endtry

execute 'normal!' a:line . 'G'
redraw
Expand Down
2 changes: 1 addition & 1 deletion autoload/vimtex/view/zathura.vim
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,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 +\%{line} \%{input}\" \%S'
\ . ' --remote-expr \"\\\"\"vimtex#view#reverse_goto(\%{line}, ''"''"''\%{input}''"''"'')\"\\\"\"\" \%S'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hahahaha! This makes my day! :D

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the worst part of this is that it works! Great work!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I had to do quite a bit of testing with latexmk on the command line. For the record, this is what I used:

latexmk -pvc -new-viewer -pdf lectures.tex -e '$pdf_previewer = "zathura -x \"/usr/bin/vim.gtk3 --servername VIM --remote-expr \"\\\"\"vimtex#view#reverse_goto(\%{line}, '"'"'\%{input}'"'"')\"\\\"\"\" lectures.pdf"'

The output contained the command line used by latexmk to start Zathura, which I could cross-check against the use of --remote-expr already in zathura.vim:

Running 'start zathura -x "/usr/bin/vim.gtk3 --servername VIM --remote-expr "\""vimtex#view#reverse_goto(%{line}, '%{input}')"\""" lectures.pdf'

endif

let cmd = vimtex#compiler#latexmk#wrap_option('new_viewer_always', '0')
Expand Down
12 changes: 12 additions & 0 deletions doc/vimtex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2743,6 +2743,18 @@ OPTIONS *vimtex-options*

Default value: 1

*g:vimtex_view_reverse_search_edit_cmd*
When working in a multi-file project, initiating backward search
|vimtex-synctex-backward-search| may require opening a file that is not
currently open in a window. This option controls the command that is used to
open files as a result of a backward search.

Examples: `edit` (open buffer in current window); `tabedit` (open buffer in
new tab page, similar to the Vim command-line option `--remote-tab`);
`split` (split current window to open buffer, similar to `--remote`)

Default value: `edit`

*g:vimtex_view_method*
Set the viewer method.

Expand Down