def _handleCommand(text, repetitions): """ Handle a simple command """ sci = scintilla.Scintilla(pn.CurrentDoc()) for x in xrange(repetitions): if text == 'j': sci.LineDown() elif text == 'k': sci.LineUp() elif text == 'h': sci.CharLeft() elif text == 'l': sci.CharRight() elif text == '^': sci.Home() elif text == '$': sci.LineEnd() elif text == 'w': sci.WordRight() elif text == 'b': sci.WordLeft() elif text == 'u': sci.Undo() elif text == 'i': return _insertMode(sci) else: return text return ""
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()
def InvertCase(): editor = scintilla.Scintilla(pn.CurrentDoc()) editor.BeginUndoAction() selText = editor.GetTextRange(editor.SelectionStart, editor.SelectionEnd) selText = selText.swapcase() editor.ReplaceSel(selText) editor.EndUndoAction()
def doBase64(): """ This method will grab the curent selection/document and create a new document that is a base64 vesion of the text """ doc = pn.CurrentDoc() if doc is not None: #Lets try not to crash pn too often... editor = scintilla.Scintilla(doc) start = editor.SelectionStart end = editor.SelectionEnd if (start == end): #nothing is selected so we will just grab it all... start = 0 end = editor.Length text = editor.GetTextRange(start, end) newDoc = pn.NewDocument(None) newEditor = scintilla.Scintilla(newDoc) newEditor.BeginUndoAction() encoded = base64.b64encode(text) l = len(encoded) m = 0 while l > 80: str = encoded[m:m + 80] + '\n' newEditor.AppendText(len(str), str) l, m = l - 80, m + 80 str = encoded[m:m + l] newEditor.AppendText(len(str), str) newEditor.EndUndoAction()
def TitleCase(): """ Title case text by Benoit """ editor = scintilla.Scintilla(pn.CurrentDoc()) editor.BeginUndoAction() selText = editor.GetTextRange(editor.SelectionStart, editor.SelectionEnd) selText = selText.title() editor.ReplaceSel(selText) editor.EndUndoAction()
def Capitalize(): """ Capitalise text by Benoit """ editor = scintilla.Scintilla(pn.CurrentDoc()) editor.BeginUndoAction() selText = editor.GetTextRange(editor.SelectionStart, editor.SelectionEnd) selText = selText.capitalize() editor.ReplaceSel(selText) editor.EndUndoAction()
def LowerCase(): """ Convert text to Lower Case by Benoit """ editor = scintilla.Scintilla(pn.CurrentDoc()) editor.BeginUndoAction() selText = editor.GetTextRange(editor.SelectionStart, editor.SelectionEnd) selText = selText.lower() editor.ReplaceSel(selText) editor.EndUndoAction()
def SortLines(): """ Sort Lines (By Scott (wischeese)) """ editor = scintilla.Scintilla(pn.CurrentDoc()) editor.BeginUndoAction() lsSelection = editor.GetTextRange(editor.SelectionStart, editor.SelectionEnd) laLines = lsSelection.splitlines(0) laLines.sort() lsReplace = '\r\n'.join(laLines) editor.ReplaceSel(lsReplace) editor.EndUndoAction()
def ValidateXml(): editor = scintilla.Scintilla(pn.CurrentDoc()) text = editor.GetText(editor.Length) parser = expat.ParserCreate() try: parser.Parse(text, True) except expat.ExpatError as ex: pn.ClearOutput() pn.AddOutput("Error: " + str(ex))
def wrappedScript(): """ Wrap the script function to automatically group undo """ if not auto_undo: f() else: s = scintilla.Scintilla(pn.CurrentDoc()) try: s.BeginUndoAction() f() finally: s.EndUndoAction()
def __init__(self, pndoc=None): """x.__init__(...) initializes x; see x.__class__.__doc__ for signature""" self._closed = False self._softspace = 0 if not pndoc == None: self.doc = pndoc self.__editor = scintilla.Scintilla(self.doc) else: doc = pn.NewDocument(None) self.__editor = scintilla.Scintilla(pn.CurrentDoc()) self.__editor.BeginUndoAction()
def _handleChangeCommand(command, text): """ Handle the c, d and y commands """ m = re.match("^([0-9]+)?((i[w\\(\\[])|[w$ld]|t(.))?$", text) if m == None: return command + text s = scintilla.Scintilla(pn.CurrentDoc()) target = _getTarget(s) try: return _handleChangeCommandInner(command, text, m, s) finally: _setTarget(s, target[0], target[1])
def evalCommandEnter(text): """ Handle a command that the user has pressed Enter on """ slen = len(text) if slen == 0: return "" if text[0] == '/': # Search so = pn.SearchOptions() so.FindText = text[1:] pn.CurrentDoc().FindNext(so) return "" return ""
def undoBase64(): """ This method will grab the curent selection/document and create a new document that is the base64 decoded vesion of the text """ doc = pn.CurrentDoc() if doc is not None: #Lets try not to crash pn too often... editor = scintilla.Scintilla(doc) start = editor.SelectionStart end = editor.SelectionEnd if (start == end): #nothing is selected so we will just grab it all... start = 0 end = editor.Length text = editor.GetTextRange(start, end) decoded = base64.b64decode(text) newDoc = pn.NewDocument(None) newEditor = scintilla.Scintilla(newDoc) newEditor.BeginUndoAction() newEditor.AppendText(len(decoded), decoded) newEditor.EndUndoAction()