Tabs to Spaces with PyPN
10 Apr 2007Want to convert your tabs to spaces and annoyed that your editor author hasn’t implemented that feature in a handy menu item yet?
Add a PyPN script to do it:
<code>import pn
import scintilla
from pypn.decorators import script
def SetTarget(e, x, y):
e.TargetStart = x
e.TargetEnd = y
@script("Tabs to Spaces", "Text")
def TabsToSpaces():
editor = scintilla.Scintilla(pn.CurrentDoc())
tabSpaces = editor.TabWidth
spaces = ""
for x in range(tabSpaces):
spaces = spaces + " "
end = editor.Length
SetTarget(editor, 0, end)
editor.SearchFlags = 0
editor.BeginUndoAction()
pos = editor.SearchInTarget(1, "\t")
while(pos != -1):
l1 = editor.TargetEnd - editor.TargetStart
editor.ReplaceTarget(tabSpaces, spaces)
# adjust doc length
end = end + tabSpaces - l1
start = pos + tabSpaces
if start >= end:
pos = -1
else:
SetTarget(editor, start, end)
pos = editor.SearchInTarget(1, "\t")
editor.EndUndoAction()</code>