Saturday 14 December 2019

Geany Syntax Highlighter for Gambas Script

Gambas scripts can be written using any text editor in 'black & white'.


But I'm so used to seeing my code in colour that black text seems to slow down my comprehension.


Although this post shows how to add colour for your Gambas script files in Geany, it may be more widely useful for those trying to add syntax highlighting for other languages to the Geany code editor.

As discussed in my last post Gambas Scripts may be useful on systems that do not include a display (i.e. headless) or the graphical components needed to 'VNC' into the system. One example is a Raspberry Pi configured as an Internet Radio. The ability to be able to simply edit the target program in a text editor and then run it, may help in program development and code maintenance.

the Geany editor


Geany is a great text and software editor. It is lightweight, has built-in support for over 60 software scripts/languages/formats, runs on Linux and has also been ported to other operating systems.

Although you will find information on their website regarding how to add additional language support, I'm certainly not alone in finding this process tricky. Hopefully this post will help a few people also struggling to do this.

When a file is opened with a supported filetype, Geany displays syntax highlighting;

geany editor colour highlighting python


This is triggered by the file extension, in this case: .py

On Linux, the built-in filetypes are configured by the files here:-

 /usr/share/geany/filedefs

The easiest way to add support for another language, is to copy a similar filetype file to the filedefs folder within your home directory. For example:-

 /home/steve/.config/geany/filedefs

Since Gambas is similar to BASIC I have copied the filetypes.freebasic file.

The first important point is that although the built in filetype definition files have a hotch-potch of lower-case and title-case file names, any custom filetype names must be in title-case. So the newly copied file name needs to be changed to something like this:-

filetypes.Gambas.conf

From within Geany, go to menu Tools > Configuration Files > filetype_extensions.conf and edit this file to add the new language and indicate one or more associated file extensions. For example, for Gambas script under [Extensions]:-

Gambas=*.gbs;

...and as this is a scripting language, under [Groups]:-

Script=Graphviz;Gambas;


With any luck, after you have saved these changes and used menu Tools > Reload Configuration you should see the new language in menu Tools > Configuration > Filetype Configuration > Scripting Languages {or whichever Group you used}

However, if you now open a *.gbs file the text will still be in plain black & white.

In Geany go to menu Tools > Configuration > Filetype Configuration > Scripting Languages and open the new configuration file, in this case  filetypes.Gambas.conf

Edit the [settings] section like this:-

[settings]
# default extension used when saving files
extension=gbs
lexer_filetype=FreeBasic


This is the second important point; you must use the correct case for the lexer_filetype, which in this case is: FreeBasic
...and the only way of knowing what is the correct case is to look at the Geany source code:-

  https://github.com/geany/geany/blob/1.23.1/src/filetypes.c#L175

With this change made, any Gambas script file opened in Geany with a .gbs extension should now show BASIC syntax highlighting. So its now just a case of re-opening filetypes.Gambas.conf and editing the [keywords] section...
   ...this could take some time!

geany editor syntax highlighting gambas
even with minimal keyword edits, it is so much better than black text on white



No comments:

Post a Comment