Module: VER::Text::Position

Includes:
Comparable
Included in:
Index, Mark
Defined in:
lib/ver/text/position.rb

Overview

A Position is any kind of index in a Tk::Text widget, it may be a Mark, Insert, End, etc. The Position relies on a buffer property being present, which should be responding to the methods of Tk::Text that take at least one index.

Instance Method Summary collapse

Instance Method Details

#+(arg) ⇒ Object



221
222
223
224
225
226
227
228
# File 'lib/ver/text/position.rb', line 221

def +(arg)
  case arg
  when Integer
    buffer.index("#{self} + #{arg} chars")
  else
    buffer.index("#{self} + #{arg}")
  end
end

#-(arg) ⇒ Object



230
231
232
233
234
235
236
237
# File 'lib/ver/text/position.rb', line 230

def -(arg)
  case arg
  when Integer
    buffer.index("#{self} - #{arg} chars")
  else
    buffer.index("#{self} - #{arg}")
  end
end

#<=>(other) ⇒ Fixnum

Used for Comparable. Returns 1 if other is after this position. Returns -1 if +other is before this position. Returns 0 if they are equal.

Parameters:

  • other (#to_tcl)

    another index to compare against

Returns:

  • (Fixnum)


33
34
35
36
37
# File 'lib/ver/text/position.rb', line 33

def <=>(other)
  return  1 if compare('>',  other)
  return -1 if compare('<',  other)
  return  0 if compare('==', other)
end

#bboxObject



10
11
12
# File 'lib/ver/text/position.rb', line 10

def bbox
  buffer.bbox(self)
end

#change_line(count = buffer.prefix_count) ⇒ Object

Kill contents of the line at position, switch from control to insert mode. Always leaves an empty line



42
43
44
45
# File 'lib/ver/text/position.rb', line 42

def change_line(count = buffer.prefix_count)
  kill_line
  buffer.minor_mode(:control, :insert)
end

#compare(op, other) ⇒ Boolean

Compare relationship with other index using op.

Parameters:

  • op (#to_tcl)

    one of <, <=, ==, >=, >, or !=

  • other (#to_tcl)

    another index to compare against

Returns:

  • (Boolean)

    whether relationship is satisfied.



21
22
23
# File 'lib/ver/text/position.rb', line 21

def compare(op, other)
  buffer.compare(self, op, other)
end

#copy_line(count = buffer.prefix_count) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ver/text/position.rb', line 47

def copy_line(count = buffer.prefix_count)
  from = "#{self} linestart"

  to =
    case count
    when 0, 1; "#{self} lineend + 1 chars"
    else     ; "#{self} + #{count - 1} lines lineend + 1 chars"
    end

  buffer.range(buffer.index(from), buffer.index(to)).copy
end

#decrease_number(count = buffer.prefix_count) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/ver/text/position.rb', line 106

def decrease_number(count = buffer.prefix_count)
  return unless result = formatted_number(-count)
  head, tail, number = *result
  buffer.replace(
    "#{self} - #{head.size} chars",
    "#{self} + #{tail.size} chars",
    number
  )
end

#delete(*indices) ⇒ Object

Delete this and any other given indices from the buffer.



60
61
62
# File 'lib/ver/text/position.rb', line 60

def delete(*indices)
  buffer.delete(self, *indices)
end

#delete_line(count = buffer.prefix_count) ⇒ Object



64
65
66
# File 'lib/ver/text/position.rb', line 64

def delete_line(count = buffer.prefix_count)
  buffer.delete("#{self} linestart", "#{self} + #{count - 1} lines lineend + 1 chars")
end

#delta(other) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/ver/text/position.rb', line 72

def delta(other)
  line_diff = other.line - line

  if line_diff == 0
    (other.char - char).abs
  else
    line_diff.abs
  end
end

#dlineinfoObject



68
69
70
# File 'lib/ver/text/position.rb', line 68

def dlineinfo
  buffer.dlineinfo(self)
end

#dump(*options) ⇒ Object



82
83
84
# File 'lib/ver/text/position.rb', line 82

def dump(*options)
  buffer.dump(*options, self)
end

#formatted_binary(number) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/ver/text/position.rb', line 126

def formatted_binary(number)
  if number > 0
    '%#b' % number
  elsif number == 0
    '0b0'
  else
    '-0b%b' % number.abs
  end
end

#formatted_exponential(number, precicion) ⇒ Object



136
137
138
# File 'lib/ver/text/position.rb', line 136

def formatted_exponential(number, precicion)
  "%-.#{precicion}e" % number
end

#formatted_float(number, precicion) ⇒ Object



140
141
142
# File 'lib/ver/text/position.rb', line 140

def formatted_float(number, precicion)
  "%-.#{precicion}f" % number
end

#formatted_hex(number) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/ver/text/position.rb', line 116

def formatted_hex(number)
  if number > 0
    '%#x' % number
  elsif number == 0
    '0x0'
  else
    '-0x%x' % number.abs
  end
end

#formatted_number(count) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/ver/text/position.rb', line 154

def formatted_number(count)
  head = buffer.get("#{self} linestart", self)[/\S*$/]
  tail = buffer.get(self, "#{self} lineend")[/^\S*/]

  number =
    case chunk = head + tail
    when /^[+-]?0x\h+$/
      formatted_hex(Integer(chunk) + count)
    when /^[+-]?0b[01]+$/
      formatted_binary(Integer(chunk) + count)
    when /^[+-]?(\d+\.\d+|\d+)e[+-]?\d+$/
      precicion = $1[/\.(\d+)/, 1].to_s.size
      formatted_exponential(Float(chunk) + count, precicion)
    when /^[+-]?\d+\.(\d+)/
      formatted_float(Float(chunk) + count, $1.size)
    when /^[+-]?([1-9]\d*|0)$/
      "%-d" % [Integer(chunk) + count]
    when /^[+-]?0\d+$/
      formatted_octal(Integer(chunk) + count)
    else
      return
    end

  return head, tail, number
end

#formatted_octal(number) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/ver/text/position.rb', line 144

def formatted_octal(number)
  if number > 0
    '%#o' % number
  elsif number == 0
    '00'
  else
    '-0%o' % number.abs
  end
end

#get(from = nil, to = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/ver/text/position.rb', line 86

def get(from = nil, to = nil)
  if from && to
    buffer.get("#{self} #{from}", "#{self} #{to}")
  elsif from
    buffer.get("#{self} #{from}")
  else
    buffer.get(self)
  end
end

#increase_number(count = buffer.prefix_count) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/ver/text/position.rb', line 96

def increase_number(count = buffer.prefix_count)
  return unless result = formatted_number(count)
  head, tail, number = *result
  buffer.replace(
    "#{self} - #{head.size} chars",
    "#{self} + #{tail.size} chars",
    number
  )
end

#insert(*args) ⇒ Object



184
185
186
# File 'lib/ver/text/position.rb', line 184

def insert(*args)
  buffer.insert(self, *args)
end

#inspectObject



180
181
182
# File 'lib/ver/text/position.rb', line 180

def inspect
  index.inspect
end

#kill_line(count = buffer.prefix_count) ⇒ Object



188
189
190
191
# File 'lib/ver/text/position.rb', line 188

def kill_line(count = buffer.prefix_count)
  copy_line(count)
  delete_line(count)
end

#lineendObject



201
202
203
# File 'lib/ver/text/position.rb', line 201

def lineend
  buffer.index("#{self} lineend")
end

#linestartObject



197
198
199
# File 'lib/ver/text/position.rb', line 197

def linestart
  buffer.index("#{self} linestart")
end

#seeObject



193
194
195
# File 'lib/ver/text/position.rb', line 193

def see
  buffer.see(self)
end

#tag_namesObject



209
210
211
# File 'lib/ver/text/position.rb', line 209

def tag_names
  buffer.tag_names(self)
end

#tagsObject



205
206
207
# File 'lib/ver/text/position.rb', line 205

def tags
  buffer.tags(self)
end

#to_aObject



213
214
215
# File 'lib/ver/text/position.rb', line 213

def to_a
  index.to_a
end

#toggle_case!(count = buffer.prefix_count) ⇒ Object



217
218
219
# File 'lib/ver/text/position.rb', line 217

def toggle_case!(count = buffer.prefix_count)
  buffer.range(self, self + "#{count} displaychars").toggle_case!
end