Simplify ag/grep setup

Thu, 28 Oct 2021 21:46:26 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Thu, 28 Oct 2021 21:46:26 -0500
changeset 953
e033f9ce0d8c
parent 952
0d98622cf4d6
child 954
93e7838ac6a5

Simplify ag/grep setup

.vim/autoload/vimrc.vim file | annotate | diff | comparison | revisions
.vimrc file | annotate | diff | comparison | revisions
--- a/.vim/autoload/vimrc.vim
+++ b/.vim/autoload/vimrc.vim
@@ -35,27 +35,15 @@ func! vimrc#AutoSessionCheck() abort
   endif
 endfunc
 
-func! vimrc#Ag(args) abort
-  let orig_t_ti = &t_ti
-  let orig_t_te = &t_te
-  let orig_shellpipe = &shellpipe
-
-  set t_ti= t_te=
-  let &shellpipe = substitute(&shellpipe, '| tee', ' >', '')
-
-  let grepargs = a:args == '' ? expand('<cword>') : a:args . join(a:000, ' ')
+func! vimrc#Grep(...) abort
+  let pattern = get(a:000, 0, expand('<cword>'))
+  let cmd = join([&grepprg, shellescape(pattern)] + a:000[1:], ' ')
 
-  try
-    silent! execute "grep " . escape(grepargs, '|')
-    copen
-
-    let @/ = matchstr(a:args, "\\v(-)\@<!(\<)\@<=\\w+|['\"]\\zs.{-}\\ze['\"]")
-    call feedkeys(":let &hlsearch=1 \| echo \<CR>", 'n')
-  finally
-    let &t_ti = orig_t_ti
-    let &t_te = orig_t_te
-    let &shellpipe = orig_shellpipe
-  endtry
+  cgetexpr system(cmd)
+  call setqflist([], 'a', {"title": cmd})
+  let @/ = '\v' . pattern
+  copen
+  cfirst
 endfunc
 
 func! vimrc#Gcd() abort
--- a/.vimrc
+++ b/.vimrc
@@ -108,8 +108,8 @@ nnoremap <silent> <leader><leader>k :cal
 " mark line
 nmap <leader>l V<leader>k
 
-" use Ag for a recursive *
-nnoremap g* :Ag<CR>
+" use Grep for a recursive *
+nnoremap g* :Grep<CR>
 
 " K: doc, gKK: doc current filename
 nnoremap gKK :call ViewDoc('doc', expand('%:p'))<CR>
@@ -157,7 +157,9 @@ command! -nargs=* -complete=file Tig    
 command!                         TigBlame call tig#TigBlame()
 command!                         TigLog   call tig#Tig('log', '-p', '--', expand('%'))
 
-command! -nargs=* -complete=file -bar Ag call vimrc#Ag(<q-args>)
+command! -nargs=* -complete=file Grep call vimrc#Grep(<f-args>)
+CAlias Ag Grep
+CAlias grep Grep
 
 CAlias Q q
 CAlias Qa qa
@@ -263,6 +265,7 @@ endif
 if executable('ag')
   let &grepprg = "ag --vimgrep"
   set grepformat^=%f:%l:%c:%m,%f
+  set errorformat+=%f
 endif
 
 if g:on_windows

mercurial