Skip to content

Commit 6e49b48

Browse files
committed
Move helper functions into autoload
1 parent 376b689 commit 6e49b48

File tree

2 files changed

+451
-450
lines changed

2 files changed

+451
-450
lines changed

autoload/EasyGrep.vim

Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,289 @@ endif
1212
" }}}
1313
" }}}
1414

15+
" Helper Functions {{{
16+
" countstr {{{
17+
function! EasyGrep#countstr(str, ele)
18+
let end = len(a:str)
19+
let c = 0
20+
let i = 0
21+
while i < end
22+
if a:str[i] == a:ele
23+
let c += 1
24+
endif
25+
let i += 1
26+
endwhile
27+
28+
return c
29+
endfunction
30+
"}}}
31+
" unique {{{
32+
function! EasyGrep#unique(lst)
33+
if empty(a:lst)
34+
return a:lst
35+
endif
36+
37+
let lst = a:lst
38+
call sort(lst)
39+
40+
let end = len(lst)
41+
let i = 1
42+
let lastSeen = lst[0]
43+
while i < end
44+
if lst[i] == lastSeen
45+
call remove(lst, i)
46+
let end -= 1
47+
else
48+
let i += 1
49+
endif
50+
endwhile
51+
52+
return lst
53+
endfunction
54+
"}}}
55+
" BackToForwardSlash {{{
56+
function! EasyGrep#BackToForwardSlash(arg)
57+
return substitute(a:arg, '\\', '/', 'g')
58+
endfunction
59+
"}}}
60+
" ForwardToBackSlash {{{
61+
function! EasyGrep#ForwardToBackSlash(arg)
62+
return substitute(a:arg, '/', '\\', 'g')
63+
endfunction
64+
"}}}
65+
" GetBuffersOutput {{{
66+
function! EasyGrep#GetBuffersOutput(all)
67+
let optbang = a:all ? "!" : ""
68+
redir => bufoutput
69+
exe "silent! buffers".optbang
70+
" This echo clears a bug in printing that shows up when it is not present
71+
silent! echo ""
72+
redir END
73+
74+
return bufoutput
75+
endfunction
76+
" }}}
77+
" GetBufferIdList {{{
78+
function! EasyGrep#GetBufferIdList()
79+
let bufoutput = EasyGrep#GetBuffersOutput(0)
80+
81+
let bufids = []
82+
for i in split(bufoutput, "\n")
83+
let s1 = 0
84+
while i[s1] == ' '
85+
let s1 += 1
86+
endwhile
87+
88+
let s2 = stridx(i, ' ', s1) - 1
89+
let id = str2nr(i[s1 : s2])
90+
91+
call add(bufids, id)
92+
endfor
93+
94+
return bufids
95+
endfunction
96+
" }}}
97+
" GetBufferNamesList {{{
98+
function! EasyGrep#GetBufferNamesList()
99+
let bufoutput = EasyGrep#GetBuffersOutput(0)
100+
101+
let bufNames = []
102+
for i in split(bufoutput, "\n")
103+
let s1 = stridx(i, '"') + 1
104+
let s2 = stridx(i, '"', s1) - 1
105+
let str = i[s1 : s2]
106+
107+
if str[0] == '[' && str[len(str)-1] == ']'
108+
continue
109+
endif
110+
111+
if str != "" && has("win32") && str[0] == "/"
112+
" Add the drive prefix
113+
let str = fnamemodify(str, ":p")
114+
endif
115+
116+
call add(bufNames, str)
117+
endfor
118+
119+
return bufNames
120+
endfunction
121+
" }}}
122+
" GetBufferDirsList {{{
123+
function! EasyGrep#GetBufferDirsList()
124+
let dirs = {}
125+
let bufs = EasyGrep#GetBufferNamesList()
126+
let currDir = EasyGrep#GetCwdEscaped()
127+
for buf in bufs
128+
let d = fnamemodify(expand(buf), ":.:h")
129+
if empty(d)
130+
let d = currDir
131+
elseif has("win32") && d[0] == "/"
132+
" Add the drive prefix but remove the trailing slash
133+
let d = fnamemodify(d, ":p:s-/$--")
134+
endif
135+
let dirs[d]=1
136+
endfor
137+
" Note that this returns a unique set of directories
138+
return sort(keys(dirs))
139+
endfunction
140+
" }}}
141+
" GetVisibleBuffers {{{
142+
function! EasyGrep#GetVisibleBuffers()
143+
let tablist = []
144+
for i in range(tabpagenr('$'))
145+
call extend(tablist, tabpagebuflist(i + 1))
146+
endfor
147+
let tablist = EasyGrep#unique(tablist)
148+
return tablist
149+
endfunction
150+
" }}}
151+
" IsListOpen {{{
152+
function! EasyGrep#IsListOpen(name)
153+
let bufoutput = EasyGrep#GetBuffersOutput(1)
154+
return match(bufoutput, "\\[".a:name." List\\]", 0, 0) != -1
155+
endfunction
156+
" }}}
157+
" IsQuickfixListOpen {{{
158+
function! EasyGrep#IsQuickfixListOpen()
159+
let a = EasyGrep#IsListOpen("Quickfix")
160+
return EasyGrep#IsListOpen("Quickfix")
161+
endfunction
162+
" }}}
163+
" IsLocationListOpen {{{
164+
function! EasyGrep#IsLocationListOpen()
165+
return EasyGrep#IsListOpen("Location")
166+
endfunction
167+
" }}}
168+
" GetCwdEscaped {{{
169+
function! EasyGrep#GetCwdEscaped()
170+
return EasyGrep#FileEscape(getcwd())
171+
endfunction
172+
"}}}
173+
" EscapeList/ShellEscapeList {{{
174+
function! EasyGrep#FileEscape(item)
175+
return escape(a:item, ' \')
176+
endfunction
177+
function! EasyGrep#ShellEscape(item)
178+
return shellescape(a:item, 1)
179+
endfunction
180+
function! EasyGrep#DoEscapeList(lst, seperator, func)
181+
let escapedList = []
182+
for item in a:lst
183+
let e = a:func(item).a:seperator
184+
call add(escapedList, e)
185+
endfor
186+
return escapedList
187+
endfunction
188+
function! EasyGrep#EscapeList(lst, seperator)
189+
return EasyGrep#DoEscapeList(a:lst, a:seperator, function("EasyGrep#FileEscape"))
190+
endfunction
191+
function! EasyGrep#ShellEscapeList(lst, seperator)
192+
return EasyGrep#DoEscapeList(a:lst, a:seperator, function("EasyGrep#ShellEscape"))
193+
endfunction
194+
"}}}
195+
" GetSavedVariableName {{{
196+
function! EasyGrep#GetSavedVariableName(var)
197+
let var = a:var
198+
if match(var, "g:") == 0
199+
let var = substitute(var, "g:", "g_", "")
200+
endif
201+
return "s:saved_".var
202+
endfunction
203+
" }}}
204+
" SaveVariable {{{
205+
function! EasyGrep#SaveVariable(var)
206+
if empty(a:var)
207+
return
208+
endif
209+
let savedName = EasyGrep#GetSavedVariableName(a:var)
210+
if match(a:var, "g:") == 0
211+
execute "let ".savedName." = ".a:var
212+
else
213+
execute "let ".savedName." = &".a:var
214+
endif
215+
endfunction
216+
" }}}
217+
" RestoreVariable {{{
218+
" if a second variable is present, indicate no unlet
219+
function! EasyGrep#RestoreVariable(var, ...)
220+
let doUnlet = a:0 == 1
221+
let savedName = EasyGrep#GetSavedVariableName(a:var)
222+
if exists(savedName)
223+
if match(a:var, "g:") == 0
224+
execute "let ".a:var." = ".savedName
225+
else
226+
execute "let &".a:var." = ".savedName
227+
endif
228+
if doUnlet
229+
unlet savedName
230+
endif
231+
endif
232+
endfunction
233+
" }}}
234+
" OnOrOff {{{
235+
function! EasyGrep#OnOrOff(num)
236+
return a:num == 0 ? 'off' : 'on'
237+
endfunction
238+
"}}}
239+
" Trim {{{
240+
function! EasyGrep#Trim(s)
241+
let len = strlen(a:s)
242+
243+
let beg = 0
244+
while beg < len
245+
if a:s[beg] != " " && a:s[beg] != "\t"
246+
break
247+
endif
248+
let beg += 1
249+
endwhile
250+
251+
let end = len - 1
252+
while end > beg
253+
if a:s[end] != " " && a:s[end] != "\t"
254+
break
255+
endif
256+
let end -= 1
257+
endwhile
258+
259+
return strpart(a:s, beg, end-beg+1)
260+
endfunction
261+
"}}}
262+
" ClearNewline {{{
263+
function! EasyGrep#ClearNewline(s)
264+
if empty(a:s)
265+
return a:s
266+
endif
267+
268+
let lastchar = strlen(a:s)-1
269+
if char2nr(a:s[lastchar]) == 10
270+
return strpart(a:s, 0, lastchar)
271+
endif
272+
273+
return a:s
274+
endfunction
275+
"}}}
276+
" Info/Warning/Error {{{
277+
function! EasyGrep#Log(message)
278+
if exists("g:EasyGrepEnableLogging")
279+
echohl Title | echomsg "[EasyGrep] Log: ".a:message | echohl None
280+
endif
281+
endfunction
282+
function! EasyGrep#Info(message)
283+
echohl Normal | echomsg "[EasyGrep] Info: ".a:message | echohl None
284+
endfunction
285+
function! EasyGrep#Warning(message)
286+
echohl WarningMsg | echomsg "[EasyGrep] Warning: ".a:message | echohl None
287+
endfunction
288+
function! EasyGrep#Error(message)
289+
echohl ErrorMsg | echomsg "[EasyGrep] Error: ".a:message | echohl None
290+
endfunction
291+
function! EasyGrep#InternalFailure(message)
292+
echoerr a:message
293+
call EasyGrep#Info("Please record the error message above and contact EasyGrep's author for help in resolving this issue")
294+
endfunction
295+
"}}}
296+
" }}}
297+
15298
" ResultList Functions {{{
16299
" GetErrorList {{{
17300
function! EasyGrep#GetErrorList()

0 commit comments

Comments
 (0)