Created
December 20, 2017 22:36
-
-
Save dantonnoriega/f3baf0ff49b55ad399ff49a60ba2aa66 to your computer and use it in GitHub Desktop.
sublime text plugin to move forward or backward to a given pattern. in this case, i search for "headers" defined as strings "####" or "# TEXT ---------". mostly designed for R.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime, sublime_plugin | |
class MoveForwardToSectionCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
v = self.view | |
s = v.sel() | |
pattern = "^[ ]*# [#]{4,}|^[ ]*[#]{4,}|^[ ]*#+ [\w]+.+[-]+$|^`{3}" | |
# get current point and then look forward | |
first_point = v.line(s[0]).a | |
section_line = v.find(pattern, first_point) | |
# if at section_line header, jump forward | |
print('%d %d' % (first_point, section_line.a)) | |
if first_point == section_line.a: | |
line = v.rowcol(section_line.a)[0] # get line number | |
new_point = v.line(v.text_point(line + 1, 0)).a | |
section_line = v.find(pattern, new_point) | |
s.clear() | |
s.add(section_line.a) | |
v.show_at_center(section_line.a) | |
class MoveBackwardToSectionCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
v = self.view | |
s = v.sel() | |
def get_text(row, col): | |
return v.substr(v.line(v.text_point(row, col))) | |
def search_backward(line, pattern): | |
# print('%d' % line) | |
if pattern.match(get_text(line, 0)): # if line -1 matches, return current line | |
return search_backward(line - 1, pattern) | |
elif pattern.match(get_text(line - 1, 0)): # if the next back line matches stop, jump back 2 | |
top = v.line(v.text_point(line - 1, 0)) | |
return top | |
else: | |
return search_backward(line - 1, pattern) | |
import re | |
pattern = re.compile("^[ ]*# [#]{4,}|^[ ]*[#]{4,}|^[ ]*#+ [\w]+.+[-]+$|^`{3}") | |
# search for closest section top and bottom | |
first_point = v.line(s[0]).a | |
current_line = v.rowcol(first_point)[0] # get line number | |
section_line = search_backward(current_line, pattern) | |
s.clear() | |
s.add(section_line.a) | |
v.show_at_center(section_line.a) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment