The other day, I wrote about Fuco1’s efforts to add some context awareness to Emacs font-locking. Now he’s back with a new font-locking problem. This time, he wants to highlight interpolated variables in quoted strings in shell code. Those of you familiar with the Unix way will recall that there are two situations: variables will be interpolated in double-quoted strings but not in single-quoted strings. Fuco1 wants to distinguish the two cases by highlighting the first case but not the second. Thus we want
Foo = "bar" String1 = "We want highlighting for $Foo in this string." String2 = 'But no highlighting for $Foo is this string.'
This is another case where the font-locking has to be context aware: we want it in a double-quoted string but not in a single-quoted string so the context of where the interpolated variable appears matters.
As Fuco1 said in his original post, you can substitute a function for the normal regular expression controlling font-locking as long as the function has the same interface and returns as re-search-forward
. Check out Fuco1’s post for how he solved the problem. If you, like Fuco1, have a refined sense of style in such matters, you can install his code and get his results yourself.