Skip to content

Commit 0cee7a3

Browse files
patrickwhite256pwhite-cl
authored andcommitted
Adding option to choose default result window
- Adding `g:ag_default_window_type` to choose to use the quickfix or location list - Adding `:LAg...` variants of commands that did not already have them - Adding `:QAg...` variants of all commands Previously, it was difficult to use the location list by default, since something like `command -nargs=* Ag LAg <args>` in the vimrc would get overridden by the `command!` in the plugin. In my opinion, the variants should eventually be removed, since I don't really see a reason why someone would want to use one list sometimes but the other sometimes. If people agree with me: - The `:LAg...` variants should be put on a deprecation path - The `:QAg...` variants should just be removed now
1 parent 994c27d commit 0cee7a3

File tree

3 files changed

+108
-38
lines changed

3 files changed

+108
-38
lines changed

autoload/ag.vim

+38-15
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ if !exists("g:ag_working_path_mode")
6060
let g:ag_working_path_mode = 'c'
6161
endif
6262

63-
function! ag#AgBuffer(cmd, args)
63+
if !exists("g:ag_default_window_type")
64+
let g:ag_default_window_type = 'c'
65+
endif
66+
67+
function! ag#AgBuffer(cmd, args, ...)
6468
let l:bufs = filter(range(1, bufnr('$')), 'buflisted(v:val)')
6569
let l:files = []
6670
for buf in l:bufs
@@ -69,10 +73,10 @@ function! ag#AgBuffer(cmd, args)
6973
call add(l:files, l:file)
7074
endif
7175
endfor
72-
call ag#Ag(a:cmd, a:args . ' ' . join(l:files, ' '))
76+
call ag#Ag(a:cmd, a:args . ' ' . join(l:files, ' '), a:000)
7377
endfunction
7478

75-
function! ag#Ag(cmd, args)
79+
function! ag#Ag(cmd, args, ...)
7680
let l:ag_executable = get(split(g:ag_prg, " "), 0)
7781

7882
" Ensure that `ag` is installed
@@ -85,7 +89,7 @@ function! ag#Ag(cmd, args)
8589
if empty(a:args)
8690
let l:grepargs = expand("<cword>")
8791
else
88-
let l:grepargs = a:args . join(a:000, ' ')
92+
let l:grepargs = a:args
8993
end
9094

9195
if empty(l:grepargs)
@@ -95,14 +99,35 @@ function! ag#Ag(cmd, args)
9599

96100
" Format, used to manage column jump
97101
if a:cmd =~# '-g$'
98-
let s:ag_format_backup=g:ag_format
102+
if exists("g:ag_format")
103+
let s:ag_format_backup=g:ag_format
104+
endif
99105
let g:ag_format="%f"
100106
elseif exists("s:ag_format_backup")
101107
let g:ag_format=s:ag_format_backup
102108
elseif !exists("g:ag_format")
103109
let g:ag_format="%f:%l:%c:%m"
104110
endif
105111

112+
if a:0 == 1
113+
" Because a:000 is passed in from wrapper functions, check for that case
114+
if type(a:1) == type([]) && len(a:1) > 0
115+
let l:matches_window_prefix = a:1[0]
116+
elseif type(a:1) == type('')
117+
let l:matches_window_prefix = a:1
118+
else
119+
let l:matches_window_prefix = g:ag_default_window_type
120+
end
121+
else
122+
let l:matches_window_prefix = g:ag_default_window_type
123+
endif
124+
125+
if l:matches_window_prefix == 'l'
126+
let l:cmd = 'l' . a:cmd
127+
else
128+
let l:cmd = a:cmd
129+
endif
130+
106131
let l:grepprg_bak=&grepprg
107132
let l:grepformat_bak=&grepformat
108133
let l:t_ti_bak=&t_ti
@@ -120,11 +145,11 @@ function! ag#Ag(cmd, args)
120145
catch
121146
echom 'Failed to change directory to:'.l:cwd
122147
finally
123-
silent! execute a:cmd . " " . escape(l:grepargs, '|')
148+
silent! execute l:cmd . " " . escape(l:grepargs, '|')
124149
exe "lcd ".l:cwd_back
125150
endtry
126151
else " Someone chose an undefined value or 'c' so we revert to the default
127-
silent! execute a:cmd . " " . escape(l:grepargs, '|')
152+
silent! execute l:cmd . " " . escape(l:grepargs, '|')
128153
endif
129154
finally
130155
let &grepprg=l:grepprg_bak
@@ -133,20 +158,18 @@ function! ag#Ag(cmd, args)
133158
let &t_te=l:t_te_bak
134159
endtry
135160

136-
if a:cmd =~# '^l'
161+
if l:matches_window_prefix == 'l'
137162
let l:match_count = len(getloclist(winnr()))
138163
else
139164
let l:match_count = len(getqflist())
140165
endif
141166

