Created
September 29, 2022 03:12
-
-
Save tjdevries/5ea9a4aa5afc64667a250794c1ee9279 to your computer and use it in GitHub Desktop.
cfilter.vim -> cfilter.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- vim9script | |
-- # cfilter.vim: Plugin to filter entries from a quickfix/location list | |
-- # Last Change: Jun 30, 2022 | |
-- # Maintainer: Yegappan Lakshmanan (yegappan AT yahoo DOT com) | |
-- # Version: 2.0 | |
-- # | |
-- # Commands to filter the quickfix list: | |
-- # :Cfilter[!] /{pat}/ | |
-- # Create a new quickfix list from entries matching {pat} in the current | |
-- # quickfix list. Both the file name and the text of the entries are | |
-- # matched against {pat}. If ! is supplied, then entries not matching | |
-- # {pat} are used. The pattern can be optionally enclosed using one of | |
-- # the following characters: ', ", /. If the pattern is empty, then the | |
-- # last used search pattern is used. | |
-- # :Lfilter[!] /{pat}/ | |
-- # Same as :Cfilter but operates on the current location list. | |
-- # | |
-- Token(EndOfLine, "\n", (18,0)->(18,0)) | |
local Qf_filter = function(qf, searchpat, bang) | |
local Xgetlist = nil | |
local Xsetlist = nil | |
local cmd = nil | |
local firstchar = nil | |
local lastchar = nil | |
local pat = nil | |
local title = nil | |
local Cond = nil | |
local items = nil | |
-- Token(EndOfLine, "\n", (29,0)->(29,0)) | |
if qf then | |
Xgetlist = function(...) | |
return vim.fn["getqflist"](...) | |
end | |
Xsetlist = function(...) | |
return vim.fn["setqflist"](...) | |
end | |
cmd = require("vim9script").ops["StringConcat"](":Cfilter", bang) | |
else | |
Xgetlist = function(...) | |
local copied = vim.deepcopy({ 0 }) | |
for _, val in ipairs({ ... }) do | |
table.insert(copied, val) | |
end | |
return vim.fn["function"](unpack(copied)) | |
end | |
Xsetlist = function(...) | |
local copied = vim.deepcopy({ 0 }) | |
for _, val in ipairs({ ... }) do | |
table.insert(copied, val) | |
end | |
return vim.fn["function"](unpack(copied)) | |
end | |
cmd = require("vim9script").ops["StringConcat"](":Lfilter", bang) | |
end | |
-- Token(EndOfLine, "\n", (39,0)->(39,0)) | |
firstchar = require("vim9script").index(searchpat, 0) | |
lastchar = require("vim9script").slice(searchpat, require("vim9script").prefix["Minus"](1), nil) | |
if | |
require("vim9script").ops["And"]( | |
require("vim9script").ops["EqualTo"](firstchar, lastchar), | |
( | |
require("vim9script").ops["Or"]( | |
require("vim9script").ops["Or"]( | |
require("vim9script").ops["EqualTo"](firstchar, "/"), | |
require("vim9script").ops["EqualTo"](firstchar, '"') | |
), | |
require("vim9script").ops["EqualTo"](firstchar, "'") | |
) | |
) | |
) | |
then | |
pat = require("vim9script").slice(searchpat, 1, require("vim9script").prefix["Minus"](2)) | |
if require("vim9script").ops["EqualTo"](pat, "") then | |
-- # Use the last search pattern | |
pat = vim.fn.getreg("/") | |
end | |
else | |
pat = searchpat | |
end | |
-- Token(EndOfLine, "\n", (52,0)->(52,0)) | |
if require("vim9script").ops["EqualTo"](pat, "") then | |
return | |
end | |
-- Token(EndOfLine, "\n", (56,0)->(56,0)) | |
if require("vim9script").ops["EqualTo"](bang, "!") then | |
Cond = function(_, val) | |
return require("vim9script").ops["And"]( | |
require("vim9script").ops["NotRegexpMatches"](val["text"], pat), | |
require("vim9script").ops["NotRegexpMatches"](vim.fn["bufname"](val["bufnr"]), pat) | |
) | |
end | |
else | |
Cond = function(_, val) | |
return require("vim9script").ops["Or"]( | |
require("vim9script").ops["RegexpMatches"](val["text"], pat), | |
require("vim9script").ops["RegexpMatches"](vim.fn["bufname"](val["bufnr"]), pat) | |
) | |
end | |
end | |
-- Token(EndOfLine, "\n", (62,0)->(62,0)) | |
items = vim.fn["filter"](Xgetlist(), Cond) | |
title = require("vim9script").ops["StringConcat"]( | |
require("vim9script").ops["StringConcat"](require("vim9script").ops["StringConcat"](cmd, " /"), pat), | |
"/" | |
) | |
Xsetlist({}, " ", { title = title, items = items }) | |
end | |
-- Token(EndOfLine, "\n", (67,0)->(67,0)) | |
vim.api.nvim_create_user_command("Cfilter", function(__vim9_arg_1) | |
Qf_filter(true, __vim9_arg_1.args, vim.fn.expand("<q-bang>")) | |
end, { | |
nargs = "+", | |
}) | |
vim.api.nvim_create_user_command("Lfilter", function(__vim9_arg_1) | |
Qf_filter(false, __vim9_arg_1.args, vim.fn.expand("<q-bang>")) | |
end, { | |
nargs = "+", | |
}) | |
-- Token(EndOfLine, "\n", (70,0)->(70,0)) | |
-- # vim: shiftwidth=2 sts=2 expandtab | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vim9script | |
# cfilter.vim: Plugin to filter entries from a quickfix/location list | |
# Last Change: Jun 30, 2022 | |
# Maintainer: Yegappan Lakshmanan (yegappan AT yahoo DOT com) | |
# Version: 2.0 | |
# | |
# Commands to filter the quickfix list: | |
# :Cfilter[!] /{pat}/ | |
# Create a new quickfix list from entries matching {pat} in the current | |
# quickfix list. Both the file name and the text of the entries are | |
# matched against {pat}. If ! is supplied, then entries not matching | |
# {pat} are used. The pattern can be optionally enclosed using one of | |
# the following characters: ', ", /. If the pattern is empty, then the | |
# last used search pattern is used. | |
# :Lfilter[!] /{pat}/ | |
# Same as :Cfilter but operates on the current location list. | |
# | |
def Qf_filter(qf: bool, searchpat: string, bang: string) | |
var Xgetlist: func | |
var Xsetlist: func | |
var cmd: string | |
var firstchar: string | |
var lastchar: string | |
var pat: string | |
var title: string | |
var Cond: func | |
var items: list<any> | |
if qf | |
Xgetlist = function('getqflist') | |
Xsetlist = function('setqflist') | |
cmd = ':Cfilter' .. bang | |
else | |
Xgetlist = function('getloclist', [0]) | |
Xsetlist = function('setloclist', [0]) | |
cmd = ':Lfilter' .. bang | |
endif | |
firstchar = searchpat[0] | |
lastchar = searchpat[-1 :] | |
if firstchar == lastchar && | |
(firstchar == '/' || firstchar == '"' || firstchar == "'") | |
pat = searchpat[1 : -2] | |
if pat == '' | |
# Use the last search pattern | |
pat = @/ | |
endif | |
else | |
pat = searchpat | |
endif | |
if pat == '' | |
return | |
endif | |
if bang == '!' | |
Cond = (_, val) => val.text !~# pat && bufname(val.bufnr) !~# pat | |
else | |
Cond = (_, val) => val.text =~# pat || bufname(val.bufnr) =~# pat | |
endif | |
items = filter(Xgetlist(), Cond) | |
title = cmd .. ' /' .. pat .. '/' | |
Xsetlist([], ' ', {title: title, items: items}) | |
enddef | |
command! -nargs=+ -bang Cfilter Qf_filter(true, <q-args>, <q-bang>) | |
command! -nargs=+ -bang Lfilter Qf_filter(false, <q-args>, <q-bang>) | |
# vim: shiftwidth=2 sts=2 expandtab |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment