diff --git a/lua/plugins/chezmoi.lua b/lua/plugins/chezmoi.lua index e50d7ad..6d22e3f 100644 --- a/lua/plugins/chezmoi.lua +++ b/lua/plugins/chezmoi.lua @@ -4,75 +4,77 @@ return { 'xvzc/chezmoi.nvim', dependencies = { 'nvim-lua/plenary.nvim' }, config = function() - -- Auto run chezmoi apply - -- e.g. ~/.local/share/chezmoi/* + require('chezmoi').setup { + edit = { watch = true, force = false }, + events = { + on_open = { notification = { enable = true, msg = 'Opened chezmoi file' } }, + on_watch = { notification = { enable = false } }, + on_apply = { notification = { enable = true, msg = 'Applied' } }, + }, + telescope = { select = { '' } }, + } + + local chezmoi_dir = vim.env.HOME .. '/.local/share/chezmoi' + vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, { - pattern = { vim.fn.expand '~/.local/share/chezmoi/' .. '*' }, + group = vim.api.nvim_create_augroup('chezmoi_auto_edit', { clear = true }), callback = function(ev) - local bufnr = ev.buf - local edit_watch = function() - require('chezmoi.commands.__edit').watch(bufnr) + if vim.fn.executable 'chezmoi' == 0 then + return + end + + local filepath = vim.api.nvim_buf_get_name(ev.buf) + if filepath == '' then + return + end + + -- If already in chezmoi source dir, enable auto-apply + if vim.startswith(filepath, chezmoi_dir .. '/') then + vim.schedule(function() + require('chezmoi.commands.__edit').watch(ev.buf) + end) + return end - vim.schedule(edit_watch) + + -- Check if file is managed and switch to source + vim.schedule(function() + local cmd = string.format('chezmoi source-path %s 2>/dev/null', vim.fn.shellescape(filepath)) + local handle = io.popen(cmd) + if not handle then + return + end + + local source_path = handle:read '*l' + handle:close() + + if source_path and source_path ~= '' then + require('chezmoi.commands.__edit').execute { + targets = { filepath }, + args = { '--watch' }, + } + end + end) end, }) - - require('chezmoi').setup { - { - edit = { - watch = true, - force = false, - }, - events = { - on_open = { - notification = { - enable = true, - msg = 'Opened a chezmoi-managed file', - opts = {}, - }, - }, - on_watch = { - notification = { - enable = true, - msg = 'This file will be automatically applied', - opts = {}, - }, - }, - on_apply = { - notification = { - enable = true, - msg = 'Successfully applied', - opts = {}, - }, - }, - }, - telescope = { - select = { '' }, - }, - }, - } end, }, { 'alker0/chezmoi.vim', lazy = false, init = function() - -- This option is required. vim.g['chezmoi#use_tmp_buffer'] = true - -- add other options here if needed. end, config = function() - -- try env vars in this order for max compatibility local temp = os.getenv 'TMPDIR' or os.getenv 'TMP' or os.getenv 'TEMP' or '/tmp' - -- Match any files in tmp that start with chezmoi- local pattern = temp:gsub('\\', '/') .. '/chezmoi%-[^/]+/.+' + vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, { + group = vim.api.nvim_create_augroup('chezmoi_tmpl', { clear = true }), callback = function(args) local fname = args.file:gsub('\\', '/') if fname:match(pattern) then - -- auto-detect the buffer format - local type = vim.filetype.match { buf = args.buf } or '' - vim.api.nvim_set_option_value('filetype', type .. '.chezmoitmpl', { buf = args.buf }) + local filetype = vim.filetype.match { buf = args.buf } or '' + vim.bo[args.buf].filetype = filetype .. '.chezmoitmpl' end end, }) diff --git a/lua/plugins/mini.lua b/lua/plugins/mini.lua index faf055e..29e8c7a 100644 --- a/lua/plugins/mini.lua +++ b/lua/plugins/mini.lua @@ -22,52 +22,38 @@ local function sync_git_status() return sync_mod.get_nvim_config_git_status() or '' end --- Cache variable outside the function to persist between calls -local cached_managed_files = nil +-- Chezmoi statusbar indicator +local chezmoi_cache = {} +local chezmoi_dir = vim.env.HOME .. '/.local/share/chezmoi' local function chezmoi_file() - -- Check if chezmoi is installed if vim.fn.executable 'chezmoi' == 0 then return '' end - local current_file = vim.fn.expand '%:p' -- Full path of the current file - local home_dir = vim.env.HOME -- Get the home directory - local chezmoi_dir = home_dir .. '/.local/share/chezmoi' - - -- Skip if editing the source file in the chezmoi directory - if current_file:sub(1, #chezmoi_dir) == chezmoi_dir then - return '' - end - - local ok, chezmoi = pcall(require, 'chezmoi.commands') - if not ok then - return '' + local bufnr = vim.api.nvim_get_current_buf() + if chezmoi_cache[bufnr] ~= nil then + return chezmoi_cache[bufnr] end - -- Use cached managed files if available, otherwise fetch and cache - if not cached_managed_files then - cached_managed_files = chezmoi.list {} - end + local filepath = vim.api.nvim_buf_get_name(bufnr) + local result = '' - for _, file in ipairs(cached_managed_files) do - local full_managed_path = home_dir .. '/' .. file - if full_managed_path == current_file then - local edit_watch = function() - vim.notify 'You opened a file that is managed by chezmoi' - chezmoi.edit { - targets = { current_file }, - args = { '--watch', '--apply' }, - } - end - vim.schedule(edit_watch) - return 'Chezmoi File' - end + if filepath ~= '' and vim.bo.buftype == '' and vim.startswith(filepath, chezmoi_dir .. '/') then + result = 'CHEZMOI' end - return '' + chezmoi_cache[bufnr] = result + return result end +vim.api.nvim_create_autocmd('BufDelete', { + group = vim.api.nvim_create_augroup('chezmoi_statusline', { clear = true }), + callback = function(ev) + chezmoi_cache[ev.buf] = nil + end, +}) + --- Returns filetype icon, name and size local function my_fileinfo() local filetype = vim.bo.filetype @@ -228,7 +214,8 @@ return { -- Collection of various small independent plugins/modules '%<', -- Mark general truncate point { hl = 'MiniStatuslineFilename', strings = { filename } }, '%=', -- End left alignment - { hl = 'MiniStatuslineInactive', strings = { chezmoi_status, git_status } }, + { hl = 'MiniStatuslineInactive', strings = { git_status } }, + { hl = mode_hl, strings = { chezmoi_status } }, { hl = 'MiniStatuslineFileinfo', strings = { lazy_updates_status(), my_fileinfo() } }, { hl = mode_hl, strings = { search, location } }, }