142-
if a:cmd =~# '^l' && l:match_count
167+
if l:matches_window_prefix && l:match_count
143168
exe g:ag_lhandler
144169
let l:apply_mappings = g:ag_apply_lmappings
145-
let l:matches_window_prefix = 'l' " we're using the location list
146170
elseif l:match_count
147171
exe g:ag_qhandler
148172
let l:apply_mappings = g:ag_apply_qmappings
149-
let l:matches_window_prefix = 'c' " we're using the quickfix window
150173
endif
151174

152175
" If highlighting is on, highlight the search keyword.
@@ -192,11 +215,11 @@ function! ag#Ag(cmd, args)
192215
endif
193216
endfunction
194217

195-
function! ag#AgFromSearch(cmd, args)
218+
function! ag#AgFromSearch(cmd, args, ...)
196219
let search = getreg('/')
197220
" translate vim regular expression to perl regular expression.
198221
let search = substitute(search,'\(\\<\|\\>\)','\\b','g')
199-
call ag#Ag(a:cmd, '"' . search .'" '. a:args)
222+
call ag#Ag(a:cmd, '"' . search .'" '. a:args, a:000)
200223
endfunction
201224

202225
function! ag#GetDocLocations()
@@ -210,9 +233,9 @@ function! ag#GetDocLocations()
210233
return dp
211234
endfunction
212235

213-
function! ag#AgHelp(cmd,args)
236+
function! ag#AgHelp(cmd,args,...)
214237
let args = a:args.' '.ag#GetDocLocations()
215-
call ag#Ag(a:cmd,args)
238+
call ag#Ag(a:cmd,args,a:000)
216239
endfunction
217240

218241
function! s:guessProjectRoot()

doc/ag.txt

+57-18
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,91 @@ the results in a split window.
1111

1212
Search recursively in {directory} (which defaults to the current
1313
directory) for the {pattern}. Behaves just like the |:grep| command, but
14-
will open the |Quickfix| window for you. If [!] is not given the first
15-
error is jumped to.
14+
will open the result window for you. If [!] is not given the first
15+
error is jumped to. By default, the results are displayed in the
16+
|quickfix| list. This can be customized with the |g:ag_default_window_type|
17+
option.
18+
19+
:LAg [options] {pattern} [{directory}] *:LAg*
20+
21+
Just like |:Ag| but matches are always placed in the |location-list|.
22+
23+
:QAg [options] {pattern} [{directory}] *:QAg*
24+
25+
Just like |:Ag| but matches are always placed in the |quickfix| list.
1626

1727
:AgBuffer[!] [options] {pattern} *:AgBuffer*
1828

1929
Search for {pattern} in all open buffers. Behaves just like the |:grep|
20-
command, but will open the |Quickfix| window for you. If [!] is not given
30+
command, but will open the result window for you. If [!] is not given
2131
the first error is jumped to.
2232

2333
Note: this will not find changes in modified buffers, since ag can only
2434
find what is on disk! You can save buffers automatically when searching
2535
with the 'autowrite' option. A buffer will be ignored if it is a directory
2636
(an explorer, like netrw).
2737

38+
:LAgBuffer[!] [options] {pattern} *:LAgBuffer*
39+
40+
Just like |:AgBuffer| but matches are always placed in the |location-list|.
41+
42+
:QAgBuffer[!] [options] {pattern} *:QAgBuffer*
43+
44+
Just like |:AgBuffer| but matches are always placed in the |quickfix| list.
45+
2846
:AgAdd [options] {pattern} [{directory}] *:AgAdd*
2947

3048
Just like |:Ag|, but instead of making a new list, the matches are
31-
appended to the current |quickfix| list.
49+
appended to the current result list.
3250

33-
:AgFromSearch [{directory}] *:AgFromSearch*
51+
:LAgAdd [options] {pattern} [{directory}] *:LAgAdd*
3452

35-
Just like |:Ag| but the pattern is from previous search.
53+
Just like |:AgAdd| but matches are always placed in the |location-list|.
3654

37-
:LAg [options] {pattern} [{directory}] *:LAg*
55+
:QAgAdd [options] {pattern} [{directory}] *:QAgAdd*
3856

39-
Just like |:Ag| but instead of the |quickfix| list, matches are placed in
40-
the current |location-list|.
57+
Just like |:AgAdd| but matches are always placed in the |quickfix| list.
4158

42-
:LAgBuffer [options] {pattern} *:LAgBuffer*
59+
:AgFromSearch [{directory}] *:AgFromSearch*
60+
61+
Just like |:Ag| but the pattern is from previous search.
4362

44-
Just like |:AgBuffer| but instead of the |quickfix| list, matches are
45-
placed in the current |location-list|.
63+
:LAgFromSearch [{directory}] *:LAgFromSearch*
4664

47-
:LAgAdd [options] {pattern} [{directory}] *:LAgAdd*
65+
Just like |:AgFromSearch| but matches are always placed in the
66+
|location-list|.
4867

49-
Just like |:AgAdd| but instead of the |quickfix| list, matches are added
50-
to the current |location-list|
68+
:QAgFromSearch [{directory}] *:QAgFromSearch*
69+
70+
Just like |:AgFromSearch| but matches are always placed in the |quickfix|
71+
list.
5172

5273
:AgFile [options] {pattern} [{directory}] *:AgFile*
5374

