"Tabs to Spaces" by simon

Rating: (0 votes)

Report this Script

Name: Tabs to Spaces
Uploaded By: simon
Date Created: May 2, 2009 3:37 a.m.
Date Modified: May 2, 2009 3:37 a.m.
Description: This shows a PyPN implementation of Tabs to Spaces. PN has this built-in, but if you want it to work differently then this script is a good place to start.
Num Views: 399
Num Downloads: 114

Contents

Download this script

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()

Comments: