Module: Rvim::Modeline

Defined in:
lib/rvim/modeline.rb

Constant Summary collapse

SET_FORM =

vim: set option=value option=value : (set form) vim: option=value option=value (no-set form)

/\b(?:vim|ex):\s*se?t?\s+(?<opts>.+?):/.freeze
PLAIN_FORM =
/\b(?:vim|ex):\s*(?<opts>.+?)(?::|$)/.freeze

Class Method Summary collapse

Class Method Details

.apply(editor, buffer) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rvim/modeline.rb', line 17

def self.apply(editor, buffer)
  return unless editor.settings.get(:modeline)

  n = editor.settings.get(:modelines).to_i
  return if n <= 0

  lines = buffer.lines
  head = (0...[n, lines.size].min)
  tail_start = [lines.size - n, head.last.to_i + 1].max
  tail = (tail_start...lines.size)

  (head.to_a + tail.to_a).uniq.each do |i|
    line = lines[i]
    next unless line

    tokens = parse(line)
    next unless tokens

    apply_tokens(editor, buffer, tokens)
  end
end

.apply_tokens(editor, buffer, tokens) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rvim/modeline.rb', line 39

def self.apply_tokens(editor, buffer, tokens)
  tokens.each do |tok|
    m = tok.match(Rvim::Command::SET_TOKEN_RE)
    next unless m

    name = m[2]
    value = if m[1] == 'no'
              false
            elsif m[3]
              m[3].match?(/\A\d+\z/) ? m[3].to_i : m[3]
            else
              true
            end
    next unless editor.settings.known?(name)

    editor.settings.set(name, value, buffer: buffer)
  end
end

.parse(line) ⇒ Object



10
11
12
13
14
15
# File 'lib/rvim/modeline.rb', line 10

def self.parse(line)
  m = SET_FORM.match(line) || PLAIN_FORM.match(line)
  return nil unless m

  m[:opts].split(/[\s:]+/).reject(&:empty?)
end