From a11dfbc5865957b3e3047ffd0e750a28b1dc5ce0 Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Tue, 18 Jan 2022 10:25:54 +0100 Subject: [PATCH 01/18] add: basic note renaming --- README.md | 6 ++++++ doc/telekasten.txt | 6 ++++++ lua/telekasten.lua | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/README.md b/README.md index 3ed95b4..e37e260 100644 --- a/README.md +++ b/README.md @@ -304,6 +304,8 @@ require('telekasten').setup({ -- except for notes/with/subdirs/in/title. new_note_location = "smart", + -- should all links be updated when a file is renamed + rename_update_links = true, }) END ``` @@ -338,6 +340,7 @@ END | | - `#tag` (default) | | | | - `:tag:` | | | | - `yaml-bare` | | +| `rename_update_links` | update links when a file is renamed | true | | | see [2.1 Tag notation](#24-tag-notation)| | | `command_palette_theme` | theme (layout) of the command palette| ivy | | | - `ivy` (default): bottom panel overlay | | @@ -463,6 +466,7 @@ the list for a more detailed description: - `insert_img_link` : Browse images / media files and insert a link to the selected one - `preview_img` : preview image under the cursor - `browse_media` : Browse images / media files +- `rename_note` : Rename current note and possibly update the links pointing to it The Telekasten command supports sub-command completion, in my case by pressing TAB. @@ -558,6 +562,7 @@ The plugin defines the following functions: - `setup(opts)`: used for configuring paths, file extension, etc. - `panel()` : brings up the command palette - `show_tags()` : brings up the tag list. From there you can select a tag to search for tagged notes - or yank or insert the tag +- `rename_note()` : rename the current note and update the links pointing to it To use one of the functions above, just run them with the `:lua ...` command. @@ -809,6 +814,7 @@ nnoremap zp :lua require('telekasten').preview_img() nnoremap zm :lua require('telekasten').browse_media() nnoremap za :lua require('telekasten').show_tags() nnoremap # :lua require('telekasten').show_tags() +nnoremap zr :lua require('telekasten').rename_note() " on hesitation, bring up the panel nnoremap z :lua require('telekasten').panel() diff --git a/doc/telekasten.txt b/doc/telekasten.txt index 65596d2..643cedd 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -312,6 +312,12 @@ telekasten.setup({opts}) Default: `smart` + *telekasten.settings.rename_update_links* + rename_update_links:~ + If `true`, telekasten will automatically update the links after a file + has been renamed. + + Default: `true` *telekasten.calendar_opts* ----------------------------------- Valid keys for {opts.calendar_opts} diff --git a/lua/telekasten.lua b/lua/telekasten.lua index ae2563d..bd1da49 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -123,6 +123,9 @@ M.Cfg = { -- present or else in home -- except for notes/with/subdirs/in/title. new_note_location = "smart", + + -- should all links be updated when a file is renamed + rename_update_links = true, } local function file_exists(fname) @@ -198,6 +201,20 @@ local function escape(s) return string.gsub(s, "[%%%]%^%-$().[*+?]", "%%%1") end +local function recursive_substitution(dir, old, new) + os.execute( + "find " + .. dir + .. " -type f -name '*" + .. M.Cfg.extension + .. "' -exec sed -i 's|" + .. old + .. "|" + .. new + .. "|g' {} +" + ) +end + -- ---------------------------------------------------------------------------- -- image stuff local function imgFromClipboard() @@ -1396,6 +1413,32 @@ local function YankLink() print("yanked " .. title) end +-- +-- RenameNote: +-- ----------- +-- +-- Prompt for new note title, rename the note and update all links. +-- +local function RenameNote() + local oldname = Pinfo:new({ filepath = vim.fn.expand("%:p"), M.Cfg }).title + local newname = vim.fn.input("New name: ") + newname = newname:gsub("[" .. M.Cfg.extension .. "]+$", "") + + 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) + 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 .. "\\]\\]" + recursive_substitution(M.Cfg.home, oldlink, newlink) + end +end + -- -- GotoDate: -- ---------- @@ -2564,6 +2607,7 @@ M.new_note = CreateNote M.goto_thisweek = GotoThisWeek M.find_weekly_notes = FindWeeklyNotes M.yank_notelink = YankLink +M.rename_note = RenameNote M.new_templated_note = CreateNoteSelectTemplate M.show_calendar = ShowCalendar M.CalendarSignDay = CalendarSignDay @@ -2592,6 +2636,7 @@ local TelekastenCmd = { { "goto thisweek", "goto_thisweek", M.goto_thisweek }, { "find weekly notes", "find_weekly_notes", M.find_weekly_notes }, { "yank link to note", "yank_notelink", M.yank_notelink }, + { "rename note", "rename_note", M.rename_note }, { "new templated note", "new_templated_note", From 6d70bcef87ef718c2352734d19e254863a02ecc3 Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Tue, 18 Jan 2022 11:05:46 +0100 Subject: [PATCH 02/18] 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 From 5f19d27a9a6124b33f99a66778f4986fc587a065 Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Tue, 18 Jan 2022 11:30:30 +0100 Subject: [PATCH 03/18] add: renaming in subdirs --- lua/telekasten.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 8c1127c..76d5bd2 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -1421,11 +1421,15 @@ end -- local function RenameNote() local oldname = Pinfo:new({ filepath = vim.fn.expand("%:p"), M.Cfg }).title + local subdir = Pinfo:new({ filepath = vim.fn.expand("%:p"), M.Cfg }).sub_dir + local newname = vim.fn.input("New name: ") newname = newname:gsub("[" .. M.Cfg.extension .. "]+$", "") + local newpath = newname:match("(.*/)") - -- 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 + if M.Cfg.subdirs_in_links == true and newpath == nil then + newname = subdir .. "/" .. newname + end -- could probably be improved substantially if newname ~= "" and newname ~= oldname then From 15d4355ac1ceaeb97ba51003dc2480b41c3de4df Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Tue, 18 Jan 2022 11:52:35 +0100 Subject: [PATCH 04/18] fix: properly treat files in ZK home --- lua/telekasten.lua | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 76d5bd2..c7d1a3b 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -202,17 +202,21 @@ local function escape(s) end local function recursive_substitution(dir, old, new) - os.execute( - "find " - .. dir - .. " -type f -name '*" - .. M.Cfg.extension - .. "' -exec sed -i 's|" - .. old - .. "|" - .. new - .. "|g' {} +" - ) + if vim.fn.has("mac") == 1 or vim.fn.has("unix") == 1 then + os.execute( + "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 -- ---------------------------------------------------------------------------- @@ -1427,7 +1431,7 @@ local function RenameNote() newname = newname:gsub("[" .. M.Cfg.extension .. "]+$", "") local newpath = newname:match("(.*/)") - if M.Cfg.subdirs_in_links == true and newpath == nil then + if M.Cfg.subdirs_in_links == true and newpath == nil and subdir ~= "" then newname = subdir .. "/" .. newname end From 2010f29179338a8b43d69f00e5cf8ad5b1b79513 Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Tue, 18 Jan 2022 22:38:34 +0100 Subject: [PATCH 05/18] improve checks, save all buffers --- README.md | 2 +- lua/telekasten.lua | 44 ++++++++++++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index e37e260..5f060be 100644 --- a/README.md +++ b/README.md @@ -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 - `preview_img` : preview image under the cursor - `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 TAB. diff --git a/lua/telekasten.lua b/lua/telekasten.lua index c7d1a3b..e5db71c 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -202,21 +202,30 @@ local function escape(s) end local function recursive_substitution(dir, old, new) - if vim.fn.has("mac") == 1 or vim.fn.has("unix") == 1 then - os.execute( - "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") + if not global_dir_check() then + return 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 -- ---------------------------------------------------------------------------- @@ -1431,11 +1440,12 @@ local function RenameNote() newname = newname:gsub("[" .. M.Cfg.extension .. "]+$", "") 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 newname = subdir .. "/" .. newname end - -- could probably be improved substantially + -- Savas newfile, delete buffer of old one and remove old file if newname ~= "" and newname ~= oldname then vim.cmd("saveas " .. newname .. 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 -- 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 + -- Save all open buffers before looking for links to replace + vim.cmd("wa") + recursive_substitution(M.Cfg.home, oldlink, newlink) recursive_substitution(M.Cfg.dailies, oldlink, newlink) recursive_substitution(M.Cfg.weeklies, oldlink, newlink) From 0e00cafafabb94cf8be2268d8a80fef61032309e Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Thu, 20 Jan 2022 10:34:22 +0100 Subject: [PATCH 06/18] add prompt before saving buffers --- lua/telekasten.lua | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index a55d17d..6b601bf 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -1438,34 +1438,47 @@ end -- Prompt for new note title, rename the note and update all links. -- local function RenameNote() - local oldname = Pinfo:new({ filepath = vim.fn.expand("%:p"), M.Cfg }).title - local subdir = Pinfo:new({ filepath = vim.fn.expand("%:p"), M.Cfg }).sub_dir + 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("(.*/)") -- 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 - newname = subdir .. "/" .. newname + if + M.Cfg.subdirs_in_links == true + and newpath == nil + 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 ~= oldname then + if newname ~= "" and newname ~= oldfile.title then vim.cmd("saveas " .. newname .. M.Cfg.extension) - vim.cmd("bdelete " .. oldname .. M.Cfg.extension) - os.execute("rm " .. M.Cfg.home .. "/" .. oldname .. M.Cfg.extension) + vim.cmd("bdelete " .. oldfile.title .. M.Cfg.extension) + os.execute( + "rm " .. M.Cfg.home .. "/" .. oldfile.title .. M.Cfg.extension + ) vim.cmd("redraw!") 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 = "\\[\\[" .. oldname + local oldlink = "\\[\\[" .. oldfile.title local newlink = "\\[\\[" .. newname -- Save all open buffers before looking for links to replace - vim.cmd("wa") + if #(vim.fn.getbufinfo({ buflisted = 1 })) > 1 then + local answer = vim.fn.input( + "Telekasten.nvim: Save all current buffers before updating links? [Y/n]" + ) + answer = vim.fn.trim(answer) + if answer ~= "n" and answer ~= "N" then + vim.cmd("wa") + end + end recursive_substitution(M.Cfg.home, oldlink, newlink) recursive_substitution(M.Cfg.dailies, oldlink, newlink) From 743479b5b9670c42cd3227fa4357152ba83924b8 Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Thu, 20 Jan 2022 11:27:52 +0100 Subject: [PATCH 07/18] Only save telekasten buffers --- lua/telekasten.lua | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 6b601bf..9d9489d 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -1470,13 +1470,18 @@ local function RenameNote() local newlink = "\\[\\[" .. newname -- Save all open buffers before looking for links to replace - if #(vim.fn.getbufinfo({ buflisted = 1 })) > 1 then + if #(vim.fn.getbufinfo({ bufmodified = 1 })) > 1 then local answer = vim.fn.input( - "Telekasten.nvim: Save all current buffers before updating links? [Y/n]" + "Telekasten.nvim:" + .. "Save all telekasten buffers before updating links? [Y/n]" ) answer = vim.fn.trim(answer) if answer ~= "n" and answer ~= "N" then - vim.cmd("wa") + for i = 1, vim.fn.bufnr("$") do + if vim.fn.getbufvar(i, "&filetype") == "telekasten" then + vim.cmd(i .. "bufdo w") + end + end end end From d5c107c195059ea4a2d5ef2881253069b99656a3 Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Thu, 20 Jan 2022 11:51:29 +0100 Subject: [PATCH 08/18] Refactor tk buffer saving --- lua/telekasten.lua | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 9d9489d..efd9518 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -233,6 +233,17 @@ local function recursive_substitution(dir, old, new) ) end +local function save_all_tk_buffers() + for i = 1, vim.fn.bufnr("$") do + if + vim.fn.getbufvar(i, "&filetype") == "telekasten" + and vim.fn.getbufvar(i, "&mod") == 1 + then + vim.cmd(i .. "bufdo w") + end + end +end + -- ---------------------------------------------------------------------------- -- image stuff local function imgFromClipboard() @@ -1460,7 +1471,7 @@ local function RenameNote() os.execute( "rm " .. M.Cfg.home .. "/" .. oldfile.title .. M.Cfg.extension ) - vim.cmd("redraw!") + -- vim.cmd("redraw!") end if M.Cfg.rename_update_links == true then @@ -1469,19 +1480,18 @@ local function RenameNote() local oldlink = "\\[\\[" .. oldfile.title local newlink = "\\[\\[" .. newname - -- Save all open buffers before looking for links to replace - if #(vim.fn.getbufinfo({ bufmodified = 1 })) > 1 then + -- 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 - for i = 1, vim.fn.bufnr("$") do - if vim.fn.getbufvar(i, "&filetype") == "telekasten" then - vim.cmd(i .. "bufdo w") - end - end + save_all_tk_buffers() end end From 44ad8d87bec7407124da03623ff93cac514cdaf8 Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Thu, 20 Jan 2022 13:32:11 +0100 Subject: [PATCH 09/18] Sanitize sed input --- lua/telekasten.lua | 50 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index efd9518..68268d0 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -206,6 +206,26 @@ local function escape(s) return string.gsub(s, "[%%%]%^%-$().[*+?]", "%%%1") end +-- sanitize strings +local escape_chars = function(string) + return string.gsub(string, "[%(|%)|\\|%[|%]|%-|%{%}|%?|%+|%*|%^|%$|%/]", { + ["\\"] = "\\\\", + ["-"] = "\\-", + ["("] = "\\(", + [")"] = "\\)", + ["["] = "\\[", + ["]"] = "\\]", + ["{"] = "\\{", + ["}"] = "\\}", + ["?"] = "\\?", + ["+"] = "\\+", + ["*"] = "\\*", + ["^"] = "\\^", + ["$"] = "\\$", + ["/"] = "\\/", + }) +end + local function recursive_substitution(dir, old, new) if not global_dir_check() then return @@ -220,17 +240,21 @@ local function recursive_substitution(dir, old, new) return end - os.execute( - "find " - .. dir - .. " -type f -name '*" - .. M.Cfg.extension - .. "' -exec sed -i 's|" - .. old - .. "|" - .. new - .. "|g' {} +" - ) + old = escape_chars(old) + new = escape_chars(new) + + local replace_cmd = "find " + .. dir + .. " -type f -name '*" + .. M.Cfg.extension + .. "' -exec sed -i 's/" + .. old + .. "/" + .. new + .. "/g' {} +" + + print(replace_cmd) + os.execute(replace_cmd) end local function save_all_tk_buffers() @@ -1477,8 +1501,8 @@ local function RenameNote() 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 + local oldlink = "[[" .. oldfile.title + local newlink = "[[" .. newname -- Save open telekasten buffers before looking for links to replace if From eba6dcd1d77d99dc5b20ed4eb82ed36c415c1d3c Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Thu, 20 Jan 2022 15:06:04 +0100 Subject: [PATCH 10/18] Add existing dir and file check before renaming --- lua/telekasten.lua | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 68268d0..564e815 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -253,7 +253,6 @@ local function recursive_substitution(dir, old, new) .. new .. "/g' {} +" - print(replace_cmd) os.execute(replace_cmd) end @@ -1477,19 +1476,30 @@ local function RenameNote() local newname = vim.fn.input("New name: ") newname = newname:gsub("[" .. M.Cfg.extension .. "]+$", "") - local newpath = newname:match("(.*/)") + local newpath = newname:match("(.*/)") or "" + newpath = M.Cfg.home .. "/" .. newpath -- 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 newpath == M.Cfg.home .. "/" and oldfile.sub_dir ~= "" then newname = oldfile.sub_dir .. "/" .. newname end + 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 + -- 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 vim.cmd("saveas " .. newname .. M.Cfg.extension) vim.cmd("bdelete " .. oldfile.title .. M.Cfg.extension) os.execute( From 840e2b3493d5e040885321343fbdbcde532d9e4a Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Thu, 20 Jan 2022 16:58:57 +0100 Subject: [PATCH 11/18] Fix sed, fix saveas --- lua/telekasten.lua | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 564e815..baefac8 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -222,7 +222,6 @@ local escape_chars = function(string) ["*"] = "\\*", ["^"] = "\\^", ["$"] = "\\$", - ["/"] = "\\/", }) end @@ -231,10 +230,6 @@ local function recursive_substitution(dir, old, new) return 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 @@ -243,16 +238,22 @@ local function recursive_substitution(dir, old, new) old = escape_chars(old) new = escape_chars(new) - local replace_cmd = "find " - .. dir - .. " -type f -name '*" - .. M.Cfg.extension - .. "' -exec sed -i 's/" - .. old - .. "/" - .. new - .. "/g' {} +" + local sedcommand = "sed -i" + if vim.fn.has("mac") == 1 then + sedcommand = "sed -i ''" + end + local replace_cmd = "rg -l -t markdown '" + .. old + .. "' " + .. dir + .. " | xargs " + .. sedcommand + .. " 's|" + .. old + .. "|" + .. new + .. "|g' >/dev/null 2>&1" os.execute(replace_cmd) end @@ -1500,12 +1501,12 @@ local function RenameNote() if not (check_dir_and_ask(newpath, "Renamed file")) then return end - vim.cmd("saveas " .. newname .. M.Cfg.extension) + + vim.cmd("saveas " .. M.Cfg.home .. "/" .. newname .. M.Cfg.extension) vim.cmd("bdelete " .. oldfile.title .. M.Cfg.extension) os.execute( "rm " .. M.Cfg.home .. "/" .. oldfile.title .. M.Cfg.extension ) - -- vim.cmd("redraw!") end if M.Cfg.rename_update_links == true then From ea03546662bc8f1c411da39f4d2ad7e247c2c71c Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Thu, 20 Jan 2022 20:57:12 +0100 Subject: [PATCH 12/18] Feat: Support for absolute or relative path config --- README.md | 5 +++-- doc/telekasten.txt | 8 ++++++-- lua/telekasten.lua | 38 ++++++++++++++++++++++++++++---------- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 370cd45..14b973f 100644 --- a/README.md +++ b/README.md @@ -225,12 +225,13 @@ require('telekasten').setup({ -- and thus the telekasten syntax will not be loaded either auto_set_filetype = true, + -- dir names for special notes (absolute path or subdir name) dailies = home .. '/' .. 'daily', weeklies = home .. '/' .. 'weekly', templates = home .. '/' .. 'templates', - -- image subdir for pasting - -- subdir name + -- image (sub)dir for pasting + -- dir name (absolute path or subdir name) -- or nil if pasted images shouldn't go into a special subdir image_subdir = "img", diff --git a/doc/telekasten.txt b/doc/telekasten.txt index 65596d2..1ed71f0 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -158,18 +158,21 @@ telekasten.setup({opts}) *telekasten.settings.daily* daily: ~ Path to your daily notes, to separate them from 'normal' notes. + Accepts absolute path or sub-directory name. Default: '~/zettelkasten/daily' *telekasten.settings.weekly* weekly: ~ Path to your weekly notes, to separate them from 'normal' notes. + Accepts absolute path or sub-directory name. Default: '~/zettelkasten/weekly' *telekasten.settings.templates* templates: ~ Path to your note templates. + Accepts absolute path or sub-directory name. Default: '~/zettelkasten/templates' @@ -181,8 +184,9 @@ telekasten.setup({opts}) *telekasten.settings.image_subdir* image_subdir: ~ - Sub-directory where pasted images should go to. Set to `nil` if images - should not go into a sub-directory. + Path to the directory where pasted images should go to. Accepts + absolute path or sub-directory name.Set to `nil` if images should not + go into a sub-directory. Default: `nil` diff --git a/lua/telekasten.lua b/lua/telekasten.lua index efad941..267b86e 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -39,12 +39,13 @@ M.Cfg = { -- and thus the telekasten syntax will not be loaded either auto_set_filetype = true, + -- dir names for special notes (absolute path or subdir name) dailies = home .. "/" .. "daily", weeklies = home .. "/" .. "weekly", templates = home .. "/" .. "templates", - -- image subdir for pasting - -- subdir name + -- image (sub)dir for pasting + -- dir name (absolute path or subdir name) -- or nil if pasted images shouldn't go into a special subdir image_subdir = nil, @@ -188,12 +189,13 @@ local function global_dir_check() ret = ret and check_dir_and_ask(M.Cfg.dailies, "dailies") ret = ret and check_dir_and_ask(M.Cfg.weeklies, "weeklies") ret = ret and check_dir_and_ask(M.Cfg.templates, "templates") + ret = ret and check_dir_and_ask(M.Cfg.image_subdir, "images") - local img_dir = M.Cfg.home - if M.Cfg.image_subdir then - img_dir = img_dir .. "/" .. M.Cfg.image_subdir - end - ret = ret and check_dir_and_ask(img_dir, "image_subdir") + -- local img_dir = M.Cfg.home + -- if M.Cfg.image_subdir then + -- img_dir = img_dir .. "/" .. M.Cfg.image_subdir + -- end + -- ret = ret and check_dir_and_ask(img_dir, "M.Cfg.image_subdir") return ret end @@ -203,6 +205,14 @@ local function escape(s) return string.gsub(s, "[%%%]%^%-$().[*+?]", "%%%1") end +local function make_absolute_path(path) + local ret = path + if not (Path:new(path):is_absolute()) and path ~= nil then + ret = M.Cfg.home .. "/" .. path + end + return ret +end + -- ---------------------------------------------------------------------------- -- image stuff local function imgFromClipboard() @@ -251,10 +261,12 @@ local function imgFromClipboard() local relpath = pngname if M.Cfg.image_subdir then - relpath = M.Cfg.image_subdir .. "/" .. pngname - pngpath = M.Cfg.home .. "/" .. M.Cfg.image_subdir + relpath = Path:new(M.Cfg.image_subdir):make_relative(M.Cfg.home) + .. "/" + .. pngname + --pngpath = M.Cfg.home .. "/" .. M.Cfg.image_subdir end - pngpath = pngpath .. "/" .. pngname + pngpath = M.Cfg.image_subdir .. "/" .. pngname os.execute("xclip -selection clipboard -t image/png -o > " .. pngpath) if file_exists(pngpath) then @@ -2562,6 +2574,12 @@ local function Setup(cfg) print("-----------------") print(vim.inspect(M.Cfg)) end + + -- Convert all directories in full path + M.Cfg.image_subdir = make_absolute_path(M.Cfg.image_subdir) + M.Cfg.dailies = make_absolute_path(M.Cfg.dailies) + M.Cfg.weeklies = make_absolute_path(M.Cfg.weeklies) + M.Cfg.templates = make_absolute_path(M.Cfg.templates) end M.find_notes = FindNotes From 2b9f22ff898d34453b4e4050994936e330739954 Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Thu, 20 Jan 2022 21:01:54 +0100 Subject: [PATCH 13/18] Chore: Remove commented code --- lua/telekasten.lua | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 267b86e..70734af 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -191,12 +191,6 @@ local function global_dir_check() ret = ret and check_dir_and_ask(M.Cfg.templates, "templates") ret = ret and check_dir_and_ask(M.Cfg.image_subdir, "images") - -- local img_dir = M.Cfg.home - -- if M.Cfg.image_subdir then - -- img_dir = img_dir .. "/" .. M.Cfg.image_subdir - -- end - -- ret = ret and check_dir_and_ask(img_dir, "M.Cfg.image_subdir") - return ret end From 5e31b13cfb06658f8f99fda0f7475435d5e3509e Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Thu, 20 Jan 2022 21:08:36 +0100 Subject: [PATCH 14/18] Fix: path to save img from clipboard --- lua/telekasten.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 70734af..45007d8 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -251,16 +251,15 @@ local function imgFromClipboard() -- 00000090 10 66 d7 01 b1 e4 fb 79 7c f2 2c e7 cc 39 e7 3d |.f.....y|.,..9.=| local pngname = "pasted_img_" .. os.date("%Y%m%d%H%M%S") .. ".png" - local pngpath = M.Cfg.home + local pngpath = M.Cfg.home .. "/" .. pngname local relpath = pngname if M.Cfg.image_subdir then relpath = Path:new(M.Cfg.image_subdir):make_relative(M.Cfg.home) .. "/" .. pngname - --pngpath = M.Cfg.home .. "/" .. M.Cfg.image_subdir + pngpath = M.Cfg.image_subdir .. "/" .. pngname end - pngpath = M.Cfg.image_subdir .. "/" .. pngname os.execute("xclip -selection clipboard -t image/png -o > " .. pngpath) if file_exists(pngpath) then From c2084a30c7ba87784f03f806f2b97a6e24a4a297 Mon Sep 17 00:00:00 2001 From: lambtho12 Date: Fri, 21 Jan 2022 14:58:38 +0100 Subject: [PATCH 15/18] Refact: better name for make_absolute_path() --- lua/telekasten.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 45007d8..52bbf5d 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -199,7 +199,7 @@ local function escape(s) return string.gsub(s, "[%%%]%^%-$().[*+?]", "%%%1") end -local function make_absolute_path(path) +local function make_config_path_absolute(path) local ret = path if not (Path:new(path):is_absolute()) and path ~= nil then ret = M.Cfg.home .. "/" .. path @@ -2569,10 +2569,10 @@ local function Setup(cfg) end -- Convert all directories in full path - M.Cfg.image_subdir = make_absolute_path(M.Cfg.image_subdir) - M.Cfg.dailies = make_absolute_path(M.Cfg.dailies) - M.Cfg.weeklies = make_absolute_path(M.Cfg.weeklies) - M.Cfg.templates = make_absolute_path(M.Cfg.templates) + M.Cfg.image_subdir = make_config_path_absolute(M.Cfg.image_subdir) + M.Cfg.dailies = make_config_path_absolute(M.Cfg.dailies) + M.Cfg.weeklies = make_config_path_absolute(M.Cfg.weeklies) + M.Cfg.templates = make_config_path_absolute(M.Cfg.templates) end M.find_notes = FindNotes From 19c52829435296c04681ec796351ac67d986f1f6 Mon Sep 17 00:00:00 2001 From: Jacob Pierce Date: Fri, 21 Jan 2022 15:46:41 -0800 Subject: [PATCH 16/18] opts.default_text used when passed in SearchNotes for default_text --- lua/telekasten.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index efad941..f6ebebd 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -1587,7 +1587,7 @@ local function SearchNotes(opts) prompt_title = "Search in notes", cwd = M.Cfg.home, search_dirs = { M.Cfg.home }, - default_text = vim.fn.expand(""), + default_text = opts.default_text or vim.fn.expand(""), find_command = M.Cfg.find_command, attach_mappings = function(_, map) actions.select_default:replace(picker_actions.select_default) From e346e1481fce795d879966b06dd130a0fde54d98 Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Sun, 23 Jan 2022 18:10:51 +0100 Subject: [PATCH 17/18] attempt to fix #79 : https origin for wiki submodule --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index 55be111..ae7da07 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "wiki"] path = wiki - url = git@github.com:renerocksai/telekasten.nvim.wiki.git + url = https://github.com/renerocksai/telekasten.nvim.wiki.git From 15f68e7d9fbf050c9c4b60d9ce890c49867f0abf Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Mon, 24 Jan 2022 09:51:08 +0100 Subject: [PATCH 18/18] fix merge-oopsie for PR #73 --- lua/telekasten.lua | 1 + wiki | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 9c06fe6..fbfc50b 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -208,6 +208,7 @@ local function make_config_path_absolute(path) ret = M.Cfg.home .. "/" .. path end return ret +end -- sanitize strings local escape_chars = function(string) diff --git a/wiki b/wiki index 542099b..22c30fd 160000 --- a/wiki +++ b/wiki @@ -1 +1 @@ -Subproject commit 542099b8930b74c9294650351b1826bd628625ba +Subproject commit 22c30fdc40a03a24e0aab18c859012acd8cb1e46