Module: Rvim::Lua::Fn

Defined in:
lib/rvim/lua/fn.rb

Overview

vim.fn — shims for vim’s builtin functions. v3.6 ships a curated whitelist; plugins that call something not yet shimmed get a clear “not implemented” error rather than silently misbehaving.

Class Method Summary collapse

Class Method Details

.bufnr(editor, arg) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/rvim/lua/fn.rb', line 127

def bufnr(editor, arg)
  case arg
  when '%' then editor.current_buffer&.id || -1
  when '#' then -1
  else
    buf = editor.buffers&.values&.find { |b| b.filepath == arg }
    buf ? buf.id : -1
  end
end

.col(editor, arg) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/rvim/lua/fn.rb', line 111

def col(editor, arg)
  case arg
  when '.' then editor.byte_pointer + 1
  when '$' then ((editor.buffer_of_lines[editor.line_index] || '').bytesize) + 1
  else 0
  end
end

.empty?(v) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
146
147
148
149
150
# File 'lib/rvim/lua/fn.rb', line 143

def empty?(v)
  case v
  when nil, '', 0, 0.0 then true
  when Hash, Array then v.empty?
  else
    v.respond_to?(:to_h) ? v.to_h.empty? : false
  end
end

.exepath(name) ⇒ Object



211
212
213
214
215
216
217
# File 'lib/rvim/lua/fn.rb', line 211

def exepath(name)
  ENV['PATH'].to_s.split(':').each do |dir|
    path = File.join(dir, name)
    return path if File.executable?(path) && !File.directory?(path)
  end
  ''
end

.exists?(editor, name) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
# File 'lib/rvim/lua/fn.rb', line 94

def exists?(editor, name)
  case name
  when /\A&(.+)\z/ then editor.settings.known?(Regexp.last_match(1))
  when /\Ag:(.+)\z/ then editor.let_vars.key?(Regexp.last_match(1))
  when /\A:(.+)\z/ then true
  else false
  end
end

.expand(editor, arg) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rvim/lua/fn.rb', line 46

