Class: VER::Text::Selection::Char

Inherits:
VER::Text::Selection show all
Defined in:
lib/ver/text/selection/char.rb

Instance Method Summary collapse

Instance Method Details

#eachObject

yields the range



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ver/text/selection/char.rb', line 10

def each
  return Enumerator.new(self, :each) unless block_given?

  each_range do |range|
    fy, fx, ty, tx = *range.first, *range.last

    if fy == ty
      yield fy, fx, ty, tx
    elsif (ty - fy) == 1
      efy, efx = *buffer.index("#{fy}.#{fx} lineend")
      sty, stx = *buffer.index("#{ty}.#{tx} linestart")
      yield fy, fx, efy, efx
      yield sty, stx, ty, tx
    else
      efy, efx = *buffer.index("#{fy}.#{fx} lineend")
      yield fy, fx, efy, efx

      ((fy + 1)...ty).each do |y|
        sy, sx = *buffer.index("#{y}.0 linestart")
        ey, ex = *buffer.index("#{y}.0 lineend")
        yield sy, sx, ey, ex
      end

      sty, stx = *buffer.index("#{ty}.#{tx} linestart")
      yield sty, stx, ty, tx
    end
  end
end

#mode_nameObject



5
6
7
# File 'lib/ver/text/selection/char.rb', line 5

def mode_name
  :select_char
end

#refreshObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ver/text/selection/char.rb', line 39

def refresh
  return unless @refresh
  start  = anchor.index
  insert = buffer.at_insert
  clear

  if insert > start
    add(start, insert + '1 chars')
  else
    add(insert, start + '1 chars')
  end
end

#replace_with_clipboardObject

we assume char selection is always continuous, so no need to check the ranges.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ver/text/selection/char.rb', line 54

def replace_with_clipboard
  return unless content = Clipboard.dwim

  start = buffer.index('sel.first')

  case content
  when Array
    replace(content.join("\n"))
  when String
    replace(content)
  else
    raise "Unknown kind of clipboard content: %p" % [content]
  end

  finish
  buffer.insert = start
end

#replace_with_string(string, expand) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ver/text/selection/char.rb', line 72

def replace_with_string(string, expand)
  insert = buffer.index(:insert)
  anchor = self.anchor.index

  from, to = buffer.index('sel.first'), buffer.index('sel.last')

  buffer.undo_record do |record|
    if expand
      length = buffer.count(from, to, :displaychars)
      record.replace(from, to, string * length)
    else
      record.replace(from, to, string)
    end
  end

  self.anchor.index = anchor
  refresh
end