2022-05-29 06:37:47 -04:00
|
|
|
local M = {}
|
2022-06-05 09:41:26 -04:00
|
|
|
local api = vim.api
|
|
|
|
local fn = vim.fn
|
2021-08-22 03:49:15 -04:00
|
|
|
|
2022-05-29 05:38:53 -04:00
|
|
|
local merge_tb = vim.tbl_deep_extend
|
|
|
|
|
2022-05-29 06:37:47 -04:00
|
|
|
M.close_buffer = function(force)
|
2022-04-30 22:31:47 -04:00
|
|
|
if vim.bo.buftype == "terminal" then
|
2022-06-16 00:01:15 -04:00
|
|
|
force = force or #api.nvim_list_wins() < 2 and ":bd!"
|
|
|
|
local swap = force and #api.nvim_list_bufs() > 1 and ":bp | bd!" .. fn.bufnr()
|
|
|
|
return vim.cmd(swap or force or "hide")
|
2022-04-30 22:31:47 -04:00
|
|
|
end
|
|
|
|
|
2022-06-05 09:41:26 -04:00
|
|
|
local fileExists = fn.filereadable(fn.expand "%p")
|
|
|
|
local modified = api.nvim_buf_get_option(fn.bufnr(), "modified")
|
2022-04-30 22:31:47 -04:00
|
|
|
|
|
|
|
-- if file doesnt exist & its modified
|
|
|
|
if fileExists == 0 and modified then
|
|
|
|
print "no file name? add it now!"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
force = force or not vim.bo.buflisted or vim.bo.buftype == "nofile"
|
2022-06-18 00:27:17 -04:00
|
|
|
|
2022-04-30 22:31:47 -04:00
|
|
|
-- if not force, change to prev buf and then close current
|
2022-06-05 09:41:26 -04:00
|
|
|
local close_cmd = force and ":bd!" or ":bp | bd" .. fn.bufnr()
|
2022-04-30 22:31:47 -04:00
|
|
|
vim.cmd(close_cmd)
|
2021-08-22 03:49:15 -04:00
|
|
|
end
|
|
|
|
|
2022-05-29 06:37:47 -04:00
|
|
|
M.load_config = function()
|
2022-05-29 05:38:53 -04:00
|
|
|
local config = require "core.default_config"
|
|
|
|
local chadrc_exists, chadrc = pcall(require, "custom.chadrc")
|
2022-05-23 03:24:03 -04:00
|
|
|
|
2022-04-10 07:06:05 -04:00
|
|
|
if chadrc_exists then
|
|
|
|
-- merge user config if it exists and is a table; otherwise display an error
|
2022-05-29 05:38:53 -04:00
|
|
|
if type(chadrc) == "table" then
|
2022-05-29 06:37:47 -04:00
|
|
|
M.remove_default_keys()
|
2022-05-29 05:38:53 -04:00
|
|
|
config = merge_tb("force", config, chadrc)
|
2022-04-10 07:06:05 -04:00
|
|
|
else
|
2022-05-29 05:38:53 -04:00
|
|
|
error "chadrc must return a table!"
|
2022-04-10 07:06:05 -04:00
|
|
|
end
|
2021-11-17 00:30:57 -05:00
|
|
|
end
|
2021-08-25 01:28:56 -04:00
|
|
|
|
2022-05-29 05:38:53 -04:00
|
|
|
config.mappings.disabled = nil
|
|
|
|
return config
|
2021-08-25 01:28:56 -04:00
|
|
|
end
|
|
|
|
|
2022-05-29 06:37:47 -04:00
|
|
|
M.remove_default_keys = function()
|
2022-05-29 05:38:53 -04:00
|
|
|
local chadrc = require "custom.chadrc"
|
|
|
|
local user_mappings = chadrc.mappings or {}
|
|
|
|
local user_keys = {}
|
|
|
|
local user_sections = vim.tbl_keys(user_mappings)
|
2022-05-23 03:24:03 -04:00
|
|
|
|
2022-05-29 05:38:53 -04:00
|
|
|
-- push user_map keys in user_keys table
|
|
|
|
for _, section in ipairs(user_sections) do
|
|
|
|
user_keys = vim.tbl_deep_extend("force", user_keys, user_mappings[section])
|
2022-05-13 19:31:13 -04:00
|
|
|
end
|
|
|
|
|
2022-05-29 05:38:53 -04:00
|
|
|
local function disable_key(mode, keybind, mode_mapping)
|
|
|
|
local keys_in_mode = vim.tbl_keys(user_keys[mode] or {})
|
2022-05-23 03:24:03 -04:00
|
|
|
|
2022-05-29 05:38:53 -04:00
|
|
|
if vim.tbl_contains(keys_in_mode, keybind) then
|
|
|
|
mode_mapping[keybind] = nil
|
2022-05-14 08:56:55 -04:00
|
|
|
end
|
|
|
|
end
|
2022-05-23 03:24:03 -04:00
|
|
|
|
2022-05-29 05:38:53 -04:00
|
|
|
local default_mappings = require("core.default_config").mappings
|
2022-05-14 08:56:55 -04:00
|
|
|
|
2022-05-29 05:38:53 -04:00
|
|
|
-- remove user_maps from default mapping table
|
|
|
|
for _, section_mappings in pairs(default_mappings) do
|
|
|
|
for mode, mode_mapping in pairs(section_mappings) do
|
|
|
|
for keybind, _ in pairs(mode_mapping) do
|
|
|
|
disable_key(mode, keybind, mode_mapping)
|
2022-05-13 19:31:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-29 06:37:47 -04:00
|
|
|
M.load_mappings = function(mappings, mapping_opt)
|
2022-05-31 15:19:23 -04:00
|
|
|
-- set mapping function with/without whichkey
|
2022-05-29 05:38:53 -04:00
|
|
|
local map_func
|
|
|
|
local whichkey_exists, wk = pcall(require, "which-key")
|
2021-08-25 01:28:56 -04:00
|
|
|
|
2022-05-29 05:38:53 -04:00
|
|
|
if whichkey_exists then
|
|
|
|
map_func = function(keybind, mapping_info, opts)
|
|
|
|
wk.register({ [keybind] = mapping_info }, opts)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
map_func = function(keybind, mapping_info, opts)
|
|
|
|
local mode = opts.mode
|
|
|
|
opts.mode = nil
|
|
|
|
vim.keymap.set(mode, keybind, mapping_info[1], opts)
|
2021-08-25 01:28:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-31 15:19:23 -04:00
|
|
|
mappings = mappings or vim.deepcopy(M.load_config().mappings)
|
|
|
|
mappings.lspconfig = nil
|
2021-08-25 01:28:56 -04:00
|
|
|
|
2022-05-31 15:19:23 -04:00
|
|
|
for _, section_mappings in pairs(mappings) do
|
|
|
|
-- skip mapping this as its mapppings are loaded in lspconfig
|
|
|
|
for mode, mode_mappings in pairs(section_mappings) do
|
|
|
|
for keybind, mapping_info in pairs(mode_mappings) do
|
|
|
|
-- merge default + user opts
|
2022-05-24 19:16:08 -04:00
|
|
|
|
2022-05-31 15:19:23 -04:00
|
|
|
local default_opts = merge_tb("force", { mode = mode }, mapping_opt or {})
|
|
|
|
local opts = merge_tb("force", default_opts, mapping_info.opts or {})
|
2022-05-24 19:16:08 -04:00
|
|
|
|
2022-05-31 15:19:23 -04:00
|
|
|
if mapping_info.opts then
|
|
|
|
mapping_info.opts = nil
|
2022-05-24 19:16:08 -04:00
|
|
|
end
|
2022-05-31 15:19:23 -04:00
|
|
|
|
|
|
|
map_func(keybind, mapping_info, opts)
|
2022-05-12 08:56:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-29 01:04:44 -04:00
|
|
|
-- remove plugins defined in chadrc
|
2022-05-29 06:37:47 -04:00
|
|
|
M.remove_default_plugins = function(plugins)
|
|
|
|
local removals = M.load_config().plugins.remove or {}
|
2022-05-12 09:57:11 -04:00
|
|
|
|
2022-04-29 01:04:44 -04:00
|
|
|
if not vim.tbl_isempty(removals) then
|
2022-04-30 22:31:47 -04:00
|
|
|
for _, plugin in pairs(removals) do
|
|
|
|
plugins[plugin] = nil
|
|
|
|
end
|
2022-04-29 01:04:44 -04:00
|
|
|
end
|
2022-05-12 09:57:11 -04:00
|
|
|
|
2022-04-29 01:04:44 -04:00
|
|
|
return plugins
|
|
|
|
end
|
|
|
|
|
2022-04-27 11:42:28 -04:00
|
|
|
-- merge default/user plugin tables
|
2022-05-29 06:37:47 -04:00
|
|
|
M.merge_plugins = function(default_plugins)
|
|
|
|
local user_plugins = M.load_config().plugins.user
|
2021-11-13 12:37:20 -05:00
|
|
|
|
2022-04-27 11:42:28 -04:00
|
|
|
-- merge default + user plugin table
|
2022-05-29 05:38:53 -04:00
|
|
|
default_plugins = merge_tb("force", default_plugins, user_plugins)
|
2022-01-20 19:12:09 -05:00
|
|
|
|
2022-04-27 11:42:28 -04:00
|
|
|
local final_table = {}
|
2022-01-20 19:12:09 -05:00
|
|
|
|
2022-04-27 11:42:28 -04:00
|
|
|
for key, _ in pairs(default_plugins) do
|
|
|
|
default_plugins[key][1] = key
|
2022-01-31 03:43:51 -05:00
|
|
|
|
2022-04-27 11:42:28 -04:00
|
|
|
final_table[#final_table + 1] = default_plugins[key]
|
2022-01-30 20:28:19 -05:00
|
|
|
end
|
2022-04-27 11:42:28 -04:00
|
|
|
|
|
|
|
return final_table
|
2022-02-13 02:39:07 -05:00
|
|
|
end
|
2022-01-31 03:43:51 -05:00
|
|
|
|
2022-05-29 06:37:47 -04:00
|
|
|
M.load_override = function(default_table, plugin_name)
|
|
|
|
local user_table = M.load_config().plugins.override[plugin_name]
|
2022-05-12 09:57:11 -04:00
|
|
|
|
2022-06-15 08:11:53 -04:00
|
|
|
if type(user_table) == "function" then
|
|
|
|
user_table = user_table()
|
2022-06-18 00:27:17 -04:00
|
|
|
elseif type(user_table) == "table" then
|
2022-05-29 05:38:53 -04:00
|
|
|
default_table = merge_tb("force", default_table, user_table)
|
2022-04-27 11:42:28 -04:00
|
|
|
else
|
|
|
|
default_table = default_table
|
2022-02-13 02:39:07 -05:00
|
|
|
end
|
2022-05-12 09:57:11 -04:00
|
|
|
|
2022-04-27 11:42:28 -04:00
|
|
|
return default_table
|
2022-01-30 20:28:19 -05:00
|
|
|
end
|
2022-05-29 06:37:47 -04:00
|
|
|
|
|
|
|
return M
|