2015-07-13 06:22:46 -04:00
|
|
|
" PathSep returns the appropriate OS specific path separator.
|
|
|
|
function! go#util#PathSep()
|
2016-02-17 14:02:39 -05:00
|
|
|
if go#util#IsWin()
|
|
|
|
return '\'
|
|
|
|
endif
|
|
|
|
return '/'
|
2015-07-13 06:22:46 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" PathListSep returns the appropriate OS specific path list separator.
|
|
|
|
function! go#util#PathListSep()
|
2016-02-17 14:02:39 -05:00
|
|
|
if go#util#IsWin()
|
|
|
|
return ";"
|
|
|
|
endif
|
|
|
|
return ":"
|
2015-07-13 06:22:46 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" LineEnding returns the correct line ending, based on the current fileformat
|
|
|
|
function! go#util#LineEnding()
|
2016-02-17 14:02:39 -05:00
|
|
|
if &fileformat == 'dos'
|
|
|
|
return "\r\n"
|
|
|
|
elseif &fileformat == 'mac'
|
|
|
|
return "\r"
|
|
|
|
endif
|
2015-07-13 06:22:46 -04:00
|
|
|
|
2016-02-17 14:02:39 -05:00
|
|
|
return "\n"
|
2015-07-13 06:22:46 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" IsWin returns 1 if current OS is Windows or 0 otherwise
|
|
|
|
function! go#util#IsWin()
|
2016-02-17 14:02:39 -05:00
|
|
|
let win = ['win16', 'win32', 'win64', 'win95']
|
|
|
|
for w in win
|
|
|
|
if (has(w))
|
|
|
|
return 1
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return 0
|
2015-07-13 06:22:46 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" StripPath strips the path's last character if it's a path separator.
|
|
|
|
" example: '/foo/bar/' -> '/foo/bar'
|
|
|
|
function! go#util#StripPathSep(path)
|
2016-02-17 14:02:39 -05:00
|
|
|
let last_char = strlen(a:path) - 1
|
|
|
|
if a:path[last_char] == go#util#PathSep()
|
|
|
|
return strpart(a:path, 0, last_char)
|
|
|
|
endif
|
2015-07-13 06:22:46 -04:00
|
|
|
|
2016-02-17 14:02:39 -05:00
|
|
|
return a:path
|
2015-07-13 06:22:46 -04:00
|
|
|
endfunction
|
|
|
|
|
2016-03-14 06:04:57 -04:00
|
|
|
" StripTrailingSlash strips the trailing slash from the given path list.
|
|
|
|
" example: ['/foo/bar/'] -> ['/foo/bar']
|
|
|
|
function! go#util#StripTrailingSlash(paths)
|
|
|
|
return map(copy(a:paths), 'go#util#StripPathSep(v:val)')
|
|
|
|
endfunction
|
|
|
|
|
2015-12-08 08:20:04 -05:00
|
|
|
" Shelljoin returns a shell-safe string representation of arglist. The
|
|
|
|
" {special} argument of shellescape() may optionally be passed.
|
|
|
|
function! go#util#Shelljoin(arglist, ...)
|
2016-02-17 14:02:39 -05:00
|
|
|
try
|
|
|
|
let ssl_save = &shellslash
|
|
|
|
set noshellslash
|
|
|
|
if a:0
|
|
|
|
return join(map(copy(a:arglist), 'shellescape(v:val, ' . a:1 . ')'), ' ')
|
|
|
|
endif
|
|
|
|
|
|
|
|
return join(map(copy(a:arglist), 'shellescape(v:val)'), ' ')
|
|
|
|
finally
|
|
|
|
let &shellslash = ssl_save
|
|
|
|
endtry
|
2015-12-16 08:53:53 -05:00
|
|
|
endfunction
|
|
|
|
|
2016-02-17 14:02:39 -05:00
|
|
|
" Shelllist returns a shell-safe representation of the items in the given
|
2015-12-16 08:53:53 -05:00
|
|
|
" arglist. The {special} argument of shellescape() may optionally be passed.
|
|
|
|
function! go#util#Shelllist(arglist, ...)
|
2016-02-17 14:02:39 -05:00
|
|
|
try
|
|
|
|
let ssl_save = &shellslash
|
|
|
|
set noshellslash
|
|
|
|
if a:0
|
|
|
|
return map(copy(a:arglist), 'shellescape(v:val, ' . a:1 . ')')
|
|
|
|
endif
|
|
|
|
return map(copy(a:arglist), 'shellescape(v:val)')
|
|
|
|
finally
|
|
|
|
let &shellslash = ssl_save
|
|
|
|
endtry
|
2015-12-16 08:53:53 -05:00
|
|
|
endfunction
|
|
|
|
|
2016-03-20 14:01:44 -04:00
|
|
|
" Returns the byte offset for line and column
|
|
|
|
function! go#util#Offset(line, col)
|
|
|
|
if &encoding != 'utf-8'
|
|
|
|
let sep = go#util#LineEnding()
|
|
|
|
let buf = a:line == 1 ? '' : (join(getline(1, a:line-1), sep) . sep)
|
|
|
|
let buf .= a:col == 1 ? '' : getline('.')[:a:col-2]
|
|
|
|
return len(iconv(buf, &encoding, 'utf-8'))
|
|
|
|
endif
|
|
|
|
return line2byte(a:line) + (a:col-2)
|
|
|
|
endfunction
|
|
|
|
"
|
|
|
|
" Returns the byte offset for the cursor
|
|
|
|
function! go#util#OffsetCursor()
|
|
|
|
return go#util#Offset(line('.'), col('.'))
|
|
|
|
endfunction
|
|
|
|
|
2015-12-16 08:53:53 -05:00
|
|
|
" TODO(arslan): I couldn't parameterize the highlight types. Check if we can
|
|
|
|
" simplify the following functions
|
|
|
|
|
|
|
|
function! go#util#EchoSuccess(msg)
|
|
|
|
redraws! | echon "vim-go: " | echohl Function | echon a:msg | echohl None
|
2015-12-08 08:20:04 -05:00
|
|
|
endfunction
|
2015-12-16 08:53:53 -05:00
|
|
|
|
|
|
|
function! go#util#EchoError(msg)
|
|
|
|
redraws! | echon "vim-go: " | echohl ErrorMsg | echon a:msg | echohl None
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! go#util#EchoWarning(msg)
|
|
|
|
redraws! | echon "vim-go: " | echohl WarningMsg | echon a:msg | echohl None
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! go#util#EchoProgress(msg)
|
|
|
|
redraws! | echon "vim-go: " | echohl Identifier | echon a:msg | echohl None
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" vim:ts=4:sw=4:et
|