Last active
August 29, 2015 14:01
-
-
Save persquare/1f65f6fcb0ddc41f44c9 to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/env python -S | |
| #encoding: utf-8 | |
| # For TM2:TextTasks | |
| # Settings: | |
| # Input::Line::Text | |
| # output::Replace Selection::Snippet | |
| import os | |
| import sys | |
| import re | |
| tm_support_path = os.environ['TM_BUNDLE_SUPPORT'] | |
| if tm_support_path not in sys.path: | |
| sys.path.insert(0, tm_support_path) | |
| import exit_codes as exit | |
| if os.environ.get('TM_SELECTED_TEXT', None): | |
| # If there is a selection I can't get access to the line?! Bail. | |
| exit.discard() | |
| line = os.environ.get('TM_CURRENT_LINE') | |
| line = line.rstrip('\r\n ') | |
| col = int(os.environ.get('TM_LINE_INDEX')) | |
| # The zero-or-one match '- ?' in the regex below | |
| # is needed since ' ' are stripped off from the end of line above, | |
| # if there was no task, the ' ' after '-' is also removed. | |
| match = re.match(r'^(\s*)- ?(.*)$', line) | |
| if not match: | |
| # Not a task => just insert newline | |
| sys.stdout.write('\n') | |
| else: | |
| # exit.show_tool_tip('<%s>\n<%s>\n<%s>' % (match.group(0), match.group(1), match.group(2))) | |
| # Four cases: | |
| # 1) in leading whitespace => insert new task before | |
| # 2) at end => insert new task after | |
| # 3) inside task => split at cursor with new task after | |
| # 4) at end after empty task => clear empty task and align with leading space | |
| indent = match.group(1) | |
| task = match.group(2) | |
| indent_level = len(indent) | |
| # Case (4) | |
| if not task and col >= indent_level: | |
| exit.replace_text(indent) | |
| # Case (1) | |
| if col <= indent_level: | |
| sys.stdout.write('%s- ${0}\n' % indent[col:]) | |
| # Case (2) & (3) | |
| if col > indent_level: | |
| sys.stdout.write('\n- ${0}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment