From 6d70bcef87ef718c2352734d19e254863a02ecc3 Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Tue, 18 Jan 2022 11:05:46 +0100 Subject: [PATCH] fix: old file deletion and link update - Old file properly deleted after renaming no matter where we are in the zettelkasten tree. - Workaround links with `#heading` and `#^paragraph` by only looking for the first elements of the link. Ideally it should be a proper regex --- lua/telekasten.lua | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index bd1da49..8c1127c 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -1424,18 +1424,27 @@ local function RenameNote() local newname = vim.fn.input("New name: ") newname = newname:gsub("[" .. M.Cfg.extension .. "]+$", "") + -- oldname should include subdir if subdirs_in_links = true + -- newname should automatically add subdir if subdirs_in_links = true and user did not add it themselves + + -- could probably be improved substantially if newname ~= "" and newname ~= oldname then vim.cmd("saveas " .. newname .. M.Cfg.extension) vim.cmd("bdelete " .. oldname .. M.Cfg.extension) - vim.cmd("!rm " .. oldname .. M.Cfg.extension) + os.execute("rm " .. M.Cfg.home .. "/" .. oldname .. M.Cfg.extension) vim.cmd("redraw!") end if M.Cfg.rename_update_links == true then - -- Sed magic to rename all links (in a separate function) - local oldlink = "\\[\\[" .. oldname .. "\\]\\]" - local newlink = "\\[\\[" .. newname .. "\\]\\]" + -- Only look for the first part of the link, so we do not touch to #heading or #^paragraph + -- Should use regex instead to ensure it is a proper link + -- Should also account for other types of links (?) + local oldlink = "\\[\\[" .. oldname + local newlink = "\\[\\[" .. newname + recursive_substitution(M.Cfg.home, oldlink, newlink) + recursive_substitution(M.Cfg.dailies, oldlink, newlink) + recursive_substitution(M.Cfg.weeklies, oldlink, newlink) end end