function textmate_detect_drupal_theme()
{
    if (isset($_SERVER['TM_DIRECTORY'])) {
        // Try to find the .info file of the theme.
        $path = explode('/', $_SERVER['TM_DIRECTORY']);
        while (!empty($path) && empty($info_file)) {
            $info_file = textmate_scan_directory($_SERVER['TM_DIRECTORY'], '/\\.info$/', array('recurse' => FALSE));
        }
        $info = array_pop($info_file);
        return $info->name;
    }
}
function textmate_scan_directory($dir, $mask, $options = array(), $depth = 0)
{
    // Merge in defaults.
    $options += array('nomask' => '/(\\.\\.?|CVS)$/', 'callback' => 0, 'recurse' => TRUE, 'key' => 'filepath', 'min_depth' => 0);
    $options['key'] = in_array($options['key'], array('filepath', 'filename', 'name')) ? $options['key'] : 'filepath';
    $files = array();
    if (is_dir($dir) && ($handle = opendir($dir))) {
        while (FALSE !== ($filename = readdir($handle))) {
            if (!preg_match($options['nomask'], $filename) && $filename[0] != '.') {
                $filepath = "{$dir}/{$filename}";
                if (is_dir($filepath) && $options['recurse']) {
                    // Give priority to files in this folder by merging them in after any subdirectory files.
                    $files = array_merge(textmate_scan_directory($filepath, $mask, $options, $depth + 1), $files);
                } elseif ($depth >= $options['min_depth'] && preg_match($mask, $filename)) {
                    // Always use this match over anything already set in $files with the
                    // same $$options['key'].
                    $file = (object) array('filepath' => $filepath, 'filename' => $filename, 'name' => pathinfo($filename, PATHINFO_FILENAME));
                    $key = $options['key'];
                    $files[$file->{$key}] = $file;
                    if ($options['callback']) {
                        $options['callback']($filepath);
                    }
                }
            }
        }
        closedir($handle);
    }
    return $files;
}