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

Backward search doesn't work on neovim + Skim #2157

Closed
AlexFreik opened this issue Sep 3, 2021 · 42 comments
Closed

Backward search doesn't work on neovim + Skim #2157

AlexFreik opened this issue Sep 3, 2021 · 42 comments
Labels

Comments

@AlexFreik
Copy link

AlexFreik commented Sep 3, 2021

Description

My ~/.latexmkrc':

$pdflatex = 'pdflatex -synctex=1';

My ~/.config/nvim/init.vim (checked that the shortened version below still reproduces the problem):

call plug#begin('~/vim/plugged')
Plug 'lervag/vimtex'
    let g:vimtex_view_method='skim'
    let g:vimtex_view_skim_activate=1
    let g:vimtex_view_skim_reading_bar=1
call plug#end()

function! SetServerName()
  if has('win32')
    let nvim_server_file = $TEMP . "/curnvimserver.txt"
  else
    let nvim_server_file = "/tmp/curnvimserver.txt"
  endif
  let cmd = printf("echo %s > %s", v:servername, nvim_server_file)
  call system(cmd)
endfunction

augroup vimtex_common
    autocmd!
    autocmd FileType tex call SetServerName()
augroup END

How I fixed the problem

However, backward search works when using the solution from this article.

TL;DR:

  1. Add the following setting to nvim config.

    function! SetServerName()
      if has('win32')
        let nvim_server_file = $TEMP . "/curnvimserver.txt"
      else
        let nvim_server_file = "/tmp/curnvimserver.txt"
      endif
      let cmd = printf("echo %s > %s", v:servername, nvim_server_file)
      call system(cmd)
    endfunction
    
    augroup vimtex_common
        autocmd!
        autocmd FileType tex call SetServerName()
    augroup END
  2. In the Skim Preferences menu, select "Sync > Custom" and set

    Args: --servername `cat /tmp/curnvimserver.txt` +"%line" "%file"

    (instead of Args: --remote +"%line" "%file")

Steps to reproduce

  1. Follow the instructions from :help vimtex-faq-skimviewer
  2. Try to use Backward search (with Shift-Cmd-Click)

Expected behavior

Backward search

Actual behavior

nothing

Do you use a latexmkrc file?

Yes

VimtexInfo

System info
  OS: macOS 11.4 (20F71)
  Vim version: NVIM v0.6.0-dev+170-gdb1b0ee3b
  Has clientserver: true
  Servername: /var/folders/b6/2r1sc5kn7rz3mb9kqv9439gr0000gn/T/nvim79CnEo/0

VimTeX project: homework1_answersheet
  base: homework1_answersheet.tex
  root: /Users/alexfreik/Documents/GitHub/TUD-CSE/hw1
  tex: /Users/alexfreik/Documents/GitHub/TUD-CSE/hw1/homework1_answersheet.tex
  out: /Users/alexfreik/Documents/GitHub/TUD-CSE/hw1/homework1_answersheet.pdf
  log: /Users/alexfreik/Documents/GitHub/TUD-CSE/hw1/homework1_answersheet.log
  aux: /Users/alexfreik/Documents/GitHub/TUD-CSE/hw1/homework1_answersheet.aux
  fls: /Users/alexfreik/Documents/GitHub/TUD-CSE/hw1/homework1_answersheet.fls
  main parser: current file verified
  source files:
    homework1_answersheet.tex
    header.tex
  compiler: latexmk
    configuration: 
      continuous: 1
      callback: 1
      latexmk options:
        -verbose
        -file-line-error
        -synctex=1
        -interaction=nonstopmode
      latexmk engine: -pdf
    job: 3
    pid: 26882
    cmd: max_print_line=2000 latexmk -verbose -file-line-error -synctex=1 -interaction=nonstopmode -pdf -pvc -view=none -e '$success_cmd = ($success_cmd ? $success_cmd . " ; " : "") . "echo vimtex_compiler_callback_success"' -e '$failure_cmd = ($failure_cmd ? $failure_cmd . " ; " : "") . "echo vimtex_compiler_callback_failure"' 'homework1_answersheet.tex'
  viewer: Skim
    process: 
      pid: -
      cmd: osascript -e 'set theFile to POSIX file "/Users/alexfreik/Documents/GitHub/TUD-CSE/hw1/homework1_answersheet.pdf"' -e 'set thePath to POSIX path of (theFile as alias)' -e 'tell application "Skim"' -e 'try' -e 'set theDocs to get documents whose path is thePath' -e 'if (count of theDocs) > 0 then revert theDocs' -e 'end try' -e 'open theFile' -e 'end tell' >/dev/null 2>&1 &
  qf: LaTeX logfile
  document class: exam
  packages: amsbsy amsfonts amsgen amsmath amsopn amssymb amstext amsthm array atbegshi atbegshi-ltx atveryend atveryend-ltx auxhook babel bigintcalc bitset booktabs color datetime datetime-defaults enumerate epstopdf-base etexcmds etoolbox fcnumparser fcprefix fmtcount gettitlestring graphics graphicx hycolor hyperref iftex ifthen infwarerr intcalc keyval kvdefinekeys kvoptions kvsetkeys letltxmacro ltxcmds mathabx mathdots mathrsfs nameref pdfescape pdftexcmds pgf pgfcomp-version-0-65 pgfcomp-version-1-18 pgfcore pgffor pgfkeys pgfmath pgfplots pgfrcs pgfsys refcount rerunfilecheck tabularx tikz trig uniquecounter url xcolor xkeyval
