problem with searchpair()

4 views
Skip to first unread message

A.Politz

unread,
Aug 25, 2007, 11:40:25 AM8/25/07
to v...@vim.org
--------%<------------------
//source this

fun! SPTest()
call searchpair('(','',')','bcW','Skip()')
// ^
endfun
func! Skip()
let res= synIDattr(synID(line('.'), col('.'), 0),'name') =~?
'string\|comment\|character'
echo col('.').'|'.res
return res
endfun

finish
void fun( /*)*/text())
// fp^ ^

// vim:ft=c
--------%<------------------


I don't understand the behaviour of this code. When the cursor is on 'text'
and I call SPTest() the cursor does not move. But if I remove the 'c'
flag from
searchpair(), it does so to the first paren.

As I understand it, the 'c' flag means : A match at the cursorposition
satisfies
the searchcondition. If there is no such match, act like w/o the 'c' flag.

So I would expect that this code, with the cursor on `text'
would do exactly the same with or without 'c'.

What's the problem ?

-ap

A.Politz

unread,
Aug 26, 2007, 3:13:54 PM8/26/07
to vim...@googlegroups.com
A.Politz wrote:

>--------%<------------------


>"source this
>
>fun! SPTest()
> call searchpair('(','',')','bcW','Skip()')

> " ^
>endfun
>func! Skip()
> let res= synIDattr(synID(line('.'), col('.'), 0),'name') =~?
>'string\|comment\|character'
> echo col('.').'|'.res
> return res
>endfun
>
>finish
>void fun( /*)*/text())

>" fp^ ^

Antony Scriven

unread,
Aug 28, 2007, 4:44:20 AM8/28/07
to vim...@googlegroups.com

searchpair() searches backwards until it hits the paren in
/*)*/. It skips it (because of Skip()) and then starts
searching again at that point. The `c' in your flags causes
it to match at the same position again; and because the
cursor hasn't moved since the first match searchpair() bails
out. I would have expected it to carry on searching too. --Antony

A.Politz

unread,
Aug 28, 2007, 8:06:36 AM8/28/07
to vim...@googlegroups.com
Antony Scriven wrote:

In other words, a skipped match is still a match.
Thanks Antony.

It works the same in the most simple case :

f( g() )
^p1 p2^

On position p1:
call searchpair('(','',')','c') does not move the cursor.
On position p2:
call searchpair('(','',')','cb') does not move the cursor.


-ap

Antony Scriven

unread,
Aug 30, 2007, 5:41:30 AM8/30/07
to vim...@googlegroups.com

Yes, searchpair() searches, moves the cursor if a match is
found, and then checks the skip function. However I wonder
if you have found a bug. I reckon that `c' in the flags
should only affect the initial cursor position. I have
neither source code nor compiler to hand, but maybe removing
`c' from the flags sometime after searchpair() first calls
searchit() would fix that. --Antony

A.Politz

unread,
Sep 4, 2007, 4:38:49 AM9/4/07
to vim...@googlegroups.com

There is another `problem`.
The 'r' flag in searchpos() searches for the outermost
match, but this searches forever if 'wrapscan' is
set and the 'W' flag is not given. Since this is pretty
much useless in any case, 'r' therefore should imply 'W'.

This is the behaviour at least in this case :

:set wrapscan

f(a,b,c,d)

Cursor on a comma and

call searchpair('(',',',')','r')

-ap

Reply all
Reply to author
Forward
0 new messages