vim-settings/neovim/lua/utils.lua

101 lines
2.4 KiB
Lua
Raw Normal View History

-- luacheck: globals packer_plugins
2021-08-24 17:38:14 +00:00
local M = {}
2021-09-30 18:00:09 +00:00
-- Utils taken from https://github.com/zzzeyez/dots/blob/master/nvim/lua/utils.lua
2021-08-24 17:38:14 +00:00
-- Key mapping
function M.map(mode, key, result, opts)
vim.fn.nvim_set_keymap(
mode,
key,
result,
{
noremap = true,
silent = opts.silent or false,
expr = opts.expr or false,
script = opts.script or false
}
)
end
function M.augroup(group, fn)
vim.api.nvim_command("augroup " .. group)
vim.api.nvim_command("autocmd!")
fn()
vim.api.nvim_command("augroup END")
end
function M.get_color(synID, what, mode)
return vim.fn.synIDattr(vim.fn.synIDtrans(vim.fn.hlID(synID)), what, mode)
end
2021-09-30 18:00:09 +00:00
-- end zzzeyez utils
2021-08-24 17:38:14 +00:00
-- Create an autocmd
function M.autocmd(group, cmds, clear)
clear = clear == nil and false or clear
if type(cmds) == "string" then cmds = {cmds} end
vim.cmd("augroup " .. group)
if clear then vim.cmd [[au!]] end
for _, cmd in ipairs(cmds) do vim.cmd("autocmd " .. cmd) end
vim.cmd [[augroup END]]
end
-- Terminal escape a given string
function M.t(str)
return vim.api.nvim_replace_termcodes(str, true, true, true)
end
-- Get env value or use default, if not present
function M.env_default(name, def)
2021-09-16 01:13:17 +00:00
local val = vim.env[name]
2021-08-24 17:38:14 +00:00
return val == nil and def or val
end
2021-08-24 23:12:16 +00:00
-- Checks to see if a package can be required
function M.can_require(name)
if package.loaded[name] then
return false
else
for _, searcher in ipairs(package.searchers or package.loaders) do
local loader = searcher(name)
if type(loader) == 'function' then
package.preload[name] = loader
return true
end
end
return false
end
end
-- Require a package if possible
function M.maybe_require(name)
if M.can_require(name) then
return require(name)
end
return nil
end
-- Require a package and a "_local" suffixed one
function M.require_with_local(name)
-- TODO: Decide if local should completely override the versioned module
-- In that case, the local file would probably start with a `require` for the
-- non-local version. This would allow more control but at the cost of a bit
-- of boiler plate
2021-09-16 01:13:17 +00:00
local rmod = require(name)
local lmod = M.maybe_require(name .. "_local")
if lmod ~= nil then
return lmod
end
return rmod
2021-08-24 23:12:16 +00:00
end
-- Returns whether or not packer plugin is loaded
function M.is_plugin_loaded(name)
return _G["packer_plugins"] and packer_plugins[name] and packer_plugins[name].loaded
end
2021-08-24 17:38:14 +00:00
return M