@AlexFreik AlexFreik added the bug label Sep 3, 2021
@AlexFreik AlexFreik mentioned this issue Sep 3, 2021
@clason
Copy link
Contributor

clason commented Sep 3, 2021

Your config is not minimal ;) What happens when you remove the autocommand (and the SetServerName function)?

@AlexFreik
Copy link
Author

Your config is not minimal ;) What happens when you remove the autocommand (and the SetServerName function)?

It is still not working (and my current workaround also stops working, of course)

@clason
Copy link
Contributor

clason commented Sep 3, 2021

Weird. How did you install neovim?

@AlexFreik
Copy link
Author

Weird. How did you install neovim (and what version)?

brew install neovim --HEAD

@lervag
Copy link
Owner

lervag commented Sep 3, 2021

Notice that you are not on the latest VimTeX. It may not be crucial here, but it's best practise to ensure you're on the latest commits.

@AlexFreik
Copy link
Author

Notice that you are not on the latest VimTeX. It may not be crucial here, but it's best practise to ensure you're on the latest commits.

Thanks for advice! Updated VimTex, but still the same problem)

@lervag
Copy link
Owner

lervag commented Sep 3, 2021

Can you simplify even more, that is, use the following minimal test.vim:

set nocompatible
let &rtp = '~/.vim/plugged/vimtex,' . &rtp
let &rtp .= ',~/.vim/plugged/vimtex/after'
filetype plugin indent on
syntax enable

let g:vimtex_view_method = 'skim'

Also, could you for now remove the .latexmkrc file (I'm 99.9% sure it's not relevant, but still).

With that, you can open your TeX file with nvim -u test.vim main.tex and again reproduce your issue.

Also, please: Can you be very specific/verbose in exactly the steps you use to reproduce? Your current description is not specific. This would be an example (but it may not be exactly what you are doing):

  1. nvim -u test.vim main.tex
  2. Start compilation: \ll
  3. Observe that Skim is opened after successful compilation
  4. Now do "Shift-Cmd-Click" within Skim

Expect: Backward search

Observe: Nothing

@AlexFreik
Copy link
Author

Can you simplify even more, that is, use the following minimal test.vim:

set nocompatible
let &rtp = '~/.vim/plugged/vimtex,' . &rtp
let &rtp .= ',~/.vim/plugged/vimtex/after'
filetype plugin indent on
syntax enable

let g:vimtex_view_method = 'skim'

Also, could you for now remove the .latexmkrc file (I'm 99.9% sure it's not relevant, but still).

With that, you can open your TeX file with nvim -u test.vim main.tex and again reproduce your issue.

Also, please: Can you be very specific/verbose in exactly the steps you use to reproduce? Your current description is not specific. This would be an example (but it may not be exactly what you are doing):

  1. nvim -u test.vim main.tex
  2. Start compilation: \ll
  3. Observe that Skim is opened after successful compilation
  4. Now do "Shift-Cmd-Click" within Skim

