Home Explore Blog CI



neovim

45th chunk of `runtime/doc/lua.txt`
7eba747184b3188fde069b916105913ad160b3a953d4a7140000000100000fc7
 capture. This pattern matches the empty string and produces
    the values produced by the most recent group capture named `name` (where
    `name` can be any Lua value). Most recent means the last complete
    outermost group capture with the given name. A Complete capture means that
    the entire pattern corresponding to the capture has matched. An Outermost
    capture means that the capture is not inside another complete capture. In
    the same way that LPeg does not specify when it evaluates captures, it
    does not specify whether it reuses values previously produced by the group
    or re-evaluates them.

    Parameters: ~
      • {name}  (`any`)

    Return: ~
        (`vim.lpeg.Capture`)

vim.lpeg.Cc({...})                                             *vim.lpeg.Cc()*
    Creates a constant capture. This pattern matches the empty string and
    produces all given values as its captured values.

    Parameters: ~
      • {...}  (`any`)

    Return: ~
        (`vim.lpeg.Capture`)

vim.lpeg.Cf({patt}, {func})                                    *vim.lpeg.Cf()*
    Creates a fold capture. If `patt` produces a list of captures C1 C2 ...
    Cn, this capture will produce the value
    `func(...func(func(C1, C2), C3)...,Cn)`, that is, it will fold (or
    accumulate, or reduce) the captures from `patt` using function `func`.
    This capture assumes that `patt` should produce at least one capture with
    at least one value (of any type), which becomes the initial value of an
    accumulator. (If you need a specific initial value, you may prefix a
    constant capture to `patt`.) For each subsequent capture, LPeg calls
    `func` with this accumulator as the first argument and all values produced
    by the capture as extra arguments; the first result from this call becomes
    the new value for the accumulator. The final value of the accumulator
    becomes the captured value.

    Example: >lua
        local number = lpeg.R('09') ^ 1 / tonumber
        local list = number * (',' * number) ^ 0
        local function add(acc, newvalue) return acc + newvalue end
        local sum = lpeg.Cf(list, add)
        assert(sum:match('10,30,43') == 83)
<

    Parameters: ~
      • {patt}  (`vim.lpeg.Pattern|string|integer|boolean|table|function`)
      • {func}  (`fun(acc, newvalue)`)

    Return: ~
        (`vim.lpeg.Capture`)

vim.lpeg.Cg({patt}, {name})                                    *vim.lpeg.Cg()*
    Creates a group capture. It groups all values returned by `patt` into a
    single capture. The group may be anonymous (if no name is given) or named
    with the given name (which can be any non-nil Lua value).

    Parameters: ~
      • {patt}  (`vim.lpeg.Pattern|string|integer|boolean|table|function`)
      • {name}  (`string?`)

    Return: ~
        (`vim.lpeg.Capture`)

vim.lpeg.Cmt({patt}, {fn})                                    *vim.lpeg.Cmt()*
    Creates a match-time capture. Unlike all other captures, this one is
    evaluated immediately when a match occurs (even if it is part of a larger
    pattern that fails later). It forces the immediate evaluation of all its
    nested captures and then calls `function`. The given function gets as
    arguments the entire subject, the current position (after the match of
    `patt`), plus any capture values produced by `patt`. The first value
    returned by `function` defines how the match happens. If the call returns
    a number, the match succeeds and the returned number becomes the new
    current position. (Assuming a subject sand current position `i`, the
    returned number must be in the range `[i, len(s) + 1]`.) If the call
    returns `true`, the match succeeds without consuming any input (so, to
    return true is equivalent to return `i`). If the call returns `false`,
    `nil`, or no value, the match fails. Any extra values returned by the
    function become the values produced by the capture.

    Parameters: ~
      • {patt}  (`vim.lpeg.Pattern|string|integer|boolean|table|function`)

Title: Lua API: vim.lpeg Capture Functions (Continued)
Summary
This section continues the description of `vim.lpeg` capture functions. It covers `vim.lpeg.Cc()` (constant capture), `vim.lpeg.Cf()` (fold capture, which applies a function to accumulate captured values), `vim.lpeg.Cg()` (group capture, for grouping captured values, optionally with a name), and `vim.lpeg.Cmt()` (match-time capture, which executes a function immediately upon a match and uses its return value to determine the match's success and captured values). Each function's parameters and return values are explained.