2022-05-10 14:43:48 +01:00
|
|
|
_G.nvchad = {}
|
2021-08-22 08:49:15 +01:00
|
|
|
|
2022-05-10 14:43:48 +01:00
|
|
|
nvchad.close_buffer = function(force)
|
2022-05-01 03:31:47 +01:00
|
|
|
if vim.bo.buftype == "terminal" then
|
|
|
|
vim.api.nvim_win_hide(0)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local fileExists = vim.fn.filereadable(vim.fn.expand "%p")
|
|
|
|
local modified = vim.api.nvim_buf_get_option(vim.fn.bufnr(), "modified")
|
|
|
|
|
|
|
|
-- 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"
|
|
|
|
|
|
|
|
-- if not force, change to prev buf and then close current
|
|
|
|
local close_cmd = force and ":bd!" or ":bp | bd" .. vim.fn.bufnr()
|
|
|
|
vim.cmd(close_cmd)
|
2021-08-22 08:49:15 +01:00
|
|
|
end
|
|
|
|
|
2022-05-10 14:43:48 +01:00
|
|
|
nvchad.load_config = function()
|
2021-11-15 16:09:35 +00:00
|
|
|
local conf = require "core.default_config"
|
2022-05-14 13:56:55 +01:00
|
|
|
local ignore_modes = { "mode_opts" }
|
2021-08-25 06:28:56 +01:00
|
|
|
|
2022-04-10 12:06:05 +01:00
|
|
|
-- attempt to load and merge a user config
|
|
|
|
local chadrc_exists = vim.fn.filereadable(vim.fn.stdpath "config" .. "/lua/custom/chadrc.lua") == 1
|
2022-05-23 08:24:03 +01:00
|
|
|
|
2022-04-10 12:06:05 +01:00
|
|
|
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-05-23 08:24:03 +01:00
|
|
|
|
2022-04-27 16:42:28 +01:00
|
|
|
if type(user_config) == "table" then
|
2022-05-14 18:42:02 +01:00
|
|
|
conf.mappings = conf.mappings and nvchad.prune_key_map(conf.mappings, user_config.mappings, ignore_modes) or {}
|
2022-05-23 08:24:03 +01:00
|
|
|
user_config.mappings = user_config.mappings
|
2022-05-25 00:16:08 +01:00
|
|
|
and nvchad.prune_key_map(user_config.mappings, "rm_disabled", ignore_modes)
|
|
|
|
or {}
|
2022-04-10 12:06:05 +01:00
|
|
|
conf = vim.tbl_deep_extend("force", conf, user_config)
|
|
|
|
else
|
2022-04-27 16:42:28 +01:00
|
|
|
error "User config (chadrc.lua) *must* return a table!"
|
2022-04-10 12:06:05 +01:00
|
|
|
end
|
2021-11-17 05:30:57 +00:00
|
|
|
end
|
2021-08-25 06:28:56 +01:00
|
|
|
|
2021-11-15 16:09:35 +00:00
|
|
|
return conf
|
2021-08-25 06:28:56 +01:00
|
|
|
end
|
|
|
|
|
2022-05-14 13:56:55 +01:00
|
|
|
-- reduces a given keymap to a table of modes each containing a list of key maps
|
2022-05-14 00:31:13 +01:00
|
|
|
nvchad.reduce_key_map = function(key_map, ignore_modes)
|
|
|
|
local prune_keys = {}
|
2022-05-23 08:24:03 +01:00
|
|
|
|
2022-05-14 00:31:13 +01:00
|
|
|
for _, modes in pairs(key_map) do
|
|
|
|
for mode, mappings in pairs(modes) do
|
|
|
|
if not vim.tbl_contains(ignore_modes, mode) then
|
|
|
|
prune_keys[mode] = prune_keys[mode] and prune_keys[mode] or {}
|
|
|
|
prune_keys[mode] = vim.list_extend(prune_keys[mode], vim.tbl_keys(mappings))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return prune_keys
|
|
|
|
end
|
|
|
|
|
2022-05-14 13:56:55 +01:00
|
|
|
-- remove disabled mappings from a given key map
|
|
|
|
nvchad.remove_disabled_mappings = function(key_map)
|
|
|
|
local clean_map = {}
|
2022-05-23 08:24:03 +01:00
|
|
|
|
2022-05-16 13:31:47 +01:00
|
|
|
if key_map == nil or key_map == "" then
|
|
|
|
return key_map
|
|
|
|
end
|
2022-05-23 08:24:03 +01:00
|
|
|
|
2022-05-14 13:56:55 +01:00
|
|
|
if type(key_map) == "table" then
|
|
|
|
for k, v in pairs(key_map) do
|
2022-05-23 08:24:03 +01:00
|
|
|
if v ~= nil and v ~= "" then
|
|
|
|
clean_map[k] = v
|
|
|
|
end
|
2022-05-14 13:56:55 +01:00
|
|
|
end
|
|
|
|
end
|
2022-05-23 08:24:03 +01:00
|
|
|
|
2022-05-14 13:56:55 +01:00
|
|
|
return clean_map
|
|
|
|
end
|
|
|
|
|
2022-05-14 00:31:13 +01:00
|
|
|
-- prune keys from a key map table by matching against another key map table
|
|
|
|
nvchad.prune_key_map = function(key_map, prune_map, ignore_modes)
|
2022-05-23 08:24:03 +01:00
|
|
|
if not prune_map then
|
|
|
|
return key_map
|
|
|
|
end
|
|
|
|
if not key_map then
|
|
|
|
return prune_map
|
|
|
|
end
|
2022-05-14 13:56:55 +01:00
|
|
|
local prune_keys = type(prune_map) == "table" and nvchad.reduce_key_map(prune_map, ignore_modes)
|
2022-05-25 00:16:08 +01:00
|
|
|
or { n = {}, v = {}, i = {}, t = {} }
|
2022-05-14 00:31:13 +01:00
|
|
|
|
|
|
|
for ext, modes in pairs(key_map) do
|
|
|
|
for mode, mappings in pairs(modes) do
|
|
|
|
if not vim.tbl_contains(ignore_modes, mode) then
|
2022-05-16 13:31:47 +01:00
|
|
|
-- filter mappings table so that only keys that are not in user_mappings are left
|
|
|
|
for b, _ in pairs(mappings) do
|
|
|
|
if prune_keys[mode] and vim.tbl_contains(prune_keys[mode], b) then
|
|
|
|
key_map[ext][mode][b] = nil
|
2022-05-14 00:31:13 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-05-16 13:31:47 +01:00
|
|
|
key_map[ext][mode] = nvchad.remove_disabled_mappings(mappings)
|
2022-05-14 00:31:13 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return key_map
|
|
|
|
end
|
|
|
|
|
2022-05-10 14:43:48 +01:00
|
|
|
nvchad.map = function(mode, keys, command, opt)
|
2022-05-03 15:06:47 +01:00
|
|
|
local options = { silent = true }
|
2022-04-27 16:42:28 +01:00
|
|
|
|
2021-08-25 06:28:56 +01:00
|
|
|
if opt then
|
|
|
|
options = vim.tbl_extend("force", options, opt)
|
|
|
|
end
|
|
|
|
|
2022-04-27 16:42:28 +01:00
|
|
|
if type(keys) == "table" then
|
|
|
|
for _, keymap in ipairs(keys) do
|
2022-05-10 14:43:48 +01:00
|
|
|
nvchad.map(mode, keymap, command, opt)
|
2021-08-25 06:28:56 +01:00
|
|
|
end
|
2022-04-27 16:42:28 +01:00
|
|
|
return
|
2021-08-25 06:28:56 +01:00
|
|
|
end
|
|
|
|
|
2022-04-27 16:42:28 +01:00
|
|
|
vim.keymap.set(mode, keys, command, opt)
|
2021-08-25 06:28:56 +01:00
|
|
|
end
|
|
|
|
|
2022-05-25 00:16:08 +01:00
|
|
|
-- register mappings through which-key
|
|
|
|
nvchad.whichKey_map = function(maps, opts)
|
|
|
|
local present, wk = pcall(require, "which-key")
|
2022-05-26 16:49:13 +01:00
|
|
|
local caller_path = nvchad.get_caller_file_path(4)
|
2022-05-25 00:16:08 +01:00
|
|
|
|
|
|
|
if not present then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
for mode, opt in pairs(opts.mode_opts) do
|
|
|
|
for _, value in pairs(maps) do
|
|
|
|
if value[mode] then
|
|
|
|
-- check if caller_path is in the ignore list
|
|
|
|
if not value["ignore"] or not vim.tbl_contains(value["ignore"], caller_path) then
|
2022-05-26 16:49:13 +01:00
|
|
|
local mode_opts = value["mode_opts"] and vim.tbl_deep_extend("force", opt, value["mode_opts"]) or opt
|
2022-05-25 00:16:08 +01:00
|
|
|
wk.register(value[mode], mode_opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
-- for those who disabled whichkey and want to add specific mapping tables
|
2022-05-26 16:49:13 +01:00
|
|
|
nvchad.no_WhichKey_map = function(mappings)
|
|
|
|
local caller_path = nvchad.get_caller_file_path(4)
|
2022-05-14 00:31:13 +01:00
|
|
|
local ignore_modes = { "mode_opts" }
|
2022-05-26 16:49:13 +01:00
|
|
|
mappings = mappings or nvchad.load_config().mappings
|
2022-05-14 00:31:13 +01:00
|
|
|
|
|
|
|
for _, value in pairs(mappings) do
|
2022-05-26 16:49:13 +01:00
|
|
|
if not value["ignore"] or not vim.tbl_contains(value["ignore"], caller_path) then
|
|
|
|
for mode, keymap in pairs(value) do
|
|
|
|
if not vim.tbl_contains(ignore_modes, mode) then
|
|
|
|
for keybind, cmd in pairs(keymap) do
|
|
|
|
-- disabled keys will not have cmd set
|
|
|
|
if cmd ~= "" and cmd[1] then
|
|
|
|
nvchad.map(mode, keybind, cmd[1], value["mode_opts"] or nil)
|
|
|
|
end
|
2022-05-14 00:31:13 +01:00
|
|
|
end
|
2022-05-12 13:56:01 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-26 10:40:26 +01:00
|
|
|
-- load plugin after entering vim ui
|
2022-05-10 14:43:48 +01:00
|
|
|
nvchad.packer_lazy_load = function(plugin, timer)
|
2021-08-26 10:40:26 +01:00
|
|
|
if plugin then
|
|
|
|
timer = timer or 0
|
|
|
|
vim.defer_fn(function()
|
|
|
|
require("packer").loader(plugin)
|
|
|
|
end, timer)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-29 06:04:44 +01:00
|
|
|
-- remove plugins defined in chadrc
|
2022-05-10 14:43:48 +01:00
|
|
|
nvchad.remove_default_plugins = function(plugins)
|
|
|
|
local removals = nvchad.load_config().plugins.remove or {}
|
2022-05-12 14:57:11 +01:00
|
|
|
|
2022-04-29 06:04:44 +01:00
|
|
|
if not vim.tbl_isempty(removals) then
|
2022-05-01 03:31:47 +01:00
|
|
|
for _, plugin in pairs(removals) do
|
|
|
|
plugins[plugin] = nil
|
|
|
|
end
|
2022-04-29 06:04:44 +01:00
|
|
|
end
|
2022-05-12 14:57:11 +01:00
|
|
|
|
2022-04-29 06:04:44 +01:00
|
|
|
return plugins
|
|
|
|
end
|
|
|
|
|
2022-04-27 16:42:28 +01:00
|
|
|
-- merge default/user plugin tables
|
2022-05-23 08:24:03 +01:00
|
|
|
nvchad.merge_plugins = function(default_plugins)
|
2022-05-10 14:43:48 +01:00
|
|
|
local user_plugins = nvchad.load_config().plugins.user
|
2021-11-13 17:37:20 +00:00
|
|
|
|
2022-04-27 16:42:28 +01: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 16:42:28 +01:00
|
|
|
local final_table = {}
|
2022-01-21 00:12:09 +00:00
|
|
|
|
2022-04-27 16:42:28 +01:00
|
|
|
for key, _ in pairs(default_plugins) do
|
|
|
|
default_plugins[key][1] = key
|
2022-01-31 08:43:51 +00:00
|
|
|
|
2022-04-27 16:42:28 +01:00
|
|
|
final_table[#final_table + 1] = default_plugins[key]
|
2022-01-31 01:28:19 +00:00
|
|
|
end
|
2022-04-27 16:42:28 +01:00
|
|
|
|
|
|
|
return final_table
|
2022-02-13 07:39:07 +00:00
|
|
|
end
|
2022-01-31 08:43:51 +00:00
|
|
|
|
2022-05-10 14:43:48 +01:00
|
|
|
nvchad.load_override = function(default_table, plugin_name)
|
|
|
|
local user_table = nvchad.load_config().plugins.override[plugin_name]
|
2022-05-12 14:57:11 +01:00
|
|
|
|
2022-04-27 16:42:28 +01:00
|
|
|
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-05-12 14:57:11 +01:00
|
|
|
|
2022-04-27 16:42:28 +01:00
|
|
|
return default_table
|
2022-01-31 01:28:19 +00:00
|
|
|
end
|
2022-05-25 00:16:08 +01:00
|
|
|
|
2022-05-26 16:49:13 +01:00
|
|
|
nvchad.get_caller_file_path = function(call_stack_depth)
|
|
|
|
local success, result = pcall(debug.getinfo, call_stack_depth, "S")
|
2022-05-25 00:16:08 +01:00
|
|
|
|
|
|
|
if success then
|
2022-05-26 16:49:13 +01:00
|
|
|
return result.source:match("@(.*)"):gsub(vim.fn.stdpath "config", "")
|
2022-05-25 00:16:08 +01:00
|
|
|
else
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
end
|