Tue 09 Jun 2009 11:41:19 AM UTC, original submission:
I see that this topic was already discussed a few times, but it's
still unsatisfactory, and I have improved regexes (bottom).
The problem is that Python multiline strings have the same start and
end delimiters: """...""" and '''...'''
And nano's multi-line coloring logic (src/winio.c:2579) is optimized
for things like C /.../ comments where they differ:
- Nano starts from current line and goes backward searching for the
last start/end.
But if start==end, you can't know if the last match is a start or
an end - the only reliable way is to parse from the start of the file!
(An optimization might still be possible but requires relying on
previous coloring and knowing which parts of the file changed.
Sounds complicated to get right.)
- Nano ignores repeated occurences of start until it sees the end.
This is ok for /.../...*/ but not for """..."""...""".
Perhaps the algorithm can be slightly tweaked to consider something
that matches both start and end as an end?
This could slightly improve some corner cases. But in any case start
and end must differ for multi-line coloring to work.
The current Python string syntaxes are:
color brightgreen "['][^'][^\\][']" "[']{3}.[^\\][']{3}"
color brightgreen "["][^"][^\\]["]" "["]{3}.[^\\]["]{3}"
color brightgreen start=""""[^"]" end=""""" start="'''[^']" end="'''"
This takes the approximation that """ only starts a string if it's not
at the end of the line.
It does cover:
"""Single line"""
and:
"""Compact two
lines"""
but there is absolutely no way to color this string:
"""
Separate lines.
"""
OK, so it won't be colored. What's most important is that string
coloring never spills over to the rest of the file.
Unfortunately, the current regex is too subtle. For example adding a
single space after the closing """ will
turn it into a start match, causing the dreaded spillover.
Here are better regexes:
color magenta "['][^']*[^\\][']"
color magenta "["][^"]*[^\\]["]"
color magenta start="["]{3}[ \t][^ \t"]" end="["]{3}[ \t]"
color magenta start="[']{3}[ \t][^ \t']" end="[']{3}[ \t]"
These consider """ as a start only if it's followed by any
non-whitespace on the same line.
P.S. it appears that \t doesn't work and has to be replaced with a
literal tab in all occurences.
That's a pity, now I have to use M-P to edit nanorc ;-(.
|