Files
neovim/runtime/lua/vim/_core/util.lua
Justin M. Keyes 20e77c5d88 build: ship "_core/*" as bytecode (built-into Nvim binary)
Problem:
We want to encourage implementing core features in Lua instead of C, but
it's clumsy because:
- Core Lua code (built into `nvim` so it is available even if VIMRUNTIME
  is missing/invalid) requires manually updating CMakeLists.txt, or
  stuffing it into `_editor.lua`.
- Core Lua modules are not organized similar to C modules, `_editor.lua`
  is getting too big.

Solution:
- Introduce `_core/` where core Lua code can live. All Lua modules added
  there will automatically be included as bytecode in the `nvim` binary.
- Move these core modules into `_core/*`:
  ```
  _defaults.lua
  _editor.lua
  _options.lua
  _system.lua
  shared.lua
  ```

TODO:
- Move `_extui/ => _core/ui2/`
2025-12-30 01:44:24 -05:00

67 lines
1.9 KiB
Lua

-- Nursery for random things that may later find their way into stdlib if they mature.
local M = {}
--- Adds one or more blank lines above or below the cursor.
-- TODO: move to _core/defaults.lua once it is possible to assign a Lua function to options #25672
--- @param above? boolean Place blank line(s) above the cursor
local function add_blank(above)
local offset = above and 1 or 0
local repeated = vim.fn['repeat']({ '' }, vim.v.count1)
local linenr = vim.api.nvim_win_get_cursor(0)[1]
vim.api.nvim_buf_set_lines(0, linenr - offset, linenr - offset, true, repeated)
end
-- TODO: move to _core/defaults.lua once it is possible to assign a Lua function to options #25672
function M.space_above()
add_blank(true)
end
-- TODO: move to _core/defaults.lua once it is possible to assign a Lua function to options #25672
function M.space_below()
add_blank()
end
--- Edit a file in a specific window
--- @param winnr number
--- @param file string
--- @return number buffer number of the edited buffer
M.edit_in = function(winnr, file)
local function resolved_path(path)
if not path or path == '' then
return ''
end
return vim.fn.resolve(vim.fs.abspath(path))
end
return vim.api.nvim_win_call(winnr, function()
local current_buf = vim.api.nvim_win_get_buf(winnr)
local current = resolved_path(vim.api.nvim_buf_get_name(current_buf))
-- Check if the current buffer is already the target file
if current == resolved_path(file) then
return current_buf
end
-- Read the file into the buffer
vim.cmd.edit(vim.fn.fnameescape(file))
return vim.api.nvim_get_current_buf()
end)
end
--- Read a chunk of data from a file
--- @param file string
--- @param size number
--- @return string? chunk or nil on error
function M.read_chunk(file, size)
local fd = io.open(file, 'rb')
if not fd then
return nil
end
local chunk = fd:read(size)
fd:close()
return tostring(chunk)
end
return M