我经常在打开 TextMate 窗口的情况下进行编码,并在与之相邻的终端窗口中使用 irb 进程。我希望能够在 TextMate 中按下一个键序列,它执行以下操作:
- 复制当前选择,如果没有,则复制当前行。
- 将其粘贴到运行 irb 的最顶层终端窗口中。
- 按下回车键以便在 irb 窗口中执行该行代码。
我在 R 中编码时使用了这种交互开发风格,发现它非常方便。我很确定 emacs 和 SLIME 也可以让你像这样工作。Ruby 和 TextMate 可以吗?
您必须创建一个捆绑命令和一个键盘快捷键来执行此操作。
命令:
#!/usr/bin/ruby
input = STDIN.gets
`osascript << EOF
tell application "Terminal"
activate
end tell
delay 1
tell application "System Events"
keystroke "#{input.gsub('"', '\"')}"
keystroke return
end tell
EOF`
假设您不想查看终端,而是希望结果显示在 TextMate 中,就像在 Smalltalk 工作区中一样。
本质上,您希望在 text mate 中运行 ruby,但您希望在执行之间记住变量。你可以拥有它。
(感谢stef关于如何添加新命令的说明)
gem install daemons
@@hi = "Hello World"
和@@hi + "ya"
irb 服务器:
#!/usr/bin/env ruby -w
require 'rubygems'
require 'daemons'
require 'socket'
LARGE = 100000000
PIPE = "/tmp/.irbservepipe"
def kill_pipe
`rm -f #{PIPE}`
end
def respond_to(pipe)
inp = pipe.recv LARGE
inp.nil? and return
begin
out = eval(inp)
rescue Exception => e
out = e
end
pipe.send out.inspect, 0
end
def ensure_server
["EXIT", "INT", "HUP", "TERM"].each {|ea| trap( ea ) { kill_pipe }}
File.exists?(PIPE) and kill_pipe
server = UNIXServer.new(PIPE)
loop {
c = server.accept
respond_to c
c.close
}
end
Daemons.daemonize
ensure_server
命令:
#!/usr/bin/env macruby -w
require 'socket'
LARGE = 100000000
PIPE = "/tmp/.irbservepipe"
input = STDIN.read
socket = UNIXSocket.new(PIPE)
socket.send input, 0
puts socket.recv LARGE