Home Explore Blog CI



neovim

46th chunk of `runtime/doc/lua.txt`
0ff4e623ef710775d340b0a7caa103183fa61fc5b8c57f0b0000000100000faa
 (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`)
      • {fn}    (`fun(s: string, i: integer, ...: any)`) (position:
                boolean|integer, ...: any)

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

vim.lpeg.Cp()                                                  *vim.lpeg.Cp()*
    Creates a position capture. It matches the empty string and captures the
    position in the subject where the match occurs. The captured value is a
    number.

    Example: >lua
        local I = lpeg.Cp()
        local function anywhere(p) return lpeg.P({I * p * I + 1 * lpeg.V(1)}) end
        local match_start, match_end = anywhere('world'):match('hello world!')
        assert(match_start == 7)
        assert(match_end == 12)
<

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

vim.lpeg.Cs({patt})                                            *vim.lpeg.Cs()*
    Creates a substitution capture. This function creates a substitution
    capture, which captures the substring of the subject that matches `patt`,
    with substitutions. For any capture inside `patt` with a value, the
    substring that matched the capture is replaced by the capture value (which
    should be a string). The final captured value is the string resulting from
    all replacements.

    Example: >lua
        local function gsub (s, patt, repl)
          patt = lpeg.P(patt)
          patt = lpeg.Cs((patt / repl + 1) ^ 0)
          return lpeg.match(patt, s)
        end
        assert(gsub('Hello, xxx!', 'xxx', 'World') == 'Hello, World!')
<

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

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

vim.lpeg.Ct({patt})                                            *vim.lpeg.Ct()*
    Creates a table capture. This capture returns a table with all values from
    all anonymous captures made by `patt` inside this table in successive
    integer keys, starting at 1. Moreover, for each named capture group
    created by `patt`, the first value of the group is put into the table with
    the group name as its key. The captured value is only the table.

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

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

vim.lpeg.locale({tab})                                     *vim.lpeg.locale()*
    Returns a table with patterns for matching some character classes
    according to the current locale. The table has fields named `alnum`,
    `alpha`, `cntrl`, `digit`, `graph`, `lower`, `print`, `punct`, `space`,
    `upper`, and `xdigit`, each one containing a correspondent pattern. Each
    pattern matches any single character that belongs to its class. If called
    with an argument `table`, then it creates those fields inside the given
    table and returns that table.

    Example: >lua
        lpeg.locale(lpeg)
        local space = lpeg.space ^ 0
        local name = lpeg.C(lpeg.alpha ^ 1) * space
        local sep = lpeg.S(',;') * space
        local pair = lpeg.Cg(name * '=' * space * name) * sep ^ -1

Title: Lua API: vim.lpeg Capture Functions (Continued) and Locale
Summary
This section details the remaining `vim.lpeg` capture functions and the `vim.lpeg.locale()` function. It explains `vim.lpeg.Cp()` (position capture), `vim.lpeg.Cs()` (substitution capture), and `vim.lpeg.Ct()` (table capture, creating a table with anonymous and named capture values). It also covers `vim.lpeg.locale()`, which creates or populates a table with patterns for locale-specific character classes.