Wednesday, July 25, 2007

VIM-LaTeX Modification: Forward Searching in OS X

UPDATE 3: If it doesn't seem like commands are launching from within Vim, check out my post on setting PATH in Vim. In particular, if you are using the nascent MacVim, checking the "Launch Vim processes in a login shell" preference may fix your problems.

UPDATE 2: I've updated the patch file to fix a few bugs in my original implementation (thanks to Ross M. Richardson for the help!). This version supports forward searching from multi-file projects. By the way, if you didn't know, if you have a project with multiple files where the main LaTeX document is called my_latex_file.tex, you should create a dummy file called my_latex_file.latexmain in the same directory. That way, VIM-LaTeX will know to run its compiler and viewer on the main file and not on any of the smaller constituent files.

UPDATE 1: Skim 0.6 and up supports spell checking of a PDF. This is a strange feature of a PDF viewer since Skim does not allow you to edit the PDF text directly. However, it makes a lot of sense when inverse searches are supported. After doing Shift+Command+CLICK on the misspelled word, the TeX editor will open up near the line of TeX where the word is found.

I have already commented on doing inverse searches (or backward searches) in Vim on OS X. However, I have not handled forward searches. Ross M. Richardson offers a hack for doing forward searches into PDFView; however, it's clear that while this is a hack it is certainly not a lifehack. What I really want is a version of VIM-LaTeX that supports using PDFView, Skim, TeXniscope, or any UNIX shell script as a forward-search-compatible viewer.

The script I presented in an earlier post makes backward searches compatible in nearly any PDF/DVI viewer. Now, I just need to hack compiler.vim to do the reverse. I did this by modifying the Tex_ViewLaTeX and Tex_ForwardSearchLaTeX functions from the 20060325 release of VIM-LaTeX. You can find my changes in VIM-LaTeX-osx-inverse-search-compiler.patch. To apply the patch, go into your ~/.vim/ftplugin/latex-suite/ directory, backup your old compiler.vim file, and try
patch -p0 < VIM-LaTeX-osx-inverse-search-compiler.patch
Of course, you should either drop the patch file into that directory or change the patch command so it has the full path to the patch. Now, all you need to do is configure your ~/.vimrc and you'll be able to use \lv for viewing and \ls for forward searches.

OS X Applications: Say that I want to use Preview to view PostScript files, TeXniscope to view DVI files, and Skim to view PDF files (note that both TeXniscope and Skim can open all of these, but only TeXniscope supports DVI source specials). In that case, I could put these lines in my ~/.vimrc file:
let g:Tex_ViewRule_ps = 'Preview'
let g:Tex_ViewRule_pdf = 'Skim'
let g:Tex_ViewRule_dvi = 'TeXniscope'
Now, VIM-LaTeX will use these whenever \lv (for viewing) or \ls (for forward search) are used. That's right, it will do forward searches with these OS X applications (well, Skim and TeXniscope)! That's easy enough.

UNIX Viewer Scripts: Now, let's say I wanted to use the skim and texniscope scripts that I built earlier. In this case, I use the following in my ~/.vimrc file:
let g:Tex_TreatMacViewerAsUNIX = 1
let g:Tex_ExecuteUNIXViewerInForeground = 1
let g:Tex_ViewRule_ps = 'skim'
let g:Tex_ViewRule_pdf = 'skim'
let g:Tex_ViewRule_dvi = 'texniscope'
That g:Tex_TreatMaxViewerAsUNIX tells VIM-LaTeX not to use open -a to open the viewer. The g:Tex_ExecuteUNIXViewerInForeground gets rid of the & appended to the end of the execution strings used by VIM-LaTeX. For some reason, this option needs to be set to get UNIX viewers to work in MacVim.

Empty Viewer Scripts and Other Stuff: As before, if those ViewRule variables are set to empty values (i.e., ''), open -a will be used. On the other hand, if g:Tex_TreatMacViewerAsUNIX is set, VIM-LaTeX will act like it does on a UNIX machine, and xdvi, xdvik, and kdvi can be used. These three DVI viewers are handled specially. Any other UNIX script should follow the convention:
script "%target_file" %line "%source_file"
where script is the UNIX command, %target_file is the main PDF, PS, or DVI file to open, and %line is the line in the TeX source file %source_file for forward searching (i.e., using \ls). If \lv is being used, the %line and %source_file parts are omitted.

4 comments:

Unknown said...

Thanks for this, I found it really handy!

Anonymous said...

Another great big THANKS!

I found Skim yesterday, and it was much better than using Preview or Acrobat because it would do reverse searching...but now you've made it just about perfect by making forward searching work. Thanks again :)

Anonymous said...

Thanks a lot for this!... just a quick comment, the compiler.vim is hardcoded with the viewers Skim.... if you want to have the scripts "skim"... as your viewers, then you need to edit compiler.vim

Everytime I set a system it takes me a while to remember this and I promise myself that I'd leave a coment here...

Thanks again,

R.

Ted Pavlic said...

Read the blog post more carefully. You need not edit compiler.vim directly.

Check out the "UNIX Viewer Scripts" section. You'll see how to setup "skim" as your viewer *WITHOUT* having to modify compiler.vim.