Module: VER::Methods::SearchAndReplace

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

Constant Summary collapse

HIGHLIGHT =
Search::HIGHLIGHT
TAG =
:search_and_replace

Class Method Summary collapse

Class Method Details

.answer_to_regex(answer) ⇒ Object



55
56
57
58
# File 'lib/ver/methods/search_and_replace.rb', line 55

def answer_to_regex(answer)
  answer << '/' unless answer =~ /\/[eimnosux]*$/
  eval("/#{answer}")
end

.enter(buffer, old_mode, new_mode) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/ver/methods/search_and_replace.rb', line 13

def enter(buffer, old_mode, new_mode)
  pattern = buffer.store(self, :pattern)
  buffer.tag_all_matching(TAG, pattern, HIGHLIGHT)
  from, to = buffer.tag_nextrange(TAG, 'insert + 1 chars', 'end')
  Search.go(buffer, from)

  buffer.message 'Replace occurence (y)es (n)o (a)ll (q)uit'
end

.last_pattern(buffer) ⇒ Object



22
23
24
25
# File 'lib/ver/methods/search_and_replace.rb', line 22

def last_pattern(buffer)
  pattern = buffer.store(self, :pattern)
  pattern.inspect[1..-1] if pattern
end

.leave(buffer, old_mode, new_mode) ⇒ Object



9
10
11
# File 'lib/ver/methods/search_and_replace.rb', line 9

def leave(buffer, old_mode, new_mode)
  buffer.tag_remove(TAG, 1.0, :end)
end

.next(buffer) ⇒ Object



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

def next(buffer)
  from, to = buffer.tag_nextrange(TAG, 'insert + 1 chars', 'end')
  Search.go(buffer, from)

  buffer.message 'Replace occurence (y)es (n)o (a)ll (q)uit'
end

.prev(buffer) ⇒ Object



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

def prev(buffer)
  from, to = buffer.tag_prevrange(TAG, 'insert', '1.0')
  Search.go(buffer, from)

  buffer.message 'Replace occurence (y)es (n)o (a)ll (q)uit'
end

.query(buffer) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ver/methods/search_and_replace.rb', line 27

def query(buffer)
  question = 'Replace pattern: /'
  value = last_pattern(buffer).to_s

  buffer.ask question, value: value do |answer, action|
    case action
    when :modified
      begin
        regexp = answer_to_regex(answer)
        Search.incremental(buffer, regexp)
        buffer.warn ''
        buffer.message("=> #{regexp.inspect}")
      rescue RegexpError, SyntaxError => ex
        buffer.warn(ex.message)
      end
    when :attempt
      begin
        regexp = answer_to_regex(answer)
        buffer.message("=> #{regexp.inspect}")
        query_attempt(buffer, regexp)
        :abort
      rescue RegexpError, SyntaxError => ex
        buffer.warn(ex.message)
      end
    end
  end
end

.query_attempt(buffer, pattern) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ver/methods/search_and_replace.rb', line 60

def query_attempt(buffer, pattern)
  question = "Replace %p with: " % [pattern]
  value = buffer.store(self, :replacement)
  buffer.ask question, value: value do |replacement, action|
    case action
    when :attempt
      query_done(buffer, pattern, replacement)
      :abort
    end
  end
end

.query_done(buffer, pattern, replacement) ⇒ Object



72
73
74
75
76
# File 'lib/ver/methods/search_and_replace.rb', line 72

def query_done(buffer, pattern, replacement)
  buffer.store(self, :pattern, pattern)
  buffer.store(self, :replacement, replacement)
  buffer.minor_mode(:control, :search_and_replace)
end

.replace_all(buffer) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ver/methods/search_and_replace.rb', line 100

def replace_all(buffer)
  replacement = buffer.store(self, :replacement)
  from, to = buffer.tag_nextrange(TAG, 'insert', 'end')
  return unless from

  Undo.record buffer do |record|
    begin
      record.replace(from, to, replacement)
      buffer.mark_set(:insert, from)
      from, to = buffer.tag_nextrange(TAG, 'insert + 1 chars', 'end')
    end while from
  end

  buffer.minor_mode(:search_and_replace, :control)
end

.replace_once(buffer) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ver/methods/search_and_replace.rb', line 78

def replace_once(buffer)
  begin
    from, to = buffer.tag_nextrange(TAG, 'insert', 'end')
    return unless from && to
  rescue => ex
    if ex.message.start_with?('bad text index ""')
      buffer.minor_mode(:search_and_replace, :control)
      buffer.message 'No more matches'
      return
    else
      raise(ex)
    end
  end

  buffer.replace(from, to, buffer.store(self, :replacement))
  buffer.tag_all_matching(TAG, buffer.store(self, :pattern), HIGHLIGHT)
  from, to = buffer.tag_nextrange(TAG, 'insert + 1 chars', 'end')
  Search.go(buffer, from)

  buffer.message 'Replace occurence (y)es (n)o (a)ll (q)uit'
end