Saturday, November 19, 2011

Re: search for next occurence that is not commented out

Am 19.11.2011 13:27, schrieb Tim Chase:
> On 11/19/11 05:05, Eric Smith wrote:
>> How do I find the next occurance of a pattern which is not
>> preceeded in the same line by a comment token? The identity of
>> the comment token should be derived from the current
>> filetype.
>>
>> (Without rollng and mintaining my own recipes using \@<! )
>
> A couple ideas occur to me, but each involves merely automating
> the addition of "\@<!" or "\@!". I don't know where you're
> getting your comment leader, but my stock vim doesn't have the
> info specific to given files (just generic info in 'comments'
> which doesn't seem to adjust for file-type). For the below, I'm
> assuming you can access that information in the variable
> "b:comment_leader", but you'd have to adjust accordingly.
>
> The first way that came to mind was to map something that
> auto-inserted the comment-leader into a search pattern when you
> tried to search, saving you the trouble:
>
> :nnoremap <f4> /\%(^\s*<c-r>=escape(b:comment_leader, '*\./')<cr>.*\)\@<!
>
> which would start a forward-search, insert (assuming your
> comment_leader was "#")
>
> /\%(^\s*#.*\)\@<!
>
> whereupon you could begin your search query.
>
> The alternative would be to have something that added that to the
> beginning of an existing search, something like
>
> :nnoremap <f4> :let @/='\%(^\s*'.escape(b:comment_leader, '.').'.*\)\@<!'.@/<cr>
>
> So you could search for text, then hit <f4> and continue to use n/N to
> search forwards/backwards excluding the comment-leader.
>
> Nothing stops you from foolishly running the 2nd one multiple times
> and creating a uselessly redundant pattern.
>
> Hope this gives you some hacks to get closer to what you want.
>
> -tim

I think in place of b:comment_leader, you can (rather) almost count on
'cms'/'commentstring':

:let comment_leader = substitute(split(&cms, '%s')[0], '^\s*', '', '')
:let comlead_pat = escape(comment_leader, '\.*$^~[')

Not a complete solution, just one step further.

--
Andy

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: