diff --git a/autoload/ack.vim b/autoload/ack.vim index 814c533b..fd066a94 100644 --- a/autoload/ack.vim +++ b/autoload/ack.vim @@ -11,7 +11,7 @@ endif " Public API "----------------------------------------------------------------------------- -function! ack#Ack(cmd, args) "{{{ +function! ack#Ack(cmd, args, locations, count) "{{{ call s:Init(a:cmd) redraw @@ -26,8 +26,20 @@ function! ack#Ack(cmd, args) "{{{ let l:grepformat = '%f' endif - " If no pattern is provided, search for the word under the cursor - let l:grepargs = empty(a:args) ? expand("") : a:args . join(a:000, ' ') + if a:count > 0 + " then we've selected something in visual mode + let l:grepargs = shellescape(fnameescape(s:LastSelectedText())) + elseif empty(a:args) + " If no pattern is provided, search for the word under the cursor + let l:grepargs = expand("") + else + let l:grepargs = a:args . join(a:000, ' ') + end + + " Add locations to search in + if a:locations != '' + let l:grepargs .= ' '.a:locations + endif " NOTE: we escape special chars, but not everything using shellescape to " allow for passing arguments etc @@ -51,15 +63,14 @@ function! ack#AckFromSearch(cmd, args) "{{{ let search = getreg('/') " translate vim regular expression to perl regular expression. let search = substitute(search, '\(\\<\|\\>\)', '\\b', 'g') - call ack#Ack(a:cmd, '"' . search . '" ' . a:args) + call ack#Ack(a:cmd, '"' . search . '" ' . a:args, '', 0) endfunction "}}} -function! ack#AckHelp(cmd, args) "{{{ - let args = a:args . ' ' . s:GetDocLocations() - call ack#Ack(a:cmd, args) +function! ack#AckHelp(cmd, args, count) "{{{ + call ack#Ack(a:cmd, a:args, s:GetDocLocations(), a:count) endfunction "}}} -function! ack#AckWindow(cmd, args) "{{{ +function! ack#AckWindow(cmd, args, count) "{{{ let files = tabpagebuflist() " remove duplicated filenames (files appearing in more than one window) @@ -71,9 +82,8 @@ function! ack#AckWindow(cmd, args) "{{{ " expand to full path (avoid problems with cd/lcd in au QuickFixCmdPre) let files = map(files, "shellescape(fnamemodify(v:val, ':p'))") - let args = a:args . ' ' . join(files) - call ack#Ack(a:cmd, args) + call ack#Ack(a:cmd, a:args, join(files), a:count) endfunction "}}} function! ack#ShowResults() "{{{ @@ -201,6 +211,22 @@ function! s:SearchWithGrep(grepcmd, grepprg, grepargs, grepformat) "{{{ endtry endfunction "}}} +" The contents of the last visual selection +function! s:LastSelectedText() + let saved_cursor = getpos('.') + + let original_reg = getreg('z') + let original_reg_type = getregtype('z') + + normal! gv"zy + let text = @z + + call setreg('z', original_reg, original_reg_type) + call setpos('.', saved_cursor) + + return text +endfunction "}}} + " Are we finding matching files, not lines? (the -g option -- :AckFile) function! s:SearchingFilepaths() "{{{ return get(s:, 'searching_filepaths', 0) diff --git a/plugin/ack.vim b/plugin/ack.vim index da3ea9ab..e1ea9446 100644 --- a/plugin/ack.vim +++ b/plugin/ack.vim @@ -63,16 +63,16 @@ if !exists("g:ack_autofold_results") let g:ack_autofold_results = 0 endif -command! -bang -nargs=* -complete=file Ack call ack#Ack('grep', ) -command! -bang -nargs=* -complete=file AckAdd call ack#Ack('grepadd', ) -command! -bang -nargs=* -complete=file AckFromSearch call ack#AckFromSearch('grep', ) -command! -bang -nargs=* -complete=file LAck call ack#Ack('lgrep', ) -command! -bang -nargs=* -complete=file LAckAdd call ack#Ack('lgrepadd', ) -command! -bang -nargs=* -complete=file AckFile call ack#Ack('grep -g', ) -command! -bang -nargs=* -complete=help AckHelp call ack#AckHelp('grep', ) -command! -bang -nargs=* -complete=help LAckHelp call ack#AckHelp('lgrep', ) -command! -bang -nargs=* AckWindow call ack#AckWindow('grep', ) -command! -bang -nargs=* LAckWindow call ack#AckWindow('lgrep', ) +command! -bang -nargs=* -range=0 -complete=file Ack call ack#Ack('grep', , '', ) +command! -bang -nargs=* -range=0 -complete=file AckAdd call ack#Ack('grepadd', , '', ) +command! -bang -nargs=* -range=0 AckWindow call ack#AckWindow('grep', , ) +command! -bang -nargs=* -range=0 -complete=file LAck call ack#Ack('lgrep', , '', ) +command! -bang -nargs=* -range=0 -complete=file LAckAdd call ack#Ack('lgrepadd', , '', ) +command! -bang -nargs=* -range=0 LAckWindow call ack#AckWindow('lgrep', , ) +command! -bang -nargs=* -range=0 -complete=help AckHelp call ack#AckHelp('grep', , ) +command! -bang -nargs=* -range=0 -complete=help LAckHelp call ack#AckHelp('lgrep', , ) +command! -bang -nargs=* -range=0 -complete=file AckFile call ack#Ack('grep -g', , '', ) +command! -bang -nargs=* -complete=file AckFromSearch call ack#AckFromSearch('grep', ) let g:loaded_ack = 1