how to sign the unmatched bracket?

1 view
Skip to first unread message

StarWing

unread,
Nov 22, 2008, 11:11:04 AM11/22/08
to vim_use
hi all.

maybe you had notice that, if you write ")()" in your vimrc, you will
find the first bracket turn to red to notice it's unmatched. it's made
like this:
syn region vimOperParen oneline matchgroup=vimOper start="(" end=")"
contains=@vimOperGroup
syn match vimOperError ")"

so, if you write )(), Vim will notice you the ')' is not matched. but
if you write ()), nothing will happen! because if something matched
the 'start' section of region, Vim will think the words next it are
all in this region. so ')' will matched vimOperParen even if it's not
matched!!

i want more. i want to sign all unmatched bracket, like ()) (() ))
(( etc. how can i do it?

Ben Schmidt

unread,
Nov 23, 2008, 5:26:47 PM11/23/08
to vim...@googlegroups.com
> maybe you had notice that, if you write ")()" in your vimrc, you will
> find the first bracket turn to red to notice it's unmatched. it's made
> like this:
> syn region vimOperParen oneline matchgroup=vimOper start="(" end=")"
> contains=@vimOperGroup
> syn match vimOperError ")"
>
> so, if you write )(), Vim will notice you the ')' is not matched.

Yep.

> but
> if you write ()), nothing will happen! because if something matched
> the 'start' section of region, Vim will think the words next it are
> all in this region. so ')' will matched vimOperParen even if it's not
> matched!!

Not for me. The final ) is highlighted as Error for me.

In what context were you trying it?

> i want more. i want to sign all unmatched bracket, like ()) (() ))
> (( etc. how can i do it?

The first and third work, because the closing brackets are the errors.

The others are hard. You can't really do it with syntax regions. For
good reason, Vim doesn't check for the end of the syntax region when it
finds the start: only the start is enough. This means you can see syntax
highlighting while you type, even before you end the region. But it
means the kind of highlighting you want is hard.

You could possibly do it using the :match command or matchadd()
function, with an appropriate regular expression. It may be slow, though
and I suspect it is not worth it. By developing suitable habits and
using matchparen (:help matchparen), showmatch (:help 'showmatch')
and/or matchit (:help matchit, :help matchit-install) you can easily
keep your brackets nicely matched.

Cheers,

Ben.

StarWing

unread,
Nov 23, 2008, 5:42:15 PM11/23/08
to vim_use
yes, i know. thank you very much for your introduction of three
scripts~~
Reply all
Reply to author
Forward
0 new messages