Showing posts with label iTM. Show all posts
Showing posts with label iTM. Show all posts

Saturday, December 29, 2007

PDFSync Inverse Searches in MacVim

Remember the post on doing inverse searches in Vim on OS X? The idea was to drop gvim-pdfsync into
/usr/local/bin
(or somewhere else in your PATH) and then execute
gvim-pdfsync "%file" %line
to do inverse searches in Vim for OS X just like you can in Windows or in other UNIXes. For example, in Skim, under the PDFSync section of the Sync tab of the Preferences, you can put
Preset: Custom
Command: gvim-pdfsync
Arguments: "%file" %line

For MacVim

I thought it might be nice if I published how to do the same thing in the nascent MacVim, which supports servers just like Vim for Windows. Provided that you've installed the mvim shell script somewhere in your PATH, you can setup Skim with
Preset: Custom
Command: mvim
Arguments: --remote-tab-silent +":%line;foldo!" "%file"

or, if you don't like tabs
Preset: Custom
Command: mvim
Arguments: --remote-silent +":%line;foldo!" "%file"

Retro-Approach

Alternatively, if you really liked the gvim-pdfsync approach and want something similar for mvim, put mvimtab-pdfsync or (if you don't like tabs) mvim-pdfsync in your PATH (e.g., in /usr/local/bin) and then setup Skim for:
Preset: Custom
Command: mvimtab-pdfsync
Arguments: "%file" %line

or (if you used mvim-pdfsync)
Preset: Custom
Command: mvim-pdfsync
Arguments: "%file" %line
I hope that helps.

Wednesday, July 11, 2007

PDFSync inverse searches on Vim for OS X

FINAL UPDATE: See the new post, "PDFSync Inverse Searches in MacVim," for the state of the art on this subject.
UPDATE 8: 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.

UPDATE 7: caveat Vimmer! Inverse searches in Skim are called up with Shift+Command+CLICK. This is fine; however, if you hold Shift+Command too long, the AppleScript for calling up Vim is going to get confused. In other words, be sure to release the modifier keys as soon as possible after the "click."

UPDATE 6: I found information about doing both forward and backward searches with Vim and PDFView (see also: using gotoline.sh). From these, I've made hacks to the VIM-LaTeX scripts for PDFView and other viewers (like Skim and TeXniscope). I will describe these hacks in another post.

UPDATE 5: See Vim Tip #225 and the corresponding wiki entry for information about both backward and FORWARD searches in Vim. I also found a nice LaTeX tools script for Vim that has forward searching built in. I found these pages linked from a page on xdvi inverse searches.

UPDATE 4: Very trivial updates have been made in a 1.03 version of this script.

UPDATE 3: I have posted a 1.02 version of this script. The changes are fairly trivial, but you might be interested in checking it out.

UPDATE 2: This script is now a part of the Skim wiki.

UPDATE 1: I have mirrored this post at my web site.

WINDOWS USERS: See "Performing inverse searches" from the VIM-LaTeX quick start guide.

I have posted information about this in a number of places [1, 2, 3, 4, 5]. I plan to add something at MacResearch sometime soon too.

The package PDFSync allows users to do "backward searches" or "inverse searches" from PDF viewers like iTeXMac, TeXniscope, Skim, and others. That is, if you generate a PDF with LaTeX (or Plain TeX or ConTeXt), you will be able to click on text in the PDF and have an editor open up and position the cursor at the TeX source code that generated that text. That can be very nice.

There is a related feature for DVI files, but there are very few good DVI viewers out there (TeXniscope comes close), so I just focus on PDFSync).

I use Vim with VIM-LaTeX to do my document preparation. I would like to also be able to use PDFSync. However, while inverse searching is supported in Windows, it is not easily done in OS X.

I found a thread describing how to do inverse searching with AppleScript that issues raw commands to Vim. I decided to take that AppleScript, package it into a bash script, and fix it so that it had no problem handling files with spaces or symlinked files or multiple files with the same base name. The result is this script:
#!/bin/bash

filename="$1"
lineNum="$2"

[ "${filename:0:1}" == "/" ] || filename="${PWD}/${filename}"

exec osascript \
-e "set ESC to ASCII character of 27" \
-e "tell application \"Vim\" to activate" \
-e "tell application \"System Events\"" \
-e "tell process \"Vim\"" \
-e "keystroke ESC & \":set hidden\" & return " \
-e "keystroke \":if bufexists('$filename')\" & return " \
-e "keystroke \":exe \\\":buffer \\\" . bufnr('${filename}')\" & return " \
-e "keystroke \":else \" & return " \
-e "keystroke \": edit ${filename// /\\\\ }\" & return " \
-e "keystroke \":endif\" & return " \
-e "keystroke \":${lineNum}\" & return " \
-e "keystroke \"zO\" " \
-e "end tell" \
-e "end tell"
Copy that script in a place (preferably in your PATH) like
/usr/local/bin/gvim-pdfsync
and chmod it 0755. That is, do
chmod 0755 /usr/local/bin/gvim-pdfsync
Then you can use the script like
gvim-pdfsync "%file" %line
where %file is the name of the file to be opened and %line is the line to place the cursor on. So, for Skim, you would put in your LaTeX (or Sync) preferences under "PDFSync" support:
Preset: Custom
Command: gvim-pdfsync
Arguments: "%file" %line
After configuring Skim, BE SURE TO CLICK AWAY FROM THE TEXT BOXES before closing the configuration. For example, click on one of the other tabs. Otherwise, the dialog box may not record your changes to the last text box you changed.

