<commit_before>require 'pp'
module NeoBundle
  class Vimscript
    def initialize(config={})
      @config = {
        vim: 'vim',
        vimrc: nil
      }
      @config.merge!(config)
    end
    
    def exec(cmd)
      raise NeoBundle::VimscriptError, 'Command is empty!' if cmd.to_s.strip.empty?
      mark = '[neobundle-cmd/vim-script/command-part]'
      config = @config.clone
      config[:vimrc] = '-u %s' % config[:vimrc] unless config[:vimrc].nil?
      command = %[
        %{vim} %{vimrc} -U NONE -i NONE -c "
          try | echo '#{mark}' | #{cmd} | echo '#{mark}' | finally | q! | endtry
        " -c q -e -s -V1  2>&1
      ]
      command = command.gsub("\n",'') % config
      result = %x[#{command}]
      raise NeoBundle::VimscriptError, result if $? != 0
      r = result.split(/\r\n|\r|\n/)
      r = r[(r.index(mark)+1)..(r.rindex(mark)-1)]
      r.join("\n")
    end
  end
end<commit_msg>fixes #4<commit_after>module NeoBundle
  class Vimscript
    MARK = '[neobundle-cmd/vim-script/command-part]'
    
    def initialize(config={})
      @config = {
        vim: 'vim',
        vimrc: nil
      }
      @config.merge!(config)
    end
    
    def exec(cmd)
      raise NeoBundle::VimscriptError, 'Command is empty!' if cmd.to_s.strip.empty?
      config = @config.clone
      config[:vimrc] = '-u %s' % config[:vimrc] unless config[:vimrc].nil?
      command = (<<-SH % config).gsub(/\s+/,' ')
        %{vim} %{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)
      
      # The process surveillance.
      Thread.new do
        sleep 0.01 while process.status
        r.close
        w.close
      end
      
      # Read the command output.
      result = ''
      begin
        loop do
          result += r.gets
        end
      rescue IOError
      end
      
      raise NeoBundle::VimscriptError, result if process.value != 0
      r = result.split(/\r\n|\r|\n/)
      r = r[(r.index(MARK)+1)..(r.rindex(MARK)-1)]
      r.join("\n")
    end
  end
end
{'fraction_non_alphanumeric': 0.15420363329108577, 'fraction_numerical': 0.005914659907055344, 'mean_word_length': 1.5462365591397849, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6784291', 'n_tokens_mistral': 825, 'n_tokens_neox': 792, 'n_words': 185}