Showing posts with label Mac OS X. Show all posts
Showing posts with label Mac 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.

Tuesday, September 18, 2007

Removing WiFind

The WiFind application from TastyApps has potential, but in the end it falls short and is not worth all of the bother. WiFind will slow down your computer nearly to a halt in some cases. Because the developers made it an input manager without a preference pane, it cannot easily be turned off. Even when it's running well, it still takes a click or two in order to get it to display the information you need.

The free application coconutWiFi is almost identical to WiFind, but because it's an application, it's much easier to install, load, unload, and uninstall.

Getting rid of WiFind can be a major pain (especially if it's in the process of grinding your machine to a halt at the time). The developers gave it an installer (because it's an input manager and not an application), but despite Apple's guidelines that all programs with installers must have uninstallers, they did not give it an uninstaller.

So, here's how I went about turning OFF WiFind. WiFind uses this InputManager called "SIMBL" to modify the wireless menu directly (rather than putting up its own menu, like coconutWiFi). So, you can either remove SIMBL or remove WiFind from SIMBL so that while SIMBL loads, WiFind will not.

To remove WiFind only, look for the folder
/Library/Application Support/SIMBL/Plugins

or the folder
~/Library/Application Support/SIMBL/Plugins

where ~ is your home directory. Inside that folder, you'll find a WiFind.bundle. Move it somewhere else (or delete it) and logout (or restart) to disable/remove WiFind.

Alternatively/additionally, you can remove SIMBL. Look in the folder:
/Library/InputManagers

or
~/Library/InputManagers

where ~ is your home directory. There should be a SIMBL folder inside one of these. To remove SIMBL, delete the SIMBL folder. To disable it, remove the SIMBL folder. Again, be sure to logout or restart after making this change. When you're logged back in, SIMBL should not be loaded.

If you think you will want to turn this off later, simply move the SIMBL folder out of the InputManagers folder and into some other folder that you create (I created an InputManagersOff folder in my /Library). Next, if you logout (or restart), WiFind will not be started. At some time, you should probably also get rid of the folders
/Library/Application Support/SIMBL/

and
~/Library/Application Support/SIMBL/

where ~ is your home directory.

You might notice that there are files dropped in /Library/Receipts (or maybe ~/Library/Receipts) from WiFind (or wifind) and SIMBL. You may consider removing these files too, but it won't hurt to leave them there.

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).

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.

Monday, July 23, 2007

iUseThis: The OS X Software that I Use

Are you interested in the software that I use on my Mac? See my iUseThis profile.

You can use iusethis.com to see what people are using and find software related to other software. You can even setup your own profile and keep track of the software that you use.

Desktop Manager for OS X

This is not anything new, but there is a very nice freeware (open source) Desktop Manager for OS X. That is, this implements virtual desktops (i.e., multiple desktops that you can switch between; this lets you spread your apps across different screens).

It has a lot of cool features.

The Quick Reference Site (Cards and Books)

UPDATE 3: I did find the source of that regular expression card at Gordon McKinney's Pro IT Blog. Well, it's not a LaTeX source, but it's at least in letter format.

UPDATE 2: I found another good list of quick reference cards at the plantOzh blog. It includes a a regexp card, Perl 5 booklet, and CSS cards (1.0, 2.0). Yes, these cards are archived at the blog, so all of the commentary below still applies.

UPDATE 1: It's also annoying when someone simply archives existing quick reference cards because sometimes those archives use A4 paper. If these were linked to their origins, the source code (perhaps LaTeX) might be available, making a conversion to letter paper easy.

Lately, I have been quick reference happy [1, 2, 3, 4]. I cannot explain why, but I suddenly want to surround myself with quick references.

I think I found a great new quick reference resource. It's The Quick Reference Site, which features quick reference cards, e-books, and other papers and tutorials. From the site:
Throughout the years I have collected a huge pile of documents that deal with almost every aspect of software development. The purpose of this site is to centralize this information and to make it available to everyone who may need it or shares my passion on this subject. There are still a lot of documents in my "archive" that I like to share with you all.
At the moment this site represents only a fraction of what I want to provide, so a lot of work still needs to be done. As a result, you should consider this site as a work in progress. I'll try to keep you updated on the most challenging, powerful and continuously evolving software technologies of the moment. I hope that especially Java and C++ fanatics (like myself) will find some valuable information over here.
As far as I can tell so far, the author archives quick references that he has found over time and links us, the readers, to those archives. The advantage of this is that it minimizes the chances of broken links. The disadvantage is that the archives may not be the most recent versions. Additionally, if this site ever goes down and our bookmarks point there, those bookmarks will be broken.

Some quick reference cards that I want to point out (if you go to his list, you can also read descriptions of these programs):

So, that's fun.

On a slightly unrelated note, you can also check out Hawk Wings to find a QuickSilver quick reference and the QuickSilver user manual.

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?

Using Skim, PDFSync, and TeXShop

UPDATE 3: I have updated the script to support command-line options that will prevent TeXShop from doing things like refreshing text (and trashing your unsaved changes), activating (taking your focus away), and opening (possibly getting in front of your view). Try the -h option for details.

UPDATE 2: There is a related Skim revision that will make it easier to use AppleScript for PDFSync interfacing. This change will prevent having to make UNIX shell scripts that must be modified with chmod.

UPDATE 1: As you can see in the Skim revision history, future versions of Skim will include their "Application Support" directory (e.g., ~/Library/Application Support/Skim/) as well as a "Scripts" subdirectory in their path. When that occurs, scripts like the one below can be stored there and executed from Skim without specifying the full path name.

I know there are ways to change TeXShop's previewer; however, I don't know how to do them. Additionally, I don't use TeXShop, so I have little incentive to look those things up.

However, I do use Skim, which is compatible with pdfsync, and recently someone asked me how they could get Skim to call up TeXShop at a particular line (as someone would want to do when using pdfsync).

For that, I generated a texshop script that looks like this (an earlier version of this can be found on the Skim wiki):
fileName="$1"
lineNumber="$2"
gotoString=""

[ "${fileName:0:1}" == "/" ] || fileName="${PWD}/${fileName}"
[ "${lineNumber}" == "" ] || gotoString="goto line ${lineNumber}"

exec osascript \
-e "set texFile to POSIX file \"${fileName}\"" \
-e "tell application \"TeXShop\"" \
-e "activate" \
-e "open texFile" \
-e "tell front document" \
-e "refreshtext" \
-e "${gotoString}" \
-e "end tell" \
-e "end tell"
To use this script, follow these steps:
  1. Save that file as something (preferably in the PATH somewhere, but this is not necessary) like
    /usr/local/bin/texshop
    or anywhere you would like (e.g., /Users/username/bin/texshop). However, if you place it somewhere differently, be sure to update the red text in these instructions.

  2. Next, chmod it 0755, as in
    chmod 0755 /usr/local/bin/texshop
    This can be done from the Terminal.

  3. Finally, tell Skim's PDFSync preferences (under Preferences->LaTeX or Preferences->Sync depending on your Skim version) to use a "Custom" editor with
    Command: /usr/local/bin/texshop
    Arguments: "%file" %line
    Make sure you click AWAY (e.g., onto a different tab) from the PDFSync settings before you close the Preferences. Otherwise, one of your text boxes may not get saved.
After this is done, doing Shift+Command+CLICK in Skim on a location in the PDF will bring up the source in TeXShop located at the that generated that point in the PDF.

Note that if you have saved texshop somewhere in your PATH, then you should be able to drop the /usr/local/bin/ from the front of the Command: line.

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!