2016-07-20 1:03 GMT+03:00 Bram Moolenaar <[email protected]>: > > Nikolay Pavlov wrote: > >> >> One of the points of initial suggestion was that `submatch()` is too >> >> long to type, `{m, c1, c2 -> c2 . c1 }` (pattern looks like `\(c1\) >> >> \(c2\)`, `m='c1 c2'`) is easier. Using lambdas like this just avoids >> >> escaping, though it alone already is a good improvement over current >> >> \= syntax. >> > >> > Yeah, I was wondering if we can pass the list of submatches to the >> > function. >> > >> > substitute(text, '\<\k*\>', {m -> 'key-' . m[0]}, 'g') >> > >> > Currently it would be: >> > >> > substitute(text, '\<\k*\>', {-> 'key-' . submatch(0)}, 'g' >> > >> > It does require that the function takes one argument, even when not >> > using it: >> > >> > substitute(text, '\<\k*\>', {m -> 'key-' . g:key}, 'g') >> >> Actually, not. It is possible to determine how many arguments a >> function accepts (also whether it accepts variable number, do not >> repeat one of the Neovim bugs) and construct argument list >> accordingly. Without this dealing with capturing groups may be tricky >> (specifically I mean cases when there *is* capturing group, but it >> matched nothing or it did not match: if I am not mistaking, regexp >> engine does not report how many capturing groups there are in the >> pattern). >> >> Note: unless `text` could be a list `submatch()` is no longer needed >> for substitute() with lambdas: the only feature lambdas do not provide >> here is the second non-zero argument of submatch(). >> >> Probably, the easiest implementation would be something like below, >> but it has one downside: it will allocate memory for all captured >> groups and match, even if none are used (note: I have neither tested >> nor compiled this, it just shows the idea). > > Well, yes, I suppose you could only pass the argument if the function > wants it. Or ignore the function taking fewer arguments. > > I don't see a good reason to pass each submatch as a separate argument. > A list is much more convenient. The user is already used to the number > from "\1" in the :s command. A list of arguments is a hassle if only > using the third one.
There are not much reasons to *have* first and second capturing groups if only using the third one: there is non-capturing variant. There are cases when \N is used in a pattern and not in replacement, but they are rare. > > It does require allocating a list, but since the matching costs > something anyway it won't be much extra. It requires allocating a list, allocating all list items and allocating all captured strings and a copy of the whole match. My patch (if it works) allocates only captured strings and a copy of the whole match and allows giving submatches a name (though it is poor replacement for `(?P<name>)`). > > > -- > ARTHUR: Be quiet! > DENNIS: --but by a two-thirds majority in the case of more-- > ARTHUR: Be quiet! I order you to be quiet! > WOMAN: Order, eh -- who does he think he is? > ARTHUR: I am your king! > The Quest for the Holy Grail (Monty Python) > > /// Bram Moolenaar -- [email protected] -- 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 /// -- -- You received this message from the "vim_dev" 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 --- You received this message because you are subscribed to the Google Groups "vim_dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
