Class: Rvim::Autocommands
- Inherits:
-
Object
- Object
- Rvim::Autocommands
- Defined in:
- lib/rvim/autocommands.rb
Defined Under Namespace
Classes: Entry
Constant Summary collapse
- MAX_FIRE_DEPTH =
10
Instance Attribute Summary collapse
-
#current_group ⇒ Object
Returns the value of attribute current_group.
Class Method Summary collapse
Instance Method Summary collapse
- #add(events, patterns, command, callback: nil, group: nil) ⇒ Object
- #clear_all ⇒ Object
- #clear_group(group) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #fire(event, value, editor) ⇒ Object
-
#initialize ⇒ Autocommands
constructor
A new instance of Autocommands.
- #remove(event: nil, pattern: nil, group: :__any__) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize ⇒ Autocommands
Returns a new instance of Autocommands.
9 10 11 12 13 |
# File 'lib/rvim/autocommands.rb', line 9 def initialize @entries = [] @current_group = nil @fire_depth = 0 end |
Instance Attribute Details
#current_group ⇒ Object
Returns the value of attribute current_group.
15 16 17 |
# File 'lib/rvim/autocommands.rb', line 15 def current_group @current_group end |
Class Method Details
.pattern_to_regex(pat) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/rvim/autocommands.rb', line 93 def self.pattern_to_regex(pat) out = +'\A' i = 0 while i < pat.length c = pat[i] case c when '*' then out << '.*' when '?' then out << '.' when '{' close = pat.index('}', i) if close alts = pat[(i + 1)...close].split(',').map { |a| Regexp.escape(a) } out << "(?:#{alts.join('|')})" i = close else out << Regexp.escape(c) end when '.', '+', '(', ')', '^', '$', '|', '\\', '[', ']' out << Regexp.escape(c) else out << c end i += 1 end out << '\z' Regexp.new(out) end |
Instance Method Details
#add(events, patterns, command, callback: nil, group: nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rvim/autocommands.rb', line 17 def add(events, patterns, command, callback: nil, group: nil) Array(events).each do |event| Array(patterns).each do |pattern| @entries << Entry.new( event: normalize_event(event), pattern: pattern.to_s, command: command.to_s, group: group || @current_group, callback: callback, ) end end end |
#clear_all ⇒ Object
45 46 47 |
# File 'lib/rvim/autocommands.rb', line 45 def clear_all @entries.clear end |
#clear_group(group) ⇒ Object
41 42 43 |
# File 'lib/rvim/autocommands.rb', line 41 def clear_group(group) @entries.reject! { |e| e.group == group } end |
#each(&block) ⇒ Object
72 73 74 |
# File 'lib/rvim/autocommands.rb', line 72 def each(&block) @entries.each(&block) end |
#empty? ⇒ Boolean
76 77 78 |
# File 'lib/rvim/autocommands.rb', line 76 def empty? @entries.empty? end |
#fire(event, value, editor) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rvim/autocommands.rb', line 49 def fire(event, value, editor) ev = normalize_event(event) return if @fire_depth >= MAX_FIRE_DEPTH @fire_depth += 1 begin # Snapshot to be safe against autocmds modifying the table. @entries.dup.each do |entry| next unless entry.event == ev next unless pattern_matches?(entry.pattern, value) if entry.callback entry.callback.call(event: ev, file: value) else parsed = Rvim::Command.parse(entry.command) Rvim::Command.execute(editor, parsed) if parsed end end ensure @fire_depth -= 1 end end |
#remove(event: nil, pattern: nil, group: :__any__) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/rvim/autocommands.rb', line 31 def remove(event: nil, pattern: nil, group: :__any__) ev = event && normalize_event(event) grp = (group == :__any__) ? @current_group : group @entries.reject! do |e| (ev.nil? || e.event == ev) && (pattern.nil? || e.pattern == pattern) && (grp == :__any__ || e.group == grp) end end |
#size ⇒ Object
80 81 82 |
# File 'lib/rvim/autocommands.rb', line 80 def size @entries.size end |