Module: VER::Methods::Search

Defined in:
lib/ver/methods/search.rb

Constant Summary collapse

HIGHLIGHT =
{
  foreground: '#fff',
  background: '#660',
}
TAG =
:search
CHAR_OPPOSITES =
{
  char_left: :char_right,
  char_right: :char_left,
  till_char_left: :till_char_right,
  till_char_right: :till_char_left,
}

Class Method Summary collapse

Class Method Details

.again(buffer) ⇒ Object



223
224
225
226
227
# File 'lib/ver/methods/search.rb', line 223

def again(buffer)
  return unless needle = buffer.store(self, :last)
  tag(buffer, needle)
  self.next(buffer)
end

.again_char(buffer, count = buffer.prefix_count) ⇒ Object

Repeat the last char_left, char_right, till_char_left, or till_char_right search count times.



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/ver/methods/search.rb', line 196

def again_char(buffer, count = buffer.prefix_count)
  if name = buffer.store(self, :char_search)
    if regexp = buffer.store(self, :char_search_regexp)
      send("#{name}!", buffer, regexp, count)
    else
      buffer.warn "Regexp missing, how weird!"
    end
  else
    buffer.warn "No previous char search, can't repeat."
  end
end

.again_char_opposite(buffer, count = buffer.prefix_count) ⇒ Object

Repeat the last char_left, char_right, till_char_left, or till_char_right search count times in the opposite direction.



210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/ver/methods/search.rb', line 210

def again_char_opposite(buffer, count = buffer.prefix_count)
  if regexp = buffer.store(self, :char_search_regexp)
    if name = buffer.store(self, :char_search)
      opposite = CHAR_OPPOSITES.fetch(name)
      send("#{opposite}!", buffer, regexp, count)
    else
      buffer.warn "No previous char search, can't repeat."
    end
  else
    buffer.warn "Regexp missing, how weird!"
  end
end

.center(buffer, index) ⇒ Object



241
242
243
244
# File 'lib/ver/methods/search.rb', line 241

def center(buffer, index)
  return unless index
  Control.cursor_vertical_center(buffer, index)
end

.char_common(buffer, name, action, count) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/ver/methods/search.rb', line 130

def char_common(buffer, name, action, count)
  buffer.major_mode.read 1 do |event|
    regexp = Regexp.new(Regexp.escape(event.unicode))
    buffer.store(self, :char_search, name)
    buffer.store(self, :char_search_regexp, regexp)
    send(action, buffer, regexp, count)
  end
end

.char_left(buffer, count = buffer.prefix_count) ⇒ Object



166
167
168
169
# File 'lib/ver/methods/search.rb', line 166

def char_left(buffer, count = buffer.prefix_count)
  buffer.message 'Press the character to find to the left'
  char_common(buffer, :char_left, :char_left!, count)
end

.char_left!(buffer, regexp, count) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ver/methods/search.rb', line 171

def char_left!(buffer, regexp, count)
  from, to = 'insert', 'insert linestart'
  counter = 0

  buffer.rsearch_all regexp, from, to do |match, pos, mark|
    buffer.insert = pos
    counter += 1
    return true if counter == count
  end

  false
end

.char_right(buffer, count = buffer.prefix_count) ⇒ Object



139
140
141
142
# File 'lib/ver/methods/search.rb', line 139

def char_right(buffer, count = buffer.prefix_count)
  buffer.message 'Press the character to find to the right'
  char_common(buffer, :char_right, :char_right!, count)
end

.char_right!(buffer, regexp, count) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ver/methods/search.rb', line 144

def char_right!(buffer, regexp, count)
  from, to = 'insert + 1 chars', 'insert lineend'
  counter = 0
  buffer.search_all regexp, from, to do |match, pos, mark|
    buffer.insert = pos
    counter += 1
    return true if counter == count
  end

  false
end

.display_matches_count(buffer) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ver/methods/search.rb', line 95

def display_matches_count(buffer)
  total = buffer.tag_ranges(TAG).size

  if total == 1
    buffer.message "1 match found"
  elsif total > 1
    buffer.message "#{total} matches found"
  else
    buffer.message "No matches found"
  end
end

.first(buffer) ⇒ Object



76
77
78
79
# File 'lib/ver/methods/search.rb', line 76

def first(buffer)
  from, to = buffer.tag_nextrange(TAG, 1.0, :end)
  go(buffer, from)
end

.go(buffer, index) ⇒ Object



235
236
237
238
239
# File 'lib/ver/methods/search.rb', line 235

def go(buffer, index)
  return unless index
  center(buffer, index)
  buffer.insert = index
end

