mirror of https://github.com/NvChad/NvChad
Restructure config | Move some to a packer plugin | Lot of cleanup
* move teleacope files, updater and related utils to https://github.com/NvChad/core * restructure config file and directory structure * expose mappings for better escape * allow multiple mappings for some * improve merge table function for the same * move autocommands to a seperate file * rearrange everything alphabetically where sanely possible * rearrange packer plugin list on the basis of trigerred state config structure now . ├── init.lua ├── LICENSE ├── lua │ ├── chadrc.lua │ ├── colors │ │ ├── highlights.lua │ │ ├── init.lua │ │ └── themes │ │ ├── chadracula.lua │ │ ├── everforest.lua │ │ ├── gruvchad.lua │ │ ├── javacafe.lua │ │ ├── mountain.lua │ │ ├── norchad.lua │ │ ├── one-light.lua │ │ ├── onedark.lua │ │ ├── tokyonight.lua │ │ └── tomorrow-night.lua │ ├── core │ │ ├── autocmds.lua │ │ ├── init.lua │ │ ├── mappings.lua │ │ ├── options.lua │ │ └── utils.lua │ ├── default_config.lua │ └── plugins │ ├── configs │ │ ├── autopairs.lua │ │ ├── autosave.lua │ │ ├── bufferline.lua │ │ ├── chadsheet.lua │ │ ├── compe.lua │ │ ├── dashboard.lua │ │ ├── gitsigns.lua │ │ ├── icons.lua │ │ ├── lspconfig.lua │ │ ├── luasnip.lua │ │ ├── nvimtree.lua │ │ ├── others.lua │ │ ├── statusline.lua │ │ ├── telescope.lua │ │ ├── treesitter.lua │ │ └── zenmode.lua │ ├── init.lua │ └── packerInit.lua └── README.mdpull/331/head
parent
44ae0178f4
commit
9ffddb6b52
@ -0,0 +1,36 @@
|
||||
local M = {}
|
||||
|
||||
-- if theme given, load given theme if given, otherwise nvchad_theme
|
||||
M.init = function(theme)
|
||||
if not theme then
|
||||
theme = require("core.utils").load_config().ui.theme
|
||||
end
|
||||
|
||||
-- set the global theme, used at various places like theme switcher, highlights
|
||||
vim.g.nvchad_theme = theme
|
||||
|
||||
local present, base16 = pcall(require, "base16")
|
||||
|
||||
if present then
|
||||
-- first load the base16 theme
|
||||
base16(base16.themes(theme), true)
|
||||
|
||||
-- unload to force reload
|
||||
package.loaded["colors.highlights" or false] = nil
|
||||
-- then load the highlights
|
||||
require "colors.highlights"
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
-- returns a table of colors for givem or current theme
|
||||
M.get = function(theme)
|
||||
if not theme then
|
||||
theme = vim.g.nvchad_theme
|
||||
end
|
||||
|
||||
return require("colors.themes." .. theme)
|
||||
end
|
||||
|
||||
return M
|
@ -0,0 +1,17 @@
|
||||
-- uncomment this if you want to open nvim with a dir
|
||||
-- vim.cmd [[ autocmd BufEnter * if &buftype != "terminal" | lcd %:p:h | endif ]]
|
||||
|
||||
-- Use relative & absolute line numbers in 'n' & 'i' modes respectively
|
||||
-- vim.cmd[[ au InsertEnter * set norelativenumber ]]
|
||||
-- vim.cmd[[ au InsertLeave * set relativenumber ]]
|
||||
|
||||
-- Don't show any numbers inside terminals
|
||||
vim.cmd [[ au TermOpen term://* setlocal nonumber norelativenumber | setfiletype terminal ]]
|
||||
|
||||
-- Don't show status line on certain windows
|
||||
vim.cmd [[ autocmd BufEnter,BufWinEnter,WinEnter,CmdwinEnter,TermEnter * lua require("core.utils").hide_statusline() ]]
|
||||
|
||||
-- Open a file from its last left off position
|
||||
-- vim.cmd [[ au BufReadPost * if expand('%:p') !~# '\m/\.git/' && line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif ]]
|
||||
-- File extension specific tabbing
|
||||
-- vim.cmd [[ autocmd Filetype python setlocal expandtab tabstop=4 shiftwidth=4 softtabstop=4 ]]
|
@ -0,0 +1,15 @@
|
||||
local core_modules = {
|
||||
"core.options",
|
||||
"core.autocmds",
|
||||
"core.mappings",
|
||||
}
|
||||
|
||||
for _, module in ipairs(core_modules) do
|
||||
local ok, err = pcall(require, module)
|
||||
if not ok then
|
||||
error("Error loading " .. module .. "\n\n" .. err)
|
||||
end
|
||||
end
|
||||
|
||||
-- set all the non plugin mappings
|
||||
require("core.mappings").misc()
|
@ -0,0 +1,202 @@
|
||||
local config = require("core.utils").load_config()
|
||||
local maps = config.mappings
|
||||
local plugin_maps = maps.plugin
|
||||
local cmd = vim.cmd
|
||||
|
||||
local function map(mode, lhs, rhs, opts)
|
||||
local options = { noremap = true, silent = true }
|
||||
if opts then
|
||||
options = vim.tbl_extend("force", options, opts)
|
||||
end
|
||||
|
||||
-- if list of keys provided then run set for all of them
|
||||
if type(lhs) == "table" then
|
||||
for _, key in ipairs(lhs) do
|
||||
vim.api.nvim_set_keymap(mode, key, rhs, options)
|
||||
end
|
||||
else
|
||||
vim.api.nvim_set_keymap(mode, lhs, rhs, options)
|
||||
end
|
||||
end
|
||||
|
||||
local opt, M = {}, {}
|
||||
|
||||
-- these mappings will only be called during initialization
|
||||
M.misc = function()
|
||||
local function non_config_mappings()
|
||||
-- dont copy any deleted text , this is disabled by default so uncomment the below mappings if you want them
|
||||
-- map("n", "dd", [=[ "_dd ]=], opt)
|
||||
-- map("v", "dd", [=[ "_dd ]=], opt)
|
||||
-- map("v", "x", [=[ "_x ]=], opt)
|
||||
-- todo: this should be configurable via chadrc
|
||||
|
||||
-- Don't copy the replaced text after pasting in visual mode
|
||||
map("v", "p", '"_dP', opt)
|
||||
|
||||
-- Allow moving the cursor through wrapped lines with j, k, <Up> and <Down>
|
||||
-- http://www.reddit.com/r/vim/comments/2k4cbr/problem_with_gj_and_gk/
|
||||
-- empty mode is same as using :map
|
||||
map("", "j", 'v:count ? "j" : "gj"', { expr = true })
|
||||
map("", "k", 'v:count ? "k" : "gk"', { expr = true })
|
||||
map("", "<Down>", 'v:count ? "j" : "gj"', { expr = true })
|
||||
map("", "<Up>", 'v:count ? "k" : "gk"', { expr = true })
|
||||
|
||||
-- use ESC to turn off search highlighting
|
||||
map("n", "<Esc>", ":noh <CR>", opt)
|
||||
end
|
||||
|
||||
local function optional_mappings()
|
||||
-- navigation within insert mode
|
||||
if config.options.insert_nav then
|
||||
local inav = maps.insert_nav
|
||||
|
||||
map("i", inav.backward, "<Left>", opt)
|
||||
map("i", inav.end_of_line, "<End>", opt)
|
||||
map("i", inav.forward, "<Right>", opt)
|
||||
map("i", inav.next_line, "<Up>", opt)
|
||||
map("i", inav.prev_line, "<Down>", opt)
|
||||
map("i", inav.top_of_line, "<ESC>^i", opt)
|
||||
end
|
||||
|
||||
-- check the theme toggler
|
||||
if config.ui.theme_toggler then
|
||||
map(
|
||||
"n",
|
||||
maps.theme_toggler,
|
||||
":lua require('nvchad').toggle_theme(require('core.utils').load_config().ui.theme_toggler.fav_themes) <CR>",
|
||||
opt
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
local function required_mappings()
|
||||
map("n", maps.close_buffer, ":lua require('core.utils').close_buffer() <CR>", opt) -- close buffer
|
||||
map("n", maps.copy_whole_file, ":%y+ <CR>", opt) -- copy whole file content
|
||||
map("n", maps.new_buffer, ":enew <CR>", opt) -- new buffer
|
||||
map("n", maps.new_tab, ":tabnew <CR>", opt) -- new tabs
|
||||
map("n", maps.line_number_toggle, ":set nu! <CR>", opt) -- toggle numbers
|
||||
map("n", maps.save_file, ":w <CR>", opt) -- ctrl + s to save file
|
||||
|
||||
-- terminal mappings --
|
||||
local term_maps = maps.terminal
|
||||
-- get out of terminal mode
|
||||
map("t", term_maps.esc_termmode, "<C-\\><C-n>", opt)
|
||||
-- hide a term from within terminal mode
|
||||
map("t", term_maps.esc_hide_termmode, "<C-\\><C-n> :lua require('core.utils').close_buffer() <CR>", opt)
|
||||
-- pick a hidden term
|
||||
map("n", term_maps.pick_term, ":Telescope terms <CR>", opt)
|
||||
-- Open terminals
|
||||
-- TODO this opens on top of an existing vert/hori term, fixme
|
||||
map(
|
||||
"n",
|
||||
term_maps.new_horizontal,
|
||||
":execute 15 .. 'new +terminal' | let b:term_type = 'hori' | startinsert <CR>",
|
||||
opt
|
||||
)
|
||||
map("n", term_maps.new_vertical, ":execute 'vnew +terminal' | let b:term_type = 'vert' | startinsert <CR>", opt)
|
||||
map("n", term_maps.new_window, ":execute 'terminal' | let b:term_type = 'wind' | startinsert <CR>", opt)
|
||||
-- terminal mappings end --
|
||||
|
||||
-- Add Packer commands because we are not loading it at startup
|
||||
cmd "silent! command PackerCompile lua require 'plugins' require('packer').compile()"
|
||||
cmd "silent! command PackerInstall lua require 'plugins' require('packer').install()"
|
||||
cmd "silent! command PackerStatus lua require 'plugins' require('packer').status()"
|
||||
cmd "silent! command PackerSync lua require 'plugins' require('packer').sync()"
|
||||
cmd "silent! command PackerUpdate lua require 'plugins' require('packer').update()"
|
||||
|
||||
-- add NvChadUpdate command and mapping
|
||||
cmd "silent! command! NvChadUpdate lua require('nvchad').update_nvchad()"
|
||||
map("n", maps.update_nvchad, ":NvChadUpdate <CR>", opt)
|
||||
end
|
||||
|
||||
non_config_mappings()
|
||||
optional_mappings()
|
||||
required_mappings()
|
||||
end
|
||||
|
||||
-- below are all plugin related mappinsg
|
||||
|
||||
M.better_escape = function()
|
||||
vim.g.better_escape_shortcut = plugin_maps.better_escape.esc_insertmode or { "" }
|
||||
end
|
||||
|
||||
M.bufferline = function()
|
||||
local m = plugin_maps.bufferline
|
||||
|
||||
map("n", m.next_buffer, ":BufferLineCycleNext <CR>", opt)
|
||||
map("n", m.prev_buffer, ":BufferLineCyclePrev <CR>", opt)
|
||||
end
|
||||
|
||||
M.chadsheet = function()
|
||||
local m = plugin_maps.chadsheet
|
||||
|
||||
map("n", m.default_keys, ":lua require('cheatsheet').show_cheatsheet_telescope() <CR>", opt)
|
||||
map(
|
||||
"n",
|
||||
m.user_keys,
|
||||
":lua require('cheatsheet').show_cheatsheet_telescope{bundled_cheatsheets = false, bundled_plugin_cheatsheets = false } <CR>",
|
||||
opt
|
||||
)
|
||||
end
|
||||
|
||||
M.comment = function()
|
||||
local m = plugin_maps.comment.toggle
|
||||
map("n", m, ":CommentToggle <CR>", opt)
|
||||
map("v", m, ":CommentToggle <CR>", opt)
|
||||
end
|
||||
|
||||
M.dashboard = function()
|
||||
local m = plugin_maps.dashboard
|
||||
|
||||
map("n", m.bookmarks, ":DashboardJumpMarks <CR>", opt)
|
||||
map("n", m.new_file, ":DashboardNewFile <CR>", opt)
|
||||
map("n", m.open, ":Dashboard <CR>", opt)
|
||||
map("n", m.session_load, ":SessionLoad <CR>", opt)
|
||||
map("n", m.session_save, ":SessionSave <CR>", opt)
|
||||
end
|
||||
|
||||
M.nvimtree = function()
|
||||
map("n", plugin_maps.nvimtree.toggle, ":NvimTreeToggle <CR>", opt)
|
||||
end
|
||||
|
||||
M.neoformat = function()
|
||||
map("n", plugin_maps.neoformat.format, ":Neoformat <CR>", opt)
|
||||
end
|
||||
|
||||
M.telescope = function()
|
||||
local m = plugin_maps.telescope
|
||||
|
||||
map("n", m.buffers, ":Telescope buffers <CR>", opt)
|
||||
map("n", m.find_files, ":Telescope find_files <CR>", opt)
|
||||
map("n", m.git_commits, ":Telescope git_commits <CR>", opt)
|
||||
map("n", m.git_status, ":Telescope git_status <CR>", opt)
|
||||
map("n", m.help_tags, ":Telescope help_tags <CR>", opt)
|
||||
map("n", m.live_grep, ":Telescope live_grep <CR>", opt)
|
||||
map("n", m.oldfiles, ":Telescope oldfiles <CR>", opt)
|
||||
map("n", m.themes, ":Telescope themes <CR>", opt)
|
||||
end
|
||||
|
||||
M.telescope_media = function()
|
||||
local m = plugin_maps.telescope_media
|
||||
|
||||
map("n", m.media_files, ":Telescope media_files <CR>", opt)
|
||||
end
|
||||
|
||||
M.truezen = function()
|
||||
local m = plugin_maps.truezen
|
||||
|
||||
map("n", m.ataraxis_mode, ":TZAtaraxis <CR>", opt)
|
||||
map("n", m.focus_mode, ":TZFocus <CR>", opt)
|
||||
map("n", m.minimalistic_mode, ":TZMinimalist <CR>", opt)
|
||||
end
|
||||
|
||||
M.vim_fugitive = function()
|
||||
local m = plugin_maps.vim_fugitive
|
||||
|
||||
map("n", m.git, ":Git <CR>", opt)
|
||||
map("n", m.git_blame, ":Git blame <CR>", opt)
|
||||
map("n", m.diff_get_2, ":diffget //2 <CR>", opt)
|
||||
map("n", m.diff_get_3, ":diffget //3 <CR>", opt)
|
||||
end
|
||||
|
||||
return M
|
@ -0,0 +1,265 @@
|
||||
local M = {}
|
||||
|
||||
M.close_buffer = function(bufexpr, force)
|
||||
-- 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
|
||||
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
|
||||
|
||||
local chad_term, type = pcall(function()
|
||||
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))
|
||||
-- swtich to another buff
|
||||
-- TODO switch to next bufffer, this works too
|
||||
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()
|
||||
local hidden = require("core.utils").load_config().ui.plugin.statusline.hidden
|
||||
local shown = require("core.utils").load_config().ui.plugin.statusline.shown
|
||||
local api = vim.api
|
||||
local buftype = api.nvim_buf_get_option("%", "ft")
|
||||
|
||||
-- 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
|
||||
else
|
||||
api.nvim_set_option("laststatus", 2)
|
||||
end
|
||||
end
|
||||
|
||||
-- Base code: https://gist.github.com/revolucas/184aec7998a6be5d2f61b984fac1d7f7
|
||||
-- Changes over it: preserving table 1 contents and also update with table b, without duplicating
|
||||
-- 1st arg - base table
|
||||
-- 2nd arg - table to merge
|
||||
-- 3rg arg - list of nodes as a table, if the node is found replace the from table2 to result, rather than adding the value
|
||||
-- e.g: merge_table(t1, t2, { ['plugin']['truezen']['mappings'] })
|
||||
M.merge_table = function(into, from, nodes_to_replace)
|
||||
-- make sure both are table
|
||||
if type(into) ~= "table" or type(from) ~= "table" then
|
||||
return into
|
||||
end
|
||||
|
||||
local stack, seen = {}, {}
|
||||
local table1, table2 = into, from
|
||||
|
||||
if type(nodes_to_replace) == "table" then
|
||||
-- function that will be executed with loadstring
|
||||
local base_fn = [[
|
||||
return function(table1, table2)
|
||||
local t1, t2 = table1_node or false , table2_node or false
|
||||
if t1 and t2 then
|
||||
table1_node = table2_node
|
||||
end
|
||||
return table1
|
||||
end]]
|
||||
for _, node in ipairs(nodes_to_replace) do
|
||||
-- replace the _node in base_fn to actual given node value
|
||||
local fn = base_fn:gsub("_node", node)
|
||||
-- if the node if found, it is replaced, otherwise table 1 is returned
|
||||
table1 = loadstring(fn)()(table1, table2)
|
||||
end
|
||||
end
|
||||
|
||||
while true do
|
||||
for k, v in pairs(table2) do
|
||||
if type(v) == "table" and type(table1[k]) == "table" then
|
||||
table.insert(stack, { table1[k], table2[k] })
|
||||
else
|
||||
local present = seen[v] or false
|
||||
if not present then
|
||||
if type(k) == "number" then
|
||||
-- add the value to seen table until value is found
|
||||
-- only do when key is number we just want to append to subtables
|
||||
-- todo: maybe improve this
|
||||
|
||||
for _, value in pairs(table1) do
|
||||
if value == v then
|
||||
present = true
|
||||
break
|
||||
end
|
||||
end
|
||||
seen[v] = true
|
||||
if not present then
|
||||
table1[#table1 + 1] = v
|
||||
end
|
||||
else
|
||||
table1[k] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if #stack > 0 then
|
||||
local t = stack[#stack]
|
||||
table1, table2 = t[1], t[2]
|
||||
stack[#stack] = nil
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
return into
|
||||
end
|
||||
|
||||
-- load config
|
||||
-- 1st arg = boolean - whether to force reload
|
||||
-- Modifies _G._NVCHAD_CONFIG global variable
|
||||
M.load_config = function(reload)
|
||||
-- only do the stuff below one time, otherwise just return the set config
|
||||
if _G._NVCHAD_CONFIG_CONTENTS ~= nil and not (reload or false) then
|
||||
return _G._NVCHAD_CONFIG_CONTENTS
|
||||
end
|
||||
|
||||
-- these are the table value which will be always prioritiezed to take user config value
|
||||
local to_replace = {
|
||||
"['mappings']['plugin']['esc_insertmode']",
|
||||
"['mappings']['terminal']['esc_termmode']",
|
||||
"['mappings']['terminal']['esc_hide_termmode']",
|
||||
}
|
||||
|
||||
local default_config = "default_config"
|
||||
local config_name = vim.g.nvchad_user_config or "chadrc"
|
||||
local config_file = vim.fn.stdpath "config" .. "/lua/" .. config_name .. ".lua"
|
||||
|
||||
-- unload the modules if force reload
|
||||
if reload then
|
||||
package.loaded[default_config or false] = nil
|
||||
package.loaded[config_name or false] = nil
|
||||
end
|
||||
|
||||
-- don't enclose in pcall, it better break when default config is faulty
|
||||
_G._NVCHAD_CONFIG_CONTENTS = require(default_config)
|
||||
|
||||
-- user config is not required to run nvchad but a optional
|
||||
-- Make sure the config doesn't break the whole system if user config is not present or in bad state or not a table
|
||||
-- print warning texts if user config file is present
|
||||
-- check if the user config is present
|
||||
if vim.fn.empty(vim.fn.glob(config_file)) < 1 then
|
||||
local present, config = pcall(require, config_name)
|
||||
if present then
|
||||
-- make sure the returned value is table
|
||||
if type(config) == "table" then
|
||||
-- data = require(config_name)
|
||||
_G._NVCHAD_CONFIG_CONTENTS = require("core.utils").merge_table(
|
||||
_G._NVCHAD_CONFIG_CONTENTS,
|
||||
config,
|
||||
to_replace
|
||||
)
|
||||
else
|
||||
print("Warning: " .. config_name .. " sourced successfully but did not return a lua table.")
|
||||
end
|
||||
else
|
||||
print("Warning: " .. config_file .. " is present but sourcing failed.")
|
||||
end
|
||||
end
|
||||
return _G._NVCHAD_CONFIG_CONTENTS
|
||||
end
|
||||
|
||||
return M
|
@ -1,188 +0,0 @@
|
||||
local user_map = require("utils").load_config().mappings
|
||||
local miscMap = user_map.misc
|
||||
|
||||
local cmd = vim.cmd
|
||||
|
||||
local function map(mode, lhs, rhs, opts)
|
||||
local options = { noremap = true, silent = true }
|
||||
if opts then
|
||||
options = vim.tbl_extend("force", options, opts)
|
||||
end
|
||||
vim.api.nvim_set_keymap(mode, lhs, rhs, options)
|
||||
end
|
||||
|
||||
local M = {}
|
||||
local opt = {}
|
||||
|
||||
-- these mappings will only be called during initialization
|
||||
M.misc = function()
|
||||
-- dont copy any deleted text , this is disabled by default so uncomment the below mappings if you want them
|
||||
-- map("n", "dd", [=[ "_dd ]=], opt)
|
||||
-- map("v", "dd", [=[ "_dd ]=], opt)
|
||||
-- map("v", "x", [=[ "_x ]=], opt)
|
||||
-- todo: this should be configurable via chadrc
|
||||
|
||||
-- Don't copy the replaced text after pasting in visual mode
|
||||
map("v", "p", '"_dP', opt)
|
||||
|
||||
-- Allow moving the cursor through wrapped lines with j, k, <Up> and <Down>
|
||||
-- http://www.reddit.com/r/vim/comments/2k4cbr/problem_with_gj_and_gk/
|
||||
-- empty mode is same as using :map
|
||||
map("", "j", 'v:count ? "j" : "gj"', { expr = true })
|
||||
map("", "k", 'v:count ? "k" : "gk"', { expr = true })
|
||||
map("", "<Down>", 'v:count ? "j" : "gj"', { expr = true })
|
||||
map("", "<Up>", 'v:count ? "k" : "gk"', { expr = true })
|
||||
|
||||
-- copy whole file content
|
||||
map("n", miscMap.copywhole_file, ":%y+<CR>", opt)
|
||||
|
||||
-- toggle numbers
|
||||
map("n", miscMap.toggle_linenr, ":set nu!<CR>", opt)
|
||||
|
||||
-- terminals
|
||||
local function terms()
|
||||
local m = user_map.terms
|
||||
|
||||
-- get out of terminal mode
|
||||
map("t", m.esc_termmode, "<C-\\><C-n>", opt)
|
||||
-- hide a term from within terminal mode
|
||||
map("t", m.esc_hide_termmode, "<C-\\><C-n> :lua require('utils').close_buffer() <CR>", opt)
|
||||
-- pick a hidden term
|
||||
map("n", m.pick_term, ":Telescope terms <CR>", opt)
|
||||
|
||||
-- Open terminals
|
||||
-- TODO this opens on top of an existing vert/hori term, fixme
|
||||
map("n", m.new_wind, ":execute 'terminal' | let b:term_type = 'wind' | startinsert <CR>", opt)
|
||||
map("n", m.new_vert, ":execute 'vnew +terminal' | let b:term_type = 'vert' | startinsert <CR>", opt)
|
||||
map("n", m.new_hori, ":execute 15 .. 'new +terminal' | let b:term_type = 'hori' | startinsert <CR>", opt)
|
||||
end
|
||||
terms()
|
||||
|
||||
-- ctrl + s to save file
|
||||
map("n", "<C-s>", ":w <CR>", opt)
|
||||
|
||||
-- use ESC to turn off search highlighting
|
||||
map("n", "<Esc>", ":noh<CR>", opt)
|
||||
|
||||
-- navigation within insert mode
|
||||
local check_insertNav = require("utils").load_config().options.enable_insertNav
|
||||
|
||||
if check_insertNav == true then
|
||||
local m = user_map.insert_nav
|
||||
|
||||
map("i", m.forward, "<Right>", opt)
|
||||
map("i", m.backward, "<Left>", opt)
|
||||
map("i", m.top_of_line, "<ESC>^i", opt)
|
||||
map("i", m.end_of_line, "<End>", opt)
|
||||
map("i", m.next_line, "<Up>", opt)
|
||||
map("i", m.prev_line, "<Down>", opt)
|
||||
end
|
||||
|
||||
-- check the theme toggler
|
||||
local theme_toggler = require("utils").load_config().ui.theme_toggler
|
||||
if theme_toggler == true then
|
||||
local m = user_map.misc.theme_toggle
|
||||
|
||||
map("n", m, ":lua require('utils').toggle_theme(require('utils').load_config().ui.fav_themes)<CR>", opt)
|
||||
end
|
||||
|
||||
-- Packer commands till because we are not loading it at startup
|
||||
cmd "silent! command PackerCompile lua require 'pluginList' require('packer').compile()"
|
||||
cmd "silent! command PackerInstall lua require 'pluginList' require('packer').install()"
|
||||
cmd "silent! command PackerStatus lua require 'pluginList' require('packer').status()"
|
||||
cmd "silent! command PackerSync lua require 'pluginList' require('packer').sync()"
|
||||
cmd "silent! command PackerUpdate lua require 'pluginList' require('packer').update()"
|
||||
|
||||
-- add NvChadUpdate command and mapping
|
||||
cmd "silent! command! NvChadUpdate lua require('utils').update_nvchad()"
|
||||
map("n", user_map.misc.update_nvchad, ":NvChadUpdate<CR>", opt)
|
||||
end
|
||||
|
||||
M.bufferline = function()
|
||||
local m = user_map.bufferline
|
||||
|
||||
map("n", m.new_buffer, ":enew<CR>", opt) -- new buffer
|
||||
map("n", m.newtab, ":tabnew<CR>", opt) -- new tab
|
||||
map("n", m.close, ":lua require('utils').close_buffer() <CR>", opt) -- close buffer
|
||||
|
||||
-- move between tabs
|
||||
|
||||
map("n", m.cycleNext, ":BufferLineCycleNext<CR>", opt)
|
||||
map("n", m.cyclePrev, ":BufferLineCyclePrev<CR>", opt)
|
||||
end
|
||||
|
||||
M.chadsheet = function()
|
||||
local m = user_map.chadsheet
|
||||
|
||||
map("n", m.default_keys, ":lua require('cheatsheet').show_cheatsheet_telescope()<CR>", opt)
|
||||
map(
|
||||
"n",
|
||||
m.user_keys,
|
||||
":lua require('cheatsheet').show_cheatsheet_telescope{bundled_cheatsheets = false, bundled_plugin_cheatsheets = false }<CR>",
|
||||
opt
|
||||
)
|
||||
end
|
||||
|
||||
M.comment_nvim = function()
|
||||
local m = user_map.comment_nvim.comment_toggle
|
||||
map("n", m, ":CommentToggle<CR>", opt)
|
||||
map("v", m, ":CommentToggle<CR>", opt)
|
||||
end
|
||||
|
||||
M.dashboard = function()
|
||||
local m = user_map.dashboard
|
||||
|
||||
map("n", m.open, ":Dashboard<CR>", opt)
|
||||
map("n", m.newfile, ":DashboardNewFile<CR>", opt)
|
||||
map("n", m.bookmarks, ":DashboardJumpMarks<CR>", opt)
|
||||
map("n", m.sessionload, ":SessionLoad<CR>", opt)
|
||||
map("n", m.sessionsave, ":SessionSave<CR>", opt)
|
||||
end
|
||||
|
||||
M.fugitive = function()
|
||||
local m = user_map.fugitive
|
||||
|
||||
map("n", m.Git, ":Git<CR>", opt)
|
||||
map("n", m.diffget_2, ":diffget //2<CR>", opt)
|
||||
map("n", m.diffget_3, ":diffget //3<CR>", opt)
|
||||
map("n", m.git_blame, ":Git blame<CR>", opt)
|
||||
end
|
||||
|
||||
M.nvimtree = function()
|
||||
local m = user_map.nvimtree.treetoggle
|
||||
|
||||
map("n", m, ":NvimTreeToggle<CR>", opt)
|
||||
end
|
||||
|
||||
M.neoformat = function()
|
||||
local m = user_map.neoformat.format
|
||||
map("n", m, ":Neoformat<CR>", opt)
|
||||
end
|
||||
|
||||
M.truezen = function()
|
||||
local m = user_map.truezen
|
||||
|
||||
map("n", m.ataraxisMode, ":TZAtaraxis<CR>", opt)
|
||||
map("n", m.minimalisticmode, ":TZMinimalist<CR>", opt)
|
||||
map("n", m.focusmode, ":TZFocus<CR>", opt)
|
||||
end
|
||||
|
||||
M.telescope = function()
|
||||
local m = user_map.telescope
|
||||
|
||||
map("n", m.live_grep, ":Telescope live_grep<CR>", opt)
|
||||
map("n", m.git_status, ":Telescope git_status <CR>", opt)
|
||||
map("n", m.git_commits, ":Telescope git_commits <CR>", opt)
|
||||
map("n", m.find_files, ":Telescope find_files <CR>", opt)
|
||||
map("n", m.buffers, ":Telescope buffers<CR>", opt)
|
||||
map("n", m.help_tags, ":Telescope help_tags<CR>", opt)
|
||||
map("n", m.oldfiles, ":Telescope oldfiles<CR>", opt)
|
||||
map("n", m.themes, ":Telescope themes<CR>", opt)
|
||||
end
|
||||
|
||||
M.telescope_media = function()
|
||||
local m = user_map.telescope_media
|
||||
map("n", m.media_files, ":Telescope media_files <CR>", opt)
|
||||
end
|
||||
|
||||
return M
|
@ -1,25 +0,0 @@
|
||||
local present, chadsheet = pcall(require, "cheatsheet")
|
||||
|
||||
if not present then
|
||||
return
|
||||
end
|
||||
|
||||
local mappings = require("utils").load_config().mappings
|
||||
|
||||
-- add user mappings to the cheetsheet
|
||||
for section, data in pairs(mappings) do
|
||||
for description, keymap in pairs(data) do
|
||||
chadsheet.add_cheat(description, keymap, section)
|
||||
end
|
||||
end
|
||||
|
||||
require("cheatsheet").setup {
|
||||
|
||||
bundled_cheatsheets = {
|
||||
enabled = { "default" },
|
||||
disabled = { "unicode", "nerd-fonts" },
|
||||
},
|
||||
|
||||
bundled_plugin_cheatsheets = false,
|
||||
include_only_installed_plugins = true,
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
local present, chadsheet = pcall(require, "cheatsheet")
|
||||
|
||||
if not present then
|
||||
return
|
||||
end
|
||||
|
||||
local mappings = require("core.utils").load_config().mappings
|
||||
|
||||
-- add user mappings to the cheetsheet
|
||||
-- improve this function to not hardcode plugin
|
||||
local function add_to_chadsheet(section, keymap, desc)
|
||||
if section == "plugin" then
|
||||
for sec, key in pairs(mappings.plugin) do
|
||||
add_to_chadsheet(sec, key, sec)
|
||||
end
|
||||
else
|
||||
if type(keymap) == "table" then
|
||||
for sec, key in pairs(keymap) do
|
||||
if type(sec) == "number" then
|
||||
add_to_chadsheet(section, key, desc or section)
|
||||
else
|
||||
add_to_chadsheet(sec, key, desc or section)
|
||||
end
|
||||
end
|
||||
else
|
||||
chadsheet.add_cheat(section, keymap, desc or "Misc")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for section, keymap in pairs(mappings) do
|
||||
add_to_chadsheet(section, keymap)
|
||||
end
|
||||
|
||||
require("cheatsheet").setup {
|
||||
|
||||
bundled_cheatsheets = {
|
||||
enabled = { "default" },
|
||||
disabled = { "unicode", "nerd-fonts" },
|
||||
},
|
||||
|
||||
bundled_plugin_cheatsheets = false,
|
||||
include_only_installed_plugins = true,
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
local g = vim.g
|
||||
local fn = vim.fn
|
||||
|
||||
local plugins_count = fn.len(fn.globpath("~/.local/share/nvim/site/pack/packer/start", "*", 0, 1))
|
||||
-- local plugins_count = fn.len(fn.globpath("~/.local/share/nvim/site/pack/packer/start", "*", 0, 1))
|
||||
|
||||
g.dashboard_disable_at_vimenter = 1 -- dashboard is disabled by default
|
||||
g.dashboard_disable_statusline = 1
|
@ -1,143 +0,0 @@
|
||||
-- This file can be loaded as a telescope extension
|
||||
local M = {}
|
||||
|
||||
-- Custom theme picker
|
||||
-- Most of the code is copied from telescope buffer builtin
|
||||
-- Src: https://github.com/nvim-telescope/telescope.nvim/blob/master/lua/telescope/builtin/internal.lua
|
||||
M.term_picker = function(opts)
|
||||
local pickers, finders, previewers, make_entry, actions, action_state, utils, conf
|
||||
if pcall(require, "telescope") then
|
||||
pickers = require "telescope.pickers"
|
||||
finders = require "telescope.finders"
|
||||
previewers = require "telescope.previewers"
|
||||
|
||||
make_entry = require "telescope.make_entry"
|
||||
actions = require "telescope.actions"
|
||||
action_state = require "telescope.actions.state"
|
||||
utils = require "telescope.utils"
|
||||
conf = require("telescope.config").values
|
||||
else
|
||||
error "Cannot find telescope!"
|
||||
end
|
||||
|
||||
local filter = vim.tbl_filter
|
||||
|
||||
local local_utils = require "utils"
|
||||
|
||||
-- buffer number and name
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
local bufname = vim.api.nvim_buf_get_name(bufnr)
|
||||
|
||||
local bufnrs = filter(function(b)
|
||||
local present_type, type = pcall(function()
|
||||
return vim.api.nvim_buf_get_var(b, "term_type")
|
||||
end)
|
||||
|
||||
if not present_type then
|
||||
-- let's only terms that we created
|
||||
return false
|
||||
end
|
||||
|
||||
-- if 1 ~= vim.fn.buflisted(b) then
|
||||
-- return false
|
||||
-- end
|
||||
-- only hide unloaded buffers if opts.show_all_buffers is false, keep them listed if true or nil
|
||||
if opts.show_all_buffers == false and not vim.api.nvim_buf_is_loaded(b) then
|
||||
return false
|
||||
end
|
||||
if opts.ignore_current_buffer and b == vim.api.nvim_get_current_buf() then
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end, vim.api.nvim_list_bufs())
|
||||
if not next(bufnrs) then
|
||||
return
|
||||
end
|
||||
if opts.sort_mru then
|
||||
table.sort(bufnrs, function(a, b)
|
||||
return vim.fn.getbufinfo(a)[1].lastused > vim.fn.getbufinfo(b)[1].lastused
|
||||
end)
|
||||
end
|
||||
|
||||
local buffers = {}
|
||||
local default_selection_idx = 1
|
||||
for _, bufnr in ipairs(bufnrs) do
|
||||
local flag = bufnr == vim.fn.bufnr "" and "%" or (bufnr == vim.fn.bufnr "#" and "#" or " ")
|
||||
|
||||
if opts.sort_lastused and not opts.ignore_current_buffer and flag == "#" then
|
||||
default_selection_idx = 2
|
||||
end
|
||||
|
||||
local element = {
|
||||
bufnr = bufnr,
|
||||
flag = flag,
|
||||
info = vim.fn.getbufinfo(bufnr)[1],
|
||||
}
|
||||
|
||||
if opts.sort_lastused and (flag == "#" or flag == "%") then
|
||||
local idx = ((buffers[1] ~= nil and buffers[1].flag == "%") and 2 or 1)
|
||||
table.insert(buffers, idx, element)
|
||||
else
|
||||
table.insert(buffers, element)
|
||||
end
|
||||
end
|
||||
|
||||
if not opts.bufnr_width then
|
||||
local max_bufnr = math.max(unpack(bufnrs))
|
||||
opts.bufnr_width = #tostring(max_bufnr)
|
||||
end
|
||||
|
||||
pickers.new(opts, {
|
||||
prompt_title = "Terminal buffers",
|
||||
finder = finders.new_table {
|
||||
results = buffers,
|
||||
entry_maker = opts.entry_maker or make_entry.gen_from_buffer(opts),
|
||||
},
|
||||
previewer = conf.grep_previewer(opts),
|
||||
sorter = conf.generic_sorter(opts),
|
||||
default_selection_index = default_selection_idx,
|
||||
attach_mappings = function(prompt_bufnr)
|
||||
actions.select_default:replace(function()
|
||||
local entry = action_state.get_selected_entry()
|
||||
actions.close(prompt_bufnr)
|
||||
|
||||
local buf = entry.bufnr
|
||||
|
||||
local chad_term, type = pcall(function()
|
||||
return vim.api.nvim_buf_get_var(buf, "term_type")
|
||||
end)
|
||||
|
||||
-- TODO buffer checks/error detection (make sure we do get a buf)
|
||||
|
||||
if chad_term then
|
||||
if type == "wind" then
|
||||
-- swtich to term buff & show in bufferline
|
||||
vim.cmd(string.format("b %d | setlocal bl", buf))
|
||||
-- vim.cmd('startinsert') TODO fix this
|
||||
elseif type == "vert" then
|
||||
vim.cmd(string.format("vsp #%d", buf))
|
||||
-- vim.cmd('startinsert') TODO fix this
|
||||
elseif type == "hori" then
|
||||
-- TODO change 15 to a chad config var number
|
||||
vim.cmd(string.format("15 sp #%d ", buf))
|
||||
-- vim.cmd('startinsert') TODO fix this
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
return true
|
||||
end,
|
||||
}):find()
|
||||
end
|
||||
|
||||
-- register term picker as terms to telescope
|
||||
local present, telescope = pcall(require, "telescope")
|
||||
if present then
|
||||
return telescope.register_extension {
|
||||
exports = {
|
||||
terms = M.term_picker,
|
||||
},
|
||||
}
|
||||
else
|
||||
error "Cannot find telescope!"
|
||||
end
|
@ -1,142 +0,0 @@
|
||||
-- This file can be loaded as a telescope extension
|
||||
local M = {}
|
||||
|
||||
-- Custom theme picker
|
||||
-- Most of the code is copied from telescope colorscheme plugin, mostly for preview creation
|
||||
M.theme_switcher = function(opts)
|
||||
local pickers, finders, previewers, actions, action_state, utils, conf
|
||||
if pcall(require, "telescope") then
|
||||
pickers = require "telescope.pickers"
|
||||
finders = require "telescope.finders"
|
||||
previewers = require "telescope.previewers"
|
||||
|
||||
actions = require "telescope.actions"
|
||||
action_state = require "telescope.actions.state"
|
||||
utils = require "telescope.utils"
|
||||
conf = require("telescope.config").values
|
||||
else
|
||||
error "Cannot find telescope!"
|
||||
end
|
||||
|
||||
local local_utils = require "utils"
|
||||
local reload_theme = local_utils.reload_theme
|
||||
|
||||
-- get a table of available themes
|
||||
local themes = local_utils.list_themes()
|
||||
if next(themes) ~= nil then
|
||||
-- save this to use it for later to restore if theme not changed
|
||||
local current_theme = vim.g.nvchad_theme
|
||||
local new_theme = ""
|
||||
local change = false
|
||||
|
||||
-- buffer number and name
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
local bufname = vim.api.nvim_buf_get_name(bufnr)
|
||||
|
||||
local previewer
|
||||
|
||||
-- in case its not a normal buffer
|
||||
if vim.fn.buflisted(bufnr) ~= 1 then
|
||||
local deleted = false
|
||||
local function del_win(win_id)
|
||||
if win_id and vim.api.nvim_win_is_valid(win_id) then
|
||||
utils.buf_delete(vim.api.nvim_win_get_buf(win_id))
|
||||
pcall(vim.api.nvim_win_close, win_id, true)
|
||||
end
|
||||
end
|
||||
|
||||
previewer = previewers.new {
|
||||
preview_fn = function(_, entry, status)
|
||||
if not deleted then
|
||||
deleted = true
|
||||
del_win(status.preview_win)
|
||||
del_win(status.preview_border_win)
|
||||
end
|
||||
reload_theme(entry.value)
|
||||
end,
|
||||
}
|
||||
else
|
||||
-- show current buffer content in previewer
|
||||
previewer = previewers.new_buffer_previewer {
|
||||
get_buffer_by_name = function()
|
||||
return bufname
|
||||
end,
|
||||
define_preview = function(self, entry)
|
||||
if vim.loop.fs_stat(bufname) then
|
||||
conf.buffer_previewer_maker(bufname, self.state.bufnr, { bufname = self.state.bufname })
|
||||
else
|
||||
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
||||
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines)
|
||||
end
|
||||
reload_theme(entry.value)
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
local picker = pickers.new {
|
||||
prompt_title = "Set NvChad color",
|
||||
finder = finders.new_table(themes),
|
||||
previewer = previewer,
|
||||
sorter = conf.generic_sorter(opts),
|
||||
attach_mappings = function()
|
||||
actions.select_default:replace(
|
||||
-- if a entry is selected, change current_theme to that
|
||||
function(prompt_bufnr)
|
||||
local selection = action_state.get_selected_entry()
|
||||
new_theme = selection.value
|
||||
change = true
|
||||
actions.close(prompt_bufnr)
|
||||
end
|
||||
)
|
||||
return true
|
||||
end,
|
||||
}
|
||||
|
||||
-- rewrite picker.close_windows
|
||||
local close_windows = picker.close_windows
|
||||
picker.close_windows = function(status)
|
||||
close_windows(status)
|
||||
-- now apply the theme, if success, then ask for default theme change
|
||||
local final_theme
|
||||
if change then
|
||||
final_theme = new_theme
|
||||
else
|
||||
final_theme = current_theme
|
||||
end
|
||||
|
||||
if reload_theme(final_theme) then
|
||||
if change then
|
||||
-- ask for confirmation to set as default theme
|
||||
local ans = string.lower(vim.fn.input("Set " .. new_theme .. " as default theme ? [y/N] ")) == "y"
|
||||
local_utils.clear_cmdline()
|
||||
if ans then
|
||||
local_utils.change_theme(current_theme, final_theme)
|
||||
else
|
||||
-- will be used in restoring nvchad theme var
|
||||
final_theme = current_theme
|
||||
end
|
||||
end
|
||||
else
|
||||
final_theme = current_theme
|
||||
end
|
||||
-- set nvchad_theme global var
|
||||
vim.g.nvchad_theme = final_theme
|
||||
end
|
||||
-- launch the telescope picker
|
||||
picker:find()
|
||||
else
|
||||
print("No themes found in " .. vim.fn.stdpath "config" .. "/lua/themes")
|
||||
end
|
||||
end
|
||||
|
||||
-- register theme swticher as themes to telescope
|
||||
local present, telescope = pcall(require, "telescope")
|
||||
if present then
|
||||
return telescope.register_extension {
|
||||
exports = {
|
||||
themes = M.theme_switcher,
|
||||
},
|
||||
}
|
||||
else
|
||||
error "Cannot find telescope!"
|
||||
end
|
@ -1,12 +0,0 @@
|
||||
local chad_theme = require("utils").load_config().ui.theme
|
||||
vim.g.nvchad_theme = chad_theme
|
||||
|
||||
local present, base16 = pcall(require, "base16")
|
||||
|
||||
if present then
|
||||
base16(base16.themes(chad_theme), true)
|
||||
require "highlights"
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
@ -1,531 +0,0 @@
|
||||
local M = {}
|
||||
|
||||
-- 1st arg as current theme, 2nd as new theme
|
||||
M.change_theme = function(current_theme, new_theme)
|
||||
if current_theme == nil or new_theme == nil then
|
||||
print "Error: Provide current and new theme name"
|
||||
return false
|
||||
end
|
||||
if current_theme == new_theme then
|
||||
return
|
||||
end
|
||||
|
||||
local user_config = vim.g.nvchad_user_config
|
||||
local file = vim.fn.stdpath "config" .. "/lua/" .. user_config .. ".lua"
|
||||
-- store in data variable
|
||||
local data = assert(M.file("r", file))
|
||||
-- escape characters which can be parsed as magic chars
|
||||
current_theme = current_theme:gsub("%p", "%%%0")
|
||||
new_theme = new_theme:gsub("%p", "%%%0")
|
||||
local find = "theme = .?" .. current_theme .. ".?"
|
||||
local replace = 'theme = "' .. new_theme .. '"'
|
||||
local content = string.gsub(data, find, replace)
|
||||
-- see if the find string exists in file
|
||||
if content == data then
|
||||
print("Error: Cannot change default theme with " .. new_theme .. ", edit " .. file .. " manually")
|
||||
return false
|
||||
else
|
||||
assert(M.file("w", file, content))
|
||||
end
|
||||
end
|
||||
|
||||
M.clear_cmdline = function()
|
||||
vim.defer_fn(function()
|
||||
vim.cmd "echo"
|
||||
end, 0)
|
||||
end
|
||||
|
||||
M.close_buffer = function(bufexpr, force)
|
||||
-- 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
|
||||
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
|
||||
|
||||
local chad_term, type = pcall(function()
|
||||
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))
|
||||
-- swtich to another buff
|
||||
-- TODO switch to next bufffer, this works too
|
||||
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
|
||||
|
||||
-- wrapper to use vim.api.nvim_echo
|
||||
-- table of {string, highlight}
|
||||
-- e.g echo({{"Hello", "Title"}, {"World"}})
|
||||
M.echo = function(opts)
|
||||
if opts == nil or type(opts) ~= "table" then
|
||||
return
|
||||
end
|
||||
vim.api.nvim_echo(opts, false, {})
|
||||
end
|
||||
|
||||
-- 1st arg - r or w
|
||||
-- 2nd arg - file path
|
||||
-- 3rd arg - content if 1st arg is w
|
||||
-- return file data on read, nothing on write
|
||||
M.file = function(mode, filepath, content)
|
||||
local data
|
||||
local fd = assert(vim.loop.fs_open(filepath, mode, 438))
|
||||
local stat = assert(vim.loop.fs_fstat(fd))
|
||||
if stat.type ~= "file" then
|
||||
data = false
|
||||
else
|
||||
if mode == "r" then
|
||||
data = assert(vim.loop.fs_read(fd, stat.size, 0))
|
||||
else
|
||||
assert(vim.loop.fs_write(fd, content, 0))
|
||||
data = true
|
||||
end
|
||||
end
|
||||
assert(vim.loop.fs_close(fd))
|
||||
return data
|
||||
end
|
||||
|
||||
-- hide statusline
|
||||
-- tables fetched from load_config function
|
||||
M.hide_statusline = function(values)
|
||||
local hidden = require("utils").load_config().ui.statusline.hidden
|
||||
local shown = require("utils").load_config().ui.statusline.shown
|
||||
local api = vim.api
|
||||
local buftype = api.nvim_buf_get_option("%", "ft")
|
||||
|
||||
-- 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
|
||||
else
|
||||
api.nvim_set_option("laststatus", 2)
|
||||
end
|
||||
end
|
||||
|
||||
-- return a table of available themes
|
||||
M.list_themes = function(return_type)
|
||||
local themes = {}
|
||||
-- folder where theme files are stored
|
||||
local themes_folder = vim.fn.stdpath "config" .. "/lua/themes"
|
||||
-- list all the contents of the folder and filter out files with .lua extension, then append to themes table
|
||||
local fd = vim.loop.fs_scandir(themes_folder)
|
||||
if fd then
|
||||
while true do
|
||||
local name, typ = vim.loop.fs_scandir_next(fd)
|
||||
if name == nil then
|
||||
break
|
||||
end
|
||||
if typ ~= "directory" and string.find(name, ".lua") then
|
||||
-- return the table values as keys if specified
|
||||
if return_type == "keys_as_value" then
|
||||
themes[vim.fn.fnamemodify(name, ":r")] = true
|
||||
else
|
||||
table.insert(themes, vim.fn.fnamemodify(name, ":r"))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return themes
|
||||
end
|
||||
|
||||
-- Base code: https://gist.github.com/revolucas/184aec7998a6be5d2f61b984fac1d7f7
|
||||
-- Changes over it: preserving table 1 contents and also update with table b, without duplicating
|
||||
-- 1st arg - base table, 2nd arg - table to merge
|
||||
M.merge_table = function(into, from)
|
||||
-- make sure both are table
|
||||
if type(into) ~= "table" or type(from) ~= "table" then
|
||||
return into
|
||||
end
|
||||
local stack, seen = {}, {}
|
||||
local table1, table2 = into, from
|
||||
while true do
|
||||
for k, v in pairs(table2) do
|
||||
if type(v) == "table" and type(table1[k]) == "table" then
|
||||
table.insert(stack, { table1[k], table2[k] })
|
||||
else
|
||||
local present = seen[v] or false
|
||||
if not present then
|
||||
if type(k) == "number" then
|
||||
-- add the value to seen table until value is found
|
||||
-- only do when key is number we just want to append to subtables
|
||||
-- todo: maybe improve this
|
||||
|
||||
for _, value in pairs(table1) do
|
||||
if value == v then
|
||||
present = true
|
||||
break
|
||||
end
|
||||
end
|
||||
seen[v] = true
|
||||
if not present then
|
||||
table1[#table1 + 1] = v
|
||||
end
|
||||
else
|
||||
table1[k] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if #stack > 0 then
|
||||
local t = stack[#stack]
|
||||
table1, table2 = t[1], t[2]
|
||||
stack[#stack] = nil
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
return into
|
||||
end
|
||||
|
||||
-- load config
|
||||
-- 1st arg = boolean - whether to force reload
|
||||
-- Modifies _G._NVCHAD_CONFIG global variable
|
||||
M.load_config = function(reload)
|
||||
-- only do the stuff below one time, otherwise just return the set config
|
||||
if _G._NVCHAD_CONFIG_CONTENTS ~= nil and not (reload or false) then
|
||||
return _G._NVCHAD_CONFIG_CONTENTS
|
||||
end
|
||||
|
||||
local default_config = "default_config"
|
||||
local config_name = vim.g.nvchad_user_config or "chadrc"
|
||||
local config_file = vim.fn.stdpath "config" .. "/lua/" .. config_name .. ".lua"
|
||||
|
||||
-- unload the modules if force reload
|
||||
if reload then
|
||||
package.loaded[default_config or false] = nil
|
||||
package.loaded[config_name or false] = nil
|
||||
end
|
||||
|
||||
-- don't enclose in pcall, it better break when default config is faulty
|
||||
_G._NVCHAD_CONFIG_CONTENTS = require(default_config)
|
||||
|
||||
-- user config is not required to run nvchad but a optional
|
||||
-- Make sure the config doesn't break the whole system if user config is not present or in bad state or not a table
|
||||
-- print warning texts if user config file is present
|
||||
-- check if the user config is present
|
||||
if vim.fn.empty(vim.fn.glob(config_file)) < 1 then
|
||||
local present, config = pcall(require, config_name)
|
||||
if present then
|
||||
-- make sure the returned value is table
|
||||
if type(config) == "table" then
|
||||
-- data = require(config_name)
|
||||
_G._NVCHAD_CONFIG_CONTENTS = require("utils").merge_table(_G._NVCHAD_CONFIG_CONTENTS, config)
|
||||
else
|
||||
print("Warning: " .. config_name .. " sourced successfully but did not return a lua table.")
|
||||
end
|
||||
else
|
||||
print("Warning: " .. config_file .. " is present but sourcing failed.")
|
||||
end
|
||||
end
|
||||
return _G._NVCHAD_CONFIG_CONTENTS
|
||||
end
|
||||
|
||||
-- reload a plugin ( will try to load even if not loaded)
|
||||
-- can take a string or list ( table )
|
||||
-- return true or false
|
||||
M.reload_plugin = function(plugins)
|
||||
local status = true
|
||||
local function _reload_plugin(plugin)
|
||||
local loaded = package.loaded[plugin]
|
||||
if loaded then
|
||||
package.loaded[plugin] = nil
|
||||
end
|
||||
if not pcall(require, plugin) then
|
||||
print("Error: Cannot load " .. plugin .. " plugin!")
|
||||
status = false
|
||||
end
|
||||
end
|
||||
|
||||
if type(plugins) == "string" then
|
||||
_reload_plugin(plugins)
|
||||
elseif type(plugins) == "table" then
|
||||
for _, plugin in ipairs(plugins) do
|
||||
_reload_plugin(plugin)
|
||||
end
|
||||
end
|
||||
return status
|
||||
end
|
||||
|
||||
-- reload themes without restarting vim
|
||||
-- if no theme name given then reload the current theme
|
||||
M.reload_theme = function(theme_name)
|
||||
local reload_plugin = require("utils").reload_plugin
|
||||
|
||||
-- if theme name is empty or nil, then reload the current theme
|
||||
if theme_name == nil or theme_name == "" then
|
||||
theme_name = vim.g.nvchad_theme
|
||||
end
|
||||
|
||||
if not pcall(require, "themes/" .. theme_name) then
|
||||
print("No such theme ( " .. theme_name .. " )")
|
||||
return false
|
||||
end
|
||||
|
||||
vim.g.nvchad_theme = theme_name
|
||||
|
||||
-- reload the base16 theme
|
||||
local ok, base16 = pcall(require, "base16")
|
||||
if not ok then
|
||||
print "Error: Cannot load base16 plugin!"
|
||||
return false
|
||||
end
|
||||
base16(base16.themes(theme_name), true)
|
||||
|
||||
if
|
||||
not reload_plugin {
|
||||
"highlights",
|
||||
"plugins.bufferline",
|
||||
"galaxyline",
|
||||
"plugins.statusline",
|
||||
}
|
||||
then
|
||||
print "Error: Not able to reload all plugins."
|
||||
return false
|
||||
end
|
||||
|
||||
-- yes, this is very hacky, but due to new_async in
|
||||
-- https://github.com/glepnir/galaxyline.nvim/blob/main/lua/galaxyline/provider.lua#L5-L36
|
||||
-- it doesn't work properly and some statusline stuff dissapears
|
||||
local vcs = require "galaxyline.provider_vcs"
|
||||
local fileinfo = require "galaxyline.provider_fileinfo"
|
||||
local buffer = require "galaxyline.provider_buffer"
|
||||
local extension = require "galaxyline.provider_extensions"
|
||||
local whitespace = require "galaxyline.provider_whitespace"
|
||||
local lspclient = require "galaxyline.provider_lsp"
|
||||
_G.galaxyline_providers = {
|
||||
BufferIcon = buffer.get_buffer_type_icon,
|
||||
BufferNumber = buffer.get_buffer_number,
|
||||
FileTypeName = buffer.get_buffer_filetype,
|
||||
GitBranch = vcs.get_git_branch,
|
||||
DiffAdd = vcs.diff_add,
|
||||
DiffModified = vcs.diff_modified,
|
||||
DiffRemove = vcs.diff_remove,
|
||||
LineColumn = fileinfo.line_column,
|
||||
FileFormat = fileinfo.get_file_format,
|
||||
FileEncode = fileinfo.get_file_encode,
|
||||
FileSize = fileinfo.get_file_size,
|
||||
FileIcon = fileinfo.get_file_icon,
|
||||
FileName = fileinfo.get_current_file_name,
|
||||
SFileName = fileinfo.filename_in_special_buffer,
|
||||
LinePercent = fileinfo.current_line_percent,
|
||||
ScrollBar = extension.scrollbar_instance,
|
||||
VistaPlugin = extension.vista_nearest,
|
||||
WhiteSpace = whitespace.get_item,
|
||||
GetLspClient = lspclient.get_lsp_client,
|
||||
}
|
||||
local diagnostic = require "galaxyline.provider_diagnostic"
|
||||
_G.galaxyline_providers.DiagnosticError = diagnostic.get_diagnostic_error
|
||||
_G.galaxyline_providers.DiagnosticWarn = diagnostic.get_diagnostic_warn
|
||||
_G.galaxyline_providers.DiagnosticHint = diagnostic.get_diagnostic_hint
|
||||
_G.galaxyline_providers.DiagnosticInfo = diagnostic.get_diagnostic_info
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
-- toggle between 2 themes
|
||||
-- argument should be a table with 2 theme names
|
||||
M.toggle_theme = function(themes)
|
||||
local current_theme = vim.g.current_nvchad_theme or vim.g.nvchad_theme
|
||||
for _, name in ipairs(themes) do
|
||||
if name ~= current_theme then
|
||||
if require("utils").reload_theme(name) then
|
||||
-- open a buffer and close it to reload the statusline
|
||||
vim.cmd "new|bwipeout"
|
||||
vim.g.current_nvchad_theme = name
|
||||
if M.change_theme(vim.g.nvchad_theme, name) then
|
||||
vim.g.nvchad_theme = name
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- update nvchad
|
||||
M.update_nvchad = function()
|
||||
-- in all the comments below, config means user config
|
||||
local config_path = vim.fn.stdpath "config"
|
||||
local config_name = vim.g.nvchad_user_config or "chadrc"
|
||||
local config_file = config_path .. "/lua/" .. config_name .. ".lua"
|
||||
-- generate a random file name
|
||||
local config_file_backup = config_path .. "/" .. config_name .. ".lua.bak." .. math.random()
|
||||
local utils = require "utils"
|
||||
local echo = utils.echo
|
||||
local current_config = utils.load_config()
|
||||
local update_url = current_config.options.update_url or "https://github.com/NvChad/NvChad"
|
||||
local update_branch = current_config.options.update_branch or "main"
|
||||
|
||||
-- ask the user for confirmation to update because we are going to run git reset --hard
|
||||
echo { { "Url: ", "Title" }, { update_url } }
|
||||
echo { { "Branch: ", "Title" }, { update_branch } }
|
||||
echo {
|
||||
{ "\nUpdater will run", "WarningMsg" },
|
||||
{ " git reset --hard " },
|
||||
{
|
||||
"in config folder, so changes to existing repo files except ",
|
||||
"WarningMsg",
|
||||
},
|
||||
|
||||
{ config_name },
|
||||
{ " will be lost!\n\nUpdate NvChad ? [y/N]", "WarningMsg" },
|
||||
}
|
||||
|
||||
local ans = string.lower(vim.fn.input "-> ") == "y"
|
||||
utils.clear_cmdline()
|
||||
if not ans then
|
||||
echo { { "Update cancelled!", "Title" } }
|
||||
return
|
||||
end
|
||||
|
||||
-- first try to fetch contents of config, this will make sure it is readable and taking backup of its contents
|
||||
local config_contents = utils.file("r", config_file)
|
||||
-- also make a local backup in ~/.config/nvim, will be removed when config is succesfully restored
|
||||
utils.file("w", config_file_backup, config_contents)
|
||||
-- write original config file with its contents, will make sure charc is writable, this maybe overkill but a little precaution always helps
|
||||
utils.file("w", config_file, config_contents)
|
||||
|
||||
-- function that will executed when git commands are done
|
||||
local function update_exit(_, code)
|
||||
-- restore config file irrespective of whether git commands were succesfull or not
|
||||
if pcall(function()
|
||||
utils.file("w", config_file, config_contents)
|
||||
end) then
|
||||
-- config restored succesfully, remove backup file that was created
|
||||
if not pcall(os.remove, config_file_backup) then
|
||||
echo { { "Warning: Failed to remove backup chadrc, remove manually.", "WarningMsg" } }
|
||||
echo { { "Path: ", "WarningMsg" }, { config_file_backup } }
|
||||
end
|
||||
else
|
||||
echo { { "Error: Restoring " .. config_name .. " failed.\n", "ErrorMsg" } }
|
||||
echo { { "Backed up " .. config_name .. " path: " .. config_file_backup .. "\n\n", "None" } }
|
||||
end
|
||||
|
||||
-- close the terminal buffer only if update was success, as in case of error, we need the error message
|
||||
if code == 0 then
|
||||
vim.cmd "bd!"
|
||||
echo { { "NvChad succesfully updated.\n", "String" } }
|
||||
else
|
||||
echo { { "Error: NvChad Update failed.\n", "ErrorMsg" } }
|
||||
end
|
||||
end
|
||||
|
||||
-- git commands that will executed, reset in case config was modfied
|
||||
-- use --rebase, to not mess up if the local repo is outdated
|
||||
local update_script = table.concat({
|
||||
"git reset --hard && git pull --set-upstream",
|
||||
update_url,
|
||||
update_branch,
|
||||
"--rebase",
|
||||
}, " ")
|
||||
|
||||
-- open a new buffer
|
||||
vim.cmd "new"
|
||||
-- finally open the pseudo terminal buffer
|
||||
vim.fn.termopen(update_script, {
|
||||
-- change dir to config path so we don't need to move in script
|
||||
cwd = config_path,
|
||||
on_exit = update_exit,
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
Loading…
Reference in New Issue