I hope that's useful to someone.

Friday, July 06, 2007

Script to open file in TeXniscope from command line

UPDATE 2: A much fancier version of this script is now available. It has options that make it easier to make TeXniscope update its file and position (via PDFSync or DVI source specials) in the background. The options are described briefly with the -h parameter.

UPDATE 1: A more mature version of this script is now available. It has a usage line. It checks for the existence of files. It tries to guess at a file name if the file does not exist. It's a little more convenient for the command-line user.

Here's a script to open a (PDF or PostScript) file in TeXniscope from the command line:
#!/bin/sh

[ "`echo $*|cut -c 1`" == "/" ] || filename_prefix="`pwd`"

exec osascript \
-e "tell application \"TeXniscope\"" \
-e "activate" \
-e "open file ((POSIX file \"$filename_prefix/$*\") as string)" \
-e "refresh of the front document" \
-e "end tell";
or, alternatively,
#!/bin/bash

arguments="$*"
[ "${arguments:0:1}" == "/" ] || filename_prefix="$PWD"

exec osascript \
-e "tell application \"TeXniscope\"" \
-e "activate" \
-e "open file ((POSIX file \"$filename_prefix/$*\") as string)" \
-e "refresh of the front document" \
-e "end tell";

Create an executable script called texniscope (e.g., /usr/local/bin/texniscope that is chmod'd 755) and try texniscope FILENAME where FILENAME is the name of the PDF or PostScript file that you want to open (be sure to include the file extension).

This allows you to use TeXniscope as a LaTeX document viewer in the VIM-LaTeX suite for Vim.

See also: Script to open file in Skim from command line

I use this for Vim/GVim; however, this script was inspired by code in an example in the TeXniscope help file for making TeXniscope work with iTeXMac. See the TeXniscope documentation for information on how to build a script that will let iTeXMac call TeXniscope from the command-line (and even make use of PDFSync [CTAN, iTM]).

Script to open file in Skim from command line

UPDATE 4: I have updated the script to accept command line options. A -h option gives help text describing the new usage. The options can prevent Skim from being activated or opening the file. This may be useful when trying to get Skim to update in the background.

UPDATE 3: I have updated the script to check for whether or not files exist, try to guess the right files if they don't, and bail if it fails. It also has support for PS and DVI files. Also fixed a problem with symlinked files.

UPDATE 2: I have generated a more mature version of this script that also has the ability to position Skim's PDF view at a position corresponding to a line of your TeX source (provided you built the PDF with pdfsync).

UPDATE 1: As discussed in this feature request and this Wiki entry, in iTeXMac, you can also try
Skim.app/Contents/SharedSupport/displayline %line "%pdffile" "%texfile"
which lets you make use of Skim's PDFSync support. The Wiki page includes instructions on using this in Emacs and TextMate as well. If you just want to open the file, you might try
Skim.app/Contents/SharedSupport/displayline 1 "%pdffile"
but this might break if the TEX file isn't available (so maybe you should still use the AppleScript below).

Here's a script to open a (PDF or PostScript) file in Skim from the command line:
#!/bin/sh

[ "`echo $*|cut -c 1`" == "/" ] || filename_prefix="`pwd`"

exec osascript \
-e "tell application \"Skim\"" \
-e "activate" \
-e "open ((POSIX file \"$filename_prefix/$*\") as string)" \
-e "revert front document" \
-e "end tell";
or, alternatively,
#!/bin/bash

arguments="$*"
[ "${arguments:0:1}" == "/" ] || filename_prefix="$PWD"

exec osascript \
-e "tell application \"Skim\"" \
-e "activate" \
-e "open ((POSIX file \"$filename_prefix/$*\") as string)" \
-e "revert front document" \
-e "end tell";

Create an executable script called skim (e.g., /usr/local/bin/skim that is chmod'd 755) and try skim FILENAME where FILENAME is the name of the PDF or PostScript file that you want to open (be sure to include the file extension).

This allows you to use Skim as a LaTeX document viewer in the VIM-LaTeX suite for Vim.

NOTE ABOUT revert: The line with revert in it requires Skim version 0.5 or higher. If you don't have that version of Skim (or you don't care about refreshing the document), then delete that line.

See also: Script to open file in TeXniscope from command line

I use this for Vim/GVim; however, this script was inspired by code in an example in the TeXniscope help file for making TeXniscope work with iTeXMac. See the TeXniscope documentation for information on how to build a script that will let iTeXMac call TeXniscope from the command-line (and even make use of PDFSync [CTAN, iTM]). It should be easy to modify the script they give there to call Skim instead of TeXniscope; use my script here as an example. Note that Skim's AppleScript also supports all of the goto line stuff of TeXniscope. However, the syntax is different; see the Skim AppleScript dictionary for more information.