Adding Removing Keywords - GeSHi/geshi-1.0 GitHub Wiki

Lets say that you’re working on a large project, with many files, many classes and many functions. Perhaps also you have the source code on the web and highlighted by GeSHi, perhaps as a front end to CVS, as a learning tool, something to refer to, whatever. Well, why not highlight the names of the functions and classes your project uses, as well as the standard functions and classes? Or perhaps you’re not interested in highlighting certain functions, and would like to remove them? Or maybe you don’t mind if an entire function group goes west in the interest of speed? GeSHi can handle all of this!

Adding a Keyword

If you want to add a keyword to an existing keyword group, you use the add_keyword method:

$geshi->add_keyword($key, $word);

Where $key is the index of the group of keywords you want to add this keyword to, and $word is the word to add.

This implies knowledge of the language file to know the correct index.

Removing a Keyword

Perhaps you want to remove a keyword from an existing group. Maybe you don’t use it and want to save yourself some time. Whatever the reason, you can remove it using the remove_keyword method:

$geshi->remove_keyword($key, $word);

Where $key is the index of the group of keywords that you want to remove this keyword from, and $word is the word to remove.

This implies knowledge of the language file to know the correct index - most of the time the keywords you’ll want to remove will be in group 3, but this is not guaranteed and you should check the language file first.

This function is silent - if the keyword is not in the group you specified, nothing awful will happen ;)

Adding a Keyword Group

Lets say for your big project you have several main functions and classes that you’d like highlighted. Why not add them as their own group instead of having them highlighted the same way as other keywords? Then you can make them stand out, and people can instantly see which functions and classes are user defined or inbuilt. Furthermore, you could set the URL for this group to point at the API documentation of your project.

You add a keyword group by using the add_keyword_group method:

$geshi->add_keyword_group($key, $styles, $case_sensitive, $words);

Where $key is the key that you want to use to refer to this group, $styles is the styles that you want to use to style this group, $case_sensitive is true or false depending on whether you want this group of keywords to be case sensitive or not and $words is an array of words (or a string) of which words to add to this group. For example:

$geshi->add_keyword_group(10, 'color: #600000;', false, array('myfunc_1', 'myfunc_2', 'myfunc_3'));

Adds a keyword group referenced by index 10, of which all keywords in the group will be dark red, each keyword can be in any case and which contains the keywords “myfunc_1”, “myfunc_2” and “myfunc_3”.

After creating such a keyword group, you may call other GeSHi methods on it, just as you would for any other keyword group.

:exclamation: Caution:

If you specify a $key for which there is already a keyword group, the old keyword group will be overwritten! Most language files don’t use numbers larger than 5, so I recommend you play it safe and use a number like 10 or 42.

Removing a Keyword Group

Perhaps you really need speed? Why not just remove an entire keyword group? GeSHi won’t have to loop through each keyword checking for its existance, saving much time. You remove a keyword group by using the remove_keyword_group method:

$geshi->remove_keyword_group($key);

Where $key is the key of the group you wish to remove. This implies knowleged of the language file.