Skip to content

Instantly share code, notes, and snippets.

@houshuang
Created September 29, 2013 01:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save houshuang/6748566 to your computer and use it in GitHub Desktop.
Save houshuang/6748566 to your computer and use it in GitHub Desktop.
Automatically open function in Sublime Text (first Julia macro)
macro whichs(ex)
ex = expand(ex)
exret = Expr(:call, :error, "expression is not a function call")
if !isa(ex, Expr)
# do nothing -> error
elseif ex.head == :call
exret = Expr(:call, :whichs, map(esc, ex.args)...)
elseif ex.head == :body
a1 = ex.args[1]
if isa(a1, Expr) && a1.head == :call
a11 = a1.args[1]
if a11 == :setindex!
exret = Expr(:call, :whichs, a11, map(esc, a1.args[2:end])...)
end
end
elseif ex.head == :thunk
exret = Expr(:call, :error, "expression is not a function call, or is too complex for @which to analyze; "
* "break it down to simpler parts if possible")
end
exret
end
function whichst(f, types)
for m in methods(f, types)
lsd = m.func.code::LambdaStaticData
file = string(lsd.file)
print(file)
print(typeof(file))
print(ismatch(r"\/",file))
if !ismatch(r"\/", file)
file = string("/Users/Stian/src/julia/base/", file)
print("hi")
end
print(file)
run(`subl $(file):$(lsd.line)`)
end
end
whichs(f, args...) = whichst(f, map(a->(isa(a,Type) ? Type{a} : typeof(a)), args))
@ZacCranko
Copy link

This might be a cleaner implementation of the above functionality if anyone is interested.

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