Created
May 15, 2019 11:42
-
-
Save ujihisa/20d4717a92a434f520d2f6ceb471f130 to your computer and use it in GitHub Desktop.
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
function! s:flatten(xss) abort | |
let memo = [] | |
for xs in a:xss | |
for x in xs | |
call add(memo, x) | |
endfor | |
endfor | |
return memo | |
endfunction | |
function! s:f(cwd, file, keyword) abort | |
let path = a:cwd . '/' . a:file | |
if isdirectory(path) | |
return s:grep(path, a:keyword) | |
else | |
for line in readfile(path) | |
if line =~ a:keyword | |
return [path] | |
endif | |
endfor | |
return [] | |
endif | |
endfunction | |
function! s:grep(cwd, keyword) abort | |
let stdout = system(printf("ls %s", a:cwd)) | |
if stdout =~ 'No such file' | |
return [] | |
endif | |
let files = split(stdout, "\n") | |
return s:flatten(map(files, { _, file -> s:f(a:cwd, file, a:keyword) })) | |
endfunction | |
echo s:grep(getcwd(), 'aaa') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment