Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ros integration to vim
" Vim global plugin for working with ROS
" Maintainer: Michael Styer <michael@styer.net>
" Fixed by : Fujii Shohei <fujii.shohei@gmail.com>
" Helper commands
function! s:RosDecodePath(path)
let rosname = ""
let reldir = ""
let rosdir = ""
let last = ""
if match(a:path,'\v.+/.*') == -1
let rosname = a:path
else
let result = matchlist(a:path,'\v^([^/]+)(/.{-})([^/]*)$')
let rosname = result[1]
let reldir = result[2]
let last = result[3]
endif
let rosdir = s:RosLocationFind(rosname)
let rosvals = [rosname, rosdir, reldir, last]
return rosvals
endfunction
function! s:RosLocationFind(name)
if $ROS_LOCATIONS != ""
let loc_dict = {}
for p in split($ROS_LOCATIONS,':')
let pair = split(p, '=')
let loc_dict[pair[0]] = pair[1]
endfor
if has_key(loc_dict,a:name)
return loc_dict[a:name]
endif
endif
if a:name == 'log'
return $ROS_ROOT . "/bin/roslaunch-logs"
elseif a:name == 'test_results'
return $ROS_ROOT . "/test/rostest/bin/test-results-dir"
endif
let cmd = "export ROS_CACHE_TIMEOUT=-1.0 && rospack find " . a:name . " 2> /dev/null"
let location = system(cmd)
if v:shell_error != 0
let cmd = "export ROS_CACHE_TIMEOUT=-1.0 && rosstack find " . a:name . " 2> /dev/null"
let location = system(cmd)
endif
let location = substitute(location, "\n", "", "")
return location
endfunction
" Top level commands
function! s:RosChangeDir(...)
if a:0 == 0 || a:1 == ""
lcd $ROS_ROOT
return
endif
let rosvals = s:RosDecodePath(a:1)
if rosvals[1] == ""
echo "No such package: " . a:1
return
else
let dir = rosvals[1] . rosvals[2] . rosvals[3]
execute "lcd " . dir
endif
endfunction
function! s:RosEditDir(...)
if a:0 == 0 || a:1 == ""
edit $ROS_ROOT
return
endif
let rosvals = s:RosDecodePath(a:1)
if rosvals[1] == ""
echo "No such package: " . a:1
return
else
let dir = rosvals[1] . rosvals[2] . rosvals[3]
execute "edit " . dir
endif
endfunction
function! s:RosPackList(arg_lead,cmdline,cursor_pos)
let packs = split(system('rospack list | cut -f 1 -d" "'),"\n")
let matches = []
for pack in packs
if pack =~? '^' . strpart(a:arg_lead, 0)
call add(matches, pack )
endif
endfor
return matches
endfunction
command! -nargs=* -complete=customlist,s:RosPackList Roscd :call s:RosChangeDir(<f-args>)
command! -nargs=* -complete=customlist,s:RosPackList Rosed :call s:RosEditDir(<f-args>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment