1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
" NOTE: You must, of course, install ag / the_silver_searcher
" FIXME: Delete deprecated options below on or after 15-7 (6 months from when they were changed) {{{
if exists("g:agprg")
let g:ag_prg = g:agprg
endif
if exists("g:aghighlight")
let g:ag_highlight = g:aghighlight
endif
if exists("g:agformat")
let g:ag_format = g:agformat
endif
" }}} FIXME: Delete the deprecated options above on or after 15-7 (6 months from when they were changed)
" 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"
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
endif
if !exists("g:ag_apply_qmappings")
let g:ag_apply_qmappings=1
endif
if !exists("g:ag_apply_lmappings")
let g:ag_apply_lmappings=1
endif
if !exists("g:ag_qhandler")
let g:ag_qhandler="botright copen"
endif
if !exists("g:ag_lhandler")
let g:ag_lhandler="botright lopen"
endif
if !exists("g:ag_mapping_message")
let g:ag_mapping_message=1
endif
if !exists("g:ag_working_path_mode")
let g:ag_working_path_mode = 'c'
endif
function! ag#AgBuffer(cmd, args)
let l:bufs = filter(range(1, bufnr('$')), 'buflisted(v:val)')
let l:files = []
for buf in l:bufs
let l:file = fnamemodify(bufname(buf), ':p')
if !isdirectory(l:file)
call add(l:files, l:file)
endif
endfor
call ag#Ag(a:cmd, a:args . ' ' . join(l:files, ' '))
endfunction
function! ag#Ag(cmd, args)
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
" If no pattern is provided, search for the word under the cursor
if empty(a:args)
let l:grepargs = expand("<cword>")
else
let l:grepargs = a:args . join(a:000, ' ')
end
" Format, used to manage column jump
if a:cmd =~# '-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
elseif !exists("g:ag_format")
let g:ag_format="%f:%l:%c:%m"
endif
let l:grepprg_bak=&grepprg
let l:grepformat_bak=&grepformat
let l:t_ti_bak=&t_ti
let l:t_te_bak=&t_te
try
let &grepprg=g:ag_prg
let &grepformat=g:ag_format
set t_ti=
set t_te=
if g:ag_working_path_mode ==? 'r' " Try to find the projectroot for current buffer
let l:cwd_back = getcwd()
let l:cwd = s:guessProjectRoot()
try
exe "lcd ".l:cwd
catch
echom 'Failed to change directory to:'.l:cwd
finally
silent! execute a:cmd . " " . escape(l:grepargs, '|')
exe "lcd ".l:cwd_back
endtry
else " Someone chose an undefined value or 'c' so we revert to the default
silent! execute a:cmd . " " . escape(l:grepargs, '|')
endif
finally
let &grepprg=l:grepprg_bak
let &grepformat=l:grepformat_bak
let &t_ti=l:t_ti_bak
let &t_te=l:t_te_bak
endtry
if a:cmd =~# '^l'
let l:match_count = len(getloclist(winnr()))
else
let l:match_count = len(getqflist())
endif
if a:cmd =~# '^l' && l:match_count
exe g:ag_lhandler
let l:apply_mappings = g:ag_apply_lmappings
let l:matches_window_prefix = 'l' " we're using the location list
elseif l:match_count
exe g:ag_qhandler
let l:apply_mappings = g:ag_apply_qmappings
let l:matches_window_prefix = 'c' " we're using the quickfix window
endif
" If highlighting is on, highlight the search keyword.
if exists('g:ag_highlight')
let @/ = matchstr(a:args, "\\v(-)\@<!(\<)\@<=\\w+|['\"]\\zs.{-}\\ze['\"]")
call feedkeys(":let &hlsearch=1 \| echo \<CR>", 'n')
end
redraw!
if l:match_count
if l:apply_mappings
nnoremap <silent> <buffer> h <C-W><CR><C-w>K
nnoremap <silent> <buffer> H <C-W><CR><C-w>K<C-w>b
nnoremap <silent> <buffer> o <CR>
nnoremap <silent> <buffer> t <C-w><CR><C-w>T
nnoremap <silent> <buffer> T <C-w><CR><C-w>TgT<C-W><C-W>
nnoremap <silent> <buffer> v <C-w><CR><C-w>H<C-W>b<C-W>J<C-W>t
exe 'nnoremap <silent> <buffer> e <CR><C-w><C-w>:' . l:matches_window_prefix .'close<CR>'
exe 'nnoremap <silent> <buffer> go <CR>:' . l:matches_window_prefix . 'open<CR>'
exe 'nnoremap <silent> <buffer> q :' . l:matches_window_prefix . 'close<CR>'
exe 'nnoremap <silent> <buffer> gv :let b:height=winheight(0)<CR><C-w><CR><C-w>H:' . l:matches_window_prefix . 'open<CR><C-w>J:exe printf(":normal %d\<lt>c-w>_", b:height)<CR>'
" Interpretation:
" :let b:height=winheight(0)<CR> Get the height of the quickfix/location list window
" <CR><C-w> Open the current item in a new split
" <C-w>H Slam the newly opened window against the left edge
" :copen<CR> -or- :lopen<CR> Open either the quickfix window or the location list (whichever we were using)
" <C-w>J Slam the quickfix/location list window against the bottom edge
" :exe printf(":normal %d\<lt>c-w>_", b:height)<CR> Restore the quickfix/location list window's height from before we opened the match
if g:ag_mapping_message && l:apply_mappings
echom "ag.vim keys: q=quit <cr>/e/t/h/v=enter/edit/tab/split/vsplit go/T/H/gv=preview versions of same"
endif
endif
else
echom 'No matches for "'.a:args.'"'
endif
endfunction
function! ag#AgFromSearch(cmd, args)
let search = getreg('/')
" translate vim regular expression to perl regular expression.
let search = substitute(search,'\(\\<\|\\>\)','\\b','g')
call ag#Ag(a:cmd, '"' . search .'" '. a:args)
endfunction
function! ag#GetDocLocations()
let dp = ''
for p in split(&runtimepath,',')
let p = p.'doc/'
if isdirectory(p)
let dp = p.'*.txt '.dp
endif
endfor
return dp
endfunction
function! ag#AgHelp(cmd,args)
let args = a:args.' '.ag#GetDocLocations()
call ag#Ag(a:cmd,args)
endfunction
function! s:guessProjectRoot()
let l:splitsearchdir = split(getcwd(), "/")
while len(l:splitsearchdir) > 2
let l:searchdir = '/'.join(l:splitsearchdir, '/').'/'
for l:marker in ['.rootdir', '.git', '.hg', '.svn', 'bzr', '_darcs', 'build.xml']
" found it! Return the dir
if filereadable(l:searchdir.l:marker) || isdirectory(l:searchdir.l:marker)
return l:searchdir
endif
endfor
let l:splitsearchdir = l:splitsearchdir[0:-2] " Splice the list to get rid of the tail directory
endwhile
" Nothing found, fallback to current working dir
return getcwd()
endfunction
|