From 9b5d417f1ca9fea7a29f044ead2a58000bf39251 Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Wed, 25 Sep 2019 15:27:43 +0200 Subject: [PATCH 01/19] fixed quickfix list parsing for file search --- autoload/ag.vim | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index a9567575..dee5dc23 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -51,16 +51,11 @@ function! ag#Ag(cmd, args) abort endif " Store the backups - let l:grepprg_bak = &grepprg - let l:grepprg_bak = &l:grepprg - let l:grepformat_bak = &grepformat let l:t_ti_bak = &t_ti let l:t_te_bak = &t_te " Try to change all the system variables and run ag in the right folder try - let &l:grepprg = g:ag_prg - let &grepformat = g:ag_format set t_ti= " These 2 commands make ag.vim not bleed in terminal set t_te= if g:ag_working_path_mode ==? 'r' " Try to find the project root for current buffer @@ -77,8 +72,6 @@ function! ag#Ag(cmd, args) abort call s:executeCmd(l:grepargs, l:cmd) endif finally - let &l:grepprg = l:grepprg_bak - let &grepformat = l:grepformat_bak let &t_ti = l:t_ti_bak let &t_te = l:t_te_bak endtry @@ -196,7 +189,11 @@ function! s:handleAsyncOutput(job_id, data, event) abort let l:expandeddata = [] " Expand the path of the result so we can jump to it for l:result in s:data - if( l:result !~? '^/home/' ) " Only expand when the path is not a full path already + " At the end we usually have some bogous/empty lines, so skip them + if( l:result =~ '^\s*$') + continue + endif + if( l:result !~? '^/' ) " Only expand when the path is not a full path already let l:result = s:cwd.'/'.l:result endif let l:result = substitute(l:result , '//', '/' ,'g') " Get rid of excess slashes in filename if present @@ -204,9 +201,8 @@ function! s:handleAsyncOutput(job_id, data, event) abort endfor if len(l:expandeddata) " Only if we actually find something - - " The last element is always bogus for some reason - let l:expandeddata = l:expandeddata[0:-2] + let l:errorformat_bak = &errorformat + let &errorformat = g:ag_format if s:locListCommand " Add to location list @@ -215,6 +211,7 @@ function! s:handleAsyncOutput(job_id, data, event) abort " Add to quickfix list cgete l:expandeddata endif + let &errorformat = l:errorformat_bak call s:handleOutput() else echom 'No matches for "'.s:args.'"' From 42fd7046353bfd6e677b609a9b7daa288a71239f Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Sun, 29 Mar 2020 18:22:49 +0200 Subject: [PATCH 02/19] changed ag cmd to use f-args and direct invocation --- autoload/ag.vim | 101 ++++++++++++++---------------------------------- plugin/ag.vim | 10 +---- 2 files changed, 32 insertions(+), 79 deletions(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index dee5dc23..85dd3675 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -2,7 +2,7 @@ " " Variables required to manage async let s:job_number = 0 -let s:locListCommand = 0 +let s:toLocList = 0 let s:args = '' let s:cwd = getcwd() let s:data = [] @@ -12,42 +12,26 @@ let s:resetData = 1 " Public API "----------------------------------------------------------------------------- -function! ag#Ag(cmd, args) abort - let l:ag_executable = get(split(g:ag_prg, ' '), 0) - - " Ensure that `ag` is installed - if !executable(l:ag_executable) - echoe "Ag command '" . l:ag_executable . "' was not found. Is the silver searcher installed and on your $PATH?" - return - endif - +function! ag#Ag(cmd, ...) abort + if empty(a:000) + let l:args = [expand('')] + els " If no pattern is provided, search for the word under the cursor - if empty(a:args) - let l:grepargs = expand('') - else - let l:grepargs = a:args . join(a:000, ' ') + let l:args = a:000 end - if empty(l:grepargs) - echo "Usage: ':Ag {pattern}'. See ':help :Ag' for more information." - return - endif - " Format, used to manage column jump - if a:args =~# '-g' - let s:ag_format_backup = g:ag_format - let g:ag_format = '%f' - elseif exists('s:ag_format_backup') - let g:ag_format = s:ag_format_backup + if index(l:args, '-g') >= 0 + let s:ag_current_format = '%f' + else + let s:ag_current_format = g:ag_format endif " Set the script variables that will later be used by the async callback - let s:args = l:grepargs - let l:cmd = a:cmd . ' ' . escape(l:grepargs, '|') - if l:cmd =~# '^l' - let s:locListCommand = 1 + if a:cmd =~# '^l' + let s:toLocList = 1 else - let s:locListCommand = 0 + let s:toLocList = 0 endif " Store the backups @@ -59,17 +43,10 @@ function! ag#Ag(cmd, args) abort set t_ti= " These 2 commands make ag.vim not bleed in terminal set t_te= if g:ag_working_path_mode ==? 'r' " Try to find the project root for current buffer - let l:cwd_back = getcwd() - let s:cwd = s:guessProjectRoot() - try - exe 'lcd '.s:cwd - catch - finally - call s:executeCmd(l:grepargs, l:cmd) - exe 'lcd '.l:cwd_back - endtry + let l:cwd = s:guessProjectRoot() + call s:execAg(l:args, { 'cwd': l:cwd}) else " Someone chose an undefined value or 'c' so we revert to searching in the cwd - call s:executeCmd(l:grepargs, l:cmd) + call s:execAg(l:args, {}) endif finally let &t_ti = l:t_ti_bak @@ -122,14 +99,14 @@ endfunction "----------------------------------------------------------------------------- function! s:handleOutput() abort - if s:locListCommand + if s:toLocList let l:match_count = len(getloclist(winnr())) else let l:match_count = len(getqflist()) endif if l:match_count - if s:locListCommand + if s:toLocList exe g:ag_lhandler let l:apply_mappings = g:ag_apply_lmappings let l:matches_window_prefix = 'l' " we're using the location list @@ -202,9 +179,9 @@ function! s:handleAsyncOutput(job_id, data, event) abort if len(l:expandeddata) " Only if we actually find something let l:errorformat_bak = &errorformat - let &errorformat = g:ag_format + let &errorformat = s:ag_current_format - if s:locListCommand + if s:toLocList " Add to location list lgete l:expandeddata else @@ -219,13 +196,7 @@ function! s:handleAsyncOutput(job_id, data, event) abort endif endfunction -function! s:executeCmd(grepargs, cmd) abort - if !has('nvim') - silent! execute a:cmd - return - endif - - " Stop older running ag jobs if any +function! s:execAg(args, opts) abort try call jobstop(s:job_number) catch @@ -237,30 +208,18 @@ function! s:executeCmd(grepargs, cmd) abort endif let s:resetData = 1 - " All types of exiting the job should be directed to handleAsyncOutput - let s:callbacks = { - \ 'on_stdout': function('s:handleAsyncOutput'), - \ 'on_stderr': function('s:handleAsyncOutput'), - \ 'on_exit': function('s:handleAsyncOutput') - \ } - - let l:splitargs = split(a:grepargs) - let l:grepargs = '' - "Make sure we shellescape arguments separately and expand the ~ in a string - for l:splitarg in l:splitargs - if l:splitarg !~? '^-' - let l:grepargs = l:grepargs.' '.shellescape(substitute(l:splitarg,'^\~',$HOME, '')) - else - let l:grepargs = l:grepargs.' '.l:splitarg - endif - endfor + let l:opts = { + \ 'on_stdout': function('s:handleAsyncOutput'), + \ 'on_stderr': function('s:handleAsyncOutput'), + \ 'on_exit': function('s:handleAsyncOutput') + \ } - " Construct the command string send to job shell - - " cd [directory]; ag --vimgrep [extra flags] '[value]' '[optional directory]' - let l:agcmd = 'cd '.s:cwd.'; '.g:ag_prg . ' ' . l:grepargs + let l:cmd = g:ag_prg + a:args + let s:args = join(a:args, " ") + echom join(l:cmd, " ") echom 'Ag search started' - let s:job_number = jobstart(['sh', '-c', l:agcmd], extend({'shell': 'shell 1'}, s:callbacks)) + let s:job_number = jobstart(l:cmd, extend(l:opts, a:opts)) endfunction diff --git a/plugin/ag.vim b/plugin/ag.vim index 1139c574..24771fbd 100644 --- a/plugin/ag.vim +++ b/plugin/ag.vim @@ -11,13 +11,7 @@ endif " Location of the ag utility if !exists('g:ag_prg') - " --vimgrep (consistent output we can parse) is available from version 0.25.0+ - if split(system('ag --version'), '[ \n\r\t]')[2] =~? '\d\+.\(2[5-9]\|[3-9][0-9]\)\(.\d\+\)\?' - let g:ag_prg = 'ag --vimgrep --silent' - else - " --noheading seems odd here, but see https://github.com/ggreer/the_silver_searcher/issues/361 - let g:ag_prg = 'ag --column --nogroup --noheading' - endif + let g:ag_prg = ['ag','--vimgrep','--silent'] endif if !exists('g:ag_format') @@ -52,7 +46,7 @@ if !exists('g:ag_working_path_mode') let g:ag_working_path_mode = 'c' endif -command! -bang -nargs=* -complete=file Ag call ag#Ag('grep',) +command! -bang -nargs=* -complete=file Ag call ag#Ag('grep',) command! -bang -nargs=* -complete=file AgBuffer call ag#AgBuffer('grep',) command! -bang -nargs=* -complete=file AgAdd call ag#AgAdd('grepadd', ) command! -bang -nargs=* -complete=file AgFromSearch call ag#AgFromSearch('grep', ) From d3684eaf40f10f9d81d8f17fe0a37e9676ce1ec8 Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Sun, 29 Mar 2020 22:59:56 +0200 Subject: [PATCH 03/19] fixed other functions to use f-args --- autoload/ag.vim | 42 +++++++++++++----------------------------- plugin/ag.vim | 15 ++++++--------- 2 files changed, 19 insertions(+), 38 deletions(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index 85dd3675..90e14dc6 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -5,7 +5,7 @@ let s:job_number = 0 let s:toLocList = 0 let s:args = '' let s:cwd = getcwd() -let s:data = [] +let s:data = [''] let s:resetData = 1 "----------------------------------------------------------------------------- @@ -52,15 +52,9 @@ function! ag#Ag(cmd, ...) abort let &t_ti = l:t_ti_bak let &t_te = l:t_te_bak endtry - - " No neovim, when we finally get here we already have the output so run handleOutput - if !has('nvim') - call s:handleOutput() - return - endif endfunction -function! ag#AgBuffer(cmd, args) abort +function! ag#AgBuffer(...) abort let l:bufs = filter(range(1, bufnr('$')), 'buflisted(v:val)') let l:files = [] for buf in l:bufs @@ -69,29 +63,19 @@ function! ag#AgBuffer(cmd, args) abort call add(l:files, l:file) endif endfor - call ag#Ag(a:cmd, a:args . ' ' . join(l:files, ' ')) -endfunction - -function! ag#AgFromSearch(cmd, args) abort - let l:search = getreg('/') - " translate vim regular expression to perl regular expression. - let l:search = substitute(l:search,'\(\\<\|\\>\)','\\b','g') - call ag#Ag(a:cmd, '"' . l:search .'" '. a:args) -endfunction - -function! ag#AgHelp(cmd,args) abort - let l:args = a:args.' '.s:GetDocLocations() - call ag#Ag(a:cmd,l:args) + + let l:args = a:000 + l:files + call call(function('ag#Ag'), l:args) endfunction -function! ag#AgFile(cmd, args) abort - let l:args = ' -g ' . a:args - call ag#Ag(a:cmd, l:args) +function! ag#AgFile(cmd, ...) abort + let l:args = [cmd, '-g'] + a:000 + call call(function('ag#Ag'), l:args) endfunction -function! ag#AgAdd(cmd, args) abort +function! ag#AgAdd(...) abort let s:resetData = 0 - call ag#Ag(a:cmd, a:args) + call call(function('ag#Ag'), a:000) endfunction "----------------------------------------------------------------------------- @@ -158,7 +142,8 @@ function! s:handleAsyncOutput(job_id, data, event) abort " Store all the input we get from the shell if a:event ==# 'stdout' - let s:data = s:data+a:data + let s:data[-1] .= a:data[0] + call extend(s:data, a:data[1:]) " When the program has finished running we parse the data elseif a:event ==# 'exit' @@ -204,7 +189,7 @@ function! s:execAg(args, opts) abort " Clear all of the old captures if s:resetData - let s:data = [] + let s:data = [''] endif let s:resetData = 1 @@ -217,7 +202,6 @@ function! s:execAg(args, opts) abort let l:cmd = g:ag_prg + a:args let s:args = join(a:args, " ") - echom join(l:cmd, " ") echom 'Ag search started' let s:job_number = jobstart(l:cmd, extend(l:opts, a:opts)) endfunction diff --git a/plugin/ag.vim b/plugin/ag.vim index 24771fbd..47bae544 100644 --- a/plugin/ag.vim +++ b/plugin/ag.vim @@ -47,14 +47,11 @@ if !exists('g:ag_working_path_mode') endif command! -bang -nargs=* -complete=file Ag call ag#Ag('grep',) -command! -bang -nargs=* -complete=file AgBuffer call ag#AgBuffer('grep',) -command! -bang -nargs=* -complete=file AgAdd call ag#AgAdd('grepadd', ) -command! -bang -nargs=* -complete=file AgFromSearch call ag#AgFromSearch('grep', ) -command! -bang -nargs=* -complete=file LAg call ag#Ag('lgrep', ) -command! -bang -nargs=* -complete=file LAgBuffer call ag#AgBuffer('lgrep',) -command! -bang -nargs=* -complete=file LAgAdd call ag#AgAdd('lgrepadd', ) -command! -bang -nargs=* -complete=file AgFile call ag#AgFile('grep', ) -command! -bang -nargs=* -complete=help AgHelp call ag#AgHelp('grep',) -command! -bang -nargs=* -complete=help LAgHelp call ag#AgHelp('lgrep',) +command! -bang -nargs=* -complete=file AgBuffer call ag#AgBuffer('grep',) +command! -bang -nargs=* -complete=file AgAdd call ag#AgAdd('grepadd', ) +command! -bang -nargs=* -complete=file LAg call ag#Ag('lgrep', ) +command! -bang -nargs=* -complete=file LAgBuffer call ag#AgBuffer('lgrep',) +command! -bang -nargs=* -complete=file LAgAdd call ag#AgAdd('lgrepadd', ) +command! -bang -nargs=* -complete=file AgFile call ag#AgFile('grep', ) let g:autoloaded_ag = 1 From 382591e19d7162829b10a040063bb8d4c1f54f4c Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Tue, 31 Mar 2020 09:27:07 +0200 Subject: [PATCH 04/19] fixed cwd bug --- autoload/ag.vim | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index 90e14dc6..18eed67e 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -43,10 +43,9 @@ function! ag#Ag(cmd, ...) abort set t_ti= " These 2 commands make ag.vim not bleed in terminal set t_te= if g:ag_working_path_mode ==? 'r' " Try to find the project root for current buffer - let l:cwd = s:guessProjectRoot() - call s:execAg(l:args, { 'cwd': l:cwd}) + call s:execAg(l:args, { 'cwd': s:guessProjectRoot() }) else " Someone chose an undefined value or 'c' so we revert to searching in the cwd - call s:execAg(l:args, {}) + call s:execAg(l:args, {'cwd': getcwd() }) endif finally let &t_ti = l:t_ti_bak From b199252332d04dd33692be29bfaac23cd6e710ff Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Fri, 3 Apr 2020 19:22:10 +0200 Subject: [PATCH 05/19] use self instead of s: --- autoload/ag.vim | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index 18eed67e..aff76828 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -133,7 +133,7 @@ function! s:handleOutput() abort endif endfunction -function! s:handleAsyncOutput(job_id, data, event) abort +function! s:handleAsyncOutput(job_id, data, event) abort dict " Don't care about older async calls that have been killed or replaced if s:job_number !=# a:job_id return @@ -155,7 +155,7 @@ function! s:handleAsyncOutput(job_id, data, event) abort continue endif if( l:result !~? '^/' ) " Only expand when the path is not a full path already - let l:result = s:cwd.'/'.l:result + let l:result = self.cwd.'/'.l:result endif let l:result = substitute(l:result , '//', '/' ,'g') " Get rid of excess slashes in filename if present call add(l:expandeddata, l:result) @@ -206,17 +206,6 @@ function! s:execAg(args, opts) abort endfunction -function! s:GetDocLocations() abort - let dp = '' - for p in split(&runtimepath,',') - let p = p.'doc/' - if isdirectory(p) - let dp = p.'*.txt '.dp - endif - endfor - return dp -endfunction - " Called from within a list window, preserves its height after shuffling vsplit. " The parameter indicates whether list was opened as copen or lopen. function! s:PreviewVertical(opencmd) abort From 34816ee448a4690684351c4789606eab653a465c Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Sat, 4 Apr 2020 00:25:26 +0200 Subject: [PATCH 06/19] allow cwd to be passed as arg --- autoload/ag.vim | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index aff76828..ae0d88f9 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -12,12 +12,12 @@ let s:resetData = 1 " Public API "----------------------------------------------------------------------------- -function! ag#Ag(cmd, ...) abort - if empty(a:000) +function! ag#run(cmd, args, cwd) abort + if empty(a:args) let l:args = [expand('')] els " If no pattern is provided, search for the word under the cursor - let l:args = a:000 + let l:args = a:args end " Format, used to manage column jump @@ -42,17 +42,23 @@ function! ag#Ag(cmd, ...) abort try set t_ti= " These 2 commands make ag.vim not bleed in terminal set t_te= - if g:ag_working_path_mode ==? 'r' " Try to find the project root for current buffer - call s:execAg(l:args, { 'cwd': s:guessProjectRoot() }) - else " Someone chose an undefined value or 'c' so we revert to searching in the cwd - call s:execAg(l:args, {'cwd': getcwd() }) - endif + call s:execAg(l:args, { 'cwd': a:cwd }) finally let &t_ti = l:t_ti_bak let &t_te = l:t_te_bak endtry endfunction +function! ag#Ag(cmd, ...) abort + let l:args = a:000 + if g:ag_working_path_mode ==? 'r' " Try to find the project root for current buffer + let l:cwd = s:guessProjectRoot() + else " Someone chose an undefined value or 'c' so we revert to searching in the cwd + let l:cwd = getcwd() + endif + call ag#run(a:cmd, l:args, l:cwd) +endf + function! ag#AgBuffer(...) abort let l:bufs = filter(range(1, bufnr('$')), 'buflisted(v:val)') let l:files = [] From d358c550d410b7bbf7f54f3000b0762a6b0cf897 Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Mon, 15 Jun 2020 22:55:54 +0200 Subject: [PATCH 07/19] use rg for some searches --- autoload/ag.vim | 4 ++++ plugin/ag.vim | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index ae0d88f9..c4d43cad 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -74,8 +74,12 @@ function! ag#AgBuffer(...) abort endfunction function! ag#AgFile(cmd, ...) abort + + let l:ag_prg_prev = g:ag_prg + let g:ag_prg = ['ag','--vimgrep','--silent'] let l:args = [cmd, '-g'] + a:000 call call(function('ag#Ag'), l:args) + let g:ag_prg = l:ag_prg_prev endfunction function! ag#AgAdd(...) abort diff --git a/plugin/ag.vim b/plugin/ag.vim index 47bae544..d62e778b 100644 --- a/plugin/ag.vim +++ b/plugin/ag.vim @@ -11,7 +11,7 @@ endif " Location of the ag utility if !exists('g:ag_prg') - let g:ag_prg = ['ag','--vimgrep','--silent'] + let g:ag_prg = ['rg','-S', '--vimgrep'] endif if !exists('g:ag_format') From 0a2a7df5a41da4366104eaebfabaf7ed3bad39d1 Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Tue, 15 Sep 2020 10:41:07 +0200 Subject: [PATCH 08/19] added follow symlinks switch --- plugin/ag.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/ag.vim b/plugin/ag.vim index d62e778b..50d3a592 100644 --- a/plugin/ag.vim +++ b/plugin/ag.vim @@ -11,7 +11,7 @@ endif " Location of the ag utility if !exists('g:ag_prg') - let g:ag_prg = ['rg','-S', '--vimgrep'] + let g:ag_prg = ['rg','--follow', '--smart-case', '--vimgrep'] endif if !exists('g:ag_format') From 6d3d23b6ab3dfd2e5b0cff70085e432273606c5a Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Wed, 23 Jun 2021 00:02:12 +0200 Subject: [PATCH 09/19] updated rg invocations --- autoload/ag.vim | 7 +------ plugin/ag.vim | 4 ++-- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index c4d43cad..865ddf25 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -20,12 +20,7 @@ function! ag#run(cmd, args, cwd) abort let l:args = a:args end - " Format, used to manage column jump - if index(l:args, '-g') >= 0 - let s:ag_current_format = '%f' - else - let s:ag_current_format = g:ag_format - endif + let s:ag_current_format = g:ag_format " Set the script variables that will later be used by the async callback if a:cmd =~# '^l' diff --git a/plugin/ag.vim b/plugin/ag.vim index 50d3a592..70ccd5d2 100644 --- a/plugin/ag.vim +++ b/plugin/ag.vim @@ -4,8 +4,8 @@ if exists('g:autoloaded_ag') finish endif -if !executable('ag') - echoe "Ag command was not found. Is the silver searcher installed and on your $PATH?" +if !executable('rg') + echoe "Ag command was not found. Is ripgrep installed?" finish endif From b525d40bb859b1ab195937adfe9b4c619b559e68 Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Fri, 24 Sep 2021 16:58:21 +0200 Subject: [PATCH 10/19] . --- autoload/ag.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index 865ddf25..9f7cd2a5 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -72,7 +72,7 @@ function! ag#AgFile(cmd, ...) abort let l:ag_prg_prev = g:ag_prg let g:ag_prg = ['ag','--vimgrep','--silent'] - let l:args = [cmd, '-g'] + a:000 + let l:args = [a:cmd, '-g'] + a:000 call call(function('ag#Ag'), l:args) let g:ag_prg = l:ag_prg_prev endfunction From c762512729af00a9c4fb2c38730663a42d728b35 Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Fri, 24 Sep 2021 17:35:05 +0200 Subject: [PATCH 11/19] . --- autoload/ag.vim | 22 ++++++++++------------ plugin/ag.vim | 11 ++++++++++- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index 9f7cd2a5..43691609 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -21,6 +21,7 @@ function! ag#run(cmd, args, cwd) abort end let s:ag_current_format = g:ag_format + let s:ag_current_prg = g:ag_prg " Set the script variables that will later be used by the async callback if a:cmd =~# '^l' @@ -29,6 +30,11 @@ function! ag#run(cmd, args, cwd) abort let s:toLocList = 0 endif + if a:cmd =~# 'find' + let s:ag_current_format = g:fd_format + let s:ag_current_prg = g:fd_prg + endif + " Store the backups let l:t_ti_bak = &t_ti let l:t_te_bak = &t_te @@ -37,7 +43,7 @@ function! ag#run(cmd, args, cwd) abort try set t_ti= " These 2 commands make ag.vim not bleed in terminal set t_te= - call s:execAg(l:args, { 'cwd': a:cwd }) + call s:execAg(s:ag_current_prg, l:args, { 'cwd': a:cwd }) finally let &t_ti = l:t_ti_bak let &t_te = l:t_te_bak @@ -68,15 +74,6 @@ function! ag#AgBuffer(...) abort call call(function('ag#Ag'), l:args) endfunction -function! ag#AgFile(cmd, ...) abort - - let l:ag_prg_prev = g:ag_prg - let g:ag_prg = ['ag','--vimgrep','--silent'] - let l:args = [a:cmd, '-g'] + a:000 - call call(function('ag#Ag'), l:args) - let g:ag_prg = l:ag_prg_prev -endfunction - function! ag#AgAdd(...) abort let s:resetData = 0 call call(function('ag#Ag'), a:000) @@ -185,7 +182,7 @@ function! s:handleAsyncOutput(job_id, data, event) abort dict endif endfunction -function! s:execAg(args, opts) abort +function! s:execAg(prg, args, opts) abort try call jobstop(s:job_number) catch @@ -203,7 +200,8 @@ function! s:execAg(args, opts) abort \ 'on_exit': function('s:handleAsyncOutput') \ } - let l:cmd = g:ag_prg + a:args + let l:cmd = a:prg + a:args + echom l:cmd let s:args = join(a:args, " ") echom 'Ag search started' diff --git a/plugin/ag.vim b/plugin/ag.vim index 70ccd5d2..f4d429a4 100644 --- a/plugin/ag.vim +++ b/plugin/ag.vim @@ -14,10 +14,19 @@ if !exists('g:ag_prg') let g:ag_prg = ['rg','--follow', '--smart-case', '--vimgrep'] endif +if !exists('g:fd_prg') + let g:fd_prg = ['fd','--follow'] +endif + +if !exists('g:fd_format') + let g:fd_format = '%f' +endif + if !exists('g:ag_format') let g:ag_format = '%f:%l:%c:%m' endif + if !exists('g:ag_apply_qmappings') let g:ag_apply_qmappings = 1 endif @@ -52,6 +61,6 @@ command! -bang -nargs=* -complete=file AgAdd call ag#AgAdd('grepadd', ', ) command! -bang -nargs=* -complete=file LAgBuffer call ag#AgBuffer('lgrep',) command! -bang -nargs=* -complete=file LAgAdd call ag#AgAdd('lgrepadd', ) -command! -bang -nargs=* -complete=file AgFile call ag#AgFile('grep', ) +command! -bang -nargs=* -complete=file Fd call ag#Ag('find', ) let g:autoloaded_ag = 1 From 0320eb0f9f8d2217b8bdb71a277b4dad86d18f8b Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Fri, 1 Oct 2021 18:14:24 +0200 Subject: [PATCH 12/19] remove echo stmt --- autoload/ag.vim | 1 - 1 file changed, 1 deletion(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index 43691609..4e5454b8 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -201,7 +201,6 @@ function! s:execAg(prg, args, opts) abort \ } let l:cmd = a:prg + a:args - echom l:cmd let s:args = join(a:args, " ") echom 'Ag search started' From 7d3f5ba5287b72bc7cedd679ec4f9fb873cdac83 Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Mon, 4 Oct 2021 20:24:53 +0200 Subject: [PATCH 13/19] . --- autoload/ag.vim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index 4e5454b8..3d22623d 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -110,11 +110,11 @@ function! s:handleOutput() abort redraw! " Regular vim needs some1 to tell it to redraw if l:apply_mappings - nnoremap h :exe 'wincmd ' (&splitbelow ? 'J' : 'K')pJp - nnoremap H :exe 'wincmd ' (&splitbelow ? 'J' : 'K')pJ - nnoremap o + nnoremap f :exe 'wincmd ' (&splitbelow ? 'J' : 'K')pJp + " nnoremap H :exe 'wincmd ' (&splitbelow ? 'J' : 'K')pJ + " nnoremap o nnoremap t T - nnoremap T TgT + " nnoremap T TgT nnoremap v :exe 'wincmd ' (&splitright ? 'L' : 'H')pJp let l:closecmd = l:matches_window_prefix . 'close' @@ -200,7 +200,7 @@ function! s:execAg(prg, args, opts) abort \ 'on_exit': function('s:handleAsyncOutput') \ } - let l:cmd = a:prg + a:args + let l:cmd = a:prg + a:args + [ "./" ] let s:args = join(a:args, " ") echom 'Ag search started' From a21e7f90e0ee6e00b2432307b96f444799c66ecf Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Fri, 8 Oct 2021 15:24:31 +0200 Subject: [PATCH 14/19] agbuffer --- autoload/ag.vim | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index 3d22623d..b9882d44 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -70,7 +70,7 @@ function! ag#AgBuffer(...) abort endif endfor - let l:args = a:000 + l:files + let l:args = a:000 + ["--"] + l:files call call(function('ag#Ag'), l:args) endfunction @@ -200,8 +200,16 @@ function! s:execAg(prg, args, opts) abort \ 'on_exit': function('s:handleAsyncOutput') \ } - let l:cmd = a:prg + a:args + [ "./" ] - let s:args = join(a:args, " ") + + let l:args = copy(a:args) + let l:idx = index(l:args, "--") + if l:idx >= 0 + call remove(l:args, l:idx) + else + call add(l:args, "./") + endif + let l:cmd = a:prg + l:args + let s:args = join(l:args, " ") echom 'Ag search started' let s:job_number = jobstart(l:cmd, extend(l:opts, a:opts)) From b8fa673f7f1fc0df29c573730d8a88476ede7ad7 Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Wed, 13 Oct 2021 13:29:26 +0200 Subject: [PATCH 15/19] as_args --- lua/agvim/init.lua | 45 +++++++++++++++++++++++++++++++++++++++++++++ plugin/ag.vim | 1 + 2 files changed, 46 insertions(+) create mode 100644 lua/agvim/init.lua diff --git a/lua/agvim/init.lua b/lua/agvim/init.lua new file mode 100644 index 00000000..59c171bc --- /dev/null +++ b/lua/agvim/init.lua @@ -0,0 +1,45 @@ +-- > package.loaded[ 'agvim' ] = nil +-- > x = require("agvim") +-- > x.get_file("abc|") +-- abc| +-- lua agvim = require("agvim") +-- call v:lua.agvim.as_args() + +local function get_fname(entry) + local idx = string.find(entry, "|") + if idx == nil then + return nil + end + return string.sub(entry, 1, idx-1) +end + +local function extract_files(lines) + local seen = {} + local files = {} + local n = 1 + for _, v in ipairs(lines) do + local fn = get_fname(v) + if fn and not seen[fn] then + seen[fn] = true + files[n] = fn + n = n+1 + end + end + return files +end + +local function as_args() + local bufnr = vim.api.nvim_get_current_buf() + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) + local files = extract_files(lines) + + for i,v in ipairs(files) do + files[i] = vim.api.nvim_call_function("fnameescape",{v}) + end + vim.api.nvim_command("close") + vim.api.nvim_command("args " .. table.concat(files, " ")) +end + +return { + as_args = as_args +} diff --git a/plugin/ag.vim b/plugin/ag.vim index f4d429a4..97b9f271 100644 --- a/plugin/ag.vim +++ b/plugin/ag.vim @@ -62,5 +62,6 @@ command! -bang -nargs=* -complete=file LAg call ag#Ag('lgrep', ) command! -bang -nargs=* -complete=file LAgBuffer call ag#AgBuffer('lgrep',) command! -bang -nargs=* -complete=file LAgAdd call ag#AgAdd('lgrepadd', ) command! -bang -nargs=* -complete=file Fd call ag#Ag('find', ) +command AgAsArgs lua require("agvim").as_args() let g:autoloaded_ag = 1 From 9223ce47b040811ad8f29f9374e50600f4d11c33 Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Wed, 13 Oct 2021 13:30:52 +0200 Subject: [PATCH 16/19] ag -> fdasargs --- plugin/ag.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/ag.vim b/plugin/ag.vim index 97b9f271..bb19ba35 100644 --- a/plugin/ag.vim +++ b/plugin/ag.vim @@ -62,6 +62,6 @@ command! -bang -nargs=* -complete=file LAg call ag#Ag('lgrep', ) command! -bang -nargs=* -complete=file LAgBuffer call ag#AgBuffer('lgrep',) command! -bang -nargs=* -complete=file LAgAdd call ag#AgAdd('lgrepadd', ) command! -bang -nargs=* -complete=file Fd call ag#Ag('find', ) -command AgAsArgs lua require("agvim").as_args() +command FdAsArgs lua require("agvim").as_args() let g:autoloaded_ag = 1 From 112b3dbb9fb43585f75416f0a70f2b9456eb4c19 Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Mon, 16 May 2022 09:32:36 +0200 Subject: [PATCH 17/19] added fdabs cmd --- autoload/ag.vim | 8 ++++++++ plugin/ag.vim | 1 + 2 files changed, 9 insertions(+) diff --git a/autoload/ag.vim b/autoload/ag.vim index b9882d44..92361b52 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -35,6 +35,14 @@ function! ag#run(cmd, args, cwd) abort let s:ag_current_prg = g:fd_prg endif + if a:cmd =~# 'findfull' + let s:ag_current_prg = s:ag_current_prg + ['--full-path'] + endif + + if a:cmd =~# '!$' + let s:ag_current_prg = s:ag_current_prg + ['--hidden'] + endif + " Store the backups let l:t_ti_bak = &t_ti let l:t_te_bak = &t_te diff --git a/plugin/ag.vim b/plugin/ag.vim index bb19ba35..4cc49bb4 100644 --- a/plugin/ag.vim +++ b/plugin/ag.vim @@ -62,6 +62,7 @@ command! -bang -nargs=* -complete=file LAg call ag#Ag('lgrep', ) command! -bang -nargs=* -complete=file LAgBuffer call ag#AgBuffer('lgrep',) command! -bang -nargs=* -complete=file LAgAdd call ag#AgAdd('lgrepadd', ) command! -bang -nargs=* -complete=file Fd call ag#Ag('find', ) +command! -bang -nargs=* -complete=file Fdabs call ag#Ag('findfull', ) command FdAsArgs lua require("agvim").as_args() let g:autoloaded_ag = 1 From fa5324e2737a906b46e40c9824fd8bf6ea177443 Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Mon, 16 May 2022 09:34:13 +0200 Subject: [PATCH 18/19] log search cmd --- autoload/ag.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index 92361b52..c73b9eb3 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -219,7 +219,7 @@ function! s:execAg(prg, args, opts) abort let l:cmd = a:prg + l:args let s:args = join(l:args, " ") - echom 'Ag search started' + echom 'Ag search started (' . join(l:cmd, " ") . ')' let s:job_number = jobstart(l:cmd, extend(l:opts, a:opts)) endfunction From 31dfb289c780010e995572c6dc2145550b79e420 Mon Sep 17 00:00:00 2001 From: Joachim Bargsten Date: Wed, 10 Jul 2024 16:32:27 +0200 Subject: [PATCH 19/19] . --- autoload/ag.vim | 6 ++++++ plugin/ag.vim | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/autoload/ag.vim b/autoload/ag.vim index c73b9eb3..37be3dfc 100644 --- a/autoload/ag.vim +++ b/autoload/ag.vim @@ -39,10 +39,16 @@ function! ag#run(cmd, args, cwd) abort let s:ag_current_prg = s:ag_current_prg + ['--full-path'] endif + if a:cmd =~ 'glob' + let s:ag_current_prg = s:ag_current_prg + ['--glob=' .. l:args[0]] + let l:args = l:args[1:] + endif + if a:cmd =~# '!$' let s:ag_current_prg = s:ag_current_prg + ['--hidden'] endif + " Store the backups let l:t_ti_bak = &t_ti let l:t_te_bak = &t_te diff --git a/plugin/ag.vim b/plugin/ag.vim index 4cc49bb4..d6075bd2 100644 --- a/plugin/ag.vim +++ b/plugin/ag.vim @@ -5,7 +5,7 @@ if exists('g:autoloaded_ag') endif if !executable('rg') - echoe "Ag command was not found. Is ripgrep installed?" + echoe "rg command was not found. Is ripgrep installed?" finish endif @@ -56,6 +56,7 @@ if !exists('g:ag_working_path_mode') endif command! -bang -nargs=* -complete=file Ag call ag#Ag('grep',) +command! -bang -nargs=* -complete=file Af call ag#Ag('grepglob',) command! -bang -nargs=* -complete=file AgBuffer call ag#AgBuffer('grep',) command! -bang -nargs=* -complete=file AgAdd call ag#AgAdd('grepadd', ) command! -bang -nargs=* -complete=file LAg call ag#Ag('lgrep', )