Sunday, July 8, 2012

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

On Jul 8, 3:33 pm, Gary Johnson <garyj...@spocom.com> 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

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