Expect: Backward search

Observe: Nothing

I have done exactly the same you have written. (created test.vim, deleted .latexmkrc, and reproduce the steps you wrote) And still the same. Mystery.....

@clason
Copy link
Contributor

clason commented Sep 3, 2021

Now that we are grasping at straws (and since you didn't specify it): you did set the Command in the Skim Custom settings to nvr?

@AlexFreik
Copy link
Author

Now that we are grasping at straws (and since you didn't specify it): you did set the Command in the Skim Custom settings to nvr?

Screenshot 2021-09-04 at 01 13 34

@AlexFreik
Copy link
Author

Now that we are grasping at straws (and since you didn't specify it): you did set the Command in the Skim Custom settings to nvr?

I think we can wait if anyone else have this problem. If I am the only one it wouldn't matter) Thanks for your time!)

@lervag
Copy link
Owner

lervag commented Sep 4, 2021

Well, this is all very strange. As I don't use and don't know MacOS in any depth, it is hard to be of any more help; sorry. Of course, if you have a method for getting around this issue (as indicated in the original post), then that's very good. If it turns out that this is a problem for more people, then it might be useful/necessary to update the docs here.

@ThSGM
Copy link

ThSGM commented Sep 21, 2021

@AlexFreik I can confirm that the method posted by jdhao and posted here works while the one that I tried via the vimtex documentation does not.

@lervag What is the best way to test the nvr command without using Skim?

I don't really understand how

nvr --remote ...

is supposed to find the server name initiated by the nvim host?

Does it matter which terminal we use or how we launch nvim? In any case I have tested jdhao's solution using iTerm and Alacritty.

lervag added a commit that referenced this issue Sep 21, 2021
@lervag
Copy link
Owner

lervag commented Sep 21, 2021

@ThSGM nvr does not search for a server. It either uses the server specified (nvr --servername ...), or it uses the default (/tmp/nvimsocket and $NVIM_LISTEN_ADDRESS). See nvr --help. Each neovim instance has a servername that can be identified by :echo v:servername. So, for the Skim backward search to work, one needs to ensure that Skim knows the servername of the correct neovim instance.

I now realize that the docs are confusing in this sense, or perhaps even plain wrong. I've updated the docs now. I removed the faq entry and merged it with the vimtex-view-skim entry. Please let me know what you think, and feel free to propose an update with a complete setup.

@lervag
Copy link
Owner

lervag commented Oct 9, 2021

@clason I'm curios, what do you use for the backward search config in Skim? Do you use the simple nvr --remote..., or something else?

@clason
Copy link
Contributor

clason commented Oct 9, 2021

Yes, specifically

  • Preset: Custom
  • Command:nvr
  • Arguments: --remote +"%line" "%file"

Works like a charm, as long as Skim is started by VimTeX.

@lervag
Copy link
Owner

lervag commented Oct 9, 2021

Do I further understand correctly, then, that you only have a single instance of neovim open at most times you work with LaTeX? I would claim that this configuration would only work when there are not multiple neovim instances.

@clason
Copy link
Contributor

clason commented Oct 9, 2021

Yes, that's right. That's what splits were invented for, after all :)

lervag added a commit that referenced this issue Oct 9, 2021
@lervag
Copy link
Owner

lervag commented Oct 9, 2021

I partly agree; I usually avoid having multiple things going on at the same time. But I'm not at all surprised that some people are very different, and I would want VimTeX to work as expected in most cases.

I believe the current docs more or less explain things well (see :help vimtex-view-skim). I'm currently referring to @jdhao's blog post, but perhaps the configuration could be listed explicitly in the vimtex docs instead?

@clason
Copy link
Contributor

clason commented Oct 9, 2021

I agree that it's good to be explicit about this!

But is this really specific to Skim? Wouldn't all other viewers have the same problem? (Which would point towards including this in VimTeX itself -- not that you have to do this, but encouraging @AlexFreik to make a PR ;))

@lervag
Copy link
Owner

lervag commented Oct 9, 2021

Yes, that's a very good point. I would very much appreciate a PR, both for adding a more fullborne solution and for improving the docs on the matter.

lervag added a commit that referenced this issue Oct 20, 2021
@lervag
Copy link
Owner

lervag commented Oct 20, 2021

I've pushed a large doc update now. I would be very happy if someone could read the updated texts, and these in particular:

  • :help vimtex-faq-neovim
  • :help vimtex-view-skim
  • :help vimtex-view-sumatrapdf
  • :help vimtex-synctex-inverse-search

@lervag lervag closed this as completed Oct 20, 2021
@AlexFreik
Copy link
Author

I've pushed a large doc update now. I would be very happy if someone could read the updated texts, and these in particular:

  • :help vimtex-faq-neovim
  • :help vimtex-view-skim
  • :help vimtex-view-sumatrapdf
  • :help vimtex-synctex-inverse-search

Thanks, great update) I don't know that much about vimtex ecosystem, but I checked your update for inverse search, and added a few comments to the commit c3fd5fe

@lervag
Copy link
Owner

lervag commented Oct 21, 2021

Thanks, great update)

