Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#append(text) ⇒ Object

append text to the end of the current_buffer

:ruby append 'hello there'


115
116
117
# File 'lib/vimilicious.rb', line 115

def append text
  current_buffer.append current_buffer.length, text
end

#clearObject

deletes the current buffer (closes the file) but keeps the current layout

:ruby clear


93
94
95
96
# File 'lib/vimilicious.rb', line 93

def clear
  cmd 'let kwbd_bn= bufnr("%")|enew|exe "bdel ".kwbd_bn|unlet kwbd_bn'
  clear_buffer
end

#clear!Object

forcefully deletes the current buffer and clears the wholelayout

:ruby clear!


101
102
103
104
# File 'lib/vimilicious.rb', line 101

def clear!
  cmd 'bd!'
  clear_buffer
end

#clear_bufferObject

deletes all lines in the current_buffer



107
108
109
110
# File 'lib/vimilicious.rb', line 107

def clear_buffer
  exec 'gg'
  current_buffer.length.times { current_buffer.delete(1) }
end

#cmd(vim_ex_command) ⇒ Object

run vim command

:ruby puts cmd("echo 'hello!'")


6
7
8
# File 'lib/vimilicious.rb', line 6

def cmd vim_ex_command
  VIM::command vim_ex_command
end

#create_command(name, method = nil, &block) ⇒ Object

create a vim user command that calls a ruby method or block

:ruby create_command :test                # creates a :Test command that calls a 'test' method
:ruby create_command 'test', :hi          # creates a :Test command that calls a 'hi' method
:ruby create_command(:test){ puts 'hi' }  # creates a :Test command that calls the block passed in

WARNING … as of now, the args passed to these commands get turned into one big string which is passed along to the function and method. i haven’t figured out howto fix this yet :(



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/vimilicious.rb', line 136

def create_command name, method = nil, &block
  command_name  = name.to_s.capitalize
  method_name   = (method.nil?) ? name.to_s : method.to_s
  function_name = command_name + 'AutoGeneratedFunction'
  
  # create a function that calls method (or block)
  if block.nil?
    cmd %[fu! #{ function_name }(...)\n  ruby #{ method_name } *eval("\#{ vim_eval('string(a:000)') }")\nendfu]
  else
    generated_method = command_name + '_auto_generated_method'
    Kernel.module_eval { define_method generated_method, block }
    cmd %[fu! #{ function_name }(...)\n  ruby #{ generated_method } *eval("\#{ vim_eval('string(a:000)') }")\nendfu]
  end

  # create a vim command that calls the vim function
  cmd %{command! -nargs=* #{ command_name } call #{ function_name }(<args>)}
end

#current_bufferObject

get the current buffer

:ruby puts "the current line says: #{current_buffer.line}"


48
49
50
# File 'lib/vimilicious.rb', line 48

def current_buffer
  VIM::Buffer.current # $curbuf
end

#current_fileObject

get the name of the current file

:ruby puts "currently editing: #{current_file}"


62
63
64
# File 'lib/vimilicious.rb', line 62

def current_file
  current_buffer.name
end

#current_lineObject

get the text of the currently selected line

:ruby puts "the current line says: #{current_line}"


76
77
78
# File 'lib/vimilicious.rb', line 76

def current_line
  current_buffer.line
end

#current_windowObject

get the current window

:ruby puts "the cursor is at: #{current_window.cursor.inspect}"


55
56
57
# File 'lib/vimilicious.rb', line 55

def current_window
  VIM::Window.current # $curwin
end

#current_word(filter = /[,'`\.:\(\)\[\]\}\{]/, replace_with = '') ⇒ Object

get the word under the cursor

:ruby puts "the cursor is on top of the word: #{current_word}"


157
158
159
160
161
162
163
164
165
# File 'lib/vimilicious.rb', line 157

def current_word filter=/[,'`\.:\(\)\[\]\}\{]/, replace_with=''
  line, index = current_line, cursor[1]
  word_start, word_end = line.rindex(' ',index) || 0, line.index(' ',index)
  word_start += 1 if word_start > 0 
  word_end = line.length if word_end.nil?
  word = line[word_start, word_end-word_start] || ''
  word.gsub!(filter,replace_with) unless word.empty?
  word
end

#cursorObject

get the current cursor location (as array [line_number,position])

:ruby puts "the cursor is at: #{cursor.inspect}"


69
70
71
# File 'lib/vimilicious.rb', line 69

def cursor
  current_window.cursor
end

#exec(normal_vim_command) ⇒ Object

execute ‘normal’ command

:ruby exec '<ESC>ihello there!<ESC>'


20
21
22
# File 'lib/vimilicious.rb', line 20

def exec normal_vim_command
  cmd %[exec "normal #{normal_vim_command}"].gsub('<','\<')
end

#prompt(name = 'input', format = lambda { |name| "#{name}: " }) ⇒ Object

prompts user for input

:ruby prompt('username')


122
123
124
125
126
# File 'lib/vimilicious.rb', line 122

def prompt name = 'input', format = lambda { |name| "#{name}: " }
  input = vim_eval("inputdialog('#{ format.call(name) }')")
  puts '' # clear statusline thinger
  input
end

#puts(message) ⇒ Object

alias ‘puts’ to vim ‘print’ method

:ruby puts 'hello!'


27
28
29
# File 'lib/vimilicious.rb', line 27

def puts message
  print message
end

#set_current_line(text) ⇒ Object

set the text of the currently selected line

we’re not using the more conventional current_line= because that simply creates a local variable named current_line

:ruby set_current_line 'hi there'


86
87
88
# File 'lib/vimilicious.rb', line 86

def set_current_line text
  current_buffer[ current_buffer.line_number ] = text.to_s
end

#vim_defined?(var) ⇒ Boolean

check to see if a vim variable is defined

:ruby puts 'x is defined? ' + vim_defined?'x'

Returns:

  • (Boolean)


34
35
36
# File 'lib/vimilicious.rb', line 34

def vim_defined? var 
  vim_eval("exists('#{var}')") == '1' 
end

#vim_eval(vim_expression) ⇒ Object

evaluate vim expression

:ruby puts 'x = ' + vim_eval('x')


13
14
15
# File 'lib/vimilicious.rb', line 13

def vim_eval vim_expression
  VIM::evaluate vim_expression
end

#vim_var(var) ⇒ Object

get the value of a vim variable (else nil)

:ruby x = vim_var('x'); puts "x = #{x}"


41
42
43
# File 'lib/vimilicious.rb', line 41

def vim_var var 
  vim_eval(var) if vim_defined?var
end