Skip to content

Instantly share code, notes, and snippets.

@nocd5
Last active August 29, 2015 14:04
Show Gist options
  • Save nocd5/7475413b563097f48ca4 to your computer and use it in GitHub Desktop.
Save nocd5/7475413b563097f48ca4 to your computer and use it in GitHub Desktop.
Windowsでも`vim -g`と出来るようにするRubyスクリプト。
#!/usr/bin/env ruby
$args = ARGV
$option = Array.new
$file = Array.new
# オプションとファイルに分割
pos = $args.index("--")
if !pos.nil? then
$option = $args[0, pos]
# $fileに"--"オプションも含めてしまう
$file = $args[pos, $args.length - pos]
else
$option = $args
end
# GUIとForegroundのチェック
is_gui = false
is_fg = false
$option.map!{ |opt|
if opt =~ /^-\w*[gf]\w*/ then
buf = String.new(opt)
if buf =~ /^-\w*g\w*/ then
is_gui = true
buf.gsub!("g", "")
end
if buf =~ /^-\w*f\w*/ then
is_fg = true
buf.gsub!("f", "")
end
# "-"だけが残った場合はこれを削除する
buf == "-" ? nil : buf
else
opt
end
}.compact!
if is_gui then
vim_exec = "gvim"
else
vim_exec = "vim"
end
# パイプで起動されたか?
if STDIN.tty? then
is_pipe = false
$option.delete("-")
else
is_pipe = true
$option.unshift("-") if !$option.include?("-")
end
# スペースを含むものはクォーテーションで囲みつつ
# 与えられた引数を連結
($option + $file).each{ |arg|
vim_exec += arg.include?(" ") ? " \"#{arg}\"" : " #{arg}"
}
# プロセス起動
# GUI & Foregroundの時のみdetachして抜ける
pid = spawn(vim_exec)
if is_gui & is_fg then
Process.detach(pid)
else
Process.waitpid(pid)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment