2021-08-22 07:49:15 +00:00
|
|
|
local M = {}
|
|
|
|
|
2021-11-09 00:51:27 +00:00
|
|
|
local cmd = vim.cmd
|
2021-08-22 07:49:15 +00:00
|
|
|
|
2022-04-27 15:42:28 +00:00
|
|
|
M.close_buffer = function(force)
|
2022-04-28 08:06:38 +00:00
|
|
|
if force or not vim.bo.buflisted or vim.bo.buftype == 'nofile' then
|
2022-04-27 15:42:28 +00:00
|
|
|
cmd ":bd!"
|
2021-08-22 07:49:15 +00:00
|
|
|
else
|
2022-04-27 15:42:28 +00:00
|
|
|
cmd "bd"
|
2021-08-22 07:49:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-17 05:30:57 +00:00
|
|
|
M.load_config = function()
|
2021-11-15 16:09:35 +00:00
|
|
|
local conf = require "core.default_config"
|
2021-08-25 05:28:56 +00:00
|
|
|
|
2022-04-10 11:06:05 +00:00
|
|
|
-- attempt to load and merge a user config
|
|
|
|
local chadrc_exists = vim.fn.filereadable(vim.fn.stdpath "config" .. "/lua/custom/chadrc.lua") == 1
|
|
|
|
if chadrc_exists then
|
|
|
|
-- merge user config if it exists and is a table; otherwise display an error
|
|
|
|
local user_config = require "custom.chadrc"
|
2022-04-27 15:42:28 +00:00
|
|
|
if type(user_config) == "table" then
|
2022-04-10 11:06:05 +00:00
|
|
|
conf = vim.tbl_deep_extend("force", conf, user_config)
|
|
|
|
else
|
2022-04-27 15:42:28 +00:00
|
|
|
error "User config (chadrc.lua) *must* return a table!"
|
2022-04-10 11:06:05 +00:00
|
|
|
end
|
2021-11-17 05:30:57 +00:00
|
|
|
end
|
2021-08-25 05:28:56 +00:00
|
|
|
|
2021-11-15 16:09:35 +00:00
|
|
|
return conf
|
2021-08-25 05:28:56 +00:00
|
|
|
end
|
|
|
|
|
2021-12-10 20:19:35 +00:00
|
|
|
M.map = function(mode, keys, command, opt)
|
2021-08-25 05:28:56 +00:00
|
|
|
local options = { noremap = true, silent = true }
|
2022-04-27 15:42:28 +00:00
|
|
|
|
2021-08-25 05:28:56 +00:00
|
|
|
if opt then
|
|
|
|
options = vim.tbl_extend("force", options, opt)
|
|
|
|
end
|
|
|
|
|
2022-04-27 15:42:28 +00:00
|
|
|
if type(keys) == "table" then
|
|
|
|
for _, keymap in ipairs(keys) do
|
|
|
|
M.map(mode, keymap, command, opt)
|
2021-08-25 05:28:56 +00:00
|
|
|
end
|
2022-04-27 15:42:28 +00:00
|
|
|
return
|
2021-08-25 05:28:56 +00:00
|
|
|
end
|
|
|
|
|
2022-04-27 15:42:28 +00:00
|
|
|
vim.keymap.set(mode, keys, command, opt)
|
2021-08-25 05:28:56 +00:00
|
|
|
end
|
|
|
|
|
2021-08-26 09:40:26 +00:00
|
|
|
-- load plugin after entering vim ui
|
|
|
|
M.packer_lazy_load = function(plugin, timer)
|
|
|
|
if plugin then
|
|
|
|
timer = timer or 0
|
|
|
|
vim.defer_fn(function()
|
|
|
|
require("packer").loader(plugin)
|
|
|
|
end, timer)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-09 00:51:27 +00:00
|
|
|
-- Highlights functions
|
|
|
|
|
|
|
|
-- Define bg color
|
|
|
|
-- @param group Group
|
|
|
|
-- @param color Color
|
|
|
|
|
|
|
|
M.bg = function(group, col)
|
|
|
|
cmd("hi " .. group .. " guibg=" .. col)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Define fg color
|
|
|
|
-- @param group Group
|
|
|
|
-- @param color Color
|
|
|
|
M.fg = function(group, col)
|
|
|
|
cmd("hi " .. group .. " guifg=" .. col)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Define bg and fg color
|
|
|
|
-- @param group Group
|
|
|
|
-- @param fgcol Fg Color
|
|
|
|
-- @param bgcol Bg Color
|
|
|
|
M.fg_bg = function(group, fgcol, bgcol)
|
|
|
|
cmd("hi " .. group .. " guifg=" .. fgcol .. " guibg=" .. bgcol)
|
|
|
|
end
|
|
|
|
|
2022-04-27 15:42:28 +00:00
|
|
|
M.load_ifExists = function(module)
|
|
|
|
if #module ~= 0 then
|
|
|
|
if type(module) == "string" then
|
|
|
|
require(module)
|
2022-01-21 21:31:58 +00:00
|
|
|
|
2022-04-27 15:42:28 +00:00
|
|
|
-- file[1] = module & file[2] = function
|
|
|
|
elseif type(module) == "table" then
|
|
|
|
require(module[1])[module[2]]()
|
|
|
|
end
|
2021-11-13 17:37:20 +00:00
|
|
|
end
|
2022-04-27 15:42:28 +00:00
|
|
|
end
|
2021-11-13 17:37:20 +00:00
|
|
|
|
2022-04-29 05:04:44 +00:00
|
|
|
-- remove plugins defined in chadrc
|
|
|
|
M.remove_default_plugins = function(plugins)
|
|
|
|
local removals = require("core.utils").load_config().plugins.remove or {}
|
|
|
|
if not vim.tbl_isempty(removals) then
|
|
|
|
for _, plugin in pairs(removals) do plugins[plugin] = nil end
|
|
|
|
end
|
|
|
|
return plugins
|
|
|
|
end
|
|
|
|
|
2022-04-27 15:42:28 +00:00
|
|
|
-- merge default/user plugin tables
|
2021-11-14 01:19:33 +00:00
|
|
|
|
2022-04-27 15:42:28 +00:00
|
|
|
M.plugin_list = function(default_plugins)
|
|
|
|
local user_plugins = require("core.utils").load_config().plugins.user
|
|
|
|
local plug_override = require("core.default_config").plugins.override
|
2021-11-13 17:37:20 +00:00
|
|
|
|
2022-04-27 15:42:28 +00:00
|
|
|
-- merge default + user plugin table
|
|
|
|
default_plugins = vim.tbl_deep_extend("force", default_plugins, user_plugins)
|
2022-01-21 00:12:09 +00:00
|
|
|
|
2022-04-27 15:42:28 +00:00
|
|
|
local final_table = {}
|
2022-01-21 00:12:09 +00:00
|
|
|
|
2022-04-27 15:42:28 +00:00
|
|
|
for key, _ in pairs(default_plugins) do
|
|
|
|
default_plugins[key][1] = key
|
2022-01-31 08:43:51 +00:00
|
|
|
|
2022-04-27 15:42:28 +00:00
|
|
|
final_table[#final_table + 1] = default_plugins[key]
|
|
|
|
plug_override[#plug_override + 1] = default_plugins[key]
|
2022-01-31 01:28:19 +00:00
|
|
|
end
|
2022-04-27 15:42:28 +00:00
|
|
|
|
|
|
|
return final_table
|
2022-02-13 07:39:07 +00:00
|
|
|
end
|
2022-01-31 08:43:51 +00:00
|
|
|
|
2022-04-27 15:42:28 +00:00
|
|
|
M.load_override = function(default_table, plugin_name)
|
|
|
|
local user_table = require("core.utils").load_config().plugins.override[plugin_name]
|
|
|
|
|
|
|
|
if type(user_table) == "table" then
|
|
|
|
default_table = vim.tbl_deep_extend("force", default_table, user_table)
|
|
|
|
else
|
|
|
|
default_table = default_table
|
2022-02-13 07:39:07 +00:00
|
|
|
end
|
2022-04-27 15:42:28 +00:00
|
|
|
|
|
|
|
return default_table
|
2022-01-31 01:28:19 +00:00
|
|
|
end
|
|
|
|
|
2021-08-22 07:49:15 +00:00
|
|
|
return M
|