1

I was wondering if anyone could tell me if it possible to configure syntax highlighting in .nanorc so that in the following text ...

Dim s$ = "foo ' bar" ' bar "foo"

... the string "foo 'bar" is all highlighted one colour and the trailing comment ' bar "foo" is highlighted another.

If I do this:

# Comments.
color yellow "'.*"

# Strings
color brightmagenta ""(\\.|[^"])*""

Then I get this:

Image1

And if I do it in the other order:

# Strings
color brightmagenta ""(\\.|[^"])*""

# Comments.
color yellow "'.*"

Then I get this:

Image2

1

1 Answer 1

1

If the comment does not contain a ', that is to say that the last ' in the line is the beginning of any comment then it would seem to be best to work backwards from the end of the line, with a non-greedy search.

Try this, which uses your strings term but with a different comments term:

# Strings
color brightmagenta ""(\\.|[^"])*""

# Comments.
color green "[^']*$"

This works, except that the comment mark (') is not coloured, unfortunately:

Screenshot of almost the desired result

Adding in an additional ', like so:

# Strings
color brightmagenta ""(\\.|[^"])*""

# Comments.
color green "'[^']*$"

finally gives the desired result:

Screenshot of the desired result

Note: The alternative to the non-greedy [^']* is '.*?, which is what I first tried to use, see below, but it failed...


Lazy (non-greedy) *? causes an error

My first thought was to use .*?, which would also colour the comment marker ('), like so:

# Comments.

color green "'.*?$"

Logically, this non-greedy search from the end should also work for your comments.

However, nano throws an error:

Bad regex "'.*?$": repetition-operator operand invalid

It might be a bug, see Bad regex in php.nanorc, sh.nanorc & zsh.nanorc using nano 2.3.6 on Mac OS X from brew #41 which was supposedly fixed:

Answer: POSIX regex doesn't support ? for laziness (or laziness at all). Removing the ?s from the offending lines got rid of the errors, but you may not want to do that here since it will change evaluation for everyone else.

Note: I got this error when running nano on OS X - I'm not sure if that is relevant.

4
  • Thanks for trying, but it just seems to add a different problem - i.e. the non-highlighted ' and the incorrect handling if the comment itself contains a ' character; it gets even worse if you want to handle REM comments at the same time (yes, this is BASIC). The other option, '.*?$ doesn't report an error in Nano 4.8, but seems to behave exactly like '.* Nov 7, 2021 at 13:09
  • But the comment marker' is highlighted in green in the second example. As for the ' in a comment, it is totally doable: See Remove comments from file while ignoring quoted comment signs and How can I remove all comments from a file?... I just haven't quite finished yet figuring it out. The REM would be possible too, with a little extra work (but neither were mentioned in your question). Ah, just realised that I am using nano 2.0.6, so that probably explains why I keep on getting errors. Nov 7, 2021 at 13:42
  • Hi, thanks for your answer and sorry for going dark. I think my initial comment may have been response to the suggestion from the original version of your post and not the later edit. Your color green "'[^']*$" does work except it introduces the alternative problem of the highlighting misbehaving if the comment contains a ' character - which I didn't mention in my original question because it wasn't an issue with my own attempts. Likewise I didn't mention REM in my initial question because it is a lesser use-case and doesn't matter ' can't be handled correctly. Nov 22, 2021 at 14:16
  • No worries. I'll see if I can get a later version of nano and try to get the *? to work - I think it should do, looking at the other .rcsc files for nano, which seem to use it (IIRC). Nov 22, 2021 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.