aboutsummaryrefslogtreecommitdiffstats
path: root/plugin/50-dotfiles.lua
blob: 122912cb57bea84f9db9ca61c51e533ba56ba8a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
--
-- dotfiles plugin
--

-- Returns true if the current buffer is part of the Dotfiles repository
local function detect_dotfiles_file()
	if vim.env.DOTFILES_DIR == nil then
		return false
	end
	local ret = vim.system(
		{ "git", "--git-dir=" .. vim.env.DOTFILES_DIR, "--work-tree=" .. vim.env.HOME, "ls-files" },
		{ text = true }
	):wait()
	local buf_file_name = string.sub(vim.api.nvim_buf_get_name(0), #vim.env.HOME + 2)
	for _, dotfiles_filename in ipairs(vim.split(ret.stdout, "\n", { trimempty = true })) do
		if dotfiles_filename == buf_file_name then
			return true
		end
	end
	return false
end

vim.api.nvim_create_autocmd("BufEnter", {
	desc = "Detect dotfiles file",
	group = vim.g.dotfiles.augroup,
	callback = function()
		vim.b.dotfiles_file = detect_dotfiles_file()
	end,
})

vim.api.nvim_create_user_command("DotfilesOn", function()
	vim.g.gitgutter_git_args = "--git-dir=" .. vim.env.DOTFILES_DIR .. " --work-tree=" .. vim.env.HOME
end, { desc = "Activate dotfiles (set git env)" })
vim.api.nvim_create_user_command("DotfilesOff", function()
	vim.g.gitgutter_git_args = ""
end, { desc = "Deactivate dotfiles (unset git env)" })