shell-writing (syntax hilighting) advice?

Judah Jacobson judah.jacobson at gmail.com
Sun Jun 14 15:17:21 EDT 2009


On Sat, Jun 13, 2009 at 2:50 PM, Isaac
Dupree<ml at isaac.cedarswampstudios.org> wrote:
> I'm trying to write a shell-style thing (actually a wrapper around bash)
> that gives syntax highlighting for the line being edited, and also
> displays more info.  For example, if I type "for" and pause, the "for"
> could turn red because it'd be a syntax error, and also display some
> helpful info like:
>
> $ for
> for NAME [in WORDS ...]; do COMMANDS; done
> for (( EXPR1 ; EXPR2 ; EXPR3 )) ; do COMMANDS ; done
>
> I found out that Readline has an "event hook" that I could at least use
> to get code's control every time a character is typed (and at other
> intervals).  But is it safe/possible to just write colored characters
> over the uncolored ones?  (Haskeline, Readline, I don't really care what
> library I use, as long as I can do what I want without re-implementing a
> whole line editing system!)
>
> Hmm, I think I'll stick to that "syntax hilighting" question for a
> start, rather than trying to ask everything at once.

Interesting idea!  You may be able to do this with readline.  Probably
your biggest challenge is that when the user types a character in the
middle of the line, the contents to the right of the cursor have to be
re-printed since they're shifted by one.  So you'll need to keep track
the full line state using getLineBuffer and getPoint; after every user
input, check whether the line buffer has changed and overwrite any new
characters.

There's no real way to do what you want with Haskeline yet, because it
doesn't export such low-level APIs.  However, the library does have an
internal abstraction of backends (the RunTerm type) which might be
useful; let me know if you'd like pointers for hacking on that.

Hope that helps,
-Judah



More information about the Haskeline mailing list