commit 2e614caa3f9e4ee27c22b621dc73dc44311257ba
parent 9b255223f9fe1187d6cec88550afef6ec9af3570
Author: Peter <peter@minskio.co.uk>
Date: Wed, 30 Sep 2026 22:15:59 +0100
New neovim script
Amends lastmod: in blog posts on save (if you want it to)
Diffstat:
2 files changed, 66 insertions(+), 1 deletion(-)
diff --git a/home/neovim.nix b/home/neovim.nix
@@ -2,6 +2,7 @@
let
sortblock = pkgs.callPackage ../scripts/neovim-sortblock.nix { };
+ lastmod = pkgs.callPackage ../scripts/neovim-lastmod.nix { };
in
{
programs.neovim = {
@@ -9,7 +10,7 @@ in
withPython3 = false;
withRuby = false;
defaultEditor = true;
- plugins = with pkgs.vimPlugins; [ vim-nix sortblock ];
+ plugins = with pkgs.vimPlugins; [ vim-nix sortblock lastmod ];
extraConfig = ''
" Options
set smartcase
@@ -46,6 +47,7 @@ in
'';
initLua = ''
require("sortblock").setup()
+ require("lastmod")
vim.api.nvim_create_user_command("Uppercase", function()
vim.cmd("'<,'>s/.*/\\U&/")
diff --git a/scripts/neovim-lastmod.nix b/scripts/neovim-lastmod.nix
@@ -0,0 +1,63 @@
+{ lib, vimUtils, writeText, runCommand }:
+
+let
+ lastmodLua = writeText "lastmod.lua" ''
+ local SEARCH_LINES = 10
+ local TS_FORMAT = "%Y-%m-%dT%H:%M:%S"
+ local TS_PATTERN = "%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d:?%d*"
+
+ local group = vim.api.nvim_create_augroup("LastmodPrompt", { clear = true })
+
+ vim.api.nvim_create_autocmd("BufWritePre", {
+ group = group,
+ pattern = { "*.md", "*.markdown" },
+ desc = "Prompt to update lastmod frontmatter field on save",
+ callback = function(args)
+ local buf = args.buf
+ if not vim.bo[buf].modifiable then
+ return
+ end
+
+ local lines = vim.api.nvim_buf_get_lines(buf, 0, SEARCH_LINES, false)
+
+ for i, line in ipairs(lines) do
+ local prefix, old = line:match("^(%s*lastmod:%s*[\"']?)(" .. TS_PATTERN .. ")")
+ if old then
+ local now = os.date(TS_FORMAT)
+ local choice = vim.fn.confirm(
+ ("Update lastmod?\n %s -> %s"):format(old, now),
+ "&Yes\n&No",
+ 1 -- defaults to Yes
+ )
+ if choice == 1 then
+ -- Replace only the timestamp, preserving indentation/quotes/trailing text
+ local new_line = line:gsub(TS_PATTERN, now, 1)
+ vim.api.nvim_buf_set_lines(buf, i - 1, i, false, { new_line })
+ end
+ return -- only handle the first lastmod field found
+ end
+ end
+ end,
+ })
+
+ '';
+in
+ vimUtils.buildVimPlugin {
+ pname = "lastmod";
+ version = "1.0.0";
+
+ # buildVimPlugin expects a source tree laid out the way Neovim's
+ # runtimepath wants it, so wrap the embedded lua string in lua/.
+ src = runCommand "lastmod-src" { } ''
+ mkdir -p $out/lua
+ cp ${lastmodLua} $out/lua/lastmod.lua
+ '';
+
+ nvimRequireCheck = "lastmod";
+
+ meta = {
+ description = "Prompts whether Neovim should update the lastmod field in markdown files on save";
+ license = lib.licenses.mit;
+ platforms = lib.platforms.all;
+ };
+}