def self.find_editor
editor = ENV['EDITOR']
editor ||= %w{ /usr/bin/sensible-editor /usr/bin/editor /usr/bin/vim /usr/bin/vi }.collect {|e| e if FileTest.executable? e}.compact.first
raise StandardError, "Editor not found. Please set your EDITOR env variable" if editor.nil?
if editor.index(' ')
editor = editor.dup if editor.frozen?
editor.gsub!(/([^\\]|^)~/, '\1' + ENV['HOME'])
editor.gsub!(/(^|[^\\])"/, '\1')
editor.gsub!(/\\ /, ' ')
pieces = editor.split(' ')
paths = pieces.each_with_index.map {|_,x| pieces[0..x].join(' ')}.reverse
extensions = (ENV['PATHEXT'] || '').split(';')
pathdirs = ENV['PATH'].split(File::PATH_SEPARATOR)
paths += pathdirs.collect { |dir| paths.collect { |path| File.expand_path(path, dir) } }.flatten
editorfile = paths.select { |path|
FileTest.file?(path) || ! extensions.select {|ext| FileTest.file?(path + ext) }.empty?
}.first
raise StandardError, "Editor not found. Please set your EDITOR env variable" if editorfile.nil?
raw_command = paths[(paths.index editorfile) % pieces.size]
editor = "\"#{editorfile}\"#{editor[raw_command.size()..-1]}"
end
editor
end