Drupal syntax highlighting

Last modified: May 17, 2009 - 02:27

Note this information is adapted from the Übercart web site with permission.

With all of Drupal's myriad functions, it can be nice to be able to differentiate between them and PHP's functions. jEdit allows users to write and modify the XML files that define its syntax highlighting to achieve this.

First off, download one for Drupal 5.x or generate (see below) the XML file and put it in jEdit's 'modes' directory. Next tell jEdit about it. Open up the 'catalog' file in the 'modes' directory. Between any two <MODE> tags insert the following:

<MODE NAME="drupal" FILE="drupal.xml" />

You can put it after "doxygen" to maintain alphabetical order.

Next open up php.xml from the same directory (preferably in jEdit, it'll catch mistakes with ErrorList). In it you'll find a section that is a list of all the keywords in the PHP language. Right before that section, before the <KEYWORDS> tag, insert the following code:

<IMPORT DELEGATE="drupal::MAIN"/>

With all the files saved, jEdit should highlight Drupal functions like theme() and db_query() as KEYWORD4. The appearance of KEYWORD4 can be changed under Utilities >> Global Options >> Syntax Highlighting. By default PHP mode doesn't use KEYWORD4, so there should be no conflicts.

Updated XML files for Drupal can be generated in several ways. One is to enable all the core Drupal modules on a clean install and execute the following code in a PHP-format node:

if ($file = fopen("files/drupal.xml", 'w')){
  fwrite($file, "<?xml version=\"1.0\"?>

<!DOCTYPE MODE SYSTEM \"xmode.dtd\">

<MODE>
  <RULES>
    <KEYWORDS>
      <!-- Drupal functions -->
");
  $functions = get_defined_functions();
  foreach($functions['user'] as $func){
    fwrite($file, '      <KEYWORD4>'. $func. "</KEYWORD4>\n");
  }
  fwrite($file, "    </KEYWORDS>
  </RULES>
</MODE>");
fclose($file);
}

One thing users might want to do is add the FILE_NAME_GLOB attribute to the catalog entry, like this:

<MODE NAME="drupal" FILE="drupal.xml" FILE_NAME_GLOB="*.{module,inc,info}"/>

so Jedit can apply the syntax highlighting to files with Drupal extensions only.

 
 

Drupal is a registered trademark of Dries Buytaert.