Glad you like it!

I don't know that much about vimtex ecosystem, but I checked your update for inverse search, and added a few comments to the commit ...

Thank you for the good suggestions/comments! Regarding this comment, i.e.:

..., but the previous ones (nvr --remote-silent +%line '%file', nvr --remote-expr vimtex#view#reverse_goto(%line, '%file') , nvr --listen /tmp/mynvimservername) still doesn't

I don't quite get what you mean. Can you be more specific? I.e., what does not work with these? Please notice that the "simple" solutions require either that one only has a single instance of neovim (i.e. servername is "trivial"), or that one uses the --listen servername argument and provide this specific servername to the Skim settings. Only the solution by @jdhao is more "dynamic" here.

@clason I remember someone saying it may be better to use a different option than --remote-silent, but I don't remember who. Was it you?

@AlexFreik
Copy link
Author

AlexFreik commented Oct 21, 2021

I don't quite get what you mean. Can you be more specific? I.e., what does not work with these?

I am using only one instance, but probably I don't understand how to make them work. The @jdhao 's dynamic solution automatically creates a file /tmp/curnvimserver.txt and writes v:servername in it, and then with nvr --servername 'cat /tmp/curnvimserver.txt\' +"%line" "%file" you specify it's name. And with this inverse search works for me.

But with those solutions, like nvr --remote-silent +%line '%file' just nothing happens instead of inverse search. I don't know, am I expected to create some file or specify something else myself?

@lervag
Copy link
Owner

lervag commented Oct 21, 2021

I am using only one instance, but probably I don't understand how to make them work. The @jdhao 's dynamic solution automatically creates a file /tmp/curnvimserver.txt and writes v:servername in it, and then with nvr --servername 'cat /tmp/curnvimserver.txt\' +"%line" "%file" you specify it's name. And with this inverse search works for me.

I'm glad to hear that this solution works as expected.

But with those solutions, like nvr --remote-silent +%line '%file' just nothing happens instead of inverse search. I don't know, am I expected to create some file or specify something else myself?

Ok, and in this case you:

  1. only have a single neovim instance open, and
  2. use nvr --remote-silent +%line '%file' within the Skim settings?

I believed this would work, because I thought nvr would be able to recognize the server when there was only a single one. Perhaps I'm wrong, if so, I should write it in the docs. But perhaps this is a versioning problem? Which version of nvr do you have? From my side:

❯ nvr --version
nvr 2.4.0
pynvim 0.4.3
psutil 5.8.0
Python 3.9.7 (default, Aug 31 2021, 13:28:12)

@AlexFreik
Copy link
Author

AlexFreik commented Oct 21, 2021

I believed this would work, because I thought nvr would be able to recognize the server when there was only a single one.

For some reason mine does not.

Which version of nvr do you have?

The only difference is python version. Double-checked that I have only one nvr instance.

$ nvr --version
nvr 2.4.0
pynvim 0.4.3
psutil 5.8.0
Python 3.8.8 (default, Apr 13 2021, 12:59:45) 
$ nvr --serverlist
/var/folders/b6/2r1sc5kn7rz3mb9kqv9439gr0000gn/T/nvim29zwTZ/0

@clason
Copy link
Contributor

clason commented Oct 21, 2021

@clason I remember someone saying it may be better to use a different option than --remote-silent, but I don't remember who. Was it you?

I don't think so :)

I am using only one instance, but probably I don't understand how to make them work.

Inverse search only works from the Skim instance started by Vimtex (for me) -- try closing Skim and starting via <localleader>lv.

@lervag
Copy link
Owner

lervag commented Oct 21, 2021

Inverse search only works from the Skim instance started by Vimtex (for me) -- try closing Skim and starting via <localleader>lv.

Perhaps this is the key, then. If Skim is started by neovim, then it should know the NVIM_LISTEN_ADDRESS env var. That might be the missing link here?

@AlexFreik
Copy link
Author

AlexFreik commented Oct 21, 2021

Inverse search only works from the Skim instance started by Vimtex (for me) -- try closing Skim and starting via <localleader>lv.

Hahaha =) Indeed, Skim, when started via \ll begins to work with inverse search. But, after nvim closed, and reopened the search stops working.
Still, it is nice that Jhao's solution works regardless of how Skim was started =)

