Skip to content

Instantly share code, notes, and snippets.

@skuroda
Created March 16, 2013 18:15
Show Gist options
  • Save skuroda/5177615 to your computer and use it in GitHub Desktop.
Save skuroda/5177615 to your computer and use it in GitHub Desktop.
import sublime
import sublime_plugin
import re
class CustomMoveToEndLineCommand(sublime_plugin.TextCommand):
prev_cursors_list = []
custom = True
def run(self, edit, extend=False):
view = self.view
cursors = view.sel()
cursors_list = []
for i in range(len(cursors)):
cursors_list.append(cursors[i])
if cursors_list == self.prev_cursors_list:
if self.custom:
self._move_to_end(view, extend)
else:
self._custom_move_to_end(view, extend)
self.custom = not self.custom
else:
self._custom_move_to_end(view, extend)
self.custom = True
def _move_to_end(self, view, extend):
view.run_command("move_to", {"to": "eol", "extend": extend})
def _custom_move_to_end(self, view, extend):
cursors = view.sel()
new_cursor_pos = []
for cursor in cursors:
line = view.line(cursor)
word_region = view.word(line.end())
word = view.substr(word_region)
if "\n" in word:
word_region = sublime.Region(word_region.begin(),
word_region.end() - 1)
word = view.substr(word_region)
if re.match("^\s+$", word) and line != word_region:
if extend:
new_cursor_pos.append(sublime.Region(cursor.begin(),
word_region.begin()))
else:
new_cursor_pos.append(sublime.Region(word_region.begin(),
word_region.begin()))
else:
if extend:
new_cursor_pos.append(sublime.Region(cursor.begin(),
word_region.end()))
else:
new_cursor_pos.append(sublime.Region(word_region.end(),
word_region.end()))
cursors.clear()
self.prev_cursors_list = new_cursor_pos
for i in range(len(new_cursor_pos)):
cursors.add(new_cursor_pos[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment