Get PHP Documentation in TextMate

Thursday, September 21st, 2006 at 23:14 Thursday, September 21st, 2006 at 23:14

I've been using TextMate a lot recently. It's completely replaced TextWrangler as my editor of choice, for a lot of reasons: better syntax coloring, more extensible, more customizable and insane Unix integration. You can create commands, which are code snippets in any language you can run through the terminal, and tie them to keyboard shortcuts. This is one that I created to pull up documentation from php.net for whatever function name or keyword I have selected. It is very loosely based on code from Jeff Gould.

To use this, first copy the code below. You'll want the un-styled version, so click on the "view plain text" link just above it.

  1. #!/usr/bin/php
  2. <?php
  3.  
  4. ob_start();
  5. $func = preg_replace('/[() ;]/', '', $_ENV['TM_CURRENT_WORD']);
  6. $url = 'http://us2.php.net/manual/en/function.' . str_replace('_', '-', $func) . '.php';
  7.  
  8. $ch = curl_init($url);
  9. curl_setopt($ch, CURLOPT_HEADER, 1);
  10. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  11. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-Language: en-gb, en"));
  12.  
  13. $res = curl_exec($ch);
  14. $error = curl_error($ch);
  15. curl_close($ch);
  16.  
  17. $data = ($res) ? substr(strstr($res, "rnrn"), 4) : "Curl error: " . $error;
  18.  
  19. $data = substr($data, strpos($data, 'Swedish'));
  20. $data = substr($data, strpos($data, '<!--/UdmComment-->'));
  21. $data = substr($data, 0, strpos(strtolower($data), '<div id="usernotes">'));
  22. echo $data;
  23.  
  24. $output = ob_get_contents();
  25. ob_end_clean();
  26.  
  27. echo (strlen($output) > strlen($func)) ? $output : "Error: function " . $func . "() not found.";
  28. ?>
#!/usr/bin/php
<?php

ob_start();
$func = preg_replace('/[() ;]/', '',  $_ENV['TM_CURRENT_WORD']);
$url = 'http://us2.php.net/manual/en/function.' . str_replace('_', '-', $func) . '.php';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-Language: en-gb, en"));

$res = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);

$data = ($res) ? substr(strstr($res, "rnrn"), 4) : "Curl error: " . $error;

$data = substr($data, strpos($data, 'Swedish'));
$data = substr($data, strpos($data, '<!--/UdmComment-->'));
$data = substr($data, 0, strpos(strtolower($data), '<div id="usernotes">'));
echo $data;

$output = ob_get_contents();
ob_end_clean();

echo (strlen($output) > strlen($func)) ? $output : "Error: function " . $func . "() not found.";
?>

Next, open TextMate and go to the Bundle Editor. Create a new command under PHP and use these settings:

Settings for this command

Paste the code into the "Command(s)" text area. Use whatever activation method you like; I use ctrl-H. Now, anytime you are editing PHP code and your text cursor is in, or next to, a keyword, just activate this command to pull up a formatted version of the documentation in a new window.

Comments

beardog:Friday, September 22nd, 2006 at 9:53 #1

Great post; I love to see new TextMate techniques online. I followed the instructions exactly but when I tried calling it the new window always popped up with, "Error: function ___() not found." I changed the Scope Selector to "text.source.php" and it works perfectly. Might have simply been a problem with my machine but I wanted to put that out there just in case. Handy feature.

Ross:Friday, September 22nd, 2006 at 11:25 #2

I think the problem might have been that there was already something assigned to ctrl-H under the source.php scope. I probably removed the keyboard shortcut from the conflicting command earlier without realizing it. If I change it to text.source.php, hitting ctrl-H in a PHP block actually brings up the HTML documentation instead.

By the way, look for more TextMate posts in the future. I have a feeling I'm going to writing more scripts like this one.

beardog:Friday, September 22nd, 2006 at 11:59 #3

Haha, I see I see. I wasn't aware of the existing documentation functionality. I was tickled pink simply to discover that feature. For some reason it still gives me the error message with "source.php" but I'll give it a closer look. Thanks for the clarification.

Vaska:Friday, September 22nd, 2006 at 17:58 #4

Very nice. ;)

Comments Disabled


Recent Posts

Navigation