@jdhao
Copy link

jdhao commented Oct 21, 2021

Inverse search is indeed tricky to get it right. The more automatic, the better, so that users do not have to fight with various issues like this.

@lervag
Copy link
Owner

lervag commented Oct 21, 2021

Ah, yes, there it is, then. And the point is still valid - care must be taken to keep the servername in sync between Skim (or any viewer) and the corresponding neovim/Vim instance.

@lervag
Copy link
Owner

lervag commented Oct 21, 2021

Inverse search is indeed tricky to get it right. The more automatic, the better, so that users do not have to fight with various issues like this.

I agree, but I'm not sure exactly how to help much more than by writing good docs. Suggestions are welcome, both on possible improvements/features and to improving the docs.

Btw, @jdhao, thanks for your very nice blog posts on this topic. They were very helpful in understanding and improving the VimTeX docs!

@AlexFreik
Copy link
Author

AlexFreik commented Oct 21, 2021

I agree, but I'm not sure exactly how to help much more than by writing good docs. Suggestions are welcome, both on possible improvements/features and to improving the docs.

Maybe it is better to make Jdhao's solution the main one for Mac/Linux (don't know about the Windows' situation)? It is not that complex to add a few lines in your init.vim, but could save time in the future and reduce issues like this one

@jdhao
Copy link

jdhao commented Oct 21, 2021

@lervag I think you may need to add a warning to the doc that skim needs to be closed before starting vimtex for LaTeX preview, if they choose to use the more manual way.

@clason
Copy link
Contributor

clason commented Oct 21, 2021

Hmm, yes. Setting the servername explicitly for Neovim is reasonable (although I'd write it to stdpath('cache') .. '/vimtex' then, as that is portable).

@jdhao
Copy link

jdhao commented Oct 21, 2021

Hmm, yes. Setting the servername explicitly for Neovim is reasonable (although I'd write it to stdpath('cache') .. '/vimtex' then, as that is portable).

It is a good idea! Now we do not need two code paths for server location. How come I hadn't thought about stdpath('cache') in the first place 😅

@lervag
Copy link
Owner

lervag commented Oct 21, 2021

stdpath() is neovim only, so it would "complicate" the docs. I'm not aiming for perfect, only "good enough".

I've pushed another improvement to the docs; again I'm happy for feedback. I believe this is getting quite good now.

lervag added a commit that referenced this issue Oct 21, 2021
@clason
Copy link
Contributor

clason commented Oct 21, 2021

Do you need this dance for vim, which has --remote?

@lervag
Copy link
Owner

lervag commented Oct 21, 2021

Yes. Vim has the exact same problem, because it also needs to know the correct servername.

@clason
Copy link
Contributor

clason commented Oct 21, 2021

Ah, OK, I've only ever seen the issue raised in the context of nvr :D

Then a neovism is of course less than ideal.

@lervag
Copy link
Owner

lervag commented Oct 21, 2021

Yes, I also made the same conclusion first. But when I was rewriting all of this, I realized that we have the same exact challenges. I believe it may be somewhat more robust from Vim in that vim --remote ... will immediately use an existing server regardless of environment variables or whatever; if it exists. nvr --remote ... will use the default server /tmp/nvimsocket, which is basically never the correct servername. Thus, nvr works if the NVIM_LISTEN_ADDRESS exists or if you provide the correct servername explicitly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants