Class: VER::Text::Mark

Inherits:
Struct
  • Object
show all
Includes:
Position
Defined in:
lib/ver/text/mark.rb

Direct Known Subclasses

End, Insert

Constant Summary collapse

MATCHING_BRACE_RIGHT =
{
  '(' => ')',
  '{' => '}',
  '[' => ']',
  '<' => '>',
}
MATCHING_BRACE_LEFT =
MATCHING_BRACE_RIGHT.invert

Constants inherited from Struct

Struct::CACHE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Position

#+, #-, #<=>, #bbox, #change_line, #compare, #copy_line, #decrease_number, #delete_line, #delta, #dlineinfo, #dump, #formatted_binary, #formatted_exponential, #formatted_float, #formatted_hex, #formatted_number, #formatted_octal, #get, #increase_number, #kill_line, #lineend, #linestart, #see, #tag_names, #tags, #to_a, #toggle_case!

Methods inherited from Struct

new

Constructor Details

#initialize(buffer, name, index = nil) ⇒ Mark

Returns a new instance of Mark.



14
15
16
17
# File 'lib/ver/text/mark.rb', line 14

def initialize(buffer, name, index = nil)
  self.buffer, self.name = buffer, name
  self.index = index unless index.nil?
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer

Returns:

  • (Object)

    the current value of buffer



3
4
5
# File 'lib/ver/text/mark.rb', line 3

def buffer
  @buffer
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



3
4
5
# File 'lib/ver/text/mark.rb', line 3

def name
  @name
end

Instance Method Details

#<<(string) ⇒ Object



176
177
178
179
# File 'lib/ver/text/mark.rb', line 176

def <<(string)
  buffer.insert(self, string)
  self
end

#backward_jump(count) ⇒ Object



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/ver/text/mark.rb', line 529

def backward_jump(count)
  count.times do
    original_type = type = yield(get)
    changed = 0

    begin
      original_pos = index
      buffer.execute_only(:mark, :set, self, "#{self} - 1 chars")
      break if original_pos == index

      type = yield(get)
      changed += 1 if type != original_type
      original_type = type
    end until changed > 0 && type != :space

    type = yield(get('- 1 chars'))

    while type == original_type
      original_pos = index
      buffer.execute_only(:mark, :set, self, "#{self} - 1 chars")
      break if original_pos == index

      type = yield(get('- 1 chars'))
    end
  end
rescue => ex
  VER.error(ex)
end

#change_at(motion, *args) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/ver/text/mark.rb', line 162

def change_at(motion, *args)
  if motion.respond_to?(:call)
    motion.call(buffer, *args)
  else
    send(motion, *args)
  end

  buffer.minor_mode(:control, :insert)
end

#change_next_word_endObject

word_right_end goes to the last character, that is, the insert mark is between the second to last and last character. This means that the range to delete is off by one, account for it here.



155
156
157
158
159
160
# File 'lib/ver/text/mark.rb', line 155

def change_next_word_end
  range = buffer.at_insert.virtual(&:next_word_end)
  range.last += '1 chars'
  range.kill
  buffer.minor_mode(:control, :insert)
end

#changing(motion, *args) ⇒ Object



121
122
123
# File 'lib/ver/text/mark.rb', line 121

def changing(motion, *args)
  virtual_motion(motion, *args).change
end

#charInteger

Number of characters from the start of the line. Counting characters starts at zero.

Returns:

  • (Integer)

    Number of characters from the line-start.



38
39
40
# File 'lib/ver/text/mark.rb', line 38

def char
  index.char
end

#char=(char) ⇒ Object



54
55
56
# File 'lib/ver/text/mark.rb', line 54

def char=(char)
  set("#{line}.#{char}")
end

#chunk_char_type(char) ⇒ Object



468
469
470
471
472
473
474
475
# File 'lib/ver/text/mark.rb', line 468

def chunk_char_type(char)
  case char
  when /\S/; :nonspace
  when /\s/; :space
  else
    Kernel.raise "No matching chunk type for: %p " % [char]
  end
end

#copying(motion, *args) ⇒ Object



117
118
119
# File 'lib/ver/text/mark.rb', line 117

def copying(motion, *args)
  virtual_motion(motion, *args).copy
end

#delete(amount) ⇒ Object



181
182
183
184
185
186
187
188
189
190
# File 'lib/ver/text/mark.rb', line 181

