Skip to content

Commit

Permalink
Merge pull request #96 from lambdalisue/grep
Browse files Browse the repository at this point in the history
Add 'grep' action on 'file' scheme
  • Loading branch information
lambdalisue authored May 31, 2020
2 parents cf0645e + 7ef10e3 commit abf1671
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
12 changes: 12 additions & 0 deletions autoload/fern/internal/args.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
function! fern#internal#args#split(cmd) abort
let sq = '''\zs[^'']\+\ze'''
let dq = '"\zs[^"]\+\ze"'
let bs = '\%(\\\s\|[^ ''"]\)\+'
let pp = printf(
\ '\%%(%s\)*\zs\%%(\s\+\|$\)\ze',
\ join([sq, dq, bs], '\|')
\)
let np = '^\%("\zs.*\ze"\|''\zs.*\ze''\|.*\)$'
return map(split(a:cmd, pp), { -> matchstr(v:val, np) })
endfunction

function! fern#internal#args#index(args, pattern) abort
for index in range(len(a:args))
if a:args[index] =~# a:pattern
Expand Down
1 change: 1 addition & 0 deletions autoload/fern/scheme/file/mapping.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function! fern#scheme#file#mapping#init(disable_default_mappings) abort
call fern#scheme#file#mapping#clipboard#init(a:disable_default_mappings)
call fern#scheme#file#mapping#rename#init(a:disable_default_mappings)
call fern#scheme#file#mapping#terminal#init(a:disable_default_mappings)
call fern#scheme#file#mapping#grep#init(a:disable_default_mappings)

nnoremap <buffer><silent> <Plug>(fern-action-new-file) :<C-u>call <SID>call('new_file')<CR>
nnoremap <buffer><silent> <Plug>(fern-action-new-dir) :<C-u>call <SID>call('new_dir')<CR>
Expand Down
53 changes: 53 additions & 0 deletions autoload/fern/scheme/file/mapping/grep.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
let s:Config = vital#fern#import('Config')
let s:Promise = vital#fern#import('Async.Promise')
let s:Process = vital#fern#import('Async.Promise.Process')

function! fern#scheme#file#mapping#grep#init(disable_default_mappings) abort
nnoremap <buffer><silent> <Plug>(fern-action-grep) :<C-u>call <SID>call('grep')<CR>
endfunction

function! s:call(name, ...) abort
return call(
\ 'fern#mapping#call',
\ [funcref(printf('s:map_%s', a:name))] + a:000,
\)
endfunction

function! s:map_grep(helper) abort
let pattern = input('Grep: ', '')
if empty(pattern)
return s:Promise.reject('Cancelled')
endif
let node = a:helper.sync.get_cursor_node()
let node = node.status isnot# a:helper.STATUS_EXPANDED ? node.__owner : node
let args = s:grepargs([pattern, fern#internal#filepath#from_slash(node._path)])
let efm = g:fern#scheme#file#mapping#grep#grepformat
let title = printf('[fern] %s', join(map(copy(args), { _, v -> v =~# '\s' ? printf('"%s"', v) : v }), ' '))
let token = a:helper.fern.source.token
return s:Process.start(args, { 'token': token })
\.then({ v -> v.stdout })
\.then({ v -> setqflist([], 'a', { 'efm': efm, 'lines': v, 'title': title }) })
\.then({ -> execute('copen') })
endfunction

function! s:grepargs(args) abort
let args = fern#internal#args#split(g:fern#scheme#file#mapping#grep#grepprg)
let args = map(args, { _, v -> v =~# '^[%#]\%(:.*\)\?$' ? expand(v) : v })
let index = index(args, '$*')
return index is# -1
\ ? args + a:args
\ : args[:index - 1] + a:args + args[index + 1:]
endfunction

function! s:default_grepprg() abort
if &grepprg =~# '^\%(grep -n \|grep -n $\* /dev/null\|internal\)$'
return has('unix') ? 'grep -rn $* /dev/null' : 'gren -rn'
endif
return &grepprg
endfunction


call s:Config.config(expand('<sfile>:p'), {
\ 'grepprg': s:default_grepprg(),
\ 'grepformat': &grepformat,
\})

0 comments on commit abf1671

Please sign in to comment.