Komodo: Chess Grandmaster

Komodo chess program using Javascript macroThe macro system in Komodo allows for some creative possibilities. I've written a macro that provides the ability to replay chess games, utilizing the Komodo editor to display the board, game pieces and sequence of moves:

JavaScript macro source code here.

The sample game played by the macro recreates Game 1 of the historic first match between Deep Blue (a computer chess playing program written by IBM) and Garry Kasparov in 1996. This was the first time a computer program had beaten a chess grandmaster under standard chess tournament conditions, proving that computers could challenge the world's greatest human minds. Kasparov went on to win the match 4-2, though the following year, Deep Blue would take the rematch 3½–2½.


The Komodo macro system is extremely useful for programmatically interacting and controlling the various components inside of Komodo - the most common being the text editing widget (the Scintilla plugin, aka "scimoz").

You can run this macro in your Komodo editor by creating a new macro in your Komodo toolbox, then copying and pasting the macro source code into the macro content editor. After creating the macro, you can execute it by double-clicking it in your toolbox.

Note: If the macro fails to show the chess pieces, you'll need to ensure you are using a font that contains the chess piece glyphs, such as DejaVu Sans Mono.

This chess macro highlights the following Scintilla features:

  • unicode character handling
  • cursor and column positioning
  • editor text manipulation
  • indicators - highlighting text

Unicode character handling

The Scintilla editor always uses UTF-8 for its character encoding, which means all character positioning uses a flexible byte offset, as some unicode characters require multiple bytes while ASCII characters require just one byte. The JavaScript (or Python) macro environment uses UCS-2, which requires using 2 bytes per character. This encoding difference means you need to be aware of the positional difference when communicating between JavaScript and Scintilla, and appropriately convert the positions. Komodo provides the function ko.stringutils.bytelength(s), which you can use to calculate the UTF-8 byte length of a given string.

Note: If you're always using ASCII characters, you can mostly ignore this difference, as the lengths between the two will always be the same (ASCII character length == byte length).

Positioning and text manipulation

Here are some of the Scintilla API methods used in this macro:

  • scimoz.lineCount - how many lines are in the document
  • scimoz.positionFromLine(lineNo) - byte position at the start of the given line
  • scimoz.positionAfter(pos) - byte position for the next character
  • scimoz.addText(ko.stringutils.bytelength(line), line) - adding text at the current cursor position (scimoz.currentPos)
  • text = scimoz.getTextRange(pos1, pos2) - retrieving a piece of the text

Indicators - highlighting text

Scintilla indicators are used to colour the board (ie. the black and white chess squares), as well as to highlight the last piece moved. With Komodo, you have the ability to programmatically change the colour of indicators and set/clear the positions at which they occur. Here are some of the API methods being used to control the indicators:

  • scimoz.indicSetFore(indicNo, 0x000000) - setting the indicator color to black
  • scimoz.indicSetStyle(indicNo, scimoz.INDIC_ROUNDBOX) - rounded box around the text
  • scimoz.indicSetAlpha(indicNo, 40) - setting the transparency for the color
  • scimoz.indicatorCurrent = indicNo - sets which indicator is being referenced
  • scimoz.indicatorFillRange(pos, numbytes) - enable the indicator at the given position and length

All in all, Komodo macros are extremely powerful and can be used to enhance your editing prowess. Here are some other links to find out more about Komodo macros:

Example Komodo macro recipes - hosted on ActiveState's code site

JavaScript macro API

Komodo macro system

comments powered by Disqus