From 97a63e66bfc74cd32dec04ae7358dc96b184a3a2 Mon Sep 17 00:00:00 2001 From: bhagwan Date: Thu, 24 Mar 2022 13:12:43 -0700 Subject: [PATCH] tags|btags: properly handle all special characters (closes #371) --- lua/fzf-lua/make_entry.lua | 8 +++++++- lua/fzf-lua/path.lua | 18 ++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lua/fzf-lua/make_entry.lua b/lua/fzf-lua/make_entry.lua index ea8fe86..4c72da2 100644 --- a/lua/fzf-lua/make_entry.lua +++ b/lua/fzf-lua/make_entry.lua @@ -332,7 +332,13 @@ M.tag = function(opts, x) local name, file, text = x:match("([^\t]+)\t([^\t]+)\t(.*)") if not file or not name or not text then return x end text = text:match('(.*);"') or text -- remove ctag comments - local line, tag = text:gsub("\\/", "/"):match("(%d-);?(/.*/)") + -- unescape ctags special chars + -- '\/' -> '/' + -- '\\' -> '\' + for _, s in ipairs({ '/', '\\' }) do + text = text:gsub([[\]]..s, s) + end + local line, tag = text:match("(%d-);?(/.*/)") line = line and #line>0 and tonumber(line) return ("%s%s: %s %s"):format( M.file(opts, file), diff --git a/lua/fzf-lua/path.lua b/lua/fzf-lua/path.lua index 9d9d81c..c869fc2 100644 --- a/lua/fzf-lua/path.lua +++ b/lua/fzf-lua/path.lua @@ -140,18 +140,20 @@ end function M.entry_to_ctag(entry, noesc) - local scode = entry:match("%:.-/^?\t?(.*)/") + local ctag = entry:match("%:.-/^?\t?(.*)/") -- if tag name contains a slash we could -- have the wrong match, most tags start -- with ^ so try to match based on that - scode = scode and scode:match("/^(.*)") or scode - if scode and not noesc then - -- scode = string.gsub(scode, "[$]$", "") - scode = string.gsub(scode, [[\\]], [[\]]) - scode = string.gsub(scode, [[\/]], [[/]]) - scode = string.gsub(scode, "[*]", [[\*]]) + ctag = ctag and ctag:match("/^(.*)") or ctag + if ctag and not noesc then + -- required escapes for vim.fn.search() + -- \ ] ~ * + ctag = ctag:gsub('[\\%]~*]', + function(x) + return '\\' .. x + end) end - return scode + return ctag end function M.entry_to_location(entry)