diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 67d62c3..6491225 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -238,25 +238,6 @@ local function make_config_path_absolute(path) return ret 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 @@ -267,8 +248,8 @@ local function recursive_substitution(dir, old, new) return end - old = escape_chars(old) - new = escape_chars(new) + old = tkutils.grep_escape(old) + new = tkutils.grep_escape(new) local sedcommand = "sed -i" if vim.fn.has("mac") == 1 then diff --git a/lua/telekasten/utils/init.lua b/lua/telekasten/utils/init.lua index 5186cb1..01f10b2 100644 --- a/lua/telekasten/utils/init.lua +++ b/lua/telekasten/utils/init.lua @@ -1,7 +1,7 @@ local M = {} -- Prints a basic error message -local function print_error(s) +function M.print_error(s) vim.cmd("echohl ErrorMsg") vim.cmd("echomsg " .. '"' .. s .. '"') vim.cmd("echohl None") @@ -18,4 +18,23 @@ function M.strip(s, chars_to_remove) return s:gsub("[" .. M.escape(chars_to_remove) .. "]", "") end +-- Escapes for regex functions like grep or rg +function M.grep_escape(s) + return s:gsub("[%(|%)|\\|%[|%]|%-|%{%}|%?|%+|%*|%^|%$|%/]", { + ["\\"] = "\\\\", + ["-"] = "\\-", + ["("] = "\\(", + [")"] = "\\)", + ["["] = "\\[", + ["]"] = "\\]", + ["{"] = "\\{", + ["}"] = "\\}", + ["?"] = "\\?", + ["+"] = "\\+", + ["*"] = "\\*", + ["^"] = "\\^", + ["$"] = "\\$", + }) +end + return M