improve checks, save all buffers

This commit is contained in:
lambtho12
2022-01-18 22:38:34 +01:00
parent 15d4355ac1
commit 2010f29179
2 changed files with 29 additions and 17 deletions

View File

@@ -466,7 +466,7 @@ the list for a more detailed description:
- `insert_img_link` : Browse images / media files and insert a link to the selected one - `insert_img_link` : Browse images / media files and insert a link to the selected one
- `preview_img` : preview image under the cursor - `preview_img` : preview image under the cursor
- `browse_media` : Browse images / media files - `browse_media` : Browse images / media files
- `rename_note` : Rename current note and possibly update the links pointing to it - `rename_note` : Rename current note and update the links pointing to it
The Telekasten command supports sub-command completion, in my case by pressing <kbd>TAB</kbd>. The Telekasten command supports sub-command completion, in my case by pressing <kbd>TAB</kbd>.

View File

@@ -202,21 +202,30 @@ local function escape(s)
end end
local function recursive_substitution(dir, old, new) local function recursive_substitution(dir, old, new)
if vim.fn.has("mac") == 1 or vim.fn.has("unix") == 1 then if not global_dir_check() then
os.execute( return
"find "
.. dir
.. " -type f -name '*"
.. M.Cfg.extension
.. "' -exec sed -i 's|"
.. old
.. "|"
.. new
.. "|g' {} +"
)
else
print("Cannot open update links on your operating system")
end end
if vim.fn.executable("find") == 0 then
vim.api.nvim_err_write("Find not installed!\n")
return
end
if vim.fn.executable("sed") == 0 then
vim.api.nvim_err_write("Sed not installed!\n")
return
end
os.execute(
"find "
.. dir
.. " -type f -name '*"
.. M.Cfg.extension
.. "' -exec sed -i 's|"
.. old
.. "|"
.. new
.. "|g' {} +"
)
end end
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
@@ -1431,11 +1440,12 @@ local function RenameNote()
newname = newname:gsub("[" .. M.Cfg.extension .. "]+$", "") newname = newname:gsub("[" .. M.Cfg.extension .. "]+$", "")
local newpath = newname:match("(.*/)") local newpath = newname:match("(.*/)")
-- If no subdir specified, place the new note in the same place as old note
if M.Cfg.subdirs_in_links == true and newpath == nil and subdir ~= "" then if M.Cfg.subdirs_in_links == true and newpath == nil and subdir ~= "" then
newname = subdir .. "/" .. newname newname = subdir .. "/" .. newname
end end
-- could probably be improved substantially -- Savas newfile, delete buffer of old one and remove old file
if newname ~= "" and newname ~= oldname then if newname ~= "" and newname ~= oldname then
vim.cmd("saveas " .. newname .. M.Cfg.extension) vim.cmd("saveas " .. newname .. M.Cfg.extension)
vim.cmd("bdelete " .. oldname .. M.Cfg.extension) vim.cmd("bdelete " .. oldname .. M.Cfg.extension)
@@ -1446,10 +1456,12 @@ local function RenameNote()
if M.Cfg.rename_update_links == true then 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 -- 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 use regex instead to ensure it is a proper link
-- Should also account for other types of links (?)
local oldlink = "\\[\\[" .. oldname local oldlink = "\\[\\[" .. oldname
local newlink = "\\[\\[" .. newname local newlink = "\\[\\[" .. newname
-- Save all open buffers before looking for links to replace
vim.cmd("wa")
recursive_substitution(M.Cfg.home, oldlink, newlink) recursive_substitution(M.Cfg.home, oldlink, newlink)
recursive_substitution(M.Cfg.dailies, oldlink, newlink) recursive_substitution(M.Cfg.dailies, oldlink, newlink)
recursive_substitution(M.Cfg.weeklies, oldlink, newlink) recursive_substitution(M.Cfg.weeklies, oldlink, newlink)