mirror of
https://github.com/Ascyii/telekasten.nvim.git
synced 2026-01-01 06:14:23 -05:00
refact(rename): use prompt helper
This commit is contained in:
@@ -1662,64 +1662,73 @@ end
|
|||||||
local function RenameNote()
|
local function RenameNote()
|
||||||
local oldfile = Pinfo:new({ filepath = vim.fn.expand("%:p"), M.Cfg })
|
local oldfile = Pinfo:new({ filepath = vim.fn.expand("%:p"), M.Cfg })
|
||||||
|
|
||||||
local newname = vim.fn.input("New name: ")
|
tkutils.prompt_title(function(newname)
|
||||||
newname = newname:gsub("(%" .. M.Cfg.extension .. ")$", "")
|
if not newname then
|
||||||
local newpath = newname:match("(.*/)") or ""
|
newname = ""
|
||||||
newpath = M.Cfg.home .. "/" .. newpath
|
end
|
||||||
|
newname = strip_extension(newname, M.Cfg.extension)
|
||||||
|
|
||||||
-- If no subdir specified, place the new note in the same place as old note
|
local newpath = newname:match("(.*/)") or ""
|
||||||
if
|
newpath = M.Cfg.home .. "/" .. newpath
|
||||||
M.Cfg.subdirs_in_links == true
|
|
||||||
and newpath == M.Cfg.home .. "/"
|
|
||||||
and oldfile.sub_dir ~= ""
|
|
||||||
then
|
|
||||||
newname = oldfile.sub_dir .. "/" .. newname
|
|
||||||
end
|
|
||||||
|
|
||||||
local fname = M.Cfg.home .. "/" .. newname .. M.Cfg.extension
|
-- If no subdir specified, place the new note in the same place as old note
|
||||||
local fexists = file_exists(fname)
|
if
|
||||||
if fexists then
|
M.Cfg.subdirs_in_links == true
|
||||||
print_error("File alreay exists. Renaming abandonned")
|
and newpath == M.Cfg.home .. "/"
|
||||||
return
|
and oldfile.sub_dir ~= ""
|
||||||
end
|
then
|
||||||
|
newname = oldfile.sub_dir .. "/" .. newname
|
||||||
|
end
|
||||||
|
|
||||||
-- Savas newfile, delete buffer of old one and remove old file
|
local fname = M.Cfg.home .. "/" .. newname .. M.Cfg.extension
|
||||||
if newname ~= "" and newname ~= oldfile.title then
|
local fexists = file_exists(fname)
|
||||||
if not (check_dir_and_ask(newpath, "Renamed file")) then
|
if fexists then
|
||||||
|
print_error("File alreay exists. Renaming abandonned")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local oldTitle = oldfile.title:gsub(" ", "\\ ")
|
-- Savas newfile, delete buffer of old one and remove old file
|
||||||
vim.cmd("saveas " .. M.Cfg.home .. "/" .. newname .. M.Cfg.extension)
|
if newname ~= "" and newname ~= oldfile.title then
|
||||||
vim.cmd("bdelete " .. oldTitle .. M.Cfg.extension)
|
if not (check_dir_and_ask(newpath, "Renamed file")) then
|
||||||
os.execute("rm " .. M.Cfg.home .. "/" .. oldTitle .. M.Cfg.extension)
|
return
|
||||||
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
|
||||||
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
recursive_substitution(M.Cfg.home, oldlink, newlink)
|
if M.Cfg.rename_update_links == true then
|
||||||
recursive_substitution(M.Cfg.dailies, oldlink, newlink)
|
-- Only look for the first part of the link, so we do not touch to #heading or #^paragraph
|
||||||
recursive_substitution(M.Cfg.weeklies, oldlink, newlink)
|
-- Should use regex instead to ensure it is a proper link
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
--
|
--
|
||||||
|
|||||||
Reference in New Issue
Block a user