Re: yank matched portion of a selection of lines

26 views
Skip to first unread message

Christian Brabandt

unread,
Jul 16, 2012, 3:27:46 PM7/16/12
to vim...@googlegroups.com, vim-dev Mailingliste
Bram,

On Sa, 07 Jul 2012, Tim Chase wrote:

> On 07/07/12 06:35, Christian Brabandt wrote:
> >> I'd favor a solution with
> >> :%s/{pattern}/\=CollectMatch(submatch(0))/gn
> >>
> >> but '\=' and the "n" flag don't work together.
> >> What about a todo item? Add another flag?
>
> I must say I'm surprised that the \= and "n" flags don't play well
> together. Though it would toggle 'modified', you could have
> CollectMatch just return its argument for a noop replacement...

Attached s_eval_expr_n-flag.diff allows to execute functions inside the
substitution part of an :s-command. The function is executed inside the
sandbox, which should be good enough. Here is why I'd like such a
function:

I have a plugin Colorizer.vim, that highlights color codes and names by
their values. I found, that the fastest way to do this, is to
issue an :s/.../\=Highlight(submatch)/ Unfortunately, this creates a new
undo-branch, although my plugin does not change the buffer.

The alternative is to manually loop over each line, match against a
pattern and for each pattern found, call the function. This works, but is
way to slow. For my use-case, using the sandbox is good enough.

> > Would a textobject like i/ work?
>
> Two parts of me conflict on this: one thinks "wow, that's a cool
> idea", the other thinks "what twisted sicko thought up that one?!"
> :-) I suspect there are a bunch of odd edge-cases such as
> search-offset modifiers, zero-width assertions of what can
> precede/follow a given pattern, use of the \zs and \ze modifiers, etc.

Attached patch search_textobj.diff implements a match text-object. This
was just a fun way to, to see if this is possible. I don't know, if this
is really useful, so I like the idea. I tried to mimic the behaviour of
existing textobjects as good as possible and so far, it seems to do what
it should.

The inner object jump to the next search-object and in visual-mode on
further jumps stop in front of the next match. The 'a' object jump from
match to match, including trailing whitespace if possible (or leading
whitespace, if no trailing whitespace is possible).

Use i/ for inner-forward search i? for inner-backward object, a/ and a?
for the "a" forward/backwards motions.

BTW: This here looks suspicious:

#define RE_SEARCH 0 /* save/use pat in/from search_pattern */
#define RE_SUBST 1 /* save/use pat in/from subst_pattern */
#define RE_BOTH 2 /* save pat in both patterns */
#define RE_LAST 2 /* use last used pattern if "pat" is NULL */


regards,
Christian
--
s_eval_expr_n-flag.diff
search_textobj.diff

Andy Wokula

unread,
Jul 16, 2012, 4:36:58 PM7/16/12
to vim...@googlegroups.com
Am 16.07.2012 21:27, schrieb Christian Brabandt:
> Bram,
> On Sa, 07 Jul 2012, Tim Chase wrote:
>
>> On 07/07/12 06:35, Christian Brabandt wrote:
>>>> I'd favor a solution with
>>>> :%s/{pattern}/\=CollectMatch(submatch(0))/gn
>>>>
>>>> but '\=' and the "n" flag don't work together.
>>>> What about a todo item? Add another flag?
>>
>> I must say I'm surprised that the \= and "n" flags don't play well
>> together. Though it would toggle 'modified', you could have
>> CollectMatch just return its argument for a noop replacement...
>
> Attached s_eval_expr_n-flag.diff allows to execute functions inside the
> substitution part of an :s-command. The function is executed inside the
> sandbox, which should be good enough.

Makes sense, with "n"-flag the expression should be disallowed to change
text etc. I could apply your :substitute patch (even to an older
7.3.420) and collect some matches ... great!

--
Andy

Bram Moolenaar

unread,
Jul 19, 2012, 8:42:28 AM7/19/12
to Christian Brabandt, vim...@googlegroups.com, vim-dev Mailingliste
I'm confused. How does this patch to add i/ and a/ related to the :s
command mentioned above? I'm missing the end goal.


> BTW: This here looks suspicious:
>
> #define RE_SEARCH 0 /* save/use pat in/from search_pattern */
> #define RE_SUBST 1 /* save/use pat in/from subst_pattern */
> #define RE_BOTH 2 /* save pat in both patterns */
> #define RE_LAST 2 /* use last used pattern if "pat" is NULL */

RE_BOTH is used for save, RE_LAST for using.


--
hundred-and-one symptoms of being an internet addict:
148. You find it easier to dial-up the National Weather Service
Weather/your_town/now.html than to simply look out the window.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
Reply all
Reply to author
Forward
0 new messages