5475
Search recursively in {directory} (which defaults to the current
5576
directory) for filenames matching the {pattern}. Behaves just like the
5677
|:grep| command, but will open the |Quickfix| window for you.
5778

79+
:LAgFile [options] {pattern} [{directory}] *:LAgFile*
80+
81+
Just like |:AgFile| but matches are always placed in the |location-list|.
82+
83+
:QAgFile [options] {pattern} [{directory}] *:QAgFile*
84+
85+
Just like |:AgFile| but matches are always placed in the |quickfix| list.
86+
5887
:AgHelp[!] [options] {pattern} *:AgHelp*
5988

6089
Search vim documentation files for the {pattern}. Behaves just like the
6190
|:Ag| command, but searches only vim documentation .txt files
6291

63-
:LAgHelp [options] {pattern} *:LAgHelp*
92+
:LAgHelp[!] [options] {pattern} *:LAgHelp*
93+
94+
Just like |:AgHelp| but matches are always placed in the |location-list|.
6495

65-
Just like |:AgHelp| but instead of the |quickfix| list, matches are placed
66-
in the current |location-list|.
96+
:QAgHelp[!] [options] {pattern} *:QAgHelp*
97+
98+
Just like |:AgHelp| but matches are always placed in the |quickfix| list.
6799

68100
Files containing the search term will be listed in the split window, along
69101
with the line number of the occurrence, once for each occurrence. <Enter> on a
@@ -145,6 +177,13 @@ the mappings are not applied (see |g:ag_apply_qmappings| and
145177
|g:ag_apply_lmappings| for more info. Default 1. Example: >
146178
let g:ag_mapping_message=0
147179
<
180+
*g:ag_default_window_type*
181+
Specifies the window type to open with the results. Options are 'c' for
182+
the |quickfix| list and "l" for the |location-list|. Both are always
183+
accessible by using |:QAg| and |:LAg| respectively. Default "c". Example: >
184+
let g:ag_default_window_type="l"
185+
<
186+
148187

149188
==============================================================================
150189
MAPPINGS *ag-mappings*

plugin/ag.vim

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
" NOTE: You must, of course, install ag / the_silver_searcher
22
command! -bang -nargs=* -complete=file Ag call ag#Ag('grep<bang>',<q-args>)
3-
command! -bang -nargs=* -complete=file AgBuffer call ag#AgBuffer('grep<bang>',<q-args>)
43
command! -bang -nargs=* -complete=file AgAdd call ag#Ag('grepadd<bang>', <q-args>)
4+
command! -bang -nargs=* -complete=file AgBuffer call ag#AgBuffer('grep<bang>',<q-args>)
55
command! -bang -nargs=* -complete=file AgFromSearch call ag#AgFromSearch('grep<bang>', <q-args>)
6-
command! -bang -nargs=* -complete=file LAg call ag#Ag('lgrep<bang>', <q-args>)
7-
command! -bang -nargs=* -complete=file LAgBuffer call ag#AgBuffer('lgrep<bang>',<q-args>)
8-
command! -bang -nargs=* -complete=file LAgAdd call ag#Ag('lgrepadd<bang>', <q-args>)
96
command! -bang -nargs=* -complete=file AgFile call ag#Ag('grep<bang> -g', <q-args>)
107
command! -bang -nargs=* -complete=help AgHelp call ag#AgHelp('grep<bang>',<q-args>)
11-
command! -bang -nargs=* -complete=help LAgHelp call ag#AgHelp('lgrep<bang>',<q-args>)
8+
command! -bang -nargs=* -complete=file LAg call ag#Ag('grep<bang>', <q-args>, 'l')
9+
command! -bang -nargs=* -complete=file LAgAdd call ag#Ag('grepadd<bang>', <q-args>, 'l')
10+
command! -bang -nargs=* -complete=file LAgBuffer call ag#AgBuffer('grep<bang>',<q-args>, 'l')
11+
command! -bang -nargs=* -complete=file LAgFromSearch call ag#AgFromSearch('grep<bang>', <q-args>, 'l')
12+
command! -bang -nargs=* -complete=file LAgFile call ag#Ag('grep<bang> -g', <q-args>, 'l')
13+
command! -bang -nargs=* -complete=help LAgHelp call ag#AgHelp('grep<bang>',<q-args>, 'l')
14+
command! -bang -nargs=* -complete=file QAg call ag#Ag('grep<bang>', <q-args>, 'c')
15+
command! -bang -nargs=* -complete=file QAgAdd call ag#Ag('grepadd<bang>', <q-args>, 'c')
16+
command! -bang -nargs=* -complete=file QAgBuffer call ag#AgBuffer('grep<bang>',<q-args>, 'c')
17+
command! -bang -nargs=* -complete=file QAgFromSearch call ag#AgFromSearch('grep<bang>', <q-args>, 'c')
18+
command! -bang -nargs=* -complete=file QAgFile call ag#Ag('grep<bang> -g', <q-args>, 'c')
19+
command! -bang -nargs=* -complete=help QAgHelp call ag#AgHelp('grep<bang>',<q-args>, 'c')

0 commit comments

Comments
 (0)