robin/ivy

my custom neovim config

git clone https://git.robinwobin.dev/robin/ivy

robinplugin: function options1626f32

main
2.2 KiB88 linesraw
1if vim.g.loaded_grep then
2  return
3end
4
5vim.g.loaded_grep = true
6
7local function grep(props)
8  local grepcmd, n = vim.o.grepprg:gsub("%$%*", props.args)
9  if n == 0 then
10    grepcmd = grepcmd .. " " .. props.args
11  end
12  vim.api.nvim_exec_autocmds("QuickFixCmdPre", {
13    pattern = "grep",
14    modeline = false,
15  })
16  local fn = function(o)
17    local src = o.stderr
18    if o.code == 0 then
19      src = o.stdout
20    end
21    src = src
22    local lines = vim.split(src, "\n", { trimempty = true })
23    vim.schedule(function()
24      vim.fn.setqflist({}, " ", {
25        title = grepcmd,
26        lines = lines,
27        efm = vim.o.grepformat,
28        nr = "$",
29      })
30      vim.api.nvim_exec_autocmds("QuickFixCmdPost", {
31        pattern = "grep",
32        modeline = false,
33      })
34    end)
35  end
36  vim.system({ vim.o.shell, "-c", grepcmd }, {
37    text = true,
38  }, fn)
39end
40
41vim.api.nvim_create_user_command("Grep", grep, { nargs = "+", complete = "file_in_path", force = true })
42
43vim.api.nvim_create_autocmd("QuickFixCmdPost", {
44  group = vim.api.nvim_create_augroup("grep", { clear = true }),
45  nested = true,
46  callback = function()
47    local list = vim.fn.getqflist()
48    vim.cmd([[ cclose|botright ]] .. " " .. (math.min(10, #list)) .. "cwindow")
49  end,
50})
51
52vim.keymap.set("n", "<space>/", ":Grep ", { silent = false })
53vim.keymap.set("x", "<space>/", function()
54  local lines = vim.fn.getregion(vim.fn.getpos("v"), vim.fn.getpos("."), { type = vim.api.nvim_get_mode().mode })
55  if #lines ~= 1 then
56    return ":Grep "
57  end
58  local line = string.gsub(lines[1], "<", [[<lt>]])
59  return ":Grep " .. line
60end, { silent = false, expr = true })
61
62-- find --
63
64---@type fun(cmdarg: string, cmdcomplete: boolean): string[]
65vim.o.findfunc = function(cmdarg, _)
66  if not vim.g.findprg then
67    return {}
68  end
69  local farg = string.format("'%s'", cmdarg)
70  local findcmd, n = vim.g.findprg:gsub("%$%*", farg)
71  if n == 0 then
72    findcmd = findcmd .. " " .. farg
73  end
74  local fn = function(o)
75    local src = o.stderr
76    if o.code == 0 then
77      src = o.stdout
78    end
79    src = src
80    local lines = vim.split(src, "\n", { trimempty = true })
81    return lines
82  end
83  return fn(vim
84    .system({ vim.o.shell, "-c", findcmd }, {
85      text = true,
86    })
87    :wait())
88end