Monday, July 9, 2012

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

On 2012-07-09, Bee wrote:
> On Jul 9, 10:50 am, Andy Wokula wrote:
> > Am 09.07.2012 03:44, schrieb Gary Johnson:

> > >>>> 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...
> >
> > >      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.
> >
> > ... also checked with 7.0 and 6.4 -- same results.
> >
> > I'd say please don't declare this a bug, rather update the help.
> >
> > e.g. how would you otherwise clear the last search pattern
> >      :let @/ = ""
> > from within a function?  (maybe not the best example)
> >
> > Usually, when you assign to @/, you're supposed to know what you are
> > doing.  OTOH, when doing a range command :/pat/  or :substitute or :global
> > etc. you might not think about side effects, that's what search-undo is for.
> >
> > Plugins like :LogiPat assign to @/ within a function ().
> >
> > Just to mention it, there is also a search-undo for autocmds.
> >
> > --
> > Andy
>
> I agree with Andy, I would not call it a program bug, but a need to
> clarify the help.
>
> I like the fact that it is NOT reset on leaving a function.

I agree that the current behavior is not really a problem except
that it differs from the documented behavior.

I took a look through the source code. It appears that the current
behavior is intentional. See

set_last_search_pat()

and where it is called from

write_reg_contents_ex()

Are *quote/* and *function-search-undo* the only places where this
behavior is documented?

How about changing the note at the end of *quote/* to this:

Note that the value is restored when returning from a function
unless the value was set within the function using `:let`
|function-search-undo|.

and changing the first sentence of *function-search-undo* to this:

The redo command "." will not be changed by the function.
When the function returns, the last used search pattern will be
restored to what it was before the function was called unless
the search pattern was set within the function with `:let`.

Regards,
Gary

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