Class: NeoBundle::Vimscript

Inherits:
Object
  • Object
show all
Defined in:
lib/neobundle/vimscript.rb

Constant Summary collapse

MARK =
'[neobundle-cmd/vim-script/command-part]'

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Vimscript

Returns a new instance of Vimscript.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/neobundle/vimscript.rb', line 7

def initialize(config={})
  @config = {
    vim: 'vim',
    vimrc: 'NONE',
  }
  @config.merge!(config)
  begin
    result = %x[#{'%{vim} --version' % self.escaped_config}]
    unless result =~ /^VIM - Vi IMproved / and $? == 0 then
      raise NeoBundle::VimCommandError, 'command is not vim!' 
    end
  rescue SystemCallError 
    raise NeoBundle::VimCommandError, 'vim command not found!'
  end
end

Instance Method Details

#exec(cmd, io = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/neobundle/vimscript.rb', line 23

def exec(cmd, io=nil)
  raise NeoBundle::VimscriptError, 'Command is empty!' if cmd.to_s.strip.empty?
  command = (<<-SH % self.escaped_config).gsub(/\s+/,' ').strip
    %{vim} -u %{vimrc} -U NONE -i NONE -e -s -V1
      -c "
        try |
          echo '#{MARK}' |
          #{cmd} |
          echo '#{MARK}' |
          echo '' |
        finally |
          q! |
        endtry
      "
      -c "
        echo '#{MARK}' |
        echo '' |
        q
      "
  SH
  r,w = IO.pipe
  process = Process.detach spawn(command, out: w, err: w)
  
  Thread.new do
    process.join
    r.close
    w.close
  end
  
  begin
    result = []
    is_outputting = false
    
    loop do
      line = r.gets.rstrip
      if line == MARK then
        is_outputting = !is_outputting
      elsif is_outputting then
        io.puts line unless io.nil?
        result.push line
      end
    end
  rescue IOError
    result = result.join("\n")
    raise NeoBundle::VimscriptError, result if process.value != 0
    result
  end
end