diff --git a/vim/plugin/matcha.lua b/vim/plugin/matcha.lua index 8703616..b6cea6a 100644 --- a/vim/plugin/matcha.lua +++ b/vim/plugin/matcha.lua @@ -6,44 +6,21 @@ local function nextcol(state) end end -local function unquote(args) - local result = {} - local term - if args[1]:find([=[^["']]=]) then - term = args[1]:sub(1, 1) .. "$" - elseif args[1]:find("^%[=*%[") then - local num = args[1]:find("[", 2, true) - 2 - term = "]" .. string.rep("=", num) .. "]$" - else - return args - end - for _, arg in ipairs(args) do - table.insert(result, arg) - if term and arg:find(term) then - result = {table.concat(result, " "):sub(#term, -#term)} - term = nil - end - end - return result -end - local function colmatch(str, pattern) return nextcol, {str=str,pattern=pattern,last=0} end -local function findLocations(args) - local files if #args>1 then - files = vim.fs.find(function(file) - for _, pattern in ipairs(args), args, 1 do - if vim.fs.basename(file):find(pattern) then - return true +local function findLocations(pattern, first_glob, ...) + local files if first_glob then + files = vim.fn.glob(first_glob, false, true) + if ... then + for _, glob in ipairs { ... } do + for _, file in ipairs(vim.fn.glob(glob, false, true)) do + -- Assume overhead of table.insert is outweighed by all the file ops anyway + table.insert(files, file) end end - return false - end, { - limit=math.huge; - type="file"; - }) + end else files = vim.fn.argv() end @@ -54,7 +31,7 @@ local function findLocations(args) local lnum = 0 for line in io.lines(file) do lnum = lnum + 1 - for from, to in colmatch(line, args[1]) do + for from, to in colmatch(line, pattern) do table.insert(locations, { filename = file; lnum = lnum; @@ -69,18 +46,46 @@ local function findLocations(args) return locations end -vim.api.nvim_create_user_command("Matcha", function(params) - local locations = findLocations(unquote(params.fargs)) - if not locations[1] then return print("Nothing found") end - vim.fn.setqflist(locations) - vim.cmd("copen") - vim.cmd("crewind") -end, {nargs="+"}) +local parse_args do + if pcall(require, "arrr") then + local arrr = require "arrr" + + parse_args = arrr { + { "Use location list instead of quickfix list", "--location-list", "-l" }; + { "Appends to the current quicklist", "--append", "-a" }; + { "Replaces the current quicklist", "--replace", "-r" }; + } + else + function parse_args(args) + return args + end + end +end + + +vim.api.nvim_create_user_command("Matcha", function(params) + local args = parse_args(params.fargs) + local locations = findLocations(unpack(args)) -vim.api.nvim_create_user_command("MatchaLocal", function(params) - local locations = findLocations(unquote(params.fargs)) if not locations[1] then return print("Nothing found") end - vim.fn.setloclist(0, locations) - vim.cmd("lopen") - vim.cmd("lrewind") + + local action if args.append then + action = "a" + elseif args.replace then + action = "u" + else + action = " " + end + + local what = { title = string.format("Matcha: `%s`", args[1]), items = locations } + + if args.locationlist then + vim.fn.setloclist(0, {}, action, what) + vim.cmd("lopen") + vim.cmd("lrewind") + else + vim.fn.setqflist({}, action, what) + vim.cmd("copen") + vim.cmd("crewind") + end end, {nargs="+"}) diff --git a/vimrc b/vimrc index 4714943..b3d4674 100644 --- a/vimrc +++ b/vimrc @@ -16,4 +16,6 @@ set foldlevelstart=99 set foldmethod=expr set foldexpr=nvim_treesitter#foldexpr() +cnoreabbrev matcha Matcha -l + nnoremap g# :b #