def expand(editor, arg)
  case arg
  when /\A%(:.+)?\z/
    path = editor.filepath.to_s
    mods = arg.sub(/\A%/, '')
    fnamemodify(path, mods)
  when /\A#(:.+)?\z/
    alt = editor.instance_variable_get(:@alternate_filepath).to_s
    mods = arg.sub(/\A#/, '')
    fnamemodify(alt, mods)
  when /\A<\w+>\z/ then ''
  else File.expand_path(arg)
  end
end

.fnamemodify(path, mods) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rvim/lua/fn.rb', line 61

def fnamemodify(path, mods)
  result = path.to_s
  i = 0
  while i < mods.length
    c = mods[i]
    if c == ':'
      i += 1
      next
    end
    case c
    when 'p' then result = File.expand_path(result)
    when 'h' then result = File.dirname(result)
    when 't' then result = File.basename(result)
    when 'r' then result = result.sub(/\.[^.\/]+\z/, '')
    when 'e' then result = File.extname(result).sub(/\A\./, '')
    end
    i += 1
  end
  result
end

.has?(feat) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rvim/lua/fn.rb', line 82

def has?(feat)
  case feat
  when 'nvim' then false
  when 'mac', 'macunix' then RUBY_PLATFORM.include?('darwin')
  when 'unix' then !RUBY_PLATFORM.include?('mswin')
  when 'win32', 'win64' then RUBY_PLATFORM.include?('mswin')
  when 'linux' then RUBY_PLATFORM.include?('linux')
  when 'lua' then true
  else false
  end
end

.install(state, editor, _runtime) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rvim/lua/fn.rb', line 11

def install(state, editor, _runtime)
  state.eval('vim.fn = vim.fn or {}')

  state.function('vim.fn.expand')       { |arg| expand(editor, arg.to_s) }
  state.function('vim.fn.getcwd')       { Dir.pwd }
  state.function('vim.fn.has')          { |feat| has?(feat.to_s) ? 1 : 0 }
  state.function('vim.fn.exists')       { |name| exists?(editor, name.to_s) ? 1 : 0 }
  state.function('vim.fn.line')         { |arg| line(editor, arg.to_s) }
  state.function('vim.fn.col')          { |arg| col(editor, arg.to_s) }
  state.function('vim.fn.mode')         { mode(editor) }
  state.function('vim.fn.bufnr')        { |arg| bufnr(editor, arg.to_s) }
  state.function('vim.fn.winnr')        { (editor.windows || []).index(editor.current_window).to_i + 1 }
  state.function('vim.fn.fnamemodify')  { |path, mods| fnamemodify(path.to_s, mods.to_s) }
  state.function('vim.fn.filereadable') { |path| File.file?(File.expand_path(path.to_s)) ? 1 : 0 }
  state.function('vim.fn.isdirectory')  { |path| File.directory?(File.expand_path(path.to_s)) ? 1 : 0 }
  state.function('vim.fn.system')       { |cmd| Rvim::Filter.run(cmd.to_s).stdout }
  state.function('vim.fn.shellescape')  { |s| shellescape(s.to_s) }
  state.function('vim.fn.getenv')       { |name| ENV[name.to_s] }
  state.function('vim.fn.setenv')       { |name, val| ENV[name.to_s] = val.to_s }
  state.function('vim.fn.empty')        { |v| empty?(v) ? 1 : 0 }
  state.function('vim.fn.len')          { |v| len(v) }
  state.function('vim.fn.type')         { |v| type_id(v) }
  state.function('vim.fn.split')        { |s, sep| split_to_array(s.to_s, sep&.to_s) }
  state.function('vim.fn.join')         { |t, sep| join_lua_table(t, sep&.to_s) }
  state.function('vim.fn.substitute')   { |s, pat, rep, flags| substitute(s.to_s, pat.to_s, rep.to_s, flags.to_s) }
  state.function('vim.fn.tolower')      { |s| s.to_s.downcase }
  state.function('vim.fn.toupper')      { |s| s.to_s.upcase }
  state.function('vim.fn.trim')         { |s| s.to_s.strip }
  state.function('vim.fn.min')          { |t| values_of(t).min }
  state.function('vim.fn.max')          { |t| values_of(t).max }
  state.function('vim.fn.stdpath')      { |what| stdpath(what.to_s) }
  state.function('vim.fn.executable')   { |name| ENV['PATH'].to_s.split(':').any? { |d| File.executable?(File.join(d, name.to_s)) } ? 1 : 0 }
  state.function('vim.fn.exepath')      { |name| exepath(name.to_s) }
end

.join_lua_table(t, sep) ⇒ Object



184
185
186
# File 'lib/rvim/lua/fn.rb', line 184

def join_lua_table(t, sep)
  values_of(t).join(sep || ' ')
end

.len(v) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/rvim/lua/fn.rb', line 152

def len(v)
  case v
  when nil then 0
  when String then v.length
  when Hash, Array then v.size
  else
    v.respond_to?(:to_h) ? v.to_h.size : 0
  end
end

.line(editor, arg) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/rvim/lua/fn.rb', line 103

def line(editor, arg)
  case arg
  when '.' then editor.line_index + 1
  when '$' then editor.buffer_of_lines.size
  else 0
  end
end

.mode(editor) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/rvim/lua/fn.rb', line 119

def mode(editor)
  case editor.editing_mode_label
  when :vi_command then 'n'
  when :vi_insert then 'i'
  else editor.editing_mode_label.to_s
  end
end

.shellescape(s) ⇒ Object



137
138
139
140
141
# File 'lib/rvim/lua/fn.rb', line 137

def shellescape(s)
  return "''" if s.empty?

  "'#{s.gsub("'", %q('\\\\''))}'"
end

.split_to_array(s, sep) ⇒ Object



180
181
182
# File 'lib/rvim/lua/fn.rb', line 180

def split_to_array(s, sep)
  s.split(sep || /\s+/)
end

.stdpath(what) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/rvim/lua/fn.rb', line 197

def stdpath(what)
  cache = ENV['XDG_CACHE_HOME'] || File.expand_path('~/.cache')
  config = ENV['XDG_CONFIG_HOME'] || File.expand_path('~/.config')
  data = ENV['XDG_DATA_HOME'] || File.expand_path('~/.local/share')
  case what
  when 'config' then File.join(config, 'rvim')
  when 'data' then File.join(data, 'rvim')
  when 'cache' then File.join(cache, 'rvim')
  when 'state' then File.join(cache, 'rvim', 'state')
  when 'log' then File.join(cache, 'rvim', 'log')
  else ''
  end
end

.substitute(s, pat, rep, flags) ⇒ Object



188
189
190
191
192
193
194
195
# File 'lib/rvim/lua/fn.rb', line 188

def substitute(s, pat, rep, flags)
  regex = Regexp.new(pat)
  if flags.include?('g')
    s.gsub(regex, rep)
  else
    s.sub(regex, rep)
  end
end

.type_id(v) ⇒ Object



162
163
164
165
166
167
168
169
170
171
# File 'lib/rvim/lua/fn.rb', line 162

def type_id(v)
  case v
  when Numeric then 0
  when String then 1
  when nil then 7
  when TrueClass, FalseClass then 6
  else
    v.respond_to?(:to_h) ? 4 : 0
  end
end

.values_of(t) ⇒ Object



173
174
175
176
177
178
# File 'lib/rvim/lua/fn.rb', line 173

def values_of(t)
  return t if t.is_a?(Array)
  return t.to_h.values if t.respond_to?(:to_h)

  Array(t)
end