Skip to content

Instantly share code, notes, and snippets.

@h14i
Created March 28, 2014 20:11
Show Gist options
  • Save h14i/9841954 to your computer and use it in GitHub Desktop.
Save h14i/9841954 to your computer and use it in GitHub Desktop.
ocamlspot.vim with lua
-- ocamlspot.lua
--[[
example:
parse_loc("l100c20b3") -> 100, 20
parse_loc("abcdefghi") -> nil, nil
--]]
function parse_loc(str)
local _, _, line, col = string.find(str, "^l(%d+)c(%d+)b%d+$")
-- print(line, col)
return line, col
end
function spot(bufname, line, col)
local command, proc, sig1, sig2, sig3, w, _
w = vim.window()
command = string.format("ocamlspot %s:l%dc%d 2>&1", bufname, line, col)
print(command)
proc = io.popen(command)
for line in proc:lines() do
_, _, sig1, sig2, sig3 = string.find(line, "^Spot: <(.*):(l%d+c%d+b%d+):(l%d+c%d+b%d+)>$")
-- print(sig1, sig2, sig3)
if sig2 or sig3 then
l1, c1 = parse_loc(sig2)
l2, c2 = parse_loc(sig3)
-- print(string.format("%s %d/%d %d/%d 2>&1", sig1, l1, c1, l2, c2))
vim.command(string.format(":split %s", sig1))
w.line, w.col = l1, c1
vim.command("normal zz")
end
end
proc:close()
end
function spot_type(bufname, line, col)
local command, proc, sig, _
command = string.format("ocamlspot -n %s:l%dc%d", bufname, line, col)
-- print(command)
proc = io.popen(command)
for line in proc:lines() do
_, _, sig = string.find(line, "^Type: (.*)$")
if sig then
print(string.format("Type: %s", sig))
proc:close()
return 0
end
end
print("Type: not found")
proc:close()
end
let s:save_cpo = &cpo
set cpo&vim
exec 'luafile' expand('%:p:h') . '/ocamlspot.lua'
" if !exists('g:loaded_ocamlspot_lua')
" luafile ocamlspot.lua
" let g:loaded_ocamlspot_lua = 1
" endif
function! ocamlspot#spot() "{{{
lua << End
local w = vim.window()
spot(w.buffer.fname, w.line, w.col)
End
endfunction "}}}
function! ocamlspot#type() "{{{
lua << End
local w = vim.window()
spot_type(w.buffer.fname, w.line, w.col)
End
endfunction "}}}
let &cpo = s:save_cpo
unlet s:save_cpo
if exists('g:loaded_ocamlspot') && !empty(g:loaded_ocamlspot)
finish
endif
let g:loaded_ocamlspot = 1
let s:cpo_save = &cpo
set cpo&vim
if !has('lua')
echomsg "can't use lua."
finish
endif
if !executable('ocamlspot')
echomsg "not found ocamlspot."
finish
endif
" command! -nargs=0 OCamlSpotLua call ocamlspot#spot()
" command! -nargs=0 OCamlTypeLua call ocamlspot#type()
command! -nargs=0 OCamlSpot call ocamlspot#spot()
command! -nargs=0 OCamlType call ocamlspot#type()
echomsg "loaded ocamlspot.vim"
let &cpo = s:cpo_save
@h14i
Copy link
Author

h14i commented Mar 28, 2014

やっぱ動かないっぽい。気が向いたら直す。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment