I\'m getting burned repeatedly by unmatched parentheses while writing python code in vim. I like how they\'re handled for C code - vim highlights in red all of the curly br
Not sure if it'll be more or less confusing for you, but you could look at the lisp.vim
syntax file (especially the part where g:lisp_rainbow
is handled) to see how you can highlight matching parens.
If you manage to highlight all the matching parens, you could have the leftover parens (i.e. unmatched parens) have default Error highlighting. This is what the lisp file seems to be doing.
syn match parenError ")"
syn region matchingParens transparent start="(" end=")" contains=matchingParens
hi parenError guifg=red
If you :syn clear
and run those, it seems to work. Note that the order the syn
commands are executed matters. Per :h :syn-priority
, the rule matched last is the one that takes effect, which may be why your rules highlighted all the end-parens in the file.
What c.vim is actually doing is highlighting any {}
inside of ()
, whether everything is properly closed or not. Try typing ({})
in C mode, it still highlights the {}
as an error.
I don't think this approach can be used to test directly for a (
with an unmatched )
, because :syn region
doesn't care whether the end-pattern is there or not.
So you have to find something Python-specific that should never belong inside ()
. Then match against "(\_[^)]*the_forbidden_something"
. I don't know Python enough to know what that might be.
If nothing else, you can do:
syn match openParen "(\_[^)]*\%$"
which matches an open paren with no closing parens before the end-of-file. This fails if it finds any closing paren at all, which means it won't even catch (()
.