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

  1. Posted by Ian Cheung

    Just wondering Bob, have you shifted all your dev. onto Mac OSX?  I remember you were a big BSD fan.

    April 19th, 2006 06:03 AM


  2. Posted by UltraBob

    I still run all my servers on FreeBSD which is what I was doing before, but yeah—I’d say the vast majority of my development work happens on the Mac.  I never really used a FreeBSD desktop aside from about a month span where I was running FreeBSD on my windows compatible laptop.  The machine had some kind of glitch that would freeze it completely (fairly sure it was a hardware glitch) during some types of video display operations.  It would freeze the machine up completely so I had to give up on that.

    April 19th, 2006 11:33 PM


  3. Posted by Spiceee

    You’ve got a typo in the second line of code: ‘on run argv -e should be ‘on run argv’ -e

    Thanks for the tip!

    April 28th, 2006 12:41 AM


  4. Posted by UltraBob

    Hi Spiceee,

    Thanks for catching that typo!  Could have sworn I copy and pasted my bundle in so I’m not sure how that got in there.  I’m glad someone is finding this tip useful—it was driving me nuts that I couldn’t do it until I took the time to figure it out.

    April 28th, 2006 12:49 AM


  5. Posted by Casey

    Mine says “documentation for ***** not found” for pretty much everything I try. mysql_query doesn’t work. if works, but for doesn’t. Not sure what I’m doing wrong. I copy and pasted the code from here and then when that didn’t work I formatted it to match the screenshot. Any help?

    May 4th, 2006 04:22 AM


  6. Posted by UltraBob

    Hi Casey,

    When you type mysql_query directly into the search box in PHPfi does it work for you?  If not I’d guess you may need to download and install the php documentation in PHPfi.

    May 4th, 2006 04:28 AM


  7. Posted by Casey

    hmm, I restarted textmate and now I get the following as my tooltip: “/bin/bash/: -c: line 3: syntax error: unexpected end of file”

    Any suggestions? Oh, and when typing it into PHPfi’s serach field everything works great.

    May 4th, 2006 04:36 AM


  8. Posted by UltraBob

    I bet you are including the \’s in your bundle aren’t you?  Those just mean that the line continues, that shouldn’t actually be in your bundle.  Have a look at the screenshot for more details if that is your problem.  If not, please let me know.

    May 4th, 2006 05:38 AM


  9. Posted by Casey

    I don’t think that’s it because I removed those slashes. Just to be sure I copied and pasted it again and then removed the slashes. I also tried adding a tab before the “then” line.  Now I get the same error, only its line 2 this time. not sure why that changed.

    May 4th, 2006 05:59 AM


  10. Posted by UltraBob

    What version of Mac OS X are you using?  If you aren’t in Tiger osascript probably won’t work for you, I think I remember something about not being able to accept arguments to osascript prior to 10.4 (man page for more details)

    Does your bundle editor window look just like that in the screenshot (i.e. are all the options set up the same?)

    If you are in Tiger and the options are the same, could you give me a paste of the text in your bundle window?

    May 4th, 2006 06:08 AM


  11. Posted by Casey

    I’m running 10.4.6 on a macbook pro. My screen looks identitcal to yours in the screenshot. Here’s an exact paste.

    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

    May 4th, 2006 06:11 AM


  12. Posted by UltraBob

    Hmmm, I’m running out of guesses, I’m horrid at AppleScript, with this being the very first time I’ve ever managed to get it to do something useful for me.

    Guess 1:
    It is hard to tell from your paste where line breaks are.

    There should be a line break after -Esq ‘[a-zA-Z0-9_]+’

    and one before
    fi

    There should only be two line breaks in your bundle, if you have broken up the ginormous middle line that might be the problem

    Guess 2: The conversion my blog does on punctuation makes it tough to tell if maybe you are using different quotes in your bundle? mine are all straight primes and double primes.

    May 4th, 2006 06:30 AM


  13. Posted by UltraBob

    So did that work?

    May 4th, 2006 03:38 PM


  14. Posted by Casey

    No, I don’t think any of that is it. I have breaks in all the right spots and I have the correct quotes used. Oh well, I guess I’ll just have to do without it. Thanks for all the help though! I’ll give it a try sometime in the future and maybe it will work. We’ll see smile

    May 4th, 2006 03:42 PM


  15. Posted by UltraBob

    bizarre.  Do you maybe not have the developer’s toolkit installed?  Probably not that I guess.

    One further debugging step would be to try running that whole osascript command from the command line with mysql_query as an argument, and see if you get the same error then.

    May 5th, 2006 02:11 AM


  16. Posted by Timothy Dearborn

    Thanks so much for figuring this out for us. It worked like a charm and I would have never been able to put in the hours to figure it out.

    I agree with your assessment of Applescript. I would love to use it more, but many times the functionality isn’t there or I don’t have a clue what I am doing.  Guess I need to hang around Sal more.

    June 6th, 2006 02:57 AM


  17. Posted by roberto

    It worked, no problem at all..

    I’m also looking for some kind of tooltip or *maybe* even code completion, and bind phpfi to just the whole documentation with examples.

    I found another script to do just that but didn’t worked :(

    Anyways, this is great help!

    Thank you!

    June 7th, 2006 05:08 AM


  18. Posted by Rafael Vega

    Hi Bob. I’m having the same problem as Casey, Do I need to install the developper tools for it to work? thanks!

    August 26th, 2006 08:31 PM


  19. Posted by UltraBob

    Hi Rafael,

    I think it is very likely that you need to install the developer tools.  I think osascript may come with the developer tools.

    August 28th, 2006 01:56 AM


  20. Posted by Sam Figueroa

    Very useful tip UltraBob thank you. I had the same -better to stay offline, but need to look up constantly- problem.

    February 27th, 2007 06:34 PM


  21. Posted by UltraBob

    Very glad I could be of help.  With the advent of CJK input for textmate just about all of the irritating things about TextMate for me are gone.

    February 28th, 2007 09:44 AM


  22. Posted by bikoz

    FYI: the screenshot link is (half) broken. It points to the thumbnail version of the image phpfi_thumb.png instead of phpfi.png wink

    November 28th, 2007 09:45 PM


  23. Posted by UltraBob

    Hey Bikoz,

    Yeah, I guess with javascript off you would be pointed to the wrong image.  I’ll go fix that.  Thanks for pointing it out.

    UB

    November 29th, 2007 04:12 AM


Commenting is not available in this weblog entry.