1 if exists('g:loaded_gocode')
4 let g:loaded_gocode = 1
6 fu! s:gocodeCurrentBuffer()
7 let buf = getline(1, '$')
8 if &encoding != 'utf-8'
9 let buf = map(buf, 'iconv(v:val, &encoding, "utf-8")')
11 if &l:fileformat == 'dos'
12 " XXX: line2byte() depend on 'fileformat' option.
13 " so if fileformat is 'dos', 'buf' must include '\r'.
14 let buf = map(buf, 'v:val."\r"')
17 call writefile(buf, file)
21 let s:vim_system = get(g:, 'gocomplete#system_function', 'system')
23 fu! s:system(str, ...)
24 return call(s:vim_system, [a:str] + a:000)
27 fu! s:gocodeShellescape(arg)
29 let ssl_save = &shellslash
31 return shellescape(a:arg)
33 let &shellslash = ssl_save
37 fu! s:gocodeCommand(cmd, preargs, args)
38 for i in range(0, len(a:args) - 1)
39 let a:args[i] = s:gocodeShellescape(a:args[i])
41 for i in range(0, len(a:preargs) - 1)
42 let a:preargs[i] = s:gocodeShellescape(a:preargs[i])
44 let result = s:system(printf('gocode %s %s %s', join(a:preargs), a:cmd, join(a:args)))
48 if &encoding != 'utf-8'
49 let result = iconv(result, 'utf-8', &encoding)
55 fu! s:gocodeCurrentBufferOpt(filename)
56 return '-in=' . a:filename
60 if &encoding != 'utf-8'
62 let buf = line('.') == 1 ? "" : (join(getline(1, line('.')-1), "\n") . "\n")
63 let buf .= c == 1 ? "" : getline('.')[:c-2]
64 return printf('%d', len(iconv(buf, &encoding, "utf-8")))
66 return printf('%d', line2byte(line('.')) + (col('.')-2))
69 fu! s:gocodeAutocomplete()
70 let filename = s:gocodeCurrentBuffer()
71 let result = s:gocodeCommand('autocomplete',
72 \ [s:gocodeCurrentBufferOpt(filename), '-f=vim'],
73 \ [expand('%:p'), s:gocodeCursor()])
78 fu! gocomplete#Complete(findstart, base)
79 "findstart = 1 when we need to get the text length
81 execute "silent let g:gocomplete_completions = " . s:gocodeAutocomplete()
82 return col('.') - g:gocomplete_completions[0] - 1
83 "findstart = 0 when we need to return the list of completions
85 return g:gocomplete_completions[1]