def delete(amount)
  case amount
  when Integer
    buffer.delete(self, self + "#{amount} chars")
  when Float, String, Symbol
    buffer.delete(self, amount)
  else
    raise ArgumentError
  end
end

#deleting(motion, *args) ⇒ Object



132
133
134
# File 'lib/ver/text/mark.rb', line 132

def deleting(motion, *args)
  virtual_motion(motion, *args).delete
end

#down_nonblank(count = buffer.prefix_count) ⇒ Object



337
338
339
340
# File 'lib/ver/text/mark.rb', line 337

def down_nonblank(count = buffer.prefix_count)
  offset = (count - 1).abs
  go_first_nonblank(buffer.index("insert + #{offset} lines"))
end

#encoding_rot13(motion, *args) ⇒ Object



148
149
150
# File 'lib/ver/text/mark.rb', line 148

def encoding_rot13(motion, *args)
  virtual_motion(motion, *args).encode_rot13!
end

#end_of_buffer(count = nil) ⇒ Object



354
355
356
357
358
359
360
# File 'lib/ver/text/mark.rb', line 354

def end_of_buffer(count = nil)
  if count
    set("#{count}.0")
  else
    set(:end)
  end
end

#end_of_sentence(count = buffer.prefix_count) ⇒ Object



396
397
398
399
400
401
402
# File 'lib/ver/text/mark.rb', line 396

def end_of_sentence(count = buffer.prefix_count)
  buffer.search_all(/\.\s/, self) do |match, from, to|
    set("#{to} - 1 chars")
    count -= 1
    return if count <= 0
  end
end

#first_line(line = buffer.prefix_count) ⇒ Object



296
297
298
# File 'lib/ver/text/mark.rb', line 296

def first_line(line = buffer.prefix_count)
  go_first_nonblank(buffer.index("#{line}.0"))
end

#forward_jump(count) ⇒ Object



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/ver/text/mark.rb', line 477

def forward_jump(count)
  count.times do
    original_type = type = yield(get)
    changed = 0

    begin
      original_pos = index
      buffer.execute_only(:mark, :set, self, "#{self} + 1 chars")
      break if original_pos == index

      type = yield(get)
      changed += 1 if type != original_type
      original_type = type
    end until changed > 0 && type != :space
  end
rescue => ex
  VER.error(ex)
end

#forward_jump_end(count) ⇒ Object



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/ver/text/mark.rb', line 496

def forward_jump_end(count)
  offset = 1
  last = buffer.index('end')

  count.times do
    pos  = buffer.index("#{self} + #{offset} chars")

    return if pos == last

    type = yield(pos.get)

    while type == :space
      offset += 1
      pos = buffer.index("#{self} + #{offset} chars")
      break if pos == last
      type = yield(pos.get)
    end

    lock = type

    while type == lock && type != :space
      offset += 1
      pos = buffer.index("#{self} + #{offset} chars")
      break if pos == last
      type = yield(pos.get)
    end
  end

  set("#{self} + #{offset - 1} chars")
rescue => ex
  VER.error(ex)
end

#go_char(char = buffer.prefix_count(0)) ⇒ Object



318
319
320
# File 'lib/ver/text/mark.rb', line 318

def go_char(char = buffer.prefix_count(0))
  set("#{self} linestart + #{char} display chars")
end

#go_first_nonblank(index) ⇒ Object



308
309
310
311
312
313
314
315
316
# File 'lib/ver/text/mark.rb', line 308

def go_first_nonblank(index)
  line = index.get('linestart', 'lineend')

  if first_nonblank = (line =~ /\S/)
    set("#{index.line}.#{first_nonblank}")
  else
    set("#{index.line}.0")
  end
end

#go_line_char(line = nil, char = nil) ⇒ Object



322
323
324
# File 'lib/ver/text/mark.rb', line 322

def go_line_char(line = nil, char = nil)
  set("#{line || self.line}.#{char || self.char}")
end

#go_percent(count = buffer.prefix_count(nil)) ⇒ Object

Go to count percentage in the file, on the first non-blank in the line linewise. To compute the new line number this formula is used: (count * number-of-lines + 99) / 100

Raises:

  • (ArgumentError)


330
331
332
333
334
335
# File 'lib/ver/text/mark.rb', line 330

