Skip to content

Instantly share code, notes, and snippets.

@tewe

tewe/Indent.py Secret

Created June 1, 2014 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tewe/ef6d2aa6341d8ac2ce54 to your computer and use it in GitHub Desktop.
Save tewe/ef6d2aa6341d8ac2ce54 to your computer and use it in GitHub Desktop.
Indent
import sys
import editor
TAB = ' ' * 4 # configure here
LEN = len(TAB)
try:
start, end = editor.get_selection()
except ValueError:
sys.exit()
line_start, line_end = editor.get_line_selection()
text = editor.get_text()[line_start:line_end]
if sys.argv[1] == '-i':
lines = [TAB + l for l in text.splitlines()]
start += LEN
end += LEN * len(lines)
else:
lines = []
for l in text.splitlines():
if l.startswith(TAB):
l = l[LEN:]
end -= LEN
lines.append(l)
if text.startswith(TAB):
start -= LEN
if text.endswith('\n'):
lines.append('')
text = '\n'.join(lines)
editor.replace_text(line_start, line_end, text)
editor.set_selection(start, end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment