Skip to content

Commit

Permalink
Extracted args binding prototype function for #25
Browse files Browse the repository at this point in the history
  • Loading branch information
amerlyq committed Dec 2, 2015
1 parent f064aa0 commit a2ab6b3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 50 deletions.
55 changes: 6 additions & 49 deletions autoload/ag.vim
Original file line number Diff line number Diff line change
Expand Up @@ -66,54 +66,16 @@ function! ag#AgBuffer(cmd, args)
call ag#Ag(a:cmd, a:args . ' ' . join(l:files, ' '))
endfunction

function! ag#VisualSelection()
let selection = ""
try
let a_save = @a
normal! gv"ay
let selection = @a
finally
let @a = a_save
endtry
return selection
endfunction

let g:last_aggroup=""

function! ag#AgGroupLast(ncontext)
call ag#AgGroup(a:ncontext, 0, '', g:last_aggroup)
endfunction

function! ag#GetArgs(ncontext, visualmode, args)
if !empty(a:args)
let l:grepargs = a:args
else
if a:visualmode
let l:grepargs = ag#VisualSelection()
else
let l:grepargs = ""
endif
if empty(l:grepargs)
let l:grepargs = expand("<cword>")
if empty(l:grepargs)
let l:grepargs = g:last_aggroup
endif
else
let l:grepargs = '"' . l:grepargs . '"'
endif
endif
return l:grepargs
endfunction

function! ag#AgGroup(ncontext, visualmode, fileregexp, args)
let l:grepargs = ag#GetArgs(a:ncontext, a:visualmode, a:args)

if empty(l:grepargs)
echo "empty search"
return
endif

function! ag#AgGroup(args, ncontext, ...)
let l:grepargs = a:args
let g:last_aggroup = l:grepargs
let fileregexp = (a:0<1 ? '' : '-G'.a:1)

silent! wincmd P
if !&previewwindow
Expand All @@ -133,11 +95,6 @@ function! ag#AgGroup(ncontext, visualmode, fileregexp, args)
let context = '-C' . a:ncontext
endif

if empty(a:fileregexp)
let fileregexp = ''
else
let fileregexp = '-G' . a:fileregexp
endif

let l:grepargs = substitute(l:grepargs, '#', '\\#','g')
let l:grepargs = substitute(l:grepargs, '%', '\\%','g')
Expand Down Expand Up @@ -195,7 +152,7 @@ function ToggleShowLine()
setlocal conceallevel=2
else
setlocal conceallevel=0
endif
endif
endfunction

function DeleteFold()
Expand All @@ -216,14 +173,14 @@ endfunction
"
function NextFold()
let save_a_mark = getpos("'a")
let mark_a_exists = save_a_mark[1] == 0
let mark_a_exists = save_a_mark[1] == 0
mark a
execute 'normal zMzjzo'
if getpos('.')[1] == getpos("'a")[1]
"no movement go to first position
normal gg
execute 'normal zMzjzo'
endif
endif
if mark_a_exists
call setpos("'a", save_a_mark)
else
Expand Down
31 changes: 31 additions & 0 deletions autoload/ag/args.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function! s:get_vsel(...)
" DEV:RFC:ADD: 'range' postfix and use a:firstline, etc -- to exec f once?
let [lnum1, col1] = getpos("'<")[1:2]
let [lnum2, col2] = getpos("'>")[1:2]
let lines = getline(lnum1, lnum2)
let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)]
let lines[0] = lines[0][col1 - 1:]
return a:0 >= 1 ? join(lines, a:1) : lines
endfunction


function! ag#args#derive()
" TODO: for derived always add -Q -- if don't have option 'treat_as_rgx'
if visualmode() !=# ''
return s:get_vsel('\n')
endif
" TODO: add -w for words
let l:args = expand("<cword>")
if !empty(l:args)
return l:args
endif
return g:last_aggroup
endfunction


function! ag#args#bind(func, args, ...)
let l:args = (empty(a:args) ? ag#args#derive() : a:args)
if empty(l:args) | echo "empty search" | return | endif
" TODO: replace quotes with escaping
call call(a:func, ['"'.l:args.'"'] + a:000)
endfunction
2 changes: 1 addition & 1 deletion plugin/ag.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ command! -bang -nargs=* -complete=file LAgAdd call ag#Ag('lgrepadd<bang>', <q-ar
command! -bang -nargs=* -complete=file LAgBuffer call ag#AgBuffer('lgrep<bang>',<q-args>)
command! -bang -nargs=* -complete=help LAgHelp call ag#AgHelp('lgrep<bang>',<q-args>)

command! -count -nargs=* AgGroup call ag#AgGroup(<count>, 0, '', <q-args>)
command! -count -nargs=* AgGroup call ag#args#bind('ag#AgGroup', <q-args>, <count>)
command! -count -nargs=* AgGroupFile call ag#AgGroup(<count>, 0, <f-args>)
command! -count AgGroupLast call ag#AgGroupLast(<count>)

Expand Down

0 comments on commit a2ab6b3

Please sign in to comment.