def go_percent(count = buffer.prefix_count(nil))
  raise ArgumentError unless count
  number_of_lines = buffer.count('1.0', 'end', :lines)
  line = (count * number_of_lines + 99) / 100
  go_first_nonblank(buffer.index("#{line}.0"))
end

#gravityObject



62
63
64
# File 'lib/ver/text/mark.rb', line 62

def gravity
  buffer.mark_gravity(self)
end

#gravity=(direction) ⇒ Object



58
59
60
# File 'lib/ver/text/mark.rb', line 58

def gravity=(direction)
  buffer.mark_gravity(self, direction)
end

#home_of_lineObject

Move to the non-blank character of the line in which insert mark is located.



391
392
393
394
# File 'lib/ver/text/mark.rb', line 391

def home_of_line
  char = get('linestart', 'lineend').index(/\S/) || 0
  self.char = char
end

#indexObject



83
84
85
# File 'lib/ver/text/mark.rb', line 83

def index
  buffer.index(self)
end

#insert(string, *rest) ⇒ Object



172
173
174
# File 'lib/ver/text/mark.rb', line 172

def insert(string, *rest)
  buffer.insert(self, string, *rest)
end

#insert_indented_newlineObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/ver/text/mark.rb', line 208

def insert_indented_newline
  buffer.undo_record do |record|
    indent1 = get('linestart', 'lineend')[/^\s*/]
    record.insert(self, "\n")

    indent2 = get('linestart', 'lineend')[/^\s*/]
    record.replace(
      "#{self} linestart",
      "#{self} linestart + #{indent2.size} chars",
      indent1
    )

    Methods::Control.clean_line(buffer, "#{self} - 1 line", record)
  end
end

#insert_indented_newline_belowObject



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

def insert_indented_newline_below
  line = get('linestart', 'lineend')
  indent = line[/^\s*/]
  set "#{self} lineend"
  self << "\n#{indent}"
end

#insert_newlineObject



200
201
202
203
204
205
206
# File 'lib/ver/text/mark.rb', line 200

def insert_newline
  if buffer.options.autoindent
    insert_indented_newline
  else
    self << "\n"
  end
end

#insert_newline_aboveObject



237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/ver/text/mark.rb', line 237

def insert_newline_above
  if char > 1
    set(self - '1 line')
    insert_newline_below
  else
    buffer.undo_record do |record|
      record.insert("#{self} linestart", "\n")
      set "#{self} - 1 line"
      Methods::Control.clean_line(buffer, "#{self} - 1 line", record)
    end
  end
end

#insert_newline_belowObject



224
225
226
227
228
# File 'lib/ver/text/mark.rb', line 224

def insert_newline_below
  return insert_indented_newline_below if buffer.options.autoindent
  set "#{self} lineend"
  self << "\n"
end

#insert_selectionObject



192
193
194
# File 'lib/ver/text/mark.rb', line 192

def insert_selection
  self << Tk::Selection.get(type: 'UTF8_STRING')
end

#insert_tabObject



196
197
198
# File 'lib/ver/text/mark.rb', line 196

def insert_tab
  self << "\t"
end

#inspectString

Returns representation of #name and #buffer.

Returns:



30
31
32
# File 'lib/ver/text/mark.rb', line 30

def inspect
  "#<VER::Text::Mark %p on %p>" % [name, buffer]
end

#killing(motion, *args) ⇒ Object

if the motion is up-down, we might want to kill whole lines? that’s what vim does, but i don’t find it very intuitive or easy to implement.



128
129
130
# File 'lib/ver/text/mark.rb', line 128

def killing(motion, *args)
  virtual_motion(motion, *args).kill
end

#last_char(count = buffer.prefix_count) ⇒ Object

Move to the end of the line where mark is located.

With count it moves to the end of line count lines below.



365
366
367
# File 'lib/ver/text/mark.rb', line 365

def last_char(count = buffer.prefix_count)
  set("#{self} + #{count - 1} lines lineend")
end

#last_line(line = buffer.prefix_count(:end)) ⇒ Object



300
301
302
303
304
305
306
# File 'lib/ver/text/mark.rb', line 300

def last_line(line = buffer.prefix_count(:end))
  if line == :end
    go_first_nonblank(buffer.index("end - 1 chars"))
  else
    go_first_nonblank(buffer.index("#{line}.0"))
  end
end

#lineInteger

