refact(rename): use prompt helper

This commit is contained in:
Thomas Lambert
2022-09-29 11:38:14 +02:00
parent 76919abaaf
commit fb7f476dcd

View File

@@ -1662,64 +1662,73 @@ end
local function RenameNote()
local oldfile = Pinfo:new({ filepath = vim.fn.expand("%:p"), M.Cfg })
local newname = vim.fn.input("New name: ")
newname = newname:gsub("(%" .. M.Cfg.extension .. ")$", "")
local newpath = newname:match("(.*/)") or ""
newpath = M.Cfg.home .. "/" .. newpath
tkutils.prompt_title(function(newname)
if not newname then
newname = ""
end
newname = strip_extension(newname, M.Cfg.extension)
-- If no subdir specified, place the new note in the same place as old note
if
M.Cfg.subdirs_in_links == true
and newpath == M.Cfg.home .. "/"
and oldfile.sub_dir ~= ""
then
newname = oldfile.sub_dir .. "/" .. newname
end
local newpath = newname:match("(.*/)") or ""
newpath = M.Cfg.home .. "/" .. newpath
local fname = M.Cfg.home .. "/" .. newname .. M.Cfg.extension
local fexists = file_exists(fname)
if fexists then
print_error("File alreay exists. Renaming abandonned")
return
end
-- If no subdir specified, place the new note in the same place as old note
if
M.Cfg.subdirs_in_links == true
and newpath == M.Cfg.home .. "/"
and oldfile.sub_dir ~= ""
then
newname = oldfile.sub_dir .. "/" .. newname
end
-- Savas newfile, delete buffer of old one and remove old file
if newname ~= "" and newname ~= oldfile.title then
if not (check_dir_and_ask(newpath, "Renamed file")) then
local fname = M.Cfg.home .. "/" .. newname .. M.Cfg.extension
local fexists = file_exists(fname)
if fexists then
print_error("File alreay exists. Renaming abandonned")
return
end
local oldTitle = oldfile.title:gsub(" ", "\\ ")
vim.cmd("saveas " .. M.Cfg.home .. "/" .. newname .. M.Cfg.extension)
vim.cmd("bdelete " .. oldTitle .. M.Cfg.extension)
os.execute("rm " .. M.Cfg.home .. "/" .. oldTitle .. M.Cfg.extension)
end
if M.Cfg.rename_update_links == true then
-- 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
local oldlink = "[[" .. oldfile.title
local newlink = "[[" .. newname
-- Save open telekasten buffers before looking for links to replace
if
#(vim.fn.getbufinfo({ bufmodified = 1 })) > 1
and M.Cfg.auto_set_filetype == true
then
local answer = vim.fn.input(
"Telekasten.nvim:"
.. "Save all telekasten buffers before updating links? [Y/n]"
)
answer = vim.fn.trim(answer)
if answer ~= "n" and answer ~= "N" then
save_all_tk_buffers()
-- Savas newfile, delete buffer of old one and remove old file
if newname ~= "" and newname ~= oldfile.title then
if not (check_dir_and_ask(newpath, "Renamed file")) then
return
end
local oldTitle = oldfile.title:gsub(" ", "\\ ")
vim.cmd(
"saveas " .. M.Cfg.home .. "/" .. newname .. M.Cfg.extension
)
vim.cmd("bdelete " .. oldTitle .. M.Cfg.extension)
os.execute(
"rm " .. M.Cfg.home .. "/" .. oldTitle .. M.Cfg.extension
)
end
recursive_substitution(M.Cfg.home, oldlink, newlink)
recursive_substitution(M.Cfg.dailies, oldlink, newlink)
recursive_substitution(M.Cfg.weeklies, oldlink, newlink)
end
if M.Cfg.rename_update_links == true then
-- 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
local oldlink = "[[" .. oldfile.title
local newlink = "[[" .. newname
-- Save open telekasten buffers before looking for links to replace
if
#(vim.fn.getbufinfo({ bufmodified = 1 })) > 1
and M.Cfg.auto_set_filetype == true
then
local answer = vim.fn.input(
"Telekasten.nvim:"
.. "Save all telekasten buffers before updating links? [Y/n]"
)
answer = vim.fn.trim(answer)
if answer ~= "n" and answer ~= "N" then
save_all_tk_buffers()
end
end
recursive_substitution(M.Cfg.home, oldlink, newlink)
recursive_substitution(M.Cfg.dailies, oldlink, newlink)
recursive_substitution(M.Cfg.weeklies, oldlink, newlink)
end
end)
end
--