I'm trying to typeset SuperCollider code using the listings
package.
Any "freestanding" identifiers that begin with a capital letter are class names, and should be highlighted; by "freestanding", I mean outside of strings or comments. I would be tickled pink if listings
could let you define identifiers in terms of regexp, e.g. [A-Z][A-Za-z0-9_]*
but the manual doesn't suggests that as a possibility.
SuperCollider has some 2300 classes… I really don't relish listing all of them literally in my preamble. I guess I could extract them by hand for each individual listing, but I'd rather not have to.
Is there a way to do this without a really massive morekeywords
expression? (Perhaps using another package?)
Sample SuperCollider code:
p.clear;
~grains.addSpec(\tfreq, [1, 40, \exp]);
~grains.addSpec(\overlap, [0.1, 10, \exp]);
~grains.addSpec(\pos, [0, b.duration]); // 3.43 is nice!
~grains.addSpec(\rate, [0.5, 2, \exp]);
~grains = { |tfreq = 25, overlap = 6, pan = 0, amp = 0.2, pos = 3.43,
rate = 1|
var trig = Impulse.ar(tfreq);
TGrains.ar(2, trig, b, rate, pos, overlap / tfreq, pan, amp)
};
~grains.play;
Impulse
and TGrains
should be highlighted in blue. I have dozens of other listings using different capitalized keywords.
Best Answer
Edit: see https://tex.stackexchange.com/a/166159/21891 for a more complete
listings
language definition for the SuperCollider language.Syntax highlighting of identifiers by regexp is not a feature of
listings
, but it is possible to parse identifiers in order to check whether they pass a certain test and then conditionally apply a style to them.In the code below, all identifiers of the form
[A-Z][A-Za-z0-9_]*
are highlighted in a user-defined "class style". The class style below consists in boldface, green colour. You can customize the way classes are typeset by passing the appropriate declarations (e.g.\bfseries\color{red}
) to theclassstyle
key, which I've defined for convenience.If everything works as expected, you shouldn't have to specify your SuperCollider classes one by one anymore. Happy days :) Also, you can still define keywords starting by an uppercase letter; the keyword style will override the class style for those keywords.
References: This solution combines ideas laid out in Marco Daniel's answer, and David Carlisle's answer. The SuperCollider sample I used for this example is adapted from there.