I have written my first script to contribute to the Programmer's Notepad community. It converts all identifiers in a file that are in under_score format into PascalCase format.
This was a great project to help me learn more about Programmer's Notepad and scripting the editor and the Scintilla interface with Python. I used the TabsToSpaces script as a starting point, and used the API listing script and the Scintilla documentation to help figure out functions and parameters.
I look forward to writing more scripts and integrating PNotepad more into my daily work. Thank you Simon for putting together such a great little editor!
# PascalCase.py
# convert identifiers from under_score to PascalCase format
# Todd Fiske
# 2009-02-27 15:46:58
import pn
import scintilla
import string
from pypn.decorators import script
#---
# helper functions
def SetTarget(e, x, y):
e.TargetStart = x
e.TargetEnd = y
def Say(s):
pn.AddOutput(s + "\n")
def USToPC(s):
# replace underscores with spaces, title case, replace spaces with no char
return s.replace("_", " ").title().replace(" ", "")
#---
@script("under_score to PascalCase", "Text")
def UnderScoreToPascalCase():
Say("----------")
Say("UnderScoreToPascalCase")
editor = scintilla.Scintilla(pn.CurrentDoc())
start = 0
end = editor.Length
FindRegExp = 2097152
UnderScoreRegEx = "[a-z]+_[a-z_]+"
SetTarget(editor, start, end)
editor.SearchFlags = 0
editor.SearchFlags = FindRegExp
editor.BeginUndoAction()
pos = editor.SearchInTarget(len(UnderScoreRegEx), UnderScoreRegEx)
while (start < end) and (pos > 0):
sUnderScore = editor.GetTextRange(editor.TargetStart, editor.TargetEnd)
sPascalCase = USToPC(sUnderScore)
Say(" %d: %s -> %s" % (editor.TargetStart, sUnderScore, sPascalCase))
editor.ReplaceTarget(-1, sPascalCase)
start = editor.TargetEnd + 2
end = editor.Length
if start < end:
SetTarget(editor, start, end)
pos = editor.SearchInTarget(len(UnderScoreRegEx), UnderScoreRegEx)
editor.EndUndoAction()
Say("done")
###