You are here: News
Akatombo Media logo
English 日本語

Akatombo Web Log

Wednesday, April 19, 2006

Using PHPfi with TextMate

<UPDATE> This bundle has been included with the latest version of phpfi (2.1).  I’m thrilled that this code will now automatically be available to the group of people who need it.  If you code in PHP on the mac, check out phpfi whether you use textmate or not, it truly is a fantastic reference, especially if you like to go offline to do your coding.</UPDATE>

I couldn’t find any information about it anywhere, so I took it upon myself to figure out how to make a command in TextMate to allow it to do automatic function lookup in PHP based on the existing .  The advantage of being able to use Documentation for Word command in the PHP bundle which allowed a user to go find an entry in the online manual for the selected word in Textmate.  The biggest advantage to using PHP for looking these things up is that you don’t have to be online to lookup a function.  I generally find that I’m more productive and focused when I go offline to program.  It keeps me away from the temptation to go online to look something up real quick, coming back 3 hours later staggering and wild-eyed from a wild information intake bender.

Without further ado, here is a screenshot of the bundle, settings and all.  Click the thumbnail to view it full size.  A rough explanation of how it works is below the image.

phpfi bundle in textmate

The hardest part about figuring out how to do this was that php interface to other problems is Applescript, but TextMate doesn’t support Applescript.  I searched the manual and mailing list and the rest of the web in hopes of finding a way to make Textmate execute some Applescript code (later on I found information that would have been very helpful on the TextMate wiki), but was having no luck.  Then I remembered that TextMate can execute shell commands, so I went in search of a shell command for executing Applescript and found it in osascript This applescript requires an argument (the currently selected word in TextMate) and arguments weren’t allowed until 10.4 so it would likely not work in pre-Tiger versions of Mac OS X.  The -e parameter lets you feed the Applescript in line by line instead of giving osascript a file to load and execute, so that is what we did.

if grep <<<${TM_CURRENT_WORD:-!} -Esq '[a-zA-Z0-9_]+'
then osascript -e 'on run argv' -e 'set theSelection to item 1 of argv ' \
-e 'tell application "PHP Function Index"' -e 'activate' \
-e 'set the searchString to theSelection' -e 'end tell' -e 'end run' $TM_CURRENT_WORD
fi

We pass in $TM_CURRENT_WORD as an argument and the applescript passes that word onto PHP as search term.  I was very happy to get it to work, because this is the first time I’ve ever been able to make Applescript do anything useful for me.  Either the ways I generally think I would like to use Applescript are different than what Applescript is actually intended to do, the applications I’ve tried to do these things with don’t have complete enough Applescript support, or I just don’t know what I’m doing with Applescript.  I think it is mostly the last one, but I get the feeling that there were liberal doses of the second option in there too.

At any rate I’m very happy that I can now use phpfi with TextMate, and now I have to dive into an extremely tight programming deadline.

Posted by UltraBob on 04/19 at 12:32 AM
23 Comments | No Trackbacks | Permanent Link


Page 1 of 1 pages