Skip to content

Instantly share code, notes, and snippets.

@ujihisa
Created May 15, 2019 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ujihisa/20d4717a92a434f520d2f6ceb471f130 to your computer and use it in GitHub Desktop.
Save ujihisa/20d4717a92a434f520d2f6ceb471f130 to your computer and use it in GitHub Desktop.
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