if vim.g.loaded_grep then return end vim.g.loaded_grep = true local function grep(props) local grepcmd, n = vim.o.grepprg:gsub("%$%*", props.args) if n == 0 then grepcmd = grepcmd .. " " .. props.args end vim.api.nvim_exec_autocmds("QuickFixCmdPre", { pattern = "grep", modeline = false, }) local fn = function(o) local src = o.stderr if o.code == 0 then src = o.stdout end src = src local lines = vim.split(src, "\n", { trimempty = true }) vim.schedule(function() vim.fn.setqflist({}, " ", { title = grepcmd, lines = lines, efm = vim.o.grepformat, nr = "$", }) vim.api.nvim_exec_autocmds("QuickFixCmdPost", { pattern = "grep", modeline = false, }) end) end vim.system({ vim.o.shell, "-c", grepcmd }, { text = true, }, fn) end vim.api.nvim_create_user_command("Grep", grep, { nargs = "+", complete = "file_in_path", force = true }) vim.api.nvim_create_autocmd("QuickFixCmdPost", { group = vim.api.nvim_create_augroup("grep", { clear = true }), nested = true, callback = function() local list = vim.fn.getqflist() vim.cmd([[ cclose|botright ]] .. " " .. (math.min(10, #list)) .. "cwindow") end, }) vim.keymap.set("n", "/", ":Grep ", { silent = false }) vim.keymap.set("x", "/", function() local lines = vim.fn.getregion(vim.fn.getpos("v"), vim.fn.getpos("."), { type = vim.api.nvim_get_mode().mode }) if #lines ~= 1 then return ":Grep " end local line = string.gsub(lines[1], "<", [[]]) return ":Grep " .. line end, { silent = false, expr = true }) -- find -- vim.o.findfunc = "v:lua.vim.g.findfunc" ---@type fun(cmdarg: string, cmdcomplete: boolean): string[] vim.g.findfunc = function(cmdarg, _) if not vim.g.findprg then return {} end local farg = string.format("'%s'", cmdarg) local findcmd, n = vim.g.findprg:gsub("%$%*", farg) if n == 0 then findcmd = findcmd .. " " .. farg end local fn = function(o) local src = o.stderr if o.code == 0 then src = o.stdout end src = src local lines = vim.split(src, "\n", { trimempty = true }) return lines end return fn(vim .system({ vim.o.shell, "-c", findcmd }, { text = true, }) :wait()) end