local M = {} M.defaults = { file = { path = (vim.env.XDG_RUNTIME_DIR or vim.fn.stdpath('state')) .. '/nvim.regfile' }, ssh = { host = nil, path = '~/.local/state/nvim/regfile' }, osc52 = { register = '+' }, tmux = { buffer = nil }, timestamp = { format = '%Y-%m-%dT%H:%M:%S%z' }, } -- name, module, configuration section; registration semantics are (write, read). local providers = { { 'osc52', 'osc52', 'osc52' }, { 'tmux', 'tmux', 'tmux' }, { 'buffer', 'buffer' }, { 'file.basename', 'file_basename' }, { 'file.stem', 'file_stem' }, { 'file.dirname', 'file_dirname' }, { 'file.backed', 'file', 'file' }, { 'ssh.backed', 'ssh', 'ssh' }, { 'timestamp', 'timestamp', 'timestamp' }, { 'uuid', 'uuid' }, } local function notify(msg, level) vim.schedule(function() vim.notify('extregs.nvim: ' .. msg, level or vim.log.levels.ERROR) end) end local function guard(name, fn) return function(...) local ok, result = pcall(fn, ...) if not ok then notify(name .. ': ' .. tostring(result)) return false end return result end end local function register(name, provider) assert(type(provider.read) == 'function', name .. ': missing read()') assert(type(provider.write) == 'function', name .. ': missing write()') vim._extregs.register(name, guard(name, function(reg) if provider.readonly then notify(name .. ' is read-only', vim.log.levels.WARN) return false end return provider:write(reg) end), guard(name, function() return provider:read() end)) end function M.setup(opts) if vim.fn.has('extregs') ~= 1 or not (vim._extregs and vim._extregs.register) then notify("requires a Neovim build with has('extregs') and vim._extregs.register") return false end if M._initialized then return true end local cfg = vim.tbl_deep_extend('force', vim.deepcopy(M.defaults), opts or {}) for _, spec in ipairs(providers) do local name, module, section = unpack(spec) local provider = require('extregs.' .. module).get_provider(cfg[section] or {}) register(name, provider) end M._initialized = true return true end return M