lib/ver/methods/delete.rb in ver-2009.12.14 vs lib/ver/methods/delete.rb in ver-2010.02
- old
+ new
@@ -1,87 +1,102 @@
module VER
module Methods
module Delete
- def change_motion(motion, count = 1)
- delete_motion(motion, count)
- start_insert_mode
+ module_function
+
+ def change_motion(text, motion, count = text.prefix_count)
+ delete_motion(text, motion, count)
+ text.minor_mode(:control, :insert)
end
# Given a +motion+, this method will execute a virtual movement with the
# +count+ argument and [delete] the corresponding indices.
#
# @param [Symbol String] motion name of a method acceptable for [virtual_movement]
# @param [#to_i] count
#
# @see delete
- # @see VER::Move#virtual_movement
- def delete_motion(motion, count = 1)
- delete(*virtual_movement(motion, count))
+ # @see VER::Move::virtual_movement
+ def delete_motion(text, motion, count = text.prefix_count)
+ movement = Move.virtual(text, motion, count)
+ delete(text, *movement)
end
# Given a +motion+, this method will execute a virtual movement with the
# +count+ argument and [kill] the corresponding indices.
#
# @param [Symbol String] motion name of a method acceptable for [virtual_movement]
# @param [#to_i] count
#
# @see kill
# @see VER::Move#virtual_movement
- def kill_motion(motion, count = 1)
- kill(*virtual_movement(motion, count))
+ def kill_motion(text, motion, count = text.prefix_count)
+ movement = Move.virtual(text, motion, count)
+ kill(text, *movement)
end
+ # [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.
+ def change_word_right_end(text, count = text.prefix_count)
+ index = Move.index_at_word_right_end(text, count)
+ delete(text, :insert, index + 1)
+ text.minor_mode(:control, :insert)
+ end
+
# Delete current line and upto +count+ subsequent lines.
#
# @param [#to_i] count Number of lines to delete
#
# @see delete
# @see kill_line
- def delete_line(count = 1)
- from = index('insert linestart')
+ def delete_line(text, count = text.prefix_count)
count = count.abs - 1
- to = index("#{from.y + count}.#{from.x} lineend + 1 char")
- delete(from, to)
+ from = text.index('insert linestart')
+ to = "#{from.y + count}.#{from.x} lineend + 1 char"
+ delete(text, from, text.index(to))
end
# Copy current line and upto +count+ subsequent lines and delete them.
#
# @param [#to_i] count Number of lines to kill
#
# @see kill
# @see delete_line
- def kill_line(count = 1)
- from = index('insert linestart')
+ def kill_line(text, count = text.prefix_count)
count = count.abs - 1
- to = index("#{from.y + count.to_i}.#{from.x} lineend + 1 char")
- kill(from, to)
+ from = text.index('insert linestart')
+ to = "#{from.y + count.to_i}.#{from.x} lineend + 1 char"
+ kill(text, from, text.index(to))
end
# Wrapper for [kill_line] that starts insert mode after killing +count+
- # lines.
+ # lines. It also doesn't delete the newline.
#
# @param [#to_i] count Number of lines to kill
#
# @see kill_line
- # @see start_insert_mode
- def change_line(count = 1)
- kill_line(count)
- start_insert_mode
+ def change_line(text, count = text.prefix_count)
+ count = count.abs - 1
+ from = text.index('insert linestart')
+ to = "#{from.y + count}.#{from.x} lineend"
+ kill(text, from, text.index(to))
+ text.minor_mode(:control, :insert)
end
# Tag and delete all trailing whitespace in the current buffer.
- def delete_trailing_whitespace
- tag_all_trailing_whitespace
- execute :delete, *tag_ranges('invalid.trailing-whitespace').flatten
+ def delete_trailing_whitespace(text)
+ ranges = text.tag_ranges('invalid.trailing-whitespace').flatten
+ text.execute(:delete, *ranges) unless ranges.empty?
end
# Delete text between +indices+
- def delete(*indices)
+ def delete(text, *indices)
indices_size = indices.size
return if indices_size == 0
- undo_record do |record|
+ Undo.record text do |record|
if indices_size == 1
record.delete(indices.first)
else
indices.each_slice(2) do |from, to|
next if from == to
@@ -94,18 +109,19 @@
# Copy text between +indices+ and delete it.
#
# @param [Array<Text::Index, String, Symbol>] indices
# one or more indices within the buffer, must be an even number of
# indices if more than one.
- def kill(*indices)
+ def kill(text, *indices)
if indices.size > 2
- deleted = indices.each_slice(2).map{|left, right| get(left, right) }
+ deleted = indices.each_slice(2).map{|left, right|
+ text.get(left, right) }
else
- deleted = get(*indices)
+ deleted = text.get(*indices)
end
- copy(deleted)
- delete(*indices)
+ Clipboard.copy(text, deleted)
+ delete(text, *indices)
end
end
end
end