Sublime Forum

Column matching for syntax highlighting?

#1

I’m planning to use ST to edit (mostly) “punch-card FORTRAN” using The TextMate FORTRAN bundle. I’m almost happy with this, but there’s a disconnect between this strictly column-oriented language and regexp-based expression matching. I would like to highlight

  • Line continuation characters (any character in column 6 in a non-comment line); and

  • Line sequence numbers (in columns 73-80)

Vim offers the c**%n** to match column n. I had been hoping to accomplish something like this using look-behinds:

(?<=^.{72})\S

but this doesn’t work and makes ST angry: syntax highlighting stops working until I restart ST.

Any helpful ideas of how to accomplish this with Oniguruma regexp’s, or any chance of getting a feature implemented for this?

0 Likes

#2

I’m not sure where you’re using your regex, but if within a syntax file then you’ll need to entity-encode the less than < symbol:

(?&lt;=^.{72})\S

You might also try an explicit capture, although it’s not generally necessary:

(?&lt;=^.{72})(\S)
0 Likes

#3

Thanks for your quick response! I directly modified the XML, and yes, I did the entity encoding, just left it out in this post for clarity. There’s a very explicit error message when your XML isn’t well-formed.

I can try the explicit capture later today. I may also try directly matching the text to be marked rather than trying to define a begin and end match. I’ll report back!

0 Likes

#4

After that encouragement, I managed to implement sensible solutions; thanks again!

To share with other folks in a similar predicament, I solved the problem by matching the whole target rather than a “begin” and “end.” This also benefits from a simpler syntax in the definition. Here’s my solution for the junk beyond column 72:

<key>comment</key>
<string>FORTRAN sequence numbers past column 73</string>
<key>match</key>
<string>(?&lt;=^.{72})\s*\S.*?(?=\s*$)</string>
<key>name</key>
<string>comment.fortran</string>

As planned, I eat the 72 chars from the line’s beginning in a lookbehind on 72 characters. I don’t care about trailing blanks, so I swallow those with a lookahead expression.

Similar technique to highlight any nonblank in column 6 that’s not in a comment:

<key>comment</key>
<string>FORTRAN continuation char in column 6</string>
<key>match</key>
<string>(?&lt;=^.{5})\S</string>
<key>name</key>
<string>invalid.deprecated.fortran</string>
0 Likes