############################################################################### # This is a list of a few handy tasks: # # - Jump to Matching Brace # This script will move the cursor to a matching brace. This can be very useful if # a matching brace is not showing on the screen. # # - Select to Matching Brace # This script will move the cursor to a matching brace and select the text between # the two braces. This can be very useful if you want to copy the text between two # braces to another document so you can examine the text in isolation -- maybe for # searching, for example. # # - Count Words in Selection # Count the words in a selecation. This is useful if you want to see how many # words there are in a selected area of a document. A word is a sequence of # non-whitespace characters. # # - Filename to Clipboard # Copies the filename of a document to the clipboard. Clones the functionality # available when you right-click on a tab. # # I make these scripts available in the public domain. There are no restrictions on # their use, and no attribution is necessary. No warranty is made. Use at your own risk. # # Author: Rob Favero # ############################################################################### import pn import scintilla from pypn.decorators import script # # Jump to a matching brace and optionally highlight the text between the # two matching braces. # def GotoToMB(doSelect): doc = pn.CurrentDoc() if doc is None: return editor = scintilla.Scintilla(doc) curPos = editor.CurrentPos # # I need to do something a little funny here. The highlighting of braces occurs if # the cursor is located to either the left of a brace or to the right of # a brace. However, the BraceMatch function in Scintilla only returns a matching # location if the cursor is positioned on the left side of a brace. The following # lines will move the cursor, even if it is positioned to the right of a brace. # Also, I need to account for the case where two braces, either both open braces # or both closed braces, are adjacent and the cursor is positioned between them. # In this situation, the brace to the left of the cursor is the one that gets # highlighted, so the jump needs to be based on the highlighted one. # # To achieve the desired behavior, first see if we can match a brace on the character # to the left of the cursor. If that does not result in a match, try a match on the # character to the right of the cursor. if (curPos > 0): matchPos = editor.BraceMatch(curPos - 1) if (matchPos > -1): if (doSelect): editor.SetSel(curPos, matchPos + 1) else: editor.SetSel(matchPos + 1, matchPos + 1) return matchPos = editor.BraceMatch(curPos) if (matchPos > -1): if (doSelect): editor.SetSel(curPos, matchPos + 1) else: editor.SetSel(matchPos + 1, matchPos + 1) return return # # Move the cursor to a matching brace. # @script("Jump to Matching Brace", "Handy Tasks") def JumpToMB(): GotoToMB(False) return # # Move the cursor to a matching brace and select the text in between. # @script("Select to Matching Brace", "Handy Tasks") def SelectToMB(): GotoToMB(True) return # # Count the number of words (whitespace separated characters) in a selection (or # entire document if there is no selection). # @script("Count Words", "Handy Tasks") def CountWordsInSel(): doc = pn.CurrentDoc() if doc is None: return editor = scintilla.Scintilla(doc) start = editor.SelectionStart end = editor.SelectionEnd if (start == end): start = 0 end = editor.Length text = editor.GetTextRange(start, end) newDoc = pn.NewDocument(None) newEditor = scintilla.Scintilla(newDoc) words = len(text.split()) output = str(words) + " words" newEditor.AppendText(len(output), output) newEditor.SetSavePoint() return # # Copy the filename of the document to the clipboard. # @script("Filename to Clipboard", "Handy Tasks") def GetFileName(): doc = pn.CurrentDoc() if doc is None: return editor = scintilla.Scintilla(doc) output = str(doc.FileName) editor.CopyText(len(output), output) return