/
spaces_to_tabs.py
41 lines (35 loc) · 1.47 KB
/
spaces_to_tabs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'''Converts spaces in the document to tabs and vice-versa'''
import re
import tea_actions as tea
# This is a special variable; if it exists in a module, the module will be
# passed the actionObject as the second parameter
req_action_object = True
def act(context, actionObject, operation='entab'):
def replacements(match):
'''Utility function for replacing items'''
return match.group(0).replace(search, replace)
spaces = int(actionObject.userInput().stringValue())
if operation == 'entab':
target = re.compile(r'^(\t* +\t*)+', re.MULTILINE)
search = ' ' * spaces
replace = '\t'
else:
target = re.compile(r'^( *\t+ *)+', re.MULTILINE)
search = '\t'
replace = ' ' * spaces
insertions = tea.new_recipe()
ranges = tea.get_ranges(context)
if len(ranges) == 1 and ranges[0].length == 0:
# No selection, use the document
ranges[0] = tea.new_range(0, context.string().length())
for range in ranges:
text = tea.get_selection(context, range)
# Non-Unix line endings will bork things; convert them
text = tea.unix_line_endings(text)
text = re.sub(target, replacements, text)
if tea.get_line_ending(context) != '\n':
text = tea.clean_line_endings(context, text)
insertions.addReplacementString_forRange_(text, range)
insertions.setUndoActionName_(operation.title())
context.applyTextRecipe_(insertions)
return True