PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/misc/Factor.tmbundle/Support/lib/tm_factor.rb

http://github.com/abeaumont/factor
Ruby | 42 lines | 34 code | 8 blank | 0 comment | 5 complexity | 94a0e2e86ba84ca66cab65aea9397db4 MD5 | raw file
 1require 'osx/cocoa'
 2
 3def _wait_for_return_value(pb)
 4    origCount = pb.changeCount
 5    sleep 0.125 while pb.changeCount == origCount
 6end
 7
 8def perform_service(service, in_string, wait_for_return_value=false)
 9    p = OSX::NSPasteboard.pasteboardWithUniqueName
10    p.declareTypes_owner([OSX::NSStringPboardType], nil)
11    p.setString_forType(in_string, OSX::NSStringPboardType)
12    raise "Unable to call service #{service}" unless OSX::NSPerformService(service, p)
13    _wait_for_return_value(p) if wait_for_return_value
14    p.stringForType(OSX::NSStringPboardType)
15end
16
17def textmate_front()
18    system %Q{osascript -e 'tell app "TextMate" to activate'};
19end
20
21def factor_run(code)
22    perform_service("Factor/Evaluate in Listener", code)
23end
24
25def factor_eval(code)
26    r = perform_service("Factor/Evaluate Selection", code, true)
27    textmate_front
28    r
29end
30
31def doc_using_statements(document)
32    document.scan(/\b(USING:\s[^;]*\s;|USE:\s+\S+|IN:\s\S+)/).join("\n") << "\n"
33end
34
35def doc_vocab(document) 
36  document.sub(/\bIN:\s(\S+)/, %Q("\\1"))
37end
38
39def line_current_word(line, point)
40    left = line.rindex(/\s/, point - 1) || 0; right = line.index(/\s/, point) || line.length
41    line[left..right]
42end