Syntax Tables in Emacs
I wanted to write a simple function in elisp that would find the comment characters used in the current major mode. It is part of an effort to create a simple literate programming tool for Emacs (aka Slipcore – initial version to be posted soon). The obvious way to do this is to leverage the current major mode’s syntax table and troll through it for comment-significant characters.
Emacs keeps track not only of the character class, but also several character flags (this stuff is described in the elisp manual’s section on syntax descriptors). There are no lisp functions to directly access this information, but in section 35.8 on syntax table internals, it mentions that the higher order bits (16+) encode these 6 flags. It turns out that the syntax table is in fact a vector, so using (elt (syntax-table) 53) where 53 is the ASCII code for the character number I care about, I can access the integer representation of the character class and associated flags.
As it turns out, using this method to determine comment characters doesn’t really work all that well. There are a handful of languages that will simply break because they don’t use the model of commenting I’m assuming when I look at the syntax flags. After I posted about this in comp.emacs, I received a nice reply explaining why, despite my notion that this would be a nice general approach to accessing comment syntax, it was in fact kind of misguided.
I am now using a simple alist matching major mode string to a single-line comment character. I don’t know if it will stay that way, but it works for basic testing.
You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.