Class: Grep
- Inherits:
-
Object
- Object
- Grep
- Defined in:
- lib/vimamsa/search_replace.rb
Constant Summary collapse
- @@cur =
Current object of class
nil
Instance Attribute Summary collapse
-
#buf ⇒ Object
readonly
Returns the value of attribute buf.
Class Method Summary collapse
Instance Method Summary collapse
- #apply_changes ⇒ Object
- #callback(search_str, b = nil) ⇒ Object
-
#initialize ⇒ Grep
constructor
A new instance of Grep.
- #refresh ⇒ Object
- #run ⇒ Object
- #set_buf_contents ⇒ Object
Constructor Details
#initialize ⇒ Grep
Returns a new instance of Grep.
111 112 113 114 |
# File 'lib/vimamsa/search_replace.rb', line 111 def initialize() @buf = nil @line_to_id = {} end |
Instance Attribute Details
#buf ⇒ Object (readonly)
Returns the value of attribute buf.
60 61 62 |
# File 'lib/vimamsa/search_replace.rb', line 60 def buf @buf end |
Class Method Details
.cur ⇒ Object
63 64 65 |
# File 'lib/vimamsa/search_replace.rb', line 63 def self.cur() return @@cur end |
.init ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/vimamsa/search_replace.rb', line 67 def self.init() vma.kbd.add_minor_mode("grep", :grep, :command) if cnf.experimental? bindkey "grep shift!", [:grep_apply_changes, proc { if vma.buf.module.class == Grep vma.buf.module.apply_changes else debug vma.buf.module, 2 ("ERROR") end }, "Write changes edited in grep buffer to the underlying file"] end vma.kbd.add_minor_mode("bmgr", :buf_mgr, :command) reg_act(:grep_buffer, proc { Grep.new.run }, "Grep current buffer") reg_act(:grep_refresh, proc { if vma.buf.module.class == Grep vma.buf.module.refresh end }, "Refresh current Grep buffer") bindkey "C , g", :grep_buffer bindkey "grep ctrl-r", :grep_refresh end |
Instance Method Details
#apply_changes ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/vimamsa/search_replace.rb', line 92 def apply_changes() b = @orig_buf gb = vma.buf changed = 0 gb.lines.each { |x| if m = x.match(/(\d+):(.*)/m) lineno = m[1].to_i newl = m[2] r = @orig_buf.line_range(lineno - 1, 1) old = @orig_buf[r.first..r.last] if old != newl @orig_buf.replace_range(r, newl) changed += 1 end end } ("GREP: apply changes, #{changed} changed lines") end |
#callback(search_str, b = nil) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/vimamsa/search_replace.rb', line 140 def callback(search_str, b = nil) debug "grep_cur_buffer(search_str)" @r = Regexp.new(Regexp.escape(search_str), Regexp::IGNORECASE) fpath = "" fpath = vma.buf.pathname..to_s + ":" if vma.buf.pathname @search_str = search_str @orig_buf = vma.buf @buf = create_new_buffer("", "grep") @buf.default_mode = :grep @buf.module = self vma.kbd.set_mode(:grep) @buf.line_action_handler = proc { |lineno| debug "GREP HANDLER:#{lineno}" jumpto = @grep_matches[lineno] if jumpto.class == Integer vma.buffers.set_current_buffer_by_id(@orig_buf.id) @orig_buf.jump_to_line(jumpto) end } set_buf_contents end |
#refresh ⇒ Object
116 117 118 |
# File 'lib/vimamsa/search_replace.rb', line 116 def refresh set_buf_contents end |
#run ⇒ Object
166 167 168 169 |
# File 'lib/vimamsa/search_replace.rb', line 166 def run() callb = self.method("callback") gui_one_input_action("Grep", "Search:", "grep", callb) end |
#set_buf_contents ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/vimamsa/search_replace.rb', line 120 def set_buf_contents() lines = @orig_buf.split("\n") @grep_matches = [] res_str = "" lines.each_with_index { |l, i| if @r.match(l) res_str << "#{i + 1}:" # ind = scan_indexes(l, r) res_str << "#{l}\n" @grep_matches << i + 1 # Lines start from index 1 end } @buf.set_content(res_str) Gui.highlight_match(@buf, @search_str, color: cnf.match.highlight.color!) end |