The number of lines from the top of #buffer. Counting lines starts at one.

Returns:

  • (Integer)

    Number of lines from top of buffer.



46
47
48
# File 'lib/ver/text/mark.rb', line 46

def line
  index.line
end

#line=(line) ⇒ Object



50
51
52
# File 'lib/ver/text/mark.rb', line 50

def line=(line)
  set("#{line}.#{char}")
end

#lower_casing(motion, *args) ⇒ Object



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

def lower_casing(motion, *args)
  virtual_motion(motion, *args).lower_case!
end

#matching_brace(count = buffer.prefix_count) ⇒ Object



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/ver/text/mark.rb', line 404

def matching_brace(count = buffer.prefix_count)
  from, to = matching_brace_indices(count)
  index = self.index

  if index < from
    set(from)
  elsif index == from
    set(to)
  elsif index > to
    set(to)
  elsif index == to
    set(from)
  else
    if from.delta(index) < to.delta(index)
      set(from)
    else
      set(to)
    end
  end
end

#matching_brace_indices(count = 1) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/ver/text/mark.rb', line 425

def matching_brace_indices(count = 1)
  needle = Regexp.union(MATCHING_BRACE_RIGHT.to_a.flatten.sort.uniq)
  from = buffer.search_all(needle, self){|match, range| break range }
  return unless from

  opening = from.get

  if closing = MATCHING_BRACE_RIGHT[opening]
    start = from + '1 chars'
    search = buffer.method(:search_all)
  elsif closing = MATCHING_BRACE_LEFT[opening]
    start = from.index
    search = buffer.method(:rsearch_all)
  else
    return
  end

  balance = count
  needle = Regexp.union(opening, closing)
  search.call(needle, start){|match, range|
    case match
    when opening
      balance += 1
    when closing
      balance -= 1
    end

    if balance == 0
      return [from, range].sort
    end
  }
end

#middle_of_lineObject

Move to the middle of the display line. Vim moves to the middle of the screen width… not so useful, but in order to be compatible, do that instead.



372
373
374
375
376
# File 'lib/ver/text/mark.rb', line 372

def middle_of_line
  x, y, width, height, baseline = *buffer.dlineinfo(self)
  middle = width / 2
  set("@#{middle},#{y}")
end

#nextObject



75
76
77
# File 'lib/ver/text/mark.rb', line 75

def next
  buffer.mark_next(self)
end

#next_char(count = buffer.prefix_count) ⇒ Object

Set mark to be count display-chars to the right. Stays on the same line.



252
253
254
255
# File 'lib/ver/text/mark.rb', line 252

def next_char(count = buffer.prefix_count)
  return if self == lineend
  set(self + "#{count} displaychars")
end

#next_chunk(count = buffer.prefix_count) ⇒ Object



261
262
263
# File 'lib/ver/text/mark.rb', line 261

def next_chunk(count = buffer.prefix_count)
  forward_jump(count, &method(:chunk_char_type))
end

#next_chunk_end(count = buffer.prefix_count) ⇒ Object

Jump to the last character of the chunk the insert cursor is over currently.



273
274
275
# File 'lib/ver/text/mark.rb', line 273

def next_chunk_end(count = buffer.prefix_count)
  forward_jump_end(count, &method(:chunk_char_type))
end

#next_line_nonblank(count = buffer.prefix_count) ⇒ Object



346
347
348
# File 'lib/ver/text/mark.rb', line 346

def next_line_nonblank(count = buffer.prefix_count)
  go_first_nonblank(buffer.index("insert + #{count} lines"))
end

#next_word(count = buffer.prefix_count) ⇒ Object



257
258
259
# File 'lib/ver/text/mark.rb', line 257

def next_word(count = buffer.prefix_count)
  forward_jump(count, &method(:word_char_type))
end

#next_word_end(count = buffer.prefix_count) ⇒ Object

Jump to the last character of the word the insert cursor is over currently.



267
268
269
# File 'lib/ver/text/mark.rb', line 267

def next_word_end(count = buffer.prefix_count)
  forward_jump_end(count, &method(:word_char_type))
end

#prev_char(count = buffer.prefix_count) ⇒ Object

Set mark to be count display-chars to the left. Jumps to previous line when on first character of a line.



279
280
281
282
# File 'lib/ver/text/mark.rb', line 279

