Search code examples
perlcurses

Syntax Highlighting using Curses::UI::TextEditor


I'm just working on my first Curses-based app in Perl using Curses::UI. I'm wondering if it's possible to colour specific words in the TextEditor (not the entire thing using -fg). This would be used for syntax highlighting.

Cheers!

Brad


Solution

  • It is possible, but would probably require you to extend or subclass Curses::UI::TextEditor. This is because the TextEditor class appears to render its contents line by line.

    I did a tiny bit of fiddling with the draw_text method of Curses::UI::TextEditor to prove to my self it could be done:

    sub draw_text(;$)
    {
        # ...
        for my $id (0 .. $this->canvasheight - 1)
        {
            my $line_number = sprintf( "%04d", $id );
            $this->{-canvasscr}->attron( A_REVERSE );
            $this->{-canvasscr}->addstr( $line_number );
            $this->{-canvasscr}->attroff( A_REVERSE );
        # ...
    

    This places a reverse color block with the current line number on each line. Course, it doesn't place it at the front of the line like I expected, but I only had about 10 minutes to play. You might have better results with a little more time.

    So. Possible? Yes. Easy? Maybe not. There's still the issue of tokenizing your editor contents for highlighting.