2021-08-22 03:49:15 -04:00
|
|
|
local M = {}
|
|
|
|
|
2021-11-08 19:51:27 -05:00
|
|
|
local cmd = vim.cmd
|
2021-12-10 15:19:35 -05:00
|
|
|
M.close_buffer = function(force)
|
2021-08-22 03:49:15 -04:00
|
|
|
-- This is a modification of a NeoVim plugin from
|
|
|
|
-- Author: ojroques - Olivier Roques
|
|
|
|
-- Src: https://github.com/ojroques/nvim-bufdel
|
|
|
|
-- (Author has okayed copy-paste)
|
|
|
|
|
|
|
|
-- Options
|
|
|
|
local opts = {
|
|
|
|
next = "cycle", -- how to retrieve the next buffer
|
|
|
|
quit = false, -- exit when last buffer is deleted
|
|
|
|
--TODO make this a chadrc flag/option
|
|
|
|
}
|
|
|
|
|
|
|
|
-- ----------------
|
|
|
|
-- Helper functions
|
|
|
|
-- ----------------
|
|
|
|
|
|
|
|
-- Switch to buffer 'buf' on each window from list 'windows'
|
|
|
|
local function switch_buffer(windows, buf)
|
|
|
|
local cur_win = vim.fn.winnr()
|
|
|
|
for _, winid in ipairs(windows) do
|
2022-01-03 15:50:00 -05:00
|
|
|
winid = tonumber(winid) or 0
|
2021-08-22 03:49:15 -04:00
|
|
|
vim.cmd(string.format("%d wincmd w", vim.fn.win_id2win(winid)))
|
|
|
|
vim.cmd(string.format("buffer %d", buf))
|
|
|
|
end
|
|
|
|
vim.cmd(string.format("%d wincmd w", cur_win)) -- return to original window
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Select the first buffer with a number greater than given buffer
|
|
|
|
local function get_next_buf(buf)
|
|
|
|
local next = vim.fn.bufnr "#"
|
|
|
|
if opts.next == "alternate" and vim.fn.buflisted(next) == 1 then
|
|
|
|
return next
|
|
|
|
end
|
|
|
|
for i = 0, vim.fn.bufnr "$" - 1 do
|
|
|
|
next = (buf + i) % vim.fn.bufnr "$" + 1 -- will loop back to 1
|
|
|
|
if vim.fn.buflisted(next) == 1 then
|
|
|
|
return next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- ----------------
|
|
|
|
-- End helper functions
|
|
|
|
-- ----------------
|
|
|
|
|
|
|
|
local buf = vim.fn.bufnr()
|
|
|
|
if vim.fn.buflisted(buf) == 0 then -- exit if buffer number is invalid
|
|
|
|
vim.cmd "close"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if #vim.fn.getbufinfo { buflisted = 1 } < 2 then
|
|
|
|
if opts.quit then
|
|
|
|
-- exit when there is only one buffer left
|
|
|
|
if force then
|
|
|
|
vim.cmd "qall!"
|
|
|
|
else
|
|
|
|
vim.cmd "confirm qall"
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-12-10 15:19:35 -05:00
|
|
|
local chad_term, _ = pcall(function()
|
2021-08-22 03:49:15 -04:00
|
|
|
return vim.api.nvim_buf_get_var(buf, "term_type")
|
|
|
|
end)
|
|
|
|
|
|
|
|
if chad_term then
|
|
|
|
-- Must be a window type
|
|
|
|
vim.cmd(string.format("setlocal nobl", buf))
|
|
|
|
vim.cmd "enew"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- don't exit and create a new empty buffer
|
|
|
|
vim.cmd "enew"
|
|
|
|
vim.cmd "bp"
|
|
|
|
end
|
|
|
|
|
|
|
|
local next_buf = get_next_buf(buf)
|
|
|
|
local windows = vim.fn.getbufinfo(buf)[1].windows
|
|
|
|
|
|
|
|
-- force deletion of terminal buffers to avoid the prompt
|
|
|
|
if force or vim.fn.getbufvar(buf, "&buftype") == "terminal" then
|
|
|
|
local chad_term, type = pcall(function()
|
|
|
|
return vim.api.nvim_buf_get_var(buf, "term_type")
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- TODO this scope is error prone, make resilient
|
|
|
|
if chad_term then
|
|
|
|
if type == "wind" then
|
|
|
|
-- hide from bufferline
|
|
|
|
vim.cmd(string.format("%d bufdo setlocal nobl", buf))
|
2022-01-03 16:28:09 -05:00
|
|
|
-- switch to another buff
|
|
|
|
-- TODO switch to next buffer, this works too
|
2021-08-22 03:49:15 -04:00
|
|
|
vim.cmd "BufferLineCycleNext"
|
|
|
|
else
|
|
|
|
local cur_win = vim.fn.winnr()
|
|
|
|
-- we can close this window
|
|
|
|
vim.cmd(string.format("%d wincmd c", cur_win))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
else
|
|
|
|
switch_buffer(windows, next_buf)
|
|
|
|
vim.cmd(string.format("bd! %d", buf))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
switch_buffer(windows, next_buf)
|
|
|
|
vim.cmd(string.format("silent! confirm bd %d", buf))
|
|
|
|
end
|
|
|
|
-- revert buffer switches if user has canceled deletion
|
|
|
|
if vim.fn.buflisted(buf) == 1 then
|
|
|
|
switch_buffer(windows, buf)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- hide statusline
|
|
|
|
-- tables fetched from load_config function
|
|
|
|
M.hide_statusline = function()
|
2021-09-19 04:11:28 -04:00
|
|
|
local hidden = require("core.utils").load_config().plugins.options.statusline.hidden
|
|
|
|
local shown = require("core.utils").load_config().plugins.options.statusline.shown
|
2021-08-22 03:49:15 -04:00
|
|
|
local api = vim.api
|
2022-01-03 15:50:00 -05:00
|
|
|
local buftype = api.nvim_buf_get_option(0, "ft")
|
2021-08-22 03:49:15 -04:00
|
|
|
|
|
|
|
-- shown table from config has the highest priority
|
|
|
|
if vim.tbl_contains(shown, buftype) then
|
|
|
|
api.nvim_set_option("laststatus", 2)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if vim.tbl_contains(hidden, buftype) then
|
|
|
|
api.nvim_set_option("laststatus", 0)
|
|
|
|
return
|
|
|
|
end
|
2021-11-17 00:30:57 -05:00
|
|
|
|
|
|
|
api.nvim_set_option("laststatus", 2)
|
2021-08-22 03:49:15 -04:00
|
|
|
end
|
|
|
|
|
2021-11-17 00:30:57 -05:00
|
|
|
M.load_config = function()
|
2021-11-15 11:09:35 -05:00
|
|
|
local conf = require "core.default_config"
|
2021-08-25 01:28:56 -04:00
|
|
|
|
2022-04-10 07:06:05 -04: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"
|
|
|
|
if type(user_config) == 'table' then
|
|
|
|
conf = vim.tbl_deep_extend("force", conf, user_config)
|
|
|
|
else
|
|
|
|
error("User config (chadrc.lua) *must* return a table!")
|
|
|
|
end
|
2021-11-17 00:30:57 -05:00
|
|
|
end
|
2021-08-25 01:28:56 -04:00
|
|
|
|
2021-11-15 11:09:35 -05:00
|
|
|
return conf
|
2021-08-25 01:28:56 -04:00
|
|
|
end
|
|
|
|
|
2021-12-10 15:19:35 -05:00
|
|
|
M.map = function(mode, keys, command, opt)
|
2021-08-25 01:28:56 -04:00
|
|
|
local options = { noremap = true, silent = true }
|
|
|
|
if opt then
|
|
|
|
options = vim.tbl_extend("force", options, opt)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- all valid modes allowed for mappings
|
|
|
|
-- :h map-modes
|
|
|
|
local valid_modes = {
|
|
|
|
[""] = true,
|
|
|
|
["n"] = true,
|
|
|
|
["v"] = true,
|
|
|
|
["s"] = true,
|
|
|
|
["x"] = true,
|
|
|
|
["o"] = true,
|
|
|
|
["!"] = true,
|
|
|
|
["i"] = true,
|
|
|
|
["l"] = true,
|
|
|
|
["c"] = true,
|
|
|
|
["t"] = true,
|
|
|
|
}
|
|
|
|
|
|
|
|
-- helper function for M.map
|
|
|
|
-- can gives multiple modes and keys
|
2021-12-10 15:19:35 -05:00
|
|
|
local function map_wrapper(sub_mode, lhs, rhs, sub_options)
|
2021-08-25 01:28:56 -04:00
|
|
|
if type(lhs) == "table" then
|
|
|
|
for _, key in ipairs(lhs) do
|
2021-12-10 15:19:35 -05:00
|
|
|
map_wrapper(sub_mode, key, rhs, sub_options)
|
2021-08-25 01:28:56 -04:00
|
|
|
end
|
|
|
|
else
|
2021-12-10 15:19:35 -05:00
|
|
|
if type(sub_mode) == "table" then
|
|
|
|
for _, m in ipairs(sub_mode) do
|
|
|
|
map_wrapper(m, lhs, rhs, sub_options)
|
2021-08-25 01:28:56 -04:00
|
|
|
end
|
|
|
|
else
|
2021-12-10 15:19:35 -05:00
|
|
|
if valid_modes[sub_mode] and lhs and rhs then
|
|
|
|
vim.api.nvim_set_keymap(sub_mode, lhs, rhs, sub_options)
|
2021-08-25 01:28:56 -04:00
|
|
|
else
|
2021-12-10 15:19:35 -05:00
|
|
|
sub_mode, lhs, rhs = sub_mode or "", lhs or "", rhs or ""
|
|
|
|
print(
|
|
|
|
"Cannot set mapping [ mode = '" .. sub_mode .. "' | key = '" .. lhs .. "' | cmd = '" .. rhs .. "' ]"
|
|
|
|
)
|
2021-08-25 01:28:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-10 15:19:35 -05:00
|
|
|
map_wrapper(mode, keys, command, options)
|
2021-08-25 01:28:56 -04:00
|
|
|
end
|
|
|
|
|
2021-08-26 05:40:26 -04: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-08 19:51:27 -05: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
|
|
|
|
|
2021-11-13 12:37:20 -05:00
|
|
|
-- Override default config of a plugin based on the path provided in the chadrc
|
2022-01-21 06:49:49 -05:00
|
|
|
-- Arguments:
|
|
|
|
-- 1st - name of plugin
|
|
|
|
-- 2nd - default config path
|
|
|
|
-- 3rd - optional function name which will called from default_config path
|
|
|
|
-- e.g: if given args - "telescope", "plugins.configs.telescope", "setup"
|
|
|
|
-- then return "require('plugins.configs.telescope').setup()"
|
|
|
|
-- if 3rd arg not given, then return "require('plugins.configs.telescope')"
|
2022-01-20 19:12:09 -05:00
|
|
|
-- if override is a table, mark set the override flag for the default config to true
|
|
|
|
-- override flag being true tells the plugin to call tbl_override_req as part of configuration
|
2022-01-21 16:31:58 -05:00
|
|
|
|
2022-01-21 06:49:49 -05:00
|
|
|
M.override_req = function(name, default_config, config_function)
|
|
|
|
local override, apply_table_override =
|
|
|
|
require("core.utils").load_config().plugins.default_plugin_config_replace[name], "false"
|
|
|
|
local result = default_config
|
|
|
|
if type(override) == "string" and override ~= "" then
|
2022-01-21 16:31:58 -05:00
|
|
|
return "require('" .. override .. "')"
|
2022-01-21 06:49:49 -05:00
|
|
|
elseif type(override) == "table" then
|
|
|
|
apply_table_override = "true"
|
2022-01-21 16:31:58 -05:00
|
|
|
elseif type(override) == "function" then
|
|
|
|
return override
|
2021-11-13 12:37:20 -05:00
|
|
|
end
|
|
|
|
|
2022-01-21 06:49:49 -05:00
|
|
|
result = "('" .. result .. "')"
|
|
|
|
if type(config_function) == "string" and config_function ~= "" then
|
|
|
|
-- add the . to call the functions and concatenate true or false as argument
|
|
|
|
result = result .. "." .. config_function .. "(" .. apply_table_override .. ")"
|
2021-11-13 12:37:20 -05:00
|
|
|
end
|
2021-11-13 20:19:33 -05:00
|
|
|
|
2022-01-21 06:49:49 -05:00
|
|
|
return "require" .. result
|
2021-11-13 12:37:20 -05:00
|
|
|
end
|
|
|
|
|
2022-01-20 19:12:09 -05:00
|
|
|
-- Override parts of default config of a plugin based on the table provided in the chadrc
|
|
|
|
|
|
|
|
-- FUNCTION: tbl_override_req, use `chadrc` plugin config override to modify default config if present
|
|
|
|
-- name = name inside `default_config` / `chadrc`
|
|
|
|
-- default_table = the default configuration table of the plugin
|
|
|
|
-- returns the modified configuration table
|
|
|
|
M.tbl_override_req = function(name, default_table)
|
2022-01-21 06:49:49 -05:00
|
|
|
local override = require("core.utils").load_config().plugins.default_plugin_config_replace[name] or {}
|
2022-01-20 19:12:09 -05:00
|
|
|
return vim.tbl_deep_extend("force", default_table, override)
|
|
|
|
end
|
|
|
|
|
2022-02-13 02:39:07 -05:00
|
|
|
--provide labels to plugins instead of integers
|
|
|
|
M.label_plugins = function(plugins)
|
2022-02-14 15:32:23 -05:00
|
|
|
local plugins_labeled = {}
|
2022-02-13 02:39:07 -05:00
|
|
|
for _, plugin in ipairs(plugins) do
|
|
|
|
plugins_labeled[plugin[1]] = plugin
|
2022-01-31 00:28:52 -05:00
|
|
|
end
|
2022-02-13 02:39:07 -05:00
|
|
|
return plugins_labeled
|
|
|
|
end
|
2022-01-31 03:43:51 -05:00
|
|
|
|
2022-02-13 02:39:07 -05:00
|
|
|
-- remove plugins specified by user from the plugins table
|
|
|
|
M.remove_default_plugins = function(plugins)
|
|
|
|
local removals = require("core.utils").load_config().plugins.default_plugin_remove or {}
|
|
|
|
if not vim.tbl_isempty(removals) then
|
|
|
|
for _, plugin in pairs(removals) do
|
|
|
|
plugins[plugin] = nil
|
2022-01-30 21:13:53 -05:00
|
|
|
end
|
2022-01-30 20:28:19 -05:00
|
|
|
end
|
2022-02-13 02:39:07 -05:00
|
|
|
return plugins
|
|
|
|
end
|
2022-01-31 03:43:51 -05:00
|
|
|
|
2022-02-13 02:39:07 -05:00
|
|
|
-- append user plugins to default plugins
|
|
|
|
M.add_user_plugins = function(plugins)
|
|
|
|
local user_Plugins = require("core.utils").load_config().plugins.install or {}
|
2022-02-19 22:53:34 -05:00
|
|
|
if type(user_Plugins) == "string"
|
|
|
|
then user_Plugins=require(user_Plugins)
|
|
|
|
end
|
2022-02-13 02:39:07 -05:00
|
|
|
if not vim.tbl_isempty(user_Plugins) then
|
|
|
|
for _, v in pairs(user_Plugins) do
|
|
|
|
plugins[v[1]] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return plugins
|
2022-01-30 20:28:19 -05:00
|
|
|
end
|
|
|
|
|
2021-08-22 03:49:15 -04:00
|
|
|
return M
|