def prev_char(count = buffer.prefix_count)
  return if self == linestart
  set(self - "#{count} displaychars")
end

#prev_chunk(count = buffer.prefix_count) ⇒ Object



288
289
290
# File 'lib/ver/text/mark.rb', line 288

def prev_chunk(count = buffer.prefix_count)
  backward_jump(count, &method(:chunk_char_type))
end

#prev_line_nonblank(count = buffer.prefix_count) ⇒ Object



342
343
344
# File 'lib/ver/text/mark.rb', line 342

def prev_line_nonblank(count = buffer.prefix_count)
  go_first_nonblank(buffer.index("insert - #{count} lines"))
end

#prev_word(count = buffer.prefix_count) ⇒ Object



284
285
286
# File 'lib/ver/text/mark.rb', line 284

def prev_word(count = buffer.prefix_count)
  backward_jump(count, &method(:word_char_type))
end

#prev_word_end(count = buffer.prefix_count) ⇒ Object



292
293
294
# File 'lib/ver/text/mark.rb', line 292

def prev_word_end(count = buffer.prefix_count)
  set(index_at_word_left_end(count))
end

#previousObject



79
80
81
# File 'lib/ver/text/mark.rb', line 79

def previous
  buffer.mark_previous(self)
end

#set(index) ⇒ Object Also known as: index=



66
67
68
# File 'lib/ver/text/mark.rb', line 66

def set(index)
  buffer.mark_set(self, index)
end

#start_of_bufferObject



350
351
352
# File 'lib/ver/text/mark.rb', line 350

def start_of_buffer
  set('1.0')
end

#start_of_line(alternative = buffer.prefix_arg) ⇒ Object

Move to the beginning of the line in which insert mark is located.

With count it will move to the beginning of the display line, which takes line wraps into account.



382
383
384
385
386
387
388
# File 'lib/ver/text/mark.rb', line 382

def start_of_line(alternative = buffer.prefix_arg)
  if alternative
    set("#{self} display linestart")
  else
    set("#{self} linestart")
  end
end

#to_sString

Returns representation of #name.

Returns:

  • (String)

    representation of #name



25
26
27
# File 'lib/ver/text/mark.rb', line 25

def to_s
  name.to_s
end

#to_tclTk::TclString

Returns tcl representation of #name.

Returns:

  • (Tk::TclString)

    tcl representation of #name



20
21
22
# File 'lib/ver/text/mark.rb', line 20

def to_tcl
  name.to_tcl
end

#toggle_casing(motion, *args) ⇒ Object



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

def toggle_casing(motion, *args)
  virtual_motion(motion, *args).toggle_case!
end

#unsetObject



71
72
73
# File 'lib/ver/text/mark.rb', line 71

def unset
  buffer.mark_unset(self)
end

#upper_casing(motion, *args) ⇒ Object



144
145
146
# File 'lib/ver/text/mark.rb', line 144

def upper_casing(motion, *args)
  virtual_motion(motion, *args).upper_case!
end

#virtual(*args) {|_self, args| ... } ⇒ Object

Create a Range from a “virtual” movement. This means that you pass a block that changes the position of the insert mark, and this method will return a Range consists of the old and new position. The method yields self, so you can use the Symbol#to_proc shortcut as well as predefined blocks or other methods that take the Insert as argument.

Examples:

usage

insert = buffer.at_insert
insert.virtual(&:next_char).delete # delete to next character
insert.virtual{|ins| ins.last_char }.copy # copy until end of line

Yields:

  • (_self, args)

Yield Parameters:



99
100
101
102
103
104
105
# File 'lib/ver/text/mark.rb', line 99

def virtual(*args)
  pos = index
  yield(self, *args)
  mark = index
  set(pos)
  return Range.new(buffer, *[pos, mark].sort)
end

#virtual_motion(motion, *args) ⇒ Object



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

def virtual_motion(motion, *args)
  virtual do |mark|
    if motion.respond_to?(:call)
      motion.call(buffer, *args)
    else
      mark.send(motion, *args)
    end
  end
end

#word_char_type(char) ⇒ Object



458
459
460
461
462
463
464
465
466
# File 'lib/ver/text/mark.rb', line 458

def word_char_type(char)
  case char
  when /\w/; :word
  when /\S/; :special
  when /\s/; :space
  else
    Kernel.raise "No matching char type for: %p" % [char]
  end
end