Home Explore Blog CI



neovim

30th chunk of `runtime/doc/api.txt`
35344760ce519d666d54084175b29b8d586ad3f28193ab120000000100000ff3
 |:bunload| command or the buffer's
|'bufhidden'| option. When a buffer is unloaded its file contents are freed
from memory and vim cannot operate on the buffer lines until it is reloaded
(usually by opening the buffer again in a new window). API methods such as
|nvim_buf_get_lines()| and |nvim_buf_line_count()| will be affected.

You can use |nvim_buf_is_loaded()| or |nvim_buf_line_count()| to check
whether a buffer is loaded.


nvim_buf_attach({buffer}, {send_buffer}, {opts})           *nvim_buf_attach()*
    Activates buffer-update events on a channel, or as Lua callbacks.

    Example (Lua): capture buffer updates in a global `events` variable (use
    "vim.print(events)" to see its contents): >lua
        events = {}
        vim.api.nvim_buf_attach(0, false, {
          on_lines = function(...)
            table.insert(events, {...})
          end,
        })
<

    Attributes: ~
        Since: 0.3.0

    Parameters: ~
      • {buffer}       Buffer id, or 0 for current buffer
      • {send_buffer}  True if the initial notification should contain the
                       whole buffer: first notification will be
                       `nvim_buf_lines_event`. Else the first notification
                       will be `nvim_buf_changedtick_event`. Not for Lua
                       callbacks.
      • {opts}         Optional parameters.
                       • on_lines: Lua callback invoked on change. Return a
                         truthy value (not `false` or `nil`) to detach. Args:
                         • the string "lines"
                         • buffer id
                         • b:changedtick
                         • first line that changed (zero-indexed)
                         • last line that was changed
                         • last line in the updated range
                         • byte count of previous contents
                         • deleted_codepoints (if `utf_sizes` is true)
                         • deleted_codeunits (if `utf_sizes` is true)
                       • on_bytes: Lua callback invoked on change. This
                         callback receives more granular information about the
                         change compared to on_lines. Return a truthy value
                         (not `false` or `nil`) to detach. Args:
                         • the string "bytes"
                         • buffer id
                         • b:changedtick
                         • start row of the changed text (zero-indexed)
                         • start column of the changed text
                         • byte offset of the changed text (from the start of
                           the buffer)
                         • old end row of the changed text (offset from start
                           row)
                         • old end column of the changed text (if old end row
                           = 0, offset from start column)
                         • old end byte length of the changed text
                         • new end row of the changed text (offset from start
                           row)
                         • new end column of the changed text (if new end row
                           = 0, offset from start column)
                         • new end byte length of the changed text
                       • on_changedtick: Lua callback invoked on changedtick
                         increment without text change. Args:
                         • the string "changedtick"
                         • buffer id
                         • b:changedtick
                       • on_detach: Lua callback invoked on detach. Args:
                         • the string "detach"
                         • buffer id
                       • on_reload: Lua callback invoked on reload. The entire
                         buffer content should be considered changed. Args:
                         • the string "reload"
                         • buffer id
                       • utf_sizes:

Title: nvim_buf_attach: Attaching to Buffer Updates
Summary
This section describes the `nvim_buf_attach` function, which allows activation of buffer-update events via channels or Lua callbacks. It provides details on how unloaded buffers are handled and how to check if a buffer is loaded. The function parameters are explained, including the buffer ID, `send_buffer` option (for initial notification content), and a comprehensive list of optional parameters for Lua callbacks (`on_lines`, `on_bytes`, `on_changedtick`, `on_detach`, `on_reload`) that can be triggered by buffer changes, along with their respective arguments.