# This provides a way to run a function on all the cursors, one after another. This maintains
# all the cursors and then calls the function with one cursor at a time, with the view's
# selection state set to just that one cursor. So any calls to run_command within the function
# will operate on only that one cursor.
# The called function is supposed to return a new cursor position or None, in which case value
# is taken from the view itself.
# After the function is run on all the cursors, the view's multi-cursor state is restored with
# new values for the cursor.
def for_each_cursor(self, function, *args, **kwargs):
view = self.view
selection = view.sel()
# copy cursors into proper regions which sublime will manage while we potentially edit the
# buffer and cause things to move around
key = "tmp_cursors"
cursors = [c for c in selection]
view.add_regions(key, cursors, "tmp", "", sublime.HIDDEN)
# run the command passing in each cursor and collecting the returned cursor
for i in range(len(cursors)):
regions = view.get_regions(key)
cursor = regions[i]
cursor = function(cursor, *args, **kwargs)
if cursor is not None:
# update the cursor in its slot
regions[i] = cursor
view.add_regions(key, regions, "tmp", "", sublime.HIDDEN)
# restore the cursors
Users browsing this forum: stef-k and 4 guests