Module: CommandT::VIM

Defined in:
lib/command-t/vim.rb,
lib/command-t/vim/screen.rb,
lib/command-t/vim/window.rb

Defined Under Namespace

Modules: Screen, Window

Class Method Summary collapse

Class Method Details

.capture(cmd) ⇒ Object

Execute cmd, capturing the output into a variable and returning it.



57
58
59
60
61
62
# File 'lib/command-t/vim.rb', line 57

def capture(cmd)
  ::VIM::command 'silent redir => g:command_t_captured_output'
  ::VIM::command cmd
  ::VIM::command 'silent redir END'
  ::VIM::evaluate 'g:command_t_captured_output'
end

.current_file_dirObject



52
53
54
# File 'lib/command-t/vim.rb', line 52

def current_file_dir
  ::VIM::evaluate 'expand("%:p:h")'
end

.escape_for_single_quotes(str) ⇒ Object

Escape a string for safe inclusion in a Vim single-quoted string (single quotes escaped by doubling, everything else is literal)



66
67
68
# File 'lib/command-t/vim.rb', line 66

def escape_for_single_quotes(str)
  str.gsub "'", "''"
end

.exists?(str) ⇒ Boolean

Check for the presence of a setting such as:

- g:CommandTSmartCase (plug-in setting)
- &wildignore         (Vim setting)
- +cursorcolumn       (Vim setting, that works)

Returns:

  • (Boolean)


21
22
23
# File 'lib/command-t/vim.rb', line 21

def exists?(str)
  ::VIM::evaluate(%{exists("#{str}")}).to_i != 0
end

.get_bool(name, default = nil) ⇒ Object



29
30
31
# File 'lib/command-t/vim.rb', line 29

def get_bool(name, default = nil)
  exists?(name) ? ::VIM::evaluate("#{name}").to_i != 0 : default
end

.get_list_or_string(name) ⇒ Object

expect a string or a list of strings



38
39
40
41
42
43
44
45
46
# File 'lib/command-t/vim.rb', line 38

def get_list_or_string(name)
  return nil unless exists?(name)
  list_or_string = ::VIM::evaluate("#{name}")
  if list_or_string.kind_of?(Array)
    list_or_string.map { |item| item.to_s }
  else
    list_or_string.to_s
  end
end

.get_number(name) ⇒ Object



25
26
27
# File 'lib/command-t/vim.rb', line 25

def get_number(name)
  exists?(name) ? ::VIM::evaluate("#{name}").to_i : nil
end

.get_string(name) ⇒ Object



33
34
35
# File 'lib/command-t/vim.rb', line 33

def get_string(name)
  exists?(name) ? ::VIM::evaluate("#{name}").to_s : nil
end

.has?(feature) ⇒ Boolean

Check for the existence of a feature such as “conceal” or “syntax”.

Returns:

  • (Boolean)


11
12
13
# File 'lib/command-t/vim.rb', line 11

def has?(feature)
  ::VIM::evaluate(%{has("#{feature}")}).to_i != 0
end

.pwdObject



48
49
50
# File 'lib/command-t/vim.rb', line 48

def pwd
  ::VIM::evaluate 'getcwd()'
end

.wildignore_to_regexp(str) ⇒ Object

Conservatively convert wildignore patterns that we understand to a regex. Supported patterns noted in the inline comments below.

If this ends up doing something wrong, set ‘g:CommandTWildIgnore` to ” to opt out or otherwise override to produce a conforming pattern.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/command-t/vim.rb', line 75

def wildignore_to_regexp(str)
  patterns = str.split(',')
  regex = patterns.map do |pattern|
    if pattern.match(%r{\A([^*/]+)\z})
      # something (match file at any level)
      '(\A|/)' + Regexp.escape($~[1]) + '\z'
    elsif pattern.match(%r{\A\*\.([^*]+)\z})
      # *.something (match file with extension at any level)
      '\.' + Regexp.escape($~[1]) + '\z'
    elsif pattern.match(%r{\A\*/([^/]+)/\*\z})
      # */something/* (match directories at any level)
      '(\A|/)' + Regexp.escape($~[1]) + '/.+'
    elsif pattern.match(%r{\A\*/(.+)\z})
      # */something (match files or directories at any level)
      '(\A|/)' + Regexp.escape($~[1]) + '(/|\z)'
    end
  end.compact.join('|')
  Regexp.new(regex) unless regex.empty?
end