.incremental(buffer, term, force = false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ver/methods/search.rb', line 52

def incremental(buffer, term, force = false)
  needle =
    case term
    when nil, false
      return
    when Regexp
      needle = term
    when String
      return if !force && term.size <= buffer.options.search_incremental_min
      return if term.empty?
      begin
        Regexp.new(term)
      rescue RegexpError, SyntaxError
        Regexp.escape(term)
      end
    else
      raise ArgumentError
    end

  tag(buffer, needle)
  from, to = buffer.tag_nextrange(TAG, 1.0, :end)
  buffer.see(from) if from
end

.jump(buffer, needle) ⇒ Object



19
20
21
22
# File 'lib/ver/methods/search.rb', line 19

def jump(buffer, needle)
  tag(buffer, needle)
  self.next(buffer)
end

.last(buffer) ⇒ Object



81
82
83
84
# File 'lib/ver/methods/search.rb', line 81

def last(buffer)
  from, to = tag_prevrange(TAG, :end, 1.0)
  go(buffer, from)
end

.next(buffer, count = buffer.prefix_count) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/ver/methods/search.rb', line 86

def next(buffer, count = buffer.prefix_count)
  count.times do
    from, to = buffer.tag_nextrange(TAG, 'insert + 1 chars', 'end')
    go(buffer, from)
  end

  display_matches_count(buffer)
end

.next_word_under_cursor(buffer) ⇒ Object



116
117
118
119
120
121
# File 'lib/ver/methods/search.rb', line 116

def next_word_under_cursor(buffer)
  word = buffer.get('insert wordstart', 'insert wordend')
  return if word.squeeze == ' ' # we don't want to match space
  tag(buffer, word)
  self.next(buffer)
end

.prev(buffer, count = buffer.prefix_count) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/ver/methods/search.rb', line 107

def prev(buffer, count = buffer.prefix_count)
  count.times do
    from, to = buffer.tag_prevrange(TAG, 'insert - 1 chars', '1.0')
    go(buffer, from)
  end

  display_matches_count(buffer)
end

.prev_word_under_cursor(buffer) ⇒ Object



123
124
125
126
127
128
# File 'lib/ver/methods/search.rb', line 123

def prev_word_under_cursor(buffer)
  word = buffer.get('insert wordstart', 'insert wordend')
  return if word.squeeze == ' ' # we don't want to match space
  tag(buffer, word)
  prev(buffer)
end

.remove(buffer) ⇒ Object Also known as: clear



24
25
26
# File 'lib/ver/methods/search.rb', line 24

def remove(buffer)
  buffer.tag_remove(TAG, 1.0, :end)
end

.status_common(buffer, question) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ver/methods/search.rb', line 38

def status_common(buffer, question)
  buffer.ask question do |answer, action|
    case action
    when :modified
      incremental(buffer, answer)
    when :attempt
      incremental(buffer, answer, force = true)
      prev(buffer)
      yield
      :abort
    end
  end
end

.status_next(buffer) ⇒ Object



30
31
32
# File 'lib/ver/methods/search.rb', line 30

def status_next(buffer)
  status_common(buffer, '/'){ self.next(buffer) }
end

.status_prev(buffer) ⇒ Object



34
35
36
# File 'lib/ver/methods/search.rb', line 34

def status_prev(buffer)
  status_common(buffer, '?'){ prev(buffer) }
end

.tag(buffer, needle) ⇒ Object



229
230
231
232
233
# File 'lib/ver/methods/search.rb', line 229

def tag(buffer, needle)
  buffer.store(self, :last, needle)
  buffer.tag_all_matching(TAG, needle, HIGHLIGHT)
  buffer.tag_lower(TAG)
end

.till_char_left(buffer, count = buffer.prefix_count) ⇒ Object



184
185
186
187
# File 'lib/ver/methods/search.rb', line 184

def till_char_left(buffer, count = buffer.prefix_count)
  buffer.message 'Press the character to find to the left'
  char_common(buffer, :till_char_left, :till_char_left!, count)
end

.till_char_left!(buffer, regexp, count) ⇒ Object



189
190
191
192
# File 'lib/ver/methods/search.rb', line 189

def till_char_left!(buffer, regexp, count)
  return unless char_left!(buffer, regexp, count)
  buffer.at_insert.next_char
end

.till_char_right(buffer, count = buffer.prefix_count) ⇒ Object



156
157
158
159
# File 'lib/ver/methods/search.rb', line 156

def till_char_right(buffer, count = buffer.prefix_count)
  buffer.message 'Press the character to find to the right'
  char_common(buffer, :till_char_right, :till_char_right!, count)
end

.till_char_right!(buffer, regexp, count) ⇒ Object



161
162
163
164
# File 'lib/ver/methods/search.rb', line 161

def till_char_right!(buffer, regexp, count)
  return unless char_right!(buffer, regexp, count)
  buffer.at_insert.prev_char
end