aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/lua/vim.lua
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2019-01-14 02:22:37 +0100
committerJustin M. Keyes <justinkz@gmail.com>2019-01-14 02:25:45 +0100
commit89d7e24891c2281e301c232c4ad882b135c11bde (patch)
treec1ad4edae69595124cffa99dd80807f5cd6f3270 /src/nvim/lua/vim.lua
parent989fbad5028a2f7d31fef944aac166d562665477 (diff)
parent6c02ff474732dcc6ae5cf103c79298c9602fd116 (diff)
downloadrneovim-89d7e24891c2281e301c232c4ad882b135c11bde.tar.gz
rneovim-89d7e24891c2281e301c232c4ad882b135c11bde.tar.bz2
rneovim-89d7e24891c2281e301c232c4ad882b135c11bde.zip
Merge #9463 'Lua stdlib'
Diffstat (limited to 'src/nvim/lua/vim.lua')
-rw-r--r--src/nvim/lua/vim.lua82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua
index 47f40da72e..df75cc4ade 100644
--- a/src/nvim/lua/vim.lua
+++ b/src/nvim/lua/vim.lua
@@ -118,11 +118,93 @@ local function _update_package_paths()
last_nvim_paths = cur_nvim_paths
end
+local function gsplit(s, sep, plain)
+ assert(type(s) == "string")
+ assert(type(sep) == "string")
+ assert(type(plain) == "boolean" or type(plain) == "nil")
+
+ local start = 1
+ local done = false
+
+ local function pass(i, j, ...)
+ if i then
+ assert(j+1 > start, "Infinite loop detected")
+ local seg = s:sub(start, i - 1)
+ start = j + 1
+ return seg, ...
+ else
+ done = true
+ return s:sub(start)
+ end
+ end
+
+ return function()
+ if done then
+ return
+ end
+ if sep == '' then
+ if start == #s then
+ done = true
+ end
+ return pass(start+1, start)
+ end
+ return pass(s:find(sep, start, plain))
+ end
+end
+
+local inspect = (function()
+ local f
+ return function(...)
+ if f == nil then f = require('vim.inspect') end
+ return f(...)
+ end
+end)()
+
+local function split(s,sep,plain)
+ local t={} for c in gsplit(s, sep, plain) do table.insert(t,c) end
+ return t
+end
+
+local function trim(s)
+ assert(type(s) == "string", "Only strings can be trimmed")
+ local result = s:gsub("^%s+", ""):gsub("%s+$", "")
+ return result
+end
+
+local deepcopy
+
+local function id(v)
+ return v
+end
+
+local deepcopy_funcs = {
+ table = function(orig)
+ local copy = {}
+ for k, v in pairs(orig) do
+ copy[deepcopy(k)] = deepcopy(v)
+ end
+ return copy
+ end,
+ number = id,
+ string = id,
+ ['nil'] = id,
+ boolean = id,
+}
+
+deepcopy = function(orig)
+ return deepcopy_funcs[type(orig)](orig)
+end
+
local module = {
_update_package_paths = _update_package_paths,
_os_proc_children = _os_proc_children,
_os_proc_info = _os_proc_info,
_system = _system,
+ trim = trim,
+ split = split,
+ gsplit = gsplit,
+ deepcopy = deepcopy,
+ inspect = inspect,
}
return module