Sunday, July 8, 2012

Re: How to use matcharg(2)[1] to set search register?

On Jul 8, 6:44 pm, Gary Johnson <garyj...@spocom.com> wrote:
> On 2012-07-08, Bee wrote:
> > On Jul 8, 3:33 pm, Gary Johnson wrote:
> > > On 2012-06-24, Bee wrote:
> > > > On Jun 24, 10:32 am, Gary Johnson wrote:
> > > > > On 2012-06-24, Bee wrote:
> > > > > > 2match has been set and is working.
>
> > > > > > :echo matcharg(2)[1] returns the correct pattern.
>
> > > > > > Why does this not set the search register?
>
> > > > > > let @/=matcharg(2)[1]
>
> > > > > It works for me.
>
> > > > > Are you setting @/ within a function and expecting that setting to
> > > > > persist after the function returns? That won't work because
> > > > > functions use their own instances of @/.
>
> > > > > Regards,
> > > > > Gary
>
> > > > Thank you, I now see from :help "/
>
> > > > 9. Last search pattern register "/ *quote_/* *quote/*
> > > > ...
> > > > "Note that the valued is restored when returning from a function"
>
> > > > --And--
>
> > > > *function-search-undo*
> > > > The last used search pattern and the redo command "."
> > > > will not be changed by the function. This also
> > > > implies that the effect of |:nohlsearch| is undone
> > > > when the function returns.
>
> > > > BUT...
>
> > > > When the following function exits, the '/' register is still set as it
> > > > was in the function and 'n' will find the next occurance.
> > > > What should it have been restored to?
>
> > > > " toggle match end of long lines
> > > > hi OverLength NONE cterm=underline ctermbg=darkyellow
> > > > function! OverLength(n)
> > > > if matcharg(2)[0] == "OverLength"
> > > > 2match none OverLength
> > > > else "!greater than nth virtual column :help \%>v
> > > > let @/='\%>' . a:n . 'v.*'
> > > > execute '2match OverLength /' . @/ . '/'
> > > > endif
> > > > endf
>
> > > I would expect it to be restored to whatever value it had before the
> > > OverLength() function was called. Are you sure that the '/'
> > > register was not already set to that pattern before the function
> > > was called?
>
> > > If you can do this:
>
> > > 1. :let @/= "hello"
> > > 2. :call OverLength(40)
> > > 3. :echo @/
>
> > > and see that the '/' no longer contains "hello", then you may have
> > > found a bug in Vim.
>
> > > BTW, according to ":help 2match", clearing the match is done with
> > > ":2match none"--the second argument is not needed and appears to be
> > > ignored.
>
> > > Regards,
> > > Gary
>
> > To make it clearer:
>
> > :let @/= "hello"
>
> > :echo @/
> > hello
>
> > :call OverLength(40)
>
> > :echo @/
> > \%>40v.*
>
> > Bill
>
> I just tried that myself, as I suppose I could have earlier, and I
> see the same thing.  I even simplified the function to this:
>
>     function! MyTest()
>       let @/='world'
>     endfunction
>
> and after calling MyTest(), the '/' register contains "world".
>
> However, if I change the function to this:
>
>     function! MyTest()
>       /match
>       echo @/
>     endfunction
>
> and call it, the cursor moves to the next occurrence of "match" in
> the file and the string "match" is echoed to the command line, but
> executing ':echo @/' displays "hello".
>
> So it appears that setting the '/' register indirectly alters a
> local copy, but setting it directly with :let also alters the global
> copy.  I think that's a bug.
>
> The results were the same on versions 7.2.330 and 7.3.584, so it has
> been like this for a while.
>
> Regards,
> Gary

Thank you Gary, for looking at this.
It has me looking further, found the histadd() function.

I had been counting on the 'bug' as it made it easy to continue
searching for long lines.

Now with histadd() this function adds the long line search to the
search history.

And... THAT is something I wanted to know how to do!

It can also workaround the 'bug' if need be:

function! OverLength(n)
if matcharg(2)[0] == "OverLength"
2match none
else "!greater than nth virtual column :help \%>v
let @/ = '\%>' . a:n . 'v.*'
call histadd("search",@/)
execute '2match OverLength /' . @/ . '/'
let @/ = histget("search", -2)
endif
endf

--OR--

This is what I had originally wanted to do,
but did not know how to save to the search history.

function! OverLength(n)
if matcharg(2)[0] == "OverLength"
2match none
else "!greater than nth virtual column :help \%>v
let there = '\%>' . a:n . 'v.*'
call histadd("search",there)
execute '2match OverLength /' . there . '/'
endif
endf

Bill

--
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:

Post a Comment