Showing posts with label OS X. Show all posts
Showing posts with label OS X. Show all posts

Thursday, December 11, 2008

Making MacVim work with Vimperator's external editor feature

Because I'm a vim user, it's naturally for me to use vimperator on top of Firefox.

Today I posted a tip on vimperator.org that helps me use Vimperator on my MacOS system. I'm reposting that tip here.

To make a long story short, try an editor setting like
:set editor='bash -lc "mvim -f \$*" mvim '
or (if you prefer gvim)
:set editor='bash -lc "gvim -f \$*" gvim '
Then you should be able to hit CNTRL+I to launch an external editor for textareas. There are simpler solutions and explanations below.



Shelling out to external commands is hairier in OS X than it is on other platforms.

In my case, I use MacVim, which includes a script mvim that can be called as
mvim -f ...
so that the script will wait for the GUI to exit before it exits. This functionality is identical to the gvim found with other Vim distributions (including older Vim distros for OS X).

My mvim script is in /usr/local/bin. Unfortunately, the PATH environment variable that sits behind user processes has to be set in an special ~/.MacOSX/environment.plist file, and the default PATH does not include /usr/local/bin. If you don't believe me, try
:!env
As you can see, those environment variables are very different from the ones you'd expect in a "login shell." You could go add an appropriate environment.plist file to match your login shell, but then you'll have to keep it up-to-date after every change to your shell profile. As a consequence, it's probably a good idea to
:set shcf='-lc'
Then you'll notice that :!env gives you more expected results. However, Vimperator won't run the editor unless it can find it in what Firefox thinks is the PATH, and so shcf won't help you.

A simple solution is to
:set editor='/usr/local/bin/mvim -f'
For most people, this solution will be perfect. However, some will notice that their favorite utilities (e.g., /opt/local/bin/par or /sw/bin/aspell) will not be accessible in the editor (unless the environment.plist file is modified).

So the final solution is to use bash as your external editor, and have it operate as a login shell that calls your editor of choice.
:set editor='bash -lc "mvim -f \$*" mvim '
Here, bash calls mvim and passes "mvim" as $0 and everything following bash as $1, $2, and so on...

Thursday, September 04, 2008

iTerm AppleScript crashing fix

Lately, my officemate and I noticed that iTerm build 0.9.5.0902 crashes whenever AppleScript tries to talk to it. I was excited about some of the full screen fixes, but I couldn't do without my "Open iTerm Here" context menu item, and so I had to come up with a fix.

In my old scripts, I was doing things like this:
tell application "iTerm"
activate
set theTerm to make new terminal
tell theTerm
set theSession to make new session at the end of sessions
tell theSession to
exec command "/bin/bash -login"
write text "ls"
end tell
end tell
end tell
It appears like it's the make new session that's causing the problem. So I changed the set theSession... and tell theSession... lines to use a launch instead. The result:
tell application "iTerm"
activate
set theTerm to make new terminal
tell theTerm
launch session "Default Session"
tell the last session
exec command "/bin/bash -login"
write text "ls"
end tell
end tell
end tell
Not only does that not crash for me anymore, but it seems to be a bit faster (my imagination?).

Sunday, December 30, 2007

File Associations and PATH with MacVim

