Tuesday, March 22, 2011

Geany Customizing Syntax Highlight

Some time ago I spent most of an afternoon figuring out how to customize the Geany editor to recognize Z80 symbols in its assembly-language syntax highlighting. Most or all of this may be covered in the Geany documentation - there may be better ways to do this but here is the recap anyway.

The problem with trying to beautify assembly language is all the variations in the "syntax", so it was no surprise that I found nothing off the shelf that would display my Z80 source code just the way I wanted it. However, after looking over a bunch of editors and IDEs available under Linux, I found that Geany provided almost exactly what I needed. It offers just a little bit of IDE functionality over the plain text editor, and the syntax highlighting is easily modified by way of configuration files and requires (almost - see below) no modification to the source code. Configuration files for all the supported programming languages are found in "/usr/local/share/geany". Getting my z80 source files to look the way I wanted was just a matter of making a few small modifications to "filetypes.asm", adding the z80-specific registers, instructions, and directives specific to the asxxx assembler I am using (I should write about asxxx another time, it is pretty awesome).

Here are the instructions I added for Z80:
ld inc and jr rst ex call xor djnz bit jp or rrca dec set res cp lddr scf

And here are the Z80 registers:
a d e b c h l hl bc de

And finally, adding a couple of additional directives for my assembler, e.g. ".org .include"

... the result is a "keywords" section that looks like the following, where I leave in the existing x86 symbols or whatever was already in there:

[keywords]
# all items must be in one line
# this is by default a very simple instruction set; not of Intel or so
instructions=hlt lad spi add sub mul div jmp jez jgz jlz swap jsr ret pushac popac addst subst mulst divst lsa lds push pop cli ldi ink lia dek ldx ld inc and jr rst ex call xor djnz bit jp or rrca dec set res cp lddr scf
registers=a d e b c h l hl bc de
directives=org list nolist page equivalent word text .org .include


Finally, I came up with a change in the actual C++ code that would allow recognition of the '$' (dollar sign) hex format still used by some assemblers. (Geany already recognized '0x' as the hex designator, which is the convention of C source code).

These changes are specific to version 1.18.1, I haven't bothered to upgrade.

On line 152 of LexAsm.cxx, change...

} else if (isascii(sc.ch) && (isdigit(sc.ch) || (sc.ch == '.' && isascii(sc.chNext) && isdigit(sc.chNext)))) {
to...
} else if ( isascii(sc.ch) &&
( isdigit(sc.ch) || (sc.ch == '.' && isascii(sc.chNext) && isdigit(sc.chNext)) ||
(sc.ch == '$' && isascii(sc.chNext) /* && isdigit(sc.chNext) */) ) ) {

That's all folks!

No comments:

Post a Comment