Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search by a visual mode selection #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions autoload/ack.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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("<cword>") : 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("<cword>")
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
Expand All @@ -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)
Expand All @@ -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() "{{{
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions plugin/ack.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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<bang>', <q-args>)
command! -bang -nargs=* -complete=file AckAdd call ack#Ack('grepadd<bang>', <q-args>)
command! -bang -nargs=* -complete=file AckFromSearch call ack#AckFromSearch('grep<bang>', <q-args>)
command! -bang -nargs=* -complete=file LAck call ack#Ack('lgrep<bang>', <q-args>)
command! -bang -nargs=* -complete=file LAckAdd call ack#Ack('lgrepadd<bang>', <q-args>)
command! -bang -nargs=* -complete=file AckFile call ack#Ack('grep<bang> -g', <q-args>)
command! -bang -nargs=* -complete=help AckHelp call ack#AckHelp('grep<bang>', <q-args>)
command! -bang -nargs=* -complete=help LAckHelp call ack#AckHelp('lgrep<bang>', <q-args>)
command! -bang -nargs=* AckWindow call ack#AckWindow('grep<bang>', <q-args>)
command! -bang -nargs=* LAckWindow call ack#AckWindow('lgrep<bang>', <q-args>)
command! -bang -nargs=* -range=0 -complete=file Ack call ack#Ack('grep<bang>', <q-args>, '', <count>)
command! -bang -nargs=* -range=0 -complete=file AckAdd call ack#Ack('grepadd<bang>', <q-args>, '', <count>)
command! -bang -nargs=* -range=0 AckWindow call ack#AckWindow('grep<bang>', <q-args>, <count>)
command! -bang -nargs=* -range=0 -complete=file LAck call ack#Ack('lgrep<bang>', <q-args>, '', <count>)
command! -bang -nargs=* -range=0 -complete=file LAckAdd call ack#Ack('lgrepadd<bang>', <q-args>, '', <count>)
command! -bang -nargs=* -range=0 LAckWindow call ack#AckWindow('lgrep<bang>', <q-args>, <count>)
command! -bang -nargs=* -range=0 -complete=help AckHelp call ack#AckHelp('grep<bang>', <q-args>, <count>)
command! -bang -nargs=* -range=0 -complete=help LAckHelp call ack#AckHelp('lgrep<bang>', <q-args>, <count>)
command! -bang -nargs=* -range=0 -complete=file AckFile call ack#Ack('grep<bang> -g', <q-args>, '', <count>)
command! -bang -nargs=* -complete=file AckFromSearch call ack#AckFromSearch('grep<bang>', <q-args>)

let g:loaded_ack = 1

Expand Down