From 4be96e5331f806c6c6c98ccfaccb060a60051d16 Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Fri, 31 Jul 2026 20:51:42 +0100 Subject: [PATCH] Enable opt in project configs --- init.lua | 4 ++++ plugin/commands.lua | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/init.lua b/init.lua index 6db04fa..49f6ca7 100644 --- a/init.lua +++ b/init.lua @@ -12,6 +12,10 @@ end -- load config from lua/local.lua if present pcall(function() require('local') end) +-- source project-local .nvim.lua from cwd upwards, if trusted (see :h 'exrc') +-- must be set here: plugin/ scripts load after exrc is processed +vim.o.exrc = true + local lazypath = vim.fn.stdpath('data') .. '/lazy/lazy.nvim' if not vim.uv.fs_stat(lazypath) then vim.fn.system({ 'git', 'clone', '--filter=blob:none', diff --git a/plugin/commands.lua b/plugin/commands.lua index 83a0c8f..c089e23 100644 --- a/plugin/commands.lua +++ b/plugin/commands.lua @@ -206,6 +206,34 @@ vim.api.nvim_create_user_command('DiagnosticToggle', function(_) vim.diagnostic.enable(not vim.diagnostic.is_enabled()) end, {}) +-- :Exrc create a project-local .nvim.lua in the current directory which adds a +-- local runtime directory to 'runtimepath', defaulting to .config/nvim. Bang to +-- overwrite an existing file. The result is trusted so it is sourced on the next +-- start, editing it revokes that and requires :trust again. See :h 'exrc'. +vim.api.nvim_create_user_command('Exrc', function(opts) + local path = vim.fs.joinpath(vim.fn.getcwd(), '.nvim.lua') + if vim.uv.fs_stat(path) and not opts.bang then + vim.notify(path .. ' already exists, use :Exrc! to overwrite', + vim.log.levels.ERROR) + return + end + local dir = opts.args ~= '' and opts.args or '.config/nvim' + vim.fn.writefile({ + "local root = vim.fs.dirname(debug.getinfo(1, 'S').source:gsub('^@', ''))", + "local dir = vim.fs.joinpath(root, '" .. dir .. "')", + '', + 'vim.opt.runtimepath:prepend(dir)', + "vim.opt.runtimepath:append(vim.fs.joinpath(dir, 'after'))", + '', + "-- lazy.nvim unsets 'loadplugins' so source this dir's plugin/ files here", + "for _, file in ipairs(vim.fn.glob(dir .. '/plugin/**/*.lua', false, true)) do", + ' dofile(file)', + 'end', + }, path) + vim.secure.trust({ action = 'allow', path = path }) + vim.cmd.edit(path) +end, { bang = true, nargs = '?', complete = 'dir' }) + -- :ResizeFixed acts like :resize but also fixes the window size so it stops -- automatically changing when other windows reflow. Respects the :vertical -- modifier, resizing and fixing the width instead of the height.