Inspired by @gustavo-hms’s post Array/iterator and looping behaviour in kak script
The following snippets is build on top of kakoune’s $
command.
It helps you filter your selections more conveniently by using Boolean expression from more expressive language ( lua
and python
supported for the moment)
Example:
Lets say you have the following text:
Só outro silêncio. O senhor sabe o que o silêncio é? É a gente mesmo, demais
And you select all the word with S<space><ret>
You should have:
[Só] [outro] [silêncio]. [O] [senhor] [sabe] [o] [que] [o] [silêncio] [é]? [É] [a] [gente] [mesmo], [demais].
Now let’s keep only the selections of word longer than 5 character, using our plugin and a simple python expression. sel
is the default name of the variable holding the selection:
$ kak_filter.py "len(sel) > 5"
You should have:
Só [outro] [silêncio.] O [senhor] [sabe] o que o [silêncio] é? É a [gente] [mesmo,] [demais]
You could also use more complex expression:
$ kak_filter.py "len(sel) > 3 and 'o' in sel"
To get:
Só [outro] [silêncio.] O [senhor] sabe o que o [silêncio] é? É a gente [mesmo,] demais
Install
Paste one of the following into an executable file in a directory within your $PATH
:
python script
kak_filter.py
#! /usr/bin/env python3.7
from sys import argv
f = 'lambda sel: ' + argv[1]
filter = eval(f)
def main(input):
if filter(input):
exit(0)
else:
exit(1)
main(input())
lua script
kak_filter.lua
#! /usr/bin/env lua
f = 'function (sel) return ' .. arg[1] .. ' end'
func, err = load('return ' .. f)
ok, filter = pcall(func)
function main (input)
if filter(input) then
os.exit(0)
else
os.exit(1)
end
end
main(io.read())
mapping
I suggest to also put the following mapping inside your kakrc
:
map global normal <a-$> '$ kak_filter.py ""<left>'