Add new comment

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