TextMate command to look up functions at api.drupal.org

published on November 26, 2007

Is it also for you a routine to look up the documentation for Drupal hooks at api.drupal.org? If you also use TextMate and are sick of having to command-tab to your browser to get to the documentation, then you’ll have a much better alternative in about 15 seconds.

Go to Bundles → Bundle Editor → Show Bundle Editor. There, click the plus-button in the bottom left corner to add a new command. Set the input to None, the output to Show as Tool Tip and the scope selector to source.php. Copy/paste the command below and assign a shortcut — I use CTRL + D.

if grep <<<${TM_CURRENT_WORD:-!} -Esq '^[a-zA-Z0-9_]+$'
    then
        FILENAME=${TM_FILEPATH##*/}
        BASENAME=${FILENAME%%.*};
        if [ `echo "$TM_CURRENT_WORD" | grep -E "${BASENAME}_[0-9A-Za-z_]"` ]
        then
            HOOK=${TM_CURRENT_WORD##${BASENAME}}
            URL="http://api.drupal.org/apis/5/hook${HOOK}"
        else
            URL="http://api.drupal.org/apis/5/${TM_CURRENT_WORD}"
        fi
        exit_show_html "<meta http-equiv='Refresh' content='0;URL=$URL'>"
    else echo "Nothing to lookup (hint: place the caret on a function name)"
fi

If you now place the cursor above a Drupal core function and press the shortcut, you’ll be presented with a direct lookup of that function at api.drupal.org. Better even, if you place the cursor on a hook implementation, e.g. mymodule_menu, then you’ll get the documentation of hook_menu. Note that your file must be saved for this to work (and it must be named according to Drupal conventions, i.e. mymodule.module).

Comments

add1sun's picture

add1sun

Many thanks for sharing this - it’s totally rockin’!

moshe weitzman's picture

i have played around with creating a drupal grammer which highlights leading spaces in lines so you can see if each is two spaces and so on. this would be a great feature to bind to the drupal grammer. we would find other uses for that too. we really need a drupal bundle in textmate svn. anyone?

thanks, wim.

Wim Leers's picture

Wim Leers

I totally agree. I might step up to do this after my exams, but it’s rather unlikely. I hacked this snippet together using the code of the PHP lookup snippet :P Heck, I’m not even entirely sure what scripting language this is, I think it’s shell script?

I would definitely love to see a centralized effort for this. We’ll see.

Dries Knapen's picture

Great tip, really useful! May be worth mentioning though that it’ll only work after you saved the file according to standard practices (i.e. mymodule_menu will only be recognized if the filename is mymodule.something). The snippet doesn’t work with newly created files that have not yet been saved. :)

Wim Leers's picture

Wim Leers

You’ve set input to ‘Selected text’, while it should have been left at the default, which is ‘None’. I’ve clarified that in the article now. That should fix it for you.

emjayess's picture

emjayess

Can’t seem to get it working… the tooltip I get is: /bin/bash: -c: line 2: syntax error: unexpected end of file

I’m new to TextMate, but I’ve triple-checked the command settings… thoughts?

Wim Leers's picture

Wim Leers

Please replace the entered command with just this and report back what it echoes.

FILENAME=${TM_FILEPATH##/} BASENAME=${FILENAME%%.}; echo "file BASENAME in path FILENAME"

emjayess's picture

emjayess

/bin/bash: line 1: echo: command not found

can’t find echo?!

gah! why me? heh. I have to admit, I don’t know my way around TextMate very well yet… but I did confirm that other, pre-existing Bundle commands were working.

I saw this tip and got excited because just the other week, I spent some time ramping up a local development environment for projects, including setting up the Drupal API docs locally as per this tip: http://drupal.org/node/26669

…which would be really sweet if I could pair it up with this great tip of yours… but for some reason TextMate doesn’t like me.

Wim Leers's picture

Wim Leers

Well, I’m afraid this is beyond my very basic knowledge of bash then… You could try #textmate on irc.freenode.net, there are very nice people there who’re always willing to help.

Wim Leers's picture

Wim Leers

Cool, I was not aware of that group. Thanks! :)

Niklas Bivald's picture

Niklas Bivald

The correct URL nowadays is http://api.drupal.org/apis/ not http://api.drupal.org/apis/5/

Therefore, the code should be:

if grep <<<${TM_CURRENT_WORD:-!} -Esq '^[a-zA-Z0-9_]+$' then FILENAME=${TM_FILEPATH##/} BASENAME=${FILENAME%%.};

if [ echo "$TM_CURRENT_WORD" | grep -E "${BASENAME}_[0-9A-Za-z_]" ] then HOOK=${TM_CURRENT_WORD##${BASENAME}} URL=”http://api.drupal.org/apis/hook${HOOK}” else URL=”http://api.drupal.org/apis/${TM_CURRENT_WORD}” fi exit_show_html “” else echo “Nothing to lookup (hint: place the caret on a function name)” fi