class Utils::Editor
def activate
def activate if Array(config.vim_default_args).include?('-g') edit_remote("stupid_trick#{rand}") sleep pause_duration edit_remote_send('<ESC>:bw<CR>') else switch_to_index = `tmux list-panes -F '\#{pane_pid} \#{pane_index}'`.lines.find { |l| pid, index = l.split(' ') pid.to_i == $$ and next if `ps -eo ppid,command|grep ^#{pid}` =~ %r(/edit( |$)) break index.to_i end } switch_to_index and system "tmux select-pane -t #{switch_to_index}" end end
def cmd(*parts)
def cmd(*parts) command = parts.compact.inject([]) do |a, p| case when p == nil, p == [] a when p.respond_to?(:to_ary) a.concat p.to_ary else a << p.to_s end end $DEBUG and warn command * ' ' system(*command.map(&:to_s)) end
def derive_server_name
def derive_server_name name = ENV['VIM_SERVER'] || Dir.pwd RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ and name = "G_#{name}" name.upcase end
def edit(*filenames)
def edit(*filenames) if filenames.size == 1 and source_location = filenames.first.source_location then edit_source_location(source_location) # || edit_file(expand_globs(source_location[0, 1])) elsif source_locations = filenames.map(&:source_location).compact.full? filenames = expand_globs(source_locations.map(&:first)) edit_file(*filenames) end.tap do activate end end
def edit_file(*filenames)
def edit_file(*filenames) make_dirs(*filenames) edit_remote_file(*filenames) end
def edit_file_linenumber(filename, linenumber)
def edit_file_linenumber(filename, linenumber) make_dirs filename if wait? edit_remote_wait("+#{linenumber}", filename) else edit_remote("+#{linenumber}", filename) end end
def edit_remote(*args)
def edit_remote(*args) cmd(*vim, '--servername', servername, '--remote', *args) end
def edit_remote_file(*filenames)
def edit_remote_file(*filenames) if wait? edit_remote_wait(*filenames) else edit_remote(*filenames) end end
def edit_remote_send(*args)
def edit_remote_send(*args) cmd(*vim, '--servername', servername, '--remote-send', *args) end
def edit_remote_wait(*args)
def edit_remote_wait(*args) cmd(*vim, '--servername', servername, '--remote-wait', *args) end
def edit_source_location(source_location)
def edit_source_location(source_location) edit_file_linenumber(source_location[0], source_location[1]) end
def expand_globs(filenames)
def expand_globs(filenames) filenames.map { |f| Dir[f] }.flatten.uniq.sort.full? || filenames end
def file_linenumber?(filename)
def file_linenumber?(filename) filename.match(FILE_LINENUMBER_REGEXP) end
def fullscreen=(enabled)
def fullscreen=(enabled) start sleep pause_duration if enabled edit_remote_send '<ESC>:set fullscreen<CR>' else edit_remote_send '<ESC>:set nofullscreen<CR>' end activate end
def initialize
def initialize self.wait = false self.pause_duration = 1 self.servername = derive_server_name config = Utils::Config::ConfigFile.new config.configure_from_paths self.config = config.edit yield self if block_given? end
def make_dirs(*filenames)
def make_dirs(*filenames) if mkdir for filename in filenames FileUtils.mkdir_p File.dirname(filename) end end end
def serverlist
def serverlist @serverlist ||= `#{vim.map(&:inspect) * ' '} --serverlist`.split end
def start
def start started? or cmd(*vim, '--servername', servername) end
def started?(name = servername)
def started?(name = servername) serverlist.member?(name) end
def stop
def stop started? and edit_remote_send('<ESC>:qa<CR>') end
def vim
def vim ([ config.vim_path ] + Array(config.vim_default_args)) end