summaryrefslogtreecommitdiff
path: root/lua/vim/userregs/impl/buffercontents.lua
blob: 016b4e59cd3602bd68074dee56e9900b54ea5d70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
local userregs = require("vim.userregs")

-- Register name: "|"
-- This register represents the full contents of the current buffer.
-- Yank into it → saves the buffer.
-- Put from it  → replaces the buffer contents.

local regname = "|"

--- Reads the full contents of the current buffer as a list of lines.
--- This is returned in the format expected by 'userregfunc'.
--- @return table a 'put' dictionary with `lines`, `type`, and `additional_data`
local function read_current_buffer()
  local bufnr = vim.api.nvim_get_current_buf()
  local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)

  return {
    lines = lines,
    type = "V",  -- linewise register
    additional_data = {},
  }
end

--- Replaces the entire contents of the current buffer with the given content.
--- Used when yanking into the "|" register.
--- @param _ string  -- register name (ignored here)
--- @param content table -- dictionary with `lines` from yankreg_T
local function write_current_buffer(_, content)
  local bufnr = vim.api.nvim_get_current_buf()

  vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, content.lines or {})
end

-- Register the "|" register with the userregs framework
userregs.register_handler(regname, {
  put = function(_) return read_current_buffer() end,
  yank = write_current_buffer,
})