Skip to content

Instantly share code, notes, and snippets.

@ynkdir
Last active August 29, 2015 14:14
Show Gist options
  • Save ynkdir/39980b79852e45c17ade to your computer and use it in GitHub Desktop.
Save ynkdir/39980b79852e45c17ade to your computer and use it in GitHub Desktop.
GetVimExePath
" :echo exepath(v:progpath)
" unix: :echo resolve('/proc/self/exe')
function! GetVimExePath()
let r = SystemListUtf16(printf('wmic PROCESS WHERE ProcessID=%d GET ExecutablePath /FORMAT:CSV', getpid()))
return split(r[2], ',')[1]
endfunction
function! SystemUtf16(cmd)
return s:utf16tostr(s:lines2bytes(systemlist(a:cmd)))
endfunction
function! SystemListUtf16(cmd)
return split(SystemUtf16(a:cmd), '\r\n\|\r\|\n', 1)
endfunction
function! s:utf16tostr(b)
let r = ''
let be = 1
let start = 0
if len(a:b) > 2
if a:b[0] == 0xFE && a:b[1] == 0xFF
let be = 1
let start = 2
elseif a:b[0] == 0xFF && a:b[1] == 0xFE
let be = 0
let start = 2
endif
endif
for i in range(start, len(a:b) - 1, 2)
if be
let r .= nr2char(a:b[i] * 0x100 + a:b[i + 1], 1)
else
let r .= nr2char(a:b[i + 1] * 0x100 + a:b[i], 1)
endif
endfor
if &encoding != 'utf-8'
let r = iconv(r, 'utf-8', &encoding)
endif
return r
endfunction
function! s:lines2bytes(lines)
let bytes = []
let first = 1
for line in a:lines
if !first
call add(bytes, 10)
endif
let first = 0
call extend(bytes, map(range(len(line)), 'line[v:val] == "\n" ? 0 : char2nr(line[v:val])'))
endfor
return bytes
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment