refact: grep_escape

This commit is contained in:
Thomas Lambert
2023-04-29 00:44:52 +02:00
parent 9c95eb0e52
commit a518036fbf
2 changed files with 22 additions and 22 deletions

View File

@@ -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