better grep,live_grep search query continuation

main
bhagwan 3 years ago
parent 34eedd8c3e
commit 67b727bd8e

@ -273,7 +273,7 @@ M.fzf_files_interactive = function(opts)
-- cannot be nil
local query = opts._live_query or ''
local placeholder = utils._if(opts._is_skim, '"{}"', '{q}')
local placeholder = utils._if(opts._is_skim, "'{}'", '{q}')
local uv = vim.loop
local raw_async_act = require("fzf.actions").raw_async_action(function(pipe, args)
@ -337,20 +337,23 @@ M.fzf_files_interactive = function(opts)
-- skim interactive mode does not need a piped command
opts.fzf_fn = nil
opts._fzf_cli_args = string.format(
"--prompt='*%s' --cmd-prompt='%s' --cmd-query='%s' -i -c %s",
opts.prompt, opts.prompt, query, act_cmd)
"--prompt='*%s' --cmd-prompt='%s' --cmd-query=%s -i -c %s",
opts.prompt, opts.prompt,
vim.fn.shellescape(query),
act_cmd)
else
-- fzf already adds single quotes
-- around the place holder
opts.fzf_fn = {}
if opts.exec_empty_query or (query and #query>0) then
opts.fzf_fn = require("fzf.helpers").cmd_line_transformer(
act_cmd:gsub(placeholder, ('"%s"'):format(query)),
act_cmd:gsub(placeholder, vim.fn.shellescape(query)),
function(x)
return M.make_entry_file(opts, x)
end)
end
opts._fzf_cli_args = string.format('--phony --query="%s" --bind=%s', query,
opts._fzf_cli_args = string.format('--phony --query=%s --bind=%s',
vim.fn.shellescape(query),
vim.fn.shellescape(string.format("change:reload:%s || true", act_cmd)))
end

@ -37,10 +37,11 @@ local get_grep_cmd = function(opts, search_query, no_esc)
search_path = vim.fn.shellescape(opts.cwd)
end
if search_query == nil then search_query = '""'
elseif not no_esc and not opts.no_esc then
search_query = '"' .. utils.rg_escape(search_query) .. '"'
search_query = search_query or ''
if not (no_esc or opts.no_esc) then
search_query = utils.rg_escape(search_query)
end
search_query = vim.fn.shellescape(search_query)
return string.format('%s %s %s', command, search_query, search_path)
end
@ -50,9 +51,10 @@ local function set_search_header(opts)
if opts.no_header then return opts end
if not opts.search or #opts.search==0 then return opts end
if not opts.search_header then opts.search_header = "Searching for:" end
opts.fzf_cli_args = opts.fzf_cli_args or ''
opts.fzf_cli_args = string.format([[%s --header="%s %s"]],
opts.fzf_cli_args, opts.search_header, opts.search:gsub('"', '\\"'))
opts._fzf_cli_args = opts._fzf_cli_args or ''
opts._fzf_cli_args = string.format([[%s --header=%s]],
opts._fzf_cli_args,
vim.fn.shellescape(("%s %s"):format(opts.search_header, opts.search)))
return opts
end
@ -87,9 +89,7 @@ M.grep = function(opts)
last_search.no_esc = no_esc or opts.no_esc
last_search.query = opts.search
local command = get_grep_cmd(opts,
utils._if(no_esc, vim.fn.shellescape(opts.search), opts.search),
no_esc)
local command = get_grep_cmd(opts, opts.search, no_esc)
opts.fzf_fn = fzf_helpers.cmd_line_transformer(
command,
@ -125,7 +125,7 @@ M.live_grep = function(opts)
last_search.no_esc = true
last_search.query = opts.search
-- escape unless the user requested not to
if not no_esc and not opts.no_esc then
if not (no_esc or opts.no_esc) then
opts._live_query = utils.rg_escape(opts.search)
end
end
@ -142,7 +142,7 @@ M.live_grep = function(opts)
-- fix this collision, rename to _filespec
opts.no_esc = nil
opts.filespec = nil
return get_grep_cmd(opts, vim.fn.shellescape(query), true)
return get_grep_cmd(opts, query, true)
end
core.fzf_files_interactive(opts)
@ -167,45 +167,49 @@ M.live_grep_old = function(opts)
last_search.no_esc = true
last_search.query = opts.search
-- escape unless the user requested not to
if not no_esc and not opts.no_esc then
if not (no_esc or opts.no_esc) then
query = utils.rg_escape(opts.search)
end
end
local placeholder
local reload_command
local initial_command = nil
if opts._is_skim then
placeholder = '"{}"'
placeholder = "{}"
reload_command = get_grep_cmd(opts , placeholder, true)
-- do not run an empty string query unless the user requested
if not opts.exec_empty_query then
reload_command = "sh -c " .. vim.fn.shellescape(
("[ -z %s ] || %s"):format(placeholder, reload_command))
("[ -z '%s' ] || %s"):format(placeholder, reload_command))
else
reload_command = vim.fn.shellescape(reload_command)
end
-- skim interactive mode does not need a piped command
opts.fzf_fn = nil
opts._fzf_cli_args = string.format(
"--prompt='*%s' --cmd-prompt='%s' --cmd-query='%s' -i -c %s",
opts.prompt, opts.prompt, query, reload_command)
"--prompt='*%s' --cmd-prompt='%s' --cmd-query=%s -i -c %s",
opts.prompt, opts.prompt,
vim.fn.shellescape(query),
reload_command)
else
-- fzf already adds single quotes
-- around the place holder
-- fzf already adds single quotes around
-- the place holder (on query), delete those
-- added by 'get_grep_cmd'
placeholder = '{q}'
reload_command = get_grep_cmd(opts, placeholder, true) .. " || true"
reload_command = get_grep_cmd(opts, placeholder, true):
gsub(("'%s'"):format(placeholder), placeholder)
opts.fzf_fn = {}
if opts.exec_empty_query or (opts.search and #opts.search > 0) then
initial_command = get_grep_cmd(opts, opts.search)
opts.fzf_fn = fzf_helpers.cmd_line_transformer(
initial_command,
reload_command:gsub(placeholder, vim.fn.shellescape(query)),
function(x)
return core.make_entry_file(opts, x)
end)
end
opts._fzf_cli_args = string.format('--phony --query="%s" --bind=%s', query,
vim.fn.shellescape(string.format("change:reload:%s", reload_command)))
opts._fzf_cli_args = string.format('--phony --query=%s --bind=%s',
vim.fn.shellescape(query),
vim.fn.shellescape(("change:reload:%s"):format(
("%s || true"):format(reload_command))))
end
-- we cannot parse any entries as they're not getting called

@ -66,7 +66,7 @@ function M.rg_escape(str)
if not str then return str end
-- [(~'"\/$?'`*&&||;[]<>)]
-- escape "\~$?*|[()"
return str:gsub('[\\~$?*|{\\[()"`]', function(x)
return str:gsub('[\\~$?*|{\\[()]', function(x)
return '\\' .. x
end)
end

Loading…
Cancel
Save