UPDATE: The newest release of MacVim has a preference pane that lets you "Launch Vim processes in a login shell." If turned on, it should solve the PATH problem suffered by some Vim users.
If you use Vim for OS X or the nascent MacVim, you may have noticed that
  • your PATH is not set correctly (i.e., compilations fail) and
  • Vim is not associated with all of your text files (i.e., it doesn't show up in the "Open With" context menu)
A while ago, I realized that the gvim.app available for download from the OS X Vim site had the potential to solve both of these problems, and so I hacked it to produce:
  • gvim.app (for Vim for OS X) and
  • mvim.app (for MacVim; maybe unneeded if "Launch ... login shell" preference turned on)
To install one of these files, download the archive, unzip it, and move the application into your /Applications folder. If you start Vim from that application, it will open as usual but will have the PATH set as if you were in the Terminal (i.e., it executes a bash login before exec'ing Vim). You should also notice that the new launcher application shows up in your "Open With" menus. Finally, if you put the launcher in your Dock, you can use it to generate new Vim windows, even if Vim is already open.

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, August 29, 2007

Viewing man pages and text files in Skim

As discussed on the Skim wiki, it is possible to view man pages (and more) nicely formatted as a PDF in Skim.

If you use bash, try adding these lines to your .bashrc:
function skimcat () { cat $* | enscript -p - | open -f -a Skim; }
function manp () { man -t $* | open -f -a Skim; }
Alternatively, if you use tcsh, try adding these lines to your .cshrc:
alias skimcat="cat \!* | enscript -p - | open -f -a Skim"
alias manp="man -t \!* | open -f -a Skim"
Then you'll be able to do skimcat to cat files to Skim, and you'll be able to use manp to view man pages formatted in Skim.

NOTE: You can actually pipe anything that is PostScript (or PDF) formatted to Skim (via the open command shown above).

Tuesday, July 17, 2007

Using Skim with MATLAB

I found a hint for using TeXShop as a MATLAB previewer on OS X, primarily because plots look better in PDF and TeXShop auto-updates files from disk. This was a tip from back in 2005. Since then, MATLAB's OS X plotting has gotten a lot better. Additionally, Skim has been invented.

Because of the advances in MATLAB, this may not be a useful hint anymore, but give this a try sometime:
figure(1);
set(gcf, 'Visible', 'off');
plot(x, y);
print(gcf, '-dpdf', 'figure1.pdf');
system('open -a Skim figure1.pdf');
Now, I believe there's a way to do this without having to plot first. That is, I think there's a way to plot to PDF without first generating that figure.

What's cool about this is that any future updates of the PDF will cause Skim to update automatically. Therefore, Skim becomes your plot viewer. That might be nice, right?

Thursday, July 12, 2007

Skim automatic refreshes and simpdftex

UPDATE: I've updated the script so that it works with OS/X Leopard. See comments for details.
The OS X PDF viewer Skim has the ability to automatically refresh files after they have been updated on disk. In Skim version 0.4.1 and below, this was done by polling a file every second to see if it has been changed. In Skim version 0.5 and up, this feature is implemented using kqueue events. This is a much better way of doing things. However, if the file that is open gets deleted and a new file created with the same name, events for that new file will not be received by Skim.

Unfortunately, the script simpdftex that comes with a number of LaTeX distributions (e.g., gwTeX) deletes a PDF before updating it. This means that autorefreshes won't work when regenerating a PDF from its LaTeX source using simpdftex.

So, motivated by the history in a Skim bug report, I've put together a simpdftexnodel script that fixes this problem (based on the 2007/05/07 gwTeX simpdftex). To use this file:
  • Place the script somewhere (probably in your PATH).
  • chmod the file 0755.
  • Make sure that your distillers and TeX program (or at least symlinks to them) exist in the same directory.
  • Stop using simpdftex and start using simpdftexnodel.
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.

Saturday, July 07, 2007

MacResearch - Online Community for OS X in Science

This might have potential...

MacResearch - Online Community and Research for Mac OS X in Science
MacResearch.org is an open and independent community for scientists using Mac OS X and related hardware in their research. It is the mission of this site to cultivate a knowledgeable and vibrant community of researchers to exchange ideas and information, build a community knowledge-base, and collectively escalate the prominence of Apple technologies in the scientific research community.

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.

Wednesday, June 27, 2007

Pine, PASSFILE, all-patch, and Thunderbird 2.0 IMAP keywords

Thunderbird 2.0 has been released! It's fast and finally supports an arbitrary number of custom IMAP keywords (IMAP keywords are similar to the labels used in GMail). I'm thrilled about this.

However, regardless of how wonderful Thunderbird gets, I will always keep PINE around (which has supported IMAP keywords for a long time).

When I build PINE for my systems, I usually pick up a few of the most popular patches as well. I never noticed that there was an all of the above patch that packages ALL of the most popular patches, new features, and bug fixes. I think this is pretty exciting too.

To build PINE with PASSFILE support (i.e., support for saving passwords to file), I recommend using the infinite ink instructions:
#!/bin/sh
./build clean
./build 'EXTRACFLAGS=-DPASSFILE=\".pine.pwd\"' osx
# ^^^
# platform
You can find a list of platforms in the doc/pine-ports file. Some common ones include (see the document for any special build instructions):
    BSD (original BSD 4.3 from U.C. Berkeley)
bsd BSD 4.3

BSDi
bs3 BSDi BSD/386 Version 3 and Version 4
bs2 BSDi BSD/386 Version 2
bsi BSDi BSD/386 Version 1

Cygwin
cyg Cygwin environment under Windows

HP-UX
hpx HP-UX 10.x
hxd HP-UX 10.x w/ DCE
ghp HP-UX 10.x using gcc
hpp HP-UX 8.x and 9.x
shp HP-UX 8.x and 9.x w/ TCB
gh9 HP-UX 8.x and 9.x using gcc

Linux
lnx Linux using crypt from the C library
lnp Linux using PAM
slx Linux using -lcrypt for crypt()
sl4 Linux using -lshadow for crypt()
sl5 Linux using shadow passwords
ldb Debian Linux
lmd Mandrakelinux
lrh RedHat Enterprise and RedHat 7.2 or later
lsu SuSE Linux

Macintosh
osx Macintosh OS X
ox2 Macintosh OS X 10.2 and earlier

NetBSD
neb NetBSD

OpenBSD
bso OpenBSD w/ shared-lib

QNX
nto Neutrino

SCO
sc5 SCO Open Server 5.x
go5 SCO Open Server 5.x using gcc
sco SCO Unix
gsc SCO Unix using gcc

Sun Solaris (Solaris 9 is the same as SunOS 5.9)
gs5 Sun Solaris >= 2.5 using gcc
soc Sun Solaris >= 8 using Sun C
gs4 Sun Solaris <= 2.4 using gcc
so5 Sun Solaris >= 2.5 (try soc or gs5)
so4 Sun Solaris <= 2.4

Sun SunOS (This is pre-Solaris SunOS)
sun Sun SunOS 4.1
ssn Sun SunOS 4.1 w/ shadow passwords
gsu SunOS 4.1 using gcc
s40 Sun SunOS 4.0

System V Release 4
sv4 System V Release 4

Windows
wnt Windows